diff --git a/examples/inference.ipynb b/examples/inference.ipynb index ff0ee81..d823bd1 100644 --- a/examples/inference.ipynb +++ b/examples/inference.ipynb @@ -78,7 +78,8 @@ "collapsed": false }, "source": [ - "You may also instantiate a Raster using a local directory of tiles, given that an arbitrary directory structure e.g. `path/to/tiles/z/x/y.ext` or `path/to/tiles/z/x_y.ext` is defined. The `z` for `zoom` may or may not be included in the path, but `zoom` must be passed to the Raster arguments." + "You may also instantiate a Raster using a local directory of tiles, given that an arbitrary directory structure e.g. `path/to/tiles/z/x/y.ext` or `path/to/tiles/z/x_y.ext` is defined. \n", + "The `z` for `zoom` may or may not be included in the path, but `zoom` must be passed to the Raster arguments." ] }, { @@ -104,7 +105,8 @@ "collapsed": false }, "source": [ - "Without specifying `input_dir`, Raster will determine the most suitable tile source for the location:" + "Without specifying `input_dir`, Raster will determine the most suitable tile source for the location, if the region is suppored. \n", + "If the region is not supported, you can still create a Raster object for your region to work with for some basic tasks but you wont be able to download tile and generate network. " ] }, { @@ -216,7 +218,8 @@ }, "source": [ "### Inference\n", - "Now that the tiles have been stitched together, we can run an inference on the stitched images." + "Now that the tiles have been stitched together, we can run an inference on the stitched images. \n", + "You can use the dump_percent parameter to specify the percentage of segmentation images to save to the subdirectory 'segmentation/seg_results'. 100 means all, 0 means none. The default is 0." ] }, { @@ -227,7 +230,7 @@ }, "outputs": [], "source": [ - "raster.inference()" + "raster.inference(dump_percent=20)" ] } ], diff --git a/src/tile2net/raster/grid.py b/src/tile2net/raster/grid.py index 1e40f48..c8d0127 100644 --- a/src/tile2net/raster/grid.py +++ b/src/tile2net/raster/grid.py @@ -134,7 +134,6 @@ class BaseGrid(BaseRegion): zoom: int = field(default=19) base_tilesize: int = field(default=256) padding: bool = field(default=True, repr=False) - tile_step: int = field(default=1, repr=False) tiles: np.ndarray = field(default_factory=lambda: np.array(Tile), init=False, repr=False) def __post_init__(self): @@ -148,18 +147,18 @@ def __post_init__(self): self.xtile + col_idx, self.ytile + row_idx, position=( - int(col_idx / self.tile_step), int(row_idx / self.stitch_step) + int(col_idx), int(row_idx) ), idd=self.pos2id( - int(col_idx / self.tile_step), int(row_idx / self.stitch_step) + int(col_idx), int(row_idx) ), zoom=self.zoom, - size=self.tile_size, + size=self.base_tilesize, crs=self.crs ) - for row_idx in np.arange(0, self.base_height, self.tile_step) + for row_idx in np.arange(0, self.base_height) ] - for col_idx in np.arange(0, self.base_width, self.tile_step) + for col_idx in np.arange(0, self.base_width) ]) self.pose_dict = {tile.idd: tile.position for col in self.tiles for tile in col} # due to the rounding issues with deg2num and num2deg, we do this calculation again @@ -185,9 +184,6 @@ def base_height(self): def base_width(self): return abs(self.xtilem - self.xtile) + 1 # horizontal tiles (#columns) - @cached_property - def tile_size(self): - return self.base_tilesize * self.stitch_step def pos2id(self, col_idx: int, row_idx: int): """ diff --git a/src/tile2net/raster/pednet.py b/src/tile2net/raster/pednet.py index 916f0c4..0b01b9c 100644 --- a/src/tile2net/raster/pednet.py +++ b/src/tile2net/raster/pednet.py @@ -154,6 +154,7 @@ def create_crosswalk(self): cw_explode = cw_union.explode().reset_index(drop=True) cw_explode = cw_explode[cw_explode.geometry.notna()].reset_index(drop=True) cw_explode_ = morpho_atts(cw_explode) + # polak = [] for c, geom in enumerate(cw_explode_.geometry): if geom.area < 5: continue @@ -163,6 +164,7 @@ def create_crosswalk(self): if cw_explode_.iloc[c, cw_explode_.columns.get_loc(cw_explode_.convexity.name)] < 0.8: av_width = 4 * geom.area / geom.length geom_er = geom.buffer(-av_width / 4) + # polak.append(geom_er) if geom_er.type == "MultiPolygon": for g in list(geom_er.geoms): # shapely 2 if g.area > 2: @@ -197,6 +199,7 @@ def create_crosswalk(self): else: continue else: + # polak.append(geom) line = get_crosswalk_cnl(geom) if line.length < 8: extended = self.make_longer(line, 0.8) diff --git a/src/tile2net/raster/project.py b/src/tile2net/raster/project.py index c1177ff..8f25fe6 100644 --- a/src/tile2net/raster/project.py +++ b/src/tile2net/raster/project.py @@ -187,7 +187,6 @@ def files(self, tiles: ndarray = None) -> Iterator[str]: if tiles is None: tiles = self.project.raster.tiles path = self.path - path.mkdir(parents=True, exist_ok=True) path = path.__fspath__() R, C = np.meshgrid( np.arange(tiles.shape[0]), diff --git a/src/tile2net/raster/raster.py b/src/tile2net/raster/raster.py index d0767f1..e3995d9 100644 --- a/src/tile2net/raster/raster.py +++ b/src/tile2net/raster/raster.py @@ -72,7 +72,7 @@ def __fspath__(self): @cached_property def array(self): - return np.zeros((self.owner.base_tilesize, self.owner.base_tilesize, 3), dtype=np.uint8) + return np.zeros((self.owner.tile_size, self.owner.tile_size, 3), dtype=np.uint8) class Raster(Grid): @@ -959,3 +959,4 @@ def extension(self): ) raster.generate(2) raster.inference() + diff --git a/src/tile2net/tileseg/inference/inference.py b/src/tile2net/tileseg/inference/inference.py index 4667494..fd61155 100644 --- a/src/tile2net/tileseg/inference/inference.py +++ b/src/tile2net/tileseg/inference/inference.py @@ -451,7 +451,7 @@ class LocalInference(Inference): def validate(self, *args, grid: Raster, **kwargs): # as a temporary solution just assign the segmentation path to the tile - grid.project.resources.segmentation.path.mkdir(exist_ok=True, parents=True) + # grid.project.resources.segmentation.path.mkdir(exist_ok=True, parents=True) for segmentation, tile in zip( grid.project.resources.segmentation.files(), grid.tiles.ravel(), diff --git a/src/tile2net/tileseg/utils/misc.py b/src/tile2net/tileseg/utils/misc.py index 5d1bbaf..81b6a33 100644 --- a/src/tile2net/tileseg/utils/misc.py +++ b/src/tile2net/tileseg/utils/misc.py @@ -373,6 +373,7 @@ def get_dump_assets(self, dump_dict, img_name, idx, colorize_mask_fn, to_tensorb mask = mask.astype(np.uint8) mask_pil = Image.fromarray(mask) mask_pil = mask_pil.convert('RGB') + os.makedirs(self.save_dir, exist_ok=True) mask_pil.save(mask_fn) to_tensorboard.append(self.visualize(mask_pil)) @@ -578,7 +579,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.futures: list[Future] = [] self.threads = ThreadPoolExecutor() - os.makedirs(self.save_dir, exist_ok=True) + # os.makedirs(self.save_dir, exist_ok=True) def dump(self, dump_dict, val_idx, testing=None, grid=None): @@ -665,6 +666,7 @@ def create_composite_image(self, input_image, prediction_pil, img_name): composited.paste(prediction_pil, (prediction_pil.width, 0)) composited_fn = 'sidebside_{}.png'.format(img_name) composited_fn = os.path.join(self.save_dir, composited_fn) + os.makedirs(self.save_dir, exist_ok=True) # print(f'saving {composited_fn}') # composited.save(composited_fn) future = threads.submit(composited.save, composited_fn)