From 13b455e4ba74f14f7b1f9b03c7c1075a200bf9b2 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 14 Apr 2024 00:19:29 +0200 Subject: [PATCH] Implement test multiple inference --- test/model/test_model.cu | 177 ++++++++++++++++++++++++++------------- tools/model_test.py | 35 ++++++++ 2 files changed, 152 insertions(+), 60 deletions(-) diff --git a/test/model/test_model.cu b/test/model/test_model.cu index 0638e45..3d681cd 100644 --- a/test/model/test_model.cu +++ b/test/model/test_model.cu @@ -6,77 +6,75 @@ #include "model.hpp" class ModelTest : public ::testing::Test { - protected: - CUDANet::Model* commonTestSetup( - int inputSize = 6, - int inputChannels = 2, - int outputSize = 3, + protected: + CUDANet::Model *commonTestSetup( + int inputSize = 6, + int inputChannels = 2, + int outputSize = 3, - int kernelSize = 3, - int stride = 1, - int numFilters = 2, + int kernelSize = 3, + int stride = 1, + int numFilters = 2, - int poolingSize = 2, - int poolingStride = 2 - ) { + int poolingSize = 2, + int poolingStride = 2 + ) { + CUDANet::Model *model = + new CUDANet::Model(inputSize, inputChannels, outputSize); - CUDANet::Model *model = new CUDANet::Model(inputSize, inputChannels, outputSize); + // Conv2d + CUDANet::Layers::Conv2d *conv2d = new CUDANet::Layers::Conv2d( + inputSize, inputChannels, kernelSize, stride, numFilters, + CUDANet::Layers::Padding::VALID, + CUDANet::Layers::ActivationType::NONE + ); + // weights 6*6*2*2 + std::vector conv2dWeights = { + 0.18313f, 0.53363f, 0.39527f, 0.27575f, 0.3433f, 0.41746f, + 0.16831f, 0.61693f, 0.54599f, 0.99692f, 0.77127f, 0.25146f, + 0.4206f, 0.16291f, 0.93484f, 0.79765f, 0.74982f, 0.78336f, + 0.6386f, 0.87744f, 0.33587f, 0.9691f, 0.68437f, 0.65098f, + 0.48153f, 0.97546f, 0.8026f, 0.36689f, 0.98152f, 0.37351f, + 0.68407f, 0.2684f, 0.2855f, 0.76195f, 0.67828f, 0.603f + }; + conv2d->setWeights(conv2dWeights.data()); + model->addLayer("conv2d", conv2d); - // Conv2d - CUDANet::Layers::Conv2d *conv2d = new CUDANet::Layers::Conv2d( - inputSize, inputChannels, kernelSize, stride, numFilters, CUDANet::Layers::Padding::VALID, - CUDANet::Layers::ActivationType::NONE + // maxpool2d + CUDANet::Layers::MaxPooling2D *maxpool2d = + new CUDANet::Layers::MaxPooling2D( + inputSize - kernelSize + 1, numFilters, poolingSize, + poolingStride, CUDANet::Layers::ActivationType::RELU ); - // weights 6*6*2*2 - std::vector conv2dWeights = { - 0.18313f, 0.53363f, 0.39527f, 0.27575f, 0.3433f, 0.41746f, - 0.16831f, 0.61693f, 0.54599f, 0.99692f, 0.77127f, 0.25146f, - 0.4206f, 0.16291f, 0.93484f, 0.79765f, 0.74982f, 0.78336f, - 0.6386f, 0.87744f, 0.33587f, 0.9691f, 0.68437f, 0.65098f, - 0.48153f, 0.97546f, 0.8026f, 0.36689f, 0.98152f, 0.37351f, - 0.68407f, 0.2684f, 0.2855f, 0.76195f, 0.67828f, 0.603f - }; - conv2d->setWeights(conv2dWeights.data()); - model->addLayer("conv2d", conv2d); + model->addLayer("maxpool2d", maxpool2d); - // maxpool2d - CUDANet::Layers::MaxPooling2D *maxpool2d = new CUDANet::Layers::MaxPooling2D( - inputSize - kernelSize + 1, numFilters, poolingSize, poolingStride, CUDANet::Layers::ActivationType::RELU - ); - model->addLayer("maxpool2d", maxpool2d); + // dense + CUDANet::Layers::Dense *dense = new CUDANet::Layers::Dense( + 8, 3, CUDANet::Layers::ActivationType::SOFTMAX + ); + // dense weights 18*6 + std::vector denseWeights = { + 0.36032f, 0.33115f, 0.02948f, 0.09802f, 0.45072f, 0.56266f, + 0.43514f, 0.80946f, 0.43439f, 0.90916f, 0.08605f, 0.07473f, + 0.94788f, 0.66168f, 0.34927f, 0.09464f, 0.61963f, 0.73775f, + 0.51559f, 0.81916f, 0.64915f, 0.03934f, 0.87608f, 0.68364f, + }; + dense->setWeights(denseWeights.data()); + model->addLayer("dense", dense); - // dense - CUDANet::Layers::Dense *dense = new CUDANet::Layers::Dense( - 8, 3, CUDANet::Layers::ActivationType::SOFTMAX - ); - // dense weights 18*6 - std::vector denseWeights = { - 0.36032f, 0.33115f, 0.02948f, - 0.09802f, 0.45072f, 0.56266f, - 0.43514f, 0.80946f, 0.43439f, - 0.90916f, 0.08605f, 0.07473f, - 0.94788f, 0.66168f, 0.34927f, - 0.09464f, 0.61963f, 0.73775f, - 0.51559f, 0.81916f, 0.64915f, - 0.03934f, 0.87608f, 0.68364f, - }; - dense->setWeights(denseWeights.data()); - model->addLayer("dense", dense); + return model; + } - return model; - } + void commonTestTeardown(CUDANet::Model *model) { + delete model; + } - void commonTestTeardown(float* d_input) { - cudaDeviceReset(); - } - - cudaError_t cudaStatus; + cudaError_t cudaStatus; }; TEST_F(ModelTest, TestModelPredict) { - - int outputSize = 3; - CUDANet::Model *model = commonTestSetup(); + int outputSize = 3; + CUDANet::Model *model = commonTestSetup(); // input 6*6*2 std::vector input = { @@ -96,7 +94,7 @@ TEST_F(ModelTest, TestModelPredict) { std::vector expected = {2e-05f, 0.00021f, 0.99977f}; // predict - const float* output = model->predict(input.data()); + const float *output = model->predict(input.data()); float sum = 0.0f; for (int i = 0; i < outputSize; ++i) { @@ -105,4 +103,63 @@ TEST_F(ModelTest, TestModelPredict) { } EXPECT_NEAR(sum, 1.0f, 1e-5f); +} + +TEST_F(ModelTest, TestModelPredictMultiple) { + int outputSize = 3; + CUDANet::Model *model = commonTestSetup(); + + std::vector input_1 = { + 0.81247f, 0.03579f, 0.26577f, 0.80374f, 0.64584f, 0.19658f, 0.04817f, + 0.50769f, 0.33502f, 0.01739f, 0.32263f, 0.69625f, 0.07433f, 0.98283f, + 0.21217f, 0.48437f, 0.58012f, 0.86991f, 0.81879f, 0.63589f, 0.30264f, + 0.90318f, 0.12978f, 0.35972f, 0.95847f, 0.58633f, 0.55025f, 0.68302f, + 0.61422f, 0.79524f, 0.7205f, 0.72481f, 0.51553f, 0.83032f, 0.23561f, + 0.80631f, 0.23548f, 0.84634f, 0.05839f, 0.76526f, 0.39654f, 0.95635f, + 0.75422f, 0.75341f, 0.82431f, 0.79371f, 0.72413f, 0.88557f, 0.33594f, + 0.56363f, 0.12415f, 0.05635f, 0.15952f, 0.27887f, 0.05417f, 0.58474f, + 0.75129f, 0.1788f, 0.88958f, 0.49793f, 0.85386f, 0.86262f, 0.05568f, + 0.16811f, 0.72188f, 0.08683f, 0.66985f, 0.62707f, 0.4035f, 0.51822f, + 0.46545f, 0.88722f + }; + + std::vector expected_1 = {5e-05f, 0.00033f, 0.99962f}; + + // predict + const float *output_1 = model->predict(input_1.data()); + + float sum_1 = 0.0f; + for (int i = 0; i < outputSize; ++i) { + EXPECT_NEAR(expected_1[i], output_1[i], 1e-5f); + sum_1 += output_1[i]; + } + + EXPECT_NEAR(sum_1, 1.0f, 1e-5f); + + std::vector input_2 = { + 0.83573f, 0.19191f, 0.16004f, 0.27137f, 0.64768f, 0.38417f, 0.02167f, + 0.28834f, 0.21401f, 0.16624f, 0.12037f, 0.12706f, 0.3588f, 0.10685f, + 0.49224f, 0.71267f, 0.17677f, 0.29276f, 0.92467f, 0.76689f, 0.8209f, + 0.82226f, 0.11581f, 0.6698f, 0.01109f, 0.47085f, 0.44912f, 0.45936f, + 0.83645f, 0.83272f, 0.81693f, 0.97726f, 0.60649f, 0.9f, 0.37f, + 0.20517f, 0.81921f, 0.83573f, 0.00271f, 0.30453f, 0.78925f, 0.8453f, + 0.80416f, 0.09041f, 0.0802f, 0.98408f, 0.19746f, 0.25598f, 0.09437f, + 0.27681f, 0.92053f, 0.35385f, 0.17389f, 0.14293f, 0.60151f, 0.12338f, + 0.81858f, 0.56294f, 0.97378f, 0.93272f, 0.36075f, 0.64944f, 0.2433f, + 0.66075f, 0.64496f, 0.1191f, 0.66261f, 0.63431f, 0.7137f, 0.14851f, + 0.84456f, 0.44482f + }; + + std::vector expected_2 = {5e-05f, 0.0001f, 0.99985f}; + + // predict + const float *output_2 = model->predict(input_2.data()); + + float sum_2 = 0.0f; + for (int i = 0; i < outputSize; ++i) { + EXPECT_NEAR(expected_2[i], output_2[i], 1e-5f); + sum_2 += output_2[i]; + } + + EXPECT_NEAR(sum_2, 1.0f, 1e-5f); } \ No newline at end of file diff --git a/tools/model_test.py b/tools/model_test.py index 3373095..bafb57e 100644 --- a/tools/model_test.py +++ b/tools/model_test.py @@ -84,5 +84,40 @@ if __name__ == "__main__": # input = torch.rand(2, 6, 6) + print("Single test output:") + out = model(input) + utils.print_cpp_vector(out) + + print("Multiple predict test output 1:") + input = torch.tensor([ + 0.81247, 0.03579, 0.26577, 0.80374, 0.64584, 0.19658, 0.04817, + 0.50769, 0.33502, 0.01739, 0.32263, 0.69625, 0.07433, 0.98283, + 0.21217, 0.48437, 0.58012, 0.86991, 0.81879, 0.63589, 0.30264, + 0.90318, 0.12978, 0.35972, 0.95847, 0.58633, 0.55025, 0.68302, + 0.61422, 0.79524, 0.7205, 0.72481, 0.51553, 0.83032, 0.23561, + 0.80631, 0.23548, 0.84634, 0.05839, 0.76526, 0.39654, 0.95635, + 0.75422, 0.75341, 0.82431, 0.79371, 0.72413, 0.88557, 0.33594, + 0.56363, 0.12415, 0.05635, 0.15952, 0.27887, 0.05417, 0.58474, + 0.75129, 0.1788, 0.88958, 0.49793, 0.85386, 0.86262, 0.05568, + 0.16811, 0.72188, 0.08683, 0.66985, 0.62707, 0.4035, 0.51822, + 0.46545, 0.88722 + ]).reshape(2, 6, 6) + out = model(input) + utils.print_cpp_vector(out) + + print("Multiple predict test output 2:") + input = torch.tensor([ + 0.83573, 0.19191, 0.16004, 0.27137, 0.64768, 0.38417, 0.02167, + 0.28834, 0.21401, 0.16624, 0.12037, 0.12706, 0.3588, 0.10685, + 0.49224, 0.71267, 0.17677, 0.29276, 0.92467, 0.76689, 0.8209, + 0.82226, 0.11581, 0.6698, 0.01109, 0.47085, 0.44912, 0.45936, + 0.83645, 0.83272, 0.81693, 0.97726, 0.60649, 0.9, 0.37, + 0.20517, 0.81921, 0.83573, 0.00271, 0.30453, 0.78925, 0.8453, + 0.80416, 0.09041, 0.0802, 0.98408, 0.19746, 0.25598, 0.09437, + 0.27681, 0.92053, 0.35385, 0.17389, 0.14293, 0.60151, 0.12338, + 0.81858, 0.56294, 0.97378, 0.93272, 0.36075, 0.64944, 0.2433, + 0.66075, 0.64496, 0.1191, 0.66261, 0.63431, 0.7137, 0.14851, + 0.84456, 0.44482 + ]).reshape(2, 6, 6) out = model(input) utils.print_cpp_vector(out)