def predict_and_save_pics_model(): with tf.Session(config=session_config) as sess: # 入口 train_x, train_y = create_inputs(is_train) # train_y = tf.reshape(train_y, labels_shape) x = tf.placeholder(tf.float32, shape=input_shape) y = tf.placeholder(tf.float32, shape=labels_shape) # 构建网络 prediction, end_points = model(images=x, is_train=is_train, size=input_shape, l2_reg=0.0001) # 打印模型结构 dk.print_model_struct(end_points) # prediction = tf.reshape(prediction, labels_shape) # 初始化变量 coord, threads = dk.init_variables_and_start_thread(sess) # 恢复model saver = dk.restore_model(sess, ckpt, restore_model=restore_model) # 显示参数量 dk.show_parament_numbers() # 测试loop start_epoch = 0 index = 0 for epoch_n in range(start_epoch, test_opoch): for n_batch in range(n_batch_test): batch_x, batch_y = sess.run([train_x, train_y]) # 取出一个batchsize的图片。 batch_x = batch_x / 255.0 # 3、预测输出 since = time.time() predict = sess.run(prediction, feed_dict={x: batch_x}) seconds = time.time() - since # 4、预测图转二值化图(非0即1) predict[predict >= 0.5] = 1 predict[predict < 0.5] = 0 # 5、把batch图片分割一张张图片 batch_pre_list = np.split(predict, batch_size, axis=0) pre_pics_list = np.squeeze(batch_pre_list, axis=0) for img, label, predict in zip(batch_x, batch_y, pre_pics_list): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) label = cv2.cvtColor(label, cv2.COLOR_GRAY2RGB) predict = cv2.cvtColor(predict, cv2.COLOR_GRAY2RGB) hstack = np.hstack((img, label, predict)) hstack = cv2.resize(hstack, (512, 512)) save_name = '{}.jpg'.format(index) save_name = os.path.join(predict_pics_save, save_name) cv2.imshow('hstack', hstack) cv2.waitKey(500) cv2.imwrite(save_name, hstack * 255) index += 1 print(save_name) dk.stop_threads(coord, threads)
def train_mnist_model(): with tf.Session(config=session_config) as sess: mnist = create_inputs.read_data_sets(dataset_path, one_hot=True) # 计算有多少批次 n_batch = mnist.train.num_examples // batch_size # 构建网络 x = tf.placeholder(tf.float32, [None, 784]) y = tf.placeholder(tf.float32, [None, class_num]) prediction, _ = model(x, input_shape, class_num) # 求loss loss = dk.cross_entropy_loss(prediction, y) # 设置优化器 global_step, train_step = dk.set_optimizer( lr_range, num_batches_per_epoch=n_batch, loss=loss) # 求acc accuracy = dk.get_acc(prediction, y) # 初始化变量 coord, threads = dk.init_variables_and_start_thread(sess) # 恢复model saver, start_epoch = dk.restore_model(sess, ckpt, restore_model=False) # 设置训练日志 summary_dict = {'loss': loss, 'accuracy': accuracy} summary_writer, summary_op = dk.set_summary(sess, logdir, summary_dict) # epoch=50 for epoch_th in range(epoch): for bat in range(n_batch): batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 训练一个step _, loss_value, acc_value, summary_str, step = sess.run( [train_step, loss, accuracy, summary_op, global_step], feed_dict={ x: batch_xs, y: batch_ys }) # 显示结果 dk.print_message(epoch_th, bat, n_batch, step, loss_value, acc_value) # 保存summary if (step + 1) % 20 == 0: summary_writer.add_summary(summary_str, step) # 保存model if (((epoch_th + 1) % 50)) == 0: print('saving movdel.......') saver.save(sess, os.path.join(ckpt, 'model_{}.ckpt'.format(epoch_th)), global_step=global_step) dk.stop_threads(coord, threads)
def evaluate_result(): with tf.Session(config=session_config) as sess: test_x, test_y = create_inputs(is_train) # test_y = tf.one_hot(test_y, depth=class_num, axis=1, dtype=tf.int32) # 构建网络 x = tf.placeholder(tf.float32, shape=[ input_shape[0], input_shape[1], input_shape[2], input_shape[3] ]) y = tf.placeholder(tf.float32, shape=[input_shape[0], class_num]) prediction, endpoint = model(x, input_shape, class_num) # 打印模型结构 dk.print_model_struct(endpoint) # 求acc accuracy = dk.get_acc(prediction, y) # 初始化变量 coord, threads = dk.init_variables_and_start_thread(sess) # 恢复model saver = dk.restore_model(sess, ckpt, restore_model=restore_model) # 显示参数量 dk.show_parament_numbers() start_epoch = 0 acc_list = [] if is_train: print('train_number:', train_number) n_batch_train = int(train_number // batch_size) n_batch_total = n_batch_train else: print('test_number:', test_number) n_batch_test = int(test_number // batch_size) n_batch_total = n_batch_test for epoch_n in range(start_epoch, epoch): for n_batch in range(n_batch_total): batch_x, batch_y = sess.run([test_x, test_y]) # 训练一个step acc_value = sess.run(accuracy, feed_dict={ x: batch_x, y: batch_y }) # 显示结果batch_size print('epoch_n:', epoch_n, ' n_batch:', n_batch, ' acc_value:', acc_value) acc_list.append(acc_value) result = np.mean(acc_list) print('final result: ', result) dk.stop_threads(coord, threads)
########################## end ########################################## #代码初始化 session_config = dk.set_gpu() n_batch_train = int(train_data_number //batch_size) n_batch_test = int(test_data_number //batch_size) os.makedirs(save_plot_curve_dir,exist_ok=True) if __name__== '__main__': with tf.Session(config=session_config) as sess: # 入口 train_x, train_y = create_inputs(is_train) train_y = tf.reshape(train_y, labels_shape) x = tf.placeholder(tf.float32, shape=input_shape) y = tf.placeholder(tf.float32, shape=labels_shape) # 构建网络 prediction = model(images=x, is_train=True, size=input_shape, l2_reg=0.0001) prediction = tf.reshape(prediction, labels_shape) # 初始化变量 coord, threads = dk.init_variables_and_start_thread(sess) # 恢复model saver = dk.restore_model(sess, ckpt, restore_model=restore_model) # 显示参数量 dk.show_parament_numbers() # 列表初始化 AUC_ROC_list,AUC_prec_rec_list,accuracy_list,specificity_list,sensitivity_list,\ precision_list,jaccard_index_list,F1_score_list,all_list,mean_list\ =[],[],[],[],[],[],[],[],[],[] # 测试loop start_epoch= 0 index = 0 for epoch_n in range(start_epoch, test_opoch):
def train_cifar10_model(): with tf.Session(config=session_config) as sess: #入口 train_x, train_y = create_inputs(is_train) x = tf.placeholder(tf.float32, shape=input_shape) y = tf.placeholder(tf.float32, shape=label_shape) # 构建网络 prediction, _ = model(x, input_shape, class_num) # 求loss loss = dk.cross_entropy_loss(prediction, y) # 设置优化器 global_step, train_step = dk.set_optimizer( lr_range, num_batches_per_epoch=n_batch_train, loss=loss) # 求acc accuracy = dk.get_acc(prediction, y) # 初始化变量 coord, threads = dk.init_variables_and_start_thread(sess) # 设置训练日志 summary_dict = {'loss': loss, 'accuracy': accuracy} summary_writer, summary_op = dk.set_summary(sess, logdir, summary_dict) # 恢复model saver, start_epoch = dk.restore_model(sess, ckpt, restore_model=False) # 显示参数量 dk.show_parament_numbers() start_epoch = 0 if restore_model: step = sess.run(global_step) start_epoch = int( step / n_batch_train / save_epoch_n) * save_epoch_n # 训练loop total_step = n_batch_train * epoch for epoch_n in range(epoch): pre_index = 0 since = time.time() acc_value_list = [] for n_batch in range(n_batch_train): if pre_index + batch_size < 50000: batch_x = train_x[pre_index:pre_index + batch_size] batch_y = train_y[pre_index:pre_index + batch_size] else: batch_x = train_x[pre_index:] batch_y = train_y[pre_index:] # 训练一个step _, loss_value, acc_value, summary_str, step = sess.run( [train_step, loss, accuracy, summary_op, global_step], feed_dict={ x: batch_x, y: batch_y }) # 显示结果 dk.print_message(epoch_n, n_batch, n_batch_train, step, loss_value, acc_value) # 保存summary if (step + 1) % 20 == 0: summary_writer.add_summary(summary_str, step) pre_index += batch_size # 保存结果 acc_value_list.append(acc_value) # 显示进度、耗时、最小最大平均值 seconds_mean = (time.time() - since) / n_batch_train dk.print_progress_and_time_massge(seconds_mean, step, total_step, acc_value_list) # 保存model if (((epoch_n + 1) % save_epoch_n)) == 0: print('epoch_n :{} saving movdel.......'.format(epoch_n)) saver.save(sess, os.path.join(ckpt, 'model_{}.ckpt'.format(epoch_n)), global_step=global_step) dk.stop_threads(coord, threads)
def train_model(): # 代码初始化 n_batch_train = int(train_data_number // batch_size) print('n_batch_train: ', n_batch_train) os.makedirs(ckpt, exist_ok=True) session_config = dk.set_gpu() with tf.Session(config=session_config) as sess: #如果使用tensorlfow1的debug神器(主要用于查出哪里有inf或nan,不能在pycharm运行调试程序,只能在xshell里面运行) if use_tensoflow_debug: sess = tfdbg.LocalCLIDebugWrapperSession(sess) sess.add_tensor_filter("has_inf_or_nan", tfdbg.has_inf_or_nan) #然后在xshell里面运行run -f has_inf_or_nan # 一旦inf / nan出现,界面现实所有包含此类病态数值的张量,按照时间排序。所以第一个就最有可能是最先出现inf / nan的节点。 # 可以用node_info, list_inputs等命令进一步查看节点的类型和输入,来发现问题的缘由。 #教程https://blog.csdn.net/tanmx219/article/details/82318133 # 入口 train_x, train_y = create_inputs(is_train) x = tf.placeholder(tf.float32, shape=input_shape) y = tf.placeholder(tf.float32, shape=labels_shape) # 构建网络和预测 prediction, endpoint = model(images=x, is_train=is_train, size=input_shape, l2_reg=0.0001) # 打印模型结构 dk.print_model_struct(endpoint) # 求loss the_loss = get_loss(choose_loss) loss = the_loss(y, prediction, labels_shape_vec) # 设置优化器 global_step, train_step = dk.set_optimizer( lr_range=lr_range, num_batches_per_epoch=n_batch_train, loss=loss) # 求dice_hard,不合适用acc dice_hard = dk.dice_hard(y, prediction, threshold=0.5, axis=[1, 2, 3], smooth=1e-5) # dice_hard = dk.iou_metric(prediction, y) # 初始化变量 coord, threads = dk.init_variables_and_start_thread(sess) # 设置训练日志 summary_dict = {'loss': loss, 'dice_hard': dice_hard} summary_writer, summary_op = dk.set_summary(sess, logdir, summary_dict) # 恢复model saver, start_epoch = dk.restore_model(sess, ckpt, restore_model=restore_model) # 显示参数量 dk.show_parament_numbers() # 训练loop total_step = n_batch_train * epoch for epoch_n in range(start_epoch, epoch): dice_hard_value_list = [] #清空 since = time.time() for n_batch in range(n_batch_train): batch_x, batch_y = sess.run([train_x, train_y]) ########################## 数据增强 ################################### batch_x = batch_x / 255.0 # 归一化,加了这句话loss值小了几十倍 batch_x, batch_y = augmentImages(batch_x, batch_y) ########################## end ####################################### # 训练一个step _, loss_value, dice_hard_value, summary_str, step = sess.run( [train_step, loss, dice_hard, summary_op, global_step], feed_dict={ x: batch_x, y: batch_y }) # 显示结果batch_size dk.print_effect_message(epoch_n, n_batch, n_batch_train, loss_value, dice_hard_value) # 保存summary if (step + 1) % 20 == 0: summary_writer.add_summary(summary_str, step) # 保存结果 dice_hard_value_list.append(dice_hard_value) # 显示进度、耗时、最小最大平均值 seconds_mean = (time.time() - since) / n_batch_train dk.print_progress_and_time_massge(seconds_mean, step, total_step, dice_hard_value_list) # 保存model if (((epoch_n + 1) % save_epoch_n)) == 0: print('epoch_n :{} saving movdel.......'.format(epoch_n)) saver.save(sess, os.path.join(ckpt, 'model_{}.ckpt'.format(epoch_n)), global_step=global_step) dk.stop_threads(coord, threads)
def evaluate_by_sklearn_model(): with tf.Session(config=session_config) as sess: # 入口 train_x, train_y = create_inputs(is_train) train_y = tf.reshape(train_y, labels_shape) x = tf.placeholder(tf.float32, shape=input_shape) y = tf.placeholder(tf.float32, shape=labels_shape) # 构建网络 prediction, end_points = model(images=x, is_train=True, size=input_shape, l2_reg=0.0001) prediction = tf.reshape(prediction, labels_shape) # 打印模型结构 dk.print_model_struct(end_points) # 初始化变量 coord, threads = dk.init_variables_and_start_thread(sess) # 恢复model saver = dk.restore_model(sess, ckpt, restore_model=restore_model) # 显示参数量 dk.show_parament_numbers() # 列表初始化 AUC_ROC_list,AUC_prec_rec_list,accuracy_list,specificity_list,sensitivity_list,\ precision_list,jaccard_index_list,F1_score_list,all_list,mean_list\ =[],[],[],[],[],[],[],[],[],[] # 测试loop start_epoch = 0 index = 0 for epoch_n in range(start_epoch, test_opoch): for n_batch in range(n_batch_test): batch_x, batch_y = sess.run([train_x, train_y]) # 取出一个batchsize的图片。 batch_x = batch_x / 255.0 # 3、预测输出 since = time.time() predict = sess.run(prediction, feed_dict={x: batch_x}) seconds = time.time() - since # 4、预测图转二值化图(非0即1),加了这步性能指标会下降2%左右 predict[predict >= 0.5] = 1 predict[predict < 0.5] = 0 # 5、把batch图片分割一张张图片 batch_pre_list = np.split(predict, batch_size, axis=0) pre_pics_list = np.squeeze(batch_pre_list, axis=0) for label, pre in zip(batch_y, pre_pics_list): index += 1 y_scores = pre.reshape(-1, 1) y_true = label.reshape(-1, 1) print( '######################### start ####################################' ) # 1、画ROC曲线 AUC_ROC = esk.plot_roc_curve(y_true, y_scores, save_plot_curve_dir, curve_name=str(index)) AUC_ROC_list.append(AUC_ROC) #2、画P_R-curve曲线 AUC_prec_rec = esk.plot_precision_recall_curve( y_true, y_scores, save_plot_curve_dir, curve_name=str(index)) #3、Confusion matrix y_pred_binary = esk.convert_to_binary( shape=y_scores.shape[0], y_scores=y_scores) accuracy, specificity, sensitivity, precision \ = esk.plot_confusion_matrix(y_true, y_pred_binary) #4、Jaccard similarity index jaccard_index = esk.get_jaccard_index( y_true, y_pred_binary) #5、F1 score F1_score = esk.get_F1_score(y_true, y_pred_binary) print( '######################### end ####################################' ) # 保存结果 AUC_prec_rec_list.append(AUC_prec_rec) accuracy_list.append(accuracy) specificity_list.append(specificity) sensitivity_list.append(sensitivity) precision_list.append(precision) jaccard_index_list.append(jaccard_index) F1_score_list.append(F1_score) #1、评估数据存进列表中 all_list = [AUC_ROC_list, AUC_prec_rec_list, accuracy_list, specificity_list \ , sensitivity_list, precision_list, jaccard_index_list, F1_score_list] name_list = [ 'AUC_ROC', 'AUC_prec_rec', 'accuracy', 'specificity', 'sensitivity', 'precision', 'jaccard_index', 'F1_score' ] # 2、panda保存所有图片的评估值到CSV文件 esk.save_all_pics_value(name_list, all_list, save_list_csv) #3、panda保存平均值到CSV文件 esk.save_mean_value(name_list, all_list, save_mean_csv) #4、结束 dk.stop_threads(coord, threads)
def predict_and_save_tensor_feature_map_model(): with tf.Session(config=session_config) as sess: # 入口 train_x, train_y = create_inputs(is_train) x = tf.placeholder(tf.float32, shape=input_shape) # 构建网络 prediction, end_points = model(images=x, is_train=is_train, size=input_shape, l2_reg=0.0001) # 打印模型结构 dk.print_model_struct(end_points) # 初始化变量 coord, threads = dk.init_variables_and_start_thread(sess) # 恢复model saver, start_epoch = dk.restore_model(sess, ckpt, restore_model=restore_model) # 显示参数量 dk.show_parament_numbers() # 测试loop n_batch_index = 0 n_pic_index = 0 channel_index = 0 for n_batch in range(n_batch_test): n_batch_index += 1 batch_x, batch_y = sess.run([train_x, train_y]) # 取出一个batchsize的图片。 batch_x = batch_x / 255.0 # 3、预测输出一个张量 for key, value in end_points.items(): #保存的路径 save_root = os.path.join(predict_tensor_feature_map, key) os.makedirs(save_root, exist_ok=True) predict_end_points = sess.run(value, feed_dict={x: batch_x}) shape = predict_end_points.shape if len(shape) == 4: batch_size, width, height, channel = shape elif len(shape) == 5: batch_size, width, height, channel_1, channel_2 = shape shape = (batch_size, width, height, channel_1 * channel_2) predict_end_points = np.reshape(predict_end_points, shape) channel = channel_1 * channel_2 else: raise Exception('f**k!') #切分batch_size batch_size_list = np.split(predict_end_points, batch_size, axis=0) pics_list = [] # 清空pics_list for each_b in batch_size_list: #循环一个batch_size里面的每张图片图片 pic_channel_list = [] #一张图片切分成channel个,把每个featu map装进pics_list里面 channel_list = np.split(each_b, channel, axis=3) for each_c in channel_list: # 切分channel(显示每个channel的图片) each_c = np.reshape(each_c, (width, height, 1)) #得到一张图片 # 显示方式1:二值化 # 4、预测图转二值化图(非0即1) ,经过试验tensor阈值为0最佳 # each_c[each_c >= 0] = 255 # each_c[each_c < 0] = 0 # pics_list.extend([each_c]) # 显示方式2:归一化并转成灰度图处理 each_c = MaxMinNormalization(each_c, Max=np.max(each_c), Min=np.min(each_c)) # each_c = cv2.cvtColor(each_c, cv2.COLOR_GRAY2RGB) # pics_list.extend([each_c]) # 显示方式3: # each_c = Z_ScoreNormalization(each_c, mu=np.average(each_c), sigma=np.std(each_c)) # each_c = cv2.cvtColor(each_c, cv2.COLOR_GRAY2RGB) pic_channel_list.extend([each_c]) #一张图片所有的channel pics_list.append(pic_channel_list) #把pic_channel_list添加进去 for pic_channel_list in pics_list: #第几张图片 n_pic_index += 1 new_dir = '{}_{}'.format(n_batch_index, n_pic_index) save_dir = os.path.join(save_root, new_dir) os.makedirs(save_dir, exist_ok=True) for pic in pic_channel_list: #第几个通道 channel_index += 1 # 保存的文件名 save_name = '{}.jpg'.format(channel_index) # 保存的整个路径 save_name = os.path.join(save_dir, save_name) cv2_show = True if cv2_show == True: cv2.imshow('pic', pic) cv2.imwrite(save_name, pic * 255) cv2.waitKey(500) else: plt_imshow_1_pics(pic, save_name) time.sleep(1) print(save_name) channel_index = 0 n_pic_index = 0 n_batch_index = 0 dk.stop_threads(coord, threads) # coord.request_stop() # coord.join(threads) ########################废弃代码 # 4、预测图转二值化图(非0即1) # predict[predict>=0.5] =1 # predict[predict< 0.5] =0 # 5、把batch图片分割一张张图片 # batch_pre_list = np.split(predict, batch_size, axis=0) # pre_pics_list = np.squeeze(batch_pre_list, axis=0) # for img, label, predict in zip(batch_x, batch_y, pre_pics_list): # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # label = cv2.cvtColor(label, cv2.COLOR_GRAY2RGB) # predict = cv2.cvtColor(predict, cv2.COLOR_GRAY2RGB) # hstack = np.hstack((img, label,predict)) # hstack = cv2.resize(hstack,(512,512)) # # save_name = '{}.jpg'.format(index) # save_name = os.path.join(predict_pics_save, save_name) # cv2.imshow('hstack',hstack) # cv2.waitKey(500) # cv2.imwrite(save_name, hstack*255) # index += 1 # print(save_name)