aboutsummaryrefslogtreecommitdiff
path: root/supervised/baseline.py
blob: c8bbb3760b6d0b2f00bcdd17ef54701ab215d550 (plain)
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
27
28
29
30
31
32
33
34
35
36
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
127
128
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
import os
import random

import torch
from torch.nn import CrossEntropyLoss
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torchvision.datasets import CIFAR10, ImageNet
from torchvision.transforms import transforms, InterpolationMode
from tqdm import tqdm

from datautils import color_distortion, Clip, RandomGaussianBlur
from models import CIFARResNet50, ImageNetResNet50
from optimizers import LARS
from schedulers import LinearWarmupAndCosineAnneal, LinearLR

CODENAME = 'cifar10-resnet50-256-aug-lars-warmup'
DATASET_ROOT = 'dataset'
TENSORBOARD_PATH = os.path.join('runs', CODENAME)
CHECKPOINT_PATH = os.path.join('checkpoints', CODENAME)

DATASET = 'cifar10'
CROP_SIZE = 32
CROP_SCALE = (0.8, 1)
HFLIP_P = 0.5
DISTORT_S = 0.5
GAUSSIAN_KER_SCALE = 10
GAUSSIAN_P = 0.5
GAUSSIAN_SIGMA = (0.1, 2)

BATCH_SIZE = 256
RESTORE_EPOCH = 0
N_EPOCHS = 1000
WARMUP_EPOCHS = 10
N_WORKERS = 2
SEED = 0

OPTIM = 'lars'
SCHED = 'warmup-anneal'
LR = 1
MOMENTUM = 0.9
WEIGHT_DECAY = 1e-6

random.seed(SEED)
torch.manual_seed(SEED)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

if DATASET == 'cifar10' or DATASET == 'cifar':
    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(
            CROP_SIZE,
            scale=CROP_SCALE,
            interpolation=InterpolationMode.BICUBIC
        ),
        transforms.RandomHorizontalFlip(HFLIP_P),
        color_distortion(DISTORT_S),
        transforms.ToTensor(),
        Clip()
    ])
    test_transform = transforms.Compose([
        transforms.ToTensor()
    ])

    train_set = CIFAR10(DATASET_ROOT, train=True, transform=train_transform,
                        download=True)
    test_set = CIFAR10(DATASET_ROOT, train=False, transform=test_transform)

    resnet = CIFARResNet50()
elif DATASET == 'imagenet1k' or DATASET == 'imagenet1k':
    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(
            CROP_SIZE,
            scale=CROP_SCALE,
            interpolation=InterpolationMode.BICUBIC
        ),
        transforms.RandomHorizontalFlip(HFLIP_P),
        color_distortion(DISTORT_S),
        transforms.ToTensor(),
        RandomGaussianBlur(
            kernel_size=CROP_SIZE // GAUSSIAN_KER_SCALE,
            sigma_range=GAUSSIAN_SIGMA,
            p=GAUSSIAN_P
        ),
        Clip()
    ])
    test_transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(CROP_SIZE),
        transforms.ToTensor(),
    ])

    train_set = ImageNet(DATASET_ROOT, 'train', transform=train_transform)
    test_set = ImageNet(DATASET_ROOT, 'val', transform=test_transform)

    resnet = ImageNetResNet50()
else:
    raise NotImplementedError(f"Dataset '{DATASET}' is not implemented.")

resnet = resnet.to(device)

train_loader = DataLoader(train_set, batch_size=BATCH_SIZE,
                          shuffle=True, num_workers=N_WORKERS)
test_loader = DataLoader(test_set, batch_size=BATCH_SIZE,
                         shuffle=False, num_workers=N_WORKERS)

num_train_batches = len(train_loader)
num_test_batches = len(test_loader)


def exclude_from_wd_and_adaptation(name):
    if 'bn' in name:
        return True
    if OPTIM == 'lars' and 'bias' in name:
        return True


param_groups = [
    {
        'params': [p for name, p in resnet.named_parameters()
                   if not exclude_from_wd_and_adaptation(name)],
        'weight_decay': WEIGHT_DECAY,
        'layer_adaptation': True,
    },
    {
        'params': [p for name, p in resnet.named_parameters()
                   if exclude_from_wd_and_adaptation(name)],
        'weight_decay': 0.,
        'layer_adaptation': False,
    },
]
if OPTIM == 'adam':
    optimizer = torch.optim.Adam(param_groups, lr=LR, betas=(MOMENTUM, 0.999))
