ResNet 进行图片识别 (基于tf.keras)

import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.python.keras.applications.resnet50 import ResNet50
from tensorflow.python.keras.preprocessing import image
from tensorflow.python.keras.applications.resnet50 import preprocess_input, decode_predictions


inputs = tf.placeholder(tf.float32, (224, 224, 3))#定义占位符

tensorimg = tf.expand_dims(inputs, 0)               #预处理
tensorimg =preprocess_input(tensorimg)




with tf.Session() as sess:  #在session中运行
    sess.run(tf.global_variables_initializer())
    Reslayer = ResNet50(weights='imagenet')
    logits = Reslayer(tensorimg)  #网络模型

    #Reslayer = ResNet50(weights='resnet50_weights_tf_dim_ordering_tf_kernels.h5',
    #                 input_tensor=tensorimg,input_shape = (224, 224, 3) )
    #logits = Reslayer.layers[-1].output
    #print(logits)

    img_path = 'hy.jpg'
    img = image.load_img(img_path, target_size=(224, 224))
    logitsv = sess.run(logits,feed_dict={inputs: img})
    Pred =decode_predictions(logitsv, top=3)[0]
    print('Predicted:', Pred,len(logitsv[0]))

#可视化
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 8))
fig.sca(ax1)
ax1.imshow(img)
fig.sca(ax1)

barlist = ax2.bar(range(3), [ i[2] for i in Pred ])
barlist[0].set_color('g')

plt.sca(ax2)
plt.ylim([0, 1.1])
#plt.xticks(range(3),[i[1][:15] for i in Pred], rotation='vertical')
fig.subplots_adjust(bottom=0.2)
plt.show()

随便上网找一张图片,识别结果如下:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值