RuntimeError: The size of tensor a (256) must match the size of tensor b (32) at non-singleton dimension 3

Khawar_Islam: <p>I just trained a model and now performing iteration on custom images with different sizes and types (.tif,bmp,and .PNG).</p>
<p>I just load a model and want to generate three images, compressed, original and final images. The below code works fine when i put into training file because where i i gave CIFAR dataset for testing. Now i am giving custom images and make separate file for testing.</p>
<pre><code class="lang-auto">import torch
from model import End_to_end
from torch.autograd import Variable
from loss import loss_function
from grid import save_image
from torchvision import datasets, transforms

CUDA = torch.cuda.is_available()
if CUDA:
    model = End_to_end().cuda()
else:
    model = End_to_end()

EPOCHS = 20

testset = datasets.ImageFolder(root="/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test/", transform=transforms.ToTensor())
model.load_state_dict(torch.load('./checkpoint/model.pth'))
print(testset.imgs)


def test(epoch):
    model.eval()
    test_loss = 0
    for i, (data, _) in enumerate(testset):
        data = Variable(data)
        final, residual_img, upscaled_image, com_img, orig_im = model(data.cuda())
        test_loss += loss_function(final, residual_img, upscaled_image, com_img, orig_im).data
        if epoch == EPOCHS and i == 0:
            #             save_image(final.data[0],'reconstruction_final',nrow=8)
            #             save_image(com_img.data[0],'com_img',nrow=8)
            n = min(data.size(0), 6)
            print("saving the image " + str(n))
            comparison = torch.cat([data[:n], final[:n].cpu()])
            comparison = comparison.cpu()
            #             print(comparison.data)
            save_image(com_img[:n].data, 'compressed_' + str(epoch) + '.png', nrow=n)
            save_image(comparison.data, 'reconstruction_' + str(epoch) + '.png', nrow=n)

    test_loss /= len(testset.dataset)
    print('====&gt; Test set loss: {:.4f}'.format(test_loss))


def save_images():
    epoch = EPOCHS
    model.eval()
    test_loss = 0
    for i, (data, _) in enumerate(testset):
        data = Variable(data).unsqueeze(0)
        final, residual_img, upscaled_image, com_img, orig_im = model(data.cuda())
        test_loss += loss_function(final, residual_img, upscaled_image, com_img, orig_im).data
        if i == 3:
            #             save_image(final.data[0],'reconstruction_final',nrow=8)
            #             save_image(com_img.data[0],'com_img',nrow=8)
            n = min(data.size(0), 6)
            print("saving the image " + str(n))
            comparison = torch.cat([data[:n], final[:n].cpu()])
            comparison = comparison.cpu()
            #             print(comparison.data)
            save_image(com_img[:1].data, './compressed_image/compressed_' + str(i) + '.png', nrow=n)
            save_image(final[:1].data, './final_image/final_' + str(epoch) + '.png', nrow=n)
            save_image(orig_im[:1].data, './orginal_image/original_' + str(epoch) + '.png', nrow=n)

    test_loss /= len(testset.dataset)
    print('====&gt; Test set loss: {:.4f}'.format(test_loss))


save_images()
</code></pre>
<pre><code class="lang-auto">Error
Traceback (most recent call last):
  File "/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test.py", line 74, in &lt;module&gt;
    save_images()
  File "/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test.py", line 51, in save_images
    for i, (data, _) in enumerate(test_loader):
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 345, in __next__
    data = self._next_data()
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/utils/data/dataloader.py", line 385, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/utils/data/_utils/collate.py", line 79, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/utils/data/_utils/collate.py", line 79, in &lt;listcomp&gt;
    return [default_collate(samples) for samples in transposed]
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/utils/data/_utils/collate.py", line 55, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: stack expects each tensor to be equal size, but got [3, 288, 352] at entry 0 and [3, 256, 256] at entry 1
</code></pre>

