meds.py 1.9 KB
Newer Older
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from bob.bio.demographics.datasets import MedsTorchDataset

# https://pytorch.org/docs/stable/data.html
from torch.utils.data import DataLoader
from bob.extension import rc
import os

import bob.io.image

import torch
from functools import partial

import torchvision.transforms as transforms
import click
import yaml


from bob.bio.demographics.regularizers.trainers import balance_trainer


@click.command()
@click.argument("OUTPUT_DIR")
@click.option("--max-epochs", default=600, help="Max number of epochs")
@click.option("--batch-size", default=64, help="Batch size")
@click.option("--backbone", default="iresnet100", help="Backbone")
def balance_meds(
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
27
28
29
30
    output_dir,
    max_epochs,
    batch_size,
    backbone,
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
):

    from bob.bio.demographics.regularizers import AVAILABLE_BACKBONES

    database_path = os.path.join(
        rc.get("bob.bio.demographics.directory"), "meds", "samplewrapper"
    )

    transform = transforms.Compose(
        [
            lambda x: bob.io.image.to_matplotlib(x.astype("float32")),
            # transforms.ToPILImage(mode="RGB"),
            # transforms.RandomHorizontalFlip(p=0.5),
            # transforms.RandomRotation(degrees=(-3, 3)),
            # transforms.RandomAutocontrast(p=0.1),
            transforms.ToTensor(),
            lambda x: (x - 127.5) / 128.0,
        ]
    )

    dataset = MedsTorchDataset(
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
52
53
54
        protocol="verification_fold1",
        database_path=database_path,
        transform=transform,
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
55
56
57
58
59
60
61
62
63
64
    )

    train_dataloader = DataLoader(
        dataset, batch_size=batch_size, shuffle=True, pin_memory=True, num_workers=2
    )
    # train_dataloader = DataLoader(dataset, batch_size=64, shuffle=True)

    backbone_model = AVAILABLE_BACKBONES[backbone]["prior"]()

    balance_trainer(
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
65
66
67
68
69
70
        output_dir,
        max_epochs,
        batch_size,
        train_dataloader,
        backbone_model,
        transform,
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
71
72
73
74
75
    )


if __name__ == "__main__":
    balance_meds()