Example #1
0
    def test_608_608_patches_to_image_stride_cummulative(self):
        N_IMAGES = 10
        N_CHANNEL = 3
        IMAGE_WIDTH = IMAGE_HEIGHT = 608
        PATCH_SIZE = 128
        STRIDE = 16
        N_PATCHES = 10 * 31 * 31

        random_rgb_image = np.ones(
            (N_IMAGES, IMAGE_HEIGHT, IMAGE_WIDTH, N_CHANNEL))
        patches = images.extract_patches(random_rgb_image, PATCH_SIZE, STRIDE)

        patches = patches.reshape((N_IMAGES, int(N_PATCHES / N_IMAGES),
                                   PATCH_SIZE, PATCH_SIZE, N_CHANNEL))

        reconstructed_images = images.images_from_patches(patches,
                                                          stride=STRIDE,
                                                          normalize=False)

        num_images, image_height, image_width, n_channel = reconstructed_images.shape

        self.assertTrue(num_images == N_IMAGES)
        self.assertTrue(image_height == IMAGE_HEIGHT)
        self.assertTrue(image_width == IMAGE_WIDTH)
        self.assertTrue(n_channel == N_CHANNEL)

        reconstructed_images
        plt.imsave("bla.png", reconstructed_images[0])

        print(reconstructed_images[0, 50, 100, 0])
Example #2
0
    def test_400_400_image_to_patches_no_stride(self):
        N_IMAGES = 10
        N_CHANNEL = 3
        IMAGE_WIDTH = IMAGE_HEIGHT = 400
        PATCH_SIZE = 80
        STRIDE = 80
        N_PATCHES = 5 * 5 * 10

        random_rgb_image = np.random.randint(0,
                                             PIXEL_DEPTH,
                                             size=(N_IMAGES, IMAGE_HEIGHT,
                                                   IMAGE_WIDTH, N_CHANNEL))
        patches = images.extract_patches(random_rgb_image, PATCH_SIZE, STRIDE)
        patches = patches.reshape((N_IMAGES, int(N_PATCHES / N_IMAGES),
                                   PATCH_SIZE, PATCH_SIZE, N_CHANNEL))

        n_images, n_patches, p_height, p_width, channels = patches.shape

        assert n_images == 10
        assert n_patches == 25
        assert p_height == 80
        assert p_width == 80
        assert channels == 3

        reconstructed_images = images.images_from_patches(patches)
        num_images, image_height, image_width, n_channel = reconstructed_images.shape
        self.assertTrue(num_images == N_IMAGES)
        self.assertTrue(image_height == IMAGE_HEIGHT)
        self.assertTrue(image_width == IMAGE_WIDTH)
        self.assertTrue(n_channel == N_CHANNEL)
Example #3
0
    def test_608_608_patches_to_image_stride(self):
        N_IMAGES = 10
        N_CHANNEL = 3
        IMAGE_WIDTH = IMAGE_HEIGHT = 608
        PATCH_SIZE = 128
        STRIDE = 16
        N_PATCHES = 10 * 31 * 31

        random_rgb_image = np.random.randint(0,
                                             PIXEL_DEPTH,
                                             size=(N_IMAGES, IMAGE_HEIGHT,
                                                   IMAGE_WIDTH, N_CHANNEL))
        patches = images.extract_patches(random_rgb_image, PATCH_SIZE, STRIDE)

        patches = patches.reshape((N_IMAGES, int(N_PATCHES / N_IMAGES),
                                   PATCH_SIZE, PATCH_SIZE, N_CHANNEL))

        reconstructed_images = images.images_from_patches(patches,
                                                          stride=STRIDE)

        num_images, image_height, image_width, n_channel = reconstructed_images.shape

        self.assertTrue(num_images == N_IMAGES)
        self.assertTrue(image_height == IMAGE_HEIGHT)
        self.assertTrue(image_width == IMAGE_WIDTH)
        self.assertTrue(n_channel == N_CHANNEL)
Example #4
0
    def test_visual(self):
        import matplotlib.image as mpimg
        test_image = mpimg.imread(
            r".\..\data\training\images\satImage_001.png")
        images_ = np.empty((2, 400, 400, 3))
        images_[0] = test_image.copy()
        images_[1] = test_image.copy()
        patches = images.extract_patches(images_, 80, stride=16)
        patches = patches.reshape((2, 21 * 21, 80, 80, 3))
        reconstructed_images = images.images_from_patches(
            patches, border_majority_only=True, stride=16)

        plt.imsave("bla.png", reconstructed_images[0])
