-
Notifications
You must be signed in to change notification settings - Fork 14
/
imagenet_utils.py
183 lines (144 loc) · 5.52 KB
/
imagenet_utils.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
# (C) Copyright IBM 2019.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
import time
from PIL import Image
import multiprocessing
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from tqdm import tqdm
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k 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 get_augmentor(is_train, image_size, strong=False):
augments = []
if is_train:
if strong:
augments.append(transforms.RandomRotation(10))
augments += [
transforms.RandomResizedCrop(image_size, interpolation=Image.BILINEAR),
transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4),
transforms.RandomHorizontalFlip()
]
else:
augments += [
transforms.Resize(int(image_size / 0.875 + 0.5) if image_size ==
224 else image_size, interpolation=Image.BILINEAR),
transforms.CenterCrop(image_size)
]
augments += [
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
]
augmentor = transforms.Compose(augments)
return augmentor
def get_imagenet_dataflow(is_train, data_dir, batch_size, augmentor, workers=18, is_distributed=False):
workers = min(workers, multiprocessing.cpu_count())
sampler = None
shuffle = False
if is_train:
dataset = datasets.ImageFolder(data_dir, augmentor)
sampler = torch.utils.data.distributed.DistributedSampler(dataset) if is_distributed else None
shuffle = sampler is None
else:
dataset = datasets.ImageFolder(data_dir, augmentor)
data_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=shuffle,
num_workers=workers, pin_memory=True, sampler=sampler)
return data_loader
def train(data_loader, model, criterion, optimizer, epoch,
steps_per_epoch=99999999999):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to train mode
model.train()
end = time.time()
num_batch = 0
with tqdm(total=len(data_loader)) as t_bar:
for i, (input, target) in enumerate(data_loader):
# measure data loading time
data_time.update(time.time() - end)
# compute output
output = model(input)
target = target.cuda(non_blocking=True)
loss = criterion(output, target)
# measure accuracy and record loss
prec1, prec5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(prec1[0], input.size(0))
top5.update(prec5[0], input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
num_batch += 1
t_bar.update(1)
if i > steps_per_epoch:
break
return top1.avg, top5.avg, losses.avg, batch_time.avg, data_time.avg, num_batch
def validate(data_loader, model, criterion):
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
with torch.no_grad(), tqdm(total=len(data_loader)) as t_bar:
end = time.time()
for i, (input, target) in enumerate(data_loader):
target = target.cuda(non_blocking=True)
# compute output
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
prec1, prec5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(prec1[0], input.size(0))
top5.update(prec5[0], input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
t_bar.update(1)
return top1.avg, top5.avg, losses.avg, batch_time.avg