After training a simple CNN with EMNIST-letters I always end up with a classification accuracy close to 93.7%, which feels a bit high. Are these results unrealistic? Is there anything I am doing wrong based on the code implementation below?
Here’s the model used in the experiments:
model = nn.Sequential(
nn.Conv2d(1,16,5,1,2),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(16,32,5,1,2),
nn.Dropout(p=0.2),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(32*7*7,400),
nn.Dropout(p=0.2),
nn.ReLU(),
nn.Linear(400,80),
nn.ReLU(),
nn.Linear(80,26),
)
return model
Here’s the train function:
def train(numb_epoch=3, lr=1e-3, device="cpu"):
accuracies = []
cnn = cnn_model().to(device)
cec = nn.CrossEntropyLoss()
optimizer = optim.Adam(cnn.parameters(), lr=lr)
max_accuracy = 0
averageTime = 0
for epoch in range(numb_epoch):
t0 = time.time()
for i, (images, labels) in enumerate(train_loader):
images = images.to(device)
labels = labels.to(device)
optimizer.zero_grad()
pred = cnn(images)
loss = cec(pred, labels)
loss.backward()
optimizer.step()
accuracy = float(validate(cnn,test_loader))
accuracies.append(accuracy)
if accuracy > max_accuracy:
best_model = copy.deepcopy(cnn)
max_accuracy = accuracy
print("Saving Best Model with Accuracy: ", accuracy)
averageTime += (time.time() - t0)
print("Time at Epoch ", epoch+1, ' {} seconds'.format(averageTime))
print("Epoch: ", epoch+1, " Accuracy: ", accuracy, "%")
plt.plot(accuracies)
averageTime = averageTime/numb_epoch
print("Average epoch time = ", '{} seconds'.format(averageTime))
return best_model
Here’s the vaidation function:
def validate(model,data):
total = 0
correct = 0
for i, (images, labels) in enumerate(data):
images = images.cuda()
x = model(images)
value, pred = torch.max(x,1)
pred = pred.data.cpu()
total += x.size(0)
correct += torch.sum(pred == labels)
return correct*100./total