Hi, I am newbie the PyTorch, I am trying to build a CNN model for the Image classification problem, but getting some error.
Here My code:
file_path='dataset'
train=pd.read_csv('dataset/train.csv')
test=pd.read_csv('dataset/test.csv')
train_img = []
for img_name in tqdm(train['Image']):
# defining the image path
img_path=os.path.join(file_path,'Train Images',img_name)
# reading the image
img=cv2.imread(img_path)
img=cv2.resize(img,(64,64))
# appending the image into the list
train_img.append(img)
# converting the list to numpy array
train_x = np.array(train_img)
train_x, val_x, train_y, y_val = train_test_split(train_x, y, test_size = 0.1)
train_x = torch.from_numpy(np.array(train_x))
# converting the target into torch format
train_y = torch.from_numpy(np.array(train_y))
Here My model.
import torch.nn as nn
import torch.nn.functional as F
class Net(Module):
def __init__(self):
super(Net,self).__init__()
self.conv1=nn.Conv2d(3,64,5)
self.pool=nn.MaxPool2d(2, 2)
self.conv2=nn.Conv2d(64,128,5)
self.fc1 = nn.Linear(4 * 8 * 8, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 4)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 8* 8 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
model=Net()
optimizer = Adam(model.parameters(), lr=0.07)
# defining the loss function
criterion = CrossEntropyLoss()
# checking if GPU is available
if torch.cuda.is_available():
model = model.cuda()
criterion = criterion.cuda()
print(model)
def train(epoch):
model.train()
tr_loss = 0
# getting the training set
x_train, y_train = Variable(train_x), Variable(train_y)
# getting the validation set
x_val, y_val = Variable(val_x), Variable(val_y)
# converting the data into GPU format
if torch.cuda.is_available():
x_train = x_train.cuda()
y_train = y_train.cuda()
x_val = x_val.cuda()
y_val = y_val.cuda()
# clearing the Gradients of the model parameters
optimizer.zero_grad()
# prediction for training and validation set
output_train = model(x_train)
output_val = model(x_val)
# computing the training and validation loss
loss_train = criterion(output_train, y_train)
loss_val = criterion(output_val, y_val)
train_losses.append(loss_train)
val_losses.append(loss_val)
# computing the updated weights of all the model parameters
loss_train.backward()
optimizer.step()
tr_loss = loss_train.item()
if epoch%2 == 0:
# printing the validation loss
print('Epoch : ',epoch+1, '\t', 'loss :', loss_val)
n_epochs = 25
# empty list to store training losses
train_losses = []
# empty list to store validation losses
val_losses = []
# training the model
for epoch in range(n_epochs):
train(epoch)
Here the Error:
340 _pair(0), self.dilation, self.groups)
341 return F.conv2d(input, weight, self.bias, self.stride,
--> 342 self.padding, self.dilation, self.groups)
343
344 def forward(self, input):
RuntimeError: Given groups=1, weight of size 64 3 5 5, expected input[5384, 64, 64, 3] to have 3 channels, but got 64 channels instead