if not os.path.exists(training_options['model_path']): os.mkdir(training_options['model_path']) # 定义神经网络 net, train = model.build_network(training_options=training_options, image=image, drop_rate=training_options['drop_rate'], labels=labels) # 获得saver对象,可以保存model以及读取model saver = tf.train.Saver() saver.restore(sess, training_options['model_path'] + training_options['model_name']) # 初始化 imageUtils类,获得所有训练,测试数据 imageUtils = ImageUtils() test_data = imageUtils.test_data test_label = imageUtils.test_label total_result = np.zeros((200, 2)) for i in range(200): result = sess.run([net['digit1'], net['digit2'], net['digit3'], net['digit4']], feed_dict={image: imageUtils.trainstion_data(test_data, start=i * 1000,end=i * 1000 + 1000)}) result = code_utils.batch_out_transition(result) predicted = [result[0][index] + result[1][index] + result[2][index] + result[3][index] for index in range(len(result[0]))] label = code_utils.batch_out_transition(test_label[i * 1000:i * 1000 + 1000]) four_right_count = np.count_nonzero([predicted[index] == label[index] for index in range(len(predicted))]) one_right_count = np.count_nonzero( [predicted[index][s_index] == label[index][s_index] for index in range(len(predicted)) for s_index in range(len(predicted[index]))]) total_result[i, 0] = four_right_count / 1000 * 100 total_result[i, 1] = one_right_count / 4000 * 100 print('一千个测试数据: 四个字符同时正确率: {0:.2f}%\t\t单个字符正确率: {1:.2f}%'.format(total_result[i, 0], total_result[i, 1])) print('总结果: 四个字符同时正确率: {0:.2f}%\t\t单个字符正确率: {1:.2f}%'.format(np.mean(total_result[:, 0]), np.mean(total_result[:, 1])))
labels['digit3']: sample_labels[:, 2], labels['digit4']: sample_labels[:, 3] }) writer.add_summary(result, global_step) if episode != 0 and episode % training_options['save_step'] == 0: saver.save( sess, training_options['model_path'] + training_options['model_name']) # 指定回合数时 打印预测值、真实值、正确率 if episode % training_options['show_test'] == 0: t_sample_datas, t_sample_labels = imageUtils.sample( len(test_data), 10, test_data, test_label) result = sess.run( [net['digit1'], net['digit2'], net['digit3'], net['digit4']], feed_dict={image: t_sample_datas}) result = code_utils.batch_out_transition(result) predicted = [ result[0][index] + result[1][index] + result[2][index] + result[3][index] for index in range(len(result[0])) ] label = code_utils.batch_out_transition(t_sample_labels) four_right_count = np.count_nonzero([ predicted[index] == label[index] for index in range(len(predicted)) ]) one_right_count = np.count_nonzero([ predicted[index][s_index] == label[index][s_index] for index in range(len(predicted)) for s_index in range(len(predicted[index])) ]) print('predicted:\t{} \nlabel:\t\t{}'.format(predicted, label))