-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
89 lines (70 loc) · 2.63 KB
/
evaluate.py
File metadata and controls
89 lines (70 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import argparse
import os
import numpy as np
from torch.autograd import Variable
import torch
from tqdm import tqdm
from models.model_finetunning import ResNet18_fineTune
parser = argparse.ArgumentParser(description='RecVis A3 evaluation script')
parser.add_argument('--data', type=str, default='bird_dataset', metavar='D',
help="folder where data is located. test_images/ need to be found in the folder")
parser.add_argument('--model', type=str, metavar='M',
help="the model file to be evaluated. Usually it is of the form model_X.pth")
parser.add_argument('--outfile', type=str, default='experiment/kaggle_template.csv', metavar='D',
help="name of the output csv file")
args = parser.parse_args()
use_cuda = torch.cuda.is_available()
state_dict = torch.load(args.model)
#Model
model = ResNet18_fineTune()
model.load_state_dict(state_dict['model'])
model.eval()
if use_cuda:
print('Using GPU')
model.cuda()
else:
print('Using CPU')
from data import data_transforms
test_dir = args.data + '/test_images/mistery_category'
def process_image(image_path):
'''
Scales, crops, and normalizes a PIL image for a PyTorch
model, returns an Numpy array
'''
# Open the image
from PIL import Image
img = Image.open(image_path)
# Resize
if img.size[0] > img.size[1]:
img.thumbnail((10000, 256))
else:
img.thumbnail((256, 10000))
# Crop
left_margin = (img.width - 224) / 2
bottom_margin = (img.height - 224) / 2
right_margin = left_margin + 224
top_margin = bottom_margin + 224
img = img.crop((left_margin, bottom_margin, right_margin,
top_margin))
# Normalize
img = np.array(img) / 255
mean = np.array([0.485, 0.456, 0.406]) # provided mean
std = np.array([0.229, 0.224, 0.225]) # provided std
img = (img - mean) / std
# Move color channels to first dimension as expected by PyTorch
img = img.transpose((2, 0, 1))
return img
output_file = open(args.outfile, "w")
output_file.write("Id,Category\n")
for f in tqdm(os.listdir(test_dir)):
if 'jpg' in f:
img = process_image(test_dir + '/' + f)
# Numpy -> Tensor
image_tensor = torch.from_numpy(img).type(torch.FloatTensor)
# Add batch of size 1 to image
model_input = image_tensor.unsqueeze(0)
output = model(Variable(model_input.cuda()))
pred = output.data.max(1, keepdim=True)[1]
output_file.write("%s,%d\n" % (f[:-4], pred))
output_file.close()
print("Succesfully wrote " + args.outfile + ', you can upload this file to the kaggle competition website')