Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Object detection models and replace existing models. (Restructuring Pt - 3) #8

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .ci/linux-steps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ steps:
git clone --depth 1 https://github.com/mlpack/jenkins-conf.git conf
git clone --depth 1 https://github.com/mlpack/mlpack.git

mkdir data

sudo add-apt-repository ppa:mhier/libboost-latest
sudo apt-get update

Expand Down Expand Up @@ -45,7 +43,7 @@ steps:
displayName: 'Build models'

# Run CTests.
- script: cd build/tests/ && sudo CTEST_OUTPUT_ON_FAILURE=1 ctest -R UtilsTest
- script: cd build/tests/ && sudo CTEST_OUTPUT_ON_FAILURE=1 ctest -T Test .
displayName: 'Run tests via ctest'

# Publish test results to Azure Pipelines
Expand Down
4 changes: 2 additions & 2 deletions .ci/macos-steps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ steps:

# Configure CMake Args for models.
- script: |
mkdir data && mkdir build && cd build && cmake $(CMakeArgs-models) ..
mkdir build && cd build && cmake $(CMakeArgs-models) ..
displayName: 'CMake for models'

# Build mlpack
- script: cd build && make -j2
displayName: 'Build models'

# Run CTests.
- script: cd build/tests/ && sudo CTEST_OUTPUT_ON_FAILURE=1 ctest -R UtilsTest
- script: cd build/tests/ && sudo CTEST_OUTPUT_ON_FAILURE=1 ctest -T Test .
displayName: 'Run tests via ctest'

# Publish test results to Azure Pipelines
Expand Down
3 changes: 1 addition & 2 deletions .ci/windows-steps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ steps:

- powershell: |
mkdir build
mkdir data
cp $(Agent.ToolsDirectory)\boost_libs\*.* build\
cp $(Agent.ToolsDirectory)\OpenBLAS.0.2.14.1\lib\native\lib\x64\*.* build\
cp $(Agent.ToolsDirectory)\OpenBLAS.0.2.14.1\lib\native\bin\x64\*.* build\
Expand Down Expand Up @@ -134,7 +133,7 @@ steps:
# Run tests via ctest.
- bash: |
cd build/tests
CTEST_OUTPUT_ON_FAILURE=1 ctest -T Test -C Release -R UtilsTest
CTEST_OUTPUT_ON_FAILURE=1 ctest -T Test -C Release .
displayName: 'Run tests via ctest'

# Publish test results to Azure Pipelines
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ xcode*
.idea
cmake-build-*
*.csv
*.tar
*.zip
*.tar.gz
.travis/configs.hpp
Testing/*
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ set(DIRS
utils/
dataloader/
tests/
models/
computer_vision/
)

foreach(dir ${DIRS})
Expand Down
13 changes: 13 additions & 0 deletions computer_vision/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(computer_vision)

add_subdirectory(object_classification/)

# Add directory name to sources.
set(DIR_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/)

foreach(file ${SOURCES})
set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
endforeach()

set(DIRS ${DIRS} ${DIR_SRCS} PARENT_SCOPE)
23 changes: 23 additions & 0 deletions computer_vision/object_classification/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(object_classification)

set(MODEL_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../")

set(SOURCES
object_classification.cpp
)

foreach(file ${SOURCES})
string( REPLACE ".cpp" "" name ${file})
add_executable(${name} ${MODEL_SOURCE_DIR}/${file})
target_link_libraries(${name}
${COMPILER_SUPPORT_LIBRARIES}
${ARMADILLO_LIBRARIES}
${Boost_FILESYSTEM_LIBRARY}
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_SERIALIZATION_LIBRARY}
${MLPACK_LIBRARIES}
)
endforeach()
58 changes: 58 additions & 0 deletions computer_vision/object_classification/object_classification.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @file object_classification.hpp
* @author Kartik Dutt
*
* Contains implementation of object classification suite. It can be used
* to select object classification model, it's parameter dataset and
* other training parameters.
*
* NOTE: This code needs to be adapted as this implementation doesn't support
* Command Line Arguments.
*
* mlpack is free software; you may redistribute it and/or modify it under the
* terms of the 3-clause BSD license. You should have received a copy of the
* 3-clause BSD license along with mlpack. If not, see
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
*/
#include <dataloader/dataloader.hpp>
#include <models/lenet/lenet.hpp>
#include <utils/utils.hpp>
#include <ensmallen.hpp>

