Example #1
0
                    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()

            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))
Example #2
0
            flags=cv2.INTER_NEAREST)
        output_image = cv2.warpAffine(
            output_image,
            M, (output_image.shape[1], output_image.shape[0]),
            flags=cv2.INTER_NEAREST)

    return input_image, output_image


def download_checkpoints(model_name):
    subprocess.check_output(
        ["python", "get_pretrained_checkpoints.py", "--model=" + model_name])


# Get the names of the classes so we can record the evaluation results
class_names_list, label_values = helpers.get_label_info(
    os.path.join(args.dataset, "class_dict.csv"))
class_names_string = ""
for class_name in class_names_list:
    if not class_name == class_names_list[-1]:
        class_names_string = class_names_string + class_name + ", "
    else:
        class_names_string = class_names_string + class_name

num_classes = len(label_values)

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

# Get the selected model.
# Some of them require pre-trained ResNet
Example #3
0
import cv2
import os
import numpy as np
import helpers
import csv

if __name__ == "__main__":
    dataset_path = "../autovision_dataset/"
    dirs = ["train_labels"]
    # dirs = ["test_labels", "train_labels", "val_labels"]
    class_names_list, label_values = helpers.get_label_info(
        os.path.join(dataset_path, "class_dict.csv"))
    # ds_classes = dict(zip(label_values, class_names_list))
    per_class_coverage = [0] * class_names_list.__len__()
    os.listdir(dataset_path)
    for dir in dirs:
        labels_path = os.path.join(dataset_path, dir)
        imgs_list = os.listdir(labels_path)
        for num_imgs, img_path in enumerate(imgs_list):
            label_img = cv2.imread(os.path.join(dataset_path, dir, img_path))
            all_px = label_img.size / 3
            #            label_img = cv2.cvtColor(label_img, cv2.COLOR_BGR2RGB)
            for num, value in enumerate(label_values):
                pixel_counter = np.count_nonzero(
                    np.all(label_img == value, axis=2))
                percentage_of_color = pixel_counter / all_px * 100
                per_class_coverage[
                    num] += percentage_of_color / imgs_list.__len__()
                # print("percentage of class %s is %f" % (class_names_list[num], percentage_of_color))

        split_dict = dict(zip(class_names_list, per_class_coverage))
Example #4
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)))
Example #5
0
dataset_basepath = Path("/media/jetson/Samsung500GB/SpaceNet/")
train_images = dataset_basepath / 'train'
train_masks = dataset_basepath / 'train_labels'
val_images = dataset_basepath / 'val'
val_masks = dataset_basepath / 'val_labels'
class_dict = dataset_basepath / 'class_dict.csv'

input_shape = (650, 650, 3)
# dense prediction tasks recommend multiples of 32 +1
random_crop = (256, 256, 3)
#random_crop = (638, 638, 3)
batch_size = 1
epochs = 100
validation_images = 10

class_labels, class_colors, num_classes = get_label_info(dataset_basepath /
                                                         "class_dict.csv")

myValGen = customGenerator(batch_size,
                           val_images,
                           val_masks,
                           num_classes,
                           input_shape,
                           dict(),
                           class_colors,
                           random_crop=random_crop)


def weighted_categorical_crossentropy(weights):
    def wcce(y_true, y_pred):
        Kweights = tf.constant(weights)
        if not tf.is_tensor(y_pred):
Example #6
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 __name__ == '__main__':
    # Load the data
    print("Loading the data ...")
    class_names_list, label_values = helpers.get_label_info(
        os.path.join(cfg.data_dir, 'class_dict.csv'))
    class_names_string = ""
    for class_name in class_names_list:
        if not class_name == class_names_list[-1]:
            class_names_string = class_names_string + class_name + ", "
        else:
            class_names_string = class_names_string + class_name
    num_classes = len(label_values)
    train_input_names, train_output_names, val_input_names, val_output_names, test_input_names, test_output_names = dataset.prepare_data(
    )

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    # Compute your softmax cross entropy loss
Example #7
0
# SRF_i = iou[:,0]
# SRF_iou = [i for i in SRF_i if i>0]

# PED_i = iou[:,1]
# PED_iou = [i for i in PED_i if i>0]


# print np.mean(SRF_dice), np.mean(PED_dice)
# print np.mean(SRF_iou), np.mean(PED_iou)



############ 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)
Example #8
0
import utils
import helpers
import numpy as np
import os
from termcolor import colored
import model
import setting

config = setting.config

class_names_list, label_values = helpers.get_label_info()
num_classes = len(class_names_list)

losses = None
if config["training_setting"]["class_balancing"]:
    print("Class balancing set on for training")
    print("Calculating class weights---in progress")
    class_weights = utils.compute_class_weights(
        labels_dir=config["data"]["directory"] + "/train_labels",
        label_values=label_values)
    unweighted_loss = None
    if config["training_setting"]["loss"] == "cross_entropy":
        unweighted_loss = tf.nn.softmax_cross_entropy_with_logits(
            logits=network, labels=net_output)
    elif config["training_setting"]["loss"] == "lovasz":
        unweighted_loss = helpers.lovasz_softmax(probas=network,
                                                 labels=net_output)
    losses = unweighted_loss * class_weights
else:
    if config["training_setting"]["loss"] == "cross_entropy":
        losses = tf.nn.softmax_cross_entropy_with_logits(logits=network,