kerasのimagenetモデルを使ってみる。


概要

kerasのimagenetモデルを使ってみる。

写真

Xceptionの場合

Predicted: [('n04179913', 'sewing_machine', 1.0), ('n15075141', 'toilet_tissue', 0.0), ('n02317335', 'starfish', 0.0)]
Time elapsed: 12

サンプルコード

from tensorflow.contrib.keras.python.keras.applications.xception import Xception
from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.imagenet_utils import preprocess_input, decode_predictions
import numpy as np
import time

img_path = 'p1m.jpg'
img = image.load_img(img_path, target_size = (299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
x = preprocess_input(x)
begin = time.clock()
model = Xception(weights = 'imagenet')
preds = model.predict(x)
print ('Predicted:', decode_predictions(preds, top = 3)[0])
print ('Time elapsed: %.0f' % (time.clock() - begin))


ResNet50の場合

Predicted: [('n02098286', 'West_Highland_white_terrier', 1.0), ('n15075141', 'toilet_tissue', 0.0), ('n02319095', 'sea_urchin', 0.0)]
Time elapsed: 14

サンプルコード

from tensorflow.contrib.keras.python.keras.applications.resnet50 import ResNet50
from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.imagenet_utils import preprocess_input, decode_predictions
import numpy as np
import time

img_path = 'p1m.jpg'
img = image.load_img(img_path, target_size = (224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
x = preprocess_input(x)
begin = time.clock()
model = ResNet50(weights = 'imagenet')
preds = model.predict(x)
print ('Predicted:', decode_predictions(preds, top = 3)[0])
print ('Time elapsed: %.0f' % (time.clock() - begin))


InceptionV3の場合

Predicted: [('n01924916', 'flatworm', 0.8119747), ('n04328186', 'stopwatch', 0.16520669), ('n06359193', 'web_site', 0.0094488058)]
Time elapsed: 26

サンプルコード

from tensorflow.contrib.keras.python.keras.applications.inception_v3 import InceptionV3
from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.imagenet_utils import preprocess_input, decode_predictions
import numpy as np
import time

img_path = 'p1m.jpg' #0.png 1.png
img = image.load_img(img_path, target_size = (299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
x = preprocess_input(x)
begin = time.clock()
model = InceptionV3(weights = 'imagenet')
preds = model.predict(x)
print ('Predicted:', decode_predictions(preds, top = 3)[0])
print ('Time elapsed: %.0f' % (time.clock() - begin))

VGG16の場合

Predicted: [('n03785016', 'moped', 0.51865774), ('n03791053', 'motor_scooter', 0.36139873), ('n03445924', 'golfcart', 0.049456675)]
Time elapsed: 98

サンプルコード

from tensorflow.contrib.keras.python.keras.applications.vgg16 import VGG16
from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.imagenet_utils import preprocess_input, decode_predictions
import numpy as np
import time

img_path = 'p1m.jpg' #0.png 1.png
img = image.load_img(img_path, target_size = (224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
x = preprocess_input(x)
begin = time.clock()
model = VGG16(weights = 'imagenet')
preds = model.predict(x)
print ('Predicted:', decode_predictions(preds, top = 3)[0])
print ('Time elapsed: %.0f' % (time.clock() - begin))

以上。