質問編集履歴
1
コードを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,3 +19,171 @@
|
|
19
19
|
Keras 2.3.1
|
20
20
|
|
21
21
|
PyTorch 1.4.0
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
使用したコードは下記です。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```Python
|
30
|
+
|
31
|
+
import time
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
def create_model(frame, arch):
|
36
|
+
|
37
|
+
if(frame == 'keras'):
|
38
|
+
|
39
|
+
import tensorflow.keras.applications as model
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
if(arch == 'densenet121'):
|
44
|
+
|
45
|
+
return model.DenseNet121()
|
46
|
+
|
47
|
+
if(arch == 'densenet169'):
|
48
|
+
|
49
|
+
return model.DenseNet169()
|
50
|
+
|
51
|
+
if(arch == 'densenet201'):
|
52
|
+
|
53
|
+
return model.DenseNet201()
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
elif(frame == 'pytorch'):
|
58
|
+
|
59
|
+
import torch
|
60
|
+
|
61
|
+
import torchvision.model as model
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
if(arch == 'densenet121'):
|
66
|
+
|
67
|
+
model = model.densenet121(pretrained=True).cuda()
|
68
|
+
|
69
|
+
if(arch == 'densenet169'):
|
70
|
+
|
71
|
+
model = model.densenet169(pretrained=True).cuda()
|
72
|
+
|
73
|
+
if(arch == 'densenet201'):
|
74
|
+
|
75
|
+
model = model.densenet201(pretrained=True).cuda()
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
model.eval()
|
80
|
+
|
81
|
+
model.to(torch.device('cuda'))
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
return model
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
LOOP = 10
|
92
|
+
|
93
|
+
img_path = 'xxx.jpg'
|
94
|
+
|
95
|
+
frame = 'keras'
|
96
|
+
|
97
|
+
name = 'densenet121'
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
model = create_model(frame, name)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
# inference
|
106
|
+
|
107
|
+
if(frame == 'keras'):
|
108
|
+
|
109
|
+
import numpy
|
110
|
+
|
111
|
+
from tensorflow.keras.preprocessing import image
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
start = time.perf_counter()
|
116
|
+
|
117
|
+
for i in range(LOOP):
|
118
|
+
|
119
|
+
img = image.load_img(img_path, target_size=(224, 224))
|
120
|
+
|
121
|
+
img = image.img_to_array(img)
|
122
|
+
|
123
|
+
img = numpy.expand_dims(img, axis=0)
|
124
|
+
|
125
|
+
preds = model.predict(img)
|
126
|
+
|
127
|
+
elapsed_time = (time.perf_counter() - start) / LOOP_SIZE * 1000
|
128
|
+
|
129
|
+
fps = 1 / elapsed_time * 1000
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
elif(frame == 'pytorch'):
|
134
|
+
|
135
|
+
import torchvision.transforms as transforms
|
136
|
+
|
137
|
+
from PIL import Image
|
138
|
+
|
139
|
+
import torch
|
140
|
+
|
141
|
+
from torch.autograd import Variable
|
142
|
+
|
143
|
+
device = torch.device('cuda')
|
144
|
+
|
145
|
+
transformation = transforms.Compose(
|
146
|
+
|
147
|
+
[
|
148
|
+
|
149
|
+
transforms.Resize([224, 224]),
|
150
|
+
|
151
|
+
transforms.ToTensor(),
|
152
|
+
|
153
|
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
154
|
+
|
155
|
+
]
|
156
|
+
|
157
|
+
)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
start = time.perf_counter()
|
162
|
+
|
163
|
+
for i in range(LOOP):
|
164
|
+
|
165
|
+
img = Image.open(img_path)
|
166
|
+
|
167
|
+
img = transformation(img).float()
|
168
|
+
|
169
|
+
img = img.unsqueeze_(0)
|
170
|
+
|
171
|
+
img = Variable(img)
|
172
|
+
|
173
|
+
img = img.to(device)
|
174
|
+
|
175
|
+
preds = model(img)
|
176
|
+
|
177
|
+
elapsed_time = (time.perf_counter() - start) / LOOP_SIZE * 1000
|
178
|
+
|
179
|
+
fps = 1 / elapsed_time * 1000
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
line = '| %.3g | %.3g |' % (fps, elapsed_time)
|
184
|
+
|
185
|
+
print('| FPS | Throughput |')
|
186
|
+
|
187
|
+
print(line)
|
188
|
+
|
189
|
+
```
|