Unify 2d layer naming

This commit is contained in:
2024-05-20 16:23:58 +02:00
parent 74098b24e3
commit 6dca8ccd3c
12 changed files with 41 additions and 41 deletions

View File

@@ -45,7 +45,7 @@ CUDANet::Model *createModel(
CUDANet::Layers::ActivationType::RELU CUDANet::Layers::ActivationType::RELU
); );
model->addLayer("features.0", conv1); // Match pytorch naming model->addLayer("features.0", conv1); // Match pytorch naming
CUDANet::Layers::MaxPooling2D *pool1 = new CUDANet::Layers::MaxPooling2D( CUDANet::Layers::MaxPooling2d *pool1 = new CUDANet::Layers::MaxPooling2d(
56, 64, 3, 2, CUDANet::Layers::ActivationType::NONE 56, 64, 3, 2, CUDANet::Layers::ActivationType::NONE
); );
model->addLayer("pool1", pool1); model->addLayer("pool1", pool1);
@@ -55,7 +55,7 @@ CUDANet::Model *createModel(
27, 64, 5, 1, 192, 2, CUDANet::Layers::ActivationType::RELU 27, 64, 5, 1, 192, 2, CUDANet::Layers::ActivationType::RELU
); );
model->addLayer("features.3", conv2); model->addLayer("features.3", conv2);
CUDANet::Layers::MaxPooling2D *pool2 = new CUDANet::Layers::MaxPooling2D( CUDANet::Layers::MaxPooling2d *pool2 = new CUDANet::Layers::MaxPooling2d(
27, 192, 3, 2, CUDANet::Layers::ActivationType::NONE 27, 192, 3, 2, CUDANet::Layers::ActivationType::NONE
); );
model->addLayer("pool2", pool2); model->addLayer("pool2", pool2);
@@ -77,7 +77,7 @@ CUDANet::Model *createModel(
13, 256, 3, 1, 256, 1, CUDANet::Layers::ActivationType::RELU 13, 256, 3, 1, 256, 1, CUDANet::Layers::ActivationType::RELU
); );
model->addLayer("features.10", conv5); model->addLayer("features.10", conv5);
CUDANet::Layers::MaxPooling2D *pool5 = new CUDANet::Layers::MaxPooling2D( CUDANet::Layers::MaxPooling2d *pool5 = new CUDANet::Layers::MaxPooling2d(
13, 256, 3, 2, CUDANet::Layers::ActivationType::NONE 13, 256, 3, 2, CUDANet::Layers::ActivationType::NONE
); );
model->addLayer("pool5", pool5); model->addLayer("pool5", pool5);

View File

