Exemple #1
0
                    input_image = cv2.flip(input_image, 0)
                    output_image = cv2.flip(output_image, 0)
                if args.brightness:
                    factor = 1.0 + abs(
                        random.gauss(mu=0.0, sigma=self.brightness))
                    if random.randint(0, 1):
                        factor = 1.0 / factor
                    table = np.array([
                        ((i / 255.0)**factor) * 255 for i in np.arange(0, 256)
                    ]).astype(np.uint8)
                    image = cv2.LUT(image, table)

                # 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,
                                       num_classes=num_classes))

                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()

            if args.batch_size == 1:
                input_image_batch = input_image_batch[0]
Exemple #2
0
            # Collect a batch of images
            for j in range(args.batch_size):
                index = i * args.batch_size + j
                id = id_list[index]
                input_image = load_image(train_input_names[id])
                output_image = load_image(train_output_names[id])

                with tf.device('/cpu:0'):
                    input_image, output_image = data_augmentation(
                        input_image, output_image)

                    # 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()
            file_name = utils_seg.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_map = helpers.reverse_one_hot(helpers.one_hot_it(gt_map))
            gt_map = helpers.colour_code_segmentation(gt_map)

            file_name = os.path.basename(val_input_names[ind])
            file_name = os.path.splitext(file_name)[0]
            cv2.imwrite(model_dir + "/%s_pred.png" % (ind),
                        np.uint8(out_vis_image))
            cv2.imwrite(model_dir + "/%s_gt.png" % (ind), np.uint8(gt_map))
        avg_score = np.mean(scores_list)
        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 iteration # %06d = %f" %
              (step, avg_score))
Exemple #4
0
            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.reverse_one_hot(helpers.one_hot_it(gt))
            gt = helpers.colour_code_segmentation(gt)

            file_name = os.path.basename(val_input_names[ind])
            file_name = os.path.splitext(file_name)[0]
            cv2.imwrite(
                "%s/%04d/%s_pred.png" % ("checkpoints2", epoch, file_name),
                np.uint8(out_vis_image))
            cv2.imwrite(
                "%s/%04d/%s_gt.png" % ("checkpoints2", epoch, file_name),
                np.uint8(gt))

        target.close()

        avg_score = np.mean(scores_list)
        class_avg_scores = np.mean(class_scores_list, axis=0)
def create_record(dataset_dir=args.dataset):
    train_input_names = []
    train_output_names = []
    train_output_weight_names = []
    val_input_names = []
    val_output_names = []
    test_input_names = []
    test_output_names = []
    for file in os.listdir(dataset_dir + "/train"):
        cwd = os.getcwd()
        train_input_names.append(cwd + "/" + dataset_dir + "/train/" + file)
    for file in os.listdir(dataset_dir + "/train_labels"):
        cwd = os.getcwd()
        train_output_names.append(cwd + "/" + dataset_dir + "/train_labels/" +
                                  file)
    for file in os.listdir(dataset_dir + "/val"):
        cwd = os.getcwd()
        val_input_names.append(cwd + "/" + dataset_dir + "/val/" + file)
    for file in os.listdir(dataset_dir + "/val_labels"):
        cwd = os.getcwd()
        val_output_names.append(cwd + "/" + dataset_dir + "/val_labels/" +
                                file)
    for file in os.listdir(dataset_dir + "/test"):
        cwd = os.getcwd()
        test_input_names.append(cwd + "/" + dataset_dir + "/test/" + file)
    for file in os.listdir(dataset_dir + "/test_labels"):
        cwd = os.getcwd()
        test_output_names.append(cwd + "/" + dataset_dir + "/test_labels/" +
                                 file)

    writer = tf.python_io.TFRecordWriter("train.tfrecords")

    for t_ind in range(len(train_input_names)):
        img = cv2.imread(train_input_names[t_ind], -1)
        label = cv2.imread(train_output_names[t_ind], -1)
        label = helpers.one_hot_it(label=label, num_classes=2).astype('uint8')
        # print(img.shape,img.dtype,label.shape,label.dtype)
        img_raw = img.tobytes()  #将图片转化为原生bytes
        label_raw = label.tobytes()

        example = tf.train.Example(features=tf.train.Features(
            feature={
                "label_raw":
                tf.train.Feature(bytes_list=tf.train.BytesList(
                    value=[label_raw])),
                'img_raw':
                tf.train.Feature(bytes_list=tf.train.BytesList(
                    value=[img_raw]))
            }))
        writer.write(example.SerializeToString())
    writer.close()

    writer = tf.python_io.TFRecordWriter("valid.tfrecords")

    for t_ind in range(len(val_input_names)):
        img = cv2.imread(val_input_names[t_ind], -1)
        label = cv2.imread(val_output_names[t_ind], -1)
        label = helpers.one_hot_it(label=label, num_classes=2).astype('uint8')
        # print(img.shape,img.dtype,label.shape,label.dtype)
        img_raw = img.tobytes()  #将图片转化为原生bytes
        label_raw = label.tobytes()

        example = tf.train.Example(features=tf.train.Features(
            feature={
                "label_raw":
                tf.train.Feature(bytes_list=tf.train.BytesList(
                    value=[label_raw])),
                'img_raw':
                tf.train.Feature(bytes_list=tf.train.BytesList(
                    value=[img_raw]))
            }))
        writer.write(example.SerializeToString())
    writer.close()
            # Collect a batch of images
            for j in range(args.batch_size):
                index = i * args.batch_size + j
                id = id_list[index]
                input_image = load_image(train_input_names[id])
                output_image = load_image(train_output_names[id])

                with tf.device('/cpu:0'):
                    input_image, output_image = data_augmentation(
                        input_image, output_image)

                    # 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,
                                           class_dict=class_dict))

                    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()
