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

ssalome: <p>Hi<br>
I’m using Resnet18(Not Pre-trained) for training images with shape(1, 224, 224)<br>
I have 15 output classes.<br>
Hence I have modified the first conv2d and the last linear layer accordingly.</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
<p>Sequential(<br>
<strong>(0): Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))</strong><br>
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): ReLU(inplace=True)<br>
(3): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)<br>
(4): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(5): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(downsample): Sequential(<br>
(0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)<br>
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(6): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(downsample): Sequential(<br>
(0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)<br>
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(7): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(downsample): Sequential(<br>
(0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)<br>
(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(8): AdaptiveAvgPool2d(output_size=(1, 1))<br>
<strong>(9): Linear(in_features=512, out_features=15, bias=True)</strong><br>
)</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
<p><strong>On training i get the following error</strong><br>
<strong>RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 15] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290</strong></p>
<p>It would be really great if someone could help me:)</p>

ptrblck: <p>It seems you’ve wrapped all modules into an <code>nn.Sequential</code> block.<br>
If that’s the case, you are removing the flattening, which is used <a href="https://github.com/pytorch/vision/blob/6c2cda6a0eda4c835f96f18bb2b3be5043d96ad2/torchvision/models/resnet.py#L210">here</a> directly in the <code>forward</code> method.</p>
<p>You could keep the original architecture and change both layers by reassigning the new layers:</p>
<pre><code class="lang-python">model = models.resnet18()
model.conv1 = ...
</code></pre>
<p>Or by adding an <code>nn.Flatten()</code> module inside the <code>nn.Sequential</code>.</p>

ssalome: <p>yeah it worked. Simply modifying the conv1 and last layer</p>

rashidbaloch: <p>Hello Sir ptrblck,<br>
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.<br>
I am getting this Error:</p>
<pre><code class="lang-auto">RuntimeError: size mismatch, m1: [64 x 25088], m2: [4096 x 20] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:283
</code></pre>
<p>Kindly help me out. Thank You<br>
Here is my code:</p>
<pre><code class="lang-auto">
# 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
</code></pre>

ptrblck: <p>You are replacing <code>self.fc</code> multiple times in these lines:</p>
<pre><code class="lang-python">        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
</code></pre>
<p>If you want to apply these layers sequentially, wrap them in an <code>nn.Sequential</code> container:</p>
<pre><code class="lang-python">self.fc = nn.Sequential(nn.Linear(512*7*7, 4096),
                        nn.Dropout(),
                        ...
)
</code></pre>

rashidbaloch: <p>Hi Sir ptrblck,<br>
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.<br>
Please help me out by pointing me to the correct direction.<br>
I just want to extract 4096dim feature vectors.<br>
Thank You</p>
<pre><code class="lang-auto">
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" &lt;https://arxiv.org/pdf/1409.1556.pdf&gt;`_
    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" &lt;https://arxiv.org/pdf/1409.1556.pdf&gt;`_
    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 &gt; 180:
        lr = lr / 1000000
    elif epoch &gt; 150:
        lr = lr / 100000
    elif epoch &gt; 120:
        lr = lr / 10000
    elif epoch &gt; 90:
        lr = lr / 1000
    elif epoch &gt; 60:
        lr = lr / 100
    elif epoch &gt; 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 &gt; 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
</code></pre>
<p>I am still getting this error message.</p>
<pre><code class="lang-auto">RuntimeError: size mismatch, m1: [21504 x 224], m2: [25088 x 4096] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:283
</code></pre>

ptrblck: <p>Your model seems to work fine using:</p>
<pre><code class="lang-python">model = vgg19_bn()
x = torch.randn(1, 3, 224, 224)
out = model(x)
</code></pre>
<p>If you are getting the shape mismatch error using:</p>
<pre><code class="lang-python">feature_extractor = torch.nn.Sequential(*list(model_ft.classifier.children())[:-2])
</code></pre>
<p>then note that <code>nn.Sequential</code> will only use all registered modules in a sequential way and will not apply the functional calls defined in the <code>forward</code> method.<br>
Also, you are currently only using the <code>model_ft.classifier.children()</code>, which will skip all other layers, so that your input won’t have the desired shape.</p>
<p>If you want to get the 4096 features from the first linear layer, you could use:</p>
<pre><code class="lang-python">model = vgg19_bn()
model.classifier = model.classifier[0] # only use first linear layer
</code></pre>
<p>or alternatively you could replace the last linear layer with <code>nn.Identity()</code>, which would apply the additional relu, dropout and linear layer:</p>
<pre><code class="lang-python"># alternatively
model.classifier[6] = nn.Identity() # replace last linear layer with Identity
</code></pre>

rahul_uppari: <p>Hello ptrblck sir,</p>
<p>I’m facing an issue with view, could you please help me with it below is the error:<br>
RuntimeError                              Traceback (most recent call last)<br>
 in <br>
----&gt; 1 learn.fit_one_cycle(2)</p>
<p>~\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)<br>
21     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,<br>
22                                        final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))<br>
—&gt; 23     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)<br>
24<br>
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,</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in fit(self, epochs, lr, wd, callbacks)<br>
198         else: self.opt.lr,self.opt.wd = lr,wd<br>
199         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)<br>
–&gt; 200         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)<br>
201<br>
202     def create_opt(self, lr:Floats, wd:Floats=0.)-&gt;None:</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in fit(epochs, learn, callbacks, metrics)<br>
99             for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):<br>
100                 xb, yb = cb_handler.on_batch_begin(xb, yb)<br>
–&gt; 101                 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)<br>
102                 if cb_handler.on_batch_end(loss): break<br>
103</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)<br>
24     if not is_listy(xb): xb = [xb]<br>
25     if not is_listy(yb): yb = [yb]<br>
—&gt; 26     out = model(*xb)<br>
27     out = cb_handler.on_loss_begin(out)<br>
28</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
548             result = self._slow_forward(*input, **kwargs)<br>
549         else:<br>
–&gt; 550             result = self.forward(*input, **kwargs)<br>
551         for hook in self._forward_hooks.values():<br>
552             hook_result = hook(self, input, result)</p>
<p> in forward(self, x)<br>
33     def forward(self, x):<br>
34         x = self.seq1(x)<br>
—&gt; 35         x = x.view(-1, 126<em>126</em>16)<br>
36         x = self.seq2(x)<br>
37         x = self.seq3(x)</p>
<p>RuntimeError: shape ‘[-1, 254016]’ is invalid for input of size 1548800</p>
<p>and this is the code<br>
class BuildingSegmenterNet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(BuildingSegmenterNet, self).<strong>init</strong>()<br>
self.seq1 = nn.Sequential(<br>
nn.Conv2d(3, 16, (5,5)),<br>
nn.MaxPool2d((2,2))<br>
)<br>
self.seq2 = nn.Sequential(<br>
nn.Linear((126<em>126</em>16), 512),<br>
nn.ReLU()<br>
)<br>
self.dropout1 = nn.Dropout(0.33)<br>
self.seq3 = nn.Sequential(<br>
nn.Linear(512, 128),<br>
nn.ReLU()<br>
)<br>
self.seq4 = nn.Sequential(<br>
nn.Linear(128, 128),<br>
nn.ReLU()<br>
)<br>
self.seq5 = nn.Sequential(<br>
nn.Linear(128, 512),<br>
nn.ReLU()<br>
)<br>
self.seq6 = nn.Sequential(<br>
nn.Linear(512, 256<em>256),<br>
nn.ReLU()<br>
)<br>
self.seq7 = nn.Sequential(<br>
nn.Conv2d(1, 1, (3,3), padding = 1),<br>
nn.Sigmoid()<br>
)<br>
def forward(self, x):<br>
x = self.seq1(x)<br>
x = x.view(-1, 126</em>126*16)<br>
x = self.seq2(x)<br>
x = self.seq3(x)<br>
x = self.dropout1(x)<br>
x = self.seq4(x)<br>
x = self.seq5(x)<br>
x = self.seq6(x)<br>
x = x.view(x.size(0), -1)<br>
x = self.seq7(x)<br>
return x</p>
<p>My image is size is 200*200 RGB and i have kept the batch size as 8</p>
<p>Could you please help me to resolve this error.</p>
<p>thanking you in advance</p>
<p>Regareds,<br>
Rahul Ramchandra Uppari.</p>

