detectree2.preprocessing package
Submodules
detectree2.preprocessing.crowns module
detectree2.preprocessing.tiling module
Tiling orthomosaic and crown data.
These functions tile orthomosaics and crown data for training and evaluation of models and making landscape predictions.
- detectree2.preprocessing.tiling.get_features(gdf: GeoDataFrame)
Function to parse features from GeoDataFrame in such a manner that rasterio wants them.
- Args:
gdf: Input geopandas dataframe
- Returns:
json style data
- detectree2.preprocessing.tiling.image_details(fileroot)
Take a filename and split it up to get the coordinates, tile width and the buffer and then output box structure.
- Args:
fileroot: image filename without file extension
- Returns:
Box structure
- detectree2.preprocessing.tiling.is_overlapping_box(test_boxes_array, train_box)
Check if the train box overlaps with any of the test boxes.
- Args:
test_boxes_array: train_box:
- Returns:
Boolean
- detectree2.preprocessing.tiling.load_class_mapping(file_path: str)
Function to load class-to-index mapping from a file.
- Args:
file_path: Path to the file (json or pickle)
- Returns:
class_to_idx: Loaded class-to-index mapping
- detectree2.preprocessing.tiling.process_tile(img_path: str, out_dir: str, buffer: int, tile_width: int, tile_height: int, dtype_bool: bool, minx, miny, crs, tilename, crowns: GeoDataFrame | None = None, threshold: float = 0, nan_threshold: float = 0)
Process a single tile for making predictions.
- Args:
img_path: Path to the orthomosaic out_dir: Output directory buffer: Overlapping buffer of tiles in meters (UTM) tile_width: Tile width in meters tile_height: Tile height in meters dtype_bool: Flag to edit dtype to prevent black tiles minx: Minimum x coordinate of tile miny: Minimum y coordinate of tile crs: Coordinate reference system tilename: Name of the tile
- Returns:
None
- detectree2.preprocessing.tiling.process_tile_ms(img_path: str, out_dir: str, buffer: int, tile_width: int, tile_height: int, dtype_bool: bool, minx, miny, crs, tilename, crowns: GeoDataFrame | None = None, threshold: float = 0, nan_threshold: float = 0)
Process a single tile for making predictions.
- Args:
img_path: Path to the orthomosaic out_dir: Output directory buffer: Overlapping buffer of tiles in meters (UTM) tile_width: Tile width in meters tile_height: Tile height in meters dtype_bool: Flag to edit dtype to prevent black tiles minx: Minimum x coordinate of tile miny: Minimum y coordinate of tile crs: Coordinate reference system tilename: Name of the tile
- Returns:
None
- detectree2.preprocessing.tiling.process_tile_train(img_path: str, out_dir: str, buffer: int, tile_width: int, tile_height: int, dtype_bool: bool, minx, miny, crs, tilename, crowns: GeoDataFrame, threshold, nan_threshold, mode: str = 'rgb', class_column: str | None = None) None
Process a single tile for training data.
- Args:
img_path: Path to the orthomosaic out_dir: Output directory buffer: Overlapping buffer of tiles in meters (UTM) tile_width: Tile width in meters tile_height: Tile height in meters dtype_bool: Flag to edit dtype to prevent black tiles minx: Minimum x coordinate of tile miny: Minimum y coordinate of tile crs: Coordinate reference system tilename: Name of the tile crowns: Crown polygons as a geopandas dataframe threshold: Min proportion of the tile covered by crowns to be accepted {0,1} nan_theshold: Max proportion of tile covered by nans
- Returns:
None
- detectree2.preprocessing.tiling.process_tile_train_helper(args)
- detectree2.preprocessing.tiling.record_classes(crowns: GeoDataFrame, out_dir: str, column: str = 'status', save_format: str = 'json')
Function that records a list of classes into a file that can be read during training.
- Args:
crowns: gpd dataframe with the crowns out_dir: directory to save the file column: column name to get the classes from save_format: format to save the file (‘json’ or ‘pickle’)
- Returns:
None
- detectree2.preprocessing.tiling.tile_data(img_path: str, out_dir: str, buffer: int = 30, tile_width: int = 200, tile_height: int = 200, crowns: GeoDataFrame | None = None, threshold: float = 0, nan_threshold: float = 0.1, dtype_bool: bool = False, mode: str = 'rgb', class_column: str | None = None) None
Tiles up orthomosaic and corresponding crowns (if supplied) into training/prediction tiles.
Tiles up large rasters into managable tiles for training and prediction. If crowns are not supplied the function will tile up the entire landscape for prediction. If crowns are supplied the function will tile these with the image and skip tiles without a minimum coverage of crowns. The ‘threshold’ can be varied to ensure a good coverage of crowns across a traing tile. Tiles that do not have sufficient coverage are skipped.
- Args:
img_path: Path to the orthomosaic out_dir: Output directory buffer: Overlapping buffer of tiles in meters (UTM) tile_width: Tile width in meters tile_height: Tile height in meters crowns: Crown polygons as a geopandas dataframe threshold: Min proportion of the tile covered by crowns to be accepted {0,1} nan_theshold: Max proportion of tile covered by nans dtype_bool: Flag to edit dtype to prevent black tiles
- Returns:
None
- detectree2.preprocessing.tiling.to_traintest_folders(tiles_folder: str = './', out_folder: str = './data/', test_frac: float = 0.2, folds: int = 1, strict: bool = False, seed: int | None = None) None
Send tiles to training (+validation) and test dir
With “strict” it is possible to automatically ensure no overlap between train/val and test tiles.
- Args:
tiles_folder: folder with tiles out_folder: folder to save train and test folders test_frac: fraction of tiles to be used for testing folds: number of folds to split the data into strict: if True, training/validation files will be removed if there is any overlap with test files (inc buffer)
- Returns:
None