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

username: ziad
cooked: <p>Hello all,</p>
<p>I’m trying to run this model:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>and when I try to feed it image data it returns:</p>
<pre><code class="lang-auto">TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not NoneType
</code></pre>
<p>I know my DataLoader is set up properly because I am able to plot the images fine and it worked with a previous model.</p>
<p>Does anybody have any ideas? Thanks in advance!</p>

username: rad
cooked: <p>The code provided and error don’t seem to be informative enough.</p>
<p>Would be helpful to provide a snippet of where you are calling the model (in train loop, etc.), and of the whole stack trace.</p>

username: ziad
cooked: <p>Hey <a class="mention" href="/u/rad">@rad</a> , my bad. I’ve added the training loop below:</p>
<pre><code class="lang-auto">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))
</code></pre>

username: rad
cooked: <p>So if you replace <code>cnn</code> with another network/architecture without changing anything else, does it work?</p>