mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 17:54:27 +00:00
Refactor model test
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "activation.cuh"
|
||||
#include "conv2d.cuh"
|
||||
#include "convolution.cuh"
|
||||
|
||||
@@ -5,22 +5,25 @@
|
||||
#include "max_pooling.cuh"
|
||||
#include "model.hpp"
|
||||
|
||||
TEST(Model, TestModelPredict) {
|
||||
int inputSize = 6;
|
||||
int inputChannels = 2;
|
||||
int outputSize = 3;
|
||||
class ModelTest : public ::testing::Test {
|
||||
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(inputSize, inputChannels, outputSize);
|
||||
CUDANet::Model *model = new CUDANet::Model(inputSize, inputChannels, outputSize);
|
||||
|
||||
// Conv2d
|
||||
CUDANet::Layers::Conv2d conv2d(
|
||||
CUDANet::Layers::Conv2d *conv2d = new CUDANet::Layers::Conv2d(
|
||||
inputSize, inputChannels, kernelSize, stride, numFilters, CUDANet::Layers::Padding::VALID,
|
||||
CUDANet::Layers::ActivationType::NONE
|
||||
);
|
||||
@@ -33,17 +36,17 @@ TEST(Model, TestModelPredict) {
|
||||
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->setWeights(conv2dWeights.data());
|
||||
model->addLayer("conv2d", conv2d);
|
||||
|
||||
// maxpool2d
|
||||
CUDANet::Layers::MaxPooling2D maxpool2d(
|
||||
CUDANet::Layers::MaxPooling2D *maxpool2d = new CUDANet::Layers::MaxPooling2D(
|
||||
inputSize - kernelSize + 1, numFilters, poolingSize, poolingStride, CUDANet::Layers::ActivationType::RELU
|
||||
);
|
||||
model.addLayer("maxpool2d", &maxpool2d);
|
||||
model->addLayer("maxpool2d", maxpool2d);
|
||||
|
||||
// dense
|
||||
CUDANet::Layers::Dense dense(
|
||||
CUDANet::Layers::Dense *dense = new CUDANet::Layers::Dense(
|
||||
8, 3, CUDANet::Layers::ActivationType::SOFTMAX
|
||||
);
|
||||
// dense weights 18*6
|
||||
@@ -57,8 +60,23 @@ TEST(Model, TestModelPredict) {
|
||||
0.51559f, 0.81916f, 0.64915f,
|
||||
0.03934f, 0.87608f, 0.68364f,
|
||||
};
|
||||
dense.setWeights(denseWeights.data());
|
||||
model.addLayer("dense", &dense);
|
||||
dense->setWeights(denseWeights.data());
|
||||
model->addLayer("dense", dense);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
void commonTestTeardown(float* d_input) {
|
||||
cudaDeviceReset();
|
||||
}
|
||||
|
||||
cudaError_t cudaStatus;
|
||||
};
|
||||
|
||||
TEST_F(ModelTest, TestModelPredict) {
|
||||
|
||||
int outputSize = 3;
|
||||
CUDANet::Model *model = commonTestSetup();
|
||||
|
||||
// input 6*6*2
|
||||
std::vector<float> input = {
|
||||
@@ -78,7 +96,7 @@ TEST(Model, TestModelPredict) {
|
||||
std::vector<float> 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) {
|
||||
@@ -87,6 +105,4 @@ TEST(Model, TestModelPredict) {
|
||||
}
|
||||
|
||||
EXPECT_NEAR(sum, 1.0f, 1e-5f);
|
||||
|
||||
cudaDeviceReset();
|
||||
}
|
||||
Reference in New Issue
Block a user