tensorflow实现Inception_v3目标识别

        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。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

heze09

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值