RuntimeError: Expected 4-dimensional input for 4-dimensional weight [6, 3, 5, 5], but got 3-dimensional input of size [3, 256, 256] instead

import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.utils.data as data
import torchvision
from torchvision import transforms

EPOCHS = 2
BATCH_SIZE = 10
LEARNING_RATE = 0.003
TRAIN_DATA_PATH = r"path"
TEST_DATA_PATH = r"path"
TRANSFORM_IMG = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

train_data = torchvision.datasets.ImageFolder(root=TRAIN_DATA_PATH, transform=TRANSFORM_IMG)
train_data_loader = data.DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True,  num_workers=4)
test_data = torchvision.datasets.ImageFolder(root=TEST_DATA_PATH, transform=TRANSFORM_IMG)
test_data_loader  = data.DataLoader(test_data, batch_size=BATCH_SIZE, shuffle=True, num_workers=4) 
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        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):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


net = Net()
import torch.optim as optim

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(train_data, 0):
        # get the inputs
        inputs, labels = data

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0

print('Finished Training')



i am getting this error when i run this could , how can i fix ?

2 Likes

The batch dimension is missing in your input.
Usually you would iterate over the DataLoader, not the Dataset.

If you still want to use your Dataset (for whatever reasons), you could unsqueeze your data using:

inputs = inputs.unsqueeze(0)
outputs = net(inputs)

However, I would recommend to use the DataLoader to use multiple processes, shuffling, batching etc.:

for i, data in enumerate(train_data_loader):
    ...
10 Likes

@ptrblck may I ask one question? what should I do if I want the output size of the model to be (num_images, num_classes) rather than (batch_size, num_classes). I need features vectors of each images to calculate cosine similarity. thanks

If you are working with a rather small dataset, you could load all images and pass it as a single batch.
However, usually your data is too large to be loaded all at once or passed into the model together, so you could just store the results in a list and call concatenate it after the DataLoader loop.
Assuming you donā€™t need to backprop on your cosine loss, something like this should work:

outputs = []
with torch.no_grad():
    for data, target in loader:
        output = model(data)
        outputs.append(output.detach())
outputs = torch.cat(outputs)

umā€¦the error says that there are no attribute ā€˜appendā€™ in Tensor. should I convert tensor to list and then append??

outputs is initialized as a list.
Make sure you are calling outputs.append instead of output.append.

1 Like

Hi I have problem in visualize the features from pretrained model. I loaded data from image not from loaders but itā€™s showing dimension error
ā€˜ā€™ā€™# Visualize feature maps
activation = {}

def get_activation(name):
def hook(model, input, output):
activation[name] = output.detach()

return hook

model.features[0].register_forward_hook(get_activation(ā€˜features[0]ā€™))
img_path = ā€˜data/hymenoptera_data/test/terrorist/3.jpegā€™

bgr_img = cv2.imread(img_path)

device = torch.device(ā€œcuda:0ā€ if torch.cuda.is_available() else ā€œcpuā€)

Normalise

data = bgr_img.astype(ā€œfloat32ā€)/255
data=torch.from_numpy(data).float().to(device)

print(data.size())
print (data.size())
output = model(data.cpu())

act = activation[ā€˜features[0]ā€™].squeeze()
fig, axarr = plt.subplots(act.size(0))
for idx in range(act.size(0)):
axarr[idx].imshow(act[idx])
plt.show()
RuntimeError: Expected 4-dimensional input for 4-dimensional weight 64 3 3, but got 3-dimensional input of size [168, 300, 3] instead
Can anyone please help me?

I was just passing

torch.tensor([1,32,32])
into the network that thrown this error

changing to
torch.rand((1, 1, 28, 28))

soved problem.

Yes it is, I also tried this method. But image.unsqueeze() is the best approach.