-
Notifications
You must be signed in to change notification settings - Fork 3
/
classification_template.py
133 lines (114 loc) · 4.21 KB
/
classification_template.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
import torchvision
from torch.nn import *
from torchvision.datasets import CIFAR10
from torchvision.transforms import *
from torch.optim import *
from torch.optim.lr_scheduler import CosineAnnealingLR
from torch.utils.data import random_split
from nntoolbox.vision.components import *
from nntoolbox.vision.learner import SupervisedImageLearner
from nntoolbox.utils import load_model, get_device
from nntoolbox.callbacks import *
from nntoolbox.metrics import Accuracy, Loss
from nntoolbox.vision.models import ImageClassifier
from nntoolbox.losses import SmoothedCrossEntropy
torch.backends.cudnn.benchmark=True
data = CIFAR10('data/', train=True, download=True, transform=ToTensor())
train_size = int(0.8 * len(data))
val_size = len(data) - train_size
train_dataset, val_dataset = torch.utils.data.random_split(data, [train_size, val_size])
train_dataset.dataset.transform = Compose(
[
RandomHorizontalFlip(),
RandomResizedCrop(size=32, scale=(0.95, 1.0)),
ToTensor()
]
)
test_dataset = torchvision.datasets.CIFAR10('data/', train=False, download=True, transform=ToTensor())
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=128, shuffle=False)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=128, shuffle=False)
print("Number of batches per epoch " + str(len(train_loader)))
class SEResNeXtShakeShake(ResNeXtBlock):
def __init__(self, in_channels, reduction_ratio=16, cardinality=2, activation=nn.ReLU, normalization=nn.BatchNorm2d):
super(SEResNeXtShakeShake, self).__init__(
branches=nn.ModuleList(
[
nn.Sequential(
ConvolutionalLayer(
in_channels, in_channels // 4, kernel_size=1, padding=0,
activation=activation, normalization=normalization
),
ConvolutionalLayer(
in_channels // 4, in_channels, kernel_size=3, padding=1,
activation=activation, normalization=normalization
),
SEBlock(in_channels, reduction_ratio)
) for _ in range(cardinality)
]
),
use_shake_shake=True
)
model = Sequential(
ConvolutionalLayer(in_channels=3, out_channels=16, kernel_size=3, activation=nn.ReLU),
SEResNeXtShakeShake(in_channels=16, activation=nn.ReLU),
ConvolutionalLayer(
in_channels=16, out_channels=32,
activation=nn.ReLU,
kernel_size=2, stride=2
),
SEResNeXtShakeShake(in_channels=32),
ConvolutionalLayer(
in_channels=32, out_channels=64,
kernel_size=2, stride=2
),
FeedforwardBlock(
in_channels=64,
out_features=10,
pool_output_size=2,
hidden_layer_sizes=(256, 128)
)
).to(get_device())
optimizer = SGD(model.parameters(), weight_decay=0.0001, lr=0.094, momentum=0.9)
learner = SupervisedImageLearner(
train_data=train_loader,
val_data=val_loader,
model=model,
criterion=SmoothedCrossEntropy().to(get_device()),
optimizer=optimizer,
mixup=True
)
callbacks = [
ToDeviceCallback(),
Tensorboard(),
LRSchedulerCB(CosineAnnealingLR(optimizer, eta_min=0.024, T_max=405)),
GradualLRWarmup(min_lr=0.024, max_lr=0.094, duration=810),
LossLogger(),
ModelCheckpoint(learner=learner, filepath="weights/model.pt", monitor='accuracy', mode='max'),
]
metrics = {
"accuracy": Accuracy(),
"loss": Loss()
}
final = learner.learn(
n_epoch=500,
callbacks=callbacks,
metrics=metrics,
final_metric='accuracy'
)
print(final)
load_model(model=model, path="weights/model.pt")
classifier = ImageClassifier(model, tta_transform=Compose([
RandomHorizontalFlip(),
RandomResizedCrop(size=32, scale=(0.95, 1.0)),
ToTensor()
]))
print(classifier.evaluate(test_loader))
print("Test SWA:")
model = swa.get_averaged_model()
classifier = ImageClassifier(model, tta_transform=Compose([
RandomHorizontalFlip(),
RandomResizedCrop(size=32, scale=(0.95, 1.0)),
ToTensor()
]))
print(classifier.evaluate(test_loader))