UCI Merced dataset: Example of the aitlas toolbox in for multi class image classification#

This notebook shows a sample implementation of a multi class image classification using the aitlas toolbox using the UC merced dataset.

[1]:
from aitlas.datasets import UcMercedDataset
from aitlas.models import ResNet50
from aitlas.transforms import ResizeCenterCropFlipHVToTensor, ResizeCenterCropToTensor
from aitlas.utils import image_loader

Load the dataset#

[2]:
dataset_config = {
    "data_dir": "/media/hdd/multi-class/UCMerced",
    "csv_file": "/media/hdd/multi-class/UCMerced/trainval.csv"
}
dataset = UcMercedDataset(dataset_config)

Show images from the dataset#

[3]:
fig1 = dataset.show_image(1000)
fig2 = dataset.show_image(80)
fig3 = dataset.show_batch(15)
../_images/examples_multiclass_classification_example_uc_merced_5_0.png
../_images/examples_multiclass_classification_example_uc_merced_5_1.png
../_images/examples_multiclass_classification_example_uc_merced_5_2.png

Inspect the data#

[4]:
dataset.show_samples()
[4]:
File name Label
0 river/river10.tif river
1 runway/runway09.tif runway
2 tenniscourt/tenniscourt64.tif tenniscourt
3 beach/beach17.tif beach
4 runway/runway21.tif runway
5 runway/runway11.tif runway
6 harbor/harbor89.tif harbor
7 freeway/freeway28.tif freeway
8 parkinglot/parkinglot68.tif parkinglot
9 freeway/freeway63.tif freeway
10 runway/runway22.tif runway
11 tenniscourt/tenniscourt81.tif tenniscourt
12 parkinglot/parkinglot09.tif parkinglot
13 chaparral/chaparral05.tif chaparral
14 tenniscourt/tenniscourt15.tif tenniscourt
15 forest/forest07.tif forest
16 agricultural/agricultural29.tif agricultural
17 agricultural/agricultural57.tif agricultural
18 chaparral/chaparral28.tif chaparral
19 agricultural/agricultural07.tif agricultural
[5]:
dataset.data_distribution_table()
[5]:
Label Count
0 agricultural 100
1 airplane 100
2 baseballdiamond 100
3 beach 100
4 buildings 100
5 chaparral 100
6 denseresidential 100
7 forest 100
8 freeway 100
9 golfcourse 100
10 harbor 100
11 intersection 100
12 mediumresidential 100
13 mobilehomepark 100
14 overpass 100
15 parkinglot 100
16 river 100
17 runway 100
18 sparseresidential 100
19 storagetanks 100
20 tenniscourt 100
[6]:
fig = dataset.data_distribution_barchart()
../_images/examples_multiclass_classification_example_uc_merced_9_0.png

Load train and test splits#

[7]:
train_dataset_config = {
    "batch_size": 16,
    "shuffle": True,
    "num_workers": 4,
    "data_dir": "/media/hdd/multi-class/UCMerced",
    "csv_file": "/media/hdd/multi-class/UCMerced/train.csv"
}

train_dataset = UcMercedDataset(train_dataset_config)
train_dataset.transform = ResizeCenterCropFlipHVToTensor()

test_dataset_config = {
    "batch_size": 4,
    "shuffle": False,
    "num_workers": 4,
    "data_dir": "/media/hdd/multi-class/UCMerced",
    "csv_file": "/media/hdd/multi-class/UCMerced/test.csv",
    "transforms": ["aitlas.transforms.ResizeCenterCropToTensor"]
}

test_dataset = UcMercedDataset(test_dataset_config)
len(train_dataset), len(test_dataset)
[7]:
(1680, 420)

Setup and create the model for training#

[8]:
epochs = 10
model_directory = "/media/ssd/aitlas_new/examples/experiment/ucmerced"
model_config = {
    "num_classes": 21,
    "learning_rate": 0.0001,
    "pretrained": True,
    "metrics": ["accuracy", "precision", "recall", "f1_score"]
}
model = ResNet50(model_config)
model.prepare()

Training and evaluation#

[ ]:
model.train_and_evaluate_model(
    train_dataset=train_dataset,
    epochs=epochs,
    model_directory=model_directory,
    val_dataset=test_dataset,
    run_id='1',
)

Predictions#

[10]:
model_path = "/media/ssd/aitlas_new/examples/experiment/ucmerced/checkpoint.pth.tar"
#labels = UcMercedDataset.labels
labels = ["agricultural", "airplane", "baseballdiamond", "beach", "buildings", "chaparral",
                        "denseresidential", "forest", "freeway", "golfcourse", "harbor", "intersection",
                        "mediumresidential", "mobilehomepark", "overpass", "parkinglot", "river", "runway",
                        "sparseresidential", "storagetanks", "tenniscourt"]
transform = ResizeCenterCropToTensor()
model.load_model(model_path)

image = image_loader('/media/ssd/uc_merced_multilabel/predict/buildings98.tif')
fig = model.predict_image(image, labels, transform)

image = image_loader('/media/ssd/uc_merced_multilabel/predict/golfcourse04.tif')
fig = model.predict_image(image, labels, transform)

image = image_loader('/media/ssd/uc_merced_multilabel/predict/airplane18.tif')
fig = model.predict_image(image, labels, transform)

image = image_loader('/media/ssd/uc_merced_multilabel/predict/parkinglot13.tif')
fig = model.predict_image(image, labels, transform)
2021-11-25 01:23:11,459 INFO Loading checkpoint /media/ssd/aitlas_new/examples/experiment/ucmerced/checkpoint.pth.tar
2021-11-25 01:23:11,601 INFO Loaded checkpoint /media/ssd/aitlas_new/examples/experiment/ucmerced/checkpoint.pth.tar at epoch 11
../_images/examples_multiclass_classification_example_uc_merced_17_1.png
../_images/examples_multiclass_classification_example_uc_merced_17_2.png
../_images/examples_multiclass_classification_example_uc_merced_17_3.png
../_images/examples_multiclass_classification_example_uc_merced_17_4.png