def predict_image(image_name, crop_width, crop_height): input_image = np.expand_dims(np.float32( load_image(image_name)[:crop_height, :crop_width]), axis=0) / 255.0 output_image = sess.run(network, feed_dict={input: input_image}) output_image = np.array(output_image[0, :, :, :]) output_image = helpers.reverse_one_hot(output_image) out_vis_image = helpers.colour_code_segmentation( output_image, class_dict) return out_vis_image
def generate(sess, inputs, root_path=None, path=None, idx=None, save=True): x = sess.run(G, {z: inputs}) if path is None and save: path = os.path.join(root_path, '{}_G.png'.format(idx)) #save_image(x, path) out = np.clip(x[0].transpose([1,2,0]),0,1) out = helpers.reverse_one_hot(out) out = helpers.colour_code_segmentation(out) cv2.imwrite(path, out) print("[*] Samples saved: {}".format(path)) return x
def predict(): # Equivalent to shuffling for test in test_input_names: # to get the right aspect ratio of the output loaded_image = dataset.load_image(test) height, width, channels = loaded_image.shape resize_height = int(height / (width / cfg.width)) resized_image = cv2.resize(loaded_image, (cfg.width, resize_height)) input_image = np.expand_dims( np.float32(resized_image[:cfg.height, :cfg.width]), axis=0) / 255.0 st = time.time() print(input_image.shape) output_image = sess.run(network, feed_dict={net_input: input_image}) run_time = time.time() - st output_image = np.array(output_image[0, :, :, :]) output_image = helpers.reverse_one_hot(output_image) # this needs to get generalized # class_names_list, label_values = helpers.get_label_info(os.path.join(cfg.data_dir, "class_dict.csv")) out_vis_image = helpers.colour_code_segmentation( output_image, label_values) # print(out_vis_image.shape) # out_vis_image = cv2.resize(out_vis_image, (height, width)) # out_vis_image[out_vis_image >= cfg.threshold*255] = 255 # out_vis_image[out_vis_image < cfg.threshold*255] = 0 # # save_img = cv2.cvtColor(np.uint8(loaded_image), cv2.COLOR_RGB2BGR) # transparent_image = np.append(np.array(save_img)[:, :, 0:3], out_vis_image[:, :, None], axis=-1) # transparent_image = Image.fromarray(transparent_image) file_name = utils.filepath_to_name(test) cv2.imwrite( cfg.base_dir + "%s/%s/%s_pred.png" % ("result", "test", file_name), cv2.cvtColor(np.uint8(out_vis_image), cv2.COLOR_RGB2BGR)) # cv2.imwrite(cfg.base_dir + "%s/%s/%s_pred.png" % ("result", "Test", file_name), transparent_image) print("Finished!")
if WRITE_OUTPUT: tmp_f.write("\n\n==== output_image_to_np_array : " + "\n" + "{}".format(output_image)) output_image = helpers.reverse_one_hot(output_image) if WRITE_OUTPUT: tmp_f.write("\n\n===== output_image_to_reverse_one_hot : " + "\n" + "{}".format(output_image)) # this needs to get generalized class_names_list, label_values = helpers.get_label_info( os.path.join(DATASET_NAME, LABEL_NAME)) out_vis_image = helpers.colour_code_segmentation( output_image, label_values) if WRITE_OUTPUT: tmp_f.write("\n\n===== color code : " + "\n" + "{}".format(out_vis_image)) tmp_f.close() file_name = utils.filepath_to_name(test_image) resized_out_vis_image = cv2.resize(np.uint8(out_vis_image), (WIDTH, RESIZE_HEIGHT)) cv2.imwrite("%s/%s_pred.png" % (RESULT_PATH, file_name), cv2.cvtColor(resized_out_vis_image, cv2.COLOR_RGB2BGR)) print("") print("Finished!") print("Wrote image " + "%s/%s_pred.png" % ("Test", file_name))
input_image = np.expand_dims(np.float32( cv2.imread(val_input_names[ind], -1)[:args.crop_height, :args.crop_width]), axis=0) / 255.0 gt = cv2.imread(val_output_names[ind], -1)[:args.crop_height, :args.crop_width] st = time.time() output_image = sess.run(network, feed_dict={input: input_image}) output_image = np.array(output_image[0, :, :, :]) output_image = helpers.reverse_one_hot(output_image) out_eval_image = output_image[:, :, 0] out_vis_image = helpers.colour_code_segmentation(output_image) accuracy = utils.compute_avg_accuracy(out_eval_image, gt) class_accuracies = utils.compute_class_accuracies( out_eval_image, gt, num_classes) prec = utils.precision(out_eval_image, gt) rec = utils.recall(out_eval_image, gt) f1 = utils.f1score(out_eval_image, gt) iou = utils.compute_mean_iou(out_eval_image, gt) file_name = utils.filepath_to_name(val_input_names[ind]) target.write("%s, %f, %f, %f, %f, %f" % (file_name, accuracy, prec, rec, f1, iou)) for item in class_accuracies: target.write(", %f" % (item)) target.write("\n")
input_image = np.expand_dims(np.float32( load_image(val_input_names[ind]) [:args.crop_height, :args.crop_width]), axis=0) / 255.0 gt = load_image( val_output_names[ind])[:args.crop_height, :args.crop_width] gt = helpers.reverse_one_hot(helpers.one_hot_it(gt, class_dict)) # st = time.time() output_image = sess.run(network, feed_dict={input: input_image}) output_image = np.array(output_image[0, :, :, :]) output_image = helpers.reverse_one_hot(output_image) out_vis_image = helpers.colour_code_segmentation( output_image, class_dict) accuracy, class_accuracies, prec, rec, f1, iou = utils.evaluate_segmentation( pred=output_image, label=gt, num_classes=num_classes) file_name = utils.filepath_to_name(val_input_names[ind]) target.write("%s, %f, %f, %f, %f, %f" % (file_name, accuracy, prec, rec, f1, iou)) for item in class_accuracies: target.write(", %f" % (item)) target.write("\n") scores_list.append(accuracy) class_scores_list.append(class_accuracies) precision_list.append(prec) recall_list.append(rec)
def main(): args = get_arguments() # load parameters param = cityscapes_param crop_size = param['crop_size'] num_classes = param['num_classes'] ignore_label = param['ignore_label'] num_steps = param['num_steps'] PSPNet = param['model'] PSPNet_2 = param['model2'] data_dir = param['data_dir'] image_h = 1024 image_w = 2048 overlap_size = 256 # Set up tf session and initialize variables. config = tf.ConfigProto() config.gpu_options.allow_growth = True g1 = tf.Graph() g2 = tf.Graph() g3 = tf.Graph() sess = tf.Session(config=config, graph=g1) sess2 = tf.Session(config=config, graph=g2) sess3 = tf.Session(config=config, graph=g3) # Create network.1 with g1.as_default(): sess.run(tf.global_variables_initializer()) x = tf.placeholder(dtype=tf.float32, shape=[None, None, 3]) img = preprocess(x, image_h, image_w) img = tf.image.crop_to_bounding_box(img, 0, 0, overlap_size, overlap_size) net = PSPNet({'data': img}, is_training=False, num_classes=num_classes) raw_output = net.layers['conv6'] # Predictions. raw_output_up = tf.image.resize_bilinear(raw_output, size=[overlap_size, overlap_size], align_corners=True) raw_output_up = tf.image.pad_to_bounding_box(raw_output_up, 0, 0, image_h, image_w) restore_var = tf.global_variables() ckpt = tf.train.get_checkpoint_state("./logdir_fine/") if ckpt and ckpt.model_checkpoint_path: loader = tf.train.Saver(var_list=restore_var) load_step = int(os.path.basename(ckpt.model_checkpoint_path).split('-')[1]) load(loader, sess, ckpt.model_checkpoint_path) else: print('No checkpoint file found.') # Create network.2 with g2.as_default(): sess2.run(tf.global_variables_initializer()) x2 = tf.placeholder(dtype=tf.float32, shape=[None, None, 3]) img2 = preprocess(x2, image_h, image_w) net2 = PSPNet_2({'data': img2}, is_training=False, num_classes=num_classes) raw_output2 = net2.layers['conv6_v2'] # Predictions. raw_output_up2 = tf.image.resize_bilinear(raw_output2, size=[image_h, image_w], align_corners=True) raw_output_up2 = tf.image.pad_to_bounding_box(raw_output_up2, 0, 0, image_h, image_w) restore_var2 = tf.global_variables() ckpt2 = tf.train.get_checkpoint_state('./logdir_coarse/') if ckpt2 and ckpt2.model_checkpoint_path: loader2 = tf.train.Saver(var_list=restore_var2) load(loader2, sess2, ckpt2.model_checkpoint_path) else: print('No checkpoint file found.') # combine with g3.as_default(): model1 = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 19]) model2 = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 19]) Weights1 = tf.Variable(initial_value=[0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5]) Weights2 = tf.Variable(initial_value=[0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5]) combine_output = tf.add(tf.multiply(model1,Weights1),tf.multiply(model2,Weights2)) sess3.run(tf.global_variables_initializer()) ckpt3 = tf.train.get_checkpoint_state('./combine_variables/') if ckpt3 and ckpt3.model_checkpoint_path: loader3 = tf.train.Saver() load(loader3, sess3, ckpt3.model_checkpoint_path) else: print('No checkpoint file found.') anno_filename = tf.placeholder(dtype=tf.string) # Read & Decode image anno = tf.image.decode_image(tf.read_file(anno_filename), channels=1) anno.set_shape([None, None, 1]) pred_placeholder = tf.placeholder(dtype=tf.int64) pred_expand = tf.expand_dims(pred_placeholder, dim=3) pred_flatten = tf.reshape(pred_expand, [-1, ]) raw_gt = tf.reshape(anno, [-1, ]) indices = tf.squeeze(tf.where(tf.not_equal(raw_gt, ignore_label)), 1) gt = tf.cast(tf.gather(raw_gt, indices), tf.int32) pred = tf.gather(pred_flatten, indices) mIoU, update_op = tf.contrib.metrics.streaming_mean_iou(pred, gt, num_classes=num_classes) eval_sess = tf.Session(config=config) eval_sess.run(tf.local_variables_initializer()) class_scores_list = [] class_names_list, label_values = helpers.get_label_info("class_dict.csv") file = open(param['val_list'], 'r') file_RGB = open("./list/cityscapes_val_list_RGB.txt", 'r') for step in trange(num_steps, desc='evaluation', leave=True): f1, f2 = file.readline().split('\n')[0].split(' ') f1 = os.path.join(data_dir, f1) f2 = os.path.join(data_dir, f2) img_out, _ = load_img(f1) # model 1 preds1 = np.zeros((1, 1024, 2048, 19)) for over_h in range(0, image_h, 256): for over_w in range(0, image_w, 256): if over_h <= image_h - overlap_size and over_w <= image_w - overlap_size: pred_imgs=img_out[over_h:over_h+overlap_size, over_w:over_w+overlap_size] tmp = sess.run(raw_output_up, feed_dict={x: pred_imgs}) tmp2=np.zeros((1,1024,2048,19)) for i in range(overlap_size): for j in range(overlap_size): tmp2[0][i+over_h][j+over_w]=tmp[0][i][j] preds1 += tmp2 preds2 = sess2.run(raw_output_up2, feed_dict={x2: img_out}) combine_out = sess3.run([combine_output], feed_dict={model1: preds1, model2: preds2}) img_out = np.argmax(combine_out[0], axis=3) preds = helpers.colour_code_segmentation(img_out, label_values) # misc.imsave("./1106test/" + str(step) + ".png", preds[0]) # Average per class test accuracies RGB_label = file_RGB.readline().split('\n')[0].split(' ')[1] RGB_label = os.path.join(data_dir, RGB_label) img_label, _ = load_img(RGB_label) img_label = helpers.reverse_one_hot(helpers.one_hot_it(img_label, label_values)) class_accuracies = util.evaluate_segmentation(pred=img_out, label=img_label, num_classes=num_classes) class_scores_list.append(class_accuracies) _ = eval_sess.run(update_op, feed_dict={pred_placeholder: img_out, anno_filename: f2}) print('mIoU: {:04f}'.format(eval_sess.run(mIoU))) class_avg_scores = np.mean(class_scores_list, axis=0) print("Average per class test accuracies = \n") for index, item in enumerate(class_avg_scores): print("%s = %f" % (class_names_list[index], item)) print('mIoU: {:04f}'.format(eval_sess.run(mIoU)))
from PIL import Image test_image = util.load_image(cfg.TEST_IMAGE_PATH) print(test_image.shape) with tf.device('/cpu:0'): test_image = np.float32(test_image) / 255.0 test_image = np.expand_dims(test_image, axis=0) test_image = np.array(test_image) pspnet = PSPNet() prediction = pspnet.predict(test_image) print(prediction.shape) prediction = helpers.reverse_one_hot(prediction) print(prediction.shape) # this needs to get generalized class_names_list, label_values = helpers.get_label_info(os.path.join("CamVid", "class_dict.csv")) out_vis_image = helpers.colour_code_segmentation(prediction, label_values) print(out_vis_image.shape) file_path = cfg.TEST_IMAGE_PATH.replace(".png", "_pred_psp.png") print(file_path) out_vis_image = out_vis_image.reshape((240, 240, 3)) print(out_vis_image.shape) #out_vis_image = Image.fromarray(out_vis_image) cv2.imwrite(file_path, cv2.cvtColor(np.uint8(out_vis_image), cv2.COLOR_RGB2BGR)) #cv2.imwrite(file_path, np.uint8(out_vis_image))
# input_l = np.squeeze(np.stack(input_l, axis=1)) if len(gpus) == 1: input_l = np.expand_dims(input_l, axis=0) # input_image = input_l else: input_l = np.squeeze(np.stack(input_l, axis=1)) output_image = sess.run(network,feed_dict={input:input_l, keep_prob:1.0})#, wavelet1:wav1, wavelet2:wav2, wavelet3: wav3, wavelet4: wav4, keep_prob:1.0}) # output_image = np.array(aux_out[0,:,:,:]) # output_image = helpers.reverse_one_hot(output_image) # out_vis_image = helpers.colour_code_segmentation(output_image, label_values) # cv2.imwrite("%s/%04d/%s_aux_pred.png"%(check,epoch, file_name),cv2.cvtColor(np.uint8(out_vis_image), cv2.COLOR_RGB2BGR)) output_image = np.array(output_image[0,:,:,:]) output_image = helpers.reverse_one_hot(output_image) out_vis_image = helpers.colour_code_segmentation(output_image, label_values) accuracy, class_accuracies, prec, rec, f1, iou = utils.evaluate_segmentation(pred=output_image, label=gt, num_classes=num_classes) target.write("\n %s, %f, %f, %f, %f, %f \n"%(file_name, accuracy, prec, rec, f1, iou)) for item in class_accuracies: target.write(", %f"%(item)) target.write("\n") scores_list.append(accuracy) class_scores_list.append(class_accuracies) precision_list.append(prec) recall_list.append(rec) f1_list.append(f1) iou_list.append(iou)
def val(): # Create directories if needed if not os.path.isdir(cfg.base_dir + "%s/%s" % ("result", "Val")): os.makedirs(cfg.base_dir + "%s/%s" % ("result", "Val")) target = open(cfg.base_dir + "%s/%s/val_scores.csv" % ("result", "Val"), 'w') target.write( "val_name, avg_accuracy, precision, recall, f1 score, mean iou, %s\n" % (class_names_string)) scores_list = [] class_scores_list = [] precision_list = [] recall_list = [] f1_list = [] iou_list = [] run_times_list = [] # Run testing on ALL test images for ind in range(len(val_input_names)): sys.stdout.write("\rRunning test image %d / %d" % (ind + 1, len(val_input_names))) sys.stdout.flush() input_image = np.expand_dims(np.float32( dataset.load_image(val_input_names[ind])[:cfg.height, :cfg.width]), axis=0) / 255.0 gt = dataset.load_image(val_output_names[ind])[:cfg.height, :cfg.width] gt = helpers.reverse_one_hot(helpers.one_hot_it(gt, label_values)) st = time.time() output_image = sess.run(network, feed_dict={net_input: input_image}) run_times_list.append(time.time() - st) output_image = np.array(output_image[0, :, :, :]) output_image = helpers.reverse_one_hot(output_image) out_vis_image = helpers.colour_code_segmentation( output_image, label_values) accuracy, class_accuracies, prec, rec, f1, iou = utils.evaluate_segmentation( pred=output_image, label=gt, num_classes=num_classes) file_name = utils.filepath_to_name(val_input_names[ind]) target.write("%s, %f, %f, %f, %f, %f" % (file_name, accuracy, prec, rec, f1, iou)) for item in class_accuracies: target.write(", %f" % (item)) target.write("\n") scores_list.append(accuracy) class_scores_list.append(class_accuracies) precision_list.append(prec) recall_list.append(rec) f1_list.append(f1) iou_list.append(iou) gt = helpers.colour_code_segmentation(gt, label_values) cv2.imwrite( cfg.base_dir + "%s/%s/%s_pred.png" % ("result", "Val", file_name), cv2.cvtColor(np.uint8(out_vis_image), cv2.COLOR_RGB2BGR)) cv2.imwrite( cfg.base_dir + "%s/%s/%s_gt.png" % ("result", "Val", file_name), cv2.cvtColor(np.uint8(gt), cv2.COLOR_RGB2BGR)) target.close() avg_score = np.mean(scores_list) class_avg_scores = np.mean(class_scores_list, axis=0) avg_precision = np.mean(precision_list) avg_recall = np.mean(recall_list) avg_f1 = np.mean(f1_list) avg_iou = np.mean(iou_list) avg_time = np.mean(run_times_list) print("Average test accuracy = ", avg_score) print("Average per class test accuracies = \n") for index, item in enumerate(class_avg_scores): print("%s = %f" % (class_names_list[index], item)) print("Average precision = ", avg_precision) print("Average recall = ", avg_recall) print("Average F1 score = ", avg_f1) print("Average mean IoU score = ", avg_iou) print("Average run time = ", avg_time)
def train(): if cfg.class_balancing: print("Computing class weights for trainlabel ...") class_weights = utils.compute_class_weights( labels_dir=train_output_names, label_values=label_values) weights = tf.reduce_sum(class_weights * net_output, axis=-1) unweighted_loss = None unweighted_loss = tf.nn.softmax_cross_entropy_with_logits_v2( logits=network, labels=net_output) losses = unweighted_loss * class_weights else: losses = tf.nn.softmax_cross_entropy_with_logits_v2(logits=network, labels=net_output) loss = tf.reduce_mean(losses) opt = tf.train.AdamOptimizer(cfg.lr).minimize( loss, var_list=[var for var in tf.trainable_variables()]) sess.run(tf.global_variables_initializer()) utils.count_params() # If a pre-trained ResNet is required, load the weights. # This must be done AFTER the variables are initialized with sess.run(tf.global_variables_initializer()) if init_fn is not None: init_fn(sess) avg_scores_per_epoch = [] avg_loss_per_epoch = [] # Which validation images do we want val_indices = [] num_vals = min(cfg.num_val_images, len(val_input_names)) # Set random seed to make sure models are validated on the same validation images. # So you can compare the results of different models more intuitively. random.seed(16) val_indices = random.sample(range(0, len(val_input_names)), num_vals) # Do the training here for epoch in range(0, cfg.num_epochs): current_losses = [] cnt = 0 # Equivalent to shuffling id_list = np.random.permutation(len(train_input_names)) num_iters = int(np.floor(len(id_list) / cfg.batch_size)) st = time.time() epoch_st = time.time() for i in range(num_iters): # st=time.time() input_image_batch = [] output_image_batch = [] # Collect a batch of images for j in range(cfg.batch_size): index = i * cfg.batch_size + j id = id_list[index] input_image = dataset.load_image(train_input_names[id]) output_image = dataset.load_image(train_output_names[id]) h, w, _ = input_image.shape new_h, new_w = dataset.getTrainSize(h, w) with tf.device('/cpu:0'): input_image, output_image = dataset.data_augmentation( input_image, output_image, new_h, new_w) # Prep the data. Make sure the labels are in one-hot format input_image = np.float32(input_image) / 255.0 output_image = np.float32( helpers.one_hot_it(label=output_image, label_values=label_values)) input_image_batch.append( np.expand_dims(input_image, axis=0)) output_image_batch.append( np.expand_dims(output_image, axis=0)) # ***** THIS CAUSES A MEMORY LEAK AS NEW TENSORS KEEP GETTING CREATED ***** # input_image = tf.image.crop_to_bounding_box(input_image, offset_height=0, offset_width=0, # target_height=args.crop_height, target_width=args.crop_width).eval(session=sess) # output_image = tf.image.crop_to_bounding_box(output_image, offset_height=0, offset_width=0, # target_height=args.crop_height, target_width=args.crop_width).eval(session=sess) # ***** THIS CAUSES A MEMORY LEAK AS NEW TENSORS KEEP GETTING CREATED ***** # memory() # print(cfg.batch_size) if cfg.batch_size == 1: input_image_batch = input_image_batch[0] output_image_batch = output_image_batch[0] else: input_image_batch = np.squeeze( np.stack(input_image_batch, axis=1)) output_image_batch = np.squeeze( np.stack(output_image_batch, axis=1)) # print(input_image_batch.shape) # Do the training _, current = sess.run([opt, loss], feed_dict={ net_input: input_image_batch, net_output: output_image_batch }) current_losses.append(current) cnt = cnt + cfg.batch_size if cnt % 20 == 0: string_print = "Epoch = %d Count = %d Current_Loss = %.4f Time = %.2f" % ( epoch, cnt, current, time.time() - st) utils.LOG(string_print) st = time.time() mean_loss = np.mean(current_losses) avg_loss_per_epoch.append(mean_loss) # Create directories if needed if not os.path.isdir(cfg.base_dir + "%s/%s/%04d" % ("checkpoints", cfg.model, epoch)): os.makedirs(cfg.base_dir + "%s/%s/%04d" % ("checkpoints", cfg.model, epoch)) # Save latest checkpoint to same file name print("Saving latest checkpoint") saver.save(sess, model_checkpoint_name) if val_indices != 0 and epoch % cfg.checkpoint_step == 0: print("Saving checkpoint for this epoch") saver.save( sess, cfg.base_dir + "%s/%s/%04d/model.ckpt" % ("checkpoints", cfg.model, epoch)) if epoch % cfg.validation_step == 0: print("Performing validation") target = open( cfg.base_dir + "%s/%s/%04d/val_scores.csv" % ("checkpoints", cfg.model, epoch), 'w') target.write( "val_name, avg_accuracy, precision, recall, f1 score, mean iou, %s\n" % (class_names_string)) scores_list = [] class_scores_list = [] precision_list = [] recall_list = [] f1_list = [] iou_list = [] # Do the validation on a small set of validation images for ind in val_indices: input_image = dataset.load_image(val_input_names[ind]) output_image = dataset.load_image(val_output_names[ind]) h, w, _ = input_image.shape new_h, new_w = dataset.getTrainSize(h, w) input_image, output_image = utils.random_crop( input_image, output_image, new_h, new_w) input_image = np.expand_dims(np.float32(input_image), axis=0) / 255.0 gt = helpers.reverse_one_hot( helpers.one_hot_it(output_image, label_values)) # st = time.time() output_image = sess.run(network, feed_dict={net_input: input_image}) output_image = np.array(output_image[0, :, :, :]) output_image = helpers.reverse_one_hot(output_image) out_vis_image = helpers.colour_code_segmentation( output_image, label_values) accuracy, class_accuracies, prec, rec, f1, iou = utils.evaluate_segmentation( pred=output_image, label=gt, num_classes=num_classes) file_name = utils.filepath_to_name(val_input_names[ind]) target.write("%s, %f, %f, %f, %f, %f" % (file_name, accuracy, prec, rec, f1, iou)) for item in class_accuracies: target.write(", %f" % (item)) target.write("\n") scores_list.append(accuracy) class_scores_list.append(class_accuracies) precision_list.append(prec) recall_list.append(rec) f1_list.append(f1) iou_list.append(iou) gt = helpers.colour_code_segmentation(gt, label_values) file_name = os.path.basename(val_input_names[ind]) file_name = os.path.splitext(file_name)[0] cv2.imwrite( cfg.base_dir + "%s/%s/%04d/%s_pred.png" % ("checkpoints", cfg.model, epoch, file_name), cv2.cvtColor(np.uint8(out_vis_image), cv2.COLOR_RGB2BGR)) cv2.imwrite( cfg.base_dir + "%s/%s/%04d/%s_gt.png" % ("checkpoints", cfg.model, epoch, file_name), cv2.cvtColor(np.uint8(gt), cv2.COLOR_RGB2BGR)) target.close() avg_score = np.mean(scores_list) class_avg_scores = np.mean(class_scores_list, axis=0) avg_scores_per_epoch.append(avg_score) avg_precision = np.mean(precision_list) avg_recall = np.mean(recall_list) avg_f1 = np.mean(f1_list) avg_iou = np.mean(iou_list) print("\nAverage validation accuracy for epoch # %04d = %f" % (epoch, avg_score)) print("Average per class validation accuracies for epoch # %04d:" % (epoch)) for index, item in enumerate(class_avg_scores): print("%s = %f" % (class_names_list[index], item)) print("Validation precision = ", avg_precision) print("Validation recall = ", avg_recall) print("Validation F1 score = ", avg_f1) print("Validation IoU score = ", avg_iou) epoch_time = time.time() - epoch_st remain_time = epoch_time * (cfg.num_epochs - 1 - epoch) m, s = divmod(remain_time, 60) h, m = divmod(m, 60) if s != 0: train_time = "Remaining training time = %d hours %d minutes %d seconds\n" % ( h, m, s) else: train_time = "Remaining training time : Training completed.\n" utils.LOG(train_time) scores_list = [] utils.drawLine(range(cfg.num_epochs), avg_scores_per_epoch, cfg.base_dir + 'checkpoints/' + cfg.model + '/accuracy_vs_epochs.png', title='Average validation accuracy vs epochs', xlabel='Epoch', ylabel='Avg. val. accuracy') utils.drawLine(range(cfg.num_epochs), avg_loss_per_epoch, cfg.base_dir + 'checkpoints/' + cfg.model + '/loss_vs_epochs.png', title='Average loss vs epochs', xlabel='Epoch', ylabel='Current loss')