Skip to content

train module

Compose

Custom compose transform that works with image and target.

Source code in geoai/train.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
class Compose:
    """Custom compose transform that works with image and target."""

    def __init__(self, transforms):
        """
        Initialize compose transform.

        Args:
            transforms (list): List of transforms to apply.
        """
        self.transforms = transforms

    def __call__(self, image, target):
        for t in self.transforms:
            image, target = t(image, target)
        return image, target

__init__(transforms)

Initialize compose transform.

Parameters:

Name Type Description Default
transforms list

List of transforms to apply.

required
Source code in geoai/train.py
273
274
275
276
277
278
279
280
def __init__(self, transforms):
    """
    Initialize compose transform.

    Args:
        transforms (list): List of transforms to apply.
    """
    self.transforms = transforms

ObjectDetectionDataset

Bases: Dataset

Dataset for object detection from GeoTIFF images and labels.

Source code in geoai/train.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
class ObjectDetectionDataset(Dataset):
    """Dataset for object detection from GeoTIFF images and labels."""

    def __init__(self, image_paths, label_paths, transforms=None, num_channels=None):
        """
        Initialize dataset.

        Args:
            image_paths (list): List of paths to image GeoTIFF files.
            label_paths (list): List of paths to label GeoTIFF files.
            transforms (callable, optional): Transformations to apply to images and masks.
            num_channels (int, optional): Number of channels to use from images. If None,
                auto-detected from the first image.
        """
        self.image_paths = image_paths
        self.label_paths = label_paths
        self.transforms = transforms

        # Auto-detect the number of channels if not specified
        if num_channels is None:
            with rasterio.open(self.image_paths[0]) as src:
                self.num_channels = src.count
        else:
            self.num_channels = num_channels

    def __len__(self):
        return len(self.image_paths)

    def __getitem__(self, idx):
        # Load image
        with rasterio.open(self.image_paths[idx]) as src:
            # Read as [C, H, W] format
            image = src.read().astype(np.float32)

            # Normalize image to [0, 1] range
            image = image / 255.0

            # Handle different number of channels
            if image.shape[0] > self.num_channels:
                image = image[
                    : self.num_channels
                ]  # Keep only first 4 bands if more exist
            elif image.shape[0] < self.num_channels:
                # Pad with zeros if less than 4 bands
                padded = np.zeros(
                    (self.num_channels, image.shape[1], image.shape[2]),
                    dtype=np.float32,
                )
                padded[: image.shape[0]] = image
                image = padded

            # Convert to CHW tensor
            image = torch.as_tensor(image, dtype=torch.float32)

        # Load label mask
        with rasterio.open(self.label_paths[idx]) as src:
            label_mask = src.read(1)
            binary_mask = (label_mask > 0).astype(np.uint8)

        # Find all building instances using connected components
        labeled_mask, num_instances = measure.label(
            binary_mask, return_num=True, connectivity=2
        )

        # Create list to hold masks for each building instance
        masks = []
        boxes = []
        labels = []

        for i in range(1, num_instances + 1):
            # Create mask for this instance
            instance_mask = (labeled_mask == i).astype(np.uint8)

            # Calculate area and filter out tiny instances (noise)
            area = instance_mask.sum()
            if area < 10:  # Minimum area threshold
                continue

            # Find bounding box coordinates
            pos = np.where(instance_mask)
            if len(pos[0]) == 0:  # Skip if mask is empty
                continue

            xmin = np.min(pos[1])
            xmax = np.max(pos[1])
            ymin = np.min(pos[0])
            ymax = np.max(pos[0])

            # Skip invalid boxes
            if xmax <= xmin or ymax <= ymin:
                continue

            # Add small padding to ensure the mask is within the box
            xmin = max(0, xmin - 1)
            ymin = max(0, ymin - 1)
            xmax = min(binary_mask.shape[1] - 1, xmax + 1)
            ymax = min(binary_mask.shape[0] - 1, ymax + 1)

            boxes.append([xmin, ymin, xmax, ymax])
            masks.append(instance_mask)
            labels.append(1)  # 1 for building class

        # Handle case with no valid instances
        if len(boxes) == 0:
            # Create a dummy target with minimal required fields
            target = {
                "boxes": torch.zeros((0, 4), dtype=torch.float32),
                "labels": torch.zeros((0), dtype=torch.int64),
                "masks": torch.zeros(
                    (0, binary_mask.shape[0], binary_mask.shape[1]), dtype=torch.uint8
                ),
                "image_id": torch.tensor([idx]),
                "area": torch.zeros((0), dtype=torch.float32),
                "iscrowd": torch.zeros((0), dtype=torch.int64),
            }
        else:
            # Convert to tensors
            boxes = torch.as_tensor(boxes, dtype=torch.float32)
            labels = torch.as_tensor(labels, dtype=torch.int64)
            masks = torch.as_tensor(np.array(masks), dtype=torch.uint8)

            # Calculate area of boxes
            area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])

            # Prepare target dictionary
            target = {
                "boxes": boxes,
                "labels": labels,
                "masks": masks,
                "image_id": torch.tensor([idx]),
                "area": area,
                "iscrowd": torch.zeros_like(labels),  # Assume no crowd instances
            }

        # Apply transforms if specified
        if self.transforms is not None:
            image, target = self.transforms(image, target)

        return image, target

__init__(image_paths, label_paths, transforms=None, num_channels=None)

Initialize dataset.

Parameters:

Name Type Description Default
image_paths list

List of paths to image GeoTIFF files.

required
label_paths list

List of paths to label GeoTIFF files.

required
transforms callable

Transformations to apply to images and masks.

None
num_channels int

Number of channels to use from images. If None, auto-detected from the first image.

None
Source code in geoai/train.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def __init__(self, image_paths, label_paths, transforms=None, num_channels=None):
    """
    Initialize dataset.

    Args:
        image_paths (list): List of paths to image GeoTIFF files.
        label_paths (list): List of paths to label GeoTIFF files.
        transforms (callable, optional): Transformations to apply to images and masks.
        num_channels (int, optional): Number of channels to use from images. If None,
            auto-detected from the first image.
    """
    self.image_paths = image_paths
    self.label_paths = label_paths
    self.transforms = transforms

    # Auto-detect the number of channels if not specified
    if num_channels is None:
        with rasterio.open(self.image_paths[0]) as src:
            self.num_channels = src.count
    else:
        self.num_channels = num_channels

RandomHorizontalFlip

Random horizontal flip transform.

Source code in geoai/train.py
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
class RandomHorizontalFlip:
    """Random horizontal flip transform."""

    def __init__(self, prob=0.5):
        """
        Initialize random horizontal flip.

        Args:
            prob (float): Probability of applying the flip.
        """
        self.prob = prob

    def __call__(self, image, target):
        if random.random() < self.prob:
            # Flip image
            image = torch.flip(image, dims=[2])  # Flip along width dimension

            # Flip masks
            if "masks" in target and len(target["masks"]) > 0:
                target["masks"] = torch.flip(target["masks"], dims=[2])

            # Update boxes
            if "boxes" in target and len(target["boxes"]) > 0:
                boxes = target["boxes"]
                width = image.shape[2]
                boxes[:, 0], boxes[:, 2] = width - boxes[:, 2], width - boxes[:, 0]
                target["boxes"] = boxes

        return image, target

__init__(prob=0.5)

Initialize random horizontal flip.

Parameters:

Name Type Description Default
prob float

Probability of applying the flip.

0.5
Source code in geoai/train.py
308
309
310
311
312
313
314
315
def __init__(self, prob=0.5):
    """
    Initialize random horizontal flip.

    Args:
        prob (float): Probability of applying the flip.
    """
    self.prob = prob

SemanticRandomHorizontalFlip

Random horizontal flip transform for semantic segmentation.

Source code in geoai/train.py
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
class SemanticRandomHorizontalFlip:
    """Random horizontal flip transform for semantic segmentation."""

    def __init__(self, prob=0.5):
        self.prob = prob

    def __call__(self, image, mask):
        if random.random() < self.prob:
            # Flip image and mask along width dimension
            image = torch.flip(image, dims=[2])
            mask = torch.flip(mask, dims=[1])
        return image, mask

SemanticSegmentationDataset

Bases: Dataset

Dataset for semantic segmentation from GeoTIFF, PNG, JPG, and other image formats.

Source code in geoai/train.py
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
1323
1324
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
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
class SemanticSegmentationDataset(Dataset):
    """Dataset for semantic segmentation from GeoTIFF, PNG, JPG, and other image formats."""

    def __init__(
        self,
        image_paths,
        label_paths,
        transforms=None,
        num_channels=None,
        target_size=None,
        resize_mode="resize",
        num_classes=2,
    ):
        """
        Initialize dataset for semantic segmentation.

        Args:
            image_paths (list): List of paths to image files (GeoTIFF, PNG, JPG, etc.).
            label_paths (list): List of paths to label files (GeoTIFF, PNG, JPG, etc.).
            transforms (callable, optional): Transformations to apply to images and masks.
            num_channels (int, optional): Number of channels to use from images. If None,
                auto-detected from the first image.
            target_size (tuple, optional): Target size (height, width) for standardizing images.
                If None, images will keep their original sizes.
            resize_mode (str): How to handle size standardization. Options:
                'resize' - Resize images to target_size (may change aspect ratio)
                'pad' - Pad images to target_size (preserves aspect ratio)
            num_classes (int): Number of classes for segmentation. Used for mask normalization.
        """
        self.image_paths = image_paths
        self.label_paths = label_paths
        self.transforms = transforms
        self.target_size = target_size
        self.resize_mode = resize_mode
        self.num_classes = num_classes

        # Auto-detect the number of channels if not specified
        if num_channels is None:
            self.num_channels = self._get_num_channels(self.image_paths[0])
        else:
            self.num_channels = num_channels

    def _is_geotiff(self, file_path):
        """Check if file is a GeoTIFF based on extension."""
        return file_path.lower().endswith((".tif", ".tiff"))

    def _get_num_channels(self, image_path):
        """Get number of channels from an image file."""
        if self._is_geotiff(image_path):
            with rasterio.open(image_path) as src:
                return src.count
        else:
            # For standard image formats, use PIL
            with Image.open(image_path) as img:
                if img.mode == "RGB":
                    return 3
                elif img.mode == "RGBA":
                    return 4
                elif img.mode == "L":
                    return 1
                else:
                    # Convert to RGB and return 3 channels
                    return 3

    def _resize_image_and_mask(self, image, mask):
        """Resize image and mask to target size."""
        if self.target_size is None:
            return image, mask

        target_h, target_w = self.target_size

        if self.resize_mode == "resize":
            # Direct resize (may change aspect ratio)
            image = F.interpolate(
                image.unsqueeze(0),
                size=(target_h, target_w),
                mode="bilinear",
                align_corners=False,
            ).squeeze(0)

            mask = (
                F.interpolate(
                    mask.unsqueeze(0).unsqueeze(0).float(),
                    size=(target_h, target_w),
                    mode="nearest",
                )
                .squeeze(0)
                .squeeze(0)
                .long()
            )
            # Clamp mask values to ensure they're within valid range [0, num_classes-1]
            mask = torch.clamp(mask, 0, self.num_classes - 1)

        elif self.resize_mode == "pad":
            # Pad to target size (preserves aspect ratio)
            image = self._pad_to_size(image, (target_h, target_w))
            mask = self._pad_to_size(mask.unsqueeze(0), (target_h, target_w)).squeeze(0)
            # Clamp mask values to ensure they're within valid range [0, num_classes-1]
            mask = torch.clamp(mask, 0, self.num_classes - 1)

        return image, mask

    def _pad_to_size(self, tensor, target_size):
        """Pad tensor to target size with zeros."""
        target_h, target_w = target_size

        if tensor.dim() == 3:  # Image [C, H, W]
            _, h, w = tensor.shape
        elif tensor.dim() == 2:  # Mask [H, W]
            h, w = tensor.shape
        else:
            raise ValueError(f"Unexpected tensor dimensions: {tensor.shape}")

        # Calculate padding
        pad_h = max(0, target_h - h)
        pad_w = max(0, target_w - w)

        # Pad equally on both sides
        pad_top = pad_h // 2
        pad_bottom = pad_h - pad_top
        pad_left = pad_w // 2
        pad_right = pad_w - pad_left

        # Apply padding (left, right, top, bottom)
        padded = F.pad(tensor, (pad_left, pad_right, pad_top, pad_bottom), value=0)

        # Crop if tensor is larger than target
        if tensor.dim() == 3:
            padded = padded[:, :target_h, :target_w]
        else:
            padded = padded[:target_h, :target_w]

        return padded

    def __len__(self):
        return len(self.image_paths)

    def __getitem__(self, idx):
        # Load image
        image_path = self.image_paths[idx]
        if self._is_geotiff(image_path):
            # Load GeoTIFF using rasterio
            with rasterio.open(image_path) as src:
                # Read as [C, H, W] format
                image = src.read().astype(np.float32)
                # Normalize image to [0, 1] range
                image = image / 255.0
        else:
            # Load standard image formats using PIL
            with Image.open(image_path) as img:
                # Convert to RGB if needed
                if img.mode != "RGB":
                    img = img.convert("RGB")
                # Convert to numpy array [H, W, C]
                image = np.array(img, dtype=np.float32)
                # Normalize to [0, 1] range
                image = image / 255.0
                # Convert to [C, H, W] format
                image = np.transpose(image, (2, 0, 1))

        # Handle different number of channels
        if image.shape[0] > self.num_channels:
            image = image[: self.num_channels]  # Keep only specified bands
        elif image.shape[0] < self.num_channels:
            # Pad with zeros if less than specified bands
            padded = np.zeros(
                (self.num_channels, image.shape[1], image.shape[2]),
                dtype=np.float32,
            )
            padded[: image.shape[0]] = image
            image = padded

        # Convert to CHW tensor
        image = torch.as_tensor(image, dtype=torch.float32)

        # Load label mask
        label_path = self.label_paths[idx]
        if self._is_geotiff(label_path):
            # Load GeoTIFF label using rasterio
            with rasterio.open(label_path) as src:
                label_mask = src.read(1).astype(np.int64)
        else:
            # Load standard image format label using PIL
            with Image.open(label_path) as img:
                # Convert to grayscale if needed
                if img.mode != "L":
                    img = img.convert("L")
                label_mask = np.array(img, dtype=np.int64)

        # Normalize mask values to expected class range [0, num_classes-1]
        # This handles cases where masks contain pixel values outside the expected range
        unique_vals = np.unique(label_mask)
        if len(unique_vals) > 2:
            # For multi-class case, we need to map values to proper class indices
            # For now, we'll use a simple thresholding approach for binary segmentation
            if self.num_classes == 2:
                # Binary segmentation: convert to 0 (background) and 1 (foreground)
                label_mask = (label_mask > 0).astype(np.int64)
            else:
                # For multi-class, we could implement more sophisticated mapping
                # For now, just ensure values are in valid range
                label_mask = np.clip(label_mask, 0, self.num_classes - 1)
        elif len(unique_vals) == 2 and unique_vals.max() > 1:
            # Binary mask with values not in [0,1] range - normalize to [0,1]
            label_mask = (label_mask > 0).astype(np.int64)

        # Convert to tensor
        mask = torch.as_tensor(label_mask, dtype=torch.long)

        # Resize image and mask to target size if specified
        image, mask = self._resize_image_and_mask(image, mask)

        # Apply transforms if specified
        if self.transforms is not None:
            image, mask = self.transforms(image, mask)

        return image, mask

__init__(image_paths, label_paths, transforms=None, num_channels=None, target_size=None, resize_mode='resize', num_classes=2)

Initialize dataset for semantic segmentation.

Parameters:

Name Type Description Default
image_paths list

List of paths to image files (GeoTIFF, PNG, JPG, etc.).

required
label_paths list

List of paths to label files (GeoTIFF, PNG, JPG, etc.).

required
transforms callable

Transformations to apply to images and masks.

None
num_channels int

Number of channels to use from images. If None, auto-detected from the first image.

None
target_size tuple

Target size (height, width) for standardizing images. If None, images will keep their original sizes.

None
resize_mode str

How to handle size standardization. Options: 'resize' - Resize images to target_size (may change aspect ratio) 'pad' - Pad images to target_size (preserves aspect ratio)

'resize'
num_classes int

Number of classes for segmentation. Used for mask normalization.

2
Source code in geoai/train.py
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
def __init__(
    self,
    image_paths,
    label_paths,
    transforms=None,
    num_channels=None,
    target_size=None,
    resize_mode="resize",
    num_classes=2,
):
    """
    Initialize dataset for semantic segmentation.

    Args:
        image_paths (list): List of paths to image files (GeoTIFF, PNG, JPG, etc.).
        label_paths (list): List of paths to label files (GeoTIFF, PNG, JPG, etc.).
        transforms (callable, optional): Transformations to apply to images and masks.
        num_channels (int, optional): Number of channels to use from images. If None,
            auto-detected from the first image.
        target_size (tuple, optional): Target size (height, width) for standardizing images.
            If None, images will keep their original sizes.
        resize_mode (str): How to handle size standardization. Options:
            'resize' - Resize images to target_size (may change aspect ratio)
            'pad' - Pad images to target_size (preserves aspect ratio)
        num_classes (int): Number of classes for segmentation. Used for mask normalization.
    """
    self.image_paths = image_paths
    self.label_paths = label_paths
    self.transforms = transforms
    self.target_size = target_size
    self.resize_mode = resize_mode
    self.num_classes = num_classes

    # Auto-detect the number of channels if not specified
    if num_channels is None:
        self.num_channels = self._get_num_channels(self.image_paths[0])
    else:
        self.num_channels = num_channels

SemanticToTensor

Convert numpy.ndarray to tensor for semantic segmentation.

Source code in geoai/train.py
1493
1494
1495
1496
1497
class SemanticToTensor:
    """Convert numpy.ndarray to tensor for semantic segmentation."""

    def __call__(self, image, mask):
        return image, mask

SemanticTransforms

Custom transforms for semantic segmentation.

Source code in geoai/train.py
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
class SemanticTransforms:
    """Custom transforms for semantic segmentation."""

    def __init__(self, transforms):
        self.transforms = transforms

    def __call__(self, image, mask):
        for t in self.transforms:
            image, mask = t(image, mask)
        return image, mask

ToTensor

Convert numpy.ndarray to tensor.

Source code in geoai/train.py
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
class ToTensor:
    """Convert numpy.ndarray to tensor."""

    def __call__(self, image, target):
        """
        Apply transform to image and target.

        Args:
            image (torch.Tensor): Input image.
            target (dict): Target annotations.

        Returns:
            tuple: Transformed image and target.
        """
        return image, target

__call__(image, target)

Apply transform to image and target.

Parameters:

Name Type Description Default
image Tensor

Input image.

required
target dict

Target annotations.

required

Returns:

Name Type Description
tuple

Transformed image and target.

Source code in geoai/train.py
291
292
293
294
295
296
297
298
299
300
301
302
def __call__(self, image, target):
    """
    Apply transform to image and target.

    Args:
        image (torch.Tensor): Input image.
        target (dict): Target annotations.

    Returns:
        tuple: Transformed image and target.
    """
    return image, target

collate_fn(batch)

Custom collate function for batching samples.

Parameters:

Name Type Description Default
batch list

List of (image, target) tuples.

required

Returns:

Name Type Description
tuple

Tuple of images and targets.

Source code in geoai/train.py
355
356
357
358
359
360
361
362
363
364
365
def collate_fn(batch):
    """
    Custom collate function for batching samples.

    Args:
        batch (list): List of (image, target) tuples.

    Returns:
        tuple: Tuple of images and targets.
    """
    return tuple(zip(*batch))

dice_coefficient(pred, target, smooth=1e-06, num_classes=None)

Calculate Dice coefficient for segmentation (binary or multi-class).

Parameters:

Name Type Description Default
pred Tensor

Predicted mask (probabilities or logits) with shape [C, H, W] or [H, W].

required
target Tensor

Ground truth mask with shape [H, W].

required
smooth float

Smoothing factor to avoid division by zero.

1e-06
num_classes int

Number of classes. If None, auto-detected.

None

Returns:

Name Type Description
float

Mean Dice coefficient across all classes.

Source code in geoai/train.py
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
def dice_coefficient(pred, target, smooth=1e-6, num_classes=None):
    """
    Calculate Dice coefficient for segmentation (binary or multi-class).

    Args:
        pred (torch.Tensor): Predicted mask (probabilities or logits) with shape [C, H, W] or [H, W].
        target (torch.Tensor): Ground truth mask with shape [H, W].
        smooth (float): Smoothing factor to avoid division by zero.
        num_classes (int, optional): Number of classes. If None, auto-detected.

    Returns:
        float: Mean Dice coefficient across all classes.
    """
    # Convert predictions to class predictions
    if pred.dim() == 3:  # [C, H, W] format
        pred = torch.softmax(pred, dim=0)
        pred_classes = torch.argmax(pred, dim=0)
    elif pred.dim() == 2:  # [H, W] format
        pred_classes = pred
    else:
        raise ValueError(f"Unexpected prediction dimensions: {pred.shape}")

    # Auto-detect number of classes if not provided
    if num_classes is None:
        num_classes = max(pred_classes.max().item(), target.max().item()) + 1

    # Calculate Dice for each class and average
    dice_scores = []
    for class_id in range(num_classes):
        pred_class = (pred_classes == class_id).float()
        target_class = (target == class_id).float()

        intersection = (pred_class * target_class).sum()
        union = pred_class.sum() + target_class.sum()

        if union > 0:
            dice = (2.0 * intersection + smooth) / (union + smooth)
            dice_scores.append(dice.item())

    return sum(dice_scores) / len(dice_scores) if dice_scores else 0.0

evaluate(model, data_loader, device)

Evaluate the model on the validation set.

Parameters:

Name Type Description Default
model Module

The model to evaluate.

required
data_loader DataLoader

DataLoader for validation data.

required
device device

Device to evaluate on.

required

Returns:

Name Type Description
dict

Evaluation metrics including loss and IoU.

Source code in geoai/train.py
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
def evaluate(model, data_loader, device):
    """
    Evaluate the model on the validation set.

    Args:
        model (torch.nn.Module): The model to evaluate.
        data_loader (torch.utils.data.DataLoader): DataLoader for validation data.
        device (torch.device): Device to evaluate on.

    Returns:
        dict: Evaluation metrics including loss and IoU.
    """
    model.eval()

    # Initialize metrics
    total_loss = 0
    iou_scores = []

    with torch.no_grad():
        for images, targets in data_loader:
            # Move to device
            images = list(image.to(device) for image in images)
            targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

            # During evaluation, Mask R-CNN directly returns predictions, not losses
            # So we'll only get loss when we provide targets explicitly
            if len(targets) > 0:
                try:
                    # Try to get loss dict (this works in some implementations)
                    loss_dict = model(images, targets)
                    if isinstance(loss_dict, dict):
                        losses = sum(loss for loss in loss_dict.values())
                        total_loss += losses.item()
                except Exception as e:
                    print(f"Warning: Could not compute loss during evaluation: {e}")
                    # If we can't compute loss, we'll just focus on IoU
                    pass

            # Get predictions
            outputs = model(images)

            # Calculate IoU for each image
            for i, output in enumerate(outputs):
                if len(output["masks"]) == 0 or len(targets[i]["masks"]) == 0:
                    continue

                # Convert predicted masks to binary (threshold at 0.5)
                pred_masks = (output["masks"].squeeze(1) > 0.5).float()

                # Combine all instance masks into a single binary mask
                pred_combined = (
                    torch.max(pred_masks, dim=0)[0]
                    if pred_masks.shape[0] > 0
                    else torch.zeros_like(targets[i]["masks"][0])
                )
                target_combined = (
                    torch.max(targets[i]["masks"], dim=0)[0]
                    if targets[i]["masks"].shape[0] > 0
                    else torch.zeros_like(pred_combined)
                )

                # Calculate IoU
                intersection = (pred_combined * target_combined).sum().item()
                union = ((pred_combined + target_combined) > 0).sum().item()

                if union > 0:
                    iou = intersection / union
                    iou_scores.append(iou)

    # Calculate metrics
    avg_loss = total_loss / len(data_loader) if total_loss > 0 else float("inf")
    avg_iou = sum(iou_scores) / len(iou_scores) if iou_scores else 0

    return {"loss": avg_loss, "IoU": avg_iou}

evaluate_semantic(model, data_loader, device, criterion, num_classes=2)

Evaluate the semantic segmentation model on the validation set.

Parameters:

Name Type Description Default
model Module

The model to evaluate.

required
data_loader DataLoader

DataLoader for validation data.

required
device device

Device to evaluate on.

required
criterion

Loss function.

required
num_classes int

Number of classes for evaluation metrics.

2

Returns:

Name Type Description
dict

Evaluation metrics including loss, IoU, and Dice.

Source code in geoai/train.py
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
def evaluate_semantic(model, data_loader, device, criterion, num_classes=2):
    """
    Evaluate the semantic segmentation model on the validation set.

    Args:
        model (torch.nn.Module): The model to evaluate.
        data_loader (torch.utils.data.DataLoader): DataLoader for validation data.
        device (torch.device): Device to evaluate on.
        criterion: Loss function.
        num_classes (int): Number of classes for evaluation metrics.

    Returns:
        dict: Evaluation metrics including loss, IoU, and Dice.
    """
    model.eval()

    total_loss = 0
    dice_scores = []
    iou_scores = []
    num_batches = len(data_loader)

    with torch.no_grad():
        for images, targets in data_loader:
            # Move to device
            images = images.to(device)
            targets = targets.to(device)

            # Forward pass
            outputs = model(images)
            loss = criterion(outputs, targets)
            total_loss += loss.item()

            # Calculate metrics for each sample in the batch
            for pred, target in zip(outputs, targets):
                dice = dice_coefficient(pred, target, num_classes=num_classes)
                iou = iou_coefficient(pred, target, num_classes=num_classes)
                dice_scores.append(dice)
                iou_scores.append(iou)

    # Calculate metrics
    avg_loss = total_loss / num_batches
    avg_dice = sum(dice_scores) / len(dice_scores) if dice_scores else 0
    avg_iou = sum(iou_scores) / len(iou_scores) if iou_scores else 0

    return {"loss": avg_loss, "Dice": avg_dice, "IoU": avg_iou}

get_instance_segmentation_model(num_classes=2, num_channels=3, pretrained=True)

Get Mask R-CNN model with custom input channels and output classes.

Parameters:

Name Type Description Default
num_classes int

Number of output classes (including background).

2
num_channels int

Number of input channels (3 for RGB, 4 for RGBN).

3
pretrained bool

Whether to use pretrained backbone.

True

Returns:

Type Description

torch.nn.Module: Mask R-CNN model with specified input channels and output classes.

Raises:

Type Description
ValueError

If num_channels is less than 3.

Source code in geoai/train.py
 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
def get_instance_segmentation_model(num_classes=2, num_channels=3, pretrained=True):
    """
    Get Mask R-CNN model with custom input channels and output classes.

    Args:
        num_classes (int): Number of output classes (including background).
        num_channels (int): Number of input channels (3 for RGB, 4 for RGBN).
        pretrained (bool): Whether to use pretrained backbone.

    Returns:
        torch.nn.Module: Mask R-CNN model with specified input channels and output classes.

    Raises:
        ValueError: If num_channels is less than 3.
    """
    # Validate num_channels
    if num_channels < 3:
        raise ValueError("num_channels must be at least 3")

    # Load pre-trained model
    model = maskrcnn_resnet50_fpn(
        pretrained=pretrained,
        progress=True,
        weights=(
            torchvision.models.detection.MaskRCNN_ResNet50_FPN_Weights.DEFAULT
            if pretrained
            else None
        ),
    )

    # Modify transform if num_channels is different from 3
    if num_channels != 3:
        # Get the transform
        transform = model.transform

        # Default values are [0.485, 0.456, 0.406] and [0.229, 0.224, 0.225]
        # Calculate means and stds for additional channels
        rgb_mean = [0.485, 0.456, 0.406]
        rgb_std = [0.229, 0.224, 0.225]

        # Extend them to num_channels (use the mean value for additional channels)
        mean_of_means = sum(rgb_mean) / len(rgb_mean)
        mean_of_stds = sum(rgb_std) / len(rgb_std)

        # Create new lists with appropriate length
        transform.image_mean = rgb_mean + [mean_of_means] * (num_channels - 3)
        transform.image_std = rgb_std + [mean_of_stds] * (num_channels - 3)

    # Get number of input features for the classifier
    in_features = model.roi_heads.box_predictor.cls_score.in_features

    # Replace the pre-trained head with a new one
    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)

    # Get number of input features for mask classifier
    in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels
    hidden_layer = 256

    # Replace mask predictor with a new one
    model.roi_heads.mask_predictor = MaskRCNNPredictor(
        in_features_mask, hidden_layer, num_classes
    )

    # Modify the first layer if num_channels is different from 3
    if num_channels != 3:
        original_layer = model.backbone.body.conv1
        model.backbone.body.conv1 = torch.nn.Conv2d(
            num_channels,
            original_layer.out_channels,
            kernel_size=original_layer.kernel_size,
            stride=original_layer.stride,
            padding=original_layer.padding,
            bias=original_layer.bias is not None,
        )

        # Copy weights from the original 3 channels to the new layer
        with torch.no_grad():
            # Copy the weights for the first 3 channels
            model.backbone.body.conv1.weight[:, :3, :, :] = original_layer.weight

            # Initialize additional channels with the mean of the first 3 channels
            mean_weight = original_layer.weight.mean(dim=1, keepdim=True)
            for i in range(3, num_channels):
                model.backbone.body.conv1.weight[:, i : i + 1, :, :] = mean_weight

            # Copy bias if it exists
            if original_layer.bias is not None:
                model.backbone.body.conv1.bias = original_layer.bias

    return model

get_semantic_transform(train)

Get transforms for semantic segmentation data augmentation.

Parameters:

Name Type Description Default
train bool

Whether to include training-specific transforms.

required

Returns:

Name Type Description
SemanticTransforms

Composed transforms.

Source code in geoai/train.py
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
def get_semantic_transform(train):
    """
    Get transforms for semantic segmentation data augmentation.

    Args:
        train (bool): Whether to include training-specific transforms.

    Returns:
        SemanticTransforms: Composed transforms.
    """
    transforms = []
    transforms.append(SemanticToTensor())

    if train:
        transforms.append(SemanticRandomHorizontalFlip(0.5))

    return SemanticTransforms(transforms)

get_smp_model(architecture='unet', encoder_name='resnet34', encoder_weights='imagenet', in_channels=3, classes=2, activation=None, **kwargs)

Get a segmentation model from segmentation-models-pytorch using the generic create_model function.

Parameters:

Name Type Description Default
architecture str

Model architecture (e.g., 'unet', 'deeplabv3', 'deeplabv3plus', 'fpn', 'pspnet', 'linknet', 'manet', 'pan', 'upernet', etc.). Case insensitive.

'unet'
encoder_name str

Encoder backbone name (e.g., 'resnet34', 'efficientnet-b0', 'mit_b0', etc.).

'resnet34'
encoder_weights str

Encoder weights ('imagenet' or None).

'imagenet'
in_channels int

Number of input channels.

3
classes int

Number of output classes.

2
activation str

Activation function for output layer.

None
**kwargs

Additional arguments passed to smp.create_model().

{}

Returns:

Type Description

torch.nn.Module: Segmentation model.

Note

This function uses smp.create_model() which supports all architectures available in segmentation-models-pytorch, making it future-proof for new model additions.

Source code in geoai/train.py
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
def get_smp_model(
    architecture="unet",
    encoder_name="resnet34",
    encoder_weights="imagenet",
    in_channels=3,
    classes=2,
    activation=None,
    **kwargs,
):
    """
    Get a segmentation model from segmentation-models-pytorch using the generic create_model function.

    Args:
        architecture (str): Model architecture (e.g., 'unet', 'deeplabv3', 'deeplabv3plus', 'fpn',
            'pspnet', 'linknet', 'manet', 'pan', 'upernet', etc.). Case insensitive.
        encoder_name (str): Encoder backbone name (e.g., 'resnet34', 'efficientnet-b0', 'mit_b0', etc.).
        encoder_weights (str): Encoder weights ('imagenet' or None).
        in_channels (int): Number of input channels.
        classes (int): Number of output classes.
        activation (str): Activation function for output layer.
        **kwargs: Additional arguments passed to smp.create_model().

    Returns:
        torch.nn.Module: Segmentation model.

    Note:
        This function uses smp.create_model() which supports all architectures available in
        segmentation-models-pytorch, making it future-proof for new model additions.
    """
    if not SMP_AVAILABLE:
        raise ImportError(
            "segmentation-models-pytorch is not installed. "
            "Please install it with: pip install segmentation-models-pytorch"
        )

    try:
        # Use the generic create_model function - supports all SMP architectures
        model = smp.create_model(
            arch=architecture,  # Case insensitive
            encoder_name=encoder_name,
            encoder_weights=encoder_weights,
            in_channels=in_channels,
            classes=classes,
            **kwargs,
        )

        # Apply activation if specified (note: activation is handled differently in create_model)
        if activation is not None:
            import warnings

            warnings.warn(
                "The 'activation' parameter is deprecated when using smp.create_model(). "
                "Apply activation manually after model creation if needed.",
                DeprecationWarning,
                stacklevel=2,
            )

        return model

    except Exception as e:
        # Provide helpful error message
        available_archs = []
        try:
            # Try to get available architectures from smp
            if hasattr(smp, "get_available_models"):
                available_archs = smp.get_available_models()
            else:
                available_archs = [
                    "unet",
                    "unetplusplus",
                    "manet",
                    "linknet",
                    "fpn",
                    "pspnet",
                    "deeplabv3",
                    "deeplabv3plus",
                    "pan",
                    "upernet",
                ]
        except:
            available_archs = [
                "unet",
                "fpn",
                "deeplabv3plus",
                "pspnet",
                "linknet",
                "manet",
            ]

        raise ValueError(
            f"Failed to create model with architecture '{architecture}' and encoder '{encoder_name}'. "
            f"Error: {str(e)}. "
            f"Available architectures include: {', '.join(available_archs)}. "
            f"Please check the segmentation-models-pytorch documentation for supported combinations."
        )

get_transform(train)

Get transforms for data augmentation.

Parameters:

Name Type Description Default
train bool

Whether to include training-specific transforms.

required

Returns:

Name Type Description
Compose

Composed transforms.

Source code in geoai/train.py
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
def get_transform(train):
    """
    Get transforms for data augmentation.

    Args:
        train (bool): Whether to include training-specific transforms.

    Returns:
        Compose: Composed transforms.
    """
    transforms = []
    transforms.append(ToTensor())

    if train:
        transforms.append(RandomHorizontalFlip(0.5))

    return Compose(transforms)

inference_on_geotiff(model, geotiff_path, output_path, window_size=512, overlap=256, confidence_threshold=0.5, batch_size=4, num_channels=3, device=None, **kwargs)

Perform inference on a large GeoTIFF using a sliding window approach with improved blending.

Parameters:

Name Type Description Default
model Module

Trained model for inference.

required
geotiff_path str

Path to input GeoTIFF file.

required
output_path str

Path to save output mask GeoTIFF.

required
window_size int

Size of sliding window for inference.

512
overlap int

Overlap between adjacent windows.

256
confidence_threshold float

Confidence threshold for predictions (0-1).

0.5
batch_size int

Batch size for inference.

4
num_channels int

Number of channels to use from the input image.

3
device device

Device to run inference on. If None, uses CUDA if available.

None
**kwargs

Additional arguments.

{}

Returns:

Name Type Description
tuple

Tuple containing output path and inference time in seconds.

Source code in geoai/train.py
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 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
def inference_on_geotiff(
    model,
    geotiff_path,
    output_path,
    window_size=512,
    overlap=256,
    confidence_threshold=0.5,
    batch_size=4,
    num_channels=3,
    device=None,
    **kwargs,
):
    """
    Perform inference on a large GeoTIFF using a sliding window approach with improved blending.

    Args:
        model (torch.nn.Module): Trained model for inference.
        geotiff_path (str): Path to input GeoTIFF file.
        output_path (str): Path to save output mask GeoTIFF.
        window_size (int): Size of sliding window for inference.
        overlap (int): Overlap between adjacent windows.
        confidence_threshold (float): Confidence threshold for predictions (0-1).
        batch_size (int): Batch size for inference.
        num_channels (int): Number of channels to use from the input image.
        device (torch.device, optional): Device to run inference on. If None, uses CUDA if available.
        **kwargs: Additional arguments.

    Returns:
        tuple: Tuple containing output path and inference time in seconds.
    """
    if device is None:
        device = get_device()

    # Put model in evaluation mode
    model.to(device)
    model.eval()

    # Open the GeoTIFF
    with rasterio.open(geotiff_path) as src:
        # Read metadata
        meta = src.meta
        height = src.height
        width = src.width

        # Update metadata for output raster
        out_meta = meta.copy()
        out_meta.update(
            {"count": 1, "dtype": "uint8"}  # Single band for mask  # Binary mask
        )

        # We'll use two arrays:
        # 1. For accumulating predictions
        pred_accumulator = np.zeros((height, width), dtype=np.float32)
        # 2. For tracking how many predictions contribute to each pixel
        count_accumulator = np.zeros((height, width), dtype=np.float32)

        # Calculate the number of windows needed to cover the entire image
        steps_y = math.ceil((height - overlap) / (window_size - overlap))
        steps_x = math.ceil((width - overlap) / (window_size - overlap))

        # Ensure we cover the entire image
        last_y = height - window_size
        last_x = width - window_size

        total_windows = steps_y * steps_x
        print(
            f"Processing {total_windows} windows with size {window_size}x{window_size} and overlap {overlap}..."
        )

        # Create progress bar
        pbar = tqdm(total=total_windows)

        # Process in batches
        batch_inputs = []
        batch_positions = []
        batch_count = 0

        start_time = time.time()

        # Slide window over the image - make sure we cover the entire image
        for i in range(steps_y + 1):  # +1 to ensure we reach the edge
            y = min(i * (window_size - overlap), last_y)
            y = max(0, y)  # Prevent negative indices

            if y > last_y and i > 0:  # Skip if we've already covered the entire height
                continue

            for j in range(steps_x + 1):  # +1 to ensure we reach the edge
                x = min(j * (window_size - overlap), last_x)
                x = max(0, x)  # Prevent negative indices

                if (
                    x > last_x and j > 0
                ):  # Skip if we've already covered the entire width
                    continue

                # Read window
                window = src.read(window=Window(x, y, window_size, window_size))

                # Check if window is valid
                if window.shape[1] != window_size or window.shape[2] != window_size:
                    # This can happen at image edges - adjust window size
                    current_height = window.shape[1]
                    current_width = window.shape[2]
                    if current_height == 0 or current_width == 0:
                        continue  # Skip empty windows
                else:
                    current_height = window_size
                    current_width = window_size

                # Normalize and prepare input
                image = window.astype(np.float32) / 255.0

                # Handle different number of bands
                if image.shape[0] > num_channels:
                    image = image[:num_channels]
                elif image.shape[0] < num_channels:
                    padded = np.zeros(
                        (num_channels, current_height, current_width), dtype=np.float32
                    )
                    padded[: image.shape[0]] = image
                    image = padded

                # Convert to tensor
                image_tensor = torch.tensor(image, device=device)

                # Add to batch
                batch_inputs.append(image_tensor)
                batch_positions.append((y, x, current_height, current_width))
                batch_count += 1

                # Process batch when it reaches the batch size or at the end
                if batch_count == batch_size or (i == steps_y and j == steps_x):
                    # Forward pass
                    with torch.no_grad():
                        outputs = model(batch_inputs)

                    # Process each output in the batch
                    for idx, output in enumerate(outputs):
                        y_pos, x_pos, h, w = batch_positions[idx]

                        # Create weight matrix that gives higher weight to center pixels
                        # This helps with smooth blending at boundaries
                        y_grid, x_grid = np.mgrid[0:h, 0:w]

                        # Calculate distance from each edge
                        dist_from_left = x_grid
                        dist_from_right = w - x_grid - 1
                        dist_from_top = y_grid
                        dist_from_bottom = h - y_grid - 1

                        # Combine distances (minimum distance to any edge)
                        edge_distance = np.minimum.reduce(
                            [
                                dist_from_left,
                                dist_from_right,
                                dist_from_top,
                                dist_from_bottom,
                            ]
                        )

                        # Convert to weight (higher weight for center pixels)
                        # Normalize to [0, 1]
                        edge_distance = np.minimum(edge_distance, overlap / 2)
                        weight = edge_distance / (overlap / 2)

                        # Get masks for predictions above threshold
                        if len(output["scores"]) > 0:
                            # Get all instances that meet confidence threshold
                            keep = output["scores"] > confidence_threshold
                            masks = output["masks"][keep].squeeze(1)

                            # Combine all instances into one mask
                            if len(masks) > 0:
                                combined_mask = torch.max(masks, dim=0)[0] > 0.5
                                combined_mask = (
                                    combined_mask.cpu().numpy().astype(np.float32)
                                )

                                # Apply weight to prediction
                                weighted_pred = combined_mask * weight

                                # Add to accumulators
                                pred_accumulator[
                                    y_pos : y_pos + h, x_pos : x_pos + w
                                ] += weighted_pred
                                count_accumulator[
                                    y_pos : y_pos + h, x_pos : x_pos + w
                                ] += weight

                    # Reset batch
                    batch_inputs = []
                    batch_positions = []
                    batch_count = 0

                    # Update progress bar
                    pbar.update(len(outputs))

        # Close progress bar
        pbar.close()

        # Calculate final mask by dividing accumulated predictions by counts
        # Handle division by zero
        mask = np.zeros((height, width), dtype=np.uint8)
        valid_pixels = count_accumulator > 0
        if np.any(valid_pixels):
            # Average predictions where we have data
            mask[valid_pixels] = (
                pred_accumulator[valid_pixels] / count_accumulator[valid_pixels] > 0.5
            ).astype(np.uint8)

        # Record time
        inference_time = time.time() - start_time
        print(f"Inference completed in {inference_time:.2f} seconds")

        # Save output
        with rasterio.open(output_path, "w", **out_meta) as dst:
            dst.write(mask, 1)

        print(f"Saved prediction to {output_path}")

        return output_path, inference_time

iou_coefficient(pred, target, smooth=1e-06, num_classes=None)

Calculate IoU coefficient for segmentation (binary or multi-class).

Parameters:

Name Type Description Default
pred Tensor

Predicted mask (probabilities or logits) with shape [C, H, W] or [H, W].

required
target Tensor

Ground truth mask with shape [H, W].

required
smooth float

Smoothing factor to avoid division by zero.

1e-06
num_classes int

Number of classes. If None, auto-detected.

None

Returns:

Name Type Description
float

Mean IoU coefficient across all classes.

Source code in geoai/train.py
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
def iou_coefficient(pred, target, smooth=1e-6, num_classes=None):
    """
    Calculate IoU coefficient for segmentation (binary or multi-class).

    Args:
        pred (torch.Tensor): Predicted mask (probabilities or logits) with shape [C, H, W] or [H, W].
        target (torch.Tensor): Ground truth mask with shape [H, W].
        smooth (float): Smoothing factor to avoid division by zero.
        num_classes (int, optional): Number of classes. If None, auto-detected.

    Returns:
        float: Mean IoU coefficient across all classes.
    """
    # Convert predictions to class predictions
    if pred.dim() == 3:  # [C, H, W] format
        pred = torch.softmax(pred, dim=0)
        pred_classes = torch.argmax(pred, dim=0)
    elif pred.dim() == 2:  # [H, W] format
        pred_classes = pred
    else:
        raise ValueError(f"Unexpected prediction dimensions: {pred.shape}")

    # Auto-detect number of classes if not provided
    if num_classes is None:
        num_classes = max(pred_classes.max().item(), target.max().item()) + 1

    # Calculate IoU for each class and average
    iou_scores = []
    for class_id in range(num_classes):
        pred_class = (pred_classes == class_id).float()
        target_class = (target == class_id).float()

        intersection = (pred_class * target_class).sum()
        union = pred_class.sum() + target_class.sum() - intersection

        if union > 0:
            iou = (intersection + smooth) / (union + smooth)
            iou_scores.append(iou.item())

    return sum(iou_scores) / len(iou_scores) if iou_scores else 0.0

object_detection(input_path, output_path, model_path, window_size=512, overlap=256, confidence_threshold=0.5, batch_size=4, num_channels=3, model=None, pretrained=True, device=None, **kwargs)

Perform object detection on a GeoTIFF using a pre-trained Mask R-CNN model.

Parameters:

Name Type Description Default
input_path str

Path to input GeoTIFF file.

required
output_path str

Path to save output mask GeoTIFF.

required
model_path str

Path to trained model weights.

required
window_size int

Size of sliding window for inference.

512
overlap int

Overlap between adjacent windows.

256
confidence_threshold float

Confidence threshold for predictions (0-1).

0.5
batch_size int

Batch size for inference.

4
num_channels int

Number of channels in the input image and model.

3
model Module

Predefined model. If None, a new model is created.

None
pretrained bool

Whether to use pretrained backbone for model loading.

True
device device

Device to run inference on. If None, uses CUDA if available.

None
**kwargs

Additional arguments passed to inference_on_geotiff.

{}

Returns:

Name Type Description
None

Output mask is saved to output_path.

Source code in geoai/train.py
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
def object_detection(
    input_path,
    output_path,
    model_path,
    window_size=512,
    overlap=256,
    confidence_threshold=0.5,
    batch_size=4,
    num_channels=3,
    model=None,
    pretrained=True,
    device=None,
    **kwargs,
):
    """
    Perform object detection on a GeoTIFF using a pre-trained Mask R-CNN model.

    Args:
        input_path (str): Path to input GeoTIFF file.
        output_path (str): Path to save output mask GeoTIFF.
        model_path (str): Path to trained model weights.
        window_size (int): Size of sliding window for inference.
        overlap (int): Overlap between adjacent windows.
        confidence_threshold (float): Confidence threshold for predictions (0-1).
        batch_size (int): Batch size for inference.
        num_channels (int): Number of channels in the input image and model.
        model (torch.nn.Module, optional): Predefined model. If None, a new model is created.
        pretrained (bool): Whether to use pretrained backbone for model loading.
        device (torch.device, optional): Device to run inference on. If None, uses CUDA if available.
        **kwargs: Additional arguments passed to inference_on_geotiff.

    Returns:
        None: Output mask is saved to output_path.
    """
    # Load your trained model
    if device is None:
        device = get_device()
    if model is None:
        model = get_instance_segmentation_model(
            num_classes=2, num_channels=num_channels, pretrained=pretrained
        )

    if not os.path.exists(model_path):
        try:
            model_path = download_model_from_hf(model_path)
        except Exception as e:
            raise FileNotFoundError(f"Model file not found: {model_path}")

    model.load_state_dict(torch.load(model_path, map_location=device))
    model.to(device)
    model.eval()

    inference_on_geotiff(
        model=model,
        geotiff_path=input_path,
        output_path=output_path,
        window_size=window_size,  # Adjust based on your model and memory
        overlap=overlap,  # Overlap to avoid edge artifacts
        confidence_threshold=confidence_threshold,
        batch_size=batch_size,  # Adjust based on your GPU memory
        num_channels=num_channels,
        device=device,
        **kwargs,
    )

object_detection_batch(input_paths, output_dir, model_path, filenames=None, window_size=512, overlap=256, confidence_threshold=0.5, batch_size=4, model=None, num_channels=3, pretrained=True, device=None, **kwargs)

Perform object detection on a GeoTIFF using a pre-trained Mask R-CNN model.

Parameters:

Name Type Description Default
input_paths str or list

Path(s) to input GeoTIFF file(s). If a directory is provided, all .tif files in that directory will be processed.

required
output_dir str

Directory to save output mask GeoTIFF files.

required
model_path str

Path to trained model weights.

required
filenames list

List of output filenames. If None, defaults to "_mask.tif" for each input file. If provided, must match the number of input files.

None
window_size int

Size of sliding window for inference.

512
overlap int

Overlap between adjacent windows.

256
confidence_threshold float

Confidence threshold for predictions (0-1).

0.5
batch_size int

Batch size for inference.

4
num_channels int

Number of channels in the input image and model.

3
model Module

Predefined model. If None, a new model is created.

None
pretrained bool

Whether to use pretrained backbone for model loading.

True
device device

Device to run inference on. If None, uses CUDA if available.

None
**kwargs

Additional arguments passed to inference_on_geotiff.

{}

Returns:

Name Type Description
None

Output mask is saved to output_path.

Source code in geoai/train.py
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
def object_detection_batch(
    input_paths,
    output_dir,
    model_path,
    filenames=None,
    window_size=512,
    overlap=256,
    confidence_threshold=0.5,
    batch_size=4,
    model=None,
    num_channels=3,
    pretrained=True,
    device=None,
    **kwargs,
):
    """
    Perform object detection on a GeoTIFF using a pre-trained Mask R-CNN model.

    Args:
        input_paths (str or list): Path(s) to input GeoTIFF file(s). If a directory is provided,
            all .tif files in that directory will be processed.
        output_dir (str): Directory to save output mask GeoTIFF files.
        model_path (str): Path to trained model weights.
        filenames (list, optional): List of output filenames. If None, defaults to
            "<input_filename>_mask.tif" for each input file.
            If provided, must match the number of input files.
        window_size (int): Size of sliding window for inference.
        overlap (int): Overlap between adjacent windows.
        confidence_threshold (float): Confidence threshold for predictions (0-1).
        batch_size (int): Batch size for inference.
        num_channels (int): Number of channels in the input image and model.
        model (torch.nn.Module, optional): Predefined model. If None, a new model is created.
        pretrained (bool): Whether to use pretrained backbone for model loading.
        device (torch.device, optional): Device to run inference on. If None, uses CUDA if available.
        **kwargs: Additional arguments passed to inference_on_geotiff.

    Returns:
        None: Output mask is saved to output_path.
    """
    # Load your trained model
    if device is None:
        device = get_device()

    if model is None:
        model = get_instance_segmentation_model(
            num_classes=2, num_channels=num_channels, pretrained=pretrained
        )

    if not os.path.exists(output_dir):
        os.makedirs(os.path.abspath(output_dir), exist_ok=True)

    if not os.path.exists(model_path):
        try:
            model_path = download_model_from_hf(model_path)
        except Exception as e:
            raise FileNotFoundError(f"Model file not found: {model_path}")

    model.load_state_dict(torch.load(model_path, map_location=device))
    model.to(device)
    model.eval()

    if isinstance(input_paths, str) and (not input_paths.endswith(".tif")):
        files = glob.glob(os.path.join(input_paths, "*.tif"))
        files.sort()
    elif isinstance(input_paths, str):
        files = [input_paths]

    if filenames is None:
        filenames = [
            os.path.join(output_dir, os.path.basename(f).replace(".tif", "_mask.tif"))
            for f in files
        ]
    else:
        if len(filenames) != len(files):
            raise ValueError("Number of filenames must match number of input files.")

    for index, file in enumerate(files):
        print(f"Processing file {index + 1}/{len(files)}: {file}")
        inference_on_geotiff(
            model=model,
            geotiff_path=file,
            output_path=filenames[index],
            window_size=window_size,  # Adjust based on your model and memory
            overlap=overlap,  # Overlap to avoid edge artifacts
            confidence_threshold=confidence_threshold,
            batch_size=batch_size,  # Adjust based on your GPU memory
            num_channels=num_channels,
            device=device,
            **kwargs,
        )

semantic_inference_on_geotiff(model, geotiff_path, output_path, window_size=512, overlap=256, batch_size=4, num_channels=3, num_classes=2, device=None, quiet=False, **kwargs)

Perform semantic segmentation inference on a large GeoTIFF using a sliding window approach.

Parameters:

Name Type Description Default
model Module

Trained semantic segmentation model.

required
geotiff_path str

Path to input GeoTIFF file.

required
output_path str

Path to save output mask GeoTIFF.

required
window_size int

Size of sliding window for inference.

512
overlap int

Overlap between adjacent windows.

256
batch_size int

Batch size for inference.

4
num_channels int

Number of channels to use from the input image.

3
num_classes int

Number of classes in the model output.

2
device device

Device to run inference on.

None
quiet bool

If True, suppress progress bar. Defaults to False.

False
**kwargs

Additional arguments.

{}

Returns:

Name Type Description
tuple

Tuple containing output path and inference time in seconds.

Source code in geoai/train.py
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
def semantic_inference_on_geotiff(
    model,
    geotiff_path,
    output_path,
    window_size=512,
    overlap=256,
    batch_size=4,
    num_channels=3,
    num_classes=2,
    device=None,
    quiet=False,
    **kwargs,
):
    """
    Perform semantic segmentation inference on a large GeoTIFF using a sliding window approach.

    Args:
        model (torch.nn.Module): Trained semantic segmentation model.
        geotiff_path (str): Path to input GeoTIFF file.
        output_path (str): Path to save output mask GeoTIFF.
        window_size (int): Size of sliding window for inference.
        overlap (int): Overlap between adjacent windows.
        batch_size (int): Batch size for inference.
        num_channels (int): Number of channels to use from the input image.
        num_classes (int): Number of classes in the model output.
        device (torch.device, optional): Device to run inference on.
        quiet (bool): If True, suppress progress bar. Defaults to False.
        **kwargs: Additional arguments.

    Returns:
        tuple: Tuple containing output path and inference time in seconds.
    """
    if device is None:
        device = get_device()

    # Put model in evaluation mode
    model.to(device)
    model.eval()

    # Open the GeoTIFF
    with rasterio.open(geotiff_path) as src:
        # Read metadata
        meta = src.meta
        height = src.height
        width = src.width

        # Update metadata for output raster
        out_meta = meta.copy()
        out_meta.update({"count": 1, "dtype": "uint8"})

        # Initialize accumulator arrays for multi-class probability blending
        # We'll accumulate probabilities for each class and then take argmax
        prob_accumulator = np.zeros((num_classes, height, width), dtype=np.float32)
        count_accumulator = np.zeros((height, width), dtype=np.float32)

        # Calculate steps
        steps_y = math.ceil((height - overlap) / (window_size - overlap))
        steps_x = math.ceil((width - overlap) / (window_size - overlap))
        last_y = height - window_size
        last_x = width - window_size

        total_windows = steps_y * steps_x
        if not quiet:
            print(f"Processing {total_windows} windows...")

        if not quiet:
            pbar = tqdm(total=total_windows)
        else:
            pbar = None

        batch_inputs = []
        batch_positions = []
        batch_count = 0

        start_time = time.time()

        for i in range(steps_y + 1):
            y = min(i * (window_size - overlap), last_y)
            y = max(0, y)

            if y > last_y and i > 0:
                continue

            for j in range(steps_x + 1):
                x = min(j * (window_size - overlap), last_x)
                x = max(0, x)

                if x > last_x and j > 0:
                    continue

                # Read window
                window = src.read(window=Window(x, y, window_size, window_size))

                if window.shape[1] == 0 or window.shape[2] == 0:
                    continue

                current_height = window.shape[1]
                current_width = window.shape[2]

                # Normalize and prepare input
                image = window.astype(np.float32) / 255.0

                # Handle different number of bands
                if image.shape[0] > num_channels:
                    image = image[:num_channels]
                elif image.shape[0] < num_channels:
                    padded = np.zeros(
                        (num_channels, current_height, current_width), dtype=np.float32
                    )
                    padded[: image.shape[0]] = image
                    image = padded

                # Convert to tensor
                image_tensor = torch.tensor(image, device=device)

                # Add to batch
                batch_inputs.append(image_tensor)
                batch_positions.append((y, x, current_height, current_width))
                batch_count += 1

                # Process batch
                if batch_count == batch_size or (i == steps_y and j == steps_x):
                    with torch.no_grad():
                        batch_tensor = torch.stack(batch_inputs)
                        outputs = model(batch_tensor)

                        # Apply softmax to get class probabilities
                        probs = torch.softmax(outputs, dim=1)

                    # Process each output in the batch
                    for idx, prob in enumerate(probs):
                        y_pos, x_pos, h, w = batch_positions[idx]

                        # Create weight matrix for blending
                        y_grid, x_grid = np.mgrid[0:h, 0:w]
                        dist_from_left = x_grid
                        dist_from_right = w - x_grid - 1
                        dist_from_top = y_grid
                        dist_from_bottom = h - y_grid - 1

                        edge_distance = np.minimum.reduce(
                            [
                                dist_from_left,
                                dist_from_right,
                                dist_from_top,
                                dist_from_bottom,
                            ]
                        )
                        edge_distance = np.minimum(edge_distance, overlap / 2)

                        # Avoid zero weights - use minimum weight of 0.1
                        weight = np.maximum(edge_distance / (overlap / 2), 0.1)

                        # For non-overlapping windows, use uniform weight
                        if overlap == 0:
                            weight = np.ones_like(weight)

                        # Convert probabilities to numpy [C, H, W]
                        prob_np = prob.cpu().numpy()

                        # Accumulate weighted probabilities for each class
                        y_slice = slice(y_pos, y_pos + h)
                        x_slice = slice(x_pos, x_pos + w)

                        # Add weighted probabilities for each class
                        for class_idx in range(num_classes):
                            prob_accumulator[class_idx, y_slice, x_slice] += (
                                prob_np[class_idx] * weight
                            )

                        # Update weight accumulator
                        count_accumulator[y_slice, x_slice] += weight

                    # Reset batch
                    batch_inputs = []
                    batch_positions = []
                    batch_count = 0
                    if pbar is not None:
                        pbar.update(len(probs))

        if pbar is not None:
            pbar.close()

        # Calculate final mask by taking argmax of accumulated probabilities
        mask = np.zeros((height, width), dtype=np.uint8)
        valid_pixels = count_accumulator > 0

        if np.any(valid_pixels):
            # Normalize accumulated probabilities by weights
            normalized_probs = np.zeros_like(prob_accumulator)
            for class_idx in range(num_classes):
                normalized_probs[class_idx, valid_pixels] = (
                    prob_accumulator[class_idx, valid_pixels]
                    / count_accumulator[valid_pixels]
                )

            # Take argmax to get final class predictions
            mask[valid_pixels] = np.argmax(
                normalized_probs[:, valid_pixels], axis=0
            ).astype(np.uint8)

            # Check class distribution in predictions (summary only)
            unique_classes, class_counts = np.unique(
                mask[valid_pixels], return_counts=True
            )
            bg_ratio = np.sum(mask == 0) / mask.size
            if not quiet:
                print(
                    f"Predicted classes: {len(unique_classes)} classes, Background: {bg_ratio:.1%}"
                )

        inference_time = time.time() - start_time
        if not quiet:
            print(f"Inference completed in {inference_time:.2f} seconds")

        # Save output
        out_dir = os.path.abspath(os.path.dirname(output_path))
        os.makedirs(out_dir, exist_ok=True)
        with rasterio.open(output_path, "w", **out_meta) as dst:
            dst.write(mask, 1)

        if not quiet:
            print(f"Saved prediction to {output_path}")

        return output_path, inference_time

semantic_inference_on_image(model, image_path, output_path, window_size=512, overlap=256, batch_size=4, num_channels=3, num_classes=2, device=None, binary_output=True, quiet=False, **kwargs)

Perform semantic segmentation inference on a regular image (JPG, PNG, etc.) using a sliding window approach.

Parameters:

Name Type Description Default
model Module

Trained semantic segmentation model.

required
image_path str

Path to input image file (JPG, PNG, etc.).

required
output_path str

Path to save output mask image.

required
window_size int

Size of sliding window for inference.

512
overlap int

Overlap between adjacent windows.

256
batch_size int

Batch size for inference.

4
num_channels int

Number of channels to use from the input image.

3
num_classes int

Number of classes in the model output.

2
device device

Device to run inference on.

None
binary_output bool

If True, convert multi-class output to binary (class > 0).

True
quiet bool

If True, suppress progress bar. Defaults to False.

False
**kwargs

Additional arguments.

{}

Returns:

Name Type Description
tuple

Tuple containing output path and inference time in seconds.

Source code in geoai/train.py
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
def semantic_inference_on_image(
    model,
    image_path,
    output_path,
    window_size=512,
    overlap=256,
    batch_size=4,
    num_channels=3,
    num_classes=2,
    device=None,
    binary_output=True,
    quiet=False,
    **kwargs,
):
    """
    Perform semantic segmentation inference on a regular image (JPG, PNG, etc.) using a sliding window approach.

    Args:
        model (torch.nn.Module): Trained semantic segmentation model.
        image_path (str): Path to input image file (JPG, PNG, etc.).
        output_path (str): Path to save output mask image.
        window_size (int): Size of sliding window for inference.
        overlap (int): Overlap between adjacent windows.
        batch_size (int): Batch size for inference.
        num_channels (int): Number of channels to use from the input image.
        num_classes (int): Number of classes in the model output.
        device (torch.device, optional): Device to run inference on.
        binary_output (bool): If True, convert multi-class output to binary (class > 0).
        quiet (bool): If True, suppress progress bar. Defaults to False.
        **kwargs: Additional arguments.

    Returns:
        tuple: Tuple containing output path and inference time in seconds.
    """
    from PIL import Image

    if device is None:
        device = get_device()

    # Put model in evaluation mode
    model.to(device)
    model.eval()

    # Open the image using PIL
    with Image.open(image_path) as pil_img:
        # Convert to RGB if needed
        if pil_img.mode != "RGB":
            pil_img = pil_img.convert("RGB")

        # Convert to numpy array [H, W, C]
        img_array = np.array(pil_img, dtype=np.uint8)
        height, width = img_array.shape[:2]

        # Convert to [C, H, W] format like rasterio
        img_array = np.transpose(img_array, (2, 0, 1))

        if not quiet:
            print(f"Processing image: {width}x{height}")

        # Initialize accumulator arrays for multi-class probability blending
        prob_accumulator = np.zeros((num_classes, height, width), dtype=np.float32)
        count_accumulator = np.zeros((height, width), dtype=np.float32)

        # Calculate steps
        steps_y = math.ceil((height - overlap) / (window_size - overlap))
        steps_x = math.ceil((width - overlap) / (window_size - overlap))
        last_y = height - window_size
        last_x = width - window_size

        total_windows = steps_y * steps_x
        if not quiet:
            print(f"Processing {total_windows} windows...")

        if not quiet:
            pbar = tqdm(total=total_windows)
        else:
            pbar = None

        batch_inputs = []
        batch_positions = []
        batch_count = 0

        start_time = time.time()

        for i in range(steps_y + 1):
            y = min(i * (window_size - overlap), last_y)
            y = max(0, y)

            if y > last_y and i > 0:
                continue

            for j in range(steps_x + 1):
                x = min(j * (window_size - overlap), last_x)
                x = max(0, x)

                if x > last_x and j > 0:
                    continue

                # Extract window from image array
                y_end = min(y + window_size, height)
                x_end = min(x + window_size, width)
                window = img_array[:, y:y_end, x:x_end]

                if window.shape[1] == 0 or window.shape[2] == 0:
                    continue

                current_height = window.shape[1]
                current_width = window.shape[2]

                # Pad window to window_size if needed
                if current_height < window_size or current_width < window_size:
                    padded_window = np.zeros(
                        (window.shape[0], window_size, window_size), dtype=window.dtype
                    )
                    padded_window[:, :current_height, :current_width] = window
                    window = padded_window

                # Normalize and prepare input
                image = window.astype(np.float32) / 255.0

                # Handle different number of channels
                if image.shape[0] > num_channels:
                    image = image[:num_channels]
                elif image.shape[0] < num_channels:
                    padded = np.zeros(
                        (num_channels, image.shape[1], image.shape[2]), dtype=np.float32
                    )
                    padded[: image.shape[0]] = image
                    image = padded

                # Convert to tensor
                image_tensor = torch.tensor(image, device=device)

                # Add to batch
                batch_inputs.append(image_tensor)
                batch_positions.append((y, x, current_height, current_width))
                batch_count += 1

                # Process batch
                if batch_count == batch_size or (i == steps_y and j == steps_x):
                    with torch.no_grad():
                        batch_tensor = torch.stack(batch_inputs)
                        outputs = model(batch_tensor)

                        # Apply softmax to get class probabilities
                        probs = torch.softmax(outputs, dim=1)

                    # Process each output in the batch
                    for idx, prob in enumerate(probs):
                        y_pos, x_pos, h, w = batch_positions[idx]

                        # Create weight matrix for blending
                        y_grid, x_grid = np.mgrid[0:h, 0:w]
                        dist_from_left = x_grid
                        dist_from_right = w - x_grid - 1
                        dist_from_top = y_grid
                        dist_from_bottom = h - y_grid - 1

                        edge_distance = np.minimum.reduce(
                            [
                                dist_from_left,
                                dist_from_right,
                                dist_from_top,
                                dist_from_bottom,
                            ]
                        )
                        edge_distance = np.minimum(edge_distance, overlap / 2)

                        # Avoid zero weights - use minimum weight of 0.1
                        weight = np.maximum(edge_distance / (overlap / 2), 0.1)

                        # For non-overlapping windows, use uniform weight
                        if overlap == 0:
                            weight = np.ones_like(weight)

                        # Convert probabilities to numpy [C, H, W] - crop to actual size
                        prob_np = prob.cpu().numpy()[:, :h, :w]

                        # Accumulate weighted probabilities for each class
                        y_slice = slice(y_pos, y_pos + h)
                        x_slice = slice(x_pos, x_pos + w)

                        # Add weighted probabilities for each class
                        for class_idx in range(num_classes):
                            prob_accumulator[class_idx, y_slice, x_slice] += (
                                prob_np[class_idx] * weight
                            )

                        # Update weight accumulator
                        count_accumulator[y_slice, x_slice] += weight

                    # Reset batch
                    batch_inputs = []
                    batch_positions = []
                    batch_count = 0
                    if pbar is not None:
                        pbar.update(len(probs))

        if pbar is not None:
            pbar.close()

        # Calculate final mask by taking argmax of accumulated probabilities
        mask = np.zeros((height, width), dtype=np.uint8)
        valid_pixels = count_accumulator > 0

        if np.any(valid_pixels):
            # Normalize accumulated probabilities by weights
            normalized_probs = np.zeros_like(prob_accumulator)
            for class_idx in range(num_classes):
                normalized_probs[class_idx, valid_pixels] = (
                    prob_accumulator[class_idx, valid_pixels]
                    / count_accumulator[valid_pixels]
                )

            # Take argmax to get final class predictions
            mask[valid_pixels] = np.argmax(
                normalized_probs[:, valid_pixels], axis=0
            ).astype(np.uint8)

            # Check class distribution in predictions before binary conversion
            unique_classes, class_counts = np.unique(mask, return_counts=True)
            # Convert numpy types to regular Python types for cleaner output
            class_distribution = {
                int(cls): int(count) for cls, count in zip(unique_classes, class_counts)
            }
            if not quiet:
                print(f"Raw predicted classes and counts: {class_distribution}")

            # Convert to binary if requested and num_classes == 2
            if binary_output and num_classes == 2:
                # For binary segmentation, convert class 1 to 255 (white) and class 0 to 0 (black)
                # Use proper thresholding to ensure only 0 and 255 values
                binary_mask = np.zeros_like(mask)
                binary_mask[mask > 0] = 255
                mask = binary_mask

                # Final check
                unique_classes, class_counts = np.unique(mask, return_counts=True)
                # Convert numpy types to regular Python types for cleaner output
                binary_distribution = {
                    int(cls): int(count)
                    for cls, count in zip(unique_classes, class_counts)
                }
                if not quiet:
                    print(f"Binary predicted classes and counts: {binary_distribution}")

        inference_time = time.time() - start_time
        if not quiet:
            print(f"Inference completed in {inference_time:.2f} seconds")

        # Save output as image
        # For binary masks, use PNG to avoid JPEG compression artifacts
        if binary_output and num_classes == 2:
            # Change extension to PNG if binary output to preserve exact values
            output_path_png = os.path.splitext(output_path)[0] + ".png"
            output_img = Image.fromarray(mask, mode="L")
            out_dir = os.path.abspath(os.path.dirname(output_path))
            os.makedirs(out_dir, exist_ok=True)
            output_img.save(output_path_png)
            if not quiet:
                print(
                    f"Saved binary prediction to {output_path_png} (PNG format to preserve exact values)"
                )

            # Also save the original requested format for compatibility
            if output_path != output_path_png:
                output_img.save(output_path)
                print(f"Also saved to {output_path} (may have compression artifacts)")
        else:
            output_img = Image.fromarray(mask, mode="L")
            output_img.save(output_path)
            if not quiet:
                print(f"Saved prediction to {output_path}")

        return output_path, inference_time

semantic_segmentation(input_path, output_path, model_path, architecture='unet', encoder_name='resnet34', num_channels=3, num_classes=2, window_size=512, overlap=256, batch_size=4, device=None, quiet=False, **kwargs)

Perform semantic segmentation on an image file using a trained model.

This function automatically detects the input format and uses the appropriate inference method for either GeoTIFF files or regular image formats (JPG, PNG, etc.).

Parameters:

Name Type Description Default
input_path str

Path to input image file (GeoTIFF, JPG, PNG, etc.).

required
output_path str

Path to save output mask file.

required
model_path str

Path to trained model weights.

required
architecture str

Model architecture used for training.

'unet'
encoder_name str

Encoder backbone name used for training.

'resnet34'
num_channels int

Number of channels in the input image and model.

3
num_classes int

Number of classes in the model.

2
window_size int

Size of sliding window for inference.

512
overlap int

Overlap between adjacent windows.

256
batch_size int

Batch size for inference.

4
device device

Device to run inference on.

None
quiet bool

If True, suppress progress bar. Defaults to False.

False
**kwargs

Additional arguments.

{}

Returns:

Name Type Description
None

Output mask is saved to output_path.

Source code in geoai/train.py
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
def semantic_segmentation(
    input_path,
    output_path,
    model_path,
    architecture="unet",
    encoder_name="resnet34",
    num_channels=3,
    num_classes=2,
    window_size=512,
    overlap=256,
    batch_size=4,
    device=None,
    quiet=False,
    **kwargs,
):
    """
    Perform semantic segmentation on an image file using a trained model.

    This function automatically detects the input format and uses the appropriate
    inference method for either GeoTIFF files or regular image formats (JPG, PNG, etc.).

    Args:
        input_path (str): Path to input image file (GeoTIFF, JPG, PNG, etc.).
        output_path (str): Path to save output mask file.
        model_path (str): Path to trained model weights.
        architecture (str): Model architecture used for training.
        encoder_name (str): Encoder backbone name used for training.
        num_channels (int): Number of channels in the input image and model.
        num_classes (int): Number of classes in the model.
        window_size (int): Size of sliding window for inference.
        overlap (int): Overlap between adjacent windows.
        batch_size (int): Batch size for inference.
        device (torch.device, optional): Device to run inference on.
        quiet (bool): If True, suppress progress bar. Defaults to False.
        **kwargs: Additional arguments.

    Returns:
        None: Output mask is saved to output_path.
    """
    if device is None:
        device = get_device()

    # Detect file format based on extension
    input_ext = os.path.splitext(input_path)[1].lower()
    is_geotiff = input_ext in [".tif", ".tiff"]

    if not quiet:
        print(
            f"Input file format: {'GeoTIFF' if is_geotiff else 'Regular image'} ({input_ext})"
        )

    # Load model
    model = get_smp_model(
        architecture=architecture,
        encoder_name=encoder_name,
        encoder_weights=None,  # We're loading trained weights
        in_channels=num_channels,
        classes=num_classes,
        activation=None,
    )

    if not os.path.exists(model_path):
        try:
            model_path = download_model_from_hf(model_path)
        except Exception as e:
            raise FileNotFoundError(f"Model file not found: {model_path}")

    model.load_state_dict(torch.load(model_path, map_location=device))
    model.to(device)
    model.eval()

    # Use appropriate inference function based on file format
    if is_geotiff:
        semantic_inference_on_geotiff(
            model=model,
            geotiff_path=input_path,
            output_path=output_path,
            window_size=window_size,
            overlap=overlap,
            batch_size=batch_size,
            num_channels=num_channels,
            num_classes=num_classes,
            device=device,
            quiet=quiet,
            **kwargs,
        )
    else:
        # Create output directory if it doesn't exist
        os.makedirs(os.path.abspath(os.path.dirname(output_path)), exist_ok=True)

        semantic_inference_on_image(
            model=model,
            image_path=input_path,
            output_path=output_path,
            window_size=window_size,
            overlap=overlap,
            batch_size=batch_size,
            num_channels=num_channels,
            num_classes=num_classes,
            device=device,
            binary_output=True,  # Convert to binary output for better visualization
            quiet=quiet,
            **kwargs,
        )

semantic_segmentation_batch(input_dir, output_dir, model_path, architecture='unet', encoder_name='resnet34', num_channels=3, num_classes=2, window_size=512, overlap=256, batch_size=4, device=None, filenames=None, quiet=False, **kwargs)

Perform semantic segmentation on a batch of images from an input directory.

This function processes all images in a directory and saves the results to an output directory. It automatically detects the input format and uses the appropriate inference method for either GeoTIFF files or regular image formats (JPG, PNG, etc.). For GeoTIFF inputs, outputs are saved as GeoTIFF. For other formats, outputs are saved as PNG to preserve exact values.

Parameters:

Name Type Description Default
input_dir str

Directory containing input image files to process.

required
output_dir str

Directory to save output mask files.

required
model_path str

Path to trained model weights.

required
architecture str

Model architecture used for training. Defaults to "unet".

'unet'
encoder_name str

Encoder backbone name used for training. Defaults to "resnet34".

'resnet34'
num_channels int

Number of channels in the input image and model. Defaults to 3.

3
num_classes int

Number of classes in the model. Defaults to 2.

2
window_size int

Size of sliding window for inference. Defaults to 512.

512
overlap int

Overlap between adjacent windows. Defaults to 256.

256
batch_size int

Batch size for inference. Defaults to 4.

4
device device

Device to run inference on. If None, uses CUDA if available.

None
filenames list

List of output filenames. If None, defaults to "_mask." for each input file where is "tif" for GeoTIFF inputs and "png" for other formats. If provided, must match the number of input files.

None
quiet bool

If True, suppress progress bar. Defaults to False.

False
**kwargs

Additional arguments passed to the inference functions.

{}

Returns:

Name Type Description
None

Output masks are saved to output_dir.

Raises:

Type Description
FileNotFoundError

If input_dir doesn't exist or contains no supported image files.

ValueError

If filenames is provided but doesn't match the number of input files.

Source code in geoai/train.py
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
def semantic_segmentation_batch(
    input_dir,
    output_dir,
    model_path,
    architecture="unet",
    encoder_name="resnet34",
    num_channels=3,
    num_classes=2,
    window_size=512,
    overlap=256,
    batch_size=4,
    device=None,
    filenames=None,
    quiet=False,
    **kwargs,
):
    """
    Perform semantic segmentation on a batch of images from an input directory.

    This function processes all images in a directory and saves the results to an output directory.
    It automatically detects the input format and uses the appropriate inference method for either
    GeoTIFF files or regular image formats (JPG, PNG, etc.). For GeoTIFF inputs, outputs are saved
    as GeoTIFF. For other formats, outputs are saved as PNG to preserve exact values.

    Args:
        input_dir (str): Directory containing input image files to process.
        output_dir (str): Directory to save output mask files.
        model_path (str): Path to trained model weights.
        architecture (str): Model architecture used for training. Defaults to "unet".
        encoder_name (str): Encoder backbone name used for training. Defaults to "resnet34".
        num_channels (int): Number of channels in the input image and model. Defaults to 3.
        num_classes (int): Number of classes in the model. Defaults to 2.
        window_size (int): Size of sliding window for inference. Defaults to 512.
        overlap (int): Overlap between adjacent windows. Defaults to 256.
        batch_size (int): Batch size for inference. Defaults to 4.
        device (torch.device, optional): Device to run inference on. If None, uses CUDA if available.
        filenames (list, optional): List of output filenames. If None, defaults to
            "<input_filename>_mask.<ext>" for each input file where <ext> is "tif" for GeoTIFF
            inputs and "png" for other formats. If provided, must match the number of input files.
        quiet (bool): If True, suppress progress bar. Defaults to False.
        **kwargs: Additional arguments passed to the inference functions.

    Returns:
        None: Output masks are saved to output_dir.

    Raises:
        FileNotFoundError: If input_dir doesn't exist or contains no supported image files.
        ValueError: If filenames is provided but doesn't match the number of input files.
    """
    if device is None:
        device = get_device()

    # Check if input directory exists
    if not os.path.exists(input_dir):
        raise FileNotFoundError(f"Input directory does not exist: {input_dir}")

    # Create output directory if it doesn't exist
    os.makedirs(os.path.abspath(output_dir), exist_ok=True)

    # Get all supported image files
    image_extensions = (".tif", ".tiff", ".png", ".jpg", ".jpeg")
    image_files = sorted(
        [
            os.path.join(input_dir, f)
            for f in os.listdir(input_dir)
            if f.lower().endswith(image_extensions)
        ]
    )

    if len(image_files) == 0:
        raise FileNotFoundError(f"No supported image files found in {input_dir}")

    print(f"Found {len(image_files)} image files to process")

    # Load model once for all images
    model = get_smp_model(
        architecture=architecture,
        encoder_name=encoder_name,
        encoder_weights=None,  # We're loading trained weights
        in_channels=num_channels,
        classes=num_classes,
        activation=None,
    )

    if not os.path.exists(model_path):
        try:
            model_path = download_model_from_hf(model_path)
        except Exception as e:
            raise FileNotFoundError(f"Model file not found: {model_path}")

    model.load_state_dict(torch.load(model_path, map_location=device))
    model.to(device)
    model.eval()

    # Generate output filenames if not provided
    if filenames is None:
        filenames = []
        for image_file in image_files:
            base_name = os.path.splitext(os.path.basename(image_file))[0]
            input_ext = os.path.splitext(image_file)[1].lower()

            # Use GeoTIFF extension for GeoTIFF inputs, PNG for others
            if input_ext in [".tif", ".tiff"]:
                output_ext = ".tif"
            else:
                output_ext = ".png"

            output_filename = f"{base_name}_mask{output_ext}"
            filenames.append(os.path.join(output_dir, output_filename))
    else:
        # Validate filenames list
        if len(filenames) != len(image_files):
            raise ValueError(
                f"Number of filenames ({len(filenames)}) must match number of input files ({len(image_files)})"
            )

    # Process each image
    for i, (input_path, output_path) in enumerate(zip(image_files, filenames)):
        print(
            f"Processing file {i + 1}/{len(image_files)}: {os.path.basename(input_path)}"
        )

        # Detect file format based on extension
        input_ext = os.path.splitext(input_path)[1].lower()
        is_geotiff = input_ext in [".tif", ".tiff"]

        try:
            # Use appropriate inference function based on file format
            if is_geotiff:
                semantic_inference_on_geotiff(
                    model=model,
                    geotiff_path=input_path,
                    output_path=output_path,
                    window_size=window_size,
                    overlap=overlap,
                    batch_size=batch_size,
                    num_channels=num_channels,
                    num_classes=num_classes,
                    device=device,
                    quiet=quiet,
                    **kwargs,
                )
            else:
                semantic_inference_on_image(
                    model=model,
                    image_path=input_path,
                    output_path=output_path,
                    window_size=window_size,
                    overlap=overlap,
                    batch_size=batch_size,
                    num_channels=num_channels,
                    num_classes=num_classes,
                    device=device,
                    binary_output=True,  # Convert to binary output for better visualization
                    quiet=quiet,
                    **kwargs,
                )
        except Exception as e:
            print(f"Error processing {input_path}: {str(e)}")
            continue

    print(f"Batch processing completed. Results saved to {output_dir}")

train_MaskRCNN_model(images_dir, labels_dir, output_dir, num_channels=3, model=None, pretrained=True, pretrained_model_path=None, batch_size=4, num_epochs=10, learning_rate=0.005, seed=42, val_split=0.2, visualize=False, resume_training=False, print_freq=10, device=None, verbose=True)

Train and evaluate Mask R-CNN model for instance segmentation.

This function trains a Mask R-CNN model for instance segmentation using the provided dataset. It supports loading a pretrained model to either initialize the backbone or to continue training from a specific checkpoint.

Parameters:

Name Type Description Default
images_dir str

Directory containing image GeoTIFF files.

required
labels_dir str

Directory containing label GeoTIFF files.

required
output_dir str

Directory to save model checkpoints and results.

required
num_channels int

Number of input channels. If None, auto-detected. Defaults to 3.

3
model Module

Predefined model. If None, a new model is created.

None
pretrained bool

Whether to use pretrained backbone. This is ignored if pretrained_model_path is provided. Defaults to True.

True
pretrained_model_path str

Path to a .pth file to load as a pretrained model for continued training. Defaults to None.

None
batch_size int

Batch size for training. Defaults to 4.

4
num_epochs int

Number of training epochs. Defaults to 10.

10
learning_rate float

Initial learning rate. Defaults to 0.005.

0.005
seed int

Random seed for reproducibility. Defaults to 42.

42
val_split float

Fraction of data to use for validation (0-1). Defaults to 0.2.

0.2
visualize bool

Whether to generate visualizations of model predictions. Defaults to False.

False
resume_training bool

If True and pretrained_model_path is provided, will try to load optimizer and scheduler states as well. Defaults to False.

False
print_freq int

Frequency of printing training progress. Defaults to 10.

10
device device

Device to train on. If None, uses CUDA if available.

None
verbose bool

If True, prints detailed training progress. Defaults to True.

True

Raises:

Type Description
FileNotFoundError

If pretrained_model_path is provided but file doesn't exist.

RuntimeError

If there's an issue loading the pretrained model.

Source code in geoai/train.py
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
def train_MaskRCNN_model(
    images_dir,
    labels_dir,
    output_dir,
    num_channels=3,
    model=None,
    pretrained=True,
    pretrained_model_path=None,
    batch_size=4,
    num_epochs=10,
    learning_rate=0.005,
    seed=42,
    val_split=0.2,
    visualize=False,
    resume_training=False,
    print_freq=10,
    device=None,
    verbose=True,
):
    """Train and evaluate Mask R-CNN model for instance segmentation.

    This function trains a Mask R-CNN model for instance segmentation using the
    provided dataset. It supports loading a pretrained model to either initialize
    the backbone or to continue training from a specific checkpoint.

    Args:
        images_dir (str): Directory containing image GeoTIFF files.
        labels_dir (str): Directory containing label GeoTIFF files.
        output_dir (str): Directory to save model checkpoints and results.
        num_channels (int, optional): Number of input channels. If None, auto-detected.
            Defaults to 3.
        model (torch.nn.Module, optional): Predefined model. If None, a new model is created.
        pretrained (bool): Whether to use pretrained backbone. This is ignored if
            pretrained_model_path is provided. Defaults to True.
        pretrained_model_path (str, optional): Path to a .pth file to load as a
            pretrained model for continued training. Defaults to None.
        batch_size (int): Batch size for training. Defaults to 4.
        num_epochs (int): Number of training epochs. Defaults to 10.
        learning_rate (float): Initial learning rate. Defaults to 0.005.
        seed (int): Random seed for reproducibility. Defaults to 42.
        val_split (float): Fraction of data to use for validation (0-1). Defaults to 0.2.
        visualize (bool): Whether to generate visualizations of model predictions.
            Defaults to False.
        resume_training (bool): If True and pretrained_model_path is provided,
            will try to load optimizer and scheduler states as well. Defaults to False.
        print_freq (int): Frequency of printing training progress. Defaults to 10.
        device (torch.device): Device to train on. If None, uses CUDA if available.
        verbose (bool): If True, prints detailed training progress. Defaults to True.
    Returns:
        None: Model weights are saved to output_dir.

    Raises:
        FileNotFoundError: If pretrained_model_path is provided but file doesn't exist.
        RuntimeError: If there's an issue loading the pretrained model.
    """

    import datetime

    # Set random seeds for reproducibility
    torch.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    # Create output directory
    os.makedirs(os.path.abspath(output_dir), exist_ok=True)

    # Get device
    if device is None:
        device = get_device()
    print(f"Using device: {device}")

    # Get all image and label files
    # Support multiple image formats: GeoTIFF, PNG, JPG, JPEG, TIF, TIFF
    image_extensions = (".tif", ".tiff", ".png", ".jpg", ".jpeg")
    label_extensions = (".tif", ".tiff", ".png", ".jpg", ".jpeg")

    image_files = sorted(
        [
            os.path.join(images_dir, f)
            for f in os.listdir(images_dir)
            if f.lower().endswith(image_extensions)
        ]
    )
    label_files = sorted(
        [
            os.path.join(labels_dir, f)
            for f in os.listdir(labels_dir)
            if f.lower().endswith(label_extensions)
        ]
    )

    print(f"Found {len(image_files)} image files and {len(label_files)} label files")

    # Ensure matching files
    if len(image_files) != len(label_files):
        print("Warning: Number of image files and label files don't match!")
        # Find matching files by basename
        basenames = [os.path.basename(f) for f in image_files]
        label_files = [
            os.path.join(labels_dir, os.path.basename(f))
            for f in image_files
            if os.path.exists(os.path.join(labels_dir, os.path.basename(f)))
        ]
        image_files = [
            f
            for f, b in zip(image_files, basenames)
            if os.path.exists(os.path.join(labels_dir, b))
        ]
        print(f"Using {len(image_files)} matching files")

    # Split data into train and validation sets
    train_imgs, val_imgs, train_labels, val_labels = train_test_split(
        image_files, label_files, test_size=val_split, random_state=seed
    )

    print(f"Training on {len(train_imgs)} images, validating on {len(val_imgs)} images")

    # Create datasets
    train_dataset = ObjectDetectionDataset(
        train_imgs, train_labels, transforms=get_transform(train=True)
    )
    val_dataset = ObjectDetectionDataset(
        val_imgs, val_labels, transforms=get_transform(train=False)
    )

    # Create data loaders
    # Use num_workers=0 on macOS and Windows to avoid multiprocessing issues
    # Windows often has issues with multiprocessing in Jupyter notebooks
    num_workers = 0 if platform.system() in ["Darwin", "Windows"] else 4

    train_loader = DataLoader(
        train_dataset,
        batch_size=batch_size,
        shuffle=True,
        collate_fn=collate_fn,
        num_workers=num_workers,
    )

    val_loader = DataLoader(
        val_dataset,
        batch_size=batch_size,
        shuffle=False,
        collate_fn=collate_fn,
        num_workers=num_workers,
    )

    # Initialize model (2 classes: background and building)
    if model is None:
        model = get_instance_segmentation_model(
            num_classes=2, num_channels=num_channels, pretrained=pretrained
        )
    model.to(device)

    # Set up optimizer
    params = [p for p in model.parameters() if p.requires_grad]
    optimizer = torch.optim.SGD(
        params, lr=learning_rate, momentum=0.9, weight_decay=0.0005
    )

    # Set up learning rate scheduler
    lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.8)

    # Initialize training variables
    start_epoch = 0
    best_iou = 0

    # Load pretrained model if provided
    if pretrained_model_path:
        if not os.path.exists(pretrained_model_path):
            raise FileNotFoundError(
                f"Pretrained model file not found: {pretrained_model_path}"
            )

        print(f"Loading pretrained model from: {pretrained_model_path}")
        try:
            # Check if it's a full checkpoint or just model weights
            checkpoint = torch.load(pretrained_model_path, map_location=device)

            if isinstance(checkpoint, dict) and "model_state_dict" in checkpoint:
                # It's a checkpoint with extra information
                model.load_state_dict(checkpoint["model_state_dict"])

                if resume_training:
                    # Resume from checkpoint
                    start_epoch = checkpoint.get("epoch", 0) + 1
                    best_iou = checkpoint.get("best_iou", 0)

                    if "optimizer_state_dict" in checkpoint:
                        optimizer.load_state_dict(checkpoint["optimizer_state_dict"])

                    if "scheduler_state_dict" in checkpoint:
                        lr_scheduler.load_state_dict(checkpoint["scheduler_state_dict"])

                    print(f"Resuming training from epoch {start_epoch}")
                    print(f"Previous best IoU: {best_iou:.4f}")
            else:
                # Assume it's just the model weights
                model.load_state_dict(checkpoint)

            print("Pretrained model loaded successfully")
        except Exception as e:
            raise RuntimeError(f"Failed to load pretrained model: {str(e)}")

    # Training loop
    for epoch in range(start_epoch, num_epochs):
        # Train one epoch
        train_loss = train_one_epoch(
            model, optimizer, train_loader, device, epoch, print_freq, verbose
        )

        # Update learning rate
        lr_scheduler.step()

        # Evaluate
        eval_metrics = evaluate(model, val_loader, device)

        # Print metrics
        print(
            f"Epoch {epoch+1}/{num_epochs}: Train Loss: {train_loss:.4f}, Val Loss: {eval_metrics['loss']:.4f}, Val IoU: {eval_metrics['IoU']:.4f}"
        )

        # Save best model
        if eval_metrics["IoU"] > best_iou:
            best_iou = eval_metrics["IoU"]
            print(f"Saving best model with IoU: {best_iou:.4f}")
            torch.save(model.state_dict(), os.path.join(output_dir, "best_model.pth"))

        # Save checkpoint every 10 epochs
        if (epoch + 1) % 10 == 0 or epoch == num_epochs - 1:
            torch.save(
                {
                    "epoch": epoch,
                    "model_state_dict": model.state_dict(),
                    "optimizer_state_dict": optimizer.state_dict(),
                    "scheduler_state_dict": lr_scheduler.state_dict(),
                    "best_iou": best_iou,
                },
                os.path.join(output_dir, f"checkpoint_epoch_{epoch+1}.pth"),
            )

    # Save final model
    torch.save(model.state_dict(), os.path.join(output_dir, "final_model.pth"))

    # Save full checkpoint of final state
    torch.save(
        {
            "epoch": num_epochs - 1,
            "model_state_dict": model.state_dict(),
            "optimizer_state_dict": optimizer.state_dict(),
            "scheduler_state_dict": lr_scheduler.state_dict(),
            "best_iou": best_iou,
        },
        os.path.join(output_dir, "final_checkpoint.pth"),
    )

    # Load best model for evaluation and visualization
    model.load_state_dict(torch.load(os.path.join(output_dir, "best_model.pth")))

    # Final evaluation
    final_metrics = evaluate(model, val_loader, device)
    print(
        f"Final Evaluation - Loss: {final_metrics['loss']:.4f}, IoU: {final_metrics['IoU']:.4f}"
    )

    # Visualize results
    if visualize:
        print("Generating visualizations...")
        visualize_predictions(
            model,
            val_dataset,
            device,
            num_samples=5,
            output_dir=os.path.join(output_dir, "visualizations"),
        )

    # Save training summary
    with open(os.path.join(output_dir, "training_summary.txt"), "w") as f:
        f.write(
            f"Training completed on: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
        )
        f.write(f"Total epochs: {num_epochs}\n")
        f.write(f"Best validation IoU: {best_iou:.4f}\n")
        f.write(f"Final validation IoU: {final_metrics['IoU']:.4f}\n")
        f.write(f"Final validation loss: {final_metrics['loss']:.4f}\n")

        if pretrained_model_path:
            f.write(f"Started from pretrained model: {pretrained_model_path}\n")
            if resume_training:
                f.write(f"Resumed training from epoch {start_epoch}\n")

    print(f"Training complete! Trained model saved to {output_dir}")

train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10, verbose=True)

Train the model for one epoch.

Parameters:

Name Type Description Default
model Module

The model to train.

required
optimizer Optimizer

The optimizer to use.

required
data_loader DataLoader

DataLoader for training data.

required
device device

Device to train on.

required
epoch int

Current epoch number.

required
print_freq int

How often to print progress.

10
verbose bool

Whether to print detailed progress.

True

Returns:

Name Type Description
float

Average loss for the epoch.

Source code in geoai/train.py
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
def train_one_epoch(
    model, optimizer, data_loader, device, epoch, print_freq=10, verbose=True
):
    """
    Train the model for one epoch.

    Args:
        model (torch.nn.Module): The model to train.
        optimizer (torch.optim.Optimizer): The optimizer to use.
        data_loader (torch.utils.data.DataLoader): DataLoader for training data.
        device (torch.device): Device to train on.
        epoch (int): Current epoch number.
        print_freq (int): How often to print progress.
        verbose (bool): Whether to print detailed progress.

    Returns:
        float: Average loss for the epoch.
    """
    model.train()
    total_loss = 0

    start_time = time.time()

    for i, (images, targets) in enumerate(data_loader):
        # Move images and targets to device
        images = list(image.to(device) for image in images)
        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

        # Forward pass
        loss_dict = model(images, targets)
        losses = sum(loss for loss in loss_dict.values())

        # Backward pass
        optimizer.zero_grad()
        losses.backward()
        optimizer.step()

        # Track loss
        total_loss += losses.item()

        # Print progress
        if i % print_freq == 0:
            elapsed_time = time.time() - start_time
            if verbose:
                print(
                    f"Epoch: {epoch}, Batch: {i}/{len(data_loader)}, Loss: {losses.item():.4f}, Time: {elapsed_time:.2f}s"
                )
            start_time = time.time()

    # Calculate average loss
    avg_loss = total_loss / len(data_loader)
    return avg_loss

train_segmentation_model(images_dir, labels_dir, output_dir, architecture='unet', encoder_name='resnet34', encoder_weights='imagenet', num_channels=3, num_classes=2, batch_size=8, num_epochs=50, learning_rate=0.001, weight_decay=0.0001, seed=42, val_split=0.2, print_freq=10, verbose=True, save_best_only=True, plot_curves=False, device=None, checkpoint_path=None, resume_training=False, target_size=None, resize_mode='resize', **kwargs)

Train a semantic segmentation model for object detection using segmentation-models-pytorch.

This function trains a semantic segmentation model for object detection (e.g., building detection) using models from the segmentation-models-pytorch library. Unlike instance segmentation (Mask R-CNN), this approach treats the task as pixel-level binary classification.

Parameters:

Name Type Description Default
images_dir str

Directory containing image GeoTIFF files.

required
labels_dir str

Directory containing label GeoTIFF files.

required
output_dir str

Directory to save model checkpoints and results.

required
architecture str

Model architecture ('unet', 'deeplabv3', 'deeplabv3plus', 'fpn', 'pspnet', 'linknet', 'manet'). Defaults to 'unet'.

'unet'
encoder_name str

Encoder backbone name (e.g., 'resnet34', 'resnet50', 'efficientnet-b0'). Defaults to 'resnet34'.

'resnet34'
encoder_weights str

Encoder pretrained weights ('imagenet' or None). Defaults to 'imagenet'.

'imagenet'
num_channels int

Number of input channels. Defaults to 3.

3
num_classes int

Number of output classes (typically 2 for binary segmentation). Defaults to 2.

2
batch_size int

Batch size for training. Defaults to 8.

8
num_epochs int

Number of training epochs. Defaults to 50.

50
learning_rate float

Initial learning rate. Defaults to 0.001.

0.001
weight_decay float

Weight decay for optimizer. Defaults to 1e-4.

0.0001
seed int

Random seed for reproducibility. Defaults to 42.

42
val_split float

Fraction of data to use for validation (0-1). Defaults to 0.2.

0.2
print_freq int

Frequency of printing training progress. Defaults to 10.

10
verbose bool

If True, prints detailed training progress. Defaults to True.

True
save_best_only bool

If True, only saves the best model. Otherwise saves all checkpoints. Defaults to True.

True
plot_curves bool

If True, plots training curves. Defaults to False.

False
device device

Device to train on. If None, uses CUDA if available.

None
checkpoint_path str

Path to a checkpoint file to load for resuming training. If provided, will load model weights and optionally optimizer/scheduler state.

None
resume_training bool

If True and checkpoint_path is provided, will resume training from the checkpoint including optimizer and scheduler state. Defaults to False.

False
target_size tuple

Target size (height, width) for standardizing images. If None, the function will automatically detect if images have varying sizes and set a default target_size of (512, 512) to prevent batching errors. To disable automatic resizing, set this parameter explicitly. Example: (512, 512). Defaults to None.

None
resize_mode str

How to handle size standardization when target_size is specified. 'resize' - Resize images to target_size (may change aspect ratio) 'pad' - Pad images to target_size (preserves aspect ratio). Defaults to 'resize'.

'resize'
**kwargs

Additional arguments passed to smp.create_model().

{}

Raises:

Type Description
ImportError

If segmentation-models-pytorch is not installed.

FileNotFoundError

If input directories don't exist or contain no matching files.

