TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not NoneType

Hello all,

I’m trying to run this model:

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

    self.conv1 = nn.Conv2d(3, 5, 3, stride = 2)
    self.conv2 = nn.Conv2d(5, 3, 3, stride = 1)
    self.pool1 = nn.MaxPool2d(2,2)
    self.conv3 = nn.Conv2d(3,3, 3, stride = 1)
    self.conv4 = nn.Conv2d(3,3,3, stride=1)
    self.pool2 = nn.MaxPool2d(2,2)

    self.batch = nn.BatchNorm2d(10)
    self.fc1 = nn.Linear(810, 50)
    self.fc2 = nn.Linear(50, 7)

    self.local = nn.Sequential(
        nn.Conv2d(3, 8, kernel_size = 7),
        nn.MaxPool2d(2, stride = 2),
        nn.ReLU(True),
        nn.Conv2d(8, 10, kernel_size = 5),
        nn.MaxPool2d(2, stride = 2),
        nn.ReLU(True)
    )

    self.fc_local = nn.Sequential(
        nn.Linear(640, 32),
        nn.ReLU(True),
        nn.Linear(32, 3 * 2)
    )

    self.fc_local[2].weight.data.zero_()
    self.fc_local[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype = torch.float))

  def stn(self, x):
    xs = self.local(x)
    xs = xs.view(-1, 640)
    y = self.fc_local(xs)
    y = y.view(-1, 2, 3)

  def forward(self, input):
    x = self.stn(input)
    x = F.relu(self.conv1(x))
    x = self.conv2(x)
    x = F.relu(self.pool1(x))
    x = F.relu(self.conv3(x))
    x = self.batch(self.conv4(x))
    x = F.relu(self.pool2(x))
    x = F.dropout(x)
    x = x.view(-1, 810)
    x = F.relu(self.fc1(x))
    x = self.fc2(x)

    return x

and when I try to feed it image data it returns:

TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not NoneType

I know my DataLoader is set up properly because I am able to plot the images fine and it worked with a previous model.

Does anybody have any ideas? Thanks in advance!

The code provided and error don’t seem to be informative enough.

Would be helpful to provide a snippet of where you are calling the model (in train loop, etc.), and of the whole stack trace.

Hey @rad , my bad. I’ve added the training loop below:

count = 0
loss_list = []
iteration_list = []
accuracy_list = []
epochs = 30

for epoch in range(epochs):
    for i, (images, labels) in enumerate(train_loader):

            optimiser.zero_grad()
            outputs = cnn(images)
            loss = criterion(outputs, labels)
            loss.backward()
            optimiser.step()

            count += 1

            if count % 50 == 0:
                correct = 0
                total = 0

                for images, labels in valid_loader:

                    outputs = cnn(images)
                    predicted = torch.max(outputs.data, 1)[1]
                    total += len(labels)
                    correct += (predicted == labels).sum()
                accuracy = 100 * correct / float(total)

                loss_list.append(loss.data)
                iteration_list.append(count)
                accuracy_list.append(accuracy)

                if count % 500 == 0:
                    print("Iteration: {} Loss: {} Accuracy: {} %".format(count, loss.data, accuracy))

So if you replace cnn with another network/architecture without changing anything else, does it work?