-
大小: 8KB文件类型: .zip金币: 2下载: 0 次发布日期: 2021-05-16
- 语言: Python
- 标签: Python 机器学习 Transorflow 深度学习
资源简介
最好联系书本学习,input是输入文件,evaluateCatorDog是主文件,traning是训练文件,model模型文件

代码片段和文件信息
#-*- 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(0n)
img_dir = os.path.join(trainfiles[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 = ‘train‘
# 获取图片路径集和标签集
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])
# you need to change the directories to yours.
logs_train_dir = ‘/Data/savemodel‘
# 定义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()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2839 2018-04-30 16:09 evaluateCatOrDog.py
文件 1558 2018-05-03 14:42 input_data.cpython-36.pyc
文件 2583 2018-04-09 15:09 input_data.py
文件 2934 2018-05-03 14:42 model.cpython-36.pyc
文件 5541 2018-04-09 15:09 model.py
文件 2886 2018-05-02 17:08 training.py
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论