Giving stride, padding, and kernel_size to the convolution

username: brighteningeyes
cooked: <p>hello,<br>
I want to code my crnn, and I am lost in giving stride, padding and kernel_size to nn.Conv2d<br>
consider that we have an image of 224x224, how would i calculate padding, stride and kernel_size for each convolution layer in such a way that makes it possible to feed that to rnn?<br>
thanks.</p>

username: ptrblck
cooked: <p><code>nn.RNN</code> expects an input tensor in the shape <code>[seq_len, batch_size, features]</code> in the default setup as described in the <a href="https://pytorch.org/docs/stable/nn.html?highlight=rnn#torch.nn.RNN">docs</a>.<br>
The convolution arguments are generally independent from this shape as long as you know, how you would like to reshape the last conv output.<br>
Note that a conv layer will output an activation in the shape <code>[batch_size, nb_filters, height, width]</code>, which you would have to reshape to the expected input shape of the RNN.</p>

username: brighteningeyes
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> thanks.<br>
so, how would I reshape it for feeding it to rnn? I mean, when the<br>
image is not sequenced, how would i make it sequenced in such a way<br>
that sequence length is 1?<br>
because if i reshape without considering the size of the tensor, I<br>
will get error.<br>
thanks.</p>
<p>۱۳۹۹-۰۳-۰۹ ۱۴:۳۵ ‎+۰۴:۳۰ گرینویچ, ptrblck via PyTorch Forums<br>
<a href="mailto:noreply@discuss.pytorch.org">noreply@discuss.pytorch.org</a>:</p>

username: ptrblck
cooked: <p>If you don’t want to create a “temporal” dimension, you could reshape the activation such that the sequence length would be 1, the batch size would stay the same, and all other activation values would be the features via:</p>
<pre><code class="lang-python">out = ... # assuming it has the shape `[batch_size, nb_filters, height, width]
out = out.view(1, out.size(0), -1) # [1, batch_size, nb_filters*height*width]
</code></pre>
<p>However, without a temporal dimension, I’m not sure if an RNN would make much sense.<br>
Could you explain your use case a bit?</p>

username: brighteningeyes
cooked: <p>I have some images of captcha (which are generated by myself) and I<br>
want to find the texts inside it to experiment with simple OCR use<br>
cases.</p>
<p>۱۳۹۹-۰۳-۱۰ ۹:۱۸ ‎+۰۴:۳۰ گرینویچ, ptrblck via PyTorch Forums<br>
<a href="mailto:noreply@discuss.pytorch.org">noreply@discuss.pytorch.org</a>:</p>

username: ptrblck
cooked: <p>In that case you could also try to pass the spatial dimensions (<code>height*width</code>) as the temporal dimension and the channels as the feature dimension.<br>
Still unsure, how well this would work, but might be a reasonable approach.</p>

username: brighteningeyes
cooked: <p>so, the shape for RNN should be [channels, batch_size, width*height]?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="brighteningeyes" data-post="8" data-topic="83305" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brighteningeyes/48/18991_2.png" class="avatar"> brighteningeyes:</div>
<blockquote>
<p>so, the shape for RNN should be [channels, batch_size, width*height]?</p>
</blockquote>
</aside>
<p>No, in the default setup, this shapes is expected:</p>
<aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="83305">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>nn.RNN</code> expects an input tensor in the shape <code>[seq_len, batch_size, features]</code> in the default setup as described in the <a href="https://pytorch.org/docs/stable/nn.html?highlight=rnn#torch.nn.RNN">docs</a>.</p>
</blockquote>
</aside>
<p>If you want to use the pixels as the temporal dimension, you would have to permute them to dim0.<br>
Note that you could also set <code>batch_first</code> during the creation of the RNN, which would then expect the input in the shape <code>[batch_size, seq_len, features]</code>.</p>