TF 训练好的模型应用 图片输入转换成矩阵 调用带参模型输出结果

图片输入转化
def pre_pic(picName):
 img = Image.open(picName)
 reIm = img.resize((28,28), Image.ANTIALIAS)
 im_arr = np.array(reIm.convert('L'))        转灰度  矩阵

反颜色
 threshold = 50
 for i in range(28):
  for j in range(28):
   im_arr[i][j] = 255 - im_arr[i][j]
    if (im_arr[i][j] < threshold):
     im_arr[i][j] = 0               黑色
   else: im_arr[i][j] = 255     白色

 nm_arr = im_arr.reshape([1, 784])
 nm_arr = nm_arr.astype(np.float32)
 img_ready = np.multiply(nm_arr, 1.0/255.0)        0 or 1 二值
 return img_ready


调用现有模型

def restore_model(testPicArr):

 with tf.Graph().as_default() as tg:
  x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
  y = mnist_forward.forward(x, None)
  preValue = tf.argmax(y, 1)
  滑动平均值恢复
  variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
   variables_to_restore = variable_averages.variables_to_restore()
   saver = tf.train.Saver(variables_to_restore)

  with tf.Session() as sess:
模型调用
   ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
   if ckpt and ckpt.model_checkpoint_path:
    saver.restore(sess, ckpt.model_checkpoint_path)
  
    preValue = sess.run(preValue, feed_dict={x:testPicArr})
    return preValue
   else:
    print("No checkpoint file found")
    return -1



断点后续训练

    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

         ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)

        for i in range(STEPS):
            xs, ys = mnist.train.next_batch(BATCH_SIZE)
            _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys})
            if i % 1000 == 0:
                print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
                saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)

def main():
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    backward(mnist)





#coding:utf-8
import tensorflow as tf
import numpy as np
from PIL import Image
import mnist_backward
import mnist_forward

def restore_model(testPicArr):
 with tf.Graph().as_default() as tg:
  x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
  y = mnist_forward.forward(x, None)
  preValue = tf.argmax(y, 1)

  variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
   variables_to_restore = variable_averages.variables_to_restore()
   saver = tf.train.Saver(variables_to_restore)

  with tf.Session() as sess:
   ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
   if ckpt and ckpt.model_checkpoint_path:
    saver.restore(sess, ckpt.model_checkpoint_path)
  
    preValue = sess.run(preValue, feed_dict={x:testPicArr})
    return preValue
   else:
    print("No checkpoint file found")
    return -1


def pre_pic(picName):
 img = Image.open(picName)
 reIm = img.resize((28,28), Image.ANTIALIAS)
 im_arr = np.array(reIm.convert('L'))
 threshold = 50
 for i in range(28):
  for j in range(28):
   im_arr[i][j] = 255 - im_arr[i][j]
    if (im_arr[i][j] < threshold):
     im_arr[i][j] = 0
   else: im_arr[i][j] = 255

 nm_arr = im_arr.reshape([1, 784])
 nm_arr = nm_arr.astype(np.float32)
 img_ready = np.multiply(nm_arr, 1.0/255.0)
 return img_ready

def application():
 testNum = input("input the number of test pictures:")
 for i in range(testNum):
  testPic = raw_input("the path of test picture:")
  testPicArr = pre_pic(testPic)
  preValue = restore_model(testPicArr)
  print "The prediction number is:", preValue

def main():
 application()

if __name__ == '__main__':
 main() 


猜你喜欢

转载自blog.csdn.net/weixin_42247762/article/details/80725855