elif OPTIM == 'sdg' or OPTIM == 'lars':
    optimizer = torch.optim.SGD(param_groups, lr=LR, momentum=MOMENTUM)
else:
    raise NotImplementedError(f"Optimizer '{OPTIM}' is not implemented.")

# Restore checkpoint
if RESTORE_EPOCH > 0:
    checkpoint_path = os.path.join(CHECKPOINT_PATH, f'{RESTORE_EPOCH:04d}.pt')
    checkpoint = torch.load(checkpoint_path)
    resnet.load_state_dict(checkpoint['resnet_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    print(f'[RESTORED][{RESTORE_EPOCH}/{N_EPOCHS}]\t'
          f'Train loss: {checkpoint["train_loss"]:.4f}\t'
          f'Test loss: {checkpoint["test_loss"]:.4f}')

if SCHED == 'warmup-anneal':
    scheduler = LinearWarmupAndCosineAnneal(
        optimizer,
        warm_up=WARMUP_EPOCHS / N_EPOCHS,
        T_max=N_EPOCHS * num_train_batches,
        last_epoch=RESTORE_EPOCH * num_train_batches - 1
    )
elif SCHED == 'linear':
    scheduler = LinearLR(
        optimizer,
        num_epochs=N_EPOCHS * num_train_batches,
        last_epoch=RESTORE_EPOCH * num_train_batches - 1
    )
elif SCHED is None or SCHED == '' or SCHED == 'const':
    scheduler = None
else:
    raise NotImplementedError(f"Scheduler '{SCHED}' is not implemented.")

if OPTIM == 'lars':
    optimizer = LARS(optimizer)

criterion = CrossEntropyLoss()

if not os.path.exists(CHECKPOINT_PATH):
    os.makedirs(CHECKPOINT_PATH)
writer = SummaryWriter(TENSORBOARD_PATH)

curr_train_iters = RESTORE_EPOCH * num_train_batches
curr_test_iters = RESTORE_EPOCH * num_test_batches
for epoch in range(RESTORE_EPOCH, N_EPOCHS):
    train_loss = 0
    training_progress = tqdm(
        enumerate(train_loader), desc='Train loss: ', total=num_train_batches
    )

    resnet.train()
    for batch, (images, targets) in training_progress:
        images, targets = images.to(device), targets.to(device)

        resnet.zero_grad()
        output = resnet(images)
        loss = criterion(output, targets)
        loss.backward()
        optimizer.step()
        if SCHED:
            scheduler.step()

        train_loss += loss.item()
        train_loss_mean = train_loss / (batch + 1)
        training_progress.set_description(f'Train loss: {train_loss_mean:.4f}')
        writer.add_scalar('Loss/train', loss, curr_train_iters + 1)
        curr_train_iters += 1

    test_loss = 0
    test_acc = 0
    test_progress = tqdm(
        enumerate(test_loader), desc='Test loss: ', total=num_test_batches
    )

    resnet.eval()
    with torch.no_grad():
        for batch, (images, targets) in test_progress:
            images, targets = images.to(device), targets.to(device)

            output = resnet(images)
            loss = criterion(output, targets)
            _, prediction = output.max(-1)

            test_loss += loss
            test_loss_mean = test_loss / (batch + 1)
            test_progress.set_description(f'Test loss: {test_loss_mean:.4f}')
            test_acc += (prediction == targets).float().mean()
            test_acc_mean = test_acc / (batch + 1)
            writer.add_scalar('Loss/test', loss, curr_test_iters + 1)
            curr_test_iters += 1

    train_loss_mean = train_loss / num_train_batches
    test_loss_mean = test_loss / num_test_batches
    test_acc_mean = test_acc / num_test_batches
    print(f'[{epoch + 1}/{N_EPOCHS}]\t'
          f'Train loss: {train_loss_mean:.4f}\t'
          f'Test loss: {test_loss_mean:.4f}\t',
          f'Test acc: {test_acc_mean:.4f}')

    writer.add_scalar('Acc', test_acc_mean, epoch + 1)

    torch.save({'epoch': epoch,
                'resnet_state_dict': resnet.state_dict(),
                'optimizer_state_dict': optimizer.state_dict(),
                'train_loss': train_loss_mean, 'test_loss': test_loss_mean,
                }, os.path.join(CHECKPOINT_PATH, f'{epoch + 1:04d}.pt'))