Replicate paper CNN by using pytorch

Zila_Tajudin: <p>I am trying to replicate CNN model based on paper that I found.<br>
But during training, I didn’t get the same result as the author said.<br>
This is the paper model.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/e/9e291562e2e87cbad2f2a415fae9b3f1733aa0b4.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/9e291562e2e87cbad2f2a415fae9b3f1733aa0b4" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e291562e2e87cbad2f2a415fae9b3f1733aa0b4_2_690x238.jpeg" alt="image" data-base62-sha1="mz9sBERoF5ePNASNCBPfREQpclS" width="690" height="238" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e291562e2e87cbad2f2a415fae9b3f1733aa0b4_2_690x238.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e291562e2e87cbad2f2a415fae9b3f1733aa0b4_2_1035x357.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e291562e2e87cbad2f2a415fae9b3f1733aa0b4_2_1380x476.jpeg 2x" data-dominant-color="F0F4EE"><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">image</span><span class="informations">1441×499 93.5 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>
<blockquote>
<p>Blockquote<br>
The input of the model is 224x224, the Convl to Conv3 are<br>
the convolution layers, convolution kernel size is 3x3, the<br>
stride value is 1 and use Rectified Linear Unit to activate.<br>
After each convolutional layer, a 3 x 3 maximum pooling layer<br>
is used for down-sampling. To avoid overfitting, the Dropout<br>
method was introduced to the Fcl layer during training to<br>
randomly inactivate many neurons. The Dropout probability<br>
value was set to 0.5 in this method. The activation function<br>
of Fcl layer is Sigmoid, and a Softmax layer is used for<br>
calculating class probability.</p>
</blockquote>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F
train_on_gpu = torch.cuda.is_available()
# define the CNN architecture
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # convolutional layer
        self.conv1_1 = nn.Conv2d(3, 32, 3)
        self.conv1_2 = nn.Conv2d(32, 64, 3)
        # max pooling layer
        self.pool = nn.MaxPool2d(3, 3)
        self.conv2_1 = nn.Conv2d(64, 64, 3)
        self.conv2_2 = nn.Conv2d(64, 64, 3)
        self.conv3_1 = nn.Conv2d(64, 128, 3)
        self.conv3_2 = nn.Conv2d(128, 128, 3)
        self.conv3_3 = nn.Conv2d(128, 128, 3)
        self.dropout = nn.Dropout(0.5)
        self.fc1 = nn.Linear(128*5*5, 1024)
        self.fc2 = nn.Linear(1024, 8)
        self.fc3 = nn.Linear(8, 8)
        self.softmax = nn.Softmax(dim=1)

        
    def forward(self, x):
        # add sequence of convolutional and max pooling layers
        x = F.relu(self.conv1_1(x))
        #print('conv1_1:',x.shape)
        x = self.conv1_2(x)
        x = self.pool(x)
        #print('conv1_2:',x.shape)
        x = F.relu(self.conv2_1(x))
        #print('conv2_1:',x.shape)
        x = self.conv2_2(x)
        #print('conv2_2:',x.shape)
        x = self.pool(x)
        x = F.relu(self.conv3_1(x))
        #print('conv3_1:',x.shape)
        x = self.conv3_2(x)
        #print('conv3_2:',x.shape)
        x = F.relu(self.conv3_3(x))
        #print('conv3_3:',x.shape)
        x = self.pool(x)
        #print('lastpool:',x.shape)
        #x = self.dropout(x)
        #print(x.shape)
        x = x.view(-1,128*5*5)
        #print('reshape:',x.shape)
        x = self.dropout(torch.sigmoid(self.fc1(x)))
        #print('fc1:',x.shape)
        x = self.dropout(torch.sigmoid(self.fc2(x)))
        #print('fc2:',x.shape)
        x = self.softmax(self.fc3(x))
        #print('fc3:',x.shape)
        return x

# create a complete CNN
model = Net()
print(model)

# move tensors to GPU if CUDA is available
if train_on_gpu:
    model.cuda()
    #model=nn.DataParallel(model,device_ids=[0,1])#train on 2 GPU
</code></pre>
<p>Is my code wrong? I try to print each layer output but the size is not the same.<br>
Maybe that is why I didn’t get the same result.<br>
I am beginner at pytorch and python. I study electronics before.</p>

ptrblck: <p>The last <code>nn.Softmax</code> layer could be wrong, if you are using <code>nn.CrossEntropyLoss</code> or <code>nn.NLLLoss</code>.<br>
The former expects raw logits, so remove this activation, while the latter expects log probabilities, so use <code>nn.LogSoftmax</code> instead.</p>
<p>The difference in the spatial size could come from a different padding setup in your conv layers.<br>
Also, the quoted section doesn’t mention anything about the stride of the pooling layers, which would also influence the activation shape.</p>