Example #1
0
    def get_vector_scene(self, class_id, use_aoi=False):
        gt_uri = data_file_path('{}-gt-polygons.geojson'.format(class_id))
        pred_uri = data_file_path('{}-pred-polygons.geojson'.format(class_id))

        scene_id = str(class_id)
        rs = MockRasterSource(channel_order=[0, 1, 3], num_channels=3)
        rs.set_raster(np.zeros((10, 10, 3)))

        crs_transformer = IdentityCRSTransformer()
        extent = Box.make_square(0, 0, 360)

        gt_rs = RasterizedSource(GeoJSONVectorSource(gt_uri, crs_transformer),
                                 RasterizedSourceConfig.RasterizerOptions(2),
                                 extent, crs_transformer)
        gt_ls = SemanticSegmentationLabelSource(source=gt_rs)

        pred_rs = RasterizedSource(
            GeoJSONVectorSource(pred_uri, crs_transformer),
            RasterizedSourceConfig.RasterizerOptions(2), extent,
            crs_transformer)
        pred_ls = SemanticSegmentationLabelSource(source=pred_rs)
        pred_ls.vector_output = [{
            'uri': pred_uri,
            'denoise': 0,
            'mode': 'polygons',
            'class_id': class_id
        }]

        if use_aoi:
            aoi_uri = data_file_path('{}-aoi.geojson'.format(class_id))
            aoi_geojson = json.loads(file_to_str(aoi_uri))
            aoi_polygons = [shape(aoi_geojson['features'][0]['geometry'])]
            return Scene(scene_id, rs, gt_ls, pred_ls, aoi_polygons)

        return Scene(scene_id, rs, gt_ls, pred_ls)
    def test_accounts_for_aoi(self):
        class_config = ClassConfig(names=['car', 'building', 'background'])

        label_source_uri = data_file_path('evaluator/cc-label-filtered.json')
        label_source_cfg = ChipClassificationLabelSourceConfig(
            vector_source=GeoJSONVectorSourceConfig(
                uri=label_source_uri, default_class_id=None))

        label_store_uri = data_file_path('evaluator/cc-label-full.json')
        label_store_cfg = ChipClassificationGeoJSONStoreConfig(
            uri=label_store_uri)

        raster_source_uri = data_file_path('evaluator/cc-label-img-blank.tif')
        raster_source_cfg = RasterioSourceConfig(uris=[raster_source_uri])

        aoi_uri = data_file_path('evaluator/cc-label-aoi.json')
        s = SceneConfig(
            id='test',
            raster_source=raster_source_cfg,
            label_source=label_source_cfg,
            label_store=label_store_cfg,
            aoi_uris=[aoi_uri])

        with rv_config.get_tmp_dir() as tmp_dir:
            scene = s.build(class_config, tmp_dir)
            output_uri = os.path.join(tmp_dir, 'eval.json')

            evaluator = ChipClassificationEvaluatorConfig(
                output_uri=output_uri).build(class_config)
            evaluator.process([scene], tmp_dir)

            overall = file_to_json(output_uri)['overall']
            for item in overall:
                self.assertEqual(item['f1'], 1.0)
