Skip to content

canopy module

Canopy height estimation module using Meta's HighResCanopyHeight model.

This module provides canopy height estimation from RGB imagery using DINOv2 backbone with DPT decoder, based on Meta's HighResCanopyHeight research (https://github.com/facebookresearch/HighResCanopyHeight).

Reference

Tolan et al., "Very high resolution canopy height maps from RGB imagery using self-supervised vision transformer and convolutional decoder trained on Aerial Lidar," Remote Sensing of Environment, 2024. https://doi.org/10.1016/j.rse.2023.113888

CanopyHeightEstimation

Estimate canopy height from RGB imagery using Meta's HighResCanopyHeight model.

This class provides canopy height estimation from RGB aerial or satellite imagery using a DINOv2 backbone with DPT decoder architecture. The model predicts canopy height in meters for each pixel.

The model was developed by Meta AI Research (FAIR) and is described in: Tolan et al., "Very high resolution canopy height maps from RGB imagery using self-supervised vision transformer and convolutional decoder trained on Aerial Lidar," Remote Sensing of Environment, 2023.

Attributes:

Name Type Description
model_name str

Name of the model variant being used.

device str

Device the model is running on.

Example

estimator = CanopyHeightEstimation() estimator.predict("input.tif", output_path="canopy_height.tif")

