Entrypoints
Entrypoints are functions that can be directly called to perform a high-level task. Each of the entrypoints listed below can also be called as a command line script; the scripts accept arguments that are passed straight through to these functions.
For flag-by-flag documentation of the command line form, including which arguments are required, see Command Line Usage.
generate_predictions(raster_folder_path, chip_size, tree_detection_model, chip_stride=None, chip_overlap_percentage=None, use_units_meters=False, region_of_interest=None, resolution=None, output_CRS=None, predictions_save_path=None, view_predictions_plot=False, run_nms=True, iou_threshold=0.3, min_confidence=0.3, batch_size=1, detectree2_weights_path=Path(CHECKPOINTS_FOLDER, '230103_randresize_full.pth'), sam2_checkpoint_path=Path(CHECKPOINTS_FOLDER, 'sam2.1_hiera_large.pt'), sam3_checkpoint_path=Path(CHECKPOINTS_FOLDER, 'sam3.pt'), sam3_huggingface_token=None, detector_kwargs={})
¶
Entrypoint script to generate tree detections for a raster dataset input. Supports visualizing and saving predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster_folder_path
|
PATH_TYPE
|
Path to the folder or raster files. |
required |
chip_size
|
float
|
Dimension of the chip. May be pixels or meters, based on |
required |
chip_stride
|
Optional[float]
|
Stride of the chip. May be pixels or meters, based on |
None
|
tree_detection_model
|
str
|
Selected model for detecting trees. One of "deepforest", "detectree2", "sam2", "sam3", or "tcd". |
required |
chip_overlap_percentage
|
Optional[float]
|
Percent overlap of the chip from 0-100. If used, |
None
|
use_units_meters
|
bool
|
Use units of meters rather than pixels when interpreting the |
False
|
region_of_interest
|
Optional[BOUNDARY_TYPE]
|
Only data from this spatial region will be included in the dataloader. Defaults to None. |
None
|
resolution
|
Optional[float]
|
Spatial resolution the data in meters/pixel. If un-set, will be the resolution of the first raster data that is read. Defaults to None. |
None
|
output_CRS
|
Optional[CRS]
|
(Optional[pyproj.CRS], optional): The coordinate reference system to use for the output data. If un-set, will be the CRS of the first tile found. Defaults to None. |
None
|
predictions_save_path
|
Optional[PATH_TYPE]
|
Path to a geofile to save the prediction outputs. |
None
|
view_predictions_plot
|
bool
|
Set to True if visualization of the detected regions is needed. Defaults to False. |
False
|
run_nms
|
bool
|
(bool, optional): Set to True if non-max suppresion needs to be run on predictions from multiple regions. |
True
|
iou_threshold
|
float
|
What intersection over union value to consider an overlapping detection. Defaults to 0.5. |
0.3
|
min_confidence
|
float
|
Prediction score threshold for detections to be included. |
0.3
|
batch_size
|
int
|
Number of images to load in a batch. Defaults to 1. |
1
|
sam2_checkpoint_path
|
PATH_TYPE
|
Path to the SAM2 checkpoint file. Defaults to checkpoints/sam2.1_hiera_large.pt. |
Path(CHECKPOINTS_FOLDER, 'sam2.1_hiera_large.pt')
|
sam3_checkpoint_path
|
PATH_TYPE
|
Path to a local SAM3 checkpoint file. Defaults to checkpoints/sam3.pt. |
Path(CHECKPOINTS_FOLDER, 'sam3.pt')
|
sam3_huggingface_token
|
str
|
HuggingFace token for downloading SAM3 weights. Only used when sam3_checkpoint_path is None. |
None
|
detector_kwargs
|
dict
|
Optional keyword arguments to be unpacked for the detector constructor. |
{}
|
Source code in tree_detection_framework/entrypoints/generate_predictions.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
detect_trees_two_stage(CHM_file, tree_tops_save_path, tree_crowns_save_path=None, chip_size=CHIP_SIZE, chip_stride=CHIP_STRIDE, resolution=RESOLUTION, raster_blur_sigma=None, edge_suppression_meters=None, tree_top_detector_kwargs={}, crown_segmentation_kwargs={})
¶
Detect trees geometrically and save the detected tree tops and tree crowns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
CHM_file
|
Path
|
Path to a CHM file to detect trees from |
required |
tree_tops_save_path
|
Path
|
Where to save the detected tree tops. |
required |
tree_crowns_save_path
|
Path
|
Where to save the detected tree crowns. If not provided, no crowns will be detected. Defaults to None. |
None
|
chip_size
|
int
|
The size of the chip in pixels. Defaults to CHIP_SIZE. |
CHIP_SIZE
|
chip_stride
|
int
|
The stride of the sliding chip window in pixels. Defaults to CHIP_STRIDE. |
CHIP_STRIDE
|
resolution
|
float
|
The spatial resolution that the CHM is resampled to. Defaults to OUTPUT_RESOLUTION. |
RESOLUTION
|
raster_blur_sigma
|
float
|
The standard deviation of the 2D gaussian kernel, in meters. Defaults to None, no smoothing. |
None
|
tree_top_detector_kwargs
|
dict
|
Keyword arguments to pass to the tree top detector. Defaults to {}. |
{}
|
crown_segmentation_kwargs
|
dict
|
Keyword arguments to pass to the crown segmentation approach. Defaults to {}. |
{}
|
Source code in tree_detection_framework/entrypoints/detect_geometric_two_stage.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
main(image_dir, out_dir, model_key, detectree_checkpoints, chip_size, chip_stride, batch_size)
¶
Detect trees in raw images using a specified model and save detection results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_dir
|
Path
|
Directory containing raw images, or nested subdirectories with raw images in them. Will read all images recusively from this directory. |
required |
out_dir
|
Path
|
Directory to save detection results. Will be created if it does not already exist. The subdirectory structure of image_dir will be mirrored, if there is any. |
required |
model_key
|
str
|
Model to use for detection. |
required |
detectree_checkpoints
|
Path or None
|
Path to detectree2 checkpoints. Required if model_key is 'detectree2'. |
required |
chip_size
|
int
|
Chip size for tiling images. |
required |
chip_stride
|
int
|
Chip stride for tiling images. |
required |
batch_size
|
int
|
Batch size for inference. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an unknown model_key is provided. |
Source code in tree_detection_framework/entrypoints/detect_in_raw_images.py
tile_data(raster_folder_path, chip_size, chip_stride=None, chip_overlap_percentage=None, use_units_meters=False, region_of_interest=None, resolution=None, output_CRS=None, vector_label_folder_path=None, vector_label_attribute=None, visualize_n_tiles=None, save_folder=None, save_n_tiles=None, random_sample=False, batch_size=1)
¶
Entrypoint script for testing preprocessing functions. It enables creating a dataloader, visualizing sample tiles from the dataloader, and saving the contents of the dataloader to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster_folder_path
|
PATH_TYPE
|
Path to the folder or raster files. |
required |
chip_size
|
float
|
Dimension of the chip. May be pixels or meters, based on |
required |
chip_stride
|
Optional[float]
|
Stride of the chip. May be pixels or meters, based on |
None
|
chip_overlap_percentage
|
Optional[float]
|
Percent overlap of the chip from 0-100. If used, |
None
|
use_units_meters
|
bool
|
Use units of meters rather than pixels when interpreting the |
False
|
region_of_interest
|
Optional[BOUNDARY_TYPE]
|
Only data from this spatial region will be included in the dataloader. Defaults to None. |
None
|
resolution
|
Optional[float]
|
Spatial resolution the data in meters/pixel. If un-set, will be the resolution of the first raster data that is read. Defaults to None. |
None
|
output_CRS
|
Optional[CRS]
|
(Optional[pyproj.CRS], optional): The coordinate reference system to use for the output data. If un-set, will be the CRS of the first tile found. Defaults to None. |
None
|
vector_label_folder_path
|
Optional[PATH_TYPE]
|
A folder of geospatial vector files that will be used for the label. If un-set, the dataloader will not be labeled. Defaults to None. |
None
|
vector_label_attribute
|
Optional[str]
|
Attribute to read from the vector data, such as the class or instance ID. Defaults to None. |
None
|
visualize_n_tiles
|
int
|
The number of randomly-sampled tiles to display. |
None
|
save_folder
|
Optional[PATH_TYPE]
|
Folder to save data to. Will be created if it doesn't exist. |
None
|
save_n_tiles
|
Optional[int]
|
Number of tiles to save. Whether they are the first tiles or random is controlled by
|
None
|
random_sample
|
bool
|
If |
False
|
batch_size
|
int
|
Number of images to load in a batch. Defaults to 1. |
1
|
Source code in tree_detection_framework/entrypoints/tile_data.py
visualize_detection_sets(image_dir, detection_dir, out_dir, n_images, step, show_centroid, image_ext)
¶
Visualize detection results by pairing images and detection files, sampling according to n_images and step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_dir
|
Path
|
Directory containing raw images. |
required |
detection_dir
|
Path
|
Directory containing detection .gpkg files. |
required |
out_dir
|
Path
|
Directory to save visualizations. |
required |
n_images
|
int
|
Number of images to visualize. |
required |
step
|
int
|
Step size between visualized images. |
required |
show_centroid
|
bool
|
Whether to show centroid in visualization. |
required |
image_ext
|
str
|
Extension of image files (e.g., 'JPG', 'tif'). |
required |
Returns: None. Saves files in the output directory.
Source code in tree_detection_framework/entrypoints/visualize_detections.py
detect_trees(detector, detection_params, detector_dir, preprocessed_local_files)
¶
Detect trees using the specified detector and save raw detections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
detector
|
str
|
Detector name: geometric, deepforest, detectree2, sam2, sam3, or tcd. |
required |
detection_params
|
dict
|
Detection parameters (chip_size, chip_stride, resolution, batch_size, detectree2_weights_path, sam3_checkpoint_path, etc.). |
required |
detector_dir
|
Path
|
Directory for detector outputs. raw_detections.gpkg is written here. |
required |
preprocessed_local_files
|
dict
|
Preprocessed local file paths (should have keys "ortho" and "chm"). |
required |
Source code in tree_detection_framework/entrypoints/detect_trees.py
postprocess(postprocessing_id, chm_path, min_tree_height, postproc_config_path, detector_dir=None, raw_detections_path=None, output_path=None)
¶
Apply postprocessing chain to raw tree detections and save final detections as detections.gpkg in detector_dir. This script is meant to be run after detect_trees.py.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
postprocessing_id
|
str
|
Postprocessing config ID that corresponds to a chain of postprocessing steps
defined in |
required |
chm_path
|
str
|
Path to the CHM raster (used for height filtering and polygon-to-point conversion). |
required |
min_tree_height
|
float
|
Minimum tree height (meters) used to filter detections. |
required |
postproc_config_path
|
Path
|
Path to postprocessing_config.yaml. |
required |
detector_dir
|
Path | None
|
Directory containing raw_detections.gpkg. detections.gpkg is written here. Used to derive raw_detections_path and output_path if they are not provided. |
None
|
raw_detections_path
|
Path | None
|
Path to raw_detections.gpkg. If not provided, derived from detector_dir. |
None
|
output_path
|
Path | None
|
Path to write detections.gpkg. If not provided, derived from detector_dir. |
None
|