-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtrain_resnet_umdface.py
executable file
·279 lines (226 loc) · 9.42 KB
/
train_resnet_umdface.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import argparse
import datetime
import os
import os.path as osp
import pytz
import torch
import torchvision
from torchvision import models
import torch.nn as nn
import torch.optim
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.autograd import Variable
import yaml
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
here = osp.dirname(osp.abspath(__file__)) # output folder is located here
root_dir,_ = osp.split(here)
import sys
sys.path.append(root_dir)
import train
from config import configurations
import utils
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--exp_name', default='resnet_umdfaces')
parser.add_argument('-c', '--config', type=int, default=1,
choices=configurations.keys())
parser.add_argument('-d', '--dataset_path',
default='/srv/data1/arunirc/datasets/UMDFaces/face_crops')
parser.add_argument('-m', '--model_path', default=None,
help='Initialize from pre-trained model')
parser.add_argument('--resume', help='Checkpoint path')
args = parser.parse_args()
# gpu = args.gpu
cfg = configurations[args.config]
out = get_log_dir(args.exp_name, args.config, cfg, verbose=False)
resume = args.resume
# os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu)
cuda = torch.cuda.is_available()
torch.manual_seed(1337)
if cuda:
torch.cuda.manual_seed(1337)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True # enable if all images are same size
# -----------------------------------------------------------------------------
# 1. Dataset
# -----------------------------------------------------------------------------
# Images should be arranged like this:
# data_root/
# class_1/....jpg..
# class_2/....jpg..
# ......./....jpg..
data_root = args.dataset_path
kwargs = {'num_workers': 4, 'pin_memory': True} if cuda else {}
RGB_MEAN = [ 0.485, 0.456, 0.406 ]
RGB_STD = [ 0.229, 0.224, 0.225 ]
# Data transforms
# http://pytorch.org/docs/master/torchvision/transforms.html
train_transform = transforms.Compose([
transforms.Scale(256), # smaller side resized
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean = RGB_MEAN,
std = RGB_STD),
])
val_transform = transforms.Compose([
transforms.Scale((224,224)),
# transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean = RGB_MEAN,
std = RGB_STD),
])
# Data loaders - using PyTorch built-in objects
# loader = DataLoaderClass(DatasetClass)
# * `DataLoaderClass` is PyTorch provided torch.utils.data.DataLoader
# * `DatasetClass` loads samples from a dataset; can be a standard class
# provided by PyTorch (datasets.ImageFolder) or a custom-made class.
# - More info: http://pytorch.org/docs/master/torchvision/datasets.html#imagefolder
# * Balanced class sampling: https://discuss.pytorch.org/t/balanced-sampling-between-classes-with-torchvision-dataloader/2703/3
traindir = osp.join(data_root, 'train')
dataset_train = datasets.ImageFolder(traindir, train_transform)
# For unbalanced dataset we create a weighted sampler
weights = utils.make_weights_for_balanced_classes(
dataset_train.imgs, len(dataset_train.classes))
weights = torch.DoubleTensor(weights)
sampler = torch.utils.data.sampler.WeightedRandomSampler(weights, len(weights))
train_loader = torch.utils.data.DataLoader(
dataset_train, batch_size=cfg['batch_size'],
sampler = sampler, **kwargs)
valdir = osp.join(data_root, 'val')
val_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(valdir, val_transform),
batch_size=cfg['batch_size'], shuffle=False, **kwargs)
# print 'dataset classes:' + str(train_loader.dataset.classes)
num_class = len(train_loader.dataset.classes)
print 'Number of classes: %d' % num_class
# -----------------------------------------------------------------------------
# 2. Model
# -----------------------------------------------------------------------------
model = torchvision.models.resnet50(pretrained=True) # ImageNet pre-trained for quicker convergence
# Check if final fc layer sizes match num_class
if not model.fc.weight.size()[0] == num_class:
# Replace last layer
print model.fc
model.fc = torch.nn.Linear(2048, num_class)
print model.fc
else:
pass
# TODO - config options for DataParallel and device_ids
model = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3, 4])
if cuda:
model.cuda()
if args.model_path:
# If existing model is to be loaded from a file
checkpoint = torch.load(args.model_path)
model.load_state_dict(checkpoint['model_state_dict'])
start_epoch = 0
start_iteration = 0
# Loss - cross entropy between predicted scores (unnormalized) and class labels (integers)
criterion = nn.CrossEntropyLoss()
if cuda:
criterion = criterion.cuda()
if resume:
# Resume training from last saved checkpoint
checkpoint = torch.load(resume)
model.load_state_dict(checkpoint['model_state_dict'])
start_epoch = checkpoint['epoch']
start_iteration = checkpoint['iteration']
else:
pass
# -----------------------------------------------------------------------------
# 3. Optimizer
# -----------------------------------------------------------------------------
params = filter(lambda p: p.requires_grad, model.parameters())
# Parameters with p.requires_grad=False are not updated during training.
# This can be specified when defining the nn.Modules during model creation
if 'optim' in cfg.keys():
if cfg['optim'].lower()=='sgd':
optim = torch.optim.SGD(params,
lr=cfg['lr'],
momentum=cfg['momentum'],
weight_decay=cfg['weight_decay'])
elif cfg['optim'].lower()=='adam':
optim = torch.optim.Adam(params,
lr=cfg['lr'], weight_decay=cfg['weight_decay'])
else:
raise NotImplementedError('Optimizers: SGD or Adam')
else:
optim = torch.optim.SGD(params,
lr=cfg['lr'],
momentum=cfg['momentum'],
weight_decay=cfg['weight_decay'])
if resume:
optim.load_state_dict(checkpoint['optim_state_dict'])
# -----------------------------------------------------------------------------
# [optional] Sanity-check: forward pass with a single batch
# -----------------------------------------------------------------------------
DEBUG = False
if DEBUG:
# model = model.cpu()
dataiter = iter(val_loader)
img, label = dataiter.next()
print 'Labels: ' + str(label.size()) # batchSize x num_class
print 'Input: ' + str(img.size()) # batchSize x 3 x 224 x 224
im = img.squeeze().numpy()
im = im[0,:,:,:] # get first image in the batch
im = im.transpose((1,2,0)) # permute to 224x224x3
im = im * [ 0.229, 0.224, 0.225 ] # unnormalize
im = im + [ 0.485, 0.456, 0.406 ]
im[im<0] = 0
f = plt.figure()
plt.imshow(im)
plt.savefig('sanity-check-im.jpg') # save transformed image in current folder
inputs = Variable(img)
if cuda:
inputs = inputs.cuda()
model.eval()
outputs = model(inputs)
print 'Network output: ' + str(outputs.size())
model.train()
import pdb; pdb.set_trace() # breakpoint c5e7c878 //
else:
pass
# -----------------------------------------------------------------------------
# 4. Training
# -----------------------------------------------------------------------------
trainer = train.Trainer(
cuda=cuda,
model=model,
criterion=criterion,
optimizer=optim,
init_lr=cfg['lr'],
lr_decay_epoch = cfg['lr_decay_epoch'],
train_loader=train_loader,
val_loader=val_loader,
out=out,
max_iter=cfg['max_iteration'],
interval_validate=cfg.get('interval_validate', len(train_loader)),
)
trainer.epoch = start_epoch
trainer.iteration = start_iteration
trainer.train()
def get_log_dir(model_name, config_id, cfg, verbose=True):
# Creates an output directory for each experiment, timestamped
name = 'MODEL-%s_CFG-%03d' % (model_name, config_id)
if verbose:
for k, v in cfg.items():
v = str(v)
if '/' in v:
continue
name += '_%s-%s' % (k.upper(), v)
now = datetime.datetime.now(pytz.timezone('US/Eastern'))
name += '_TIME-%s' % now.strftime('%Y%m%d-%H%M%S')
log_dir = osp.join(here, 'logs', name)
if not osp.exists(log_dir):
os.makedirs(log_dir)
with open(osp.join(log_dir, 'config.yaml'), 'w') as f:
yaml.safe_dump(cfg, f, default_flow_style=False)
return log_dir
if __name__ == '__main__':
main()