Nikronic: <p>Hi,</p>
<p>Your custom dataset does not have images with same shape. You can add a <code>Resize</code> as a transform in your dataloader so all images can be stacked to each other when dataloader tries to create a batch.</p>
<aside class="quote no-group" data-username="Khawar_Islam" data-post="1" data-topic="94546">
<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/khawar_islam/48/27640_2.png" class="avatar"> Khawar_Islam:</div>
<blockquote>
<pre><code class="lang-auto">testset = datasets.ImageFolder(root="/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test/", transform=transforms.ToTensor())
</code></pre>
</blockquote>
</aside>
<p>You can use following transform instead:</p>
<pre><code class="lang-auto">tfs = transforms.Compose([
            transforms.Resize(img_size),
            transforms.ToTensor(),
            # normalize,
        ])
</code></pre>
<p>Bests</p>

Khawar_Islam: <p>Traceback (most recent call last):<br>
File “/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test.py”, line 24, in <br>
print(test_loader.dataset)<br>
File “/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torchvision/datasets/vision.py”, line 41, in <strong>repr</strong><br>
body += [repr(self.transforms)]<br>
File “/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torchvision/datasets/vision.py”, line 75, in <strong>repr</strong><br>
"Transform: ")<br>
File “/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torchvision/datasets/vision.py”, line 67, in _format_transform_repr<br>
lines = transform.<strong>repr</strong>().splitlines()<br>
File “/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torchvision/transforms/transforms.py”, line 68, in <strong>repr</strong><br>
format_string += ’    {0}’.format(t)<br>
File “/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torchvision/transforms/transforms.py”, line 201, in <strong>repr</strong><br>
interpolate_str = _pil_interpolation_to_str[self.interpolation]<br>
KeyError: 256</p>

ptrblck: <p>It seems you are passing two arguments to <code>Resize</code>, where the second one will be interpreted as the interpolation method.<br>
If you want to pass the size specifying the height and width, pass it as a tuple:</p>
<pre><code class="lang-python">Resize((256, 256))
</code></pre>

Khawar_Islam: <pre><code class="lang-auto">Traceback (most recent call last):
  File "/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test.py", line 78, in &lt;module&gt;
    save_images()
  File "/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test.py", line 60, in save_images
    final, residual_img, upscaled_image, com_img, orig_im = model(data.cuda())
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/khawar/Desktop/End-to-End_IEEE-TVSCT/model.py", line 64, in forward
    com_img = self.encode(x)
  File "/home/khawar/Desktop/End-to-End_IEEE-TVSCT/model.py", line 43, in encode
    out = self.relu(self.conv1(x))
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/nn/modules/conv.py", line 353, in forward
    return self._conv_forward(input, self.weight)
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/nn/modules/conv.py", line 350, in _conv_forward
    self.padding, self.dilation, self.groups)
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 3, 3, 3], but got 5-dimensional input of size [1, 8, 3, 256, 256] instead
</code></pre>

Khawar_Islam: <p>Complete code. I tried a lot and changes a lot but dimension error remain same</p>
<pre><code class="lang-auto">import torch
from torch.utils.data import DataLoader

from model import End_to_end
from torch.autograd import Variable
from loss import loss_function
from grid import save_image
from torchvision import datasets, transforms

CUDA = torch.cuda.is_available()
if CUDA:
    model = End_to_end().cuda()
else:
    model = End_to_end()

EPOCHS = 5

tfs = transforms.Compose([
    transforms.Resize((256, 256)),
    transforms.ToTensor(),
])
testset = datasets.ImageFolder(root="/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test/", transform=tfs)
test_loader = DataLoader(testset, batch_size=8, shuffle=True)
print(test_loader.dataset)
model.load_state_dict(torch.load('./checkpoint/model.pth'))
print(testset.imgs)


