TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片

作者:双斜杠少年 时间:2023-09-07 03:34:41 

本文是Python通过TensorFlow卷积神经网络实现猫狗识别的姊妹篇,是加载上一篇训练好的模型,进行猫狗识别

本文逻辑:

  1. 我从网上下载了十几张猫和狗的图片,用于检验我们训练好的模型。

  2. 处理我们下载的图片

  3. 加载模型

  4. 将图片输入模型进行检验

代码如下:


#coding=utf-8
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import input_data
import numpy as np
import model
import os
#从指定目录中选取一张图片
def get_one_image(train):
 files = os.listdir(train)
 n = len(files)
 ind = np.random.randint(0,n)
 img_dir = os.path.join(train,files[ind])
 image = Image.open(img_dir)
 plt.imshow(image)
 plt.show()
 image = image.resize([208, 208])
 image = np.array(image)
 return image
def evaluate_one_image():
#存放的是我从百度下载的猫狗图片路径
 train = '/Users/yangyibo/GitWork/pythonLean/AI/猫狗识别/testImg/'
 image_array = get_one_image(train)
 with tf.Graph().as_default():
   BATCH_SIZE = 1 # 因为只读取一副图片 所以batch 设置为1
   N_CLASSES = 2 # 2个输出神经元,[1,0] 或者 [0,1]猫和狗的概率
   # 转化图片格式
   image = tf.cast(image_array, tf.float32)
   # 图片标准化
   image = tf.image.per_image_standardization(image)
   # 图片原来是三维的 [208, 208, 3] 重新定义图片形状 改为一个4D 四维的 tensor
   image = tf.reshape(image, [1, 208, 208, 3])
   logit = model.inference(image, BATCH_SIZE, N_CLASSES)
   # 因为 inference 的返回没有用激活函数,所以在这里对结果用softmax 激活
   logit = tf.nn.softmax(logit)
   # 用最原始的输入数据的方式向模型输入数据 placeholder
   x = tf.placeholder(tf.float32, shape=[208, 208, 3])
   # 我门存放模型的路径
   logs_train_dir = '/Users/yangyibo/GitWork/pythonLean/AI/猫狗识别/saveNet/'  
   # 定义saver
   saver = tf.train.Saver()
   with tf.Session() as sess:
     print("从指定的路径中加载模型。。。。")
     # 将模型加载到sess 中
     ckpt = tf.train.get_checkpoint_state(logs_train_dir)
     if ckpt and ckpt.model_checkpoint_path:
       global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
       saver.restore(sess, ckpt.model_checkpoint_path)
       print('模型加载成功, 训练的步数为 %s' % global_step)
     else:
       print('模型加载失败,,,文件没有找到')
     # 将图片输入到模型计算
     prediction = sess.run(logit, feed_dict={x: image_array})
     # 获取输出结果中最大概率的索引
     max_index = np.argmax(prediction)
     if max_index==0:
       print('猫的概率 %.6f' %prediction[:, 0])
     else:
       print('狗的概率 %.6f' %prediction[:, 1])
# 测试
evaluate_one_image()

/Users/yangyibo/GitWork/pythonLean/AI/猫狗识别/testImg/ 存放的是我从百度下载的猫狗图片

TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片

执行结果:

因为从testimg 中选取图片是随机的,所以每次执行的结果不同

从指定的路径中加载模型。。。。
模型加载成功, 训练的步数为 11999
狗的概率 0.964047
[Finished in 6.8s]

代码地址:https://github.com/527515025/My-TensorFlow-tutorials/blob/master/猫狗识别/evaluateCatOrDog.py

欢迎star。

来源:https://blog.csdn.net/u012373815/article/details/79222121

标签:tensorflow,卷积神经网络,训练好的模型,识别猫狗图片
0
投稿

猜你喜欢

  • Python自动化测试利器selenium详解

    2021-06-07 20:22:10
  • python魔法方法-自定义序列详解

    2022-10-08 08:56:12
  • mysql 5.7.23 安装配置方法图文教程

    2024-01-14 02:58:36
  • python+mysql实现教务管理系统

    2024-01-26 11:43:03
  • javascript实现文本框标签验证的实例代码

    2024-04-25 13:07:41
  • Python写一个简单的api接口的实现

    2023-07-23 20:20:53
  • 在Tensorflow中查看权重的实现

    2022-05-02 20:14:56
  • go流程控制代码详解

    2023-10-15 18:14:19
  • python中web框架的自定义创建

    2023-09-18 14:54:24
  • javascript 删除dom对象的事件函数代码

    2024-04-19 09:48:02
  • axios拦截器工作方式及原理源码解析

    2023-07-02 16:38:36
  • select 终极美化

    2007-10-16 17:57:00
  • Python ckeditor富文本编辑器代码实例解析

    2023-08-23 13:03:44
  • 对python 自定义协议的方法详解

    2023-05-26 07:20:04
  • mysql 5.7.17 winx64安装配置图文教程

    2024-01-28 04:57:13
  • javascript中的后退和刷新实现方法

    2023-08-23 09:36:45
  • Python图像处理实现两幅图像合成一幅图像的方法【测试可用】

    2022-11-13 08:36:56
  • python脚本使用阿里云slb对恶意攻击进行封堵的实现

    2021-11-20 16:32:07
  • 如何给 legend 标签设定宽度

    2008-07-26 12:18:00
  • MySQL 字符串截取相关函数小结

    2024-01-14 21:37:14
  • asp之家 网络编程 m.aspxhome.com