Error while running CNN for grayscale image

sarthak1996: My net: <SCODE>class convNet(nn.Module):
    #constructor
    def __init__(self):
        super(convNet, self).__init__()
        #defining layers in convnet
        #input size=32x32x3
        self.conv1 = nn.Conv2d(1, 96, kernel_size=3,stride=1,padding=1)
        #conv1: 28x28x1 -> 28x28x96
        self.conv2 = nn.Conv2d(96, 256, kernel_size=3,stride=1,padding=1)
        #conv2: 28x28x96 -> 28x28x256
        self.conv3 = nn.Conv2d(256,384,kernel_size=3,stride=1,padding=1)
        #conv3: 28x28x256 -> 28x28x384
        #pooling: 28x28x384 -> 14x14x384
        self.conv4 = nn.Conv2d(384,512,kernel_size=3,stride=1,padding=1)
        #conv4: 14x14x384 -> 14x14x512
        #pooling: 14x14x512 -> 7x7x512        
        self.fc1   = nn.Linear(7*7*512, 300)
        self.fc2   = nn.Linear(300, 100)
        self.fc3   = nn.Linear(100, 10)
    
def forward(self, x):
    conv1_relu = nnFunctions.relu(self.conv1(x))
    conv2_relu = nnFunctions.relu(self.conv2(conv1_relu))
    conv3_relu =nnFunctions.max_pool2d(nnFunctions.relu(self.conv3(conv2_relu)),2)
    conv4_relu =nnFunctions.max_pool2d(nnFunctions.relu(self.conv4(conv3_relu)),2)

    x = conv4_relu.view(-1, 7*7*512)
    x = nnFunctions.relu(self.fc1(x))
    x = nnFunctions.relu(self.fc2(x))
    x = self.fc3(x)
    return x
<ECODE> But it gives me the following error: <SCODE>RuntimeError: CHECK_ARG((long)pad.size() == input->nDimension - 2) failed at torch/csrc/cudnn/Conv.cpp:258<ECODE>

gsp-27: Are you sure you are passing the image as (1, 28, 28). It accepts input as (N, C, H, W). It runs on my computer perfectly. <SCODE>import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

class channel1(nn.Module):
	def __init__(self):
		super(channel1, self).__init__()
		self.conv1 = nn.Conv2d(1, 96, kernel_size=3, stride=1, padding=1)
		self.relu1 = nn.ReLU(inplace=True)
		self.conv2 = nn.Conv2d(96, 10, kernel_size=3, stride=1, padding=1)

	def forward(self, x):
		x = self.conv1(x)
		x = self.relu1(x)
		x = self.conv2(x)
		return x

if __name__ == '__main__':
	someNet = channel1()
	x = Variable(torch.randn(1, 1, 28, 28))
	print someNet(x)
<ECODE>

gsp-27: your code runs too on my system <SCODE>if __name__ == '__main__':
    net = convNet()
    x = Variable(torch.randn(10, 1, 28, 28))
    print(net(x))
<ECODE>

apaszke: 

sarthak1996: This is my complete code: <SCODE>np_data.shape                ->(42000, 784)
np_data=np_data.astype(np.float32).reshape(42000,28,28,1)
np_labels.shape              ->(42000, 1)
np_data.shape                ->(42000, 28, 28, 1)
np_data=np.transpose(np_data,[0,3,1,2])
np_data.shape                 ->(42000, 1, 28, 28)

batch_size=50
features=torch.from_numpy(np_data)
features=features.contiguous().view(42000,-1)
targets=torch.from_numpy(np_labels)

import torch.utils.data as data_utils

train = data_utils.TensorDataset(features, targets)
train_loader = data_utils.DataLoader(train, batch_size=batch_size, shuffle=True)

criterion = nn.CrossEntropyLoss() # use a Classification Cross-Entropy loss
import torch.optim as optim

def trainConvNet(train_loader,net,criterion,epochs,total_samples,learning_rate):
    prev_loss=0
    
    optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9)
    for epoch in range(int(epochs)): # loop over the dataset multiple times

        running_loss = 0.0
        for i,data in enumerate(train_loader):

            inputs,labels=data
            # wrap them in Variable
            inputs, labels = Variable(inputs).cuda(), Variable(labels).cuda()
            
            # zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            outputs = net(inputs)
            loss = criterion(outputs, labels[:,0])
            loss.backward()        
            optimizer.step()
            # print statistics
            running_loss += loss.data[0]
    print('Finished Training')
    return net


class convNet(nn.Module):
    #constructor
    def __init__(self):
        super(convNet, self).__init__()
        #defining layers in convnet
        #input size=32x32x3
        self.conv1 = nn.Conv2d(1, 96, kernel_size=3,stride=1,padding=1)
        #conv1: 28x28x1 -> 28x28x96
        self.conv2 = nn.Conv2d(96, 256, kernel_size=3,stride=1,padding=1)
        #conv2: 28x28x96 -> 28x28x256
        self.conv3 = nn.Conv2d(256,384,kernel_size=3,stride=1,padding=1)
        #conv3: 28x28x256 -> 28x28x384
        #pooling: 28x28x384 -> 14x14x384
        self.conv4 = nn.Conv2d(384,512,kernel_size=3,stride=1,padding=1)
        #conv4: 14x14x384 -> 14x14x512
        #pooling: 14x14x512 -> 7x7x512        
        self.fc1   = nn.Linear(7*7*512, 300)
        self.fc2   = nn.Linear(300, 100)
        self.fc3   = nn.Linear(100, 10)
        
    def forward(self, x):
        x = nnFunctions.relu(self.conv1(x))
        x = nnFunctions.relu(self.conv2(x))
        x = nnFunctions.max_pool2d(nnFunctions.relu(self.conv3(x)),2)
        x = nnFunctions.max_pool2d(nnFunctions.relu(self.conv4(x)),2)
    
        x = x.view(-1, 7*7*512)
        x = nnFunctions.relu(self.fc1(x))
        x = nnFunctions.relu(self.fc2(x))
        x = self.fc3(x)
        return x
net=convNet()
net.cuda()
net=trainConvNet(train_loader,net,criterion,1,42000,0.01)
<ECODE> I dont understand why am I getting the above error.

apaszke: 

sarthak1996: