Inception_v3是GoogLeNet的第3个版本,于2016年发布,对于其中的Inception模块的n*n卷积进行了1*n和n*1的替换,本文使用tensorflow当中Keras自带的Function API实现一个简单的目标识别任务。
Keras Applications提供了多种网络模型的高级API,可以通过API来快速搭建网络架构,它提供了已训练好的网络权重供加载使用。
首先准备一张图片,可自行取名,如下图所示:本例中图片名为14.jpg
1.导入必要套件:
其中preprocess_input是对网络架构的图像进行预处理,而decode_predictions是网络架构的输出解码,即输出预测类别与属于该类别的概率。
import tensorflow as tf
import numpy as np
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.applications.inception_v3 import decode_predictions
2.建立Inception_v3网络架构
model = tf.keras.applications.InceptionV3(include_top=True, weights='imagenet')
3.建立读取图像函数
其中resize=(299,299)为Inception_v3网络默认图像大小,该函数也是将非标准大小图像进行转换的工作。
def read_img(img_path, resize=(299,299)):
img_string = tf.io.read_file(img_path)
img_decode = tf.image.decode_image(img_string)
img_decode = tf.image.resize(img_decode, resize)
img_decode = tf.expand_dims(img_decode, axis=0)
return img_decode
4.读取图像进行预测
img_path为图像存储路径,本例中创建了image的文件夹,上述图像14.jpg存储在image文件夹内。
img_path = 'image/14.jpg'
img = read_img(img_path)
img = preprocess_input(img)
preds = model.predict(img)
print("Predicted:", decode_predictions(preds, top=3)[0])
5.预测结果
表示上述图像有0.4977222的概率为tiger_cat,有0.2851467的概率为Egyptian_cat,有0.10232914的概率为tabby。