Example #5
0
    def test_608_608_image_to_patches_no_stride(self):
        N_IMAGES = 10
        N_CHANNEL = 3
        IMAGE_WIDTH = IMAGE_HEIGHT = 608
        PATCH_SIZE = 32

        random_rgb_image = np.random.randint(0,
                                             PIXEL_DEPTH,
                                             size=(N_IMAGES, IMAGE_HEIGHT,
                                                   IMAGE_WIDTH, N_CHANNEL))
        patches = images.extract_patches(random_rgb_image, PATCH_SIZE)

        n_patches, p_height, p_width, channels = patches.shape

        assert n_patches == 3610
        assert p_height == 32
        assert p_width == 32
        assert channels == 3
Example #6
0
    def test_608_608_image_to_patches_stride(self):
        N_IMAGES = 10
        N_CHANNEL = 3
        IMAGE_WIDTH = IMAGE_HEIGHT = 608
        PATCH_SIZE = 128
        STRIDE = 16
        N_PATCHES = 10 * 31 * 31

        random_rgb_image = np.random.randint(0,
                                             PIXEL_DEPTH,
                                             size=(N_IMAGES, IMAGE_HEIGHT,
                                                   IMAGE_WIDTH, N_CHANNEL))
        patches = images.extract_patches(random_rgb_image, PATCH_SIZE, STRIDE)
        patches = patches.reshape((N_IMAGES, int(N_PATCHES / N_IMAGES),
                                   PATCH_SIZE, PATCH_SIZE, N_CHANNEL))

        n_images, n_patches, p_height, p_width, channels = patches.shape

        assert n_images == 10
        assert n_patches == 31 * 31
        assert p_height == 128
        assert p_width == 128
        assert channels == 3
    def img_to_label_patches(self, img, patch_size=IMG_PATCH_SIZE):
        img = images.extract_patches(img, patch_size)
        img = images.labels_for_patches(img)
        img.resize((img.shape[0], patch_size, patch_size))

        return img
Example #8
0
def main(_):
    opts = Options()
    if opts.gpu == -1:
        config = tf.ConfigProto()
    else:
        config = tf.ConfigProto(device_count={'GPU': opts.num_gpu},
                                allow_soft_placement=True)

    print(opts.patch_size)
    with tf.Graph().as_default(), tf.Session(config=config) as session:
        device = '/device:CPU:0' if opts.gpu == -1 else '/device:GPU:{}'.format(
            opts.gpu)
        print("Running on device {}".format(device))
        with tf.device(device):
            model = ConvolutionalModel(opts, session)

        if opts.restore_model:
            if opts.model_path is not None:
                model.restore(file=opts.model_path)
                print("Restore model: {}".format(opts.model_path))
            else:
                print("Restore date: {}".format(opts.restore_date))
                model.restore(date=opts.restore_date, epoch=opts.restore_epoch)

        if opts.num_epoch > 0:
            train_images, train_groundtruth = images.load_train_data(
                opts.train_data_dir)
            print(len(train_images))
            input_size = unet.input_size_needed(opts.patch_size,
                                                opts.num_layers)
            offset = int((input_size - opts.patch_size) / 2)
            extended_images = images.expand_and_rotate(train_images,
                                                       opts.rotation_angles,
                                                       offset)
            patches = images.extract_patches(
                extended_images,
                patch_size=input_size,
                predict_patch_size=opts.patch_size,
                stride=opts.stride)

            print("Train on {} patches of size {}x{}".format(
                patches.shape[0], patches.shape[1], patches.shape[2]))

            train_groundtruth_exp = images.expand_and_rotate(
                train_groundtruth, opts.rotation_angles, 0)
            labels_patches = images.extract_patches(train_groundtruth_exp,
                                                    patch_size=opts.patch_size,
                                                    stride=opts.stride)

            print("Train on {} groundtruth patches of size {}x{}".format(
                labels_patches.shape[0], labels_patches.shape[1],
                labels_patches.shape[2]))

            model._summary.add_to_eval_patch_summary(train_groundtruth)
            for i in range(opts.num_epoch):
                print("==== Train epoch: {} ====".format(i))
                tf.local_variables_initializer().run()  # Reset scores

                # Drop last dimension if input image is not PNG-8
                if (len(labels_patches.shape) == 4):
                    labels_patches = labels_patches[:, :, :, 0]
                if (len(train_groundtruth.shape) == 4):
                    train_groundtruth = train_groundtruth[:, :, :, 0]

                model.train(patches, labels_patches, train_images,
                            train_groundtruth)  # Process one epoch
                model.save(i)  # Save model to disk

        if opts.eval_train:
            print("Evaluate Test")
            eval_images, eval_groundtruth = images.load_train_data(
                opts.train_data_dir)
            pred_masks = model.predict_batchwise(eval_images,
                                                 opts.pred_batch_size)
            pred_labels = ((pred_masks > 0.5) * 1).squeeze(-1)
            pred_overlays = images.overlays(eval_images, pred_masks, fade=0.5)
            overlapped = images.overlap_pred_true(pred_labels,
                                                  eval_groundtruth)
            error = images.overlapp_error(pred_labels, eval_groundtruth)

            images.save_all(pred_labels,
                            opts.eval_data_dir,
                            "eval_binary_pred_{:03d}.png",
                            greyscale=True)
            images.save_all(pred_masks,
                            opts.eval_data_dir,
                            "eval_probability_pred_{:03d}.png",
                            greyscale=True)
            images.save_all(pred_overlays, opts.eval_data_dir,
                            "eval_overlays_pred_{:03d}.png")
            images.save_all(overlapped, opts.eval_data_dir,
                            "eval_confusion_{:03d}.png")
            images.save_all(error,
                            opts.eval_data_dir,
                            "eval_orror_{:03d}.png",
                            greyscale=True)

        if opts.eval_data_dir and not opts.eval_train:
            print("Running inference on eval data {}".format(
                opts.eval_data_dir))
            eval_images = images.load(opts.eval_data_dir)
            start = time.time()
            masks = model.predict_batchwise(eval_images, opts.pred_batch_size)
            stop = time.time()
            print("Prediction time:{} mins".format((stop - start) / 60))
            masks = images.quantize_mask(masks,
                                         patch_size=IMG_PATCH_SIZE,
                                         threshold=FOREGROUND_THRESHOLD)
            overlays = images.overlays(eval_images, masks, fade=0.4)
            save_dir = os.path.abspath(
                os.path.join(opts.save_path, model.experiment_name))
            images.save_all(overlays, save_dir)
            images.save_submission_csv(masks, save_dir, IMG_PATCH_SIZE)

            # Save model used for prediction
            saved_path = model.saver.save(model._session,
                                          save_dir + "-model.chkpt")

            model_info = "Model used for submission: " + ""

        if opts.interactive:
            code.interact(local=locals())