@@ -30,7 +30,7 @@ class BasicConv2d : public CUDANet::Module {
int batchNormSize = conv->getOutputSize(); int batchNormSize = conv->getOutputSize();
CUDANet::Layers::BatchNorm2D *batchNorm = new CUDANet::Layers::BatchNorm2D( CUDANet::Layers::BatchNorm2d *batchNorm = new CUDANet::Layers::BatchNorm2d(
batchNormSize, outputChannels, 1e-3f, batchNormSize, outputChannels, 1e-3f,
CUDANet::Layers::ActivationType::RELU CUDANet::Layers::ActivationType::RELU
); );

View File

@@ -6,16 +6,16 @@
namespace CUDANet::Layers { namespace CUDANet::Layers {
class AvgPooling2D : public SequentialLayer { class AvgPooling2d : public SequentialLayer {
public: public:
AvgPooling2D( AvgPooling2d(
dim2d inputSize, dim2d inputSize,
int nChannels, int nChannels,
dim2d poolingSize, dim2d poolingSize,
dim2d stride, dim2d stride,
ActivationType activationType ActivationType activationType
); );
~AvgPooling2D(); ~AvgPooling2d();
float* forward(const float* d_input); float* forward(const float* d_input);

View File

@@ -8,11 +8,11 @@
namespace CUDANet::Layers { namespace CUDANet::Layers {
class BatchNorm2D : public WeightedLayer { class BatchNorm2d : public WeightedLayer {
public: public:
BatchNorm2D(dim2d inputSize, int inputChannels, float epsilon, ActivationType activationType); BatchNorm2d(dim2d inputSize, int inputChannels, float epsilon, ActivationType activationType);
~BatchNorm2D(); ~BatchNorm2d();
/** /**
* @brief Compute the forward pass of the batchnorm layer * @brief Compute the forward pass of the batchnorm layer

View File

@@ -6,16 +6,16 @@
namespace CUDANet::Layers { namespace CUDANet::Layers {
class MaxPooling2D : public SequentialLayer { class MaxPooling2d : public SequentialLayer {
public: public:
MaxPooling2D( MaxPooling2d(
dim2d inputSize, dim2d inputSize,
int nChannels, int nChannels,
dim2d poolingSize, dim2d poolingSize,
dim2d stride, dim2d stride,
ActivationType activationType ActivationType activationType
); );
~MaxPooling2D(); ~MaxPooling2d();
float* forward(const float* d_input); float* forward(const float* d_input);

View File

@@ -4,7 +4,7 @@
using namespace CUDANet::Layers; using namespace CUDANet::Layers;
AvgPooling2D::AvgPooling2D( AvgPooling2d::AvgPooling2d(
dim2d inputSize, dim2d inputSize,
int nChannels, int nChannels,
dim2d poolingSize, dim2d poolingSize,
@@ -31,12 +31,12 @@ AvgPooling2D::AvgPooling2D(
)); ));
} }
AvgPooling2D::~AvgPooling2D() { AvgPooling2d::~AvgPooling2d() {
cudaFree(d_output); cudaFree(d_output);
delete activation; delete activation;
} }
float* AvgPooling2D::forward(const float* d_input) { float* AvgPooling2d::forward(const float* d_input) {
dim3 block(8, 8, 8); dim3 block(8, 8, 8);
dim3 grid( dim3 grid(
(outputSize.first + block.x - 1) / block.x, (outputSize.first + block.x - 1) / block.x,
@@ -55,10 +55,10 @@ float* AvgPooling2D::forward(const float* d_input) {
return d_output; return d_output;
} }
int AvgPooling2D::getOutputSize() { int AvgPooling2d::getOutputSize() {
return outputSize.first * outputSize.second * nChannels; return outputSize.first * outputSize.second * nChannels;
} }
int AvgPooling2D::getInputSize() { int AvgPooling2d::getInputSize() {
return inputSize.first * inputSize.second * nChannels; return inputSize.first * inputSize.second * nChannels;
} }

View File

@@ -9,7 +9,7 @@
using namespace CUDANet::Layers; using namespace CUDANet::Layers;
BatchNorm2D::BatchNorm2D( BatchNorm2d::BatchNorm2d(
dim2d inputSize, dim2d inputSize,
int inputChannels, int inputChannels,
float epsilon, float epsilon,
@@ -72,7 +72,7 @@ BatchNorm2D::BatchNorm2D(
(inputSize.first * inputSize.second + BLOCK_SIZE - 1) / BLOCK_SIZE; (inputSize.first * inputSize.second + BLOCK_SIZE - 1) / BLOCK_SIZE;
} }
BatchNorm2D::~BatchNorm2D() { BatchNorm2d::~BatchNorm2d() {
cudaFree(d_output); cudaFree(d_output);
cudaFree(d_mean); cudaFree(d_mean);
cudaFree(d_mean_sub); cudaFree(d_mean_sub);
@@ -83,33 +83,33 @@ BatchNorm2D::~BatchNorm2D() {
cudaFree(d_epsilon); cudaFree(d_epsilon);
} }
void BatchNorm2D::initializeWeights() { void BatchNorm2d::initializeWeights() {
std::fill(weights.begin(), weights.end(), 1.0f); std::fill(weights.begin(), weights.end(), 1.0f);
} }
void BatchNorm2D::initializeBiases() { void BatchNorm2d::initializeBiases() {
std::fill(biases.begin(), biases.end(), 0.0f); std::fill(biases.begin(), biases.end(), 0.0f);
} }
void BatchNorm2D::setWeights(const float *weights_input) { void BatchNorm2d::setWeights(const float *weights_input) {
std::copy(weights_input, weights_input + weights.size(), weights.begin()); std::copy(weights_input, weights_input + weights.size(), weights.begin());
toCuda(); toCuda();
} }
std::vector<float> BatchNorm2D::getWeights() { std::vector<float> BatchNorm2d::getWeights() {
return weights; return weights;
} }
void BatchNorm2D::setBiases(const float *biases_input) { void BatchNorm2d::setBiases(const float *biases_input) {
std::copy(biases_input, biases_input + biases.size(), biases.begin()); std::copy(biases_input, biases_input + biases.size(), biases.begin());
toCuda(); toCuda();
} }
std::vector<float> BatchNorm2D::getBiases() { std::vector<float> BatchNorm2d::getBiases() {
return biases; return biases;
} }
void BatchNorm2D::toCuda() { void BatchNorm2d::toCuda() {
CUDA_CHECK(cudaMemcpy( CUDA_CHECK(cudaMemcpy(
d_weights, weights.data(), sizeof(float) * inputChannels, d_weights, weights.data(), sizeof(float) * inputChannels,
cudaMemcpyHostToDevice cudaMemcpyHostToDevice
@@ -120,15 +120,15 @@ void BatchNorm2D::toCuda() {
)); ));
} }
int BatchNorm2D::getInputSize() { int BatchNorm2d::getInputSize() {
return inputSize.first * inputSize.second * inputChannels; return inputSize.first * inputSize.second * inputChannels;
} }
int BatchNorm2D::getOutputSize() { int BatchNorm2d::getOutputSize() {
return inputSize.first * inputSize.second * inputChannels; return inputSize.first * inputSize.second * inputChannels;
} }
float *BatchNorm2D::forward(const float *d_input) { float *BatchNorm2d::forward(const float *d_input) {
// Compute per-channel batch normalization // Compute per-channel batch normalization
for (int i = 0; i < inputChannels; i++) { for (int i = 0; i < inputChannels; i++) {
// Compute mean // Compute mean

View File

@@ -4,7 +4,7 @@
using namespace CUDANet::Layers; using namespace CUDANet::Layers;
MaxPooling2D::MaxPooling2D( MaxPooling2d::MaxPooling2d(
dim2d inputSize, dim2d inputSize,
int nChannels, int nChannels,
dim2d poolingSize, dim2d poolingSize,
@@ -29,12 +29,12 @@ MaxPooling2D::MaxPooling2D(
)); ));
} }
MaxPooling2D::~MaxPooling2D() { MaxPooling2d::~MaxPooling2d() {
cudaFree(d_output); cudaFree(d_output);
delete activation; delete activation;
} }
float* MaxPooling2D::forward(const float* d_input) { float* MaxPooling2d::forward(const float* d_input) {
dim3 block(8, 8, 8); dim3 block(8, 8, 8);
dim3 grid( dim3 grid(
(outputSize.first + block.x - 1) / block.x, (outputSize.first + block.x - 1) / block.x,
@@ -53,10 +53,10 @@ float* MaxPooling2D::forward(const float* d_input) {
return d_output; return d_output;
} }
int MaxPooling2D::getOutputSize() { int MaxPooling2d::getOutputSize() {
return outputSize.first * outputSize.second * nChannels; return outputSize.first * outputSize.second * nChannels;
} }
int MaxPooling2D::getInputSize() { int MaxPooling2d::getInputSize() {
return inputSize.first * inputSize.second * nChannels; return inputSize.first * inputSize.second * nChannels;
} }

View File

@@ -28,7 +28,7 @@ TEST(AvgPoolingLayerTest, AvgPoolForwardTest) {
// clang-format on // clang-format on
}; };
CUDANet::Layers::AvgPooling2D avgPoolingLayer( CUDANet::Layers::AvgPooling2d avgPoolingLayer(
inputSize, nChannels, poolingSize, stride, inputSize, nChannels, poolingSize, stride,
CUDANet::Layers::ActivationType::NONE CUDANet::Layers::ActivationType::NONE
); );

View File

@@ -12,7 +12,7 @@ TEST(BatchNormLayerTest, BatchNormSmallForwardTest) {
cudaError_t cudaStatus; cudaError_t cudaStatus;
CUDANet::Layers::BatchNorm2D batchNorm( CUDANet::Layers::BatchNorm2d batchNorm(
inputSize, nChannels, 1e-5f, CUDANet::Layers::ActivationType::NONE inputSize, nChannels, 1e-5f, CUDANet::Layers::ActivationType::NONE
); );
@@ -69,7 +69,7 @@ TEST(BatchNormLayerTest, BatchNormSmallForwardTest) {
-0.0269f, 0.26878f, 0.81411f, 0.09022f, -0.0269f, 0.26878f, 0.81411f, 0.09022f,
0.9126f, 0.71485f, -0.08184f, -0.19131f}; 0.9126f, 0.71485f, -0.08184f, -0.19131f};
// std::cout << "BatchNorm2D: " << std::endl; // std::cout << "BatchNorm2d: " << std::endl;
for (int i = 0; i < output.size(); i++) { for (int i = 0; i < output.size(); i++) {
EXPECT_NEAR(output[i], expected[i], 1e-5); EXPECT_NEAR(output[i], expected[i], 1e-5);
// std::cout << output[i] << " "; // std::cout << output[i] << " ";

View File

@@ -28,7 +28,7 @@ TEST(MaxPoolingLayerTest, MaxPoolForwardTest) {
// clang-format on // clang-format on
}; };
CUDANet::Layers::MaxPooling2D maxPoolingLayer( CUDANet::Layers::MaxPooling2d maxPoolingLayer(
inputSize, nChannels, poolingSize, stride, inputSize, nChannels, poolingSize, stride,
CUDANet::Layers::ActivationType::NONE CUDANet::Layers::ActivationType::NONE
); );

View File

@@ -42,8 +42,8 @@ class ModelTest : public ::testing::Test {
inputSize.first - kernelSize.first + 1, inputSize.first - kernelSize.first + 1,
inputSize.second - kernelSize.second + 1 inputSize.second - kernelSize.second + 1
}; };
CUDANet::Layers::MaxPooling2D *maxpool2d = CUDANet::Layers::MaxPooling2d *maxpool2d =
new CUDANet::Layers::MaxPooling2D( new CUDANet::Layers::MaxPooling2d(
poolingInput, numFilters, poolingSize, poolingInput, numFilters, poolingSize,
poolingStride, CUDANet::Layers::ActivationType::RELU poolingStride, CUDANet::Layers::ActivationType::RELU
); );