From 3955ddd888c3e03c0ce87ebde0934c69bc72420f Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 27 May 2024 21:30:42 +0200 Subject: [PATCH] Implement Inception block D --- examples/inception_v3/inception_v3.cpp | 154 +++++++++++++++++++++++++ include/layers/max_pooling.cuh | 10 +- 2 files changed, 159 insertions(+), 5 deletions(-) diff --git a/examples/inception_v3/inception_v3.cpp b/examples/inception_v3/inception_v3.cpp index 243f18a..117b0f8 100644 --- a/examples/inception_v3/inception_v3.cpp +++ b/examples/inception_v3/inception_v3.cpp @@ -39,6 +39,11 @@ class BasicConv2d : public CUDANet::Module { addLayer(prefix + ".bn", batchNorm); } + ~BasicConv2d() { + delete conv; + delete batchNorm; + } + float *forward(const float *d_input) { float *d_output = conv->forward(d_input); return batchNorm->forward(d_output); @@ -123,6 +128,20 @@ class InceptionA : public CUDANet::Module { ); } + ~InceptionA() { + delete branch1x1; + delete branch5x5_1; + delete branch5x5_2; + delete branch3x3dbl_1; + delete branch3x3dbl_2; + delete branch3x3dbl_3; + delete branchPool_1; + delete branchPool_2; + delete concat_1; + delete concat_2; + delete concat_3; + } + float *forward(const float *d_input) { float *d_branch1x1_out = branch1x1->forward(d_input); @@ -210,6 +229,16 @@ class InceptionB : public CUDANet::Module { ); } + ~InceptionB() { + delete branch3x3; + delete branch3x3dbl_1; + delete branch3x3dbl_2; + delete branch3x3dbl_3; + delete branchPool; + delete concat_1; + delete concat_2; + } + float *forward(const float *d_input) { float *d_branch3x3_out = branch3x3->forward(d_input); @@ -314,6 +343,23 @@ class InceptionC : public CUDANet::Module { ); } + ~InceptionC() { + delete branch1x1; + delete branch7x7_1; + delete branch7x7_2; + delete branch7x7_3; + delete branch7x7dbl_1; + delete branch7x7dbl_2; + delete branch7x7dbl_3; + delete branch7x7dbl_4; + delete branch7x7dbl_5; + delete branchPool_1; + delete branchPool_2; + delete concat_1; + delete concat_2; + delete concat_3; + } + float *forward(const float *d_input) { float *branch1x1_output = branch1x1->forward(d_input); @@ -359,4 +405,112 @@ class InceptionC : public CUDANet::Module { CUDANet::Layers::Concat *concat_1; CUDANet::Layers::Concat *concat_2; CUDANet::Layers::Concat *concat_3; +}; + +class InceptionD : public CUDANet::Module { + public: + InceptionD( + const shape2d inputSize, + const int inputChannels, + const std::string &prefix + ) + : inputSize(inputSize), inputChannels(inputChannels) { + // Branch 3x3 + branch3x3_1 = new BasicConv2d( + inputSize, inputChannels, 192, {1, 1}, {1, 1}, {0, 0}, + prefix + "branch3x3" + ); + addLayer("", branch3x3_1); + branch3x3_2 = new BasicConv2d( + inputSize, 192, 320, {3, 3}, {2, 2}, {0, 0}, + prefix + "branch3x3_2" + ); + addLayer("", branch3x3_2); + + // Branch 7x7x3 + branch7x7x3_1 = new BasicConv2d( + inputSize, inputChannels, 192, {1, 1}, {1, 1}, {0, 0}, + prefix + "branch7x7x3_1" + ); + addLayer("", branch7x7x3_1); + branch7x7x3_2 = new BasicConv2d( + inputSize, 192, 192, {1, 7}, {1, 1}, {0, 3}, + prefix + "branch7x7x3_2" + ); + addLayer("", branch7x7x3_2); + branch7x7x3_3 = new BasicConv2d( + inputSize, 192, 192, {7, 1}, {1, 1}, {3, 0}, + prefix + "branch7x7x3_3" + ); + addLayer("", branch7x7x3_3); + branch7x7x3_4 = new BasicConv2d( + inputSize, 192, 192, {3, 3}, {2, 2}, {0, 0}, + prefix + "branch7x7x3_4" + ); + addLayer("", branch7x7x3_4); + + // Branch Pool + branchPool = new CUDANet::Layers::MaxPooling2d( + inputSize, 192, {3, 3}, {2, 2}, {0, 0}, + CUDANet::Layers::ActivationType::NONE + ); + addLayer("", branchPool); + + // Concat + concat_1 = new CUDANet::Layers::Concat( + branch3x3_2->getOutputSize(), + branch7x7x3_4->getOutputSize() + ); + concat_2 = new CUDANet::Layers::Concat( + concat_1->getOutputSize(), + branchPool->getOutputSize() + ); + } + + ~InceptionD() { + delete branch3x3_1; + delete branch3x3_2; + delete branch7x7x3_1; + delete branch7x7x3_2; + delete branch7x7x3_3; + delete branch7x7x3_4; + delete branchPool; + delete concat_1; + delete concat_2; + } + + float *forward(float *d_input) { + float *branch1x1_output = branch3x3_1->forward(d_input); + branch1x1_output = branch3x3_2->forward(branch1x1_output); + + float *branch7x7_output = branch7x7x3_1->forward(d_input); + branch7x7_output = branch7x7x3_2->forward(branch7x7_output); + branch7x7_output = branch7x7x3_3->forward(branch7x7_output); + branch7x7_output = branch7x7x3_4->forward(branch7x7_output); + + float *branchPool_output = branchPool->forward(d_input); + + float *d_output = concat_1->forward(branch1x1_output, branch7x7_output); + d_output = concat_2->forward(d_output, branchPool_output); + + return d_output; + } + + private: + shape2d inputSize; + int inputChannels; + + BasicConv2d *branch3x3_1; + BasicConv2d *branch3x3_2; + + BasicConv2d *branch7x7x3_1; + BasicConv2d *branch7x7x3_2; + BasicConv2d *branch7x7x3_3; + BasicConv2d *branch7x7x3_4; + + CUDANet::Layers::MaxPooling2d *branchPool; + + CUDANet::Layers::Concat *concat_1; + CUDANet::Layers::Concat *concat_2; + }; \ No newline at end of file diff --git a/include/layers/max_pooling.cuh b/include/layers/max_pooling.cuh index 7f86515..af52aae 100644 --- a/include/layers/max_pooling.cuh +++ b/include/layers/max_pooling.cuh @@ -9,11 +9,11 @@ namespace CUDANet::Layers { class MaxPooling2d : public SequentialLayer, public TwoDLayer { public: MaxPooling2d( - shape2d inputSize, + shape2d inputSize, int nChannels, - shape2d poolingSize, - shape2d stride, - shape2d padding, + shape2d poolingSize, + shape2d stride, + shape2d padding, ActivationType activationType ); ~MaxPooling2d(); @@ -38,7 +38,7 @@ class MaxPooling2d : public SequentialLayer, public TwoDLayer { private: shape2d inputSize; - int nChannels; + int nChannels; shape2d poolingSize; shape2d stride; shape2d padding;