Пример #1
0
 def _apply_sliding_windows(self, raster):
     with rasterio.open(raster) as src:
         windows = sliding_windows(self.size,
                                   self.step_size,
                                   height=src.height,
                                   width=src.width)
         return [(win, box(*src.window_bounds(win))) for win in windows]
Пример #2
0
def test_sliding_windows_whole_width_only():
    windows = list(sliding_windows(size=2, step_size=2, width=6, height=5))
    assert windows == [
        # row 0
        Window(col_off=0, row_off=0, width=2, height=2),
        Window(col_off=2, row_off=0, width=2, height=2),
        Window(col_off=4, row_off=0, width=2, height=2),
        # row 1
        Window(col_off=0, row_off=2, width=2, height=2),
        Window(col_off=2, row_off=2, width=2, height=2),
        Window(col_off=4, row_off=2, width=2, height=2),
    ]
Пример #3
0
def test_sliding_windows_odd_size():
    windows = list(sliding_windows(size=4, step_size=2, width=7, height=9))
    assert windows == [
        # row 0
        Window(col_off=0, row_off=0, width=4, height=4),
        Window(col_off=2, row_off=0, width=4, height=4),
        # row 1
        Window(col_off=0, row_off=2, width=4, height=4),
        Window(col_off=2, row_off=2, width=4, height=4),
        # row 2
        Window(col_off=0, row_off=4, width=4, height=4),
        Window(col_off=2, row_off=4, width=4, height=4),
    ]
Пример #4
0
def predict_image(fname,
                  model,
                  size,
                  threshold,
                  step_size=None,
                  rescale_intensity=False,
                  lower_cut=2,
                  upper_cut=98):
    if not step_size:
        step_size = size

    with rio.open(fname) as src:
        matching_windows = []

        windows = sliding_windows(
            size, step_size, height=src.shape[0], width=src.shape[1])

        for window in windows:
            window_box = box(*src.window_bounds(window))

            img = np.dstack([src.read(b, window=window) for b in range(1, 4)])

            if rescale_intensity:
                low, high = np.percentile(img, (lower_cut, upper_cut))
                img = exposure.rescale_intensity(img, in_range=(low, high))

            img = img / 255.
            preds = model.predict(np.array([img]))
            preds_b = preds[:, 0]

            for i in np.nonzero(preds_b > threshold)[0]:
                _logger.info((window, float(preds_b[i])))
                reproject_window_box = reproject_shape(window_box, src.crs,
                                                       WGS84_CRS)
                shape_with = ShapeWithProps(
                    shape=reproject_window_box, props={})
                shape_with.props["prob"] = float(preds_b[i])
                matching_windows.append(shape_with)

        return matching_windows
Пример #5
0
def predict_image(fname,
                  model,
                  size,
                  threshold,
                  step_size=None,
                  rasters_contour=None,
                  rescale_intensity=True,
                  lower_cut=2,
                  upper_cut=98):

    if not step_size:
        step_size = size

    if rescale_intensity:
        percentiles = calculate_percentiles(fname,
                                            lower_cut=lower_cut,
                                            upper_cut=upper_cut)

    with rio.open(fname) as src:
        matching_windows = []

        windows = sliding_windows(size,
                                  step_size,
                                  height=src.shape[0],
                                  width=src.shape[1])

        windows_and_boxes = [(w, box(*src.window_bounds(w))) for w in windows]
        _logger.info('Total windows: %d', len(windows_and_boxes))

        if rasters_contour:
            contour_polygon, contour_crs = load_raster_contour_polygon(
                rasters_contour)
            contour_polygon = reproject_shape(contour_polygon, contour_crs,
                                              src.crs)
            windows_and_boxes = [(w, b) for w, b in windows_and_boxes
                                 if contour_polygon.intersection(b)]
            _logger.info(
                'Total windows (after filtering with raster contour shape): %d',
                len(windows_and_boxes))

        total = len(windows_and_boxes) // BATCH_SIZE
        for group in tqdm.tqdm(grouper(windows_and_boxes, BATCH_SIZE),
                               total=total):
            imgs = []
            window_boxes = []
            for pair in group:
                if pair:
                    window, window_box = pair

                    img = np.dstack(
                        [src.read(b, window=window) for b in range(1, 4)])

                    if rescale_intensity:
                        img = exposure.rescale_intensity(img,
                                                         in_range=percentiles)

                    img = resnet50.preprocess_input(img)

                    imgs.append(img)
                    window_boxes.append(window_box)

            preds = model.predict(np.array(imgs))
            preds_b = preds[:, 0]

            for i in np.nonzero(preds_b >= threshold)[0]:
                _logger.info((window, float(preds_b[i])))
                window_box = window_boxes[i]
                reproject_window_box = reproject_shape(window_box, src.crs,
                                                       WGS84_CRS)
                s = ShapeWithProps(shape=reproject_window_box, props={})
                s.props['prob'] = float(preds_b[i])
                matching_windows.append(s)

        return matching_windows