Skip to content

Commit

Permalink
[resnet] Add test to resnet application
Browse files Browse the repository at this point in the history
This patch adds test check on the training of the resnet application.
This test can be enabled with enable-long-test and enable-test.

Resolves #1533

Signed-off-by: Parichay Kapoor <[email protected]>
  • Loading branch information
kparichay authored and jijoongmoon committed Sep 23, 2021
1 parent d0f3db7 commit 118483a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
37 changes: 36 additions & 1 deletion Applications/Resnet/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <sstream>
#include <vector>

#if defined(ENABLE_TEST)
#include <gtest/gtest.h>
#endif

#include <layer.h>
#include <model.h>
#include <optimizer.h>
Expand All @@ -29,6 +33,10 @@ using ModelHandle = std::unique_ptr<ml::train::Model>;

using UserDataType = std::unique_ptr<nntrainer::resnet::DataLoader>;

/** cache loss values post training for test */
float training_loss = 0.0;
float validation_loss = 0.0;

/**
* @brief make "key=value" from key and value
*
Expand Down Expand Up @@ -210,6 +218,13 @@ int validData_cb(float **input, float **label, bool *last, void *user_data) {
return 0;
}

#if defined(ENABLE_TEST)
TEST(Resnet_Training, verify_accuracy) {
EXPECT_FLOAT_EQ(training_loss, 4.389328);
EXPECT_FLOAT_EQ(validation_loss, 11.611803);
}
#endif

/// @todo maybe make num_class also a parameter
void createAndRun(unsigned int epochs, unsigned int batch_size,
UserDataType &train_user_data,
Expand Down Expand Up @@ -243,6 +258,10 @@ void createAndRun(unsigned int epochs, unsigned int batch_size,
std::move(dataset_valid));

model->train();
#if defined(ENABLE_TEST)
training_loss = model->getTrainingLoss();
validation_loss = model->getValidationLoss();
#endif
}

std::array<UserDataType, 2>
Expand Down Expand Up @@ -327,5 +346,21 @@ int main(int argc, char *argv[]) {
std::cout << "finished computation at " << std::ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "s\n";

return 0;
int status = 0;
#if defined(ENABLE_TEST)
try {
testing::InitGoogleTest(&argc, argv);
} catch (...) {
std::cerr << "Error duing InitGoogleTest" << std::endl;
return 0;
}

try {
status = RUN_ALL_TESTS();
} catch (...) {
std::cerr << "Error duing RUN_ALL_TSETS()" << std::endl;
}
#endif

return status;
}
16 changes: 13 additions & 3 deletions Applications/Resnet/jni/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ resnet_sources = [
'cifar_dataloader.cpp'
]

resnet_dependencies = [app_utils_dep,
iniparser_dep,
nntrainer_dep,
nntrainer_ccapi_dep
]

if get_option('enable-test')
resnet_dependencies += [gtest_dep]
endif

e = executable('nntrainer_resnet18',
resnet_sources,
include_directories: '.',
dependencies: [app_utils_dep, iniparser_dep, nntrainer_dep, nntrainer_ccapi_dep],
dependencies: resnet_dependencies,
install: get_option('install-app'),
install_dir: application_install_dir
)


if get_option('enable-long-test')
testenv = environment()
testenv.set('OPENBLAS_NUM_THREADS', 1)
test('app_resnet18', e, args: ['fake', '1', '512'], env: testenv, timeout: 300)
testenv.set('OPENBLAS_NUM_THREADS', '4')
test('app_resnet18', e, args: ['fake', '1', '128', '1'], env: testenv, timeout: 300)
endif
6 changes: 3 additions & 3 deletions nntrainer/tensor/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,9 @@ Tensor Tensor::sum(const std::vector<unsigned int> &axes, float alpha) const {
return sum(axes, ret, alpha);
}

void Tensor::merge_axis(unsigned int axis1, unsigned int axis2) {
void Tensor::mergeAxis(unsigned int axis1, unsigned int axis2) {
if (axis2 != axis1 + 1)
throw std::invalid_argument("Axis to be merged must be continuous.");
throw std::invalid_argument("axis2 must be axis1 + 1 for merging.");

dim.setTensorDim(axis2, dim.getTensorDim(axis1) * dim.getTensorDim(axis2));
dim.setTensorDim(axis1, 1);
Expand All @@ -761,7 +761,7 @@ Tensor &Tensor::sum(const std::vector<unsigned int> &axes, Tensor &output,
std::vector<unsigned int> new_axes = {axes[0]};
for (unsigned int i = 1; i < axes.size(); ++i) {
if (axes[i] == axes[i - 1] + 1) {
new_reshaped.merge_axis(axes[i - 1], axes[i]);
new_reshaped.mergeAxis(axes[i - 1], axes[i]);
new_axes.back() = axes[i];
} else {
new_axes.push_back(axes[i]);
Expand Down
2 changes: 1 addition & 1 deletion nntrainer/tensor/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ class Tensor {
* @param axis1 first axis to merge
* @param axis2 second axis to merge
*/
void merge_axis(unsigned int axis1, unsigned int axis2);
void mergeAxis(unsigned int axis1, unsigned int axis2);
}; // namespace nntrainer

/**
Expand Down

0 comments on commit 118483a

Please sign in to comment.