Example #3
0
    def test_make_predict_windows_with_aoi(self):
        task_config = rv.TaskConfig.builder(rv.CHIP_CLASSIFICATION) \
                                   .with_chip_size(200) \
                                   .with_classes(['car', 'building', 'background']) \
                                   .build()

        backend_config = rv.BackendConfig.builder(rv.KERAS_CLASSIFICATION) \
                                         .with_task(task_config) \
                                         .with_model_defaults(rv.RESNET50_IMAGENET) \
                                         .with_pretrained_model(None) \
                                         .build()

        label_source_uri = data_file_path('evaluator/cc-label-full.json')
        label_source = rv.LabelSourceConfig.builder(rv.CHIP_CLASSIFICATION_GEOJSON) \
                                           .with_uri(label_source_uri) \
                                           .build()

        label_source_2_uri = data_file_path('evaluator/cc-label-filtered.json')
        label_source_2 = rv.LabelSourceConfig.builder(rv.CHIP_CLASSIFICATION_GEOJSON) \
                                           .with_uri(label_source_2_uri) \
                                           .build()

        source_uri = data_file_path('evaluator/cc-label-img-blank.tif')
        raster_source = rv.RasterSourceConfig.builder(rv.RASTERIO_SOURCE) \
                                             .with_uri(source_uri) \
                                             .build()

        aoi_uri = data_file_path('evaluator/cc-label-aoi.json')
        s = rv.SceneConfig.builder() \
                          .with_id('test') \
                          .with_raster_source(raster_source) \
                          .with_label_source(label_source) \
                          .with_aoi_uri(aoi_uri) \
                          .build()

        with RVConfig.get_tmp_dir() as tmp_dir:
            scene = s.create_scene(task_config, tmp_dir)
            backend = backend_config.create_backend(task_config)
            task = task_config.create_task(backend)

            with scene.activate():
                windows = task.get_train_windows(scene)

            from rastervision.data import (ChipClassificationLabels,
                                           ChipClassificationGeoJSONStore)
            labels = ChipClassificationLabels()
            for w in windows:
                labels.set_cell(w, 1)
            store = ChipClassificationGeoJSONStore(
                os.path.join(tmp_dir, 'test.json'),
                scene.raster_source.get_crs_transformer(),
                task_config.class_map)
            store.save(labels)

            ls = label_source_2.create_source(
                task_config, scene.raster_source.get_extent(),
                scene.raster_source.get_crs_transformer(), tmp_dir)
            actual = ls.get_labels().get_cells()

            self.assertEqual(len(windows), len(actual))
Example #4
0
    def test_nonidentical_extents_and_resolutions(self):
        img_path_1 = data_file_path(
            'multi_raster_source/const_100_600x600.tiff')
        img_path_2 = data_file_path('multi_raster_source/const_175_60x60.tiff')
        img_path_3 = data_file_path('multi_raster_source/const_250_6x6.tiff')
        source_1 = RasterioSourceConfig(uris=[img_path_1], channel_order=[0])
        source_2 = RasterioSourceConfig(uris=[img_path_2], channel_order=[0])
        source_3 = RasterioSourceConfig(uris=[img_path_3], channel_order=[0])

        cfg = MultiRasterSourceConfig(raster_sources=[
            SubRasterSourceConfig(raster_source=source_1, target_channels=[0]),
            SubRasterSourceConfig(raster_source=source_2, target_channels=[1]),
            SubRasterSourceConfig(raster_source=source_3, target_channels=[2])
        ])
        rs = cfg.build(tmp_dir=self.tmp_dir)
        with rs.activate():
            for get_chip_fn in [rs._get_chip, rs.get_chip]:
                ch_1_only = get_chip_fn(Box(0, 0, 10, 10))
                self.assertEqual(tuple(ch_1_only.reshape(-1, 3).mean(axis=0)),
                                 (100, 0, 0))
                ch_2_only = get_chip_fn(Box(0, 600, 10, 600 + 10))
                self.assertEqual(tuple(ch_2_only.reshape(-1, 3).mean(axis=0)),
                                 (0, 175, 0))
                ch_3_only = get_chip_fn(Box(600, 0, 600 + 10, 10))
                self.assertEqual(tuple(ch_3_only.reshape(-1, 3).mean(axis=0)),
                                 (0, 0, 250))
                full_img = get_chip_fn(Box(0, 0, 600, 600))
                self.assertEqual(set(np.unique(full_img[..., 0])), {100})
                self.assertEqual(set(np.unique(full_img[..., 1])), {0, 175})
                self.assertEqual(set(np.unique(full_img[..., 2])), {0, 250})
    def test_accounts_for_aoi(self):
        task = rv.TaskConfig.builder(rv.CHIP_CLASSIFICATION) \
                            .with_classes(['car', 'building', 'background']) \
                            .build()

        label_source_uri = data_file_path('evaluator/cc-label-filtered.json')
        label_source = rv.LabelSourceConfig.builder(rv.CHIP_CLASSIFICATION_GEOJSON) \
                                           .with_uri(label_source_uri) \
                                           .build()

        label_store_uri = data_file_path('evaluator/cc-label-full.json')
        label_store = rv.LabelStoreConfig.builder(rv.CHIP_CLASSIFICATION_GEOJSON) \
                                         .with_uri(label_store_uri) \
                                         .build()

        source_uri = data_file_path('evaluator/cc-label-img-blank.tif')
        raster_source = rv.RasterSourceConfig.builder(rv.GEOTIFF_SOURCE) \
                                             .with_uri(source_uri) \
                                             .build()

        aoi_uri = data_file_path('evaluator/cc-label-aoi.json')
        s = rv.SceneConfig.builder() \
                          .with_id('test') \
                          .with_raster_source(raster_source) \
                          .with_label_source(label_source) \
                          .with_label_store(label_store) \
                          .with_aoi_uri(aoi_uri) \
                          .build()

        with RVConfig.get_tmp_dir() as tmp_dir:
            scene = s.create_scene(task, tmp_dir)

            output_uri = os.path.join(tmp_dir, 'eval.json')

            e = rv.EvaluatorConfig.builder(rv.CHIP_CLASSIFICATION_EVALUATOR) \
                                  .with_task(task) \
                                  .with_output_uri(output_uri) \
                                  .build()

            e.update_for_command(rv.EVAL, create_mock_experiment())

            evaluator = e.create_evaluator()

            evaluator.process([scene], tmp_dir)

            results = None
            with open(output_uri) as f:
                results = json.loads(f.read())['overall']

            for result in results:
                self.assertEqual(result['f1'], 1.0)
