title: RuntimeError: size mismatch, m1: [672 x 224], m2: [672 x 224]
body: <p>While working on Udacity's AI course I encountered the error in the title. </p>
  
  <p>At first it looks like the image isn't formatted for the model, however the instruction for the course has us format the images using resize, so I get an image dimension of <code>3x224x224</code>, that's 3 color channels and 224 pixels by 224 pixels. The image array is in the required dimensions, <code>[3,224,224]</code>.</p>
  
  <pre><code>Return/Output:
  (3, 224, 224)
  (3, 224, 224)
  torch.Size([1, 3, 224, 224])
  </code></pre>
  
  <p>Next, I figured it was the model, but when I go back, before the training loop and change the original model to fix this error I get a similar error in revers "up stream".</p>
  
  <p>I want this <code>predict</code> function to use my model to predict the category of an image.</p>
  
  <p>Posted are:</p>
  
  <ul>
  <li>Original classifier</li>
  <li>Save checkpoint block</li>
  <li>Process image function</li>
  <li>Predict image function</li>
  </ul>
  
  <pre class="lang-python prettyprint-override"><code>classifier = nn.Sequential(OrderedDict([
                        ('fc1', nn.Linear(25088, 500)),
                        ('relu', nn.ReLU()),
                        ('drop', nn.Dropout(0.25)),
                        ('fc2', nn.Linear(500, 102)),
                        ('output', nn.LogSoftmax(dim=1))
                        ]))
  
  model.load_state_dict(state_dict)
  
  checkpoint= {'input_size': 672,
              'output_size': 102,
              'hidden_layers': 224,
              'state_dict': model.state_dict()}
  
  torch.save(checkpoint, 'checkpoint.pth')
  
  def predict(image_path, model, topk=5):
  ''' Predict the class (or classes) of an image using a trained deep learning model.
  '''
  
  model= model.cuda()
  model.eval()
  
  # TODO: Implement the code to predict the class from an image file
  
  pil_img= Image.open(image_path)
  #print(pil_img)
  processed_image= process_image(pil_img)
  print(processed_image.shape)
  torch_image= torch.from_numpy(processed_image)
  torch_image= torch_image.unsqueeze_(0)
  print(torch_image.shape)
  torch_image= torch_image.float().to('cuda')
  output= model.forward(torch_image)
  #top_k= predict.topk(topk)
  #print(top_k)
  
  return torch_img
  
  image_path = 'flowers/test/8/image_03299.jpg'
  predict(image_path, model, 5)
  
  
  &gt; RuntimeError                              Traceback (most recent call last)
  &lt;ipython-input-21-d84c5999ad68&gt; in &lt;module&gt;()
       23 
       24 image_path = 'flowers/test/8/image_03299.jpg'
  ---&gt; 25 predict(image_path, model, 5)
  
  &lt;ipython-input-21-d84c5999ad68&gt; in predict(image_path, model, topk)
       16     print(torch_image.shape)
       17     torch_image= torch_image.float().to('cuda')
  ---&gt; 18     output= model.forward(torch_image)
       19     #top_k= predict.topk(topk)
       20     #print(top_k)
  
  /opt/conda/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
       89     def forward(self, input):
       90         for module in self._modules.values():
  ---&gt; 91             input = module(input)
       92         return input
       93 
  
  /opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
      489             result = self._slow_forward(*input, **kwargs)
      490         else:
  --&gt; 491             result = self.forward(*input, **kwargs)
      492         for hook in self._forward_hooks.values():
      493             hook_result = hook(self, input, result)
  
  /opt/conda/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
       53 
       54     def forward(self, input):
  ---&gt; 55         return F.linear(input, self.weight, self.bias)
       56 
       57     def extra_repr(self):
  
  /opt/conda/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
      992         return torch.addmm(bias, input, weight.t())
      993 
  --&gt; 994     output = input.matmul(weight.t())
      995     if bias is not None:
      996         output += bias
  
      RuntimeError                              Traceback (most recent call last)
  &lt;ipython-input-21-d84c5999ad68&gt; in &lt;module&gt;()
       23 
       24 image_path = 'flowers/test/8/image_03299.jpg'
  ---&gt; 25 predict(image_path, model, 5)
  
  &lt;ipython-input-21-d84c5999ad68&gt; in predict(image_path, model, topk)
       16     print(torch_image.shape)
       17     torch_image= torch_image.float().to('cuda')
  ---&gt; 18     output= model.forward(torch_image)
       19     #top_k= predict.topk(topk)
       20     #print(top_k)
  
  /opt/conda/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
       89     def forward(self, input):
       90         for module in self._modules.values():
  ---&gt; 91             input = module(input)
       92         return input
       93 
  
  /opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
      489             result = self._slow_forward(*input, **kwargs)
      490         else:
  --&gt; 491             result = self.forward(*input, **kwargs)
      492         for hook in self._forward_hooks.values():
      493             hook_result = hook(self, input, result)
  
  /opt/conda/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
       53 
       54     def forward(self, input):
  ---&gt; 55         return F.linear(input, self.weight, self.bias)
       56 
       57     def extra_repr(self):
  
  /opt/conda/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
      992         return torch.addmm(bias, input, weight.t())
      993 
  --&gt; 994     output = input.matmul(weight.t())
      995     if bias is not None:
      996         output += bias
  
  RuntimeError: size mismatch, m1: [672 x 224], m2: [672 x 224] at /opt/conda/conda-bld/pytorch_1524584710464/work/aten/src/THC/generic/THCTensorMathBlas.cu:249
  </code></pre>