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下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章