-
Notifications
You must be signed in to change notification settings - Fork 95
/
resnet-cifar10.py
168 lines (135 loc) · 6.27 KB
/
resnet-cifar10.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
import time
import configargparse
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torchvision.models.resnet import BasicBlock, ResNet
from ignite.engine import create_supervised_trainer, create_supervised_evaluator, Events
from ignite.metrics import Accuracy
from ignite.metrics import RunningAverage
from ignite.contrib.handlers import ProgressBar
from dropblock import DropBlock2D, LinearScheduler
results = []
class ResNetCustom(ResNet):
def __init__(self, block, layers, num_classes=1000, drop_prob=0., block_size=5):
super(ResNet, self).__init__()
self.inplanes = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.dropblock = LinearScheduler(
DropBlock2D(drop_prob=drop_prob, block_size=block_size),
start_value=0.,
stop_value=drop_prob,
nr_steps=5e3
)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
self.dropblock.step() # increment number of iterations
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.dropblock(self.layer1(x))
x = self.dropblock(self.layer2(x))
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.shape[0], -1)
x = self.fc(x)
return x
def resnet9(**kwargs):
return ResNetCustom(BasicBlock, [1, 1, 1, 1], **kwargs)
def logger(engine, model, evaluator, loader, pbar):
evaluator.run(loader)
metrics = evaluator.state.metrics
avg_accuracy = metrics['accuracy']
pbar.log_message(
"Test Results - Avg accuracy: {:.2f}, drop_prob: {:.2f}".format(avg_accuracy,
model.dropblock.dropblock.drop_prob)
)
results.append(avg_accuracy)
if __name__ == '__main__':
parser = configargparse.ArgumentParser()
parser.add_argument('-c', '--config', required=False,
is_config_file=True, help='config file')
parser.add_argument('--root', required=False, type=str, default='./data',
help='data root path')
parser.add_argument('--workers', required=False, type=int, default=4,
help='number of workers for data loader')
parser.add_argument('--bsize', required=False, type=int, default=256,
help='batch size')
parser.add_argument('--epochs', required=False, type=int, default=50,
help='number of epochs')
parser.add_argument('--lr', required=False, type=float, default=0.001,
help='learning rate')
parser.add_argument('--drop_prob', required=False, type=float, default=0.,
help='dropblock dropout probability')
parser.add_argument('--block_size', required=False, type=int, default=5,
help='dropblock block size')
parser.add_argument('--device', required=False, default=None, type=int,
help='CUDA device id for GPU training')
options = parser.parse_args()
root = options.root
bsize = options.bsize
workers = options.workers
epochs = options.epochs
lr = options.lr
drop_prob = options.drop_prob
block_size = options.block_size
device = 'cpu' if options.device is None \
else torch.device('cuda:{}'.format(options.device))
transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train_set = torchvision.datasets.CIFAR10(root=root, train=True,
download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=bsize,
shuffle=True, num_workers=workers)
test_set = torchvision.datasets.CIFAR10(root=root, train=False,
download=True, transform=transform)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=bsize,
shuffle=False, num_workers=workers)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# define model
model = resnet9(num_classes=len(classes), drop_prob=drop_prob, block_size=block_size)
# define loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
# create ignite engines
trainer = create_supervised_trainer(model=model,
optimizer=optimizer,
loss_fn=criterion,
device=device)
evaluator = create_supervised_evaluator(model,
metrics={'accuracy': Accuracy()},
device=device)
# ignite handlers
RunningAverage(output_transform=lambda x: x).attach(trainer, 'loss')
pbar = ProgressBar()
pbar.attach(trainer, ['loss'])
trainer.add_event_handler(Events.EPOCH_COMPLETED, logger, model, evaluator, test_loader, pbar)
# start training
t0 = time.time()
trainer.run(train_loader, max_epochs=epochs)
t1 = time.time()
print('Best Accuracy:', max(results))
print('Total time:', t1 - t0)