2019-11-09 06:02:21 +00:00
|
|
|
"""
|
|
|
|
This example is largely adapted from https://github.com/pytorch/examples/blob/master/imagenet/main.py
|
2020-08-09 10:02:07 +00:00
|
|
|
|
|
|
|
Before you can run this example, you will need to download the ImageNet dataset manually from the
|
|
|
|
`official website <http://image-net.org/download>`_ and place it into a folder `path/to/imagenet`.
|
|
|
|
|
|
|
|
Train on ImageNet with default parameters:
|
|
|
|
|
|
|
|
.. code-block: bash
|
|
|
|
|
|
|
|
python imagenet.py --data_root /path/to/imagenet
|
|
|
|
|
|
|
|
or show all options you can change:
|
|
|
|
|
|
|
|
.. code-block: bash
|
|
|
|
|
|
|
|
python imagenet.py --help
|
|
|
|
|
2019-11-09 06:02:21 +00:00
|
|
|
"""
|
|
|
|
import os
|
2020-08-07 22:33:51 +00:00
|
|
|
from argparse import ArgumentParser, Namespace
|
2019-11-09 06:02:21 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import torch.nn.functional as F
|
2020-01-20 19:50:31 +00:00
|
|
|
import torch.nn.parallel
|
2019-11-09 06:02:21 +00:00
|
|
|
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
|
2020-01-20 19:50:31 +00:00
|
|
|
import torchvision.models as models
|
|
|
|
import torchvision.transforms as transforms
|
2019-11-09 06:02:21 +00:00
|
|
|
|
|
|
|
import pytorch_lightning as pl
|
2020-02-27 21:07:51 +00:00
|
|
|
from pytorch_lightning.core import LightningModule
|
2019-11-09 06:02:21 +00:00
|
|
|
|
|
|
|
|
2020-02-27 21:07:51 +00:00
|
|
|
class ImageNetLightningModel(LightningModule):
|
2020-08-09 10:02:07 +00:00
|
|
|
|
|
|
|
# pull out resnet names from torchvision models
|
|
|
|
MODEL_NAMES = sorted(
|
|
|
|
name for name in models.__dict__
|
|
|
|
if name.islower() and not name.startswith("__") and callable(models.__dict__[name])
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
arch: str,
|
|
|
|
pretrained: bool,
|
|
|
|
lr: float,
|
|
|
|
momentum: float,
|
|
|
|
weight_decay: int,
|
|
|
|
data_path: str,
|
|
|
|
batch_size: int,
|
|
|
|
workers: int,
|
|
|
|
**kwargs,
|
|
|
|
):
|
2020-03-27 12:36:50 +00:00
|
|
|
super().__init__()
|
2020-08-09 10:02:07 +00:00
|
|
|
self.save_hyperparameters()
|
2020-05-24 22:59:08 +00:00
|
|
|
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
|
2020-08-09 10:02:07 +00:00
|
|
|
self.workers = workers
|
2020-05-24 22:59:08 +00:00
|
|
|
self.model = models.__dict__[self.arch](pretrained=self.pretrained)
|
2019-11-09 06:02:21 +00:00
|
|
|
|
2020-01-21 21:35:42 +00:00
|
|
|
def forward(self, x):
|
|
|
|
return self.model(x)
|
|
|
|
|
2019-11-09 06:02:21 +00:00
|
|
|
def training_step(self, batch, batch_idx):
|
|
|
|
images, target = batch
|
2020-03-27 07:17:56 +00:00
|
|
|
output = self(images)
|
2019-11-09 06:02:21 +00:00
|
|
|
loss_val = F.cross_entropy(output, target)
|
|
|
|
acc1, acc5 = self.__accuracy(output, target, topk=(1, 5))
|
|
|
|
|
|
|
|
tqdm_dict = {'train_loss': loss_val}
|
|
|
|
output = OrderedDict({
|
|
|
|
'loss': loss_val,
|
|
|
|
'acc1': acc1,
|
|
|
|
'acc5': acc5,
|
|
|
|
'progress_bar': tqdm_dict,
|
|
|
|
'log': tqdm_dict
|
|
|
|
})
|
|
|
|
return output
|
|
|
|
|
|
|
|
def validation_step(self, batch, batch_idx):
|
|
|
|
images, target = batch
|
2020-03-27 07:17:56 +00:00
|
|
|
output = self(images)
|
2019-11-09 06:02:21 +00:00
|
|
|
loss_val = F.cross_entropy(output, target)
|
|
|
|
acc1, acc5 = self.__accuracy(output, target, topk=(1, 5))
|
|
|
|
|
|
|
|
output = OrderedDict({
|
|
|
|
'val_loss': loss_val,
|
|
|
|
'val_acc1': acc1,
|
|
|
|
'val_acc5': acc5,
|
|
|
|
})
|
|
|
|
return output
|
|
|
|
|
2020-03-06 00:31:57 +00:00
|
|
|
def validation_epoch_end(self, outputs):
|
2019-11-09 06:02:21 +00:00
|
|
|
tqdm_dict = {}
|
|
|
|
for metric_name in ["val_loss", "val_acc1", "val_acc5"]:
|
2020-08-09 10:02:07 +00:00
|
|
|
tqdm_dict[metric_name] = torch.stack([output[metric_name] for output in outputs]).mean()
|
2019-11-09 06:02:21 +00:00
|
|
|
|
|
|
|
result = {'progress_bar': tqdm_dict, 'log': tqdm_dict, 'val_loss': tqdm_dict["val_loss"]}
|
|
|
|
return result
|
|
|
|
|
2020-08-09 10:02:07 +00:00
|
|
|
@staticmethod
|
|
|
|
def __accuracy(output, target, topk=(1,)):
|
2019-11-09 06:02:21 +00:00
|
|
|
"""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].view(-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(),
|
2020-05-24 22:59:08 +00:00
|
|
|
lr=self.lr,
|
|
|
|
momentum=self.momentum,
|
|
|
|
weight_decay=self.weight_decay
|
2019-11-09 06:02:21 +00:00
|
|
|
)
|
2020-08-09 10:02:07 +00:00
|
|
|
scheduler = lr_scheduler.LambdaLR(
|
|
|
|
optimizer,
|
|
|
|
lambda epoch: 0.1 ** (epoch // 30)
|
|
|
|
)
|
2019-11-09 06:02:21 +00:00
|
|
|
return [optimizer], [scheduler]
|
|
|
|
|
|
|
|
def train_dataloader(self):
|
2019-11-09 12:15:07 +00:00
|
|
|
normalize = transforms.Normalize(
|
|
|
|
mean=[0.485, 0.456, 0.406],
|
|
|
|
std=[0.229, 0.224, 0.225],
|
|
|
|
)
|
|
|
|
|
2020-05-24 22:59:08 +00:00
|
|
|
train_dir = os.path.join(self.data_path, 'train')
|
2019-11-09 06:02:21 +00:00
|
|
|
train_dataset = datasets.ImageFolder(
|
|
|
|
train_dir,
|
|
|
|
transforms.Compose([
|
|
|
|
transforms.RandomResizedCrop(224),
|
|
|
|
transforms.RandomHorizontalFlip(),
|
|
|
|
transforms.ToTensor(),
|
2019-11-09 12:15:07 +00:00
|
|
|
normalize,
|
2019-11-09 06:02:21 +00:00
|
|
|
]))
|
|
|
|
|
|
|
|
train_loader = torch.utils.data.DataLoader(
|
|
|
|
dataset=train_dataset,
|
2020-05-24 22:59:08 +00:00
|
|
|
batch_size=self.batch_size,
|
2020-08-09 10:02:07 +00:00
|
|
|
shuffle=True,
|
|
|
|
num_workers=self.workers,
|
2019-11-09 06:02:21 +00:00
|
|
|
)
|
|
|
|
return train_loader
|
|
|
|
|
|
|
|
def val_dataloader(self):
|
2019-11-09 12:15:07 +00:00
|
|
|
normalize = transforms.Normalize(
|
|
|
|
mean=[0.485, 0.456, 0.406],
|
|
|
|
std=[0.229, 0.224, 0.225],
|
|
|
|
)
|
2020-05-24 22:59:08 +00:00
|
|
|
val_dir = os.path.join(self.data_path, 'val')
|
2019-11-09 06:02:21 +00:00
|
|
|
val_loader = torch.utils.data.DataLoader(
|
|
|
|
datasets.ImageFolder(val_dir, transforms.Compose([
|
|
|
|
transforms.Resize(256),
|
|
|
|
transforms.CenterCrop(224),
|
|
|
|
transforms.ToTensor(),
|
2019-11-09 12:15:07 +00:00
|
|
|
normalize,
|
2019-11-09 06:02:21 +00:00
|
|
|
])),
|
2020-05-24 22:59:08 +00:00
|
|
|
batch_size=self.batch_size,
|
2019-11-09 06:02:21 +00:00
|
|
|
shuffle=False,
|
2020-08-09 10:02:07 +00:00
|
|
|
num_workers=self.workers,
|
2019-11-09 06:02:21 +00:00
|
|
|
)
|
|
|
|
return val_loader
|
|
|
|
|
2020-08-09 10:02:07 +00:00
|
|
|
def test_dataloader(self):
|
|
|
|
return self.val_dataloader()
|
|
|
|
|
|
|
|
def test_step(self, *args, **kwargs):
|
|
|
|
return self.validation_step(*args, **kwargs)
|
|
|
|
|
|
|
|
def test_epoch_end(self, *args, **kwargs):
|
|
|
|
outputs = self.validation_epoch_end(*args, **kwargs)
|
|
|
|
|
|
|
|
def substitute_val_keys(out):
|
|
|
|
return {k.replace('val', 'test'): v for k, v in out.items()}
|
|
|
|
|
|
|
|
outputs = {
|
|
|
|
'test_loss': outputs['val_loss'],
|
|
|
|
'progress_bar': substitute_val_keys(outputs['progress_bar']),
|
|
|
|
'log': substitute_val_keys(outputs['log']),
|
|
|
|
}
|
|
|
|
return outputs
|
|
|
|
|
2019-11-09 06:02:21 +00:00
|
|
|
@staticmethod
|
2020-03-19 13:14:29 +00:00
|
|
|
def add_model_specific_args(parent_parser): # pragma: no-cover
|
2020-06-01 15:38:52 +00:00
|
|
|
parser = ArgumentParser(parents=[parent_parser])
|
2020-08-09 10:02:07 +00:00
|
|
|
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)')
|
2019-11-09 06:02:21 +00:00
|
|
|
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 parser
|
|
|
|
|
|
|
|
|
2020-08-09 10:02:07 +00:00
|
|
|
def main(args: Namespace) -> None:
|
|
|
|
if args.seed is not None:
|
|
|
|
pl.seed_everything(args.seed)
|
2019-11-09 06:02:21 +00:00
|
|
|
|
2020-08-09 10:02:07 +00:00
|
|
|
if args.distributed_backend == 'ddp':
|
|
|
|
# When using a single GPU per process and per
|
|
|
|
# DistributedDataParallel, we need to divide the batch size
|
|
|
|
# ourselves based on the total number of GPUs we have
|
|
|
|
args.batch_size = int(args.batch_size / max(1, args.gpus))
|
|
|
|
args.workers = int(args.workers / max(1, args.gpus))
|
2019-11-09 06:02:21 +00:00
|
|
|
|
2020-06-01 15:38:52 +00:00
|
|
|
model = ImageNetLightningModel(**vars(args))
|
2020-08-09 10:02:07 +00:00
|
|
|
trainer = pl.Trainer.from_argparse_args(args)
|
2020-06-01 15:38:52 +00:00
|
|
|
|
|
|
|
if args.evaluate:
|
2020-08-09 10:02:07 +00:00
|
|
|
trainer.test(model)
|
2019-11-09 06:02:21 +00:00
|
|
|
else:
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
|
2020-08-09 10:02:07 +00:00
|
|
|
def run_cli():
|
|
|
|
parent_parser = ArgumentParser(add_help=False)
|
|
|
|
parent_parser = pl.Trainer.add_argparse_args(parent_parser)
|
|
|
|
parent_parser.add_argument('--data-path', 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=True,
|
|
|
|
deterministic=True,
|
|
|
|
max_epochs=90,
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
main(args)
|
|
|
|
|
|
|
|
|
2019-11-09 06:02:21 +00:00
|
|
|
if __name__ == '__main__':
|
2020-08-09 10:02:07 +00:00
|
|
|
run_cli()
|