-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
68 lines (51 loc) · 1.4 KB
/
test.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
from model import HyperResNet34
import torchvision.transforms as T
import numpy as np
import torch
from PIL import Image
import argparse
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser(description='Test our model')
parser.add_argument('-m','--model', help='Path to the trained model', type=str ,required= True)
parser.add_argument('-i','--image', help='Path to the image', type=str, required= True)
parser.add_argument('-n','--nbClass', help='Number of class', type=int, required= True)
args = parser.parse_args()
def main(args):
# model loading...
model = HyperResNet34(args.nbClass)
state = torch.load(args.model)
model.load_state_dict(state['state_dict'])
# optimizer.load_state_dict(state['optimizer'])
model.eval()
# Transformation
transform = T.Compose(
[
T.Resize((224, 224)),
T.ToTensor(),
]
)
from torch.autograd.grad_mode import no_grad
imag = Image.open(args.image).convert('RGB')
imag = transform(imag)
imag = np.expand_dims(imag, axis=0)
imag = torch.from_numpy(imag)
colors={
0: 'Black',
1: 'Blue',
2: 'Brown',
3: 'Green',
4: 'Orange',
5: 'Red',
6: 'Violet',
7: 'White',
8: 'Yellow'
}
with torch.no_grad():
out = model(imag)
show = np.squeeze(imag.permute(0,2,3,1))
plt.imshow(show)
plt.axis('off')
plt.title(colors[out.max(1)[1].cpu().item()])
plt.show()
if __name__ == '__main__':
main(args)