Skip to content

Commit

Permalink
Cleanup more cases of potential circular impots
Browse files Browse the repository at this point in the history
  • Loading branch information
plstcharles committed Nov 6, 2019
1 parent b88917b commit a369448
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
9 changes: 6 additions & 3 deletions thelper/data/geo/gdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@
"""

import collections
import logging
import os

import h5py
import numpy as np

import thelper.data
import thelper.nn.coordconv
from thelper.data.parsers import SegmentationDataset

logger = logging.getLogger(__name__)

class SegmentationDataset(thelper.data.parsers.SegmentationDataset):

class SegmentationDataset(SegmentationDataset):
"""Semantic segmentation dataset interface for GDL-based HDF5 parsing."""

def __init__(self, class_names, work_folder, dataset_type, max_sample_count=None,
dontcare=None, transforms=None):
self.dontcare = dontcare
if isinstance(dontcare, (tuple, list)) and len(dontcare) == 2:
thelper.data.logger.warning(f"will remap dontcare index from {dontcare[0]} to {dontcare[1]}")
logger.warning(f"will remap dontcare index from {dontcare[0]} to {dontcare[1]}")
dontcare = dontcare[1]
assert dontcare is None or isinstance(dontcare, int), "unexpected dontcare type"
super().__init__(class_names=class_names, input_key="sat_img", label_map_key="map_img",
Expand Down
11 changes: 6 additions & 5 deletions thelper/data/geo/ogc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import numpy as np
import tqdm

import thelper.data
import thelper.train.utils
import thelper.tasks
import thelper.utils
from thelper.data.geo.parsers import TileDataset, VectorCropDataset
from thelper.train.utils import DetectLogger

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -354,7 +355,7 @@ def __getitem__(self, idx):
return sample


class TB15D104DetectLogger(thelper.train.utils.DetectLogger):
class TB15D104DetectLogger(DetectLogger):

def __init__(self, conf_threshold=0.5):
super().__init__(conf_threshold=conf_threshold, target_name="lake",
Expand Down Expand Up @@ -407,10 +408,10 @@ def postproc_features(input_file, bboxes_srs, orig_geoms_path, output_file,
bboxes_srs = bboxes_srs_obj
logger.debug("importing lake bboxes geojson...")
with open(input_file) as bboxes_fd:
bboxes_geoms = thelper.data.geo.utils.parse_geojson(json.load(bboxes_fd))
bboxes_geoms = geo.utils.parse_geojson(json.load(bboxes_fd))
logger.debug("importing hydro features geojson...")
with open(orig_geoms_path) as hydro_fd:
hydro_geoms = thelper.data.geo.utils.parse_geojson(json.load(hydro_fd), srs_target=bboxes_srs)
hydro_geoms = geo.utils.parse_geojson(json.load(hydro_fd), srs_target=bboxes_srs)
logger.debug("computing global cascade of lake bboxes...")
detect_roi = shapely.ops.cascaded_union([bbox["geometry"] for bbox in bboxes_geoms])
output_features = []
Expand Down
13 changes: 10 additions & 3 deletions thelper/data/geo/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import shapely
import tqdm

import thelper.data
import thelper.data.geo as geo
import thelper.tasks
import thelper.utils
from thelper.data import Dataset

logger = logging.getLogger(__name__)


class VectorCropDataset(thelper.data.Dataset):
class VectorCropDataset(Dataset):
"""Abstract dataset used to combine geojson vector data and rasters."""

def __init__(self, raster_path, vector_path, px_size=None, skew=None,
Expand All @@ -32,6 +33,7 @@ def __init__(self, raster_path, vector_path, px_size=None, skew=None,
cleaner=None, cropper=None, force_parse=False,
reproj_rasters=False, reproj_all_cpus=True,
keep_rasters_open=True, transforms=None):
import thelper.data.geo as geo
# before anything else, create a hash to cache parsed data
cache_hash = thelper.utils.get_params_hash(
{k: v for k, v in vars().items() if not k.startswith("_") and k != "self"}) if not force_parse else None
Expand Down Expand Up @@ -130,6 +132,7 @@ def _default_feature_cleaner(features, area_min, area_max, target_prop=None):
def _default_feature_cropper(features, rasters_data, coverage, srs_target, px_size, skew, feature_buffer):
"""Returns the samples for a set of features (may be modified in derived classes)."""
# note: default behavior = just center on the feature, and pad if required by user
import thelper.data.geo as geo
samples = []
clean_feats = [f for f in features if f["clean"]]
srs_target_wkt = srs_target.ExportToWkt()
Expand Down Expand Up @@ -165,6 +168,7 @@ def _default_feature_cropper(features, rasters_data, coverage, srs_target, px_si
@staticmethod
def _parse_rasters(path, srs, reproj_rasters):
"""Parses rasters (geotiffs) and returns metadata/coverage information."""
import thelper.data.geo as geo
logger.info(f"parsing rasters from path '{path}'...")
raster_paths = thelper.utils.get_file_paths(path, ".", allow_glob=True)
rasters_data, coverage = geo.utils.parse_rasters(raster_paths, srs, reproj_rasters)
Expand All @@ -186,6 +190,7 @@ def _parse_rasters(path, srs, reproj_rasters):
@staticmethod
def _parse_features(path, srs, roi, cache_hash, allow_outlying, clip_outlying, cleaner):
"""Parses vector files (geojsons) and returns geometry information."""
import thelper.data.geo as geo
logger.info(f"parsing vectors from path '{path}'...")
assert os.path.isfile(path) and path.endswith("geojson"), \
"vector file must be provided as geojson (shapefile support still incomplete)"
Expand Down Expand Up @@ -231,6 +236,7 @@ def _parse_crops(self, cropper, cache_file_path, cache_hash):
def _process_crop(self, sample):
"""Returns a crop for a specific (internal) set of sampled features."""
# remember: we assume that all rasters have the same intrinsic settings
import thelper.data.geo as geo
crop_datatype = geo.utils.GDAL2NUMPY_TYPE_CONV[self.rasters_data[0]["data_type"]]
crop_size = (sample["crop_height"], sample["crop_width"], self.rasters_data[0]["band_count"])
crop = np.ma.array(np.zeros(crop_size, dtype=crop_datatype), mask=np.ones(crop_size, dtype=np.uint8))
Expand Down Expand Up @@ -341,6 +347,7 @@ def _tile_cropper(features, rasters_data, coverage, srs_target, tile_size, tile_
"""Returns the ROI information for a given feature (may be modified in derived classes)."""
# instead of iterating over features to generate samples, we tile the raster(s)
# note: the 'coverage' geometry should already be in the target srs
import thelper.data.geo as geo
roi_tl, roi_br = geo.utils.get_feature_bbox(coverage)
roi_geotransform = (roi_tl[0], px_size[0], 0.0,
roi_tl[1], 0.0, px_size[1])
Expand Down

0 comments on commit a369448

Please sign in to comment.