def judge(reshape_xs): global_step = tf.Variable(0, trainable=False) #定义平均滑动 variable_average = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) # 滑动平均值映射为本身的值 saver = tf.train.Saver(variable_average.variables_to_restore()) #定义用于输入图片数据的张量占位符,输入样本的尺寸 x = tf.placeholder(tf.float32, shape=[ None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL ], name='x-input') y = mnist_interence.interence(x, False, None) r = tf.argmax(y, 1) sess = tf.Session(config=tf.ConfigProto(log_device_placement=False, allow_soft_placement=True)) sess.run(tf.global_variables_initializer()) # 恢复变量 saver = tf.train.import_meta_graph( os.path.join(MODEL_PATH, "model-300001.meta")) #如果没有这句会报错NotFoundError saver.restore(sess, tf.train.latest_checkpoint(MODEL_PATH)) res = sess.run(r, feed_dict={x: reshape_xs})[0] print(res) return res
def predictInt2(imgdata): with tf.Graph().as_default() as g: # 将默认图设为g # 定义输入输出的格式 x = tf.placeholder(tf.float32, shape=[ 1, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL ], name='x-input') regularizer = tf.contrib.layers.l2_regularizer(0.0001) y = mnist_interence.interence(x, True, regularizer) variable_averages = tf.train.ExponentialMovingAverage( mnist_train.MOVING_AVERAGE_DECAY) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) # 这些值要从模型中提取 with tf.Session() as sess: if (os.path.exists(os.path.join('model', 'checkpoint'))): # Restore variables from disk. saver = tf.train.import_meta_graph( os.path.join('model', 'model.meta')) saver.restore( sess, tf.train.latest_checkpoint(os.path.join('model'))) xs = imgdata.reshape(1, 28, 28, 1) prediction = tf.argmax(y, 1) num = prediction.eval(feed_dict={x: xs}, session=sess) num = num[0] return num
def train(mnist): #定义用于输入图片数据的张量占位符,输入样本的尺寸 x = tf.placeholder(tf.float32, shape=[None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL], name='x-input') #定义用于输入图片标签数据的张量占位符,输入样本的尺寸 y_ = tf.placeholder(tf.float32, shape=[None, mnist_interence.OUTPUT_NODE], name='y-input') #定义采用方差的正则化函数 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_TATE) #通过interence函数获得计算结果张量 y = mnist_interence.interence(x,True,regularizer) global_step = tf.Variable(0, trainable=False) #定义平均滑动 variable_average = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) #对所有可以训练的变量采用平均滑动 variable_average_ops = variable_average.apply(tf.trainable_variables()) #对预测数据y和实际数据y_计算他们概率的交叉值 cross_entroy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) #对各对交叉值求平均,其实是计算y和y_两个随机变量概率分布的交叉熵,交叉熵值越小则表明两种概率分布越接近 cross_entroy_mean = tf.reduce_mean(cross_entroy) #采用交叉熵和正则化参数作为最后的损失函数 loss = cross_entroy_mean + tf.add_n(tf.get_collection('loss')) #设置学习率递减方式 learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY) #采用梯度下降的方式计算损失函数的最小值 train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss, global_step=global_step) #定义学习操作:采用梯度下降求一次模型训练参数,并对求得的模型参数计算滑动平均值 train_op = tf.group(train_step, variable_average_ops) #saver = tf.train.Saver() saver = tf.train.import_meta_graph('./model/model-183001.meta')# 加载模型结构 os.environ["CUDA_VISIBLE_DEVICES"] = '0' # 配置每个进程80%GPU config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.7 config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: #tf.global_variables_initializer().run() # 模型恢复 model_file=tf.train.latest_checkpoint('./model/') latest_step=model_file[model_file.index('-')+1:model_file.index('.')] saver.restore(sess,model_file) #print("model restored") for i in range(latest_step+1,TRAIN_STEP): # 由于神经网络的输入大小为[BATCH_SIZE,IMAGE_SIZE,IMAGE_SIZE,CHANNEL],因此需要reshape输入。 xs,ys = mnist.train.next_batch(BATCH_SIZE) reshape_xs = np.reshape(xs,(BATCH_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL)) #通过计算图,计算模型损失张量和学习张量的当前值 _,loss_value,step,learn_rate = sess.run([train_op,loss,global_step,learning_rate],feed_dict={x:reshape_xs,y_:ys}) if i % 1000 == 0: print('After %d step, loss on train is %g,and learn rate is %g'%(step,loss_value,learn_rate)) saver.save(sess,os.path.join(MODEL_PATH,MODEL_NAME),global_step=global_step)
def digit_recog(): # 占位符,输入的格式 x = tf.placeholder( tf.float32, [1, mnist_cnn.IMAGE_SIZE, mnist_cnn.IMAGE_SIZE, mnist_cnn.NUM_CHANNEL], name='x-input') # 直接通过调用封装好的函数来计算前向传播的结果,测试时不关注过拟合问题,所以正则化输入为None y = mnist_cnn.interence(x, None, None) # 使用tf.argmax(y, 1)就可以得到输入样例的预测类别 result = tf.argmax(y, 1) # 通过变量重命名的方式来加载模型 variable_averages = tf.train.ExponentialMovingAverage( mnist_train.MOVING_AVERAGE_DECAY) variable_to_restore = variable_averages.variables_to_restore() # 所有滑动平均的值组成的字典,处在/ExponentialMovingAverage下的值 # 为了方便加载时重命名滑动平均量,tf.train.ExponentialMovingAverage类提供了variables_to_store函数来生成tf.train.Saver类所需要的变量 saver = tf.train.Saver(variable_to_restore) with tf.Session() as sess: # tf.train.get_checkpoint_state函数会通过checkpoint文件自动找到目录中最新模型的文件名 ckpt = tf.train.get_checkpoint_state( os.path.join(os.path.dirname(__file__), mnist_train.MODEL_PATH)) if ckpt and ckpt.model_checkpoint_path: # 加载模型 saver.restore(sess, ckpt.model_checkpoint_path) # 读取本地图片,进行转换flaot32、灰度化、转换np数组操作 image_raw_data = tf.gfile.GFile('temp.png', 'rb').read() image_data = tf.image.decode_png(image_raw_data) float_image = tf.image.convert_image_dtype(image_data, dtype=tf.float32) gray_image = tf.image.rgb_to_grayscale(float_image) reshaped_image = tf.reshape(gray_image, [ 1, mnist_cnn.IMAGE_SIZE, mnist_cnn.IMAGE_SIZE, mnist_cnn.NUM_CHANNEL ]) nparray_image = reshaped_image.eval(session=sess) # 运行图 res = sess.run(result, feed_dict={x: nparray_image}) res_y = sess.run(y, feed_dict={x: nparray_image}) print(res_y) # 重置默认图 tf.reset_default_graph() # 返回识别结果 return res[0]
def train(mnist): #定义用于输入图片数据的张量占位符,输入样本的尺寸 x = tf.placeholder(tf.float32, shape=[None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL], name='x-input') #定义用于输入图片标签数据的张量占位符,输入样本的尺寸 y_ = tf.placeholder(tf.float32, shape=[None, mnist_interence.OUTPUT_NODE], name='y-input') #定义采用方差的正则化函数 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_TATE) #通过interence函数获得计算结果张量 y = mnist_interence.interence(x,True,regularizer) global_step = tf.Variable(0, trainable=False) #定义平均滑动 variable_average = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) #对所有可以训练的变量采用平均滑动 variable_average_ops = variable_average.apply(tf.trainable_variables()) #对预测数据y和实际数据y_计算他们概率的交叉值 cross_entroy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) #对各对交叉值求平均,其实是计算y和y_两个随机变量概率分布的交叉熵,交叉熵值越小则表明两种概率分布越接近 cross_entroy_mean = tf.reduce_mean(cross_entroy) #采用交叉熵和正则化参数作为最后的损失函数 loss = cross_entroy_mean + tf.add_n(tf.get_collection('loss')) #设置学习率递减方式 learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY) #采用梯度下降的方式计算损失函数的最小值 train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss, global_step=global_step) #定义学习操作:采用梯度下降求一次模型训练参数,并对求得的模型参数计算滑动平均值 train_op = tf.group(train_step, variable_average_ops) saver = tf.train.Saver() #用于保存神经网络结构,构造方法可以传参数,参数可以是dict和list。不传参数时默认保存所有变量 with tf.Session() as sess: tf.initialize_all_variables().run() #初始化所有变量 ckpt = tf.train.get_checkpoint_state(MODEL_PATH) #获取checkpoints对象 if ckpt and ckpt.model_checkpoint_path:##判断ckpt是否为空,若不为空,才进行模型的加载,否则从头开始训练 saver.restore(sess,ckpt.model_checkpoint_path)#恢复保存的神经网络结构,实现断点续训 for i in range(TRAIN_STEP): # 由于神经网络的输入大小为[BATCH_SIZE,IMAGE_SIZE,IMAGE_SIZE,CHANNEL],因此需要reshape输入。 xs,ys = mnist.train.next_batch(BATCH_SIZE) reshape_xs = np.reshape(xs,(BATCH_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL)) #通过计算图,计算模型损失张量和学习张量的当前值 _,loss_value,step,learn_rate = sess.run([train_op,loss,global_step,learning_rate],feed_dict={x:reshape_xs,y_:ys}) if i % 10 == 0: print('After %d step, loss on train is %g,and learn rate is %g'%(step,loss_value,learn_rate)) if step>=301000: break saver.save(sess,os.path.join(MODEL_PATH,MODEL_NAME),global_step=global_step)
def get_num(e): with tf.Graph().as_default(): x = tf.placeholder(tf.float32, shape=[None, 28, 28, 1], name='x-input') reshape_xs = np.reshape(e, (-1, 28, 28, 1)) y = mnist_interence.interence(x, False, None) result = tf.argmax(y, 1) saver = tf.train.Saver() with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state('model') if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) key = sess.run(result, feed_dict={x: reshape_xs}) print(key[0]) return key[0]
def evaluate(mnist): with tf.Graph().as_default(): x = tf.placeholder(tf.float32, shape=[ None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL ], name='x-input') y_ = tf.placeholder(tf.float32, shape=[None, mnist_interence.OUTPUT_NODE], name='y-input') xs, ys = mnist.test.images, mnist.test.labels reshape_xs = np.reshape( xs, (-1, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL)) print(mnist.test.labels[0]) val_feed = {x: reshape_xs, y_: mnist.test.labels} y = mnist_interence.interence(x, False, None) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) variable_average = tf.train.ExponentialMovingAverage( mnist_train.MOVING_AVERAGE_DECAY) val_to_restore = variable_average.variables_to_restore() saver = tf.train.Saver(val_to_restore) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] accuracy_score = sess.run(accuracy, feed_dict=val_feed) print('After %s train ,the accuracy is %g' % (global_step, accuracy_score)) else: print('No Checkpoint file find')
def evaluate(IMG): with tf.Graph().as_default(): x = tf.placeholder(tf.float32, shape=[None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL], name='x-input') reshape_xs = np.reshape(IMG, (-1, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL)) val_feed = {x: reshape_xs} y = mnist_interence.interence(x, False, None) num=tf.argmax(y, 1) saver = tf.train.Saver() with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) num2= sess.run(num, feed_dict=val_feed) return num2[0]
def train(mnist): #定义用于输入图片数据的张量占位符,输入样本的尺寸 x = tf.placeholder(tf.float32, shape=[ None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL ], name='x-input') #定义用于输入图片标签数据的张量占位符,输入样本的尺寸 y_ = tf.placeholder(tf.float32, shape=[None, mnist_interence.OUTPUT_NODE], name='y-input') #定义采用方差的正则化函数 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_TATE) #通过interence函数获得计算结果张量 y = mnist_interence.interence(x, True, regularizer) global_step = tf.Variable(0, trainable=False) #定义平均滑动 variable_average = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) #对所有可以训练的变量采用平均滑动 variable_average_ops = variable_average.apply(tf.trainable_variables()) #对预测数据y和实际数据y_计算他们概率的交叉值 cross_entroy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=y, labels=tf.argmax(y_, 1)) #对各对交叉值求平均,其实是计算y和y_两个随机变量概率分布的交叉熵,交叉熵值越小则表明两种概率分布越接近 cross_entroy_mean = tf.reduce_mean(cross_entroy) #采用交叉熵和正则化参数作为最后的损失函数 loss = cross_entroy_mean + tf.add_n(tf.get_collection('loss')) #设置学习率递减方式 learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY) #采用梯度下降的方式计算损失函数的最小值 train_step = tf.train.GradientDescentOptimizer(0.01).minimize( loss, global_step=global_step) #定义学习操作:采用梯度下降求一次模型训练参数,并对求得的模型参数计算滑动平均值 train_op = tf.group(train_step, variable_average_ops) # 保存变量 saver = tf.train.Saver() # tf.Session使用默认图 with tf.Session() as sess: # 初始化所有变量并且运行 # tf.global_variables_initializer().run() # 新的初始化,与恢复变量结合使用 sess.run(tf.global_variables_initializer()) # 恢复变量 (注意恢复变量必须放在初始化之后!!!!!!!!!!!!!!!!!!!!!!) # saver = tf.train.import_meta_graph(os.path.join(MODEL_PATH,"model-6001.meta")) # saver.restore(sess, tf.train.latest_checkpoint(MODEL_PATH)) # if ckpt and ckpt.all_model_checkpoint_paths: # for path in ckpt.all_model_checkpoint_paths: # print(path,"++++++") saver.restore(sess, tf.train.latest_checkpoint(MODEL_PATH)) step = sess.run(global_step) for i in range(step, TRAIN_STEP): # 由于神经网络的输入大小为[BATCH_SIZE,IMAGE_SIZE,IMAGE_SIZE,CHANNEL],因此需要reshape输入。 xs, ys = mnist.train.next_batch(BATCH_SIZE) reshape_xs = np.reshape( xs, (BATCH_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL)) #通过计算图,计算模型损失张量和学习张量的当前值 _, loss_value, step, learn_rate = sess.run( [train_op, loss, global_step, learning_rate], feed_dict={ x: reshape_xs, y_: ys }) # Launch the graph and train, saving the model every 5,000 steps. if (i + 1) % 1000 == 0: # 取i+1是为了整数倍的步长 print( 'After %d step, loss on train is %g,and learn rate is %g' % (step, loss_value, learn_rate)) saver.save(sess, os.path.join(MODEL_PATH, MODEL_NAME), global_step=global_step)
def train(mnist, haveTrained): #定义用于输入图片数据的张量占位符,输入样本的尺寸 x = tf.placeholder(tf.float32, shape=[ None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL ], name='x-input') #定义用于输入图片标签数据的张量占位符,输入样本的尺寸 y_ = tf.placeholder(tf.float32, shape=[None, mnist_interence.OUTPUT_NODE], name='y-input') #定义采用方差的正则化函数 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_TATE) #通过interence函数获得计算结果张量 y = mnist_interence.interence(x, True, regularizer) global_step = tf.Variable(0, trainable=False) #定义平均滑动 variable_average = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) #对所有可以训练的变量采用平均滑动 variable_average_ops = variable_average.apply(tf.trainable_variables()) #对预测数据y和实际数据y_计算他们概率的交叉值 cross_entroy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=y, labels=tf.argmax(y_, 1)) #对各对交叉值求平均,其实是计算y和y_两个随机变量概率分布的交叉熵,交叉熵值越小则表明两种概率分布越接近 cross_entroy_mean = tf.reduce_mean(cross_entroy) #采用交叉熵和正则化参数作为最后的损失函数 loss = cross_entroy_mean + tf.add_n(tf.get_collection('loss')) #设置学习率递减方式 learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY) #采用梯度下降的方式计算损失函数的最小值 train_step = tf.train.GradientDescentOptimizer(0.01).minimize( loss, global_step=global_step) #定义学习操作:采用梯度下降求一次模型训练参数,并对求得的模型参数计算滑动平均值 train_op = tf.group(train_step, variable_average_ops) saver = tf.train.Saver() with tf.Session() as sess: tf.global_variables_initializer().run() if haveTrained: #模型重载恢复 ckpt = tf.train.latest_checkpoint(MODEL_NAME) saver.restore(sess, ckpt) start = int(ckpt.split('-')[-1]) #start为文件名中的数字 else: start = 0 #标志位用于计数 for i in range(start, TRAIN_STEP): # 由于神经网络的输入大小为[BATCH_SIZE,IMAGE_SIZE,IMAGE_SIZE,CHANNEL],因此需要reshape输入。 xs, ys = mnist.train.next_batch(BATCH_SIZE) reshape_xs = np.reshape( xs, (BATCH_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL)) #通过计算图,计算模型损失张量和学习张量的当前值 _, loss_value, step, learn_rate = sess.run( [train_op, loss, global_step, learning_rate], feed_dict={ x: reshape_xs, y_: ys }) #判断模型是否曾经训练过 if i != 0 and i % 1 == 0: print( 'After %d step, loss on train is %g,and learn rate is %g' % (step, loss_value, learn_rate)) saver.save(sess, os.path.join(MODEL_PATH, MODEL_NAME), global_step=global_step)
def train(mnist): #定义用于输入图片数据的张量占位符,输入样本的尺寸 x = tf.placeholder(tf.float32, shape=[ None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL ], name='x-input') #定义用于输入图片标签数据的张量占位符,输入样本的尺寸 y_ = tf.placeholder(tf.float32, shape=[None, mnist_interence.OUTPUT_NODE], name='y-input') #定义采用方差的正则化函数 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_TATE) #通过interence函数获得计算结果张量 y = mnist_interence.interence(x, True, regularizer) global_step = tf.Variable(0, trainable=False) #定义平均滑动 variable_average = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) #对所有可以训练的变量采用平均滑动 variable_average_ops = variable_average.apply(tf.trainable_variables()) #对预测数据y和实际数据y_计算他们概率的交叉值 cross_entroy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=y, labels=tf.argmax(y_, 1)) #对各对交叉值求平均,其实是计算y和y_两个随机变量概率分布的交叉熵,交叉熵值越小则表明两种概率分布越接近 cross_entroy_mean = tf.reduce_mean(cross_entroy) #采用交叉熵和正则化参数作为最后的损失函数 loss = cross_entroy_mean + tf.add_n(tf.get_collection('loss')) #设置学习率递减方式 learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY) #采用梯度下降的方式计算损失函数的最小值 train_step = tf.train.GradientDescentOptimizer(0.01).minimize( loss, global_step=global_step) #定义学习操作:采用梯度下降求一次模型训练参数,并对求得的模型参数计算滑动平均值 train_op = tf.group(train_step, variable_average_ops) saver = tf.train.Saver() with tf.Session() as sess: if (os.path.exists(os.path.join(MODEL_PATH, 'checkpoint'))): # Restore variables from disk. saver = tf.train.import_meta_graph( os.path.join(MODEL_PATH, 'model.meta')) saver.restore(sess, tf.train.latest_checkpoint(os.path.join(MODEL_PATH))) print("Model restored.") else: tf.global_variables_initializer().run() step = 0 while (TRAIN_STEP - step) > 0: # 由于神经网络的输入大小为[BATCH_SIZE,IMAGE_SIZE,IMAGE_SIZE,CHANNEL],因此需要reshape输入。 xs, ys = mnist.train.next_batch(BATCH_SIZE) reshape_xs = np.reshape( xs, (BATCH_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL)) #通过计算图,计算模型损失张量和学习张量的当前值 _, loss_value, step, learn_rate = sess.run( [train_op, loss, global_step, learning_rate], feed_dict={ x: reshape_xs, y_: ys }) if step % 300 == 0: #每n次输出一次 print( 'After %d step, loss on train is %g,and learn rate is %g' % (step, loss_value, learn_rate)) if step % 1500 == 0: #每n次迭代保存一次 save_path = saver.save(sess, os.path.join(MODEL_PATH, MODEL_NAME)) print("Model-%d saved in file: " % step, save_path)
def imageRecognize(imageData): # 背景转换为黑色,画笔转换为白色 imageData = reveBlack(imageData) # 将图片数据转换为ndarray类型 npData = np.array(imageData, dtype=np.uint8).reshape(28, 28, 4) # 将RGBA格式数组转换为图片 image = Image.fromarray(npData, 'RGBA') # 保存临时图片 image.save('static/images/npimg.png') # 用opencv读取图片的RGB数据 rgbImage = cv2.imread('static/images/npimg.png', cv2.IMREAD_COLOR) # 将rgb图片转换为float32格式 rgbImage = tf.image.convert_image_dtype(rgbImage, tf.float32) # 将图片灰度化得到(28,28,1)格式的tensor rgbImage = tf.image.rgb_to_grayscale(rgbImage) # 定义输入格式(1,28,28,1) x = tf.placeholder(tf.float32, [ 1, mnist_inference.IMAGE_SIZE, mnist_inference.IMAGE_SIZE, mnist_inference.NUM_CHANNEL ], name='x-input') #直接通过调用封装好的函数来计算前向传播的结果 y = mnist_inference.interence(x, None, None) #使用tf.argmax(y, 1)就可以得到输入样例的预测类别 prediction = tf.argmax(y, 1) # 通过变量重命名的方式来加载模型 # 所有滑动平均的值组成的字典,处在/ExponentialMovingAverage下的值 # 为了方便加载时重命名滑动平均量,tf.train.ExponentialMovingAverage类 # 提供了variables_to_store函数来生成tf.train.Saver类所需要的变量 # 这些值要从模型中提取 variable_averages = tf.train.ExponentialMovingAverage( mnist_train.MOVING_AVERAGE_DECAY) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) # 用数据测试模型 with tf.Session() as sess: # get_checkoutpoint_state()会通过checkoutpoint文件自动找到目录中最新模型的文件名 ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_PATH) if ckpt and ckpt.model_checkpoint_path: # 加载模型 saver.restore(sess, ckpt.model_checkpoint_path) # 将tensor转换为np数组,这里也可以用np的reshape方法 rgbNpData = tf.reshape(rgbImage, [ 1, mnist_inference.IMAGE_SIZE, mnist_inference.IMAGE_SIZE, mnist_inference.NUM_CHANNEL ]) # 将tensor转换为ndarray reshaped_data = rgbNpData.eval(session=sess) # 将输入的测试数据格式调整为一个四维矩阵 validate_feed = {x: reshaped_data} # 获得预测的结果数组 predictionNum = sess.run(prediction, feed_dict=validate_feed) print("Number is %d" % (predictionNum[0])) tf.reset_default_graph() return predictionNum[0]
TRAIN_STEP = 300000 # 模型路径 MODEL_PATH = 'model' # 模型名称 MODEL_NAME = 'model' # 定义用于输入图片数据的张量占位符,输入样本的尺寸 x = tf.placeholder(tf.float32, shape=[None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL], name='x-input') # 定义用于输入图片标签数据的张量占位符,输入样本的尺寸 y_ = tf.placeholder(tf.float32, shape=[None, mnist_interence.OUTPUT_NODE], name='y-input') # 定义采用方差的正则化函数 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_TATE) # 通过interence函数获得计算结果张量 y = mnist_interence.interence(x, True, regularizer) global_step = tf.Variable(0, trainable=False) def predict(pic): saver = tf.train.Saver() with tf.Session() as sess: path = os.getcwd() + "/model" if os.path.exists(path): model_file = tf.train.latest_checkpoint(path) saver.restore(sess, model_file) step = sess.run(global_step) print("successful restore at {0}!".format(step)) else: return 0
#定义用于输入图片数据的张量占位符,输入样本的尺寸, (None, 28, 28, 1) x = tf.placeholder(tf.float32, shape=[ None, mnist_interence.IMAGE_SIZE, mnist_interence.IMAGE_SIZE, mnist_interence.NUM_CHANNEL ], name='x-input') #定义用于输入图片标签数据的张量占位符,输入样本的尺寸, (None, 10), 数据由float64转换为float32 # y_ = tf.placeholder(tf.float32, shape=[None, mnist_interence.OUTPUT_NODE], name='y-input') #定义采用方差的正则化函数, L2正则化 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_TATE) #通过interence函数获得计算结果张量, (bz, 10) with tf.device('/gpu:0'): # y = tf.nn.softmax(mnist_interence.interence(x,False,None), -1) y = mnist_interence.interence(x, False, None) r = tf.argmax(y, 1) # 但前训练步数 global_step = tf.Variable(0, trainable=False) #定义平均滑动 variable_average = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) # 滑动平均值映射为本身的值 saver = tf.train.Saver(variable_average.variables_to_restore()) #对所有可以训练的变量采用平均滑动, 这个滑动平均值是用于测试过程的!!! # variable_average_ops = variable_average.apply(tf.trainable_variables()) #对预测数据y和实际数据y_计算他们概率的交叉值 # logits: (bz, 10) # labels: (bz) # cross_entroy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) #对各对交叉值求平均,其实是计算y和y_两个随机变量概率分布的交叉熵,交叉熵值越小则表明两种概率分布越接近