Skip to content

Commit

Permalink
Merge pull request #63 from VIDA-NYU/blackgrid
Browse files Browse the repository at this point in the history
Fix black tile size and remove tile_step from BaseGrid
  • Loading branch information
Mary-h86 authored Apr 18, 2024
2 parents 96cab4c + 827e0a7 commit c75ed16
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
11 changes: 7 additions & 4 deletions examples/inference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
},
{
Expand All @@ -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. "
]
},
{
Expand Down Expand Up @@ -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."
]
},
{
Expand All @@ -227,7 +230,7 @@
},
"outputs": [],
"source": [
"raster.inference()"
"raster.inference(dump_percent=20)"
]
}
],
Expand Down
14 changes: 5 additions & 9 deletions src/tile2net/raster/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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):
"""
Expand Down
3 changes: 3 additions & 0 deletions src/tile2net/raster/pednet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/tile2net/raster/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
3 changes: 2 additions & 1 deletion src/tile2net/raster/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -959,3 +959,4 @@ def extension(self):
)
raster.generate(2)
raster.inference()

2 changes: 1 addition & 1 deletion src/tile2net/tileseg/inference/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 3 additions & 1 deletion src/tile2net/tileseg/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c75ed16

Please sign in to comment.