-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate_img_tester.py
74 lines (57 loc) · 2.2 KB
/
evaluate_img_tester.py
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
# -*- coding: utf-8 -*-
"""
Single Image Classification Confidence Tester
Created on Sat May 30 11:58:01 2020
@author: jack_minimonster
"""
import torch
from torch.autograd import Variable as V
import torchvision.models as models
from torchvision import transforms
from torch.nn import functional as F
import model_v4 as net
import utils
import os
import argparse
from PIL import Image
parser = argparse.ArgumentParser()
parser.add_argument('--model_dir', default='model',
help="Directory containing params.json")
parser.add_argument('--restore_file', default='best', help="name of the file in --model_dir \
containing weights to load")
# Load the parameters
args = parser.parse_args()
json_path = os.path.join(args.model_dir, 'params.json')
assert os.path.isfile(
json_path), "No json configuration file found at {}".format(json_path)
params = utils.Params(json_path)
# use GPU if available
params.cuda = True # use GPU is available
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
# define model architecture
model = net.resnet50(params, 8).to(device)
checkpoint = torch.load(os.path.join(
args.model_dir, args.restore_file + '.pth.tar'), map_location=lambda storage, loc: storage)
state_dict = {str.replace(k,'module.',''): v for k,v in checkpoint['state_dict'].items()}
model.load_state_dict(state_dict)
model.eval()
# evl and test transformer
eval_transformer = transforms.Compose([
transforms.Resize(256),
transforms.ToTensor(),
transforms.Normalize((0.4789, 0.4905, 0.4740), (0.2007, 0.2004, 0.2277))])
# load the class label
classes = ('apartment', 'church', 'garage', 'house', 'industrial', 'office', 'retail', 'roof')
# load the test image
img_name = 'test.jpg'
img = Image.open(img_name)
input_img = V(eval_transformer(img).unsqueeze(0))
# forward pass
logit = model.forward(input_img.cuda())
h_x = F.softmax(logit, 1).data.squeeze()
probs, idx = h_x.sort(0, True)
print('net prediction on {}'.format(img_name))
# output the prediction
for i in range(0, 5):
print('{:.3f} -> {}'.format(probs[i], classes[idx[i]]))