Пример #1
0
def _label_statistics(image_paths):
    '''

    Calculates label statistics (number of picked pixels for each class)

    Parameters
    ----------
    image_paths : list
        List of absolute paths for picked images

    Returns
    -------
    array: numpy array
        Number of selected pixels per class


    '''
    ds = KittiDataset()

    def _rgb_2_label(rgb):
        return ds.color2label[tuple(rgb)].trainId

    total_counts = np.zeros(ds.num_classes())
    for img in image_paths:
        rgb = skimage.data.load(img)
        labels = np.apply_along_axis(_rgb_2_label, 2, rgb)
        indices, counts = np.unique(labels, return_counts=True)
        if indices[-1] >= ds.num_classes():
            indices = indices[0:-1]
            counts = counts[0:-1]
        total_counts[indices] += counts
    return total_counts
def _label_statistics(image_paths):
    '''

    Calculates label statistics (number of picked pixels for each class)

    Parameters
    ----------
    image_paths : list
        List of absolute paths for picked images

    Returns
    -------
    array: numpy array
        Number of selected pixels per class


    '''
    ds = KittiDataset()

    def _rgb_2_label(rgb):
        return ds.color2label[tuple(rgb)].trainId

    total_counts = np.zeros(ds.num_classes())
    for img in image_paths:
        rgb = skimage.data.load(img)
        labels = np.apply_along_axis(_rgb_2_label, 2, rgb)
        indices, counts = np.unique(labels, return_counts=True)
        if indices[-1] >= ds.num_classes():
            indices = indices[0:-1]
            counts = counts[0:-1]
        total_counts[indices] += counts
    return total_counts
Пример #3
0
def main(argv=None):
    '''

    Trains neural network for efficient piecewise crf training

    '''
    print('Results dir: {}'.format(FLAGS.train_dir))
    print('Train dataset dir: {}'.format(FLAGS.train_records_dir))
    print('Validation dataset dir: {}'.format(FLAGS.val_records_dir))

    possible_datasets = ['cityscapes', 'kitti']
    parser = argparse.ArgumentParser(description='Trains neural network')
    parser.add_argument('dataset_name', type=str, choices=possible_datasets,
                        help='Name of the dataset used for training')
    parser.add_argument('--resume', dest='resume', type=str, help='Path to the partially trained model')

    args = parser.parse_args()

    if args.dataset_name == possible_datasets[0]:
        from piecewisecrf.datasets.cityscapes.cityscapes import CityscapesDataset
        dataset = CityscapesDataset(train_dir=FLAGS.train_records_dir,
                                    val_dir=FLAGS.val_records_dir)
    elif args.dataset_name == possible_datasets[1]:
        from piecewisecrf.datasets.kitti.kitti import KittiDataset
        dataset = KittiDataset(train_dir=FLAGS.train_records_dir,
                               val_dir=FLAGS.val_records_dir)

    resume_path = None
    if args.resume and os.path.exists(args.resume):
        resume_path = args.resume

    train(dataset, resume_path)
Пример #4
0
                                     "This is done separately for easier remote evaluation "
                                     "(in case of redirecting output to file) - synchronized print")
    parser.add_argument('input_dir', type=str, help="Path to the temp_dir from grid_search")
    parser.add_argument('labels_dir', type=str, help="Path to labels directory")
    possible_datasets = ['cityscapes', 'kitti']
    parser.add_argument('dataset_name', type=str, choices=possible_datasets,
                        help='Name of the dataset used for evaluation')

    args = parser.parse_args()

    if args.dataset_name == possible_datasets[0]:
        from piecewisecrf.datasets.cityscapes.cityscapes import CityscapesDataset
        dataset = CityscapesDataset()
    elif args.dataset_name == possible_datasets[1]:
        from piecewisecrf.datasets.kitti.kitti import KittiDataset
        dataset = KittiDataset()

    with open('evaluation.txt', "w") as ff:
        best_iou = 0
        best_dir = 0
        for directory in os.listdir(args.input_dir):
            if len(os.listdir(os.path.join(args.input_dir, directory))) == 0:
                continue
            ff.write(directory + "\n")
            print(directory)
            r = calculate_accuracy_t.evaluate_segmentation(os.path.join(args.input_dir,
                                                                        directory), args.labels_dir, dataset)
            ff.write("#{} => {} - {}\n".format(directory, r[5], r[3]))
            print("#{} => {} - {}\n".format(directory, r[5], r[3]))
            if r[5] > best_iou:
                best_iou = r[5]
