def run_training(txt_name): logs_train_dir = './face72/faceIDcard/' X_data = read_image() # print(X_data.shape) graph = face_net(BATCH_SIZE, IMG_H, IMG_W, N_CLASSES, learning_rate, 15, 3) sess = tf.Session() saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(logs_train_dir) if ckpt and ckpt.model_checkpoint_path: print('读取模型成功') global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print(global_step) for step in np.arange(MAX_STEP): # for i in range(BATCH_SIZE): # xb= (step%400)*16+i # ximage=np.array(X_data[xb]*255, dtype='uint8') # for xxi in range(72): # cv.circle(ximage,(int(Y_data[xb][2+2*xxi]*96),int(Y_data[xb][2+2*xxi+1]*96)),2,(0, 255, 255), -1) # cv.imshow('ximage',ximage) # cv.waitKey() _, tra_loss, sed_pos, sed_neg = sess.run( [graph['optimize'], graph['loss'], graph['d_pos'], graph['d_neg']], feed_dict={ graph['x']: np.reshape(X_data[(step % 400) * 16:(step % 400) * 16 + 16], (16, 3, 64, 64, 3)) }) #,graph['y']: np.reshape(Y_data[xb], (1, 30)) # print(se_anchor,se_positive,se_anchor) # print('se_anchor',se_anchor) # print('se_positive', se_positive) # print('se_negative', se_negative) if step % 50 == 0: print('同一个人', sed_pos, sess.run(tf.reduce_mean(sed_pos))) print('不同一个人', sed_neg, sess.run(tf.reduce_mean(sed_neg))) print('Step %d,train loss = %.5f' % (step, tra_loss)) constant_graph = graph_util.convert_variables_to_constants( sess, sess.graph_def, ['output/output']) with tf.gfile.FastGFile(logs_train_dir + 'face72.pb', mode='wb') as f: f.write(constant_graph.SerializeToString()) # 每迭代50次,打印出一次结果 # summary_str = sess.run(summary_op) # train_writer.add_summary(summary_str, step) if step % 200 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) # 每迭代200次,利用saver.save()保存一次模型文件,以便测试的时候使用 sess.close()
def run_training(txt_name): imgs = draw_form(MAX_STEP) logs_train_dir = './face72/faceID/' X_data = read_image() graph= face_net(BATCH_SIZE, IMG_H,IMG_W, N_CLASSES,learning_rate,margin=10,run_train=True) sess = tf.Session() saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(logs_train_dir) y_step=0 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(global_step) y_step = int(float(global_step)) loss_list ={} loss_list['x']=[] loss_list['y'] = [] for step in np.arange(MAX_STEP): loss_avg = 0.0 for i in range(BATCH_SIZE): xb= (step%200)*32+i _, tra_loss, sess_pos, sess_neg = sess.run([graph['optimize'],graph['loss'],graph['d_pos'],graph['d_neg']],feed_dict={ graph['x']: np.reshape(X_data[xb], (3, 64, 64, 3))}) loss_avg+=tra_loss avg_loss =loss_avg/BATCH_SIZE loss_list['x'].append(step+y_step) loss_list['y'].append(avg_loss) loss_list['x'].append(step+y_step) loss_list['y'].append(tra_loss) drow_spot(imgs,step, tra_loss, MAX_STEP) print('同:',sess_pos,'不同:',sess_neg,'距离差',sess_neg-sess_pos) if step % 50 == 0: # print('同一个人',sess.run(tf.reduce_mean(sess_pos)),'\t',sess_pos) # print('不同一个人',sess.run(tf.reduce_mean(sess_neg)),'\t',sess_neg) print('Step %d,train loss = %.5f' % (step+y_step, tra_loss)) constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['output/output']) with tf.gfile.FastGFile(logs_train_dir + 'face72.pb', mode='wb') as f: f.write(constant_graph.SerializeToString()) # 每迭代50次,打印出一次结果 if step % 200 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step+y_step) # 每迭代200次,利用saver.save()保存一次模型文件,以便测试的时候使用 sess.close()
'-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) # 调用saver.restore()函数,加载训练好的网络模型 # print('Loading success') else: print('没有保存的模型') pos_d, neg_d = sess.run([graph['d_pos'], graph['d_neg']], feed_dict={ graph['x']: np.reshape(image_arr, (1, 3, 64, 64, 3)) }) return pos_d, neg_d X_data = read_image() for i in range(200): start_time = datetime.datetime.now() pos_d, neg_d = val(X_data[6400 + i]) print('耗时:', datetime.datetime.now() - start_time) img = (X_data[6400 + i][0] * 256 + 128).astype(np.uint8) img = cv.resize(img, (480, 480), interpolation=cv.INTER_CUBIC) print(pos_d, neg_d) # biaoq ='None' # if prediction[0][0]>= 0.8 and prediction[0][0]<1.6: # biaoq = 'Smile' # elif prediction[0][0]>=1.6: # biaoq = 'Laugh' # biaoq+=':' + str(prediction[0][1])