def restore_model(testPicArr): with tf.Graph().as_default() as tg: # 为输入输出占位,因为输入改了,所以需要改为四位参数的输入 x = tf.placeholder(tf.float32, [1, cifar10_forward.IMAGE_SIZE,cifar10_forward.IMAGE_SIZE,cifar10_forward.NUM_CHANNELS]) y = cifar10_forward.forward(x, False, None) # 最大值为输出结果 preValue = tf.argmax(y, 1) # 实例化saver variable_averages = tf.train.ExponentialMovingAverage(cifar10_backward.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # 用with结构实现断点续训 with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(cifar10_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: # 没有找到ckpt时给出提示 print("No checkpoint is found") return -1
def restore_model(testPicArr): # 定义重建模型并进行预测 with tf.Graph().as_default() as g: x = tf.placeholder( tf.float32, [None, cifar10_forward.INPUT_NODE]) #定义占位符x,以之代替输入图片 y = cifar10_forward.forward(x, None) # 对x执行前向传播得到输出y predictValue = tf.argmax(y, 1) # 定义输入图片的预测值 ema = tf.train.ExponentialMovingAverage( cifar10_backward.MOVING_AVERAGE_DECAY ) # 实现滑动平均模型,参数MOVING_AVERAGE_DECAY用于控制模型更新的 # 速度,训练过程中会对每一个变量维护一个影子变量 ema_restore = ema.variables_to_restore( ) # variable_to_restore()返回dict ({ema_variables : variables}),字典中保存变量的影子值和现值 saver = tf.train.Saver( ema_restore) # 创建可还原滑动平均值的对象saver,测试时使用w的影子值,有更好的适配性 with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state( cifar10_backward.MODEL_SAVE_PATH) # 从指定路径中,加载训练好的模型 if ckpt and ckpt.model_checkpoint_path: # 若已有ckpt模型则执行以下恢复操作 saver.restore(sess, ckpt.model_checkpoint_path) # 恢复会话到当前的神经网络 predictValue = sess.run( predictValue, feed_dict={x: testPicArr}) #通过feed_dict将测试图片输入,输出预测结果 return predictValue else: # 没有成功加载ckpt模型, print "no checkpoint file found" return -1
def test(): # 复现计算图 with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [ TEST_NUM, cifar10_forward.IMAGE_SIZE, cifar10_forward.IMAGE_SIZE, cifar10_forward.NUM_CHANNELS ]) y_ = tf.placeholder(tf.float32, [None, cifar10_forward.OUTPUT_NODE]) # 前向传播计算出y的值,False意为不使用dropout y = cifar10_forward.forward(x, False, None) # 实例化带滑动平均的saver对象,赋值为各自的滑动平均值 ema = tf.train.ExponentialMovingAverage( cifar10_backward.MOVING_AVERAGE_DECAY) ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore) # 计算准确率 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) img_batch, label_batch = cifar10_generateds.get_tfRecord(TEST_NUM, isTrain=False) while True: with tf.Session() as sess: # 加载保存的模型 ckpt = tf.train.get_checkpoint_state( cifar10_backward.MODEL_SAVE_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] coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) # 计算精确度 xs, ys = sess.run([img_batch, label_batch]) # reshape_x应该定义在xs之后 reshaped_x = np.reshape( xs, (TEST_NUM, cifar10_forward.IMAGE_SIZE, cifar10_forward.IMAGE_SIZE, cifar10_forward.NUM_CHANNELS)) accuracy_score = sess.run(accuracy, feed_dict={ x: reshaped_x, y_: ys }) print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score)) coord.request_stop() coord.join(threads) else: print("No CheckPoint File Found!") return time.sleep(TEST_INTERVAL_SECS)
def backward(): #执行反向传播,训练参数w x = tf.placeholder(tf.float32, [None, cifar10_forward.INPUT_NODE]) #定义占位符x,以之代替输入图片 y_ = tf.placeholder(tf.float32, [None, cifar10_forward.OUTPUT_NODE])#定义占位符y_,用来接神经元计算结果 y = cifar10_forward.forward(x, REGULARIZER) #y是神经元的计算结果,下一步喂给y_ global_step = tf.Variable(0, trainable = False) #定义变量global_step,并把它的属性设置为不可训练 ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = y, #用交叉熵ce(cross entropy),使用tf.nn.sparse_softmax_cross_entropy_with_logits函数计算交叉熵, labels = tf.argmax(y_, 1)) #其第一个参数是神经网络不包括softmax层的前向传播结果,第二个参数是训练数据的正确答案 cem = tf.reduce_mean(ce) #计算在当前batch中所有样例的交叉熵平均值 loss = cem + tf.add_n(tf.get_collection('losses')) # 总损失等于交叉熵损失和正则化损失的和 learning_rate = tf.train.exponential_decay( # 设置指数衰减的学习率 LEARNING_RATE_BASE, # 基础学习率,随着迭代的进行,更新变量时使用的学习率在此基础上递减 global_step, # 当前迭代轮数 train_num_examples / BATCH_SIZE, # 过完所有训练数据所需迭代次数 LEARNING_RATE_DECAY, # 指数学习衰减率 staircase = True) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step = global_step)# 使用梯度下降优化损失函数,损失函数包含了交叉熵和正则化损失 ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) # 初始化滑动平均类 ema_op = ema.apply(tf.trainable_variables()) # 对所有表示神经网络参数的变量进行滑动平均 #bind operation train_step & ema_op together to realize two operations at time with tf.control_dependencies([train_step, ema_op]): # 使用tf.control_dependencies机制一次完成多个操作。在此神经网络模型中,每过一遍数据既通过 train_op = tf.no_op(name = 'train') # 反向传播更新参数,又更新了每一个参数的滑动平均值 saver = tf.train.Saver() # 声明tf.train.Saver类用于保存模型 img_batch, lable_batch = cifar10_generateds.get_tfrecord(BATCH_SIZE, isTrain=True) #3一次批获取 batch_size 张图片和标签 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # 初始化所有变量 # 恢复模块 ckpt = tf.train.get_checkpoint_state("./model") # 从"./model"中加载训练好的模型 if ckpt and ckpt.model_checkpoint_path: # 若ckpt和保存的模型在指定路径中存在,则将保存的神经网络模型加载到当前会话中 saver.restore(sess, ckpt.model_checkpoint_path) coord = tf.train.Coordinator() #4开启线程协调器 threads = tf.train.start_queue_runners(sess=sess, coord=coord) #5 for i in range(STEPS): # 迭代地训练神经网络 xs, ys = sess.run([img_batch, lable_batch]) #6将一个batch的训练数数据和对应标签分别赋给xs,ys _, loss_value, step = sess.run([train_op, loss,global_step], # 计算损失函数结果,计算节点train_op, loss,global_step并返回结果至 _, loss_value, step , feed_dict = {x : xs, y_ : ys}) #'_' means an anonymous variable which will not in use any more if i % 1000 == 0: # 每1000轮打印损失函数信息,并保存当前的模型 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) # 保存当前模型,globle_step参数可以使每个被保存模型的文件名末尾都加上训练的轮数 # 文件的名字是MODEL_SAVE_PATH + MODEL_NAME + global_step coord.request_stop() #7关闭线程协调器 coord.join(threads) #8
def backward(): # 改变后的输入 x = tf.placeholder(tf.float32, [ BATCH_SIZE, cifar10_forward.IMAGE_SIZE, cifar10_forward.IMAGE_SIZE, cifar10_forward.NUM_CHANNELS ]) y_ = tf.placeholder(tf.float32, [None, cifar10_forward.OUTPUT_NODE]) # True表示训练时使用dropout参数 y = cifar10_forward.forward(x, True, REGULARIZER) # 轮数计数器赋初值,并标记为不可训练 global_step = tf.Variable(0, trainable=False) # 使用交叉熵的协同使用实现正则化,并把参数w的正则化计入参数中 ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax( y_, 1)) cem = tf.reduce_mean(ce) loss = cem + tf.add_n(tf.get_collection('losses')) # 定义指数衰减学习率 learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, train_num_examples / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) # 定义训练过程 train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize( loss, global_step=global_step) # 滑动平均 ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) ema_op = ema.apply(tf.trainable_variables()) with tf.control_dependencies([train_step, ema_op]): train_op = tf.no_op(name='train') # 实例化Saver saver = tf.train.Saver() img_batch, label_batch = cifar10_generateds.get_tfRecord(BATCH_SIZE, isTrain=True) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) # 如果ckpt存在,则用restore方法把ckpt恢复到当前会话# # 给所有的w和b赋存在ckpt当中的值,继续上次训练 ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(STEPS): xs, ys = sess.run([img_batch, label_batch]) # 对数据集中取出的xs进行reshape操作 reshaped_xs = np.reshape( xs, (BATCH_SIZE, cifar10_forward.IMAGE_SIZE, cifar10_forward.IMAGE_SIZE, cifar10_forward.NUM_CHANNELS)) # sess.run()之后才会有结果 _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={ x: reshaped_xs, y_: ys }) if i % 100 == 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) coord.request_stop() coord.join(threads)
def backward(cifar10_image, cifar10_labels): images_test, labels_test = cifar10_input.inputs(eval_data=True, data_dir=data_dir, batch_size=BATCH_SIZE) x = tf.placeholder(tf.float32, [ BATCH_SIZE, cifar10_forward.IMAGE_SIZE, cifar10_forward.IMAGE_SIZE, cifar10_forward.NUM_CHANNELS ]) y_ = tf.placeholder(tf.int32, [BATCH_SIZE]) y = cifar10_forward.forward(x, True, BATCH_SIZE) global_step = tf.Variable(0, trainable=False) def loss(logits, labels): labels = tf.cast(labels, tf.int64) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=logits, labels=labels, name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') tf.add_to_collection('losses', cross_entropy_mean) return tf.add_n(tf.get_collection('losses'), name='total_loss') loss = loss(y, y_) # 学习率定为指数衰减型 # learning_rate = tf.train.exponential_decay( # LEARNING_RATE_BASE, # global_step, # 60000 / BATCH_SIZE, # LEARNING_RATE_DECAY, # staircase=True) # train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=global_step) train_step = tf.train.AdamOptimizer(1e-3).minimize(loss, global_step=global_step) # 滑动平均更新新的神经网络参数,保留上一次的参数影响,给予其合适的权重影响新参数 ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) ema_op = ema.apply(tf.trainable_variables()) with tf.control_dependencies([train_step, ema_op]): train_op = tf.no_op(name='train') top_k_op = tf.nn.in_top_k(y, y_, 1) # 实例化saver对象 saver = tf.train.Saver() with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) coord = tf.train.Coordinator() tf.train.start_queue_runners(coord=coord) image_batch, label_batch = sess.run([images_test, labels_test]) ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: # 加载保存的会话 saver.restore(sess, ckpt.model_checkpoint_path) for step in range(max_steps): start_time = time.time() image_batch, label_batch = sess.run( [cifar10_image, cifar10_labels]) _, loss_value = sess.run([train_op, loss], feed_dict={ x: image_batch, y_: label_batch }) duration = time.time() - start_time if step % 10 == 0: examples_per_sec = BATCH_SIZE / duration sec_per_batch = float(duration) format_str = ( 'step %d,loss=%.2f (%.1f examples/sec;%.3f sec/batch)') print(format_str % (step, loss_value, examples_per_sec, sec_per_batch)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step) num_examples = cifar10_input.NUM_EXAMPLES_PER_EPOCH_FOR_EVAL num_iter = int(num_examples / BATCH_SIZE) true_count = 0 total_sample_count = num_iter * BATCH_SIZE step = 0 for step in range(num_iter): # image_batch,label_batch = sess.run([images_test,labels_test]) predictions = sess.run([top_k_op], feed_dict={ x: image_batch, y_: label_batch }) true_count += np.sum(predictions) # print('precision @ 1 = %.3f'%(num_iter)) print('%d,%d' % (true_count, total_sample_count))
def test(): #creates a new graph and places everything (declared inside its scope) into this graph. with tf.Graph().as_default() as g: #define placeholder x,which act as input image x = tf.placeholder(tf.float32, [None, cifar10_forward.INPUT_NODE]) #define y as the computed result which will feed the parameter y_ below y_ = tf.placeholder(tf.float32, [None, cifar10_forward.OUTPUT_NODE]) #execute forward propagation without regularization,and return it's outcome to y y = cifar10_forward.forward(x, None) #Create an ExponentialMovingAverage object ema ema = tf.train.ExponentialMovingAverage( cifar10_backward.MOVING_AVERAGE_DECAY) #method variable_to_restore() return a dict ({ema_variables : variables}) ema_restore = ema.variables_to_restore() #Create a saver that loads variables from their saved shadow values. saver = tf.train.Saver(ema_restore) #if tf.argmax(y, 1) equals to tf.argmax(y_, 1),correct_prediction will be set True correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) #cast the type of corrent_prediction from boolean to float and compute it's average accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) img_batch, label_batch = cifar10_generateds.get_tfrecord( TEST_NUM, isTrain=False) #2 #load trained model in the loop while True: #create session to manage the context with tf.Session() as sess: #fetch chechpoint from sepcified path ckpt = tf.train.get_checkpoint_state( cifar10_backward.MODEL_SAVE_PATH) #if got the checkpoint sucessfully do things below if ckpt and ckpt.model_checkpoint_path: #restore the model to current neural network saver.restore(sess, ckpt.model_checkpoint_path) #fetch the step from the string ckpt.model_checkpoint and extract #the last integer via charactor "/" & "-" global_step = ckpt.model_checkpoint_path.split( '/')[-1].split('-')[-1] coord = tf.train.Coordinator() #3 threads = tf.train.start_queue_runners(sess=sess, coord=coord) #4 xs, ys = sess.run([img_batch, label_batch]) #5 #compute accuracy_score via test data set accuracy_score = sess.run(accuracy, feed_dict={ x: xs, y_: ys }) #print the predict result print("after %s training step(s), test accuracy = %g" % (global_step, accuracy_score)) coord.request_stop() #6 coord.join(threads) #7 else: #can not get checkpoint file ,print error infomation print("No checkpoint file found") #exit this moudle return #set interval time to wait for the checkpoint file which the backward function produce time.sleep(INTERVAL_TIME)
def test(): with tf.Graph().as_default() as g: #复现之前定义的计算图,并执行以下操作 x = tf.placeholder( tf.float32, [None, cifar10_forward.INPUT_NODE]) #定义占位符x,以之代替输入图片 y_ = tf.placeholder( tf.float32, [None, cifar10_forward.OUTPUT_NODE]) #定义占位符y_,用来接神经元计算结果 y = cifar10_forward.forward(x, None) #y是神经元的计算结果,下一步喂给y_ ema = tf.train.ExponentialMovingAverage( cifar10_backward.MOVING_AVERAGE_DECAY ) # 实现滑动平均模型,参数MOVING_AVERAGE_DECAY用于控制模型更新的速度,训练过程中会对每一个变量维护一个影子变量 ema_restore = ema.variables_to_restore( ) # variable_to_restore()返回dict ({ema_variables : variables}),字典中保存变量的影子值和现值 saver = tf.train.Saver( ema_restore) # 创建可还原滑动平均值的对象saver,测试时使用w的影子值,有更好的适配性 correct_prediction = tf.equal( tf.argmax(y, 1), tf.argmax(y_, 1) ) # 比较预测值和标准输出得到correct_prediction,if tf.argmax(y, 1) equals to tf.argmax(y_, 1),correct_prediction will be set True accuracy = tf.reduce_mean( tf.cast(correct_prediction, tf.float32) ) # 将correct_prediction的值从boolean型转为tf.float32型,求均值,得出预测准确率 img_batch, label_batch = cifar10_generateds.get_tfrecord( TEST_NUM, isTrain=False) #2 一次批获取 TEST_NUM 张图片和标签 while True: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state( cifar10_backward.MODEL_SAVE_PATH) # 从指定路径中,加载训练好的模型 if ckpt and ckpt.model_checkpoint_path: # 若已有ckpt模型则执行以下恢复操作 saver.restore(sess, ckpt.model_checkpoint_path) # 恢复会话到当前的神经网络 global_step = ckpt.model_checkpoint_path.split( '/' )[-1].split( '-' )[-1] # 从ckpt.model_checkpoint_path中,通过字符 "/" 和 "-"提取出最后一个整数(保存的轮数),恢复轮数, coord = tf.train.Coordinator() #3开启线程协调器 threads = tf.train.start_queue_runners(sess=sess, coord=coord) #4 xs, ys = sess.run([img_batch, label_batch ]) #5# 在 sess.run 中执行图片和标签的批获取 accuracy_score = sess.run( accuracy, # 计算准确率 feed_dict={ x: xs, y_: ys }) print("after %s training step(s), test accuracy = %g" % (global_step, accuracy_score)) coord.request_stop() #6 coord.join(threads) #7 else: #can not get checkpoint file ,print error infomation print("No checkpoint file found") return time.sleep( INTERVAL_TIME) # 设置等待时间,等backward生成新的checkpoint文件,再循环执行test函数
def backward(): #parameter type :class mnist #define placeholder x,which act as input image x = tf.placeholder(tf.float32, [None, cifar10_forward.INPUT_NODE]) #define placeholder y_,which is output result y_ = tf.placeholder(tf.float32, [None, cifar10_forward.OUTPUT_NODE]) #define y as the computed result which will feed the parameter y_ below y = cifar10_forward.forward(x, REGULARIZER) #define variable golbal_step to count the step where the model run and set it untrainable global_step = tf.Variable(0, trainable=False) #cross entropy,to calculate the distance between the standard probability #distribution and the nerual network calculated probability distribution ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax( y_, 1)) #compute average ce cem = tf.reduce_mean(ce) #compute total loss with cross entropy loss = cem + tf.add_n(tf.get_collection('losses')) #set learning_rate with exponential decay(staircase option on) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, train_num_examples / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) #use gradient descent optimizer to train model train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize( loss, global_step=global_step) #compute exponential moving average(ema) of all tainable variabels #create class ema,prepare to be computed ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) #aplly ema to all trainable variables ema_op = ema.apply(tf.trainable_variables()) #bind operation train_step & ema_op together to realize two operations at time with tf.control_dependencies([train_step, ema_op]): train_op = tf.no_op(name='train') #create class saver to save the session below saver = tf.train.Saver() img_batch, lable_batch = cifar10_generateds.get_tfrecord(BATCH_SIZE, isTrain=True) #3 #run the compute graph below with tf.Session() as sess: #initialize all global variables sess.run(tf.global_variables_initializer()) #restore module #fetch the checkpoint from path "./model" ckpt = tf.train.get_checkpoint_state("./model") #if checkpoint and it’s path exist,do restore() if ckpt and ckpt.model_checkpoint_path: #restore model to current neural network saver.restore(sess, ckpt.model_checkpoint_path) #end of restore coord = tf.train.Coordinator() #4 threads = tf.train.start_queue_runners(sess=sess, coord=coord) #5 for i in range(STEPS): xs, ys = sess.run([img_batch, lable_batch]) #6 #fetch dataset to be trained,assign train image to xs,train labels to ys ##xs, ys = mnist.train.next_batch(BATCH_SIZE) #calculate node train_op, loss,global_step and return the result to _,loss_value, step #'_' means an anonymous variable which will not in use any more _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={ x: xs, y_: ys }) #save current neural network with a 1000-step frequency, if i % 1000 == 0: print( "after %d training step(s), loss on training batch is %g." % (step, loss_value)) #save the global_step neural network(current NN)to specified path(which is MODEL_SAVE_PATH + MODEL_NAME) # and append the step number(3rd argument) to the checkpoint name(2rd argument ) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step) coord.request_stop() #7 coord.join(threads) #8