-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
332 lines (292 loc) · 13 KB
/
main.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import os
import time
import json
import argparse
import torch
import torchvision
import random
import numpy as np
from data import FaceDataset
from tqdm import tqdm
from torch import nn
from torch import optim
from collections import OrderedDict
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torch.optim import lr_scheduler
from torchvision.models.resnet import resnet18
from mean_variance_loss import MeanVarianceLoss
import cv2
LAMBDA_1 = 0.2
LAMBDA_2 = 0.05
START_AGE = 0
END_AGE = 69
VALIDATION_RATE= 0.1
random.seed(2019)
np.random.seed(2019)
torch.manual_seed(2019)
def ResNet18(num_classes):
model = resnet18(pretrained=True)
model.fc = nn.Sequential(
nn.BatchNorm1d(512),
nn.Dropout(0.5),
nn.Linear(512, num_classes),
)
return model
def train(train_loader, model, criterion1, criterion2, optimizer, epoch, result_directory):
model.train()
running_loss = 0.
running_mean_loss = 0.
running_variance_loss = 0.
running_softmax_loss = 0.
interval = 1
for i, sample in enumerate(train_loader):
images = sample['image'].cuda()
labels = sample['label'].cuda()
output = model(images)
mean_loss, variance_loss = criterion1(output, labels)
softmax_loss = criterion2(output, labels)
loss = mean_loss + variance_loss + softmax_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.data
running_softmax_loss += softmax_loss.data
running_mean_loss += mean_loss.data
running_variance_loss += variance_loss.data
if (i + 1) % interval == 0:
print('[%d, %5d] mean_loss: %.3f, variance_loss: %.3f, softmax_loss: %.3f, loss: %.3f'
% (epoch, i, running_mean_loss / interval,
running_variance_loss / interval,
running_softmax_loss / interval,
running_loss / interval))
with open(os.path.join(result_directory, 'log'), 'a') as f:
f.write('[%d, %5d] mean_loss: %.3f, variance_loss: %.3f, softmax_loss: %.3f, loss: %.3f\n'
% (epoch, i, running_mean_loss / interval,
running_variance_loss / interval,
running_softmax_loss / interval,
running_loss / interval))
running_loss = 0.
running_mean_loss = 0.
running_variance_loss = 0.
running_softmax_loss = 0.
def train_softmax(train_loader, model, criterion2, optimizer, epoch, result_directory):
model.train()
running_loss = 0.
running_softmax_loss = 0.
interval = 1
for i, sample in enumerate(train_loader):
images = sample['image'].cuda()
labels = sample['label'].cuda()
output = model(images)
loss = criterion2(output, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.data
if (i + 1) % interval == 0:
print('[%d, %5d] loss: %.3f'
% (epoch, i, running_loss / interval))
with open(os.path.join(result_directory, 'log'), 'a') as f:
f.write('[%d, %5d] loss: %.3f\n'
% (epoch, i, running_loss / interval))
running_loss = 0.
def evaluate(val_loader, model, criterion1, criterion2):
model.cuda()
model.eval()
loss_val = 0.
mean_loss_val = 0.
variance_loss_val = 0.
softmax_loss_val = 0.
mae = 0.
with torch.no_grad():
for i, sample in enumerate(val_loader):
image = sample['image'].cuda()
label = sample['label'].cuda()
output = model(image)
mean_loss, variance_loss = criterion1(output, label)
softmax_loss = criterion2(output, label)
loss = mean_loss + variance_loss + softmax_loss
loss_val += loss.data
mean_loss_val += mean_loss.data
variance_loss_val += variance_loss.data
softmax_loss_val += softmax_loss.data
m = nn.Softmax(dim=1)
output_softmax = m(output)
a = torch.arange(START_AGE, END_AGE + 1, dtype=torch.float32).cuda()
mean = (output_softmax * a).sum(1, keepdim=True).cpu().data.numpy()
pred = np.around(mean)
mae += np.absolute(pred - sample['label'].cpu().data.numpy())
return mean_loss_val / len(val_loader),\
variance_loss_val / len(val_loader),\
softmax_loss_val / len(val_loader),\
loss_val / len(val_loader),\
mae / len(val_loader)
def evaluate_softmax(val_loader, model, criterion2):
model.cuda()
model.eval()
loss_val = 0.
softmax_loss_val = 0.
mae = 0.
with torch.no_grad():
for i, sample in enumerate(val_loader):
image = sample['image'].cuda()
label = sample['label'].cuda()
output = model(image)
loss = criterion2(output, label)
loss_val += loss.data
m = nn.Softmax(dim=1)
output_softmax = m(output)
a = torch.arange(START_AGE, END_AGE + 1, dtype=torch.float32).cuda()
mean = (output_softmax * a).sum(1, keepdim=True).cpu().data.numpy()
pred = np.around(mean)
mae += np.absolute(pred - sample['label'].cpu().data.numpy())
return loss_val / len(val_loader), mae / len(val_loader)
def test(test_loader, model):
model.cuda()
model.eval()
mae = 0.
with torch.no_grad():
for i, sample in enumerate(test_loader):
image = sample['image'].cuda()
label = sample['label'].cuda()
output = model(image)
m = nn.Softmax(dim=1)
output = m(output)
a = torch.arange(START_AGE, END_AGE + 1, dtype=torch.float32).cuda()
mean = (output * a).sum(1, keepdim=True).cpu().data.numpy()
pred = np.around(mean)
mae += np.absolute(pred - sample['label'].cpu().data.numpy())
return mae / len(test_loader)
def predict(model, image):
model.eval()
with torch.no_grad():
image = image.astype(np.float32) / 255.
image = np.transpose(image, (2,0,1))
img = torch.from_numpy(image).cuda()
output = model(img[None])
m = nn.Softmax(dim=1)
output = m(output)
a = torch.arange(START_AGE, END_AGE + 1, dtype=torch.float32).cuda()
mean = (output * a).sum(1, keepdim=True).cpu().data.numpy()
pred = np.around(mean)[0][0]
return pred
def get_image_list(image_directory, leave_sub, validation_rate):
train_val_list = []
test_list = []
for fn in os.listdir(image_directory):
filepath = os.path.join(image_directory, fn)
subject = int(fn[:3])
if subject == leave_sub:
test_list.append(filepath)
else:
train_val_list.append(filepath)
num = len(train_val_list)
index_val = np.random.choice(num, int(num * validation_rate), replace=False)
train_list = []
val_list = []
for i, fp in enumerate(train_val_list):
if i in index_val:
val_list.append(fp)
else:
train_list.append(fp)
return train_list, val_list, test_list
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--batch_size', type=int, default=16)
parser.add_argument('-i', '--image_directory', type=str)
parser.add_argument('-ls', '--leave_subject', type=int)
parser.add_argument('-lr', '--learning_rate', type=float)
parser.add_argument('-e', '--epoch', type=int, default=0)
parser.add_argument('-r', '--resume', type=str, default=None)
parser.add_argument('-rd', '--result_directory', type=str, default=None)
parser.add_argument('-pi', '--pred_image', type=str, default=None)
parser.add_argument('-pm', '--pred_model', type=str, default=None)
parser.add_argument('-loss', '--is_mean_variance', action='store_true')
return parser.parse_args()
def main():
args = get_args()
if args.epoch > 0:
batch_size = args.batch_size
if args.result_directory is not None:
if not os.path.exists(args.result_directory):
os.mkdir(args.result_directory)
train_filepath_list, val_filepath_list, test_filepath_list\
= get_image_list(args.image_directory, args.leave_subject, VALIDATION_RATE)
transforms_train = torchvision.transforms.Compose([
torchvision.transforms.ToPILImage(),
torchvision.transforms.RandomApply(
[torchvision.transforms.RandomAffine(degrees=10, shear=16),
torchvision.transforms.RandomHorizontalFlip(p=1.0),
], p=0.5),
torchvision.transforms.Resize((256, 256)),
torchvision.transforms.RandomCrop((224, 224)),
torchvision.transforms.ToTensor()
])
train_gen = FaceDataset(train_filepath_list, transforms_train)
train_loader = DataLoader(train_gen, batch_size=batch_size, shuffle=True, pin_memory=True, num_workers=8)
transforms = torchvision.transforms.Compose([
torchvision.transforms.ToPILImage(),
torchvision.transforms.Resize((224, 224)),
torchvision.transforms.ToTensor()
])
val_gen = FaceDataset(val_filepath_list, transforms)
val_loader = DataLoader(val_gen, batch_size=1, shuffle=False, pin_memory=True, num_workers=8)
test_gen = FaceDataset(test_filepath_list, transforms)
test_loader = DataLoader(test_gen, batch_size=1, shuffle=False, pin_memory=True, num_workers=8)
model = ResNet18(END_AGE - START_AGE + 1)
model.cuda()
optimizer = optim.Adam(model.parameters(), lr = args.learning_rate)
criterion1 = MeanVarianceLoss(LAMBDA_1, LAMBDA_2, START_AGE, END_AGE).cuda()
criterion2 = torch.nn.CrossEntropyLoss().cuda()
scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=[80], gamma=0.1)
best_val_mae = np.inf
best_val_loss = np.inf
best_mae_epoch = -1
best_loss_epoch = -1
for epoch in range(args.epoch):
scheduler.step(epoch)
if args.is_mean_variance:
train(train_loader, model, criterion1, criterion2, optimizer, epoch, args.result_directory)
mean_loss, variance_loss, softmax_loss, loss_val, mae = evaluate(val_loader, model, criterion1, criterion2)
print('epoch: %d, mean_loss: %.3f, variance_loss: %.3f, softmax_loss: %.3f, loss: %.3f, mae: %3f' %
(epoch, mean_loss, variance_loss, softmax_loss, loss_val, mae))
with open(os.path.join(args.result_directory, 'log'), 'a') as f:
f.write('epoch: %d, mean_loss: %.3f, variance_loss: %.3f, softmax_loss: %.3f, loss: %.3f, mae: %3f\n' %
(epoch, mean_loss, variance_loss, softmax_loss, loss_val, mae))
else:
train_softmax(train_loader, model, criterion2, optimizer, epoch, args.result_directory)
loss_val, mae = evaluate_softmax(val_loader, model, criterion2)
print('epoch: %d, loss: %.3f, mae: %3f' % (epoch, loss_val, mae))
with open(os.path.join(args.result_directory, 'log'), 'a') as f:
f.write('epoch: %d, loss: %.3f, mae: %3f\n' % (epoch, loss_val, mae))
mae_test = test(test_loader, model)
print('epoch: %d, test_mae: %3f' % (epoch, mae_test))
with open(os.path.join(args.result_directory, 'log'), 'a') as f:
f.write('epoch: %d, mae_test: %3f\n' % (epoch, mae_test))
if best_val_mae > mae:
best_val_mae = mae
best_mae_epoch = epoch
torch.save(model.state_dict(), os.path.join(args.result_directory, "model_best_mae"))
if best_val_loss > loss_val:
best_val_loss = loss_val
best_loss_epoch = epoch
torch.save(model.state_dict(), os.path.join(args.result_directory, "model_best_loss"))
with open(os.path.join(args.result_directory, 'log'), 'a') as f:
f.write('best_loss_epoch: %d, best_val_loss: %f, best_mae_epoch: %d, best_val_mae: %f\n'
% (best_loss_epoch, best_val_loss, best_mae_epoch, best_val_mae))
print('best_loss_epoch: %d, best_val_loss: %f, best_mae_epoch: %d, best_val_mae: %f'
% (best_loss_epoch, best_val_loss, best_mae_epoch, best_val_mae))
if args.pred_image and args.pred_model:
model = ResNet34(END_AGE - START_AGE + 1)
model.cuda()
img = cv2.imread(args.pred_image)
resized_img = cv2.resize(img, (224, 224))
model.load_state_dict(torch.load(args.pred_model))
pred = predict(model, resized_img)
print('Age: ' + str(int(pred)))
cv2.putText(img, 'Age: ' + str(int(pred)), (int(img.shape[1]*0.1), int(img.shape[0]*0.9)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
name, ext = os.path.splitext(args.pred_image)
cv2.imwrite(name + '_result.jpg', img)
if __name__ == "__main__":
main()