PyTorch Lightning 1.1: research : CIFAR100 (VGG)
作成 : (株)クラスキャット セールスインフォメーション
作成日時 : 02/24/2021 (1.1.x)
* 本ページは以下の CIFAR10 用リソースを参考に CIFAR100 で遂行した実験結果のレポートです:
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
★ 無料セミナー実施中 ★ クラスキャット主催 人工知能 & ビジネス Web セミナー

人工知能とビジネスをテーマにウェビナー (WEB セミナー) を定期的に開催しています。スケジュールは弊社 公式 Web サイト でご確認頂けます。
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
- Windows PC のブラウザからご参加が可能です。スマートデバイスもご利用可能です。
クラスキャットは人工知能・テレワークに関する各種サービスを提供しております :
| 人工知能研究開発支援 | 人工知能研修サービス | テレワーク & オンライン授業を支援 |
| PoC(概念実証)を失敗させないための支援 (本支援はセミナーに参加しアンケートに回答した方を対象としています。) | ||
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
| 株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション |
| E-Mail:sales-info@classcat.com ; WebSite: https://www.classcat.com/ |
| Facebook: https://www.facebook.com/ClassCatJP/ |
research: CIFAR100 (VGG)
VGG11
仕様
- VGG11
- Total params: 9,277,284 (9.3 M)
- Trainable params: 9,277,284
- Non-trainable params: 0
結果
- VGG11
- {‘test_acc’: 0.670199990272522, ‘test_loss’: 1.3739653825759888}
- 100 エポック ; Wall time: 45min 10s
- ReduceLROnPlateau
VGG13
仕様
- VGG13
- Total params: 9,462,180 (9.5 M)
- Trainable params: 9,462,180
- Non-trainable params: 0
結果
- VGG13
- {‘test_acc’: 0.7024999856948853, ‘test_loss’: 1.2473818063735962}
- 100 エポック ; Wall time: 56min 51s
- ReduceLROnPlateau
VGG16
仕様
- VGG16
- Total params: 14,774,436 (14.8 M)
- Trainable params: 14,774,436
- Non-trainable params: 0
結果
- VGG16
- {‘test_acc’: 0.6888999938964844, ‘test_loss’: 1.7077816724777222}
- 100 エポック ; Wall time: 1h 8min 2s
- ReduceLROnPlateau
CIFAR 100 DM
from typing import Any, Callable, Optional, Sequence, Union
from pl_bolts.datamodules.vision_datamodule import VisionDataModule
#from pl_bolts.datasets import TrialCIFAR10
#from pl_bolts.transforms.dataset_normalizations import cifar10_normalization
from pl_bolts.utils import _TORCHVISION_AVAILABLE
from pl_bolts.utils.warnings import warn_missing_pkg
if _TORCHVISION_AVAILABLE:
from torchvision import transforms
#from torchvision import transforms as transform_lib
from torchvision.datasets import CIFAR100
else: # pragma: no cover
warn_missing_pkg('torchvision')
CIFAR100 = None
def cifar100_normalization():
if not _TORCHVISION_AVAILABLE: # pragma: no cover
raise ModuleNotFoundError(
'You want to use `torchvision` which is not installed yet, install it with `pip install torchvision`.'
)
normalize = transforms.Normalize(
mean=[x / 255.0 for x in [129.3, 124.1, 112.4]],
std=[x / 255.0 for x in [68.2, 65.4, 70.4]],
# cifar10
#mean=[x / 255.0 for x in [125.3, 123.0, 113.9]],
#std=[x / 255.0 for x in [63.0, 62.1, 66.7]],
)
return normalize
class CIFAR100DataModule(VisionDataModule):
"""
.. figure:: https://3qeqpr26caki16dnhd19sv6by6v-wpengine.netdna-ssl.com/wp-content/uploads/2019/01/
Plot-of-a-Subset-of-Images-from-the-CIFAR-10-Dataset.png
:width: 400
:alt: CIFAR-10
Specs:
- 10 classes (1 per class)
- Each image is (3 x 32 x 32)
Standard CIFAR10, train, val, test splits and transforms
Transforms::
mnist_transforms = transform_lib.Compose([
transform_lib.ToTensor(),
transforms.Normalize(
mean=[x / 255.0 for x in [125.3, 123.0, 113.9]],
std=[x / 255.0 for x in [63.0, 62.1, 66.7]]
)
])
Example::
from pl_bolts.datamodules import CIFAR10DataModule
dm = CIFAR10DataModule(PATH)
model = LitModel()
Trainer().fit(model, datamodule=dm)
Or you can set your own transforms
Example::
dm.train_transforms = ...
dm.test_transforms = ...
dm.val_transforms = ...
"""
name = "cifar100"
dataset_cls = CIFAR100
dims = (3, 32, 32)
def __init__(
self,
data_dir: Optional[str] = None,
val_split: Union[int, float] = 0.2,
num_workers: int = 16,
normalize: bool = False,
batch_size: int = 32,
seed: int = 42,
shuffle: bool = False,
pin_memory: bool = False,
drop_last: bool = False,
*args: Any,
**kwargs: Any,
) -> None:
"""
Args:
data_dir: Where to save/load the data
val_split: Percent (float) or number (int) of samples to use for the validation split
num_workers: How many workers to use for loading data
normalize: If true applies image normalize
batch_size: How many samples per batch to load
seed: Random seed to be used for train/val/test splits
shuffle: If true shuffles the train data every epoch
pin_memory: If true, the data loader will copy Tensors into CUDA pinned memory before
returning them
drop_last: If true drops the last incomplete batch
"""
super().__init__( # type: ignore[misc]
data_dir=data_dir,
val_split=val_split,
num_workers=num_workers,
normalize=normalize,
batch_size=batch_size,
seed=seed,
shuffle=shuffle,
pin_memory=pin_memory,
drop_last=drop_last,
*args,
**kwargs,
)
@property
def num_samples(self) -> int:
train_len, _ = self._get_splits(len_dataset=50_000)
return train_len
@property
def num_classes(self) -> int:
"""
Return:
10
"""
return 100
def default_transforms(self) -> Callable:
if self.normalize:
cf100_transforms = transforms.Compose([transform_lib.ToTensor(), cifar100_normalization()])
else:
cf100_transforms = transforms.Compose([transform_lib.ToTensor()])
return cf100_transforms
モデル
import torch import torch.nn as nn
cfg = {
'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
class VGG(nn.Module):
def __init__(self, vgg_name):
super(VGG, self).__init__()
self.features = self._make_layers(cfg[vgg_name])
self.classifier = nn.Linear(512, 100)
#self.classifier = nn.Linear(512, 10)
def forward(self, x):
out = self.features(x)
out = out.view(out.size(0), -1)
out = self.classifier(out)
return out
def _make_layers(self, cfg):
layers = []
in_channels = 3
for x in cfg:
if x == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
nn.BatchNorm2d(x),
nn.ReLU(inplace=True)]
in_channels = x
layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
return nn.Sequential(*layers)
net = VGG('VGG11')
print(net)
x = torch.randn(2,3,32,32)
y = net(x)
print(y.size())
VGG(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace=True)
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(4): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(5): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(6): ReLU(inplace=True)
(7): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(8): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(9): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(10): ReLU(inplace=True)
(11): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(12): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(13): ReLU(inplace=True)
(14): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(15): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(16): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(17): ReLU(inplace=True)
(18): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(19): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(20): ReLU(inplace=True)
(21): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(22): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(23): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(24): ReLU(inplace=True)
(25): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(26): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(27): ReLU(inplace=True)
(28): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(29): AvgPool2d(kernel_size=1, stride=1, padding=0)
)
(classifier): Linear(in_features=512, out_features=100, bias=True)
)
torch.Size([2, 100])
from torchsummary import summary
summary(VGG('VGG11').to('cuda'), (3, 32, 32))
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 32, 32] 1,792
BatchNorm2d-2 [-1, 64, 32, 32] 128
ReLU-3 [-1, 64, 32, 32] 0
MaxPool2d-4 [-1, 64, 16, 16] 0
Conv2d-5 [-1, 128, 16, 16] 73,856
BatchNorm2d-6 [-1, 128, 16, 16] 256
ReLU-7 [-1, 128, 16, 16] 0
MaxPool2d-8 [-1, 128, 8, 8] 0
Conv2d-9 [-1, 256, 8, 8] 295,168
BatchNorm2d-10 [-1, 256, 8, 8] 512
ReLU-11 [-1, 256, 8, 8] 0
Conv2d-12 [-1, 256, 8, 8] 590,080
BatchNorm2d-13 [-1, 256, 8, 8] 512
ReLU-14 [-1, 256, 8, 8] 0
MaxPool2d-15 [-1, 256, 4, 4] 0
Conv2d-16 [-1, 512, 4, 4] 1,180,160
BatchNorm2d-17 [-1, 512, 4, 4] 1,024
ReLU-18 [-1, 512, 4, 4] 0
Conv2d-19 [-1, 512, 4, 4] 2,359,808
BatchNorm2d-20 [-1, 512, 4, 4] 1,024
ReLU-21 [-1, 512, 4, 4] 0
MaxPool2d-22 [-1, 512, 2, 2] 0
Conv2d-23 [-1, 512, 2, 2] 2,359,808
BatchNorm2d-24 [-1, 512, 2, 2] 1,024
ReLU-25 [-1, 512, 2, 2] 0
Conv2d-26 [-1, 512, 2, 2] 2,359,808
BatchNorm2d-27 [-1, 512, 2, 2] 1,024
ReLU-28 [-1, 512, 2, 2] 0
MaxPool2d-29 [-1, 512, 1, 1] 0
AvgPool2d-30 [-1, 512, 1, 1] 0
Linear-31 [-1, 100] 51,300
================================================================
Total params: 9,277,284
Trainable params: 9,277,284
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.01
Forward/backward pass size (MB): 3.71
Params size (MB): 35.39
Estimated Total Size (MB): 39.11
----------------------------------------------------------------
Lightning モジュール
import torch import torch.nn as nn import torch.nn.functional as F from torch.optim.lr_scheduler import OneCycleLR, CyclicLR, ExponentialLR, CosineAnnealingLR, ReduceLROnPlateau from torch.optim.swa_utils import AveragedModel, update_bn import torchvision import pytorch_lightning as pl from pytorch_lightning.callbacks import LearningRateMonitor, GPUStatsMonitor, EarlyStopping from pytorch_lightning.metrics.functional import accuracy #from pl_bolts.datamodules import CIFAR10DataModule #from pl_bolts.transforms.dataset_normalizations import cifar10_normalization
pl.seed_everything(7);
batch_size = 100
train_transforms = torchvision.transforms.Compose([
torchvision.transforms.RandomCrop(32, padding=4),
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.ToTensor(),
cifar100_normalization(),
])
test_transforms = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
cifar100_normalization(),
])
cifar100_dm = CIFAR100DataModule(
batch_size=batch_size,
num_workers=8,
train_transforms=train_transforms,
test_transforms=test_transforms,
val_transforms=test_transforms,
)
class LitCifar100(pl.LightningModule):
def __init__(self, model_name='VGG11', lr=0.05, factor=0.8):
super().__init__()
self.save_hyperparameters()
self.model = VGG(model_name)
def forward(self, x):
out = self.model(x)
return F.log_softmax(out, dim=1)
def training_step(self, batch, batch_idx):
x, y = batch
logits = F.log_softmax(self.model(x), dim=1)
loss = F.nll_loss(logits, y)
self.log('train_loss', loss)
return loss
def evaluate(self, batch, stage=None):
x, y = batch
logits = self(x)
loss = F.nll_loss(logits, y)
preds = torch.argmax(logits, dim=1)
acc = accuracy(preds, y)
if stage:
self.log(f'{stage}_loss', loss, prog_bar=True)
self.log(f'{stage}_acc', acc, prog_bar=True)
def validation_step(self, batch, batch_idx):
self.evaluate(batch, 'val')
def test_step(self, batch, batch_idx):
self.evaluate(batch, 'test')
def configure_optimizers(self):
optimizer = torch.optim.SGD(self.parameters(), lr=self.hparams.lr, momentum=0.9, weight_decay=5e-4)
return {
'optimizer': optimizer,
'lr_scheduler': ReduceLROnPlateau(optimizer, 'max', patience=5, factor=self.hparams.factor, verbose=True, threshold=0.0001, threshold_mode='abs', cooldown=1, min_lr=1e-5),
'monitor': 'val_acc'
}
訓練 / 評価
%%time
model = LitCifar100('VGG11', lr=0.05, factor=0.5)
model.datamodule = cifar100_dm
trainer = pl.Trainer(
gpus=2,
accelerator='dp',
max_epochs=100,
progress_bar_refresh_rate=100,
logger=pl.loggers.TensorBoardLogger('tblogs/', name='vgg11'),
callbacks=[LearningRateMonitor(logging_interval='step')],
)
trainer.fit(model, cifar100_dm)
trainer.test(model, datamodule=cifar100_dm);
GPU available: True, used: True
TPU available: None, using: 0 TPU cores
(...)
| Name | Type | Params
-------------------------------
0 | model | VGG | 9.3 M
-------------------------------
9.3 M Trainable params
0 Non-trainable params
9.3 M Total params
37.109 Total estimated model params size (MB)
(...)
(...)
--------------------------------------------------------------------------------
DATALOADER:0 TEST RESULTS
{'test_acc': 0.670199990272522, 'test_loss': 1.3739653825759888}
--------------------------------------------------------------------------------
CPU times: user 27min 27s, sys: 7min 27s, total: 34min 55s
Wall time: 45min 10s
以上