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)
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)
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_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)
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(raster_source=gt_rs) 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(raster_source=pred_rs) 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)
def test_vector_evaluator_with_aoi(self): output_uri = join(self.tmp_dir.name, 'raster-out.json') vector_output_uri = join(self.tmp_dir.name, 'vector-out.json') scenes = [self.get_vector_scene(0, use_aoi=True)] evaluator = SemanticSegmentationEvaluator(self.class_config, output_uri, vector_output_uri) evaluator.process(scenes, self.tmp_dir.name) vector_eval_json = file_to_json(vector_output_uri) exp_vector_eval_json = file_to_json( data_file_path('expected-vector-eval-with-aoi.json')) # NOTE: The precision and recall values found in the file # `expected-vector-eval.json` are equal to fractions of the # form (n-1)/n for n <= 7 which can be seen to be (and have # been manually verified to be) correct. self.assertDictEqual(vector_eval_json, exp_vector_eval_json)
def test_shift_x(self): # Specially-engineered image w/ one meter per pixel resolution # in the x direction. img_path = data_file_path('ones.tif') channel_order = [0] config = RasterioSourceConfig(uris=[img_path], channel_order=channel_order, x_shift=1.0, y_shift=0.0) source = config.build(tmp_dir=self.tmp_dir) with source.activate(): extent = source.get_extent() data = source.get_chip(extent) self.assertEqual(data.sum(), 2**16 - 256) column = data[:, 255, 0] self.assertEqual(column.sum(), 0)
def test_gets_raw_chip_from_uint16_transformed_proto(self): img_path = data_file_path('small-uint16-tile.tif') channel_order = [0, 1] config = RasterioSourceConfig(uris=[img_path]) raw_rs = config.build(tmp_dir=self.tmp_dir) stats_uri = join(self.tmp_dir, 'tmp.tif') stats = RasterStats() stats.compute([raw_rs]) stats.save(stats_uri) transformer = StatsTransformerConfig(stats_uri=stats_uri) config = RasterioSourceConfig(uris=[img_path], channel_order=channel_order, transformers=[transformer]) rs = config.build(tmp_dir=self.tmp_dir) with rs.activate(): out_chip = rs.get_raw_image_array() self.assertEqual(out_chip.shape[2], 3)
def test_get_dtype(self): img_path = data_file_path('small-rgb-tile.tif') config = RasterioSourceConfig(uris=[img_path]) source = config.build(tmp_dir=self.tmp_dir) self.assertEqual(source.get_dtype(), np.uint8)