diff --git a/Applications/Resnet/jni/main.cpp b/Applications/Resnet/jni/main.cpp index 45eb0e95e0..7a4f17de90 100644 --- a/Applications/Resnet/jni/main.cpp +++ b/Applications/Resnet/jni/main.cpp @@ -18,6 +18,10 @@ #include #include +#if defined(ENABLE_TEST) +#include +#endif + #include #include #include @@ -29,6 +33,10 @@ using ModelHandle = std::unique_ptr; using UserDataType = std::unique_ptr; +/** 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 * @@ -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, @@ -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 @@ -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; } diff --git a/Applications/Resnet/jni/meson.build b/Applications/Resnet/jni/meson.build index ebeb72fd75..b950d2a27d 100644 --- a/Applications/Resnet/jni/meson.build +++ b/Applications/Resnet/jni/meson.build @@ -3,10 +3,20 @@ 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 ) @@ -14,6 +24,6 @@ e = executable('nntrainer_resnet18', 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 diff --git a/nntrainer/tensor/tensor.cpp b/nntrainer/tensor/tensor.cpp index 38062bd66b..f2de892e6f 100644 --- a/nntrainer/tensor/tensor.cpp +++ b/nntrainer/tensor/tensor.cpp @@ -740,9 +740,9 @@ Tensor Tensor::sum(const std::vector &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); @@ -761,7 +761,7 @@ Tensor &Tensor::sum(const std::vector &axes, Tensor &output, std::vector 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]); diff --git a/nntrainer/tensor/tensor.h b/nntrainer/tensor/tensor.h index b388eb7425..cd56f9ab75 100644 --- a/nntrainer/tensor/tensor.h +++ b/nntrainer/tensor/tensor.h @@ -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 /**