RuntimeError: Given input size: (6x253x1). Calculated output size: (6x126x0). Output size is too small

crispy: <p>The following is my code, when i run it it gives me the error written in the title:</p>
<pre><code class="lang-auto">import os
import random
import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
from matplotlib import pyplot as plt
from tqdm import tqdm

# Hyper-parameters
num_epochs = 5
batch_size = 4
learning_rate = 0.001


# Device will determine whether to run the training on GPU or CPU.
use_cuda = torch.cuda.is_available()
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

if __name__ == '__main__':
    # Use transforms.compose method to reformat images for modeling and save to variable all_transforms for later use dataset has PILImage images of range [0, 1].  We transform them to Tensors of normalized range [-1, 1]
    transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

    #give paths to train and test datasets
    ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
    test_dataset = torchvision.datasets.ImageFolder(root=ROOT_DIR + "/Face_Mask_Dataset/Test/", transform=transform)
    classes = ('WithoutMask', 'Clothmask', 'Surgicalmask', 'N95Mask')
    imgSize = 255
    train_data = []
    test_data = []


    def imshow(img):
        img = img / 2 + 0.5  # unnormalize
        npimg = img.numpy()
        plt.imshow(np.transpose(npimg, (1, 2, 0)))
        plt.show()

    def create_training_data():
        counter = 0
        rnd = random.randrange(0, 1001)
        for category in classes:  # cycle through categories
            path = os.path.join(ROOT_DIR + "/Face_Mask_Dataset/Train/", category)  # create path to categories
            class_num = classes.index(category)  # get the classification by index per category
            for img in tqdm(os.listdir(path)):  # iterate over each image per category
                try:
                    img_array = cv2.imread(os.path.join(path, img))  # convert to array
                    new_array = cv2.resize(img_array, (imgSize, imgSize)) # resize to normalize data size
                    train_data.append([new_array, class_num])  # add this to our training_data
                    counter += 1
                    if counter == rnd:
                        plt.imshow(new_array, cmap='gray')  # graph it
                        plt.show()

                except Exception as e:
                    pass

    train_dataset = train_data
    create_training_data()
    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)

    class CNN(nn.Module):

        def __init__(self):
            super(CNN, self).__init__()
            self.conv1 = nn.Conv2d(255, 6, 3)
            self.pool = nn.MaxPool2d(2, 2)
            self.conv2 = nn.Conv2d(6, 16, 5)
            self.fc1 = nn.Linear(16 * 5 * 5, 120)
            self.fc2 = nn.Linear(120, 84)
            self.fc3 = nn.Linear(84, 10)

        def forward(self, x):
            # -&gt; n, 3, 32, 32
            x = self.pool(F.relu(self.conv1(x)))  # -&gt; n, 6, 14, 14
            x = self.pool(F.relu(self.conv2(x)))  # -&gt; n, 16, 5, 5
            x = x.view(-1, 16 * 5 * 5)  # -&gt; n, 400
            x = F.relu(self.fc1(x))  # -&gt; n, 120
            x = F.relu(self.fc2(x))  # -&gt; n, 84
            x = self.fc3(x)  # -&gt; n, 10
            return x

    model = CNN().to(device)
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
    n_total_steps = len(train_loader)

    for epoch in range(num_epochs):

        for i, (images, labels) in enumerate(train_loader):
            # origin shape: [6, 3, 5, 5] = 6, 3, 25
            # input_layer: 3 input channels, 6 output channels, 5 kernel size
            images = images.to(device)
            labels = labels.to(device)
            # Forward pass
            images = images.float()
            outputs = model(images)
            loss = criterion(outputs, labels)
            # Backward and optimize
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if (i + 1) % 2000 == 0:
                print(f'Epoch [{epoch + 1}/{num_epochs}], Step [{i + 1}/{n_total_steps}], Loss: {loss.item():.4f}')

    print('Finished Training')
    PATH = './cnn.pth'
    torch.save(model.state_dict(), PATH)

    with torch.no_grad():
        n_correct = 0
        n_samples = 0
        n_class_correct = [0 for i in range(10)]
        n_class_samples = [0 for i in range(10)]

        for images, labels in test_loader:
            images = images.to(device)
            labels = labels.to(device)
            outputs = model(images)
            # max returns (value ,index)
            _, predicted = torch.max(outputs, 1)
            n_samples += labels.size(0)
            n_correct += (predicted == labels).sum().item()

            for i in range(batch_size):
                label = labels[i]
                pred = predicted[i]

                if (label == pred):
                    n_class_correct[label] += 1
                n_class_samples[label] += 1

        acc = 100.0 * n_correct / n_samples
        print(f'Accuracy of the network: {acc} %')

        for i in range(10):
            acc = 100.0 * n_class_correct[i] / n_class_samples[i]
            print(f'Accuracy of {classes[i]}: {acc} %')


</code></pre>
<p>The point of this code is to take in a training dataset that recognizes if someone in a picture is wearing a mask or not, and if they are what kind of mask. it contains around 1500 pictures in the training set and around 400 in the test. please advice on how to fix the issue. Also i’m pretty sure the values in the CNN are wrong since i’m a beginner in AI so any advice on how to get the right values would also be appreciated!</p>

ptrblck: <p>The issue is raised if the output of a layer would be empty, which could be the case in e.g. conv or pooling layers.<br>
You could try to increase the spatial size of the input or use a less aggressive pooling.</p>