def test(epoch):
    model.eval()
    test_loss = 0
    for i, (data, _) in enumerate(test_loader.dataset.imgs):
        data = Variable(data).unsqueeze(0)
        final, residual_img, upscaled_image, com_img, orig_im = model(data.cuda())
        test_loss += loss_function(final, residual_img, upscaled_image, com_img, orig_im).data
        if epoch == EPOCHS and i == 0:
            #             save_image(final.data[0],'reconstruction_final',nrow=8)
            #             save_image(com_img.data[0],'com_img',nrow=8)
            n = min(data.size(0), 6)
            print("saving the image " + str(n))
            comparison = torch.cat([data[:n], final[:n].cpu()])
            comparison = comparison.cpu()
            #             print(comparison.data)
            save_image(com_img[:n].data, 'compressed_' + str(epoch) + '.png', nrow=n)
            save_image(comparison.data, 'reconstruction_' + str(epoch) + '.png', nrow=n)

    test_loss /= len(testset.test_loader)
    print('====&gt; Test set loss: {:.4f}'.format(test_loss))


def save_images():
    epoch = EPOCHS
    model.eval()
    test_loss = 0
    for i, (data, _) in enumerate(test_loader):
        print(data)
        data = Variable(data).unsqueeze(0)
        print(data.shape)
        print(data.size())
        final, residual_img, upscaled_image, com_img, orig_im = model(data.cuda())
        test_loss += loss_function(final, residual_img, upscaled_image, com_img, orig_im).data
        if i == 3:
            #             save_image(final.data[0],'reconstruction_final',nrow=8)
            #             save_image(com_img.data[0],'com_img',nrow=8)
            n = min(data.size(0), 6)
            print("saving the image " + str(n))
            comparison = torch.cat([data[:n], final[:n].cpu()])
            comparison = comparison.cpu()
            #             print(comparison.data)
            save_image(com_img[:1].data, './compressed_image/compressed_' + str(i) + '.png', nrow=n)
            save_image(final[:1].data, './final_image/final_' + str(epoch) + '.png', nrow=n)
            save_image(orig_im[:1].data, './orginal_image/original_' + str(epoch) + '.png', nrow=n)

    test_loss /= len(test_loader.dataset)
    print('====&gt; Test set loss: {:.4f}'.format(test_loss))


save_images()

</code></pre>

ptrblck: <p>This line of code might yield this error:</p>
<pre><code class="lang-python">data = Variable(data).unsqueeze(0)
</code></pre>
<ol>
<li>
<code>Variables</code> are deprecated since PyTorch <code>0.4</code> so use tensors directly now.</li>
<li>Most likely you don’t need to <code>unsqueeze</code> the data tensor, as the <code>DataLoader</code> would return the batch in the shape <code>[batch_size, channels, height, width]</code>. However, it seems you are somehow avoiding the <code>DataLoader</code> and try to get some images directly via <code>test_loader.dataset.imgs</code>?</li>
</ol>

Khawar_Islam: <p>I just comment out  this line # data = Variable(data)    <span class="hashtag">#unsqueeze</span>(0)<br>
Second i corrected a code by giving correct test_loader. I replaced test_loader.dataset.imgs with test_loader.</p>
<p>Updated Code</p>
<pre><code class="lang-auto">import torch
from torch.utils.data import DataLoader

from model import End_to_end
from torch.autograd import Variable
from loss import loss_function
from grid import save_image
from torchvision import datasets, transforms

CUDA = torch.cuda.is_available()
if CUDA:
    model = End_to_end().cuda()
else:
    model = End_to_end()

EPOCHS = 5

tfs = transforms.Compose([
    transforms.Resize((256, 256)),
    transforms.ToTensor(),
])
testset = datasets.ImageFolder(root="/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test/", transform=tfs)
test_loader = DataLoader(testset, batch_size=8, shuffle=True)

model.load_state_dict(torch.load('./checkpoint/model.pth'))