Example #6
0
    def test_command_create_no_compression(self):
        src_path = data_file_path('small-rgb-tile.tif')
        with RVConfig.get_tmp_dir() as tmp_dir:
            cog_path = os.path.join(tmp_dir, 'cog.tif')

            cmd_conf = rv.CommandConfig.builder(rv.COGIFY) \
                                       .with_root_uri(tmp_dir) \
                                       .with_config(uris=[(src_path, cog_path)],
                                                    block_size=128,
                                                    compression='none') \
                                       .build()

            cmd_conf = rv.command.CommandConfig.from_proto(cmd_conf.to_proto())
            cmd = cmd_conf.create_command()

            self.assertTrue(cmd, rv.command.aux.CogifyCommand)

            cmd.run(tmp_dir)

            # Check that it's cogified
            with rasterio.open(cog_path) as ds:
                self.assertEqual(ds.block_shapes, [(128, 128), (128, 128),
                                                   (128, 128)])
                self.assertEqual(ds.overviews(1), [2, 4, 8, 16, 32])
                self.assertIsNone(ds.compression)
Example #7
0
    def test_command_through_experiment(self):
        src_path = data_file_path('small-rgb-tile.tif')
        with RVConfig.get_tmp_dir() as tmp_dir:
            cog_path = os.path.join(tmp_dir, 'cog.tif')

            e = mk.create_mock_experiment().to_builder() \
                                           .with_root_uri(tmp_dir) \
                                           .with_custom_config({
                                               'cogify': {
                                                   'key': 'test',
                                                   'config': {
                                                       'uris': [(src_path, cog_path)],
                                                       'block_size': 128
                                                   }
                                               }
                                           }) \
                                           .build()

            rv.ExperimentRunner.get_runner(rv.LOCAL).run(
                e, splits=2, commands_to_run=[rv.COGIFY])

            # Check that it's cogified
            with rasterio.open(cog_path) as ds:
                self.assertEqual(ds.block_shapes, [(128, 128), (128, 128),
                                                   (128, 128)])
                self.assertEqual(ds.overviews(1), [2, 4, 8, 16, 32])
    def test_get_dtype(self):
        img_path = data_file_path('small-rgb-tile.tif')
        with RVConfig.get_tmp_dir() as tmp_dir:
            source = rv.data.RasterioSourceConfig(uris=[img_path]) \
                            .create_source(tmp_dir)

            self.assertEqual(source.get_dtype(), np.uint8)
    def test_gets_raw_chip_from_uint16_transformed_proto(self):
        img_path = data_file_path('small-uint16-tile.tif')
        channel_order = [0, 1]

        with RVConfig.get_tmp_dir() as temp_dir:
            stats_uri = os.path.join(temp_dir, 'temp.tif')
            stats = RasterStats()
            stats.compute([
                rv.RasterSourceConfig.builder(rv.RASTERIO_SOURCE).with_uri(
                    img_path).build().create_source(temp_dir)
            ])
            stats.save(stats_uri)

            transformer = rv.RasterTransformerConfig.builder(rv.STATS_TRANSFORMER) \
                                                    .with_stats_uri(stats_uri) \
                                                    .build()

            msg = rv.RasterSourceConfig.builder(rv.RASTERIO_SOURCE) \
                                       .with_uri(img_path) \
                                       .with_channel_order(channel_order) \
                                       .with_transformer(transformer) \
                                       .build() \
                                       .to_proto()

            source = rv.RasterSourceConfig.from_proto(msg) \
                                          .create_source(tmp_dir=None)

            with source.activate():
                out_chip = source.get_raw_image_array()
                self.assertEqual(out_chip.shape[2], 3)
