RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 15] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290

Hi
I’m using Resnet18(Not Pre-trained) for training images with shape(1, 224, 224)
I have 15 output classes.
Hence I have modified the first conv2d and the last linear layer accordingly.

Blockquote

Sequential(
(0): Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace=True)
(3): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
(4): Sequential(
(0): BasicBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(1): BasicBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(5): Sequential(
(0): BasicBlock(
(conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(6): Sequential(
(0): BasicBlock(
(conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(7): Sequential(
(0): BasicBlock(
(conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(8): AdaptiveAvgPool2d(output_size=(1, 1))
(9): Linear(in_features=512, out_features=15, bias=True)
)

Blockquote

On training i get the following error
RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 15] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290

It would be really great if someone could help me:)

It seems you’ve wrapped all modules into an nn.Sequential block.
If that’s the case, you are removing the flattening, which is used here directly in the forward method.

You could keep the original architecture and change both layers by reassigning the new layers:

model = models.resnet18()
model.conv1 = ...

Or by adding an nn.Flatten() module inside the nn.Sequential.

1 Like

yeah it worked. Simply modifying the conv1 and last layer

Hello Sir ptrblck,
Can you please point what am i wrong here. I am replicating VGG19_bn architecture (NOT Pre-Trained) for designing a CNN model for image classification. I have image 224x224 having output classes as 20.
I am getting this Error:

RuntimeError: size mismatch, m1: [64 x 25088], m2: [4096 x 20] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:283

Kindly help me out. Thank You
Here is my code:


# Replicating Architecture of VGG19_bn 
class Unit(nn.Module):
    def __init__(self,in_channels,out_channels):
        super(Unit,self).__init__()
        

        self.conv = nn.Conv2d(in_channels=in_channels,kernel_size=3,out_channels=out_channels,stride=1,padding=1)
        self.bn = nn.BatchNorm2d(num_features=out_channels)
        self.relu = nn.ReLU(inplace=True)

    def forward(self,input):
        output = self.conv(input)
        output = self.bn(output)
        output = self.relu(output)

        return output

class CNN(nn.Module):
    def __init__(self,num_classes=20):
        super(CNN,self).__init__()

        #Create 16 layers of the unit with max pooling in between
        self.unit1 = Unit(in_channels=3,out_channels=64)
        self.unit2 = Unit(in_channels=64, out_channels=64)

        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

        self.unit3 = Unit(in_channels=64, out_channels=128)
        self.unit4 = Unit(in_channels=128, out_channels=128)

        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

        self.unit5 = Unit(in_channels=128, out_channels=256)
        self.unit6 = Unit(in_channels=256, out_channels=256)
        self.unit7 = Unit(in_channels=256, out_channels=256)
        self.unit8 = Unit(in_channels=256, out_channels=256)

        self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

        self.unit9 = Unit(in_channels=256, out_channels=512)
        self.unit10 = Unit(in_channels=512, out_channels=512)
        self.unit11 = Unit(in_channels=512, out_channels=512)
        self.unit12 = Unit(in_channels=512, out_channels=512)

        self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

        self.unit13 = Unit(in_channels=512, out_channels=512)
        self.unit14 = Unit(in_channels=512, out_channels=512)
        self.unit15 = Unit(in_channels=512, out_channels=512)
        self.unit16 = Unit(in_channels=512, out_channels=512)

        self.pool5 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
        
        self.avgpool = nn.AdaptiveAvgPool2d(output_size=(7,7)) #output_size=7, 7

        #Add all the units into the Sequential layer in exact order
        self.net = nn.Sequential(self.unit1, self.unit2, self.pool1, self.unit3, self.unit4, self.pool2, self.unit5, self.unit6
                                 ,self.unit7, self.unit8, self.pool3, self.unit9, self.unit10, self.unit11, self.unit12, self.pool4
                                 ,self.unit13, self.unit14, self.unit15, self.unit16, self.pool5, self.avgpool)

        self.fc = nn.Linear(in_features=512*7*7, out_features=4096) #25088
        self.fc = nn.Dropout(p=0.5, inplace=False)
        self.fc = nn.Linear(in_features=4096, out_features=4096)
        self.fc = nn.Dropout(p=0.5, inplace=False)
        self.fc = nn.Linear(in_features=4096, out_features=num_classes) #20
        
    def forward(self, input):
        output = self.net(input)
        output = output.view(-1,512*7*7)
        output = self.fc(output)
        return output

You are replacing self.fc multiple times in these lines:

        self.fc = nn.Linear(in_features=512*7*7, out_features=4096) #25088
        self.fc = nn.Dropout(p=0.5, inplace=False)
        self.fc = nn.Linear(in_features=4096, out_features=4096)
        self.fc = nn.Dropout(p=0.5, inplace=False)
        self.fc = nn.Linear(in_features=4096, out_features=num_classes) #20

If you want to apply these layers sequentially, wrap them in an nn.Sequential container:

self.fc = nn.Sequential(nn.Linear(512*7*7, 4096),
                        nn.Dropout(),
                        ...
)
1 Like

Hi Sir ptrblck,
I really appreciate your response and for helping me out. However, I am still struggling to achieve my final goal here. I have managed to replicate VGG19_bn architecture and trained the model with my custom dataset. I have now the saved model in my hand and want to Extract the Feature Vector from the trained model and save the feature vectors as .mat file type so that I can feed those Extracted features to another Neural Network for final Classification.
Please help me out by pointing me to the correct direction.
I just want to extract 4096dim feature vectors.
Thank You


class VGG(nn.Module):

    def __init__(self, features, num_classes=20, init_weights=True):
        super(VGG, self).__init__()
        self.features = features
        self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
        self.classifier = nn.Sequential(
            nn.Linear(512 * 7 * 7, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, num_classes),
        )
        if init_weights:
            self._initialize_weights()

    def forward(self, x):
        x = self.features(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x

    def _initialize_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)


def make_layers(cfg, batch_norm=False):
    layers = []
    in_channels = 3
    for v in cfg:
        if v == 'M':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        else:
            conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
            if batch_norm:
                layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
            else:
                layers += [conv2d, nn.ReLU(inplace=True)]
            in_channels = v
    return nn.Sequential(*layers)


cfgs = {
    'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
    'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
    'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
    'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}


def _vgg(arch, cfg, batch_norm, pretrained, progress, **kwargs):
    if pretrained:
        kwargs['init_weights'] = False
    model = VGG(make_layers(cfgs[cfg], batch_norm=batch_norm), **kwargs)
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls[arch],
                                              progress=progress)
        model.load_state_dict(state_dict)
    return model


def vgg11(pretrained=False, progress=True, **kwargs):
    r"""VGG 11-layer model (configuration "A") from
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    return _vgg('vgg11', 'A', False, pretrained, progress, **kwargs)


def vgg19_bn(pretrained=False, progress=True, **kwargs):
    r"""VGG 19-layer model (configuration 'E') with batch normalization
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    return _vgg('vgg19_bn', 'E', True, pretrained, progress, **kwargs)



TRAIN_DATA_PATH = '/content/images/train_set/'
TEST_DATA_PATH = '/content/images/test_set/' 

BATCH_SIZE = 32

#Define transformations for the training set, flip the images randomly, crop out and apply mean and std normalization
train_transformations = transforms.Compose([     
    transforms.Resize((224,224)),                                                                            
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
])

train_set = torchvision.datasets.ImageFolder(root=TRAIN_DATA_PATH, transform=train_transformations)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)

#Define transformations for the test set
test_transformations = transforms.Compose([ 
    #transforms.Resize(size=256),  
    #transforms.CenterCrop(size=224),
    transforms.Resize((224,224)),                                                                                  
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
])

test_set = torchvision.datasets.ImageFolder(root=TEST_DATA_PATH, transform=test_transformations)
test_loader  = torch.utils.data.DataLoader(test_set, batch_size=BATCH_SIZE, shuffle=False, num_workers=4) 

#Check if gpu support is available
cuda_avail = torch.cuda.is_available()

#Create model, optimizer and loss function
#model = vgg19_bn(in_channels=3,num_classes=20).to(device)
model = vgg19_bn()


if cuda_avail:
    model.cuda()

optimizer = torch.optim.Adam(model.parameters(), lr=0.001,weight_decay=0.0001)
loss_fn = nn.CrossEntropyLoss()

#Create a learning rate adjustment function that divides the learning rate by 10 every 30 epochs
def adjust_learning_rate(epoch):

    lr = 0.001

    if epoch > 180:
        lr = lr / 1000000
    elif epoch > 150:
        lr = lr / 100000
    elif epoch > 120:
        lr = lr / 10000
    elif epoch > 90:
        lr = lr / 1000
    elif epoch > 60:
        lr = lr / 100
    elif epoch > 30:
        lr = lr / 10

    for param_group in optimizer.param_groups:
        param_group["lr"] = lr




def save_models(epoch):
    torch.save(model.state_dict(), "/content/VGG19ImageCNN_{}.model".format(epoch))
    print("Checkpoint saved")

def test():
    model.eval()
    test_acc = 0.0

    for i, (images, labels) in enumerate(test_loader):
      
        if cuda_avail:
                images = torch.as_tensor(images.cuda())
                labels = torch.as_tensor(labels.cuda())

        #Predict classes using images from the test set
        outputs = model(images)
        _,prediction = torch.max(outputs.data, 1)
        #prediction = prediction.cpu()
        test_acc += torch.sum(prediction == labels)


    #Compute the average acc and loss over all 10000 test images
    test_acc = test_acc/len(test_loader.dataset)  #test_acc = test_acc / 200

    return test_acc

def train(num_epochs):
    best_acc = 0.0

    for epoch in range(num_epochs):
        model.train()
        train_acc = 0.0
        train_loss = 0.0
        for i, (images, labels) in enumerate(train_loader):
            #Move images and labels to gpu if available
            if cuda_avail:
                images = torch.as_tensor(images.cuda())
                labels = torch.as_tensor(labels.cuda())

            #Clear all accumulated gradients
            optimizer.zero_grad()
            #Predict classes using images from the test set
            outputs = model(images)
            #Compute the loss based on the predictions and actual labels
            loss = loss_fn(outputs,labels)
            #Backpropagate the loss
            loss.backward()

            #Adjust parameters according to the computed gradients
            optimizer.step()

            train_loss += loss.cpu().data * images.size(0) #train_loss += loss.cpu().data[0] * images.size(0)
            _, prediction = torch.max(outputs.data, 1) #_, prediction = torch.max(outputs.data, 1)
            
            train_acc += torch.sum(prediction == labels)

        #Call the learning rate adjustment function
        adjust_learning_rate(epoch)

        #Compute the average acc and loss over all 800 training images
        train_acc = train_acc/len(train_loader.dataset) #train_acc = train_acc / 800  # train_loss = train_loss/len(train_loader.dataset)
        train_loss = train_loss/len(train_loader.dataset) #train_loss = train_loss / 800  

        #Evaluate on the test set
        test_acc = test()

        # Save the model if the test acc is greater than our current best
        if test_acc > best_acc:
            save_models(epoch)
            best_acc = test_acc


        # Print the metrics
        print("Epoch {}, Train Accuracy: {} , TrainLoss: {} , Test Accuracy: {}".format(epoch, train_acc, train_loss, test_acc))

# Extracting Feature vectors from the saved model VGG19ImageCNN_88.model

filepath = "/content/VGG19ImageCNN_88.model"

if cuda_avail:
    model.cuda()

for i, (images, labels) in enumerate(train_loader):
  if cuda_avail:
    images = torch.as_tensor(images.cuda())
    
    #Predict classes using images from the test set
    #outputs = model(images)
    print("Shape of First image:",images.shape)
    break

model.load_state_dict(torch.load(filepath))
model_ft = model
#print(model)
### strip the last layer with -1 and second last layer with -2 
feature_extractor = torch.nn.Sequential(*list(model_ft.classifier.children())[:-2])
print("Feature extractor",feature_extractor)
### check this works
x = images
#print("Print x",x.shape)
output = feature_extractor(x) # output now has the features corresponding to input x
print("Shape of Ouput is:",output.shape)
# If extracted features are equivalent to 4096 dimensional vector then SAVE the extracted features
# To (.mat) file to feed them as input to another N
if __name__ == "__main__":
    #train(200) # 200 is the number of epochs

I am still getting this error message.

RuntimeError: size mismatch, m1: [21504 x 224], m2: [25088 x 4096] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:283

Your model seems to work fine using:

model = vgg19_bn()
x = torch.randn(1, 3, 224, 224)
out = model(x)

If you are getting the shape mismatch error using:

feature_extractor = torch.nn.Sequential(*list(model_ft.classifier.children())[:-2])

then note that nn.Sequential will only use all registered modules in a sequential way and will not apply the functional calls defined in the forward method.
Also, you are currently only using the model_ft.classifier.children(), which will skip all other layers, so that your input won’t have the desired shape.

If you want to get the 4096 features from the first linear layer, you could use:

model = vgg19_bn()
model.classifier = model.classifier[0] # only use first linear layer

or alternatively you could replace the last linear layer with nn.Identity(), which would apply the additional relu, dropout and linear layer:

# alternatively
model.classifier[6] = nn.Identity() # replace last linear layer with Identity
1 Like

Hello ptrblck sir,

I’m facing an issue with view, could you please help me with it below is the error:
RuntimeError Traceback (most recent call last)
in
----> 1 learn.fit_one_cycle(2)

~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
21 callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
22 final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
—> 23 learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
24
25 def fit_fc(learn:Learner, tot_epochs:int=1, lr:float=defaults.lr, moms:Tuple[float,float]=(0.95,0.85), start_pct:float=0.72,

~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in fit(self, epochs, lr, wd, callbacks)
198 else: self.opt.lr,self.opt.wd = lr,wd
199 callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
–> 200 fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
201
202 def create_opt(self, lr:Floats, wd:Floats=0.)->None:

~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in fit(epochs, learn, callbacks, metrics)
99 for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
100 xb, yb = cb_handler.on_batch_begin(xb, yb)
–> 101 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)
102 if cb_handler.on_batch_end(loss): break
103

~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
24 if not is_listy(xb): xb = [xb]
25 if not is_listy(yb): yb = [yb]
—> 26 out = model(*xb)
27 out = cb_handler.on_loss_begin(out)
28

~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
–> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)

in forward(self, x)
33 def forward(self, x):
34 x = self.seq1(x)
—> 35 x = x.view(-1, 12612616)
36 x = self.seq2(x)
37 x = self.seq3(x)

RuntimeError: shape ‘[-1, 254016]’ is invalid for input of size 1548800

and this is the code
class BuildingSegmenterNet(nn.Module):
def init(self):
super(BuildingSegmenterNet, self).init()
self.seq1 = nn.Sequential(
nn.Conv2d(3, 16, (5,5)),
nn.MaxPool2d((2,2))
)
self.seq2 = nn.Sequential(
nn.Linear((12612616), 512),
nn.ReLU()
)
self.dropout1 = nn.Dropout(0.33)
self.seq3 = nn.Sequential(
nn.Linear(512, 128),
nn.ReLU()
)
self.seq4 = nn.Sequential(
nn.Linear(128, 128),
nn.ReLU()
)
self.seq5 = nn.Sequential(
nn.Linear(128, 512),
nn.ReLU()
)
self.seq6 = nn.Sequential(
nn.Linear(512, 256256),
nn.ReLU()
)
self.seq7 = nn.Sequential(
nn.Conv2d(1, 1, (3,3), padding = 1),
nn.Sigmoid()
)
def forward(self, x):
x = self.seq1(x)
x = x.view(-1, 126
126*16)
x = self.seq2(x)
x = self.seq3(x)
x = self.dropout1(x)
x = self.seq4(x)
x = self.seq5(x)
x = self.seq6(x)
x = x.view(x.size(0), -1)
x = self.seq7(x)
return x

My image is size is 200*200 RGB and i have kept the batch size as 8

Could you please help me to resolve this error.

thanking you in advance

Regareds,
Rahul Ramchandra Uppari.

Change this line:

x = x.view(-1, 126*126*16)

to

x = x.view(x.size(0), -1)

and rerun your code. The current view operation is failing, since x doesn’t contain a matching number of elements.
After you’ve changed the view operation, you might get a shape mismatch error in the next linear layer and should be able to fix it by changing the in_features of this layer.

PS: you can post code snippets by wrapping them into three backticks ```, which would make debugging easier. :wink:

Many thanks ptrblck.

I’m new to coding, could you please help me with one more thing Im getting the below error and don’t know how to fix it.

RuntimeError Traceback (most recent call last)
in
----> 1 learn.fit_one_cycle(2)

~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
21 callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
22 final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
—> 23 learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
24
25 def fit_fc(learn:Learner, tot_epochs:int=1, lr:float=defaults.lr, moms:Tuple[float,float]=(0.95,0.85), start_pct:float=0.72,

~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in fit(self, epochs, lr, wd, callbacks)
198 else: self.opt.lr,self.opt.wd = lr,wd
199 callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
–> 200 fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
201
202 def create_opt(self, lr:Floats, wd:Floats=0.)->None:

~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in fit(epochs, learn, callbacks, metrics)
99 for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
100 xb, yb = cb_handler.on_batch_begin(xb, yb)
–> 101 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)
102 if cb_handler.on_batch_end(loss): break
103

~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
24 if not is_listy(xb): xb = [xb]
25 if not is_listy(yb): yb = [yb]
—> 26 out = model(*xb)
27 out = cb_handler.on_loss_begin(out)
28

~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
–> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)

in forward(self, x)
34 x = self.seq1(x)
35 x = x.view(x.size(0), -1)
—> 36 x = self.seq2(x)
37 x = self.seq3(x)
38 x = self.dropout1(x)

~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
–> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)

~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
98 def forward(self, input):
99 for module in self:
–> 100 input = module(input)
101 return input
102

~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
–> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)

~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
85
86 def forward(self, input):
—> 87 return F.linear(input, self.weight, self.bias)
88
89 def extra_repr(self):

~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
1608 if input.dim() == 2 and bias is not None:
1609 # fused op is marginally faster
-> 1610 ret = torch.addmm(bias, input, weight.t())
1611 else:
1612 output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [8 x 193600], m2: [254016 x 512] at C:/cb/pytorch_1000000000000/work/aten/src\THC/generic/THCTensorMathBlas.cu:283

That’s the shape mismatch error I’ve mentioned in my previous post.
Change the in_features of self.seq2 to 254016.

I have changed the in_features in self.seq2 to 254016 but still the error persists.

    def __init__(self):
        super(BuildingSegmenterNet, self).__init__()
        self.seq1 = nn.Sequential(
            nn.Conv2d(3, 16, (5,5)),
            nn.MaxPool2d((2,2))
        )
        self.seq2 = nn.Sequential(
            nn.Linear((254016), 512),
            nn.ReLU()
        )
        self.dropout1 = nn.Dropout(0.33)
        self.seq3 = nn.Sequential(
            nn.Linear(512, 128),
            nn.ReLU()
        )

Have I made the proper changes as you mentioned

Could you post the complete model definition as well as the input shapes, so that I could rerun the code, please?

Sure ptrblck
Below is the code.

import fastai
from fastai import *
from fastai.vision import *

path = pathlib.Path('E:\dataset')
path

bs = 8
data = ImageDataBunch.from_folder(path=path,bs=bs,ds_tfms=get_transforms(),size=224,valid_pct=0.3)
data.normalize(imagenet_stats)

class SegmentationCrossEntropyLoss(nn.Module):
    def __init__(self):
        super(SegmentationCrossEntropyLoss, self).__init__()
        
    def forward(self, preds, targets):
        loss = nn.functional.binary_cross_entropy_with_logits(preds, targets.float())
        return loss

class BuildingSegmenterNet(nn.Module):
    def __init__(self):
        super(BuildingSegmenterNet, self).__init__()
        self.seq1 = nn.Sequential(
            nn.Conv2d(3, 16, (5,5)),
            nn.MaxPool2d((2,2))
        )
        self.seq2 = nn.Sequential(
            nn.Linear((254016), 512),
            nn.ReLU()
        )
        self.dropout1 = nn.Dropout(0.33)
        self.seq3 = nn.Sequential(
            nn.Linear(512, 128),
            nn.ReLU()
        )
        self.seq4 = nn.Sequential(
            nn.Linear(128, 128),
            nn.ReLU()
        )
        self.seq5 = nn.Sequential(
            nn.Linear(128, 512),
            nn.ReLU()
        )
        self.seq6 = nn.Sequential(
            nn.Linear(512, 256*256),
            nn.ReLU()
        )
        self.seq7 = nn.Sequential(
            nn.Conv2d(1, 1, (3,3), padding = 1),
            nn.Sigmoid()
        )
    def forward(self, x):
        x = self.seq1(x)
        x = x.view(x.size(0), -1)
        x = self.seq2(x)
        x = self.seq3(x)
        x = self.dropout1(x)
        x = self.seq4(x)
        x = self.seq5(x)
        x = self.seq6(x)
        x = x.view(-1, 1, 256, 256)
        x = self.seq7(x)
        return x

learn = Learner(data = data, model=BuildingSegmenterNet(), loss_func = SegmentationCrossEntropyLoss(), metrics = accuracy)

learn.fit_one_cycle(2)

Thanks for the code.
My previous recommendation was wrong and the in_features of seq2 were already set to 254016 and should be instead set to 110*110*16=193600:

        self.seq2 = nn.Sequential(
            nn.Linear((110*110*16), 512),
            nn.ReLU()
        )

Dear, sir, you saved my program, wish you a happy life!