using namespace mlpack;
using namespace mlpack::ann;
using namespace arma;
using namespace std;
using namespace ens;

int main()
{
const int EPOCHS = 3;
const double STEP_SIZE = 5e-3;
const int BATCH_SIZE = 32;
const double RATIO = 0.2;

DataLoader<> dataloader("mnist", true, RATIO);

constexpr size_t ver = 5;
LeNet<mlpack::ann::NegativeLogLikelihood<>,
mlpack::ann::RandomInitialization, ver> module1(1, 28, 28, 10);
cout << "Training." << endl;

SGD<AdamUpdate> optimizer(STEP_SIZE, BATCH_SIZE,
EPOCHS * dataloader.TrainLabels().n_cols,
1e-8,
true,
AdamUpdate(1e-8, 0.9, 0.999));

module1.GetModel().Train(dataloader.TrainFeatures(),
dataloader.TrainLabels(),
optimizer,
ens::PrintLoss(),
ens::ProgressBar(),
ens::EarlyStopAtMinLoss());

module1.SaveModel("./../weights/lenet/lenet" + to_string(ver)+"_mnist.bin");

return 0;
}
28,001 changes: 28,001 additions & 0 deletions data/mnist_test.csv

Large diffs are not rendered by default.

42,001 changes: 42,001 additions & 0 deletions data/mnist_train.csv

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions dataloader/dataloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,30 @@ class DataLoader
*/
void DownloadDataset(const std::string& dataset)
{
if (datasetMap[dataset].zipFile && (!Utils::PathExists(
datasetMap[dataset].trainPath) ||
!Utils::PathExists(datasetMap[dataset].testPath)))
{
Utils::DownloadFile(datasetMap[dataset].datasetURL,
datasetMap[dataset].datasetPath, dataset + "_training_data.",
false, false, datasetMap[dataset].serverName,
datasetMap[dataset].zipFile);

if (!Utils::CompareCRC32(datasetMap[dataset].datasetPath,
datasetMap[dataset].datasetHash))
{
mlpack::Log::Fatal << "Corrupted Data for " << dataset <<
" downloaded." << std::endl;
}

return;
}

if (!Utils::PathExists(datasetMap[dataset].trainPath))
{
Utils::DownloadFile(datasetMap[dataset].trainDownloadUrl,
Utils::DownloadFile(datasetMap[dataset].trainDownloadURL,
datasetMap[dataset].trainPath, dataset + "_training_data.",
false);
false, false, datasetMap[dataset].serverName);

if (!Utils::CompareCRC32(datasetMap[dataset].trainPath,
datasetMap[dataset].trainHash))
Expand All @@ -192,11 +211,12 @@ class DataLoader
dataset << " downloaded." << std::endl;
}
}

if (!Utils::PathExists(datasetMap[dataset].testPath))
{
Utils::DownloadFile(datasetMap[dataset].trainDownloadUrl,
Utils::DownloadFile(datasetMap[dataset].trainDownloadURL,
datasetMap[dataset].testPath, dataset + "_testing_data.",
false);
false, false, datasetMap[dataset].serverName);

if (!Utils::CompareCRC32(datasetMap[dataset].testPath,
datasetMap[dataset].testHash))
Expand Down
15 changes: 7 additions & 8 deletions dataloader/dataloader_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ template<
arma::mat trainDataset, validDataset;
data::Split(dataset, trainDataset, validDataset, ratio, shuffle);

if (useScaler)
{
scaler.Fit(trainDataset);
scaler.Transform(trainDataset, trainDataset);
scaler.Transform(validDataset, validDataset);
}

trainFeatures = trainDataset.rows(WrapIndex(startInputFeatures,
trainDataset.n_rows), WrapIndex(endInputFeatures,
trainDataset.n_rows));
Expand All @@ -125,10 +118,16 @@ template<
validDataset.n_rows), WrapIndex(endInputFeatures,
validDataset.n_rows));

validLabels = trainDataset.rows(WrapIndex(startPredictionFeatures,
validLabels = validDataset.rows(WrapIndex(startPredictionFeatures,
validDataset.n_rows), WrapIndex(endPredictionFeatures,
validDataset.n_rows));

if (useScaler)
{
scaler.Fit(trainFeatures);
scaler.Transform(trainFeatures, trainFeatures);
scaler.Transform(validFeatures, validFeatures);
}
// TODO : Add support for augmentation here.
mlpack::Log::Info << "Training Dataset Loaded." << std::endl;
}
Expand Down
Loading