Keras予備訓練のImageNetモデルは分類操作を実現します。


本論文では、事前訓練のImageNetモデルによる画像分類を紹介します。主に使用されるネットワーク構造はVGG 16、InceptionV 3、Reset Net 50、MobileNetです。
コード:

import keras
import numpy as np
from keras.applications import vgg16, inception_v3, resnet50, mobilenet
 
#     
vgg_model = vgg16.VGG16(weights='imagenet')
inception_model = inception_v3.InceptionV3(weights='imagenet')
resnet_model = resnet50.ResNet50(weights='imagenet')
mobilenet_model = mobilenet.MobileNet(weights='imagenet')
 
#             
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
%matplotlib inline
 
filename= 'images/cat.jpg'
 
#                
'''
1、    ,load_img
2、    PIL     Numpy  ,image_to_array
3、       ,Numpy expand_dims
'''
#  PIL      
original = load_img(filename, target_size=(224, 224))
print('PIL image size', original.size)
plt.imshow(original)
plt.show()
 
#       PIL     Numpy  
# In PIL--    (width, height, channel)
# In Numpy――   (height, width, channel)
numpy_image = img_to_array(original)
plt.imshow(np.uint8(numpy_image))
plt.show()
print('numpy array size', numpy_image.size)
 
#    /         
# expand_dims                
#            (    ,  ,  ,  )
#   ,          0。
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
plt.imshow(np.uint8(image_batch[0]))
 
#           
#                            。 
#        ImageNet        R,G,B                
#           
#              
# VGG16     
#     VGG          
processed_image = vgg16.preprocess_input(image_batch.copy())
 
#                 
predictions = vgg_model.predict(processed_image)
#      
#             
#                  
label_vgg = decode_predictions(predictions)
label_vgg
 
# ResNet50    
#     ResNet50          
processed_image = resnet50.preprocess_input(image_batch.copy())
 
#                 
predictions = resnet_model.predict(processed_image)
 
#          
#       3   ,    top     
label_resnet = decode_predictions(predictions, top=3)
label_resnet
 
# MobileNet    
#     MobileNet          
processed_image = mobilenet.preprocess_input(image_batch.copy())
 
#                
predictions = mobilenet_model.predict(processed_image)
 
#          
label_mobilnet = decode_predictions(predictions)
label_mobilnet
 
# InceptionV3    
#                 。         (299,299)。
#   ,              。
#      PIL  
original = load_img(filename, target_size=(299, 299))
 
#  PIL        Numpy  
numpy_image = img_to_array(original)
 
#           
image_batch = np.expand_dims(numpy_image, axis=0)
 
#         InceptionV3       
processed_image = inception_v3.preprocess_input(image_batch.copy())
 
#                 
predictions = inception_model.predict(processed_image)
 
#          
label_inception = decode_predictions(predictions)
label_inception
 
import cv2
numpy_image = np.uint8(img_to_array(original)).copy()
numpy_image = cv2.resize(numpy_image,(900,900))
 
cv2.putText(numpy_image, "VGG16: {}, {:.2f}".format(label_vgg[0][0][1], label_vgg[0][0][2]) , (350, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3)
cv2.putText(numpy_image, "MobileNet: {}, {:.2f}".format(label_mobilenet[0][0][1], label_mobilenet[0][0][2]) , (350, 75), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3)
cv2.putText(numpy_image, "Inception: {}, {:.2f}".format(label_inception[0][0][1], label_inception[0][0][2]) , (350, 110), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3)
cv2.putText(numpy_image, "ResNet50: {}, {:.2f}".format(label_resnet[0][0][1], label_resnet[0][0][2]) , (350, 145), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3)
numpy_image = cv2.resize(numpy_image, (700,700))
cv2.imwrite("images/{}_output.jpg".format(filename.split('/')[-1].split('.')[0]),cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR))
 
plt.figure(figsize=[10,10])
plt.imshow(numpy_image)
plt.axis('off')
訓練データ:

実行結果:

以上のKerasの事前訓練のImageNetモデルは分類操作を実現しました。小編纂は皆さんに全部の内容を共有しています。参考にしてもらいたいです。どうぞよろしくお願いします。