def test(epoch):
    model.eval()
    test_loss = 0
    for i, (data, _) in enumerate(test_loader):
        # data = Variable(data)    #unsqueeze(0)
        final, residual_img, upscaled_image, com_img, orig_im = model(data.cuda())
        test_loss += loss_function(final, residual_img, upscaled_image, com_img, orig_im).data
        if epoch == EPOCHS and i == 0:
            #             save_image(final.data[0],'reconstruction_final',nrow=8)
            #             save_image(com_img.data[0],'com_img',nrow=8)
            n = min(data.size(0), 6)
            print("saving the image " + str(n))
            comparison = torch.cat([data[:n], final[:n].cpu()])
            comparison = comparison.cpu()
            #             print(comparison.data)
            save_image(com_img[:n].data, 'compressed_' + str(epoch) + '.png', nrow=n)
            save_image(comparison.data, 'reconstruction_' + str(epoch) + '.png', nrow=n)

    test_loss /= len(testset.test_loader)
    print('====&gt; Test set loss: {:.4f}'.format(test_loss))


def save_images():
    epoch = EPOCHS
    model.eval()
    test_loss = 0
    for i, (data, _) in enumerate(test_loader):
        # print(data)
        # data = Variable(data) #unsqueeze(0)
        print(data.shape)
        print(data.size())
        final, residual_img, upscaled_image, com_img, orig_im = model(data.cuda())
        test_loss += loss_function(final, residual_img, upscaled_image, com_img, orig_im).data
        if i == 3:
            #             save_image(final.data[0],'reconstruction_final',nrow=8)
            #             save_image(com_img.data[0],'com_img',nrow=8)
            n = min(data.size(0), 6)
            print("saving the image " + str(n))
            comparison = torch.cat([data[:n], final[:n].cpu()])
            comparison = comparison.cpu()
            #             print(comparison.data)
            save_image(com_img[:1].data, './compressed_image/compressed_' + str(i) + '.png', nrow=n)
            save_image(final[:1].data, './final_image/final_' + str(epoch) + '.png', nrow=n)
            save_image(orig_im[:1].data, './orginal_image/original_' + str(epoch) + '.png', nrow=n)

    test_loss /= len(test_loader.dataset)
    print('====&gt; Test set loss: {:.4f}'.format(test_loss))


save_images()

</code></pre>
<p>Error.</p>
<pre><code class="lang-auto">/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/bin/python /home/khawar/Desktop/End-to-End_IEEE-TVSCT/test.py
torch.Size([8, 3, 256, 256])
torch.Size([8, 3, 256, 256])
/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/nn/_reduction.py:43: UserWarning: size_average and reduce args will be deprecated, please use reduction='sum' instead.
  warnings.warn(warning.format(ret))
/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/nn/modules/loss.py:432: UserWarning: Using a target size (torch.Size([8, 3, 32, 32])) that is different to the input size (torch.Size([8, 3, 256, 256])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)
Traceback (most recent call last):
  File "/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test.py", line 77, in &lt;module&gt;
    save_images()
  File "/home/khawar/Desktop/End-to-End_IEEE-TVSCT/test.py", line 60, in save_images
    test_loss += loss_function(final, residual_img, upscaled_image, com_img, orig_im).data
  File "/home/khawar/Desktop/End-to-End_IEEE-TVSCT/loss.py", line 7, in loss_function
    com_loss = nn.MSELoss(size_average=False)(orig_img, final_img)
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/nn/modules/loss.py", line 432, in forward
    return F.mse_loss(input, target, reduction=self.reduction)
  File "/home/khawar/anaconda3/envs/End-to-End_IEEE-TVSCT/lib/python3.5/site-packages/torch/nn/functional.py", line 2538, in mse_loss
    ret = (input - target) ** 2
RuntimeError: The size of tensor a (256) must match the size of tensor b (32) at non-singleton dimension 3

Process finished with exit code 1
</code></pre>

Khawar_Islam: <p>Error solved but i didnt find any compressed and reconstruct image.</p>

Hachim_Bani: <p>I am facing the same problem right now, how did you fix it ?</p>

Khawar_Islam: <p>The size of input image and validation image should be the same. You can do using Data agumentation</p>

Hachim_Bani: <p>Thank you, Yes I found that and it worked perfectly.</p>