In [5]:
import numpy as np
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
from matplotlib import pyplot as plt
%matplotlib inline

Создаем модель с архитектурой VGG16 и загружаем веса, обученные на наборе данных ImageNet

In [2]:
%%time
model = VGG16(weights='imagenet')
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5
553467904/553467096 [==============================] - 116s       - ETA: 32s - ETA: 31s - ETA: 29s - ETA: 27s - ETA: 27s - ETA: 25s - ETA: 11s
CPU times: user 13.4 s, sys: 4.63 s, total: 18.1 s
Wall time: 2min
In [3]:
img_path = '../img/ship.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)
In [8]:
plt.imshow(img);

Запускаем распознавание объекта на изображении

In [15]:
preds = model.predict(x)
decode_predictions(preds, top=3)[0]
Out[15]:
[(u'n04147183', u'schooner', 0.55734116),
 (u'n04612504', u'yawl', 0.25635162),
 (u'n02981792', u'catamaran', 0.094704673)]
In [19]:
img2 = image.load_img('../img/yury.jpg', target_size=(224, 224))
x2 = image.img_to_array(img2)
x2 = np.expand_dims(x2, axis=0)
x2 = preprocess_input(x2)
In [20]:
plt.imshow(img2);
In [21]:
preds = model.predict(x2)
decode_predictions(preds, top=3)[0]
Out[21]:
[(u'n04350905', u'suit', 0.32556185),
 (u'n02883205', u'bow_tie', 0.083727621),
 (u'n02804610', u'bassoon', 0.056897432)]
In [22]:
img3 = image.load_img('../img/burger.jpg', target_size=(224, 224))
x3 = image.img_to_array(img3)
x3 = np.expand_dims(x3, axis=0)
x3 = preprocess_input(x3)
In [23]:
plt.imshow(img3);
In [24]:
preds = model.predict(x3)
decode_predictions(preds, top=3)[0]
Out[24]:
[(u'n07697313', u'cheeseburger', 0.94715017),
 (u'n07693725', u'bagel', 0.046281256),
 (u'n07695742', u'pretzel', 0.0025629266)]
In [ ]: