Amazon Forest dataset: Example of the aitlas toolbox for semantic segmentation#

This notebook shows a sample implementation of a image segmentation using the aitlas toolbox.

Import the required packages#

[ ]:
from aitlas.datasets import AmazonRainforestDataset
from aitlas.models import DeepLabV3
from aitlas.transforms import MinMaxNormTranspose
from aitlas.utils import image_loader

Visualize images and masks#

[2]:
dataset_config = {
    "data_dir": "../data/AmazonForest/Training"
}
dataset = AmazonRainforestDataset(dataset_config)

print(f"Total number of patches: {len(dataset)}")
dataset.show_image(10);
dataset.show_image(26);
Total number of patches: 30
../_images/examples_semantic_segmentation_example_amazon_rainforest_4_1.png
../_images/examples_semantic_segmentation_example_amazon_rainforest_4_2.png
[3]:
dataset.data_distribution_table()
[3]:
Number of pixels
Background 3654658.0
Forest 4203929.0
[4]:
dataset.data_distribution_barchart();
../_images/examples_semantic_segmentation_example_amazon_rainforest_6_0.png

Load train data#

[5]:
train_dataset_config = {
    "batch_size": 16,
    "shuffle": True,
    "num_workers": 4,
    "data_dir": "../data/AmazonForest/Training",
    "joint_transforms": ["aitlas.transforms.FlipHVRandomRotate"],
    "transforms": ["aitlas.transforms.MinMaxNormTranspose"],
    "target_transforms": ["aitlas.transforms.Transpose"]
}
train_dataset = AmazonRainforestDataset(train_dataset_config)

len(train_dataset)
[5]:
30

Create the model#

[6]:
epochs = 50
model_directory = "./experiments/amazon_rainforest"
model_config = {
    "num_classes": 2,
    "learning_rate": 0.0001,
    "pretrained": True,
    "threshold": 0.5,
    "metrics": ["iou"]
}

model = DeepLabV3(model_config)
model.prepare()

Start the training#

[ ]:
model.train_model(
    train_dataset=train_dataset,
    epochs=epochs,
    model_directory=model_directory,
    run_id='1'
);

Evalute the model using test data#

[ ]:
test_dataset_config = {
    "batch_size": 4,
    "shuffle": False,
    "num_workers": 4,
    "data_dir": "../data/AmazonForest/Validation",
    "transforms": ["aitlas.transforms.MinMaxNormTranspose"],
    "target_transforms": ["aitlas.transforms.Transpose"]
}

test_dataset = AmazonRainforestDataset(test_dataset_config)
len(test_dataset)

model = DeepLabV3(model_config)
model.prepare()
model.running_metrics.reset()
model_path = "./experiments/amazon_rainforest/checkpoint.pth.tar"
model.evaluate(dataset=test_dataset, model_path=model_path)
model.running_metrics.get_scores(model.metrics)

Predictions#

[10]:
model_path = "./experiments/amazon_rainforest/checkpoint.pth.tar"
#labels = AmazonRainforestDataset.labels
labels = ["Background", "Forest"]
transform = MinMaxNormTranspose()
model.load_model(model_path)

image = image_loader('../data/AmazonForest/Test/7.tiff')
fig = model.predict_masks(image, labels, transform)

image = image_loader('../data/AmazonForest/Test/6.tiff')
fig = model.predict_masks(image, labels, transform)

image = image_loader('../data/AmazonForest/Test/0.tiff')
fig = model.predict_masks(image, labels, transform)

image = image_loader('../data/AmazonForest/Test/4.tiff')
fig = model.predict_masks(image, labels, transform)
2022-10-30 13:43:19,178 INFO Loading checkpoint ./experiments/amazon_rainforest/checkpoint.pth.tar
2022-10-30 13:43:19,564 INFO Loaded checkpoint ./experiments/amazon_rainforest/checkpoint.pth.tar at epoch 51
../_images/examples_semantic_segmentation_example_amazon_rainforest_16_1.png
../_images/examples_semantic_segmentation_example_amazon_rainforest_16_2.png
../_images/examples_semantic_segmentation_example_amazon_rainforest_16_3.png
../_images/examples_semantic_segmentation_example_amazon_rainforest_16_4.png