mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 01:34:22 +00:00
Add strided conv2d test
This commit is contained in:
@@ -12,9 +12,9 @@ class Conv2dTest : public ::testing::Test {
|
||||
int inputChannels,
|
||||
int kernelSize,
|
||||
int stride,
|
||||
Layers::Padding padding,
|
||||
Layers::Padding padding,
|
||||
int numFilters,
|
||||
Layers::Activation activation,
|
||||
Layers::Activation activation,
|
||||
std::vector<float>& input,
|
||||
float* kernels,
|
||||
float*& d_input,
|
||||
@@ -61,12 +61,12 @@ class Conv2dTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(Conv2dTest, SimpleTest) {
|
||||
int inputSize = 4;
|
||||
int inputChannels = 1;
|
||||
int kernelSize = 2;
|
||||
int stride = 1;
|
||||
int inputSize = 4;
|
||||
int inputChannels = 1;
|
||||
int kernelSize = 2;
|
||||
int stride = 1;
|
||||
Layers::Padding padding = Layers::Padding::VALID;
|
||||
int numFilters = 1;
|
||||
int numFilters = 1;
|
||||
Layers::Activation activation = Layers::Activation::NONE;
|
||||
|
||||
std::vector<float> input = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f,
|
||||
@@ -109,13 +109,13 @@ TEST_F(Conv2dTest, SimpleTest) {
|
||||
commonTestTeardown(d_input, d_output);
|
||||
}
|
||||
|
||||
TEST_F(Conv2dTest, ComplexTest) {
|
||||
int inputSize = 5;
|
||||
int inputChannels = 3;
|
||||
int kernelSize = 3;
|
||||
int stride = 1;
|
||||
TEST_F(Conv2dTest, PaddedTest) {
|
||||
int inputSize = 5;
|
||||
int inputChannels = 3;
|
||||
int kernelSize = 3;
|
||||
int stride = 1;
|
||||
Layers::Padding padding = Layers::Padding::SAME;
|
||||
int numFilters = 2;
|
||||
int numFilters = 2;
|
||||
Layers::Activation activation = Layers::Activation::NONE;
|
||||
|
||||
// clang-format off
|
||||
@@ -191,16 +191,110 @@ TEST_F(Conv2dTest, ComplexTest) {
|
||||
|
||||
// Generated by tools/generate_conv2d_test.py
|
||||
std::vector<float> expected = {
|
||||
2.29426f, 3.89173f, 4.17634f, 3.25501f, 2.07618f, 5.41483f, 7.09971f,
|
||||
6.39811f, 5.71432f, 3.10928f, 5.12973f, 6.29638f, 5.26962f, 5.21997f,
|
||||
3.05852f, 6.17517f, 7.19311f, 6.69771f, 6.2142f, 4.03242f, 3.3792f,
|
||||
4.36444f, 4.396f, 4.69905f, 3.62061f, 2.87914f, 3.71743f, 3.51854f,
|
||||
2.98413f, 1.46579f, 4.94951f, 6.18983f, 4.98187f, 4.38372f, 3.35386f,
|
||||
5.0364f, 5.3756f, 4.05993f, 4.89299f, 2.78625f, 5.33763f, 5.80899f,
|
||||
5.89785f, 5.51095f, 3.74287f, 2.64053f, 4.05895f, 3.96482f, 4.30177f,
|
||||
1.94269f
|
||||
// Channel 1
|
||||
2.29426f, 3.89173f, 4.17634f, 3.25501f, 2.07618f,
|
||||
5.41483f, 7.09971f, 6.39811f, 5.71432f, 3.10928f,
|
||||
5.12973f, 6.29638f, 5.26962f, 5.21997f, 3.05852f,
|
||||
6.17517f, 7.19311f, 6.69771f, 6.2142f, 4.03242f,
|
||||
3.3792f, 4.36444f, 4.396f, 4.69905f, 3.62061f,
|
||||
// Channel 2
|
||||
2.87914f, 3.71743f, 3.51854f, 2.98413f, 1.46579f,
|
||||
4.94951f, 6.18983f, 4.98187f, 4.38372f, 3.35386f,
|
||||
5.0364f, 5.3756f, 4.05993f, 4.89299f, 2.78625f,
|
||||
5.33763f, 5.80899f, 5.89785f, 5.51095f, 3.74287f,
|
||||
2.64053f, 4.05895f, 3.96482f, 4.30177f, 1.94269f
|
||||
};
|
||||
for (int i = 0; i < output.size(); i++) {
|
||||
EXPECT_NEAR(output[i], expected[i], 0.0001f);
|
||||
}
|
||||
}
|
||||
|
||||
commonTestTeardown(d_input, d_output);
|
||||
}
|
||||
|
||||
TEST_F(Conv2dTest, StridedPaddedConvolution) {
|
||||
int inputSize = 5;
|
||||
int inputChannels = 2;
|
||||
int kernelSize = 3;
|
||||
int stride = 2;
|
||||
int numFilters = 2;
|
||||
Layers::Padding padding = Layers::Padding::SAME;
|
||||
Layers::Activation activation = Layers::Activation::RELU;
|
||||
|
||||
// clang-format off
|
||||
std::vector<float> input = {
|
||||
// Channel 1
|
||||
0.946f, 0.879f, 0.382f, 0.542f, 0.453f,
|
||||
0.128f, 0.860f, 0.778f, 0.049f, 0.974f,
|
||||
0.400f, 0.874f, 0.161f, 0.271f, 0.580f,
|
||||
0.373f, 0.078f, 0.366f, 0.396f, 0.181f,
|
||||
0.246f, 0.112f, 0.179f, 0.979f, 0.026f,
|
||||
// Channel 2
|
||||
0.598f, 0.458f, 0.776f, 0.213f, 0.199f,
|
||||
0.853f, 0.170f, 0.609f, 0.269f, 0.777f,
|
||||
0.776f, 0.694f, 0.430f, 0.238f, 0.968f,
|
||||
0.473f, 0.303f, 0.084f, 0.785f, 0.444f,
|
||||
0.464f, 0.413f, 0.779f, 0.298f, 0.783f
|
||||
};
|
||||
std::vector<float> kernels = {
|
||||
// Filter 1, Channel 1
|
||||
0.744f, 0.745f, 0.641f,
|
||||
0.164f, 0.157f, 0.127f,
|
||||
0.732f, 0.761f, 0.601f,
|
||||
// Filter 1, Channel 2
|
||||
0.475f, 0.335f, 0.499f,
|
||||
0.833f, 0.793f, 0.176f,
|
||||
0.822f, 0.163f, 0.175f,
|
||||
// Filter 2, Channel 1
|
||||
0.918f, 0.340f, 0.497f,
|
||||
0.233f, 0.218f, 0.847f,
|
||||
0.931f, 0.926f, 0.199f,
|
||||
// Filter 2, Channel 2
|
||||
0.510f, 0.432f, 0.567f,
|
||||
0.236f, 0.397f, 0.739f,
|
||||
0.939f, 0.891f, 0.006f
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
float* d_input;
|
||||
float* d_output;
|
||||
|
||||
Layers::Conv2d conv2d = commonTestSetup(
|
||||
inputSize, inputChannels, kernelSize, stride, padding, numFilters,
|
||||
activation, input, kernels.data(), d_input, d_output
|
||||
);
|
||||
|
||||
EXPECT_EQ(inputSize, conv2d.outputSize);
|
||||
|
||||
conv2d.forward(d_input, d_output);
|
||||
|
||||
std::vector<float> output(
|
||||
conv2d.outputSize * conv2d.outputSize * numFilters
|
||||
);
|
||||
cudaMemcpy(
|
||||
output.data(), d_output,
|
||||
sizeof(float) * conv2d.outputSize * conv2d.outputSize * numFilters,
|
||||
cudaMemcpyDeviceToHost
|
||||
);
|
||||
|
||||
// Generated by tools/generate_conv2d_test.py
|
||||
std::vector<float> expected = {
|
||||
// Channel 1
|
||||
0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.59803f, 2.84444f, 1.6201f, 0.0f,
|
||||
0.0f, 2.38937f, 3.80762f, 3.39679f, 0.0f,
|
||||
0.0f, 1.13102f, 2.33335f, 1.98488f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
// Channel 2
|
||||
0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 2.57732f, 3.55543f, 2.24675f, 0.0f,
|
||||
0.0f, 3.36842f, 3.41373f, 3.14804f, 0.0f,
|
||||
0.0f, 1.17963f, 2.55005f, 1.63218f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 0.0f, 0.0f
|
||||
};
|
||||
|
||||
for (int i = 0; i < output.size(); i++) {
|
||||
EXPECT_NEAR(output[i], expected[i], 0.0001f);
|
||||
}
|
||||
|
||||
commonTestTeardown(d_input, d_output);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user