tensorflow学习笔记--restore使用模型

代码:
import tensorflow as tf
import numpy as np
import scipy
from scipy import ndimage
#graph = tf.Graph()

# 定义计算图---start  (计算图应与训练时的计算图相同)
X = tf.placeholder(name='X', shape=(None, 64, 64, 3), dtype=tf.float32)
Y = tf.placeholder(name='Y', shape=(None, 6), dtype=tf.float32)

W1 = tf.Variable( tf.random_normal([4,4,3,8], -1, 1), name='W1')
W2 = tf.Variable( tf.random_normal([2, 2, 8, 16], -1, 1),name='W2')

Z1 = tf.nn.conv2d(input=X, filter=W1, strides=(1, 1, 1, 1), padding='SAME')
# RELU
A1 = tf.nn.relu(Z1)
# MAXPOOL: window 8x8, sride 8, padding 'SAME'
P1 = tf.nn.max_pool(value=A1, ksize=(1, 8, 8, 1), strides=(1, 8, 8, 1), padding='SAME')
# CONV2D: filters W2, stride 1, padding 'SAME'
Z2 = tf.nn.conv2d(input=P1, filter=W2, strides=(1, 1, 1, 1), padding='SAME')
# RELU
A2 = tf.nn.relu(Z2)
# MAXPOOL: window 4x4, stride 4, padding 'SAME'
P2 = tf.nn.max_pool(value=A2, ksize=(1, 4, 4, 1), strides=(1, 4, 4, 1), padding='SAME')
# FLATTEN
P2 = tf.contrib.layers.flatten(inputs=P2)
# FULLY-CONNECTED without non-linear activation function (not not call softmax).
# 6 neurons in output layer. Hint: one of the arguments should be "activation_fn=None"
Z3 = tf.contrib.layers.fully_connected(P2, 6, activation_fn=None)
#定义模型图---end
#saver = tf.train.Saver()  saver定义在sess里外都可以
with tf.Session() as sess:

    check_point_path = 'saved_model/' # 保存好模型的文件路径

    ckpt = tf.train.get_checkpoint_state(checkpoint_dir=check_point_path)
    saver = tf.train.Saver()
    # 从模型中恢复参数
    saver.restore(sess, ckpt.model_checkpoint_path)  # 讀取成功,然后就可以使用模型参数进行预测,或者测试了。
    fname = "images/thumbs_up.jpg"
    image = np.array(ndimage.imread(fname, flatten=False))
    my_image = scipy.misc.imresize(image, size=(64, 64))
    my_image = np.expand_dims(my_image, 0)
    y = sess.run(Z3, feed_dict={X: my_image})
    print(np.argmax(np.squeeze(y)))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值