GabrielePicco / deep-learning-flower-identifier

Deep learning network to identify 102 different types of flowers
MIT License
53 stars 33 forks source link

Hard Coded Transformations #1

Closed AdityaSoni19031997 closed 5 years ago

AdityaSoni19031997 commented 5 years ago

Hi Gabriel! We shouldn't be applying hard coded transformations as such while testing, rather allow the user to define one and raise assertion error if they didn't :)

https://github.com/GabrielePicco/deep-learning-flower-identifier/blob/master/test_model_pytorch_facebook_challenge.py#L33-L38

Also somehow the code for calculating Accuracy wasn't working for me [PIL Register Decode Error even after applying the fix..], don't know why, so we can replace that with this (self curated version plus someone shared this and i modified to meet my necessities)

data_dir = 'google_test_data'

#sample transformations
transformation = transforms.Compose([
    transforms.Resize((299,299)),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

image_dataset = datasets.ImageFolder(data_dir, transform= transformation)
dataloader = torch.utils.data.DataLoader(image_dataset, batch_size= 128, shuffle=True)

#######validation##########
# track valid loss
valid_loss = 0.0
class_correct = list(0. for i in range(102))
class_total   = list(0. for i in range(102))

model.eval().to(device)
# iterate over valid data
for data, target in dataloader:
    # move tensors to GPU if CUDA is available
    data, target = data.to(device), target.to(device)
    # forward pass: compute predicted outputs by passing inputs to the model
    output = model(data)
    _, pred = torch.max(output, 1)
    # compare predictions to true label
    correct_tensor = pred.eq(target.data.view_as(pred))
    correct = np.squeeze(correct_tensor.numpy()) if not device else np.squeeze(correct_tensor.cpu().numpy())
    # calculate valid accuracy for each object class
    for i in range(len(data)):
        label = target.data[i]
        class_correct[label] += correct[i].item()
        class_total[label] += 1

for i in range(102):
    if class_total[i] > 0:
        print('Validation Accuracy of %5s %s %s: %2d%% (%2d/%2d)' % (
            model_pred_to_folder[i],'->',final[i], 100 * class_correct[i] / class_total[i],
            np.sum(class_correct[i]), np.sum(class_total[i])))

print('\nValidation Accuracy (Overall): %2d%% (%2d/%2d)' % (
    100. * np.sum(class_correct) / np.sum(class_total),
    np.sum(class_correct), np.sum(class_total)))

Sample Output

Validation Accuracy of     1 -> pink primrose: 60% ( 6/10)
Validation Accuracy of    10 -> globe thistle: 90% ( 9/10)
Validation Accuracy of   100 -> blanket flower: 80% ( 8/10)
Validation Accuracy of   101 -> trumpet creeper: 80% ( 8/10)
Validation Accuracy of   102 -> blackberry lily: 90% ( 9/10)
Validation Accuracy of    11 -> snapdragon: 80% ( 8/10)

(PS We can replace the Word Validation with Test) Again Thanks a lot for the dataset !!

GabrielePicco commented 5 years ago

Thanks for your feedback. The transformations are used to test whether the model is sufficiently general. They are deterministic, therefore the calculation of the accuracy is, in any case, the same for every execution. Thanks for your code, if you want to contribute feel free to make a merge request on the repository.