Example #10
0
    def test_read_stac(self):
        expected_keys = {
            'label_uri': str,
            'image_uris': list,
            'label_bbox': Polygon,
            'image_bbox': (type(None), Polygon),
            'bboxes_intersect': bool,
            'aoi_geometry': (type(None), dict)
        }
        zip_path = data_file_path('catalog.zip')
        with TemporaryDirectory(dir='/opt/data/tmp') as tmp_dir:
            out = read_stac(zip_path, tmp_dir)

            # check for correctness of format
            self.assertIsInstance(out, list)
            for o in out:
                self.assertIsInstance(o, dict)
                for k, v in o.items():
                    self.assertTrue(k in expected_keys)
                    self.assertIsInstance(v, expected_keys[k])
                for uri in o['image_uris']:
                    self.assertIsInstance(uri, str)

            # check for correctness of content (WRT the test catalog)
            self.assertEqual(len(out), 1)
            self.assertEqual(len(out[0]['image_uris']), 1)

            for o in out:
                label_uri = o['label_uri']
                self.assertTrue(os.path.exists(label_uri))
                self.assertTrue(label_uri.startswith(tmp_dir))
Example #11
0
    def test_with_aoi(self):
        aoi_uri = data_file_path('evaluator/cc-label-aoi.json')
        task_config = rv.TaskConfig.builder(mk.MOCK_TASK).build()

        scene_config = mk.create_mock_scene()
        scene_config = scene_config.to_builder().with_aoi_uri(aoi_uri).build()
        scene = scene_config.create_scene(task_config, self.temp_dir.name)
        self.assertEqual(1, len(scene.aoi_polygons))
    def get_base(self):
        root_uri = '/some/dummy/root'
        img_path = '/dummy.tif'
        label_path = '/dummy.json'
        backend_conf_path = data_file_path(
            'tf_object_detection/'
            'embedded_ssd_mobilenet_v1_coco.config')

        pretrained_model = ('https://dummy.com/model.gz')

        task = rv.TaskConfig.builder(rv.OBJECT_DETECTION) \
                            .with_chip_size(300) \
                            .with_classes({
                                'car': (1, 'blue'),
                                'building': (2, 'red')
                            }) \
                            .with_chip_options(neg_ratio=0.0,
                                               ioa_thresh=1.0,
                                               window_method='sliding') \
                            .with_predict_options(merge_thresh=0.1,
                                                  score_thresh=0.5) \
                            .build()

        backend = rv.BackendConfig.builder(rv.TF_OBJECT_DETECTION) \
                                  .with_task(task) \
                                  .with_template(backend_conf_path) \
                                  .with_pretrained_model(pretrained_model) \
                                  .with_train_options(sync_interval=None,
                                                      do_monitoring=False) \
                                  .build()

        raster_source = rv.RasterSourceConfig.builder(rv.RASTERIO_SOURCE) \
                          .with_uri(img_path) \
                          .with_channel_order([0, 1, 2]) \
                          .with_stats_transformer() \
                          .build()

        scene = rv.SceneConfig.builder() \
                              .with_task(task) \
                              .with_id('od_test') \
                              .with_raster_source(raster_source) \
                              .with_label_source(label_path) \
                              .build()

        dataset = rv.DatasetConfig.builder() \
                                  .with_train_scene(scene) \
                                  .with_validation_scene(scene) \
                                  .build()

        analyzer = rv.analyzer.StatsAnalyzerConfig()

        return rv.ExperimentConfig.builder() \
                                  .with_root_uri(root_uri) \
                                  .with_task(task) \
                                  .with_backend(backend) \
                                  .with_dataset(dataset) \
                                  .with_analyzer(analyzer) \
                                  .with_train_key('model_name')
 def test_evaluator(self):
     output_uri = join(self.tmp_dir.name, 'out.json')
     scenes = [self.get_scene(0), self.get_scene(1)]
     evaluator = SemanticSegmentationEvaluator(self.class_config,
                                               output_uri, None)
     evaluator.process(scenes, self.tmp_dir.name)
     eval_json = file_to_json(output_uri)
     exp_eval_json = file_to_json(data_file_path('expected-eval.json'))
     self.assertDictEqual(eval_json, exp_eval_json)