Exemple #7
0
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)))
Exemple #8
0
        # st=time.time()
        input_image_batch = []
        output_image_batch = []

        # Collect a batch of images
        for j in range(batch_size):
            index = i * batch_size + j
            id = id_list[index]
            input_image = load_image(train_input_names[id])
            output_image = load_image(train_output_names[id])
            # input_image, output_image = data_augmentation(input_image, output_image)

            # 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(output_image, depth=classes_num, on_value=1))

            input_image_batch.append(np.expand_dims(input_image, axis=0))
            output_image_batch.append(np.expand_dims(output_image, axis=0))

        # 一个batch的图片(label)需要堆叠成 NWHC 形式
        if 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))

        # Do the training
        _, cur_loss = sess.run([opt, loss], feed_dict={
                input: input_image_batch,
    ind = random.randint(0, len(val_input_names) - 1)
    val_indices.append(ind)
id_list = np.random.permutation(len(train_input_names))

##############
# 8 start training
##############
prev_measure = 1
measure_history = deque([0]*lr_update_step, lr_update_step)
for step in trange(start_step, max_step):
    id = id_list[step%len(train_input_names)]
    input_image = np.expand_dims(cv2.cvtColor(cv2.imread(train_input_names[id],-1), cv2.COLOR_BGR2RGB),0) / 255.0
    # gt_map = np.expand_dims(np.expand_dims(cv2.imread(train_output_names[id],-1),0),3)
    gt_map = cv2.imread(train_output_names[id],-1)
    print('gt_map.shape:',gt_map.shape, np.unique(gt_map))
    gt_map = np.expand_dims(helpers.one_hot_it(gt_map,num_classes=6),0)
    #gt_map /= np.max(gt_map)
    input_image = np.transpose(input_image, [0, 3, 1, 2])
    gt_map = np.transpose(gt_map, [0, 3, 1, 2])
    fetch_dict = {
        "k_update": k_update,
        "measure": measure,
    }
    if step % log_step == 0:
        fetch_dict.update({
            "summary": summary_op,
            "g_loss": g_loss,
            "d_loss": d_loss,
            "k_t": k_t,
        })
  
Exemple #10
0
iou_list = []
run_times_list = []

# Run testing on ALL test images
for ind in range(len(test_input_names)):
    sys.stdout.write("\rRunning test image %d / %d" %
                     (ind + 1, len(test_input_names)))
    sys.stdout.flush()

    input_image = np.expand_dims(np.float32(
        utils.load_image(
            test_input_names[ind])[:args.crop_height, :args.crop_width]),
                                 axis=0) / 255.0
    gt = utils.load_image(
        test_output_names[ind])[:args.crop_height, :args.crop_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(test_input_names[ind])
val_labels = []

for img_name in train_input_names:
    input_image = util.load_image(img_name)
    with tf.device('/cpu:0'):
        input_image = np.float32(input_image) / 255.0

        #input_data.append(np.expand_dims(input_image, axis=0))
        input_data.append(input_image)
        print(img_name)

for labels_name in train_output_names:
    output_image = util.load_image(labels_name, is_data=False)
    with tf.device('/cpu:0'):
        output_image = np.float32(
            helpers.one_hot_it(label=output_image, label_values=label_values))

        #output_labels.append(np.expand_dims(output_image, axis=0))
        output_labels.append(output_image)
        print(labels_name)

for img_name in val_input_names:
    input_image = util.load_image(img_name)
    with tf.device('/cpu:0'):
        input_image = np.float32(input_image) / 255.0

        #input_data.append(np.expand_dims(input_image, axis=0))
        val_data.append(input_image)
        print(img_name)

for labels_name in val_output_names:
            output_image_batch = [] 

            # Collect a batch of images
            for j in range(args.batch_size):
                index = i*args.batch_size + j
                id = id_list[index]
                input_image = load_image(train_input_names[id])
                output_image = load_image(train_output_names[id])

                with tf.device('/cpu:0'):
                    input_image, output_image = data_augmentation(input_image, output_image)


                    # 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()
            
            if args.batch_size == 1:
                input_image_batch = input_image_batch[0]
Exemple #13
0
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)
Exemple #14
0
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')
Exemple #15
0
############ DeepLab v3 ##########################

class_names_list, label_values = helpers.get_label_info(os.path.join("SRF_PED", "class_dict.csv"))
DeepLab_DIR = "deeplab_test_Val"

dice = []
iou = []
images = sorted(os.listdir(DeepLab_DIR))
for i in range(0, len(images)-1,2):
	gt_image = threshold(cv2.imread(os.path.join(DeepLab_DIR, images[i])))
	pred_image = threshold(cv2.imread(os.path.join(DeepLab_DIR, images[i+1])))
	
	# print(helpers.one_hot_it(pred_image, label_values).shape)

	gt = helpers.reverse_one_hot(helpers.one_hot_it(gt_image, label_values))
	pred = helpers.reverse_one_hot(helpers.one_hot_it(pred_image, label_values)) 
	# print np.unique(pred)
	#_, d = compute_dice_coeff(pred, gt)
	#_, i = compute_mean_iou(pred, gt)

	_, i = mean_IU(pred, gt)
	_, d = mean_Dice(pred, gt)

	dice.append(d)
	iou.append(i)


dice = np.array(dice)
iou = np.array(iou)