Passing model from keras to Pytorch

username: thatdario
cooked: <p>Hello, I am new to Pytorch (third day using it) and I am finding some trouble converting a model I had in keras. The model is the following:<br>
X_input = Input(shape = (resized_image_size, resized_image_size, 3), name = ‘X_input’)</p>
<pre><code>X = Conv2D(8, (3, 3), strides = (1,1) , padding = 'valid')(X_input)
X = MaxPooling2D(pool_size = (2, 2))(X)

X = Conv2D(16, (3, 3), strides = (1,1) , padding = 'valid')(X)
X = Conv2D(32, (3, 3), strides = (1,1) , padding = 'valid')(X)
X = MaxPooling2D(pool_size = (2, 2))(X)

X = Conv2D(64, (3, 3), strides = (1,1) , padding = 'valid')(X)
X = Conv2D(128, (3, 3), strides = (1,1) , padding = 'valid')(X)
X = MaxPooling2D(pool_size = (2, 2))(X)

X = Flatten()(X)

X = Dense(num_classes * 8 + 20, input_shape = (29 * 29 * 16, 1))(X)
X = Activation('relu')(X)
X = Dropout(0.4)(X)

X = Dense(num_classes * 4 + 10)(X)
X = Activation('relu')(X)

X = Dense(num_classes)(X)
X = Activation('softmax')(X)
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/4/544e9515a21b54fb078c8d4b74c2e10d1ff72299.png" data-download-href="https://discuss.pytorch.org/uploads/default/544e9515a21b54fb078c8d4b74c2e10d1ff72299" title="keras model"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/4/544e9515a21b54fb078c8d4b74c2e10d1ff72299.png" alt="keras model" data-base62-sha1="c1Ot6zbhe3MEJlCqVzdxBRV6ELn" width="456" height="500" data-dominant-color="F1F1F1"><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">keras model</span><span class="informations">680×745 14.8 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>What I have so far in Pytorch is the following:<br>
class NeuralNetwork(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(NeuralNetwork, self).<strong>init</strong>()<br>
self.flatten = nn.Flatten()<br>
self.l1 = nn.Sequential(<br>
nn.Conv2d(1,8,(3,3),1,1),<br>
nn.MaxPool2d(2,2))<br>
self.l2 = nn.Sequential(<br>
nn.Conv2d(8,16,(3,3),1,1),<br>
nn.Conv2d(16,32,(3,3),1,1),<br>
nn.MaxPool2d(2,2))<br>
self.l3 = nn.Sequential(<br>
nn.Conv2d(32,64,(3,3),1,1),<br>
nn.Conv2d(64,128,(3,3),1,1),<br>
nn.MaxPool2d(2,2))<br>
self.l4 = nn.Sequential(<br>
nn.Linear(,),<br>
nn.ReLU(),<br>
nn.Dropout(0.4))<br>
self.l5 = nn.Sequential(<br>
nn.Linear(,),<br>
nn.ReLU())<br>
self.l6 = nn.Sequential(<br>
nn.Linear(,),<br>
nn.Softmax())</p>
<pre><code>def forward(self, x):
    x = self.l1(x)
    x = self.l2(x)
    x = self.l3(x)
    x = self.flatten(x)
    x = self.l4(x)
    x = self.l5(x)
    x = self.l6(x)
    return x
</code></pre>
<p>model = NeuralNetwork().to(device)</p>
<p>I would like to know if the first layers (with the convolutions) are ok and how to calculate the features for Linear since I read that it is the substitute to keras’ Dense.</p>

username: ptrblck
cooked: <p>The first conv layers look alright (your code is a bit hard to read and you can post code snippets by wrapping them into three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"> ).</p>
<aside class="quote no-group" data-username="thatdario" data-post="1" data-topic="143766">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/977dab/48.png" class="avatar"> thatdario:</div>
<blockquote>
<p>how to calculate the features for Linear</p>
</blockquote>
</aside>
<p>You could either calculate them manually, print the activation shape in <code>forward</code> before passing it to the first linear layer and adapt the <code>in_features</code> based on this shape, or use the <code>nn.Lazy...</code> modules.</p>

username: ptrblck
cooked: <p>Forgot to mention one more potential issue: I don’t know which criterion you are using, but in case you are working on a multi-class classification use case with <code>nn.CrossEntropyLoss</code>, remove the last <code>nn.Softmax</code> layer in your PyTorch model as this criterion expects raw logits.</p>