Example #14
0
 def test_using_null_class_bufs(self):
     uri = data_file_path('polygon-labels.geojson')
     vs = rv.VectorSourceConfig.builder(rv.GEOJSON_SOURCE) \
             .with_uri(uri) \
             .with_buffers(line_bufs={1: None}) \
             .build()
     with self.assertRaises(rv.ConfigError):
         rv.LabelSourceConfig.builder(rv.OBJECT_DETECTION) \
             .with_vector_source(vs) \
             .build()
Example #15
0
    def test_gets_raw_chip(self):
        img_path = data_file_path('small-rgb-tile.tif')
        channel_order = [0, 1]

        config = RasterioSourceConfig(
            uris=[img_path], channel_order=channel_order)
        source = config.build(tmp_dir=self.tmp_dir)
        with source.activate():
            out_chip = source.get_raw_image_array()
            self.assertEqual(out_chip.shape[2], 3)
    def test_vector_compute(self):
        class_map = ClassMap([ClassItem(id=1, name='one', color='#000021')])
        gt_uri = data_file_path('3-gt-polygons.geojson')
        pred_uri = data_file_path('3-pred-polygons.geojson')

        eval = SemanticSegmentationEvaluation(class_map)
        eval.compute_vector(gt_uri, pred_uri, 'polygons', 1)

        # NOTE: The  two geojson files referenced  above contain three
        # unique geometries total, each  file contains two geometries,
        # and there is one geometry shared between the two.
        tp = 1.0
        fp = 1.0
        fn = 1.0
        precision = float(tp) / (tp + fp)
        recall = float(tp) / (tp + fn)

        self.assertAlmostEqual(precision, eval.class_to_eval_item[1].precision)
        self.assertAlmostEqual(recall, eval.class_to_eval_item[1].recall)
Example #17
0
 def test_with_aoi_back_compat(self):
     aoi_uri = data_file_path('evaluator/cc-label-aoi.json')
     task_config = rv.TaskConfig.builder(mk.MOCK_TASK).build()
     scene_config = mk.create_mock_scene()
     scene_msg = scene_config.to_proto()
     # Use deprecated aoi_uri field.
     del scene_msg.aoi_uris[:]
     scene_msg.aoi_uri = aoi_uri
     scene_config = rv.SceneConfig.from_proto(scene_msg)
     scene = scene_config.create_scene(task_config, self.temp_dir.name)
     self.assertEqual(1, len(scene.aoi_polygons))
    def test_gets_raw_chip(self):
        img_path = data_file_path('small-rgb-tile.tif')
        channel_order = [0, 1]

        source = rv.data.RasterioSourceConfig(uris=[img_path],
                                              channel_order=channel_order) \
                        .create_source(tmp_dir=None)

        with source.activate():
            out_chip = source.get_raw_image_array()
            self.assertEqual(out_chip.shape[2], 3)
Example #19
0
 def get_backend(task, tmp_dir):
     model_uri = os.path.join(tmp_dir, 'model')
     template_uri = data_file_path(
         'tf_object_detection/embedded_ssd_mobilenet_v1_coco.config')
     with open(model_uri, 'w') as f:
         f.write('DUMMY')
     b = rv.BackendConfig.builder(rv.TF_OBJECT_DETECTION) \
                         .with_task(task) \
                         .with_template(template_uri) \
                         .with_model_uri(model_uri) \
                         .build()
     return b
