-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
214 lines (185 loc) · 7.89 KB
/
main.py
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
import os
from argparse import ArgumentParser, Namespace
import torch
import torch.nn.functional as F
import torch.nn.parallel
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
import torch.utils.data
import torch.utils.data.distributed
import torchvision.datasets as datasets
import torchvision.models as models
import torchvision.transforms as transforms
import pytorch_lightning as pl
from pytorch_lightning.core import LightningModule
from datasets import ImageNet_C
class ImageNetLightningModel(LightningModule):
MODEL_NAMES = sorted(
name
for name in models.__dict__
if name.islower() and not name.startswith("__") and callable(models.__dict__[name])
)
def __init__(
self,
data_path: str,
arch: str = os.environ['ARCH'],
pretrained: bool = False,
lr: float = 0.1,
momentum: float = 0.9,
weight_decay: float = 1e-4,
batch_size: int = 4,
workers: int = 2,
**kwargs,
):
super().__init__()
self.save_hyperparameters()
self.arch = arch
self.pretrained = pretrained
self.lr = lr
self.momentum = momentum
self.weight_decay = weight_decay
self.data_path = data_path
self.batch_size = batch_size
self.workers = workers
print('*' * 80)
print(f'*************** Loading model {self.arch}')
print('*' * 80)
self.model = models.__dict__[self.arch](pretrained=self.pretrained)
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
images, target = batch
output = self(images)
loss_train = F.cross_entropy(output, target)
acc1, acc5 = self.__accuracy(output, target, topk=(1, 5))
self.log("train_loss", loss_train, on_step=True, on_epoch=True, logger=True)
self.log("train_acc1", acc1, on_step=True, prog_bar=True, on_epoch=True, logger=True)
self.log("train_acc5", acc5, on_step=True, on_epoch=True, logger=True)
return loss_train
def eval_step(self, batch, batch_idx, prefix: str):
images, target = batch
output = self(images)
loss_val = F.cross_entropy(output, target)
acc1, acc5 = self.__accuracy(output, target, topk=(1, 5))
self.log(f"{prefix}_loss", loss_val, on_step=True, on_epoch=True)
self.log(f"{prefix}_acc1", acc1, on_step=True, prog_bar=True, on_epoch=True)
self.log(f"{prefix}_acc5", acc5, on_step=True, on_epoch=True)
def validation_step(self, batch, batch_idx):
return self.eval_step(batch, batch_idx, "val")
@staticmethod
def __accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k."""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def configure_optimizers(self):
optimizer = optim.SGD(self.parameters(), lr=self.lr, momentum=self.momentum, weight_decay=self.weight_decay)
scheduler = lr_scheduler.LambdaLR(optimizer, lambda epoch: 0.1 ** (epoch // 30))
return [optimizer], [scheduler]
def train_dataloader(self):
# train_dir = os.path.join(self.data_path, "train")
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
trans = transforms.Compose(
[transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize]
)
# train_dataset = datasets.ImageFolder(train_dir, trans)
train_dataset = ImageNet_C(root=self.data_path, splits='train', transform=trans)
train_loader = torch.utils.data.DataLoader(
dataset=train_dataset, batch_size=self.batch_size, shuffle=True, num_workers=self.workers
)
return train_loader
def val_dataloader(self):
# val_dir = os.path.join(self.data_path, "val")
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
trans = transforms.Compose(
[transforms.Resize((256, 256)),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize]
)
# test_dataset = datasets.ImageFolder(val_dir, trans)
test_dataset = ImageNet_C(root=self.data_path, splits='val', transform=trans)
val_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=self.batch_size, shuffle=False, num_workers=self.workers,
)
return val_loader
def test_dataloader(self):
return self.val_dataloader()
def test_step(self, batch, batch_idx):
return self.eval_step(batch, batch_idx, "test")
@staticmethod
def add_model_specific_args(parent_parser): # pragma: no-cover
parser = parent_parser.add_argument_group("ImageNetLightningModel")
parser.add_argument(
"-a",
"--arch",
metavar="ARCH",
default="resnet18",
choices=ImageNetLightningModel.MODEL_NAMES,
help=("model architecture: " + " | ".join(ImageNetLightningModel.MODEL_NAMES) + " (default: resnet18)"),
)
parser.add_argument(
"-j", "--workers", default=4, type=int, metavar="N", help="number of data loading workers (default: 4)"
)
parser.add_argument(
"-b",
"--batch-size",
default=256,
type=int,
metavar="N",
help="mini-batch size (default: 256), this is the total batch size of all GPUs on the current node"
" when using Data Parallel or Distributed Data Parallel",
)
parser.add_argument(
"--lr", "--learning-rate", default=0.1, type=float, metavar="LR", help="initial learning rate", dest="lr"
)
parser.add_argument("--momentum", default=0.9, type=float, metavar="M", help="momentum")
parser.add_argument(
"--wd",
"--weight-decay",
default=1e-4,
type=float,
metavar="W",
help="weight decay (default: 1e-4)",
dest="weight_decay",
)
parser.add_argument("--pretrained", dest="pretrained", action="store_true", help="use pre-trained model")
return parent_parser
def main(args: Namespace) -> None:
if args.seed is not None:
pl.seed_everything(args.seed)
if args.accelerator == "ddp":
args.batch_size = int(args.batch_size / max(1, args.gpus))
args.workers = int(args.workers / max(1, args.gpus))
model = ImageNetLightningModel(**vars(args))
trainer = pl.Trainer.from_argparse_args(args)
if args.evaluate:
trainer.test(model)
else:
trainer.fit(model)
def run_cli():
parent_parser = ArgumentParser(add_help=False)
parent_parser = pl.Trainer.add_argparse_args(parent_parser)
parent_parser.add_argument("--data-path", default='../data/imagenet-c', metavar="DIR", type=str, help="path to dataset")
parent_parser.add_argument(
"-e", "--evaluate", dest="evaluate", action="store_true",
help="evaluate model on validation set"
)
parent_parser.add_argument("--seed", type=int, default=42, help="seed for initializing training.")
parser = ImageNetLightningModel.add_model_specific_args(parent_parser)
parser.set_defaults(profiler="simple", deterministic=True, max_epochs=90)
args = parser.parse_args()
main(args)
if __name__ == "__main__":
run_cli()