-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
200 lines (168 loc) · 7.22 KB
/
app.py
File metadata and controls
200 lines (168 loc) · 7.22 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import transforms
from torch.utils.data import DataLoader
from torch.cuda.amp import GradScaler, autocast
import torch.optim as optim
from torch.optim.lr_scheduler import CosineAnnealingWarmRestarts
import numpy as np
# Preparing for Data
print('==> Preparing data..')
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.1, contrast=0.2, saturation=0.2, hue=0.1),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.activation = nn.ReLU(inplace=True)
def forward(self, x):
out = self.activation(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out = self.activation(out)
return out
class ResNet10(nn.Module):
def __init__(self, num_classes=10):
super(ResNet10, self).__init__()
self.in_planes = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) # Layer 1
self.bn1 = nn.BatchNorm2d(64)
self.activation = nn.ReLU(inplace=True)
self.layer1 = self._make_layer(BasicBlock, 64, 1) # layer 3
self.layer2 = self._make_layer(BasicBlock, 128, 1, stride=2) # layer 5
self.layer3 = self._make_layer(BasicBlock, 256, 1, stride=2) # layer 7
self.layer4 = self._make_layer(BasicBlock, 512, 1, stride=2) # layer 9
self.linear = nn.Linear(512 * BasicBlock.expansion, num_classes) # layer 10
def _make_layer(self, block, planes, num_blocks, stride=1):
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
def forward(self, x):
out = self.activation(self.bn1(self.conv1(x)))
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = F.avg_pool2d(out, 4)
out = out.view(out.size(0), -1)
out = self.linear(out)
return out
class LabelSmoothingLoss(nn.Module):
def __init__(self, classes, smoothing=0.0, dim=-1, weight=None):
super(LabelSmoothingLoss, self).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
self.cls = classes
self.dim = dim
self.weight = weight
def forward(self, pred, target, smoothing=None):
assert 0 <= self.smoothing < 1
pred = pred.log_softmax(dim=self.dim)
with torch.no_grad():
true_dist = torch.zeros_like(pred)
if smoothing is None:
smoothing = self.smoothing
if isinstance(smoothing, float):
smoothing_value = smoothing / (self.cls - 1)
true_dist.fill_(smoothing_value)
else:
smoothing = smoothing.unsqueeze(1)
true_dist.fill_(smoothing / (self.cls - 1))
true_dist.scatter_(1, target.data.unsqueeze(1), self.confidence)
if self.weight is not None:
loss = (torch.sum(-true_dist * pred, dim=self.dim) * self.weight).mean()
else:
loss = torch.mean(torch.sum(-true_dist * pred, dim=self.dim))
return loss
def train(model, device, train_loader, optimizer, epoch, scaler):
model.train()
loss_Fn = nn.CrossEntropyLoss()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
with autocast():
output = model(data)
loss = loss_Fn(output, target)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
if batch_idx % 10 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
def test(model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.cross_entropy(output, target, reduction='sum').item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
accuracy = 100. * correct / len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset), accuracy))
return accuracy
def main():
time0 = time.time()
lr = 0.001
weight_decay = 5e-4
epochs = 150
batch_size = 64
no_cuda = False
save_model = False
use_cuda = not no_cuda and torch.cuda.is_available()
torch.manual_seed(100)
device = torch.device("cuda" if use_cuda else "cpu")
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
train_loader = DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2, pin_memory=True)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
test_loader = DataLoader(testset, batch_size=100, shuffle=False, num_workers=2, pin_memory=True)
# Load a pre-trained model and fine-tune
model = ResNet10()
model = model.to(device)
# Mixed precision training
scaler = GradScaler()
# Optimizer
optimizer = optim.AdamW(model.parameters(), lr=lr, weight_decay=weight_decay)
# Learning rate scheduler
scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=10, T_mult=2, eta_min=1e-6)
# Label smoothing
criterion = LabelSmoothingLoss(classes=10, smoothing=0.1)
# Training and testing loop
best_accuracy = 0.0
for epoch in range(1, epochs + 1):
train(model, device, train_loader, optimizer, epoch, scaler)
test_accuracy = test(model, device, test_loader)
if test_accuracy > best_accuracy:
best_accuracy = test_accuracy
if save_model:
torch.save(model.state_dict(), "cifar_resnet10.pt")
# Update learning rate
scheduler.step()
time1 = time.time()
print('Training and Testing total execution time is: %s seconds' % (time1 - time0))
print(f'Best accuracy achieved: {best_accuracy:.2f}%')
if __name__ == '__main__':
main()