Example #20
0
    def test_extent_crop(self):
        f = 1 / 4
        img_path = data_file_path('small-rgb-tile.tif')

        cfg_crop = RasterioSourceConfig(
            uris=[img_path], extent_crop=(f, f, f, f))
        rs_crop = cfg_crop.build(tmp_dir=self.tmp_dir)

        # test extent box
        extent_crop = rs_crop.get_extent()
        self.assertEqual(extent_crop.ymin, 64)
        self.assertEqual(extent_crop.xmin, 64)
        self.assertEqual(extent_crop.ymax, 192)
        self.assertEqual(extent_crop.xmax, 192)

        # test windows
        windows = extent_crop.get_windows(64, 64)
        self.assertEqual(windows[0].ymin, 64)
        self.assertEqual(windows[0].xmin, 64)
        self.assertEqual(windows[-1].ymax, 192)
        self.assertEqual(windows[-1].xmax, 192)

        # test CropOffsets class
        cfg_crop = RasterioSourceConfig(
            uris=[img_path],
            extent_crop=CropOffsets(skip_top=.5, skip_right=.5))
        rs_crop = cfg_crop.build(tmp_dir=self.tmp_dir)
        extent_crop = rs_crop.get_extent()

        self.assertEqual(extent_crop.ymin, 128)
        self.assertEqual(extent_crop.xmin, 0)
        self.assertEqual(extent_crop.ymax, 256)
        self.assertEqual(extent_crop.xmax, 128)

        # test validation
        extent_crop = CropOffsets(skip_top=.5, skip_bottom=.5)
        self.assertRaises(
            ValidationError,
            lambda: RasterioSourceConfig(uris=[img_path],
                                         extent_crop=extent_crop))

        extent_crop = CropOffsets(skip_left=.5, skip_right=.5)
        self.assertRaises(
            ValidationError,
            lambda: RasterioSourceConfig(uris=[img_path],
                                         extent_crop=extent_crop))

        # test extent_crop=None
        try:
            _ = RasterioSourceConfig(uris=[img_path], extent_crop=None)  # noqa
        except Exception:
            self.fail('extent_crop=None caused an error.')
Example #21
0
 def test_extent(self):
     img_path = data_file_path('small-rgb-tile.tif')
     cfg = RasterioSourceConfig(uris=[img_path])
     rs = cfg.build(tmp_dir=self.tmp_dir)
     extent = rs.get_extent()
     h, w = extent.get_height(), extent.get_width()
     ymin, xmin, ymax, xmax = extent
     self.assertEqual(h, 256)
     self.assertEqual(w, 256)
     self.assertEqual(ymin, 0)
     self.assertEqual(xmin, 0)
     self.assertEqual(ymax, 256)
     self.assertEqual(xmax, 256)
def make_cfg(img_path='small-rgb-tile.tif', **kwargs):
    img_path = data_file_path(img_path)
    r_source = RasterioSourceConfig(uris=[img_path], channel_order=[0])
    g_source = RasterioSourceConfig(uris=[img_path], channel_order=[1])
    b_source = RasterioSourceConfig(uris=[img_path], channel_order=[2])

    cfg = MultiRasterSourceConfig(raster_sources=[
        SubRasterSourceConfig(raster_source=r_source, target_channels=[0]),
        SubRasterSourceConfig(raster_source=g_source, target_channels=[1]),
        SubRasterSourceConfig(raster_source=b_source, target_channels=[2])
    ],
                                  **kwargs)
    return cfg
    def test_vector_compute(self):
        class_config = ClassConfig(names=['one', 'two'])
        class_config.update()
        class_config.ensure_null_class()

        gt_uri = data_file_path('2-gt-polygons.geojson')
        pred_uri = data_file_path('2-pred-polygons.geojson')

        eval = SemanticSegmentationEvaluation(class_config)
        eval.compute_vector(gt_uri, pred_uri, 'polygons', 0)

        # NOTE: The  two geojson files referenced  above contain three
        # unique geometries total, each  file contains two geometries,
        # and there is one geometry shared between the two.
        tp = 1.0
        fp = 1.0
        fn = 1.0
        precision = float(tp) / (tp + fp)
        recall = float(tp) / (tp + fn)

        self.assertAlmostEqual(precision, eval.class_to_eval_item[0].precision)
        self.assertAlmostEqual(recall, eval.class_to_eval_item[0].recall)
