Convert 3dcnn to pytorch 2dcnn

username: rizal_alfarizi
cooked: <p>hi i read a paper and i want to convert neural network architecure from 3DCNN to 2DCNN<br>
this is the picture of 3dcnn architecture:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/e/1e9887d54d2a85cac58cf8ac65356d309fb13cdf.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/1e9887d54d2a85cac58cf8ac65356d309fb13cdf" title="nntes"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/e/1e9887d54d2a85cac58cf8ac65356d309fb13cdf_2_370x500.jpeg" alt="nntes" data-base62-sha1="4mF7p1AufdKgkbguRGrqwYj8okT" width="370" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/e/1e9887d54d2a85cac58cf8ac65356d309fb13cdf_2_370x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/1/e/1e9887d54d2a85cac58cf8ac65356d309fb13cdf.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/e/1e9887d54d2a85cac58cf8ac65356d309fb13cdf.jpeg 2x" data-dominant-color="E6E7E8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">nntes</span><span class="informations">468×631 112 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>i’m confused because there is no kernel size in conv and how to set param in pytorch PRelu</p>

username: ibro45
cooked: <p>In this paper the dimensions are represented in (H, W, D, C) format (H - height, W - width, D - (temporal) depth, C - channels). So the input, (32, 32, 10, 1) is 10 consecutive grayscale (because C equals 1) frames of size 32x32. Since you can see how the number of channels changes at each level, you know how to define the <code>in_channels</code> and <code>out_channels</code> of <code>nn.Conv3d</code> or <code>nn.Conv2d</code>.</p>
<p>It appears that both Convolution3D layers have kernel sizes <code>1x1x1</code> since they don’t affect the inputs H, W and D size. The kernel size of MaxPooling layers is <code>3x3x3</code>, which can be seen by the difference in feature sizes after they’re applied. Here’s a screenshot that can make it clearer <img src="https://discuss.pytorch.org/uploads/default/original/3X/1/f/1ffd633500b2146f444e15db63b2c1862c74243f.png" alt="Screenshot from 2020-05-09 00-12-14" data-base62-sha1="4yZGh3OXFmEWdybXxL7eGSpdwED" width="471" height="332"></p>
<p>Do note that the dimension order convention in PyTorch is different from the one used in the paper. PyTorch uses (N, C, D, H, W) while the paper uses the TensorFlow convention (N, H, W, D, C). N stand for batch size.</p>
<p><code>nn.PReLU</code>’s <code>num_parameters</code> is gonna be number of channels before it.</p>
<p>Basically, the first three layers of the network are</p>
<pre><code class="lang-auto">nn.Conv3d(in_channels=1, out_channels=32, kernel_size=1)
nn.PReLU(num_parameters=32)
nn.MaxPool3d(kernel_size=3)
</code></pre>
<p>Now you can convert to 2d everything before Dense (nn.Linear) by just replacing 3d with 2d in the name of the layer. For the first Dense layer, you’ll have to check the output shape of the previous layer in order to define it.</p>

username: rizal_alfarizi
cooked: <pre><code class="lang-auto">
class Flatten(nn.Module):
    def forward(self, input):
        return input.view(input.size(0), -1)

class ActionNet(Module):

    def __init__(self, num_class=4):
        super(ActionNet, self).__init__()
        
        self.cnn_layer = Sequential(
            #conv1
            Conv2d(in_channels=1, out_channels=32, kernel_size=1),
            PReLU(num_parameters=32),
            MaxPool2d(kernel_size=3),
            #conv2
            Conv2d(in_channels=32, out_channels=64, kernel_size=1),
            PReLU(num_parameters=64),
            MaxPool2d(kernel_size=3),
            Flatten(),
            Linear(576, 128),
            ReLU(inplace=True),
            Dropout(0.5),
            Linear(128, num_class)
        )
    
    def forward(self, x):

        x = self.cnn_layer(x)
        return x
        

</code></pre>
<p>HI here is my network please correct me if i’m wrong <a class="mention" href="/u/ibro45">@ibro45</a></p>

username: rizal_alfarizi
cooked: <p>oh and also, do i need batch normalization? <a class="mention" href="/u/ibro45">@ibro45</a></p>

username: ibro45
cooked: <p>Seems good! I would use batchnorm, just remember to set <code>bias=False</code> in the convolutional layer <em>before</em> it <a href="https://discuss.pytorch.org/t/any-purpose-to-set-bias-false-in-densenet-torchvision/22067/2" class="inline-onebox">Any purpose to set bias=False in DenseNet torchvision?</a></p>