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

lavender99: <pre><code class="lang-auto">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')



</code></pre>
<p>i am getting this error when i run this could , how can i fix ?</p>

ptrblck: <p>The batch dimension is missing in your input.<br>
Usually you would iterate over the <code>DataLoader</code>, not the <code>Dataset</code>.</p>
<p>If you still want to use your <code>Dataset</code> (for whatever reasons), you could unsqueeze your data using:</p>
<pre><code class="lang-python">inputs = inputs.unsqueeze(0)
outputs = net(inputs)
</code></pre>
<p>However, I would recommend to use the <code>DataLoader</code> to use multiple processes, shuffling, batching etc.:</p>
<pre><code class="lang-python">for i, data in enumerate(train_data_loader):
    ...
</code></pre>

Saewon_Yang: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> 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</p>

ptrblck: <p>If you are working with a rather small dataset, you could load all images and pass it as a single batch.<br>
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 <code>DataLoader</code> loop.<br>
Assuming you don’t need to backprop on your cosine loss, something like this should work:</p>
<pre><code class="lang-python">outputs = []
with torch.no_grad():
    for data, target in loader:
        output = model(data)
        outputs.append(output.detach())
outputs = torch.cat(outputs)
</code></pre>

Saewon_Yang: <p>um…the error says that there are no attribute ‘append’ in Tensor. should I convert tensor to list and then append??</p>

ptrblck: <p><code>outputs</code> is initialized as a list.<br>
Make sure you are calling <code>outputs.append</code> instead of <code>output.append</code>.</p>

vijaytida: <p>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<br>
‘’’# Visualize feature maps<br>
activation = {}</p>
<p>def get_activation(name):<br>
def hook(model, input, output):<br>
activation[name] = output.detach()</p>
<pre><code>return hook
</code></pre>
<p>model.features[0].register_forward_hook(get_activation(‘features[0]’))<br>
img_path = ‘data/hymenoptera_data/test/terrorist/3.jpeg’</p>
<p>bgr_img = cv2.imread(img_path)</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)</p>
<h1>Normalise</h1>
<p>data = bgr_img.astype(“float32”)/255<br>
data=torch.from_numpy(data).float().to(device)</p>
<p>print(data.size())<br>
print (data.size())<br>
output = model(data.cpu())</p>
<p>act = activation[‘features[0]’].squeeze()<br>
fig, axarr = plt.subplots(act.size(0))<br>
for idx in range(act.size(0)):<br>
axarr[idx].imshow(act[idx])<br>
plt.show()<br>
RuntimeError: Expected 4-dimensional input for 4-dimensional weight 64 3 3, but got 3-dimensional input of size [168, 300, 3] instead<br>
Can anyone please help me?</p>

mathematics: <p>I was just passing</p>
<p><code>torch.tensor([1,32,32])</code><br>
into the network that thrown this error</p>
<p>changing to<br>
<code>torch.rand((1, 1, 28, 28))</code></p>
<p>soved problem.</p>

Hayat_Ullah: <p>Yes it is, I also tried this method. But image.unsqueeze() is the best approach.</p>