質問編集履歴
1
コードを追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,4 +8,88 @@
|
|
8
8
|
【補足】
|
9
9
|
TensorFlow 2.1.0
|
10
10
|
Keras 2.3.1
|
11
|
-
PyTorch 1.4.0
|
11
|
+
PyTorch 1.4.0
|
12
|
+
|
13
|
+
使用したコードは下記です。
|
14
|
+
|
15
|
+
```Python
|
16
|
+
import time
|
17
|
+
|
18
|
+
def create_model(frame, arch):
|
19
|
+
if(frame == 'keras'):
|
20
|
+
import tensorflow.keras.applications as model
|
21
|
+
|
22
|
+
if(arch == 'densenet121'):
|
23
|
+
return model.DenseNet121()
|
24
|
+
if(arch == 'densenet169'):
|
25
|
+
return model.DenseNet169()
|
26
|
+
if(arch == 'densenet201'):
|
27
|
+
return model.DenseNet201()
|
28
|
+
|
29
|
+
elif(frame == 'pytorch'):
|
30
|
+
import torch
|
31
|
+
import torchvision.model as model
|
32
|
+
|
33
|
+
if(arch == 'densenet121'):
|
34
|
+
model = model.densenet121(pretrained=True).cuda()
|
35
|
+
if(arch == 'densenet169'):
|
36
|
+
model = model.densenet169(pretrained=True).cuda()
|
37
|
+
if(arch == 'densenet201'):
|
38
|
+
model = model.densenet201(pretrained=True).cuda()
|
39
|
+
|
40
|
+
model.eval()
|
41
|
+
model.to(torch.device('cuda'))
|
42
|
+
|
43
|
+
return model
|
44
|
+
|
45
|
+
|
46
|
+
LOOP = 10
|
47
|
+
img_path = 'xxx.jpg'
|
48
|
+
frame = 'keras'
|
49
|
+
name = 'densenet121'
|
50
|
+
|
51
|
+
model = create_model(frame, name)
|
52
|
+
|
53
|
+
# inference
|
54
|
+
if(frame == 'keras'):
|
55
|
+
import numpy
|
56
|
+
from tensorflow.keras.preprocessing import image
|
57
|
+
|
58
|
+
start = time.perf_counter()
|
59
|
+
for i in range(LOOP):
|
60
|
+
img = image.load_img(img_path, target_size=(224, 224))
|
61
|
+
img = image.img_to_array(img)
|
62
|
+
img = numpy.expand_dims(img, axis=0)
|
63
|
+
preds = model.predict(img)
|
64
|
+
elapsed_time = (time.perf_counter() - start) / LOOP_SIZE * 1000
|
65
|
+
fps = 1 / elapsed_time * 1000
|
66
|
+
|
67
|
+
elif(frame == 'pytorch'):
|
68
|
+
import torchvision.transforms as transforms
|
69
|
+
from PIL import Image
|
70
|
+
import torch
|
71
|
+
from torch.autograd import Variable
|
72
|
+
device = torch.device('cuda')
|
73
|
+
transformation = transforms.Compose(
|
74
|
+
[
|
75
|
+
transforms.Resize([224, 224]),
|
76
|
+
transforms.ToTensor(),
|
77
|
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
78
|
+
]
|
79
|
+
)
|
80
|
+
|
81
|
+
start = time.perf_counter()
|
82
|
+
for i in range(LOOP):
|
83
|
+
img = Image.open(img_path)
|
84
|
+
img = transformation(img).float()
|
85
|
+
img = img.unsqueeze_(0)
|
86
|
+
img = Variable(img)
|
87
|
+
img = img.to(device)
|
88
|
+
preds = model(img)
|
89
|
+
elapsed_time = (time.perf_counter() - start) / LOOP_SIZE * 1000
|
90
|
+
fps = 1 / elapsed_time * 1000
|
91
|
+
|
92
|
+
line = '| %.3g | %.3g |' % (fps, elapsed_time)
|
93
|
+
print('| FPS | Throughput |')
|
94
|
+
print(line)
|
95
|
+
```
|