crispy: <p>so do i modify those values?<br>
sorry if it seems like as simple question, i’m still a beginner at AI</p>
<aside class="quote no-group" data-username="crispy" data-post="1" data-topic="153596">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/e47774/48.png" class="avatar"> crispy:</div>
<blockquote>
<pre><code class="lang-auto">self.conv1 = nn.Conv2d(255, 6, 3)
            self.pool = nn.MaxPool2d(2, 2)
            self.conv2 = nn.Conv2d(6, 16, 5)
            self.fc1 = nn.Linear(16 * 5 * 5, 120)
            self.fc2 = nn.Linear(120, 84)
            self.fc3 = nn.Linear(84, 10)
</code></pre>
</blockquote>
</aside>

ptrblck: <p>Most likely, but it also seems you are passing the input tensor in the shape <code>[batch_size, height, width, channels]</code> which is wrong as the channels-first memory layout is expected by default (you can call <code>to(memory_format=torch.channels_last)</code> but the returned shape will still be shown in the default NCHW format).</p>
<p>This code snippet uses a random input tensor, fixes the wrong <code>view</code> operation, and the feature mismatch in <code>self.fc1</code> for an input of <code>[batch_size, 3, 255, 255]</code>:</p>
<pre><code class="lang-python">class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 3)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(59536, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
    
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(x.size(0), -1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = CNN()
x = torch.randn(1, 3, 255, 255)
out = model(x)
</code></pre>

crispy: <p>Thank you very much, it has solved the issue, however right now another issue arose. it the below code it give me this issue:<br>
“ValueError: optimizer got an empty parameter list”<br>
My guess is that the hyperparameters are never initialized in the class CNN but i may be wrong. Please advice!</p>
<pre><code class="lang-auto"> model = CNN().to(device)
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters, lr=learning_rate)
    n_total_steps = len(train_loader)
</code></pre>

ptrblck: <p><code>.parameters</code> is a method and you would need to call it:</p>
<pre><code class="lang-python">optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
</code></pre>

crispy: <p>oh yeah my mistake, i was trying something before i copy and pasted haha. but the error is given even when i have this piece of code written:</p>
<pre><code class="lang-auto">optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
</code></pre>

ptrblck: <p>It works for me, so I guess you might be running another code:</p>
<pre><code class="lang-python">class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 3)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(59536, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
    
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(x.size(0), -1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = CNN()
x = torch.randn(1, 3, 255, 255)
out = model(x)

optimizer = torch.optim.SGD(model.parameters(), lr=1.)
optimizer.param_groups
</code></pre>

crispy: <p>sorry, i’ve run into another issue, im running this code:</p>
<pre><code class="lang-auto">    class CNN(nn.Module):
        def __init__(self):
            super(CNN, self).__init__()
            self.conv1 = nn.Conv2d(3, 6, 3)
            self.pool = nn.MaxPool2d(2, 2)
            self.conv2 = nn.Conv2d(6, 16, 5)
            self.fc1 = nn.Linear(59536, 120)
            self.fc2 = nn.Linear(120, 84)
            self.fc3 = nn.Linear(84, 10)

        def forward(self, x):
            x = self.pool(F.relu(self.conv1(x)))
            x = self.pool(F.relu(self.conv2(x)))
            x = x.view(x.size(0), -1)
            x = F.relu(self.fc1(x))
            x = F.relu(self.fc2(x))
            x = self.fc3(x)
            return x

    model = CNN()
    optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
    optimizer.param_groups
    criterion = nn.CrossEntropyLoss()
    n_total_steps = len(train_loader)

    for epoch in range(num_epochs):

        for i, (images, labels) in enumerate(train_loader):
            # origin shape: [6, 3, 5, 5] = 6, 3, 25
            # input_layer: 3 input channels, 6 output channels, 5 kernel size
            images = images.to(device)
            labels = labels.to(device)
            # Forward pass
            images = images.float()
            **error happens here------&gt;** outputs = model(images)
            loss = criterion(outputs, labels)
            # Backward and optimize
            optimizer.zero_grad()
            torch.autograd.set_detect_anomaly(True)
            loss.backward(retain_graph=True)
            optimizer.step()

            if (i + 1) % 2000 == 0:
                print(f'Epoch [{epoch + 1}/{num_epochs}], Step [{i + 1}/{n_total_steps}], Loss: {loss.item():.4f}')

    print('Finished Training')
    PATH = './cnn.pth'
    torch.save(model.state_dict(), PATH)
</code></pre>
<p>however i get this error:</p>
<pre><code class="lang-auto">RuntimeError: Given groups=1, weight of size [6, 3, 3, 3], expected input[4, 255, 255, 3] to have 3 channels, but got 255 channels instead
</code></pre>
<p>sorry again, I’m really confused as to what’s happening</p>

ptrblck: <p>Check my previous post <a href="https://discuss.pytorch.org/t/runtimeerror-given-input-size-6x253x1-calculated-output-size-6x126x0-output-size-is-too-small/153596/4">here</a> which explains the expected memory format and also uses an example input to show how to run the model.<br>
In your code you would most likely need to <code>permute</code> the input tensor to match the size.<br>
<code>x = x.permute(0, 3, 1, 2)</code> should work.</p>