Source code in geoai/canopy.py
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
class CanopyHeightEstimation:
    """Estimate canopy height from RGB imagery using Meta's HighResCanopyHeight model.

    This class provides canopy height estimation from RGB aerial or satellite
    imagery using a DINOv2 backbone with DPT decoder architecture. The model
    predicts canopy height in meters for each pixel.

    The model was developed by Meta AI Research (FAIR) and is described in:
    Tolan et al., "Very high resolution canopy height maps from RGB imagery
    using self-supervised vision transformer and convolutional decoder trained
    on Aerial Lidar," Remote Sensing of Environment, 2023.

    Attributes:
        model_name (str): Name of the model variant being used.
        device (str): Device the model is running on.

    Example:
        >>> estimator = CanopyHeightEstimation()
        >>> estimator.predict("input.tif", output_path="canopy_height.tif")
    """

    def __init__(
        self,
        model_name: str = "compressed_SSLhuge",
        checkpoint_path: Optional[str] = None,
        device: Optional[str] = None,
        cache_dir: str = DEFAULT_CACHE_DIR,
    ):
        """Initialize the CanopyHeightEstimation model.

        Args:
            model_name: Model variant to use. Options:
                - "compressed_SSLhuge" (default): Compressed huge model (749M),
                  CPU-friendly. Best general-purpose model.
                - "SSLhuge_satellite": Full huge model (2.9G), GPU required.
                  Best results on satellite imagery.
                - "compressed_SSLlarge": Compressed large model (400M), CPU-friendly.
                - "compressed_SSLhuge_aerial": Compressed huge model fine-tuned on
                  aerial imagery (749M), CPU-friendly.
            checkpoint_path: Optional path to a local checkpoint file.
                If None, the checkpoint will be downloaded automatically.
            device: Device to run inference on ('cpu', 'cuda', 'cuda:0', etc.).
                If None, automatically selects CUDA if available.  Compressed
                (quantized) models are loaded without quantization when placed
                on GPU, which is faster than CPU inference for large images.
            cache_dir: Directory to cache downloaded model checkpoints.
                Defaults to ~/.cache/geoai/canopy/.
        """
        self.model_name = model_name

        if model_name not in MODEL_VARIANTS:
            raise ValueError(
                f"Unknown model variant '{model_name}'. "
                f"Available: {list(MODEL_VARIANTS.keys())}"
            )

        info = MODEL_VARIANTS[model_name]

        # Determine device – prefer CUDA when available for all model variants
        if device is None:
            if torch.cuda.is_available():
                device = "cuda"
            else:
                device = "cpu"

        self.device = device

        # Download or use provided checkpoint
        if checkpoint_path is None:
            checkpoint_path = _download_checkpoint(model_name, cache_dir)
        self.checkpoint_path = checkpoint_path

        # Load model
        print(f"Loading {model_name} model on {device}...")
        self.model = _load_model(model_name, checkpoint_path, device)
        self.model.eval()
        print("Model loaded successfully.")

        # Image normalization (from Meta's inference.py)
        self._norm_mean = [0.420, 0.411, 0.296]
        self._norm_std = [0.213, 0.156, 0.143]

    def _normalize_image(self, img_tensor: torch.Tensor) -> torch.Tensor:
        """Apply normalization to an image tensor.

        Args:
            img_tensor: Image tensor of shape (B, C, H, W) with values in [0, 1].

        Returns:
            Normalized image tensor.
        """
        mean = torch.tensor(self._norm_mean, device=img_tensor.device).view(1, 3, 1, 1)
        std = torch.tensor(self._norm_std, device=img_tensor.device).view(1, 3, 1, 1)
        return (img_tensor - mean) / std

    @staticmethod
    def _make_weight_map(tile_size: int, overlap: int) -> np.ndarray:
        """Create a 2D raised-cosine weight map for smooth tile blending.

        Produces a weight map that is 1.0 in the non-overlapping centre and
        tapers smoothly to 0 at the edges using a cosine ramp over the
        overlap region.  When multiple overlapping tiles are accumulated
        with these weights the result is seamless.

        Args:
            tile_size: Size of each square tile.
            overlap: Number of pixels that overlap between adjacent tiles.

        Returns:
            2D float32 array of shape (tile_size, tile_size).
        """
        if overlap <= 0:
            return np.ones((tile_size, tile_size), dtype=np.float32)

        ramp = np.linspace(0.0, 1.0, overlap, dtype=np.float32)
        ramp = 0.5 * (1.0 - np.cos(np.pi * ramp))  # raised cosine

        w = np.ones(tile_size, dtype=np.float32)
        w[:overlap] = ramp
        w[-overlap:] = ramp[::-1]

        return np.outer(w, w)

    def predict(
        self,
        input_path: str,
        output_path: Optional[str] = None,
        tile_size: int = 256,
        overlap: int = 128,
        batch_size: int = 4,
        scale_factor: float = 10.0,
        **kwargs,
    ) -> np.ndarray:
        """Predict canopy height from a GeoTIFF image.

        Processes the input image in tiles of the specified size, runs
        inference on each tile, and assembles the results into a full
        canopy height map.  Adjacent tiles overlap and are blended using
        raised-cosine weights for seamless output.

        Args:
            input_path: Path to the input GeoTIFF file (RGB imagery).
            output_path: Optional path to save the output canopy height
                map as a GeoTIFF. If None, only returns the numpy array.
            tile_size: Size of tiles for processing (default: 256).
                The model expects 256x256 tiles.
            overlap: Number of pixels of overlap between tiles (default: 128).
                Using overlap with blending weights eliminates tile-boundary
                artefacts.  Higher values (up to tile_size // 2) give
                smoother results at the cost of more computation.
            batch_size: Number of tiles to process at once (default: 4).
                Larger values use more memory but process faster.
            scale_factor: Factor to multiply model output by to get height
                in meters (default: 10.0). This is set by the original model.
            **kwargs: Additional keyword arguments (reserved for future use).

        Returns:
            numpy.ndarray: 2D array of canopy height values in meters.

        Raises:
            FileNotFoundError: If the input file does not exist.
            ValueError: If the input file is not a valid GeoTIFF.

        Example:
            >>> estimator = CanopyHeightEstimation()
            >>> heights = estimator.predict("aerial_image.tif",
            ...                              output_path="canopy_height.tif",
            ...                              overlap=128)
        """
        if not os.path.exists(input_path):
            raise FileNotFoundError(f"Input file not found: {input_path}")

        if overlap < 0 or overlap >= tile_size:
            raise ValueError(
                f"overlap must be >= 0 and < tile_size ({tile_size}), got {overlap}"
            )

        with rasterio.open(input_path) as src:
            # Read the image
            img = src.read()  # (bands, height, width)
            profile = src.profile.copy()
            height, width = src.height, src.width

            # Handle different band counts
            if img.shape[0] >= 3:
                img = img[:3]  # Use first 3 bands (RGB)
            else:
                raise ValueError(
                    f"Input image has {img.shape[0]} bands, but at least 3 (RGB) are required."
                )

            # Convert to float32 in [0, 1]
            if img.dtype == np.uint8:
                img = img.astype(np.float32) / 255.0
            elif img.dtype == np.uint16:
                img = img.astype(np.float32) / 65535.0
            elif img.dtype in (np.float32, np.float64):
                img = img.astype(np.float32)
                # Clip to [0, 1] if needed
                img_max = img.max()
                if img_max > 1.0:
                    img = img / img_max
            else:
                img = img.astype(np.float32)
                img_max = img.max()
                if img_max > 0:
                    img = img / img_max

        # Calculate tile grid
        step = tile_size - overlap
        n_rows = max(1, math.ceil((height - overlap) / step))
        n_cols = max(1, math.ceil((width - overlap) / step))

        # Pad image to fit tile grid
        padded_h = n_rows * step + overlap
        padded_w = n_cols * step + overlap
        padded_img = np.zeros((3, padded_h, padded_w), dtype=np.float32)
        padded_img[:, :height, :width] = img

        # Blending weight map (raised-cosine taper in overlap regions)
        weight_map = self._make_weight_map(tile_size, overlap)

        # Output arrays
        output = np.zeros((padded_h, padded_w), dtype=np.float32)
        count = np.zeros((padded_h, padded_w), dtype=np.float32)

        # Process tiles in batches
        tiles = []
        positions = []
        for row in range(n_rows):
            for col in range(n_cols):
                y = row * step
                x = col * step
                tile = padded_img[:, y : y + tile_size, x : x + tile_size]
                tiles.append(tile)
                positions.append((y, x))

                if len(tiles) == batch_size:
                    self._process_batch(
                        tiles, positions, output, count, scale_factor, weight_map
                    )
                    tiles = []
                    positions = []

        # Process remaining tiles
        if tiles:
            self._process_batch(
                tiles, positions, output, count, scale_factor, weight_map
            )

        # Average overlapping regions
        count[count == 0] = 1
        output = output / count

        # Crop back to original size
        result = output[:height, :width]

        # Save output if requested
        if output_path is not None:
            out_profile = profile.copy()
            out_profile.update(
                dtype="float32",
                count=1,
                compress="lzw",
                nodata=0,
            )
            # Remove alpha band related settings
            if "photometric" in out_profile:
                del out_profile["photometric"]

            with rasterio.open(output_path, "w", **out_profile) as dst:
                dst.write(result, 1)
            print(f"Canopy height map saved to: {output_path}")

        return result

    def _process_batch(
        self,
        tiles: list,
        positions: list,
        output: np.ndarray,
        count: np.ndarray,
        scale_factor: float,
        weight_map: Optional[np.ndarray] = None,
    ):
        """Process a batch of tiles through the model.

        Args:
            tiles: List of tile arrays (C, H, W).
            positions: List of (y, x) positions for each tile.
            output: Output array to accumulate predictions.
            count: Count array for averaging overlapping tiles.
            scale_factor: Scale factor for model output.
            weight_map: Optional 2D weight array for blending overlapping
                tiles.  If None, uniform weights (1.0) are used.
        """
        tile_size = tiles[0].shape[1]
        batch = torch.from_numpy(np.stack(tiles)).to(self.device)
        batch = self._normalize_image(batch)

        with torch.no_grad():
            pred = self.model(batch)
            pred = pred * scale_factor
            pred = pred.relu()

        pred = pred.cpu().numpy()

        if weight_map is None:
            weight_map = np.ones((tile_size, tile_size), dtype=np.float32)

        for i, (y, x) in enumerate(positions):
            output[y : y + tile_size, x : x + tile_size] += pred[i, 0] * weight_map
            count[y : y + tile_size, x : x + tile_size] += weight_map

    def visualize(
        self,
        input_path: str,
        height_map: Optional[np.ndarray] = None,
        output_path: Optional[str] = None,
        figsize: Tuple[int, int] = (16, 6),
        cmap: str = "viridis",
        vmin: float = 0,
        vmax: Optional[float] = None,
        title: Optional[str] = None,
        **kwargs,
    ) -> plt.Figure:
        """Visualize the input image alongside the canopy height map.

        Args:
            input_path: Path to the input GeoTIFF image, or path to the
                canopy height GeoTIFF if height_map is None.
            height_map: Canopy height map array. If None, will attempt to
                read from input_path (assuming it's a height map).
            output_path: Optional path to save the figure.
            figsize: Figure size as (width, height).
            cmap: Colormap for the height map.
            vmin: Minimum value for colormap.
            vmax: Maximum value for colormap. If None, uses the 98th
                percentile of the height values.
            title: Optional title for the figure.
            **kwargs: Additional keyword arguments for matplotlib.

        Returns:
            matplotlib.figure.Figure: The visualization figure.

        Example:
            >>> estimator = CanopyHeightEstimation()
            >>> heights = estimator.predict("input.tif")
            >>> fig = estimator.visualize("input.tif", heights)
        """
        if height_map is None:
            # Assume input_path is the height map
            with rasterio.open(input_path) as src:
                height_map = src.read(1)
            fig, ax = plt.subplots(1, 1, figsize=(figsize[0] // 2, figsize[1]))
            if vmax is None:
                vmax = (
                    np.percentile(height_map[height_map > 0], 98)
                    if np.any(height_map > 0)
                    else 1
                )
            im = ax.imshow(height_map, cmap=cmap, vmin=vmin, vmax=vmax)
            ax.set_title(title or "Canopy Height Map")
            ax.set_xlabel("Pixels")
            ax.set_ylabel("Pixels")
            cbar = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
            cbar.set_label("Height (meters)")
            ax.axis("off")
        else:
            # Show input image and height map side by side
            with rasterio.open(input_path) as src:
                img = src.read()
                if img.shape[0] >= 3:
                    img = img[:3]
                if img.dtype == np.uint8:
                    img_display = np.moveaxis(img, 0, 2)
                elif img.dtype == np.uint16:
                    img_float = img.astype(np.float32)
                    img_max = img_float.max()
                    if img_max > 0:
                        img_float = img_float / img_max
                    img_display = np.moveaxis(img_float, 0, 2)
                else:
                    img_float = img.astype(np.float32)
                    img_max = img_float.max()
                    if img_max > 1.0:
                        img_float = img_float / img_max
                    img_display = np.moveaxis(img_float, 0, 2)

            fig, (ax1, ax2) = plt.subplots(1, 2, figsize=figsize)

            ax1.imshow(img_display)
            ax1.set_title("Input Image")
            ax1.axis("off")

            if vmax is None:
                vmax = (
                    np.percentile(height_map[height_map > 0], 98)
                    if np.any(height_map > 0)
                    else 1
                )
            im = ax2.imshow(height_map, cmap=cmap, vmin=vmin, vmax=vmax)
            ax2.set_title("Canopy Height Map")
            ax2.axis("off")
            cbar = fig.colorbar(im, ax=ax2, fraction=0.046, pad=0.04)
            cbar.set_label("Height (meters)")

        if title:
            fig.suptitle(title, fontsize=14, fontweight="bold")

        plt.tight_layout()

        if output_path:
            fig.savefig(output_path, dpi=150, bbox_inches="tight")
            print(f"Figure saved to: {output_path}")

        return fig

__init__(model_name='compressed_SSLhuge', checkpoint_path=None, device=None, cache_dir=DEFAULT_CACHE_DIR)

Initialize the CanopyHeightEstimation model.

Parameters:

Name Type Description Default
model_name str

Model variant to use. Options: - "compressed_SSLhuge" (default): Compressed huge model (749M), CPU-friendly. Best general-purpose model. - "SSLhuge_satellite": Full huge model (2.9G), GPU required. Best results on satellite imagery. - "compressed_SSLlarge": Compressed large model (400M), CPU-friendly. - "compressed_SSLhuge_aerial": Compressed huge model fine-tuned on aerial imagery (749M), CPU-friendly.

'compressed_SSLhuge'
checkpoint_path Optional[str]

Optional path to a local checkpoint file. If None, the checkpoint will be downloaded automatically.

None
device Optional[str]

Device to run inference on ('cpu', 'cuda', 'cuda:0', etc.). If None, automatically selects CUDA if available. Compressed (quantized) models are loaded without quantization when placed on GPU, which is faster than CPU inference for large images.

None
cache_dir str

Directory to cache downloaded model checkpoints. Defaults to ~/.cache/geoai/canopy/.

DEFAULT_CACHE_DIR
Source code in geoai/canopy.py
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
def __init__(
    self,
    model_name: str = "compressed_SSLhuge",
    checkpoint_path: Optional[str] = None,
    device: Optional[str] = None,
    cache_dir: str = DEFAULT_CACHE_DIR,
):
    """Initialize the CanopyHeightEstimation model.

    Args:
        model_name: Model variant to use. Options:
            - "compressed_SSLhuge" (default): Compressed huge model (749M),
              CPU-friendly. Best general-purpose model.
            - "SSLhuge_satellite": Full huge model (2.9G), GPU required.
              Best results on satellite imagery.
            - "compressed_SSLlarge": Compressed large model (400M), CPU-friendly.
            - "compressed_SSLhuge_aerial": Compressed huge model fine-tuned on
              aerial imagery (749M), CPU-friendly.
        checkpoint_path: Optional path to a local checkpoint file.
            If None, the checkpoint will be downloaded automatically.
        device: Device to run inference on ('cpu', 'cuda', 'cuda:0', etc.).
            If None, automatically selects CUDA if available.  Compressed
            (quantized) models are loaded without quantization when placed
            on GPU, which is faster than CPU inference for large images.
        cache_dir: Directory to cache downloaded model checkpoints.
            Defaults to ~/.cache/geoai/canopy/.
    """
    self.model_name = model_name

    if model_name not in MODEL_VARIANTS:
        raise ValueError(
            f"Unknown model variant '{model_name}'. "
            f"Available: {list(MODEL_VARIANTS.keys())}"
        )

    info = MODEL_VARIANTS[model_name]

    # Determine device – prefer CUDA when available for all model variants
    if device is None:
        if torch.cuda.is_available():
            device = "cuda"
        else:
            device = "cpu"

    self.device = device

    # Download or use provided checkpoint
    if checkpoint_path is None:
        checkpoint_path = _download_checkpoint(model_name, cache_dir)
    self.checkpoint_path = checkpoint_path

    # Load model
    print(f"Loading {model_name} model on {device}...")
    self.model = _load_model(model_name, checkpoint_path, device)
    self.model.eval()
    print("Model loaded successfully.")

    # Image normalization (from Meta's inference.py)
    self._norm_mean = [0.420, 0.411, 0.296]
    self._norm_std = [0.213, 0.156, 0.143]

predict(input_path, output_path=None, tile_size=256, overlap=128, batch_size=4, scale_factor=10.0, **kwargs)

Predict canopy height from a GeoTIFF image.

Processes the input image in tiles of the specified size, runs inference on each tile, and assembles the results into a full canopy height map. Adjacent tiles overlap and are blended using raised-cosine weights for seamless output.

Parameters:

Name Type Description Default
input_path str

Path to the input GeoTIFF file (RGB imagery).

required
output_path Optional[str]

Optional path to save the output canopy height map as a GeoTIFF. If None, only returns the numpy array.

None
tile_size int

Size of tiles for processing (default: 256). The model expects 256x256 tiles.

256
overlap int

Number of pixels of overlap between tiles (default: 128). Using overlap with blending weights eliminates tile-boundary artefacts. Higher values (up to tile_size // 2) give smoother results at the cost of more computation.

128
batch_size int

Number of tiles to process at once (default: 4). Larger values use more memory but process faster.

4
scale_factor float

Factor to multiply model output by to get height in meters (default: 10.0). This is set by the original model.

10.0
**kwargs

Additional keyword arguments (reserved for future use).

{}

Returns:

Type Description
ndarray

numpy.ndarray: 2D array of canopy height values in meters.

Raises:

Type Description
FileNotFoundError

If the input file does not exist.

ValueError

If the input file is not a valid GeoTIFF.

Example

estimator = CanopyHeightEstimation() heights = estimator.predict("aerial_image.tif", ... output_path="canopy_height.tif", ... overlap=128)

Source code in geoai/canopy.py
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
def predict(
    self,
    input_path: str,
    output_path: Optional[str] = None,
    tile_size: int = 256,
    overlap: int = 128,
    batch_size: int = 4,
    scale_factor: float = 10.0,
    **kwargs,
) -> np.ndarray:
    """Predict canopy height from a GeoTIFF image.

    Processes the input image in tiles of the specified size, runs
    inference on each tile, and assembles the results into a full
    canopy height map.  Adjacent tiles overlap and are blended using
    raised-cosine weights for seamless output.

    Args:
        input_path: Path to the input GeoTIFF file (RGB imagery).
        output_path: Optional path to save the output canopy height
            map as a GeoTIFF. If None, only returns the numpy array.
        tile_size: Size of tiles for processing (default: 256).
            The model expects 256x256 tiles.
        overlap: Number of pixels of overlap between tiles (default: 128).
            Using overlap with blending weights eliminates tile-boundary
            artefacts.  Higher values (up to tile_size // 2) give
            smoother results at the cost of more computation.
        batch_size: Number of tiles to process at once (default: 4).
            Larger values use more memory but process faster.
        scale_factor: Factor to multiply model output by to get height
            in meters (default: 10.0). This is set by the original model.
        **kwargs: Additional keyword arguments (reserved for future use).

    Returns:
        numpy.ndarray: 2D array of canopy height values in meters.

    Raises:
        FileNotFoundError: If the input file does not exist.
        ValueError: If the input file is not a valid GeoTIFF.

    Example:
        >>> estimator = CanopyHeightEstimation()
        >>> heights = estimator.predict("aerial_image.tif",
        ...                              output_path="canopy_height.tif",
        ...                              overlap=128)
    """
    if not os.path.exists(input_path):
        raise FileNotFoundError(f"Input file not found: {input_path}")

    if overlap < 0 or overlap >= tile_size:
        raise ValueError(
            f"overlap must be >= 0 and < tile_size ({tile_size}), got {overlap}"
        )

    with rasterio.open(input_path) as src:
        # Read the image
        img = src.read()  # (bands, height, width)
        profile = src.profile.copy()
        height, width = src.height, src.width

        # Handle different band counts
        if img.shape[0] >= 3:
            img = img[:3]  # Use first 3 bands (RGB)
        else:
            raise ValueError(
                f"Input image has {img.shape[0]} bands, but at least 3 (RGB) are required."
            )

        # Convert to float32 in [0, 1]
        if img.dtype == np.uint8:
            img = img.astype(np.float32) / 255.0
        elif img.dtype == np.uint16:
            img = img.astype(np.float32) / 65535.0
        elif img.dtype in (np.float32, np.float64):
            img = img.astype(np.float32)
            # Clip to [0, 1] if needed
            img_max = img.max()
            if img_max > 1.0:
                img = img / img_max
        else:
            img = img.astype(np.float32)
            img_max = img.max()
            if img_max > 0:
                img = img / img_max

    # Calculate tile grid
    step = tile_size - overlap
    n_rows = max(1, math.ceil((height - overlap) / step))
    n_cols = max(1, math.ceil((width - overlap) / step))

    # Pad image to fit tile grid
    padded_h = n_rows * step + overlap
    padded_w = n_cols * step + overlap
    padded_img = np.zeros((3, padded_h, padded_w), dtype=np.float32)
    padded_img[:, :height, :width] = img

    # Blending weight map (raised-cosine taper in overlap regions)
    weight_map = self._make_weight_map(tile_size, overlap)

    # Output arrays
    output = np.zeros((padded_h, padded_w), dtype=np.float32)
    count = np.zeros((padded_h, padded_w), dtype=np.float32)

    # Process tiles in batches
    tiles = []
    positions = []
    for row in range(n_rows):
        for col in range(n_cols):
            y = row * step
            x = col * step
            tile = padded_img[:, y : y + tile_size, x : x + tile_size]
            tiles.append(tile)
            positions.append((y, x))

            if len(tiles) == batch_size:
                self._process_batch(
                    tiles, positions, output, count, scale_factor, weight_map
                )
                tiles = []
                positions = []

    # Process remaining tiles
    if tiles:
        self._process_batch(
            tiles, positions, output, count, scale_factor, weight_map
        )

    # Average overlapping regions
    count[count == 0] = 1
    output = output / count

    # Crop back to original size
    result = output[:height, :width]

    # Save output if requested
    if output_path is not None:
        out_profile = profile.copy()
        out_profile.update(
            dtype="float32",
            count=1,
            compress="lzw",
            nodata=0,
        )
        # Remove alpha band related settings
        if "photometric" in out_profile:
            del out_profile["photometric"]

        with rasterio.open(output_path, "w", **out_profile) as dst:
            dst.write(result, 1)
        print(f"Canopy height map saved to: {output_path}")

    return result

visualize(input_path, height_map=None, output_path=None, figsize=(16, 6), cmap='viridis', vmin=0, vmax=None, title=None, **kwargs)

Visualize the input image alongside the canopy height map.

Parameters:

Name Type Description Default
input_path str

Path to the input GeoTIFF image, or path to the canopy height GeoTIFF if height_map is None.

required
height_map Optional[ndarray]

Canopy height map array. If None, will attempt to read from input_path (assuming it's a height map).

None
output_path Optional[str]

Optional path to save the figure.

None
figsize Tuple[int, int]

Figure size as (width, height).

(16, 6)
cmap str

Colormap for the height map.

'viridis'
vmin float

Minimum value for colormap.

0
vmax Optional[float]

Maximum value for colormap. If None, uses the 98th percentile of the height values.

None
title Optional[str]

Optional title for the figure.

None
**kwargs

Additional keyword arguments for matplotlib.

{}

Returns:

Type Description
Figure

matplotlib.figure.Figure: The visualization figure.

Example

estimator = CanopyHeightEstimation() heights = estimator.predict("input.tif") fig = estimator.visualize("input.tif", heights)

Source code in geoai/canopy.py
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
def visualize(
    self,
    input_path: str,
    height_map: Optional[np.ndarray] = None,
    output_path: Optional[str] = None,
    figsize: Tuple[int, int] = (16, 6),
    cmap: str = "viridis",
    vmin: float = 0,
    vmax: Optional[float] = None,
    title: Optional[str] = None,
    **kwargs,
) -> plt.Figure:
    """Visualize the input image alongside the canopy height map.

    Args:
        input_path: Path to the input GeoTIFF image, or path to the
            canopy height GeoTIFF if height_map is None.
        height_map: Canopy height map array. If None, will attempt to
            read from input_path (assuming it's a height map).
        output_path: Optional path to save the figure.
        figsize: Figure size as (width, height).
        cmap: Colormap for the height map.
        vmin: Minimum value for colormap.
        vmax: Maximum value for colormap. If None, uses the 98th
            percentile of the height values.
        title: Optional title for the figure.
        **kwargs: Additional keyword arguments for matplotlib.

    Returns:
        matplotlib.figure.Figure: The visualization figure.

    Example:
        >>> estimator = CanopyHeightEstimation()
        >>> heights = estimator.predict("input.tif")
        >>> fig = estimator.visualize("input.tif", heights)
    """
    if height_map is None:
        # Assume input_path is the height map
        with rasterio.open(input_path) as src:
            height_map = src.read(1)
        fig, ax = plt.subplots(1, 1, figsize=(figsize[0] // 2, figsize[1]))
        if vmax is None:
            vmax = (
                np.percentile(height_map[height_map > 0], 98)
                if np.any(height_map > 0)
                else 1
            )
        im = ax.imshow(height_map, cmap=cmap, vmin=vmin, vmax=vmax)
        ax.set_title(title or "Canopy Height Map")
        ax.set_xlabel("Pixels")
        ax.set_ylabel("Pixels")
        cbar = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
        cbar.set_label("Height (meters)")
        ax.axis("off")
    else:
        # Show input image and height map side by side
        with rasterio.open(input_path) as src:
            img = src.read()
            if img.shape[0] >= 3:
                img = img[:3]
            if img.dtype == np.uint8:
                img_display = np.moveaxis(img, 0, 2)
            elif img.dtype == np.uint16:
                img_float = img.astype(np.float32)
                img_max = img_float.max()
                if img_max > 0:
                    img_float = img_float / img_max
                img_display = np.moveaxis(img_float, 0, 2)
            else:
                img_float = img.astype(np.float32)
                img_max = img_float.max()
                if img_max > 1.0:
                    img_float = img_float / img_max
                img_display = np.moveaxis(img_float, 0, 2)

        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=figsize)

        ax1.imshow(img_display)
        ax1.set_title("Input Image")
        ax1.axis("off")

        if vmax is None:
            vmax = (
                np.percentile(height_map[height_map > 0], 98)
                if np.any(height_map > 0)
                else 1
            )
        im = ax2.imshow(height_map, cmap=cmap, vmin=vmin, vmax=vmax)
        ax2.set_title("Canopy Height Map")
        ax2.axis("off")
        cbar = fig.colorbar(im, ax=ax2, fraction=0.046, pad=0.04)
        cbar.set_label("Height (meters)")

    if title:
        fig.suptitle(title, fontsize=14, fontweight="bold")

    plt.tight_layout()

    if output_path:
        fig.savefig(output_path, dpi=150, bbox_inches="tight")
        print(f"Figure saved to: {output_path}")

    return fig

canopy_height_estimation(input_path, output_path=None, model_name='compressed_SSLhuge', checkpoint_path=None, device=None, tile_size=256, overlap=128, batch_size=4, cache_dir=DEFAULT_CACHE_DIR, **kwargs)

Convenience function for canopy height estimation.

Creates a CanopyHeightEstimation instance and runs prediction in one call.

Parameters:

Name Type Description Default
input_path str

Path to the input GeoTIFF file (RGB imagery).

required
output_path Optional[str]

Optional path to save the output as a GeoTIFF.

None
model_name str

Model variant to use. Default: "compressed_SSLhuge". See CanopyHeightEstimation for available options.

'compressed_SSLhuge'
checkpoint_path Optional[str]

Optional path to a local checkpoint file.

None
device Optional[str]

Device to run inference on. If None, auto-selects.

None
tile_size int

Size of tiles for processing (default: 256).

256
overlap int

Overlap between tiles in pixels (default: 128). Higher values give smoother results.

128
batch_size int

Number of tiles per batch (default: 4).

4
cache_dir str

Directory to cache model checkpoints.

DEFAULT_CACHE_DIR
**kwargs

Additional keyword arguments passed to predict().

{}

Returns:

Type Description
ndarray

numpy.ndarray: 2D array of canopy height values in meters.

Example

heights = canopy_height_estimation( ... "aerial_image.tif", ... output_path="canopy_height.tif", ... model_name="compressed_SSLhuge", ... )

Source code in geoai/canopy.py
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
def canopy_height_estimation(
    input_path: str,
    output_path: Optional[str] = None,
    model_name: str = "compressed_SSLhuge",
    checkpoint_path: Optional[str] = None,
    device: Optional[str] = None,
    tile_size: int = 256,
    overlap: int = 128,
    batch_size: int = 4,
    cache_dir: str = DEFAULT_CACHE_DIR,
    **kwargs,
) -> np.ndarray:
    """Convenience function for canopy height estimation.

    Creates a CanopyHeightEstimation instance and runs prediction in one call.

    Args:
        input_path: Path to the input GeoTIFF file (RGB imagery).
        output_path: Optional path to save the output as a GeoTIFF.
        model_name: Model variant to use. Default: "compressed_SSLhuge".
            See CanopyHeightEstimation for available options.
        checkpoint_path: Optional path to a local checkpoint file.
        device: Device to run inference on. If None, auto-selects.
        tile_size: Size of tiles for processing (default: 256).
        overlap: Overlap between tiles in pixels (default: 128).
            Higher values give smoother results.
        batch_size: Number of tiles per batch (default: 4).
        cache_dir: Directory to cache model checkpoints.
        **kwargs: Additional keyword arguments passed to predict().

    Returns:
        numpy.ndarray: 2D array of canopy height values in meters.

    Example:
        >>> heights = canopy_height_estimation(
        ...     "aerial_image.tif",
        ...     output_path="canopy_height.tif",
        ...     model_name="compressed_SSLhuge",
        ... )
    """
    estimator = CanopyHeightEstimation(
        model_name=model_name,
        checkpoint_path=checkpoint_path,
        device=device,
        cache_dir=cache_dir,
    )
    return estimator.predict(
        input_path=input_path,
        output_path=output_path,
        tile_size=tile_size,
        overlap=overlap,
        batch_size=batch_size,
        **kwargs,
    )

list_canopy_models()

List available canopy height estimation model variants.

Returns:

Type Description
Dict[str, str]

Dictionary mapping model names to their descriptions.

Example

models = list_canopy_models() for name, desc in models.items(): ... print(f"{name}: {desc}")

Source code in geoai/canopy.py
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
def list_canopy_models() -> Dict[str, str]:
    """List available canopy height estimation model variants.

    Returns:
        Dictionary mapping model names to their descriptions.

    Example:
        >>> models = list_canopy_models()
        >>> for name, desc in models.items():
        ...     print(f"{name}: {desc}")
    """
    return {name: info["description"] for name, info in MODEL_VARIANTS.items()}