forked from moskomule/senet.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagenet.py
47 lines (35 loc) · 1.44 KB
/
imagenet.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
import torch
from torch.nn import functional as F
from homura import callbacks, init_distributed, lr_scheduler, optim, reporters, is_distributed
from homura.trainers import SupervisedTrainer
from homura.vision import DATASET_REGISTRY
from senet.se_resnet import se_resnet50
def main():
if is_distributed():
init_distributed()
model = se_resnet50(num_classes=1000)
optimizer = optim.SGD(lr=0.6 / 1024 * args.batch_size, momentum=0.9, weight_decay=1e-4)
scheduler = lr_scheduler.MultiStepLR([50, 70])
train_loader, test_loader = DATASET_REGISTRY("imagenet")(args.batch_size)
c = [
callbacks.AccuracyCallback(),
callbacks.AccuracyCallback(k=5),
callbacks.LossCallback(),
callbacks.WeightSave("."),
reporters.TensorboardReporter("."),
reporters.TQDMReporter(range(args.epochs)),
]
with SupervisedTrainer(model, optimizer, F.cross_entropy, callbacks=c, scheduler=scheduler,) as trainer:
for _ in c[-1]:
trainer.train(train_loader)
trainer.test(test_loader)
if __name__ == "__main__":
import argparse
import warnings
warnings.filterwarnings("ignore", "(Possibly )?corrupt EXIF data", UserWarning)
p = argparse.ArgumentParser()
p.add_argument("--epochs", type=int, default=90)
p.add_argument("--batch_size", type=int, default=128)
p.add_argument("--local_rank", type=int, default=-1)
args = p.parse_args()
main()