Ejemplo n.º 1
0
def lovasz_softmax(probas,
                   labels,
                   only_present=True,
                   per_image=False,
                   ignore=None,
                   order='BHWC'):
    """
    Multi-class Lovasz-Softmax loss
      probas: [B, H, W, C] or [B, C, H, W] Variable, class probabilities at each prediction (between 0 and 1)
      labels: [B, H, W] Tensor, ground truth labels (between 0 and C - 1)
      only_present: average only on classes present in ground truth
      per_image: compute the loss per image instead of per batch
      ignore: void class labels
      order: use BHWC or BCHW
    """
    probas = tf.nn.softmax(probas, 3)
    labels = helpers.reverse_one_hot(labels)

    if per_image:

        def treat_image(prob, lab):
            prob, lab = tf.expand_dims(prob, 0), tf.expand_dims(lab, 0)
            prob, lab = _flatten_probas(prob, lab, ignore, order)
            return _lovasz_softmax_flat(prob, lab, only_present=only_present)

        losses = tf.map_fn(treat_image, (probas, labels), dtype=tf.float32)
    else:
        losses = _lovasz_softmax_flat(*_flatten_probas(probas, labels, ignore,
                                                       order),
                                      only_present=only_present)
    return losses
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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!")
Ejemplo n.º 5
0
def calculate_validation_metrics(input_image_batch, gt_image_batch,
                                 num_classes):
    accuracy_list = []
    class_accuracies_list = []
    prec_list = []
    rec_list = []
    f1_list = []
    iou_list = []
    for index in range(input_image_batch.shape[0]):
        output_image = np.array(input_image_batch[index, :, :, :])
        output_image = helpers.reverse_one_hot(output_image)
        gt_image_acc = np.array(gt_image_batch[index, :, :])
        accuracy, class_accuracies, prec, rec, f1, iou = evaluate_validation_accuracy(
            pred=output_image, label=gt_image_acc, num_classes=num_classes)
        accuracy_list.append(accuracy)
        class_accuracies_list.append(class_accuracies)
        prec_list.append(prec)
        rec_list.append(rec)
        f1_list.append(f1)
        iou_list.append(iou)
    return np.mean(accuracy_list, dtype=np.float32),np.mean(class_accuracies_list,dtype=np.float32),\
    np.mean(prec_list, dtype=np.float32),np.mean(rec_list, dtype=np.float32),np.mean(f1_list, dtype=np.float32),\
    np.mean(iou_list, dtype=np.float32)
Ejemplo n.º 6
0
                            "{}".format(output_image))

            if WRITE_LOG:
                # Create the Timeline object, and write it to a json file
                fetched_timeline = timeline.Timeline(run_metadata.step_stats)
                chrome_trace = fetched_timeline.generate_chrome_trace_format()
                with open('./checkpoints/timeline_180830.json', 'w') as t:
                    t.write(chrome_trace)

            output_image = np.array(output_image[0, :, :, :])

            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()
Ejemplo n.º 7
0
            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 = 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, 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])
Ejemplo n.º 8
0
        # Do the validation on a small set of validation images
        for ind in val_indices:

            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:
        for id in val_indices:
            input_image = np.expand_dims(
                cv2.cvtColor(cv2.imread(val_input_names[id], -1),
                             cv2.COLOR_BGR2RGB)[:256, :256, :], 0) / 255.0
            gt_map = np.expand_dims(
                cv2.imread(val_output_names[id], -1)[:256, :256], 2)
            gt_map /= np.max(gt_map)
            input_image = np.transpose(input_image, [0, 3, 1, 2])

            output_image = sess.run(G, {z: input_image})
            output_image = np.clip(output_image[0].transpose([1, 2, 0]), 0, 1)
            st = time.time()

            print('output_image.shape, gt.shape:', output_image.shape,
                  gt_map.shape)
            output_image = helpers.reverse_one_hot(output_image)
            out_vis_image = helpers.colour_code_segmentation(output_image)
            accuracy = utils_seg.compute_avg_accuracy(output_image, gt_map)
            class_accuracies = utils_seg.compute_class_accuracies(
                output_image, gt_map, 2)
            prec = utils_seg.precision(output_image, gt_map)
            rec = utils_seg.recall(output_image, gt_map)
            f1 = utils_seg.f1score(output_image, gt_map)
            iou = utils_seg.compute_mean_iou(output_image, gt_map)

            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")
Ejemplo n.º 10
0
        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 = 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" %
Ejemplo n.º 11
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)))
Ejemplo n.º 12
0
import cv2
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))
Ejemplo n.º 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)
Ejemplo n.º 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')
Ejemplo n.º 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)