ValueError: Expected input batch_size (192) to match target batch_size (64)

TheDoctor: <p>Hi,<br>
i’m working with an RNN for Image Classification, my problem which i assume is that my loaded Images are 3 Channels RGB, if i only had one channel it would work i guess. Since it has 3 channels the input_batchsize is 3 times higher than the target. Any ideas how i can solve this?</p>
<p>I also checked <a href="https://discuss.pytorch.org/t/valueerror-expected-input-batch-size-324-to-match-target-batch-size-4/24498">this post</a> which helped me already this far. But im stuck with the 3 channels now.</p>
<p>The Input Shape is torch.Size([64, 3, 224, 224])<br>
<strong>X</strong> after permute is torch.Size([224, 192, 224])</p>
<p>My parameters:</p>
<h1>parameters</h1>
<ul>
<li>BATCH_SIZE = 64</li>
<li>N_STEPS = 28</li>
<li>N_INPUTS = 224</li>
<li>N_CHANNELS = 3</li>
<li>N_NEURONS = 150</li>
<li>N_OUTPUTS = 21</li>
<li>N_EPOCHS = 5</li>
<li>N_PIXELS = 224</li>
</ul>
<pre><code class="lang-auto">class ImageRNN(nn.Module):
    def __init__(self, batch_size, n_steps, n_inputs, n_neurons, n_outputs):
        super(ImageRNN, self).__init__()

        self.n_neurons = n_neurons
        self.batch_size = batch_size
        self.n_steps = n_steps
        self.n_inputs = n_inputs
        self.n_outputs = n_outputs

        self.basic_rnn = nn.RNN(self.n_inputs, self.n_neurons)

        self.FC = nn.Linear(self.n_neurons, self.n_outputs)

    def init_hidden(self,):
        # (num_layers, batch_size, n_neurons)
        return (torch.zeros(1, self.batch_size, self.n_neurons))

    def forward(self, X):
        # transforms X to dimensions: n_steps X batch_size X n_inputs
        print(X.shape)
        X = X.permute(1, 0, 2)
        print(X.shape)
        print(X.size)
        self.batch_size = X.size(1)
        self.hidden = self.init_hidden()

        lstm_out, self.hidden = self.basic_rnn(X, self.hidden)
        out = self.FC(self.hidden)

        return out.view(-1, self.n_outputs) # batch_size X n_output


</code></pre>
<pre><code class="lang-auto">    for i, data in enumerate(trainloader):
         # zero the parameter gradients
        optimizer.zero_grad()

        # reset hidden states
        model.hidden = model.init_hidden()

        # get the inputs
        inputs, labels = data
        inputs = inputs.view(-1, N_PIXELS,N_PIXELS)

        # forward + backward + optimize
        outputs = model(inputs)

        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
</code></pre>

chetan_patil: <p>How do you want the size to be after <code>.permute</code> ?</p>

TheDoctor: <p>I want the size to be 64.</p>

chetan_patil: <p>Before <code>permute</code> it was <code>[64,3,224,224]</code>.<br>
After permute, <code>[64, ?, ?]</code> ?</p>
<p>based on your code, the RNN must accept size <code>(64, 224)</code> , how can you squash <code>(64,3,224,224)</code> into <code>(64,224)</code> ?</p>

TheDoctor: <p>Is there anyway i can use the 3 channels without loosing information?</p>

chetan_patil: <p>I’m not sure ny exact way, but you can flatten out <code>X</code> so that its shape now becomes <code>[64,3*224*224]</code>, and change your RNNs input size to be <code>3*224*224</code>.</p>

TheDoctor: <p>Thanks for the Input, i was thinking of reshaping my [64, 3, 224, 224] to a [64, 1, 672, 224]. So i just append the channels. I wouldnt loose information. What do you think about that solution, does it make sense?</p>

chetan_patil: <p>Yes, if <code>X</code> is reshaped to something like <code>[64,1,672,672]</code>, you would again need to arrive at a shape of <code>[64,n_inputs]</code> ie <code>[64, 224]</code> so that it can be fed into the RNN.</p>
<ol>
<li>Reshape it from <code>[64, 3, 224, 224]</code> to <code>[64, 150528]</code>, apply a feed-forward layer to get the desired size for the <code>basic_rnn</code> layer.</li>
<li>Apply 2 or 3 Convolutional layers and bring them to the desired shape.</li>
</ol>
<p>The 2nd approach will learn some spatial features as well, due to the Conv layers.</p>

TheDoctor: <p>Okay, what i did now is before putting my Images Batch into the Network i change<br>
inputs = inputs.view(-1, N_N_PIXELS,N_PIXELS)<br>
to<br>
inputs = inputs.view(-1, N_<strong>CHANNELS*N</strong>_PIXELS,N_PIXELS)</p>
<p>which just appends all channels. Thanks again for your help <a class="mention" href="/u/chetan_patil">@chetan_patil</a>.</p>

chetan_patil: <p>You’re welcome <a class="mention" href="/u/thedoctor">@TheDoctor</a>.</p>