Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

概要

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

写真

p1m.jpg

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))

以上。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away