Source code in geoai/train.py
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
def train_segmentation_model(
    images_dir,
    labels_dir,
    output_dir,
    architecture="unet",
    encoder_name="resnet34",
    encoder_weights="imagenet",
    num_channels=3,
    num_classes=2,
    batch_size=8,
    num_epochs=50,
    learning_rate=0.001,
    weight_decay=1e-4,
    seed=42,
    val_split=0.2,
    print_freq=10,
    verbose=True,
    save_best_only=True,
    plot_curves=False,
    device=None,
    checkpoint_path=None,
    resume_training=False,
    target_size=None,
    resize_mode="resize",
    **kwargs,
):
    """
    Train a semantic segmentation model for object detection using segmentation-models-pytorch.

    This function trains a semantic segmentation model for object detection (e.g., building detection)
    using models from the segmentation-models-pytorch library. Unlike instance segmentation (Mask R-CNN),
    this approach treats the task as pixel-level binary classification.

    Args:
        images_dir (str): Directory containing image GeoTIFF files.
        labels_dir (str): Directory containing label GeoTIFF files.
        output_dir (str): Directory to save model checkpoints and results.
        architecture (str): Model architecture ('unet', 'deeplabv3', 'deeplabv3plus', 'fpn',
            'pspnet', 'linknet', 'manet'). Defaults to 'unet'.
        encoder_name (str): Encoder backbone name (e.g., 'resnet34', 'resnet50', 'efficientnet-b0').
            Defaults to 'resnet34'.
        encoder_weights (str): Encoder pretrained weights ('imagenet' or None). Defaults to 'imagenet'.
        num_channels (int): Number of input channels. Defaults to 3.
        num_classes (int): Number of output classes (typically 2 for binary segmentation). Defaults to 2.
        batch_size (int): Batch size for training. Defaults to 8.
        num_epochs (int): Number of training epochs. Defaults to 50.
        learning_rate (float): Initial learning rate. Defaults to 0.001.
        weight_decay (float): Weight decay for optimizer. Defaults to 1e-4.
        seed (int): Random seed for reproducibility. Defaults to 42.
        val_split (float): Fraction of data to use for validation (0-1). Defaults to 0.2.
        print_freq (int): Frequency of printing training progress. Defaults to 10.
        verbose (bool): If True, prints detailed training progress. Defaults to True.
        save_best_only (bool): If True, only saves the best model. Otherwise saves all checkpoints.
            Defaults to True.
        plot_curves (bool): If True, plots training curves. Defaults to False.
        device (torch.device): Device to train on. If None, uses CUDA if available.
        checkpoint_path (str, optional): Path to a checkpoint file to load for resuming training.
            If provided, will load model weights and optionally optimizer/scheduler state.
        resume_training (bool): If True and checkpoint_path is provided, will resume training
            from the checkpoint including optimizer and scheduler state. Defaults to False.
        target_size (tuple, optional): Target size (height, width) for standardizing images.
            If None, the function will automatically detect if images have varying sizes and set
            a default target_size of (512, 512) to prevent batching errors. To disable automatic
            resizing, set this parameter explicitly. Example: (512, 512). Defaults to None.
        resize_mode (str): How to handle size standardization when target_size is specified.
            'resize' - Resize images to target_size (may change aspect ratio)
            'pad' - Pad images to target_size (preserves aspect ratio). Defaults to 'resize'.
        **kwargs: Additional arguments passed to smp.create_model().
    Returns:
        None: Model weights are saved to output_dir.

    Raises:
        ImportError: If segmentation-models-pytorch is not installed.
        FileNotFoundError: If input directories don't exist or contain no matching files.
    """
    import datetime

    if not SMP_AVAILABLE:
        raise ImportError(
            "segmentation-models-pytorch is not installed. "
            "Please install it with: pip install segmentation-models-pytorch"
        )

    # Set random seeds for reproducibility
    torch.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    # Create output directory
    os.makedirs(os.path.abspath(output_dir), exist_ok=True)

    # Get device
    if device is None:
        device = get_device()
    print(f"Using device: {device}")

    # Get all image and label files
    # Support multiple image formats: GeoTIFF, PNG, JPG, JPEG, TIF, TIFF
    image_extensions = (".tif", ".tiff", ".png", ".jpg", ".jpeg")
    label_extensions = (".tif", ".tiff", ".png", ".jpg", ".jpeg")

    image_files = sorted(
        [
            os.path.join(images_dir, f)
            for f in os.listdir(images_dir)
            if f.lower().endswith(image_extensions)
        ]
    )
    label_files = sorted(
        [
            os.path.join(labels_dir, f)
            for f in os.listdir(labels_dir)
            if f.lower().endswith(label_extensions)
        ]
    )

    print(f"Found {len(image_files)} image files and {len(label_files)} label files")

    # Ensure matching files
    if len(image_files) != len(label_files):
        print("Warning: Number of image files and label files don't match!")
        # Find matching files by basename
        basenames = [os.path.basename(f) for f in image_files]
        label_files = [
            os.path.join(labels_dir, os.path.basename(f))
            for f in image_files
            if os.path.exists(os.path.join(labels_dir, os.path.basename(f)))
        ]
        image_files = [
            f
            for f, b in zip(image_files, basenames)
            if os.path.exists(os.path.join(labels_dir, b))
        ]
        print(f"Using {len(image_files)} matching files")

    if len(image_files) == 0:
        raise FileNotFoundError("No matching image and label files found")

    # Split data into train and validation sets
    train_imgs, val_imgs, train_labels, val_labels = train_test_split(
        image_files, label_files, test_size=val_split, random_state=seed
    )

    print(f"Training on {len(train_imgs)} images, validating on {len(val_imgs)} images")

    # Auto-detect image sizes and set target_size if needed
    if target_size is None:
        print("Checking image sizes for compatibility...")

        # Sample a few images to check size consistency
        sample_images = train_imgs[: min(5, len(train_imgs))]
        image_sizes = []

        for img_path in sample_images:
            try:
                if img_path.lower().endswith((".tif", ".tiff")):
                    with rasterio.open(img_path) as src:
                        height, width = src.height, src.width
                else:
                    with Image.open(img_path) as img:
                        width, height = img.size
                image_sizes.append((height, width))
            except Exception as e:
                print(f"Warning: Could not read image {img_path}: {e}")
                continue

        # Check if all images have the same size
        if len(image_sizes) == 0:
            print(
                "Warning: Could not read any sample images. Setting target_size to (512, 512) as a safe default."
            )
            target_size = (512, 512)
        else:
            unique_sizes = set(image_sizes)
            if len(unique_sizes) > 1:
                print(
                    f"Warning: Found images with different sizes: {list(unique_sizes)}"
                )
                print(
                    "Setting target_size to (512, 512) to standardize image dimensions."
                )
                print("This will resize all images to 512x512 pixels.")
                print("To use a different size, set target_size parameter explicitly.")
                target_size = (512, 512)
            else:
                print(f"All sampled images have the same size: {image_sizes[0]}")
                print("No resizing needed.")

    # Create datasets
    train_dataset = SemanticSegmentationDataset(
        train_imgs,
        train_labels,
        transforms=get_semantic_transform(train=True),
        num_channels=num_channels,
        target_size=target_size,
        resize_mode=resize_mode,
        num_classes=num_classes,
    )
    val_dataset = SemanticSegmentationDataset(
        val_imgs,
        val_labels,
        transforms=get_semantic_transform(train=False),
        num_channels=num_channels,
        target_size=target_size,
        resize_mode=resize_mode,
        num_classes=num_classes,
    )

    # Create data loaders
    # Use num_workers=0 on macOS and Windows to avoid multiprocessing issues
    # Windows often has issues with multiprocessing in Jupyter notebooks
    num_workers = 0 if platform.system() in ["Darwin", "Windows"] else 4

    try:
        train_loader = DataLoader(
            train_dataset,
            batch_size=batch_size,
            shuffle=True,
            num_workers=num_workers,
            pin_memory=True,
        )

        val_loader = DataLoader(
            val_dataset,
            batch_size=batch_size,
            shuffle=False,
            num_workers=num_workers,
            pin_memory=True,
        )

        # Test the data loader by loading one batch to catch size mismatch errors early
        print("Testing data loader...")
        try:
            next(iter(train_loader))
            print("Data loader test passed.")
        except RuntimeError as e:
            if "stack expects each tensor to be equal size" in str(e):
                raise RuntimeError(
                    "Images have different sizes and cannot be batched together. "
                    "Please set target_size parameter to standardize image dimensions. "
                    "Example: target_size=(512, 512). "
                    f"Original error: {str(e)}"
                ) from e
            else:
                raise

    except Exception as e:
        if "stack expects each tensor to be equal size" in str(e):
            raise RuntimeError(
                "Images have different sizes and cannot be batched together. "
                "Please set target_size parameter to standardize image dimensions. "
                "Example: target_size=(512, 512). "
                f"Original error: {str(e)}"
            ) from e
        else:
            raise

    # Initialize model
    model = get_smp_model(
        architecture=architecture,
        encoder_name=encoder_name,
        encoder_weights=encoder_weights,
        in_channels=num_channels,
        classes=num_classes,
        activation=None,  # We'll apply softmax later
        **kwargs,
    )
    model.to(device)

    # Set up loss function (CrossEntropyLoss for multi-class, can also use DiceLoss)
    criterion = torch.nn.CrossEntropyLoss()

    # Set up optimizer
    optimizer = torch.optim.Adam(
        model.parameters(), lr=learning_rate, weight_decay=weight_decay
    )

    # Set up learning rate scheduler
    lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
        optimizer, mode="min", factor=0.5, patience=5
    )

    # Initialize tracking variables
    best_iou = 0
    train_losses = []
    val_losses = []
    val_ious = []
    val_dices = []
    start_epoch = 0

    # Load checkpoint if provided
    if checkpoint_path is not None:
        if not os.path.exists(checkpoint_path):
            raise FileNotFoundError(f"Checkpoint file not found: {checkpoint_path}")

        print(f"Loading checkpoint from: {checkpoint_path}")
        try:
            checkpoint = torch.load(checkpoint_path, map_location=device)

            if isinstance(checkpoint, dict) and "model_state_dict" in checkpoint:
                # Load model state
                model.load_state_dict(checkpoint["model_state_dict"])

                if resume_training:
                    # Resume training from checkpoint
                    start_epoch = checkpoint.get("epoch", 0) + 1
                    best_iou = checkpoint.get("best_iou", 0)

                    # Load optimizer state if available
                    if "optimizer_state_dict" in checkpoint:
                        optimizer.load_state_dict(checkpoint["optimizer_state_dict"])

                    # Load scheduler state if available
                    if "scheduler_state_dict" in checkpoint:
                        lr_scheduler.load_state_dict(checkpoint["scheduler_state_dict"])

                    # Load training history if available
                    if "train_losses" in checkpoint:
                        train_losses = checkpoint["train_losses"]
                    if "val_losses" in checkpoint:
                        val_losses = checkpoint["val_losses"]
                    if "val_ious" in checkpoint:
                        val_ious = checkpoint["val_ious"]
                    if "val_dices" in checkpoint:
                        val_dices = checkpoint["val_dices"]

                    print(f"Resuming training from epoch {start_epoch}")
                    print(f"Previous best IoU: {best_iou:.4f}")
                else:
                    print("Loaded model weights only (not resuming training state)")
            else:
                # Assume it's just model weights
                model.load_state_dict(checkpoint)
                print("Loaded model weights only")

        except Exception as e:
            raise RuntimeError(f"Failed to load checkpoint: {str(e)}")

    print(f"Starting training with {architecture} + {encoder_name}")
    print(f"Model parameters: {sum(p.numel() for p in model.parameters()):,}")
    if start_epoch > 0:
        print(f"Resuming from epoch {start_epoch}/{num_epochs}")

    # Training loop
    for epoch in range(start_epoch, num_epochs):
        # Train one epoch
        train_loss = train_semantic_one_epoch(
            model,
            optimizer,
            train_loader,
            device,
            epoch,
            criterion,
            print_freq,
            verbose,
        )
        train_losses.append(train_loss)

        # Evaluate on validation set
        eval_metrics = evaluate_semantic(
            model, val_loader, device, criterion, num_classes=num_classes
        )
        val_losses.append(eval_metrics["loss"])
        val_ious.append(eval_metrics["IoU"])
        val_dices.append(eval_metrics["Dice"])

        # Update learning rate
        lr_scheduler.step(eval_metrics["loss"])

        # Print metrics
        print(
            f"Epoch {epoch+1}/{num_epochs}: "
            f"Train Loss: {train_loss:.4f}, "
            f"Val Loss: {eval_metrics['loss']:.4f}, "
            f"Val IoU: {eval_metrics['IoU']:.4f}, "
            f"Val Dice: {eval_metrics['Dice']:.4f}"
        )

        # Save best model
        if eval_metrics["IoU"] > best_iou:
            best_iou = eval_metrics["IoU"]
            print(f"Saving best model with IoU: {best_iou:.4f}")
            torch.save(model.state_dict(), os.path.join(output_dir, "best_model.pth"))

        # Save checkpoint every 10 epochs (if not save_best_only)
        if not save_best_only and ((epoch + 1) % 10 == 0 or epoch == num_epochs - 1):
            torch.save(
                {
                    "epoch": epoch,
                    "model_state_dict": model.state_dict(),
                    "optimizer_state_dict": optimizer.state_dict(),
                    "scheduler_state_dict": lr_scheduler.state_dict(),
                    "best_iou": best_iou,
                    "architecture": architecture,
                    "encoder_name": encoder_name,
                    "num_channels": num_channels,
                    "num_classes": num_classes,
                    "train_losses": train_losses,
                    "val_losses": val_losses,
                    "val_ious": val_ious,
                    "val_dices": val_dices,
                },
                os.path.join(output_dir, f"checkpoint_epoch_{epoch+1}.pth"),
            )

    # Save final model
    torch.save(model.state_dict(), os.path.join(output_dir, "final_model.pth"))

    # Save training history
    history = {
        "train_losses": train_losses,
        "val_losses": val_losses,
        "val_ious": val_ious,
        "val_dices": val_dices,
    }
    torch.save(history, os.path.join(output_dir, "training_history.pth"))

    # Save training summary
    with open(
        os.path.join(output_dir, "training_summary.txt"), "w", encoding="utf-8"
    ) as f:
        f.write(
            f"Training completed on: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
        )
        f.write(f"Architecture: {architecture}\n")
        f.write(f"Encoder: {encoder_name}\n")
        f.write(f"Total epochs: {num_epochs}\n")
        f.write(f"Best validation IoU: {best_iou:.4f}\n")
        f.write(f"Final validation IoU: {val_ious[-1]:.4f}\n")
        f.write(f"Final validation Dice: {val_dices[-1]:.4f}\n")
        f.write(f"Final validation loss: {val_losses[-1]:.4f}\n")

    print(f"Training complete! Best IoU: {best_iou:.4f}")
    print(f"Models saved to {output_dir}")

    # Plot training curves
    if plot_curves:
        try:
            plt.figure(figsize=(15, 5))

            plt.subplot(1, 3, 1)
            plt.plot(train_losses, label="Train Loss")
            plt.plot(val_losses, label="Val Loss")
            plt.title("Loss")
            plt.xlabel("Epoch")
            plt.ylabel("Loss")
            plt.legend()
            plt.grid(True)

            plt.subplot(1, 3, 2)
            plt.plot(val_ious, label="Val IoU")
            plt.title("IoU Score")
            plt.xlabel("Epoch")
            plt.ylabel("IoU")
            plt.legend()
            plt.grid(True)

            plt.subplot(1, 3, 3)
            plt.plot(val_dices, label="Val Dice")
            plt.title("Dice Score")
            plt.xlabel("Epoch")
            plt.ylabel("Dice")
            plt.legend()
            plt.grid(True)

            plt.tight_layout()
            plt.savefig(
                os.path.join(output_dir, "training_curves.png"),
                dpi=150,
                bbox_inches="tight",
            )
            print(
                f"Training curves saved to {os.path.join(output_dir, 'training_curves.png')}"
            )
            plt.close()
        except Exception as e:
            print(f"Could not save training curves: {e}")

train_semantic_one_epoch(model, optimizer, data_loader, device, epoch, criterion, print_freq=10, verbose=True)

Train the semantic segmentation model for one epoch.

Parameters:

Name Type Description Default
model Module

The model to train.

required
optimizer Optimizer

The optimizer to use.

required
data_loader DataLoader

DataLoader for training data.

required
device device

Device to train on.

required
epoch int

Current epoch number.

required
criterion

Loss function.

required
print_freq int

How often to print progress.

10
verbose bool

Whether to print detailed progress.

True

Returns:

Name Type Description
float

Average loss for the epoch.

Source code in geoai/train.py
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
def train_semantic_one_epoch(
    model, optimizer, data_loader, device, epoch, criterion, print_freq=10, verbose=True
):
    """
    Train the semantic segmentation model for one epoch.

    Args:
        model (torch.nn.Module): The model to train.
        optimizer (torch.optim.Optimizer): The optimizer to use.
        data_loader (torch.utils.data.DataLoader): DataLoader for training data.
        device (torch.device): Device to train on.
        epoch (int): Current epoch number.
        criterion: Loss function.
        print_freq (int): How often to print progress.
        verbose (bool): Whether to print detailed progress.

    Returns:
        float: Average loss for the epoch.
    """
    model.train()
    total_loss = 0
    num_batches = len(data_loader)

    start_time = time.time()

    for i, (images, targets) in enumerate(data_loader):
        # Move images and targets to device
        images = images.to(device)
        targets = targets.to(device)

        # Forward pass
        outputs = model(images)
        loss = criterion(outputs, targets)

        # Backward pass
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # Track loss
        total_loss += loss.item()

        # Print progress
        if i % print_freq == 0:
            elapsed_time = time.time() - start_time
            if verbose:
                print(
                    f"Epoch: {epoch}, Batch: {i}/{num_batches}, Loss: {loss.item():.4f}, Time: {elapsed_time:.2f}s"
                )
            start_time = time.time()

    # Calculate average loss
    avg_loss = total_loss / num_batches
    return avg_loss

visualize_predictions(model, dataset, device, num_samples=5, output_dir=None)

Visualize model predictions.

Parameters:

Name Type Description Default
model Module

Trained model.

required
dataset Dataset

Dataset to visualize.

required
device device

Device to run inference on.

required
num_samples int

Number of samples to visualize.

5
output_dir str

Directory to save visualizations. If None, visualizations are displayed but not saved.

None
Source code in geoai/train.py
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
def visualize_predictions(model, dataset, device, num_samples=5, output_dir=None):
    """
    Visualize model predictions.

    Args:
        model (torch.nn.Module): Trained model.
        dataset (torch.utils.data.Dataset): Dataset to visualize.
        device (torch.device): Device to run inference on.
        num_samples (int): Number of samples to visualize.
        output_dir (str, optional): Directory to save visualizations. If None,
            visualizations are displayed but not saved.
    """
    model.eval()

    # Create output directory if needed
    if output_dir:
        os.makedirs(os.path.abspath(output_dir), exist_ok=True)

    # Select random samples
    indices = random.sample(range(len(dataset)), min(num_samples, len(dataset)))

    for idx in indices:
        # Get image and target
        image, target = dataset[idx]

        # Convert to device and add batch dimension
        image = image.to(device)
        image_batch = [image]

        # Get prediction
        with torch.no_grad():
            output = model(image_batch)[0]

        # Convert image from CHW to HWC for display (first 3 bands as RGB)
        rgb_image = image[:3].cpu().numpy()
        rgb_image = np.transpose(rgb_image, (1, 2, 0))
        rgb_image = np.clip(rgb_image, 0, 1)  # Ensure values are in [0,1]

        # Create binary ground truth mask (combine all instances)
        gt_masks = target["masks"].cpu().numpy()
        gt_combined = (
            np.max(gt_masks, axis=0)
            if len(gt_masks) > 0
            else np.zeros((image.shape[1], image.shape[2]), dtype=np.uint8)
        )

        # Create binary prediction mask (combine all instances with score > 0.5)
        pred_masks = output["masks"].cpu().numpy()
        pred_scores = output["scores"].cpu().numpy()
        high_conf_indices = pred_scores > 0.5

        pred_combined = np.zeros((image.shape[1], image.shape[2]), dtype=np.float32)
        if np.any(high_conf_indices):
            for mask in pred_masks[high_conf_indices]:
                # Apply threshold to each predicted mask
                binary_mask = (mask[0] > 0.5).astype(np.float32)
                # Combine with existing masks
                pred_combined = np.maximum(pred_combined, binary_mask)

        # Create figure
        fig, axs = plt.subplots(1, 3, figsize=(15, 5))

        # Show RGB image
        axs[0].imshow(rgb_image)
        axs[0].set_title("RGB Image")
        axs[0].axis("off")

        # Show prediction
        axs[1].imshow(pred_combined, cmap="viridis")
        axs[1].set_title(f"Predicted Buildings: {np.sum(high_conf_indices)} instances")
        axs[1].axis("off")

        # Show ground truth
        axs[2].imshow(gt_combined, cmap="viridis")
        axs[2].set_title(f"Ground Truth: {len(gt_masks)} instances")
        axs[2].axis("off")

        plt.tight_layout()

        # Save or show
        if output_dir:
            plt.savefig(os.path.join(output_dir, f"prediction_{idx}.png"))
            plt.close()
        else:
            plt.show()