Example #24
0
 def test_evaluator(self):
     class_map = ClassMap([
         ClassItem(id=1, name='one'),
         ClassItem(id=2, name='two'),
     ])
     output_uri = join(self.tmp_dir.name, 'out.json')
     scenes = [self.get_scene(1), self.get_scene(2)]
     evaluator = SemanticSegmentationEvaluator(class_map, output_uri, None)
     evaluator.process(scenes, self.tmp_dir.name)
     eval_json = json.loads(file_to_str(output_uri))
     exp_eval_json = json.loads(
         file_to_str(data_file_path('expected-eval.json')))
     self.assertDictEqual(eval_json, exp_eval_json)
    def _test_get_geojson(self, vector_tile_uri, json_uri):
        source = self._get_source(vector_tile_uri)
        geojson = source.get_geojson()
        expected_geojson = json.loads(file_to_str(data_file_path(json_uri)))

        # Need to convert to JSON and back again because geojson object has tuples
        # instead of lists because of a quirk of shapely.geometry.mapping
        # See https://github.com/Toblerity/Shapely/issues/245
        geojson = json.loads(json.dumps(geojson))
        geojson['features'].sort(key=lambda f: f['properties']['__id'])
        expected_geojson['features'].sort(
            key=lambda f: f['properties']['__id'])

        self.assertDictEqual(geojson, expected_geojson)
    def get_vector_scene(self, class_id, use_aoi=False):
        gt_uri = data_file_path('{}-gt-polygons.geojson'.format(class_id))
        pred_uri = data_file_path('{}-pred-polygons.geojson'.format(class_id))

        scene_id = str(class_id)
        rs = MockRasterSource(channel_order=[0, 1, 3], num_channels=3)
        rs.set_raster(np.zeros((10, 10, 3)))

        crs_transformer = IdentityCRSTransformer()
        extent = Box.make_square(0, 0, 360)

        config = RasterizedSourceConfig(
            vector_source=GeoJSONVectorSourceConfig(uri=gt_uri,
                                                    default_class_id=0),
            rasterizer_config=RasterizerConfig(background_class_id=1))
        gt_rs = config.build(self.class_config, crs_transformer, extent)
        gt_ls = SemanticSegmentationLabelSource(gt_rs, self.null_class_id)

        config = RasterizedSourceConfig(
            vector_source=GeoJSONVectorSourceConfig(uri=pred_uri,
                                                    default_class_id=0),
            rasterizer_config=RasterizerConfig(background_class_id=1))
        pred_rs = config.build(self.class_config, crs_transformer, extent)
        pred_ls = SemanticSegmentationLabelSource(pred_rs, self.null_class_id)
        pred_ls.vector_output = [
            PolygonVectorOutputConfig(uri=pred_uri,
                                      denoise=0,
                                      class_id=class_id)
        ]

        if use_aoi:
            aoi_uri = data_file_path('{}-aoi.geojson'.format(class_id))
            aoi_geojson = file_to_json(aoi_uri)
            aoi_polygons = [shape(aoi_geojson['features'][0]['geometry'])]
            return Scene(scene_id, rs, gt_ls, pred_ls, aoi_polygons)

        return Scene(scene_id, rs, gt_ls, pred_ls)