Example #9
0
    def predict(self, imgs):
        """Run inference on `imgs` and return predicted masks

        imgs: [num_images, image_height, image_width, num_channel]
        returns: masks [num_images, images_height, image_width] with road probabilities
        """
        opts = self._options

        num_images = imgs.shape[0]
        print("Running prediction on {} images... ".format(num_images), end="")

        if opts.ensemble_prediction:
            print("Start data augmentation for prediction...")
            imgs = images.image_augmentation_ensemble(imgs)
            print("Done")
            num_images = imgs.shape[0]

        offset = int(
            (unet.input_size_needed(opts.patch_size, opts.num_layers) -
             opts.patch_size) / 2)
        imgs_exp = images.mirror_border(imgs, offset)
        patches = images.extract_patches(imgs_exp,
                                         patch_size=unet.input_size_needed(
                                             opts.patch_size, opts.num_layers),
                                         predict_patch_size=opts.patch_size,
                                         stride=opts.stride)
        num_patches = patches.shape[0]
        num_channel = imgs.shape[3]

        # patches padding to have full batches
        if num_patches % opts.batch_size != 0:
            num_extra_patches = opts.batch_size - (num_patches %
                                                   opts.batch_size)
            extra_patches = np.zeros((num_extra_patches, opts.patch_size,
                                      opts.patch_size, num_channel))
            patches = np.concatenate([patches, extra_patches], axis=0)

        num_batches = int(patches.shape[0] / opts.batch_size)
        eval_predictions = np.ndarray(shape=(patches.shape[0], opts.patch_size,
                                             opts.patch_size))

        for batch in range(num_batches):
            offset = batch * opts.batch_size

            feed_dict = {
                self._patches_node:
                patches[offset:offset + opts.batch_size, :, :, :],
            }
            eval_predictions[offset:offset +
                             opts.batch_size, :, :] = self._session.run(
                                 self._predictions, feed_dict)

        # remove padding
        eval_predictions = eval_predictions[0:num_patches]
        patches_per_image = int(num_patches / num_images)

        # construct masks
        new_shape = (num_images, patches_per_image, opts.patch_size,
                     opts.patch_size, 1)
        masks = images.images_from_patches(eval_predictions.reshape(new_shape),
                                           stride=opts.stride)

        if opts.ensemble_prediction:
            print("Invert Data augmentation and average predictions...")
            masks = images.invert_image_augmentation_ensemble(masks)
            print("Averaging done...")

        print("Prediction Done")
        return masks