-
Notifications
You must be signed in to change notification settings - Fork 1
/
230205_main_weapon_inference_test.py
executable file
·91 lines (73 loc) · 2.49 KB
/
230205_main_weapon_inference_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#%%
from __future__ import print_function, division
import torch
from torchvision import transforms
from PIL import Image
import torch.nn.functional as F
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class ImageTransform():
def __init__(self, mean, std):
self.data_transform = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
def __call__(self, img):
return self.data_transform(img)
mean = (0.5,)
std = (0.5,)
transform = ImageTransform(mean, std)
f = open("main_weapon_list.txt","r")
class_names= []
for x in f:
class_names.append(x.rstrip("\n"))
#以下のようにしてしまうと、改行コードがlistに入ってしまうため注意
#list_row.append(x)
f.close()
#%%
img = Image.open("dinamo.jpg")
inputs = transform(img)
inputs = inputs.unsqueeze(0).to(device)
model = torch.load('main_weapons_classification_weight.pth')
model.eval() ## torch.nn.Module.eval
with torch.no_grad():
outputs = model(inputs)
batch_probs = F.softmax(outputs, dim=1)
batch_probs, batch_indices = batch_probs.sort(dim=1, descending=True)
for probs, indices in zip(batch_probs, batch_indices):
for k in range(1):
print(k)
print(indices[k])
print(class_names[indices[k]])
# %%
img = Image.open("sharpmarker.jpg")
inputs = transform(img)
inputs = inputs.unsqueeze(0).to(device)
model = torch.load('main_weapons_classification_weight.pth')
model.eval() ## torch.nn.Module.eval
with torch.no_grad():
outputs = model(inputs)
batch_probs = F.softmax(outputs, dim=1)
batch_probs, batch_indices = batch_probs.sort(dim=1, descending=True)
for probs, indices in zip(batch_probs, batch_indices):
for k in range(1):
print(k)
print(indices[k])
print(class_names[indices[k]])
#%%
img = Image.open("sharpmarker.jpg")
#img = Image.open("dinamo.jpg")
inputs = transform(img)
inputs = inputs.unsqueeze(0).to(device)
model = torch.load('main_weapons_classification_weight.pth')
model.eval() ## torch.nn.Module.eval
with torch.no_grad():
outputs = model(inputs)
batch_probs = F.softmax(outputs, dim=1)
batch_probs, batch_indices = batch_probs.sort(dim=1, descending=True)
for probs, indices in zip(batch_probs, batch_indices):
for k in range(3):
print(k)
print(indices[k])
print(class_names[indices[k]])
# %%