ptrblck: <p>Change this line:</p>
<pre><code class="lang-python">x = x.view(-1, 126*126*16)
</code></pre>
<p>to</p>
<pre><code class="lang-python">x = x.view(x.size(0), -1)
</code></pre>
<p>and rerun your code. The current <code>view</code> operation is failing, since <code>x</code> doesn’t contain a matching number of elements.<br>
After you’ve changed the <code>view</code> operation, you might get a shape mismatch error in the next linear layer and should be able to fix it by changing the <code>in_features</code> of this layer.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which would make debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

rahul_uppari: <p>Many thanks ptrblck.</p>
<p>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.</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
----&gt; 1 learn.fit_one_cycle(2)</p>
<p>~\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)<br>
21     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,<br>
22                                        final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))<br>
—&gt; 23     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)<br>
24<br>
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,</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in fit(self, epochs, lr, wd, callbacks)<br>
198         else: self.opt.lr,self.opt.wd = lr,wd<br>
199         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)<br>
–&gt; 200         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)<br>
201<br>
202     def create_opt(self, lr:Floats, wd:Floats=0.)-&gt;None:</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in fit(epochs, learn, callbacks, metrics)<br>
99             for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):<br>
100                 xb, yb = cb_handler.on_batch_begin(xb, yb)<br>
–&gt; 101                 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)<br>
102                 if cb_handler.on_batch_end(loss): break<br>
103</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\fastai\basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)<br>
24     if not is_listy(xb): xb = [xb]<br>
25     if not is_listy(yb): yb = [yb]<br>
—&gt; 26     out = model(*xb)<br>
27     out = cb_handler.on_loss_begin(out)<br>
28</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
548             result = self._slow_forward(*input, **kwargs)<br>
549         else:<br>
–&gt; 550             result = self.forward(*input, **kwargs)<br>
551         for hook in self._forward_hooks.values():<br>
552             hook_result = hook(self, input, result)</p>
<p> in forward(self, x)<br>
34         x = self.seq1(x)<br>
35         x = x.view(x.size(0), -1)<br>
—&gt; 36         x = self.seq2(x)<br>
37         x = self.seq3(x)<br>
38         x = self.dropout1(x)</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
548             result = self._slow_forward(*input, **kwargs)<br>
549         else:<br>
–&gt; 550             result = self.forward(*input, **kwargs)<br>
551         for hook in self._forward_hooks.values():<br>
552             hook_result = hook(self, input, result)</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\container.py in forward(self, input)<br>
98     def forward(self, input):<br>
99         for module in self:<br>
–&gt; 100             input = module(input)<br>
101         return input<br>
102</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
548             result = self._slow_forward(*input, **kwargs)<br>
549         else:<br>
–&gt; 550             result = self.forward(*input, **kwargs)<br>
551         for hook in self._forward_hooks.values():<br>
552             hook_result = hook(self, input, result)</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)<br>
85<br>
86     def forward(self, input):<br>
—&gt; 87         return F.linear(input, self.weight, self.bias)<br>
88<br>
89     def extra_repr(self):</p>
<p>~\Anaconda3\envs\fastai_v1\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)<br>
1608     if input.dim() == 2 and bias is not None:<br>
1609         # fused op is marginally faster<br>
-&gt; 1610         ret = torch.addmm(bias, input, weight.t())<br>
1611     else:<br>
1612         output = input.matmul(weight.t())</p>
<p>RuntimeError: size mismatch, m1: [8 x 193600], m2: [254016 x 512] at C:/cb/pytorch_1000000000000/work/aten/src\THC/generic/THCTensorMathBlas.cu:283</p>

ptrblck: <p>That’s the shape mismatch error I’ve mentioned in my previous post.<br>
Change the <code>in_features</code> of <code>self.seq2</code> to <code>254016</code>.</p>

rahul_uppari: <p>I have changed the in_features in self.seq2 to 254016 but still the error persists.</p>
<pre><code class="lang-auto">    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</code></pre>

ptrblck: <p>Could you post the complete model definition as well as the input shapes, so that I could rerun the code, please?</p>

rahul_uppari: <p>Sure ptrblck<br>
Below is the code.</p>
<pre><code class="lang-auto">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)</code></pre>

ptrblck: <p>Thanks for the code.<br>
My previous recommendation was wrong and the <code>in_features</code> of <code>seq2</code> were already set to <code>254016</code> and should be instead set to <code>110*110*16=193600</code>:</p>
<pre><code class="lang-python">        self.seq2 = nn.Sequential(
            nn.Linear((110*110*16), 512),
            nn.ReLU()
        )
</code></pre>

Zihao_Wang: <p>Dear, sir, you saved my program, wish you a happy life!</p>