-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
56 lines (38 loc) · 1.28 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
import torch
import glob
import cv2
from PIL import Image
from torchvision import transforms
import numpy as np
from base_resnet import resnet
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net = resnet()
net.load_state_dict(torch.load("/home/kuan/workspace/"
"muke/pytorch/06/"
"models/baseResnet/200.pth"))
im_list = glob.glob("/home/kuan/dataset/CIFAR10/TEST/*/*")
np.random.shuffle(im_list)
net.to(device)
label_name = ["airplane", "automobile", "bird",
"cat", "deer", "dog",
"frog", "horse", "ship", "truck"]
test_transform = transforms.Compose([
transforms.CenterCrop((28, 28)),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465),
(0.2023, 0.1994, 0.2010)),
])
for im_path in im_list:
net.eval()
im_data = Image.open(im_path)
inputs = test_transform(im_data)
inputs = torch.unsqueeze(inputs, dim=0)
inputs = inputs.to(device)
outputs = net.forward(inputs)
_, pred = torch.max(outputs.data, dim=1)
print(label_name[pred.cpu().numpy()[0]])
img = np.asarray(im_data)
img = img[:, :, [1, 2, 0]]
img = cv2.resize(img, (300, 300))
cv2.imshow("im", img)
cv2.waitKey()