I am trying to train VGG1
on CIFAR-10
dataset but for some reason, it’s not training. I have tried to make sure of all the major issues like how not to add softmax
activation after the linear layer. Even cross-checked the outputs. This is how I have defined the network:
cifar_class = torchvision.models.vgg11(pretrained=False)
cifar_class.classifier.add_module('7', nn.Linear(in_features=1000, out_features=10,bias=True))
cifar_class.features.add_module('0', nn.Conv2d(in_channels=1, out_channels=64, kernel_size=3, stride=1, padding=1, bias=True))
cifar_class = cifar_class.to(device)
I want to use grayscale images and I am loading the dataset like this:
training_data = datasets.CIFAR10(root="data", train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Grayscale(num_output_channels=1)
]))
validation_data = datasets.CIFAR10(root="data", train=False, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Grayscale(num_output_channels=1)
]))
And finally, the training process:
for epoch in range(10): # loop over the dataset multiple times
running_loss = 0.0
running_corrects = 0
for i,data in enumerate(tqdm(training_loader)):
# get the inputs; data is a list of [inputs, labels
inputs, labels = data[0].cuda(), data[1].cuda()
# zero the parameter gradients
optimizer.zero_grad()
outputs = cifar_class(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
print('[%d, %5d] loss: %.3f acc: %.3f' %
(epoch + 1, i + 1, running_loss / 50000, running_corrects.double() / 50000))
running_loss = 0.0
running_corrects = 0
print('Finished Training')
What am I doing wrong?