Keras:使用InceptionV3、ResNet50模型进行图片分类

用Keras构建网络并使用其提供的预训练权重进行简单的图像分类.

其中decode_predictions()将结果解码为元组列表,内容包括(类别,描述,概率).

使用InceptionV3进行图片分类

#!/usr/bin/python
# coding:utf8

from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_v3 import decode_predictions
from keras.preprocessing import image
import numpy as np
import cv2

model = InceptionV3(weights='imagenet', include_top=True)

img_path = "bird1.jpg"
img = image.load_img(img_path, target_size=(299, 299))
img = image.img_to_array(img) / 255.0
img = np.expand_dims(img, axis=0)  # 为batch添加第四维

predictions = model.predict(img)
print('Predicted:', decode_predictions(predictions, top=3))

description = decode_predictions(predictions, top=3)[0][0][1]

src = cv2.imread(img_path)
cv2.putText(src, description, (50,50), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,0,0), 2)
cv2.imshow("Predicted", src)
cv2.waitKey()

输出:

('Predicted:', [[(u'n02110185', u'Siberian_husky', 0.734093), (u'n02109961', u'Eskimo_dog', 0.17709365), (u'n02110063', u'malamute', 0.013321317)]])

这里写图片描述

使用ResNet50进行图片分类

#!/usr/bin/python
# coding:utf8

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
import cv2

model = ResNet50(weights='imagenet')

img_path = 'bird.jpg'
img = image.load_img(img_path, target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)

predictions = model.predict(img)
print('Predicted:', decode_predictions(predictions, top=3))
description = decode_predictions(predictions, top=3)[0][0][1]

src = cv2.imread(img_path)
cv2.putText(src, description, (50,50), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,0,0), 2)
cv2.imshow("Predicted", src)
cv2.waitKey()

输出:

('Predicted:', [[(u'n01531178', u'goldfinch', 0.88929653), (u'n01537544', u'indigo_bunting', 0.0318476), (u'n01560419', u'bulbul', 0.024045745)]])

这里写图片描述


Keras::Docs » 协助使用Keras » 预训练模型Application

imagenet_class_index.json下载

猜你喜欢

转载自blog.csdn.net/akadiao/article/details/80386115