def prepare_images(self):
        x_data = []
        y_data = []

        for img, lbl, extent in zip(self.chunks_array, self.chunks_label, self.extents):
            if self.removal_function(lbl, self.image_size, self.threshold):
                x_data.append(Raster.from_array(img, img.pixel, extent))
                y_data.append(Raster.from_array(lbl, img.pixel, extent))
        return UnetData(x_data, y_data)
    def test_split_image_to_cnn(self):
        px = Pixel(1.0, 1.0)
        epsg_4326 = Crs("epsg:4326")
        image_array = np.array([[*list(range(1, 8))], [*list(range(2, 9))],
                                [*list(range(3, 10))], [*list(range(4, 11))],
                                [*list(range(5, 12))], [*list(range(10, 17))],
                                [*list(range(20, 27))]]).reshape(7, 7, 1)

        image = Raster.from_array(image_array,
                                  extent=Extent.from_coordinates(
                                      [0, 0, 7.0, 7.0], epsg_4326),
                                  pixel=px)

        target_result = [
            Raster.from_array(image_array[0:5, 0:5],
                              extent=Extent.from_coordinates([0, 0, 1, 1],
                                                             crs=epsg_4326),
                              pixel=px),
            Raster.from_array(image_array[0:5, 1:6],
                              extent=Extent.from_coordinates([0, 0, 1, 1],
                                                             crs=epsg_4326),
                              pixel=px),
            Raster.from_array(image_array[0:5, 2:7],
                              extent=Extent.from_coordinates([0, 0, 1, 1],
                                                             crs=epsg_4326),
                              pixel=px),
            Raster.from_array(image_array[1:6, 0:5],
                              extent=Extent.from_coordinates([0, 0, 1, 1],
                                                             crs=epsg_4326),
                              pixel=px),
            Raster.from_array(image_array[1:6, 1:6],
                              extent=Extent.from_coordinates([0, 0, 1, 1],
                                                             crs=epsg_4326),
                              pixel=px),
            Raster.from_array(image_array[1:6, 2:7],
                              extent=Extent.from_coordinates([0, 0, 1, 1],
                                                             crs=epsg_4326),
                              pixel=px),
            Raster.from_array(image_array[2:7, 0:5],
                              extent=Extent.from_coordinates([0, 0, 1, 1],
                                                             crs=epsg_4326),
                              pixel=px),
            Raster.from_array(image_array[2:7, 1:6],
                              extent=Extent.from_coordinates([0, 0, 1, 1],
                                                             crs=epsg_4326),
                              pixel=px),
            Raster.from_array(image_array[2:7, 2:7],
                              extent=Extent.from_coordinates([0, 0, 1, 1],
                                                             crs=epsg_4326),
                              pixel=px)
        ]

        function_result = split_images_to_cnn(image=image, window_size=5)

        self.assertEqual(target_result, function_result)
Exemple #3
0
    def predict(self, x, threshold) -> Raster:
        if not self.is_trained:
            raise AttributeError("Model is not trained yet")
        predicted = self.model.predict(np.array([x]))
        filtered = (predicted > threshold).astype(np.uint8)
        raster = Raster.from_array(filtered[0, :, :, :],
                                   pixel=x.pixel,
                                   extent=x.extent)

        return raster