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

yipeiwu_com6年前Python基础

本文是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/ 存放的是我从百度下载的猫狗图片

执行结果:

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

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

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

欢迎star。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

pygame游戏之旅 添加碰撞效果的方法

pygame游戏之旅 添加碰撞效果的方法

本文为大家分享了pygame游戏之旅的第7篇,供大家参考,具体内容如下 对car和障碍的宽高进行比较然后打印即可: if y < thing_starty + thing_he...

python3.6 tkinter实现屏保小程序

本文实例为大家分享了python3.6 tkinter实现屏保小程序,供大家参考,具体内容如下 该小程序是在闲着没事的时候,随便写的,就当打发无聊了。 该程序是用python3.6写的,...

解决Django一个表单对应多个按钮的问题

需求: 在django中,有时候我们需要在一个表单中设置多个按钮实现不同的功能。 解决方法: 为不同按钮添加不同name属性,然后再后台判断name值。python2环境下,例如: 我们...

解决Python 中英文混输格式对齐的问题

Python中使用str.format进行格式化输出 format使用方法较多,这里只说明其在填充与对齐上的使用方法: 填充与对齐 填充常跟对齐一起使用 ^、<、>分别是居中...

python字符串的方法与操作大全

一:字符串的方法与操作 *注意:首字母为l的为从左边操作,为r的方法为从右边操作 1.__contains__()判断是否包含 判断指定字符或字符串是否包含在一个字符串内,返回值为tru...