Пример #5
0
def main(argv=None):
    '''

    Creates output files for the given dataset

    '''
    possible_datasets = ['cityscapes', 'kitti']
    parser = argparse.ArgumentParser(
        description='Evaluates trained model on given dataset')
    parser.add_argument('dataset_name',
                        type=str,
                        choices=possible_datasets,
                        help='Name of the dataset used for training')
    parser.add_argument('dataset_partition',
                        type=str,
                        choices=['train', 'validation', 'test'],
                        help='Dataset partition which will be evaluated')
    parser.add_argument(
        'checkpoint_dir',
        type=str,
        help='Path to the directory containing the trained model')
    parser.add_argument('output_dir', type=str, help='Output directory')

    args = parser.parse_args()

    if args.dataset_name == possible_datasets[0]:
        from piecewisecrf.datasets.cityscapes.cityscapes import CityscapesDataset
        dataset = CityscapesDataset(train_dir=FLAGS.train_records_dir,
                                    val_dir=FLAGS.val_records_dir,
                                    test_dir=FLAGS.test_records_dir)
    elif args.dataset_name == possible_datasets[1]:
        from piecewisecrf.datasets.kitti.kitti import KittiDataset
        dataset = KittiDataset(train_dir=FLAGS.train_records_dir,
                               val_dir=FLAGS.val_records_dir,
                               test_dir=FLAGS.test_records_dir)

    if not os.path.exists(args.checkpoint_dir):
        print('{} was not found'.format(args.checkpoint_dir))
        exit(1)

    (potentials_out_small, potentials_out_orig, prediction_out_small,
     prediction_out, prediction_ppm_out_small,
     prediction_ppm_out) = _create_dirs(args.output_dir)

    ckpt = tf.train.get_checkpoint_state(args.checkpoint_dir)
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                            log_device_placement=False))
    if ckpt and ckpt.model_checkpoint_path:
        with tf.variable_scope('model'):
            (image, labels_unary, labels_orig, labels_bin_sur,
             labels_bin_above_below, img_name, weights, weights_surr,
             weights_ab) = reader.inputs(
                 dataset,
                 shuffle=False,
                 dataset_partition=args.dataset_partition)
            unary_log, pairwise_log, pairwise_ab_log = model.inference(
                image, FLAGS.batch_size, is_training=False)

        saver = tf.train.Saver()
        print('Loading model: {}'.format(ckpt.model_checkpoint_path))
        saver.restore(sess, ckpt.model_checkpoint_path)
    else:
        print('No checkpoint file found')
        raise ValueError()

    tf.train.start_queue_runners(sess=sess)
    print(dataset.num_examples(args.dataset_partition) // FLAGS.batch_size)
    for i in trange(
            dataset.num_examples(args.dataset_partition) // FLAGS.batch_size):
        logits_unary, logits_pairwise, logits_pairwise_ab, yt, names = sess.run(
            [unary_log, pairwise_log, pairwise_ab_log, labels_unary, img_name])

        for batch in range(FLAGS.batch_size):
            print(names[batch])
            names[batch] = names[batch].decode("utf-8")
            s = mean_field.mean_field(
                logits_unary[batch, :, :, :],
                [(logits_pairwise[batch, :, :, :],
                  list(
                      zip(indices.FIRST_INDICES_SURR,
                          indices.SECOND_INDICES_SURR)),
                  indices.generate_encoding_decoding_dict(
                      logits_unary.shape[3])[1]),
                 (logits_pairwise_ab[batch, :, :, :],
                  list(zip(indices.FIRST_INDICES_AB,
                           indices.SECOND_INDICES_AB)),
                  indices.generate_encoding_decoding_dict(
                      logits_unary.shape[3])[1])])
            s = mean_field._exp_norm(s)
            y = s.argmax(2)
            y = y.astype(np.int16)
            io.dump_nparray(
                y,
                os.path.join(prediction_out_small,
                             "{}.bin".format(names[batch])))
            y_orig = skimage.transform.resize(
                y, (FLAGS.img_height, FLAGS.img_width),
                order=0,
                preserve_range=True)
            y_orig = y_orig.astype(np.int16)
            io.dump_nparray(
                y_orig,
                os.path.join(prediction_out, "{}.bin".format(names[batch])))

            yimg = np.empty((y.shape[0], y.shape[1], 3), dtype=np.uint8)
            for i in range(y.shape[0]):
                for j in range(y.shape[1]):
                    yimg[i, j, :] = dataset.trainId2label[y[i, j]].color

            skimage.io.imsave(
                os.path.join(prediction_ppm_out_small,
                             "{}.ppm".format(names[batch])), yimg)
            yimg_orig = skimage.transform.resize(
                yimg, (FLAGS.img_height, FLAGS.img_width),
                order=0,
                preserve_range=True)
            yimg_orig = yimg_orig.astype(np.int16)
            skimage.io.imsave(
                os.path.join(prediction_ppm_out,
                             "{}.ppm".format(names[batch])), yimg_orig)

            ret = -1.0 * np.log(s)
            ret = ret.astype(np.float32)
            io.dump_nparray(
                ret,
                os.path.join(potentials_out_small,
                             "{}.bin".format(names[batch])))

            # ret = np.repeat(np.repeat(s, FLAGS.subsample_factor, axis=0), FLAGS.subsample_factor, axis=1)
            ret = scipy.ndimage.zoom(s, [16, 16, 1], order=1)
            ret = -1. * np.log(ret)
            ret = ret.astype(np.float32)
            io.dump_nparray(
                ret,
                os.path.join(potentials_out_orig,
                             "{}.bin".format(names[batch])))
Пример #6
0
def main(argv=None):
    '''
    Evaluates the trained model on the specified dataset
    '''
    possible_datasets = ['cityscapes', 'kitti']
    parser = argparse.ArgumentParser(description='Evaluates trained model on given dataset')
    parser.add_argument('dataset_name', type=str, choices=possible_datasets,
                        help='Name of the dataset used for training')
    parser.add_argument('dataset_partition', type=str, choices=['train', 'validation', 'test'],
                        help='Dataset partition which will be evaluated')
    parser.add_argument('checkpoint_dir', type=str,
                        help='Path to the directory containing the trained model')
    parser.add_argument('--calculate_energy', help='Detailed output',
                        dest='calculate_energy', action='store_true')

    args = parser.parse_args()

    if args.dataset_name == possible_datasets[0]:
        from piecewisecrf.datasets.cityscapes.cityscapes import CityscapesDataset
        dataset = CityscapesDataset(train_dir=FLAGS.train_records_dir,
                                    val_dir=FLAGS.val_records_dir,
                                    test_dir=FLAGS.test_records_dir)
    elif args.dataset_name == possible_datasets[1]:
        from piecewisecrf.datasets.kitti.kitti import KittiDataset
        dataset = KittiDataset(train_dir=FLAGS.train_records_dir,
                               val_dir=FLAGS.val_records_dir,
                               test_dir=FLAGS.test_records_dir)

    if not os.path.exists(args.checkpoint_dir):
        print('{} was not found'.format(args.checkpoint_dir))
        exit(1)

    ckpt = tf.train.get_checkpoint_state(args.checkpoint_dir)
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False))
    if ckpt and ckpt.model_checkpoint_path:
        with tf.variable_scope('model'):
            (image, labels_unary, lo, labels_bin_sur,
                labels_bin_above_below, img_name, weights,
                weights_surr, weights_ab) = reader.inputs(dataset,
                                                          shuffle=False,
                                                          dataset_partition=args.dataset_partition)
            unary_log, pairwise_log, pairwise_ab_log = model.inference(image, FLAGS.batch_size, is_training=False)

        saver = tf.train.Saver()
        print('Loading model: {}'.format(ckpt.model_checkpoint_path))
        saver.restore(sess, ckpt.model_checkpoint_path)
    else:
        print('No checkpoint file found')
        raise ValueError()

    conf_mat = np.zeros((FLAGS.num_classes, FLAGS.num_classes), dtype=np.uint64)
    conf_mat_without_mf = np.zeros((FLAGS.num_classes, FLAGS.num_classes), dtype=np.uint64)
    tf.train.start_queue_runners(sess=sess)
    for i in trange(dataset.num_examples(args.dataset_partition) // FLAGS.batch_size):
        logits_unary, logits_pairwise, logits_pairwise_ab, yt, names = sess.run([unary_log, pairwise_log,
                                                                                 pairwise_ab_log, lo, img_name])

        for batch in range(FLAGS.batch_size):
            s = mean_field.mean_field(logits_unary[batch, :, :, :],
                                      [(logits_pairwise[batch, :, :, :],
                                       list(zip(indices.FIRST_INDICES_SURR, indices.SECOND_INDICES_SURR)),
                                       indices.generate_encoding_decoding_dict(logits_unary.shape[3])[1]
                                        ),
                                      (logits_pairwise_ab[batch, :, :, :],
                                       list(zip(indices.FIRST_INDICES_AB, indices.SECOND_INDICES_AB)),
                                       indices.generate_encoding_decoding_dict(logits_unary.shape[3])[1]
                                       )],
                                      calculate_energy=args.calculate_energy)
            s = scipy.ndimage.zoom(s, [16, 16, 1], order=1)
            logits_unary = scipy.ndimage.zoom(logits_unary, [1, 16, 16, 1], order=1)
            yk = logits_unary[batch].argmax(2)
            y = s.argmax(2)
            eval_helper.confusion_matrix(y.reshape(-1), yt[batch], conf_mat, dataset.num_classes())
            eval_helper.confusion_matrix(yk.reshape(-1), yt[batch], conf_mat_without_mf, dataset.num_classes())

            print(names[batch])

            eval_helper.compute_errors(conf_mat, args.dataset_partition, dataset.trainId2label)
            eval_helper.compute_errors(conf_mat_without_mf, '{} without mf'.format(args.dataset_partition),
                                       dataset.trainId2label)
    eval_helper.compute_errors(conf_mat, args.dataset_partition, dataset.trainId2label)
Пример #7
0
def main(dataset_dir, output_dir, subset, resize, onlyresize):
    '''

    Prepares all the necessary data for the semantic segmentation pipeline

    Parameters
    ----------
    dataset_dir : str
        Path to the dataset directory

    output_dir: str
        Path to the output directory directory

    subset: str
        Dataset subset (train, train_val, train_train, valid)

    resize: list
        [width, height] for the resized images

    onlyresize: bool
        Whether only resized images will be created


    '''
    print("Creating directories")
    subset_dir = os.path.join(dataset_dir, subset)
    image_input_dir = os.path.join(subset_dir, 'data')
    image_input_dir = os.path.join(image_input_dir, 'rgb')
    label_input_dir = os.path.join(subset_dir, 'labels')
    output_dir = _create_folder(output_dir, subset)

    # create folder hierarchy for original size images
    if not onlyresize:
        (label_dest_dir, image_dest_dir, image_dest_ppm_dir,
         label_dest_ppm_dir,
         label_dest_bin_dir) = _create_folders(output_dir, 'original')

    # create folder hierarchy for the resized images
    if resize:
        (label_dest_dir_resized, image_dest_dir_resized,
         image_dest_ppm_dir_resized, label_dest_ppm_dir_resized,
         label_dest_bin_dir_resized) = _create_folders(
             output_dir, '{}x{}'.format(resize[0], resize[1]))
    print("Finished creating directories")

    print("Creating image files")
    for file_name in tqdm.tqdm(os.listdir(image_input_dir)):
        img = skimage.data.load(os.path.join(image_input_dir, file_name))
        file_prefix = file_name[0:file_name.index('.')]

        if not onlyresize:
            skimage.io.imsave(
                os.path.join(image_dest_dir, '{}.png'.format(file_prefix)),
                img)
            skimage.io.imsave(
                os.path.join(image_dest_ppm_dir, '{}.ppm'.format(file_prefix)),
                img)

        if resize:
            img = skimage.transform.resize(img, (resize[1], resize[0]),
                                           order=3)
            skimage.io.imsave(
                os.path.join(image_dest_dir_resized,
                             '{}.png'.format(file_prefix)), img)
            skimage.io.imsave(
                os.path.join(image_dest_ppm_dir_resized,
                             '{}.ppm'.format(file_prefix)), img)

    print("Creating label files")
    dataset = KittiDataset()
    for file_name in tqdm.tqdm(os.listdir(label_input_dir)):
        img = skimage.data.load(os.path.join(label_input_dir, file_name))
        file_prefix = file_name[0:file_name.index('.')]

        if not onlyresize:
            skimage.io.imsave(
                os.path.join(label_dest_dir, '{}.png'.format(file_prefix)),
                img)
            skimage.io.imsave(
                os.path.join(label_dest_ppm_dir, '{}.ppm'.format(file_prefix)),
                img)

            binary_image = np.apply_along_axis(
                lambda a: dataset.color2label[(a[0], a[1], a[2])].trainId, 2,
                img)
            binary_image = binary_image.astype(np.uint8)
            io.dump_nparray(
                binary_image,
                os.path.join(label_dest_ppm_dir, '{}.bin'.format(file_prefix)))

        if resize:
            img = skimage.transform.resize(img, (resize[1], resize[0]),
                                           order=0,
                                           preserve_range=True)
            img = img.astype(np.uint8)

            skimage.io.imsave(
                os.path.join(label_dest_dir_resized,
                             '{}.png'.format(file_prefix)), img)
            skimage.io.imsave(
                os.path.join(label_dest_ppm_dir_resized,
                             '{}.ppm'.format(file_prefix)), img)

            binary_image = np.apply_along_axis(
                lambda a: dataset.color2label[(a[0], a[1], a[2])].trainId, 2,
                img)
            binary_image = binary_image.astype(np.uint8)
            io.dump_nparray(
                binary_image,
                os.path.join(label_dest_bin_dir_resized,
                             '{}.bin'.format(file_prefix)))
def main(dataset_dir, size):
    '''

    Splits the train dataset into train and validation subsets

    Parameters
    ----------
    dataset_dir : str
        Path to the dataset folder (containing train and val subsets)

    size: int
        Wanted size of the validation subset


    '''
    label_input_dir = os.path.join(dataset_dir, 'train')
    label_input_dir = os.path.join(label_input_dir, 'labels')

    img_input_dir = os.path.join(dataset_dir, 'train')
    img_input_dir = os.path.join(img_input_dir, 'data')
    img_input_dir = os.path.join(img_input_dir, 'rgb')

    picked_all_labels = False
    while not picked_all_labels:
        picked_images = _pick_images(label_input_dir, size)
        stats = _label_statistics(
            [os.path.join(label_input_dir, x) for x in picked_images])
        print("Pixels per class: {}".format(stats))
        if np.count_nonzero(stats) == KittiDataset().num_classes():
            picked_all_labels = True

    print("Processing images")
    print("Creating train_train and train_val folders")
    train_path = os.path.join(dataset_dir, 'train_train')
    val_path = os.path.join(dataset_dir, 'train_val')
    if not os.path.exists(train_path):
        os.makedirs(train_path)
    if not os.path.exists(val_path):
        os.makedirs(val_path)
    train_data_path = os.path.join(train_path, 'data')
    if not os.path.exists(train_data_path):
        os.makedirs(train_data_path)
    train_data_path = os.path.join(train_data_path, 'rgb')
    if not os.path.exists(train_data_path):
        os.makedirs(train_data_path)
    train_label_path = os.path.join(train_path, 'labels')
    if not os.path.exists(train_label_path):
        os.makedirs(train_label_path)
    val_data_path = os.path.join(val_path, 'data')
    if not os.path.exists(val_data_path):
        os.makedirs(val_data_path)
    val_data_path = os.path.join(val_data_path, 'rgb')
    if not os.path.exists(val_data_path):
        os.makedirs(val_data_path)
    val_label_path = os.path.join(val_path, 'labels')
    if not os.path.exists(val_label_path):
        os.makedirs(val_label_path)
    print("Finished creating folders")

    print("Moving image and label files")
    picked_images = set(picked_images)
    for img in tqdm.tqdm(os.listdir(img_input_dir)):
        if img in picked_images:
            shutil.copy(os.path.join(img_input_dir, img),
                        os.path.join(val_data_path, img))
            shutil.copy(os.path.join(label_input_dir, img),
                        os.path.join(val_label_path, img))
        else:
            shutil.copy(os.path.join(img_input_dir, img),
                        os.path.join(train_data_path, img))
            shutil.copy(os.path.join(label_input_dir, img),
                        os.path.join(train_label_path, img))