av-savchenko / face-emotion-recognition

Efficient face emotion recognition in photos and videos
Apache License 2.0
686 stars 127 forks source link

Scoring error #17

Closed sunggukcha closed 2 years ago

sunggukcha commented 2 years ago

Hello. Thanks for sharing your great works.

Your scoring script is correct only if len(dataloader) % batch_size == 0.

epoch_val_accuracy = 0
epoch_val_loss = 0
for data, label in test_loader:
    data = data.to(device)
    label = label.to(device)

    val_output = model(data)
    val_loss = criterion(val_output, label)

    acc = (val_output.argmax(dim=1) == label).float().mean()
    epoch_val_accuracy += acc / len(test_loader)
    epoch_val_loss += val_loss / len(test_loader)

My version follows where length is the size of the dataset (not which of dataloader).

loss = 0.0
accuracy = 0.0
for (images, emotions) in tqdm(dataloader):
    images = images.cuda()
    emotions = emotions.cuda()
    preds = model(images)
    # loss
    loss += criterion(preds, emotions) / 1
    # accuracy
    preds = torch.argmax(preds, dim=1)
    acc = torch.eq(preds, emotions).sum()
    accuracy += acc
loss /= length
accuracy /= length
av-savchenko commented 2 years ago

When I test the final quality of the model, everything was correct, but the evaluation in the training loop was slightly wrong. Thank you so much for pointing me this error. I've just changed train_emotions-pytorch.ipynb and train_faces_torch.ipynb in my repository, and everything seems to work as expected