Size mismatch, m1: [1 x 3512320], m2: [2048 x 1000] at /opt/conda/conda-bld/pytorch_1513368888240/work/torch/lib/TH/generic/THTensorMath.c:1416

Hi , I am trying to do image segmentation.

My image size is 1296x966

But ,I am getting size mismatch, m1: [1 x 3512320], m2: [2048 x 1000] at /opt/conda/conda-bld/pytorch_1513368888240/work/torch/lib/TH/generic/THTensorMath.c:1416

Here is my model definition :

import numpy as np
import torch

from PIL import Image

def colormap(n):
    cmap=np.zeros([n, 3]).astype(np.uint8)

    for i in np.arange(n):
        r, g, b = np.zeros(3)

        for j in np.arange(8):
            r = r + (1<<(7-j))*((i&(1<<(3*j))) >> (3*j))
            g = g + (1<<(7-j))*((i&(1<<(3*j+1))) >> (3*j+1))
            b = b + (1<<(7-j))*((i&(1<<(3*j+2))) >> (3*j+2))

        cmap[i,:] = np.array([r, g, b])

    return cmap

class Relabel:

    def __init__(self, olabel, nlabel):
        self.olabel = olabel
        self.nlabel = nlabel

    def __call__(self, tensor):
        assert isinstance(tensor, torch.LongTensor), 'tensor needs to be LongTensor'
        tensor[tensor == self.olabel] = self.nlabel
        return tensor


class ToLabel:

    def __call__(self, image):
        return torch.from_numpy(np.array(image)).long().unsqueeze(0)


class Colorize:

    def __init__(self, n=22):
        self.cmap = colormap(256)
        self.cmap[n] = self.cmap[-1]
        self.cmap = torch.from_numpy(self.cmap[:n])

    def __call__(self, gray_image):
        size = gray_image.size()
        color_image = torch.ByteTensor(3, size[1], size[2]).fill_(0)

        for label in range(1, len(self.cmap)):
            mask = gray_image[0] == label

            color_image[0][mask] = self.cmap[label][0]
            color_image[1][mask] = self.cmap[label][1]
            color_image[2][mask] = self.cmap[label][2]

        return color_image

import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import numpy as np
from torch.utils import model_zoo
from torchvision import models
from torch.autograd import Variable

class PSPDec(nn.Module):

    def __init__(self, in_features, out_features, downsize, upsize=18):
        super(PSPDec,self).__init__()

        self.features = nn.Sequential(
            nn.AvgPool2d(downsize, stride=downsize),
            nn.Conv2d(in_features, out_features, 1, bias=False),
            nn.BatchNorm2d(out_features,momentum=.95),
            nn.ReLU(inplace=True),
            nn.UpsamplingBilinear2d(upsize)
        )

    def forward(self, x):
        return self.features(x)
    
    
    
class PSPNet(nn.Module):

    def __init__(self, num_classes):
        super(PSPNet,self).__init__()

        #init_net=deeplab_resnet.Res_Deeplab()

        self.resnet = models.resnet101(pretrained=True)

        #state=torch.load("../models/MS_DeepLab_resnet_trained_VOC.pth")
        #init_net.load_state_dict(state)
        #self.resnet=init_net
        
        
        
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                #m.stride = 1
                m.requires_grad = False
            if isinstance(m, nn.BatchNorm2d):
                m.requires_grad = False
        

        self.layer5a = PSPDec(21, 5, 18)
        self.layer5b = PSPDec(21, 5, 9)
        self.layer5c = PSPDec(21, 5, 6)
        self.layer5d = PSPDec(21, 5, 3)

        self.final = nn.Sequential(
            nn.Conv2d(41, 25, 3, padding=1, bias=False),
            nn.BatchNorm2d(25, momentum=.95),
            nn.ReLU(inplace=True),
            nn.Dropout(.1),
            nn.Conv2d(25, num_classes, 1),
        )

    def forward(self, x):
        
        
        x=self.resnet(x)
        x=x[0]
        
        x = self.final(torch.cat([
            x,
            self.layer5a(x),
            self.layer5b(x),
            self.layer5c(x),
            self.layer5d(x),
        ], 1))

        #print('final', x.size())

        return F.upsample_bilinear(x,136)

Here is my transformation code :

input_transform = Compose([
    #CenterCrop(512),
    Resize(1296),
    ToTensor()
    #Normalize([.485, .456, .406], [.229, .224, .225]),
])
target_transform = Compose([
    #CenterCrop(256),
    Resize(1296),
    ToLabel()
    #Relabel(255, 21),
])

Exception is coming from here :


<ipython-input-46-4df4b20a63e8> in forward(self, x)
     38 
     39 
---> 40         x=self.resnet(x)
     41         x=x[0]

Can anyone please help me here ?

Thanks a lot in advance.

Hello, have you solved that question yet? I just met the same problem that you mentioned.

What is your input-image size?