Example #27
0
    def test_get_chip(self):
        # create a 3-channel raster from a 1-channel raster
        # using a ReclassTransformer to give each channel a different value
        # (100, 175, and 250 respectively)
        path = data_file_path('multi_raster_source/const_100_600x600.tiff')
        source_1 = RasterioSourceConfig(uris=[path], channel_order=[0])
        source_2 = RasterioSourceConfig(
            uris=[path],
            channel_order=[0],
            transformers=[ReclassTransformerConfig(mapping={100: 175})])
        source_3 = RasterioSourceConfig(
            uris=[path],
            channel_order=[0],
            transformers=[ReclassTransformerConfig(mapping={100: 250})])

        cfg = MultiRasterSourceConfig(raster_sources=[
            SubRasterSourceConfig(raster_source=source_1, target_channels=[0]),
            SubRasterSourceConfig(raster_source=source_2, target_channels=[1]),
            SubRasterSourceConfig(raster_source=source_3, target_channels=[2])
        ],
                                      channel_order=[2, 1, 0],
                                      transformers=[
                                          ReclassTransformerConfig(mapping={
                                              100: 10,
                                              175: 17,
                                              250: 25
                                          })
                                      ])
        rs = cfg.build(tmp_dir=self.tmp_dir)

        with rs.activate():
            window = Box(0, 0, 100, 100)

            # sub transformers and channel_order applied
            sub_chips = rs._get_sub_chips(window, raw=False)
            self.assertEqual(tuple(c.mean() for c in sub_chips),
                             (100, 175, 250))
            # sub transformers, channel_order, and transformer applied
            chip = rs.get_chip(window)
            self.assertEqual(tuple(chip.reshape(-1, 3).mean(axis=0)),
                             (25, 17, 10))

            # none of sub transformers, channel_order, and transformer applied
            sub_chips = rs._get_sub_chips(window, raw=True)
            self.assertEqual(tuple(c.mean() for c in sub_chips),
                             (100, 100, 100))
            chip = rs._get_chip(window)
            self.assertEqual(tuple(chip.reshape(-1, 3).mean(axis=0)),
                             (100, 100, 100))
Example #28
0
    def get_valid_exp_builder(self):
        root_uri = '/some/dummy/root'
        img_path = '/dummy.tif'
        label_path = '/dummy.json'
        backend_conf_path = data_file_path(
            'tf_object_detection/'
            'embedded_ssd_mobilenet_v1_coco.config')

        pretrained_model = ('https://dummy.com/model.gz')

        task = self.get_test_task()

        backend = rv.BackendConfig.builder(rv.TF_OBJECT_DETECTION) \
                                  .with_task(task) \
                                  .with_template(backend_conf_path) \
                                  .with_pretrained_model(pretrained_model) \
                                  .with_train_options(sync_interval=None,
                                                      do_monitoring=False) \
                                  .build()

        raster_source = rv.RasterSourceConfig.builder(rv.GEOTIFF_SOURCE) \
                          .with_uri(img_path) \
                          .with_channel_order([0, 1, 2]) \
                          .with_stats_transformer() \
                          .build()

        scene = rv.SceneConfig.builder() \
                              .with_task(task) \
                              .with_id('od_test') \
                              .with_raster_source(raster_source) \
                              .with_label_source(label_path) \
                              .build()

        dataset = rv.DatasetConfig.builder() \
                                  .with_train_scene(scene) \
                                  .with_validation_scene(scene) \
                                  .build()

        analyzer = rv.analyzer.StatsAnalyzerConfig()

        return rv.ExperimentConfig.builder() \
                               .with_id('object-detection-test') \
                               .with_root_uri(root_uri) \
                               .with_task(task) \
                               .with_backend(backend) \
                               .with_dataset(dataset) \
                               .with_analyzer(analyzer) \
                               .with_train_key('model_name')
Example #29
0
    def test_single_plugin_from_path(self):
        config = {
            'PLUGINS_files':
            '["{}"]'.format(data_file_path('plugins/noop_augmentor.py'))
        }
        rv._registry.initialize_config(config_overrides=config)

        try:
            augmentor = rv.AugmentorConfig.builder('NOOP_AUGMENTOR') \
                                          .build() \
                                          .create_augmentor()

            self.assertIsInstance(augmentor, rv.augmentor.Augmentor)
        finally:
            # Reset config
            rv._registry.initialize_config()
Example #30
0
    def test_custom_default_model_config(self):
        model_defaults_path = data_file_path('custom-model-defaults.json')
        overrides = {'RV_model_defaults_uri': model_defaults_path}
        rv._registry.initialize_config(config_overrides=overrides)

        try:
            b = rv.BackendConfig.builder(rv.KERAS_CLASSIFICATION) \
                                .with_task(self.generate_task()) \
                                .with_model_defaults('CUSTOM_MODEL') \
                                .build()

            expected = 'https://www.azavea.com'
            self.assertEqual(b.pretrained_model_uri, expected)
        finally:
            # Reset the config.
            rv._registry.initialize_config()