TensorFlow2教程-使用预训练CNN模型

Tensorflow 2.0 教程持续更新 :https://blog.csdn.net/qq_31456593/article/details/88606284

完整tensorflow2.0教程代码请看tensorflow2.0:中文教程tensorflow2_tutorials_chinese(欢迎star)

入门教程:
TensorFlow 2.0 教程- Keras 快速入门
TensorFlow 2.0 教程-keras 函数api
TensorFlow 2.0 教程-使用keras训练模型
TensorFlow 2.0 教程-用keras构建自己的网络层
TensorFlow 2.0 教程-keras模型保存和序列化

TensorFlow2教程-使用预训练模型

TensorFlow2教程-使用预训练CNN模型_第1张图片

import numpy as np
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications import resnet50

img = image.load_img('dog.jpg')
print(image.img_to_array(img).shape)
img
(600, 600, 3)

1.导入模型

目前看使用模型:

Import model

  • Currently, seven models are supported
    • Xception
    • VGG16
    • VGG19
    • ResNet50
    • InceptionV3
    • InceptionResNetV2
    • MobileNet
    • MobileNetV2
    • DenseNet
    • nasnet
model = resnet50.ResNet50(weights='imagenet')
img = image.load_img('dog.jpg', target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
print(img.shape)
(1, 224, 224, 3)

2.模型预测

pred_class = model.predict(img)
n = 10
top_n = resnet50.decode_predictions(pred_class, top=n)
for c in top_n[0]:
    print(c)
('n02099849', 'Chesapeake_Bay_retriever', 0.51448697)
('n02099712', 'Labrador_retriever', 0.1818683)
('n02088364', 'beagle', 0.05007153)
('n02105412', 'kelpie', 0.03155613)
('n02087394', 'Rhodesian_ridgeback', 0.020672312)
('n02090379', 'redbone', 0.018476445)
('n02100236', 'German_short-haired_pointer', 0.01802308)
('n04409515', 'tennis_ball', 0.011181626)
('n02107142', 'Doberman', 0.009305544)
('n02101388', 'Brittany_spaniel', 0.00871648)
# img = image.load_img('dog.jpg')
# img = image.img_to_array(img)
# print(img.shape)
img = resnet50.preprocess_input(img)
print(img.shape)
(1, 224, 224, 3)
pred_class = model.predict(img)
n = 10
top_n = resnet50.decode_predictions(pred_class, top=n)
for c in top_n[0]:
    print(c)
('n02106550', 'Rottweiler', 0.6937907)
('n02107142', 'Doberman', 0.10025302)
('n02107312', 'miniature_pinscher', 0.057240624)
('n02107908', 'Appenzeller', 0.041135676)
('n02101006', 'Gordon_setter', 0.036703203)
('n02112706', 'Brabancon_griffon', 0.014862828)
('n02089078', 'black-and-tan_coonhound', 0.014462694)
('n02108000', 'EntleBucher', 0.0043801107)
('n02093754', 'Border_terrier', 0.002769812)
('n02099712', 'Labrador_retriever', 0.002542331)

你可能感兴趣的:(tensorflow,TensorFlow2教程)