-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
200 lines (153 loc) · 7.02 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
# -*- coding: utf-8 -*-
import argparse
import os
import numpy as np
import torch
import torch.autograd as autograd
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as dsets
import torchvision.models as models
import lera
from data_loader import AVADataset
from models import *
def single_emd_loss(p, q, r=2):
"""
Earth Mover's Distance of one sample
Args:
p: true distribution of shape num_classes × 1
q: estimated distribution of shape num_classes × 1
r: norm parameter
"""
assert p.shape == q.shape, "Length of the two distribution must be the same"
length = p.shape[0]
emd_loss = 0.0
for i in range(1, length + 1):
emd_loss += torch.abs(sum(p[:i] - q[:i])) ** r
return (emd_loss / length) ** (1. / r)
def emd_loss(p, q, r=2):
"""
Earth Mover's Distance on a batch
Args:
p: true distribution of shape mini_batch_size × num_classes × 1
q: estimated distribution of shape mini_batch_size × num_classes × 1
r: norm parameters
"""
assert p.shape == q.shape, "Shape of the two distribution batches must be the same."
mini_batch_size = p.shape[0]
loss_vector = []
for i in range(mini_batch_size):
loss_vector.append(single_emd_loss(p[i], q[i], r=r))
return sum(loss_vector) / mini_batch_size
def main(config):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_transform = transforms.Compose([
transforms.Scale(256),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()])
val_transform = transforms.Compose([
transforms.Scale(256),
transforms.RandomCrop(224),
transforms.ToTensor()])
model = inpainting_D_AVA()
model = model.to(device)
conv_base_lr = config.conv_base_lr
dense_lr = config.dense_lr
optimizer = optim.Adam([
{'params': model.features.parameters(), 'lr': conv_base_lr},
{'params': model.classifier.parameters(), 'lr': dense_lr}
])
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(total_params)
lera.log_hyperparams({
'title': 'EMD Loss',
'train_batch_size': config.train_batch_size,
'val_batch_size': config.val_batch_size,
'optimizer': 'Adam',
'conv_base_lr': config.conv_base_lr,
'dense_lr': config.dense_lr
})
if config.train:
trainset = AVADataset(csv_file=config.train_csv_file, root_dir=config.train_img_path, transform=train_transform)
valset = AVADataset(csv_file=config.val_csv_file, root_dir=config.val_img_path, transform=val_transform)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=config.train_batch_size,
shuffle=True, num_workers=config.num_workers)
val_loader = torch.utils.data.DataLoader(valset, batch_size=config.val_batch_size,
shuffle=False, num_workers=config.num_workers)
# for early stopping
count = 0
init_val_loss = float('inf')
train_losses = []
val_losses = []
for epoch in range(config.warm_start_epoch, config.epochs):
batch_losses = []
for i, data in enumerate(train_loader):
images = data['image'].to(device)
labels = data['annotations'].to(device).float()
outputs = model(images)
outputs = outputs.view(-1, 10, 1)
optimizer.zero_grad()
loss = emd_loss(labels, outputs)
batch_losses.append(loss.item())
loss.backward()
optimizer.step()
lera.log('train_emd_loss', loss.item())
print('Epoch: %d/%d | Step: %d/%d | Training EMD loss: %.4f' % (epoch + 1, config.epochs, i + 1, len(trainset) // config.train_batch_size + 1, loss.data[0]))
avg_loss = sum(batch_losses) / (len(trainset) // config.train_batch_size + 1)
train_losses.append(avg_loss)
print('Epoch %d averaged training EMD loss: %.4f' % (epoch + 1, avg_loss))
# do validation after each epoch
batch_val_losses = []
for data in val_loader:
images = data['image'].to(device)
labels = data['annotations'].to(device).float()
with torch.no_grad():
outputs = model(images)
outputs = outputs.view(-1, 10, 1)
val_loss = emd_loss(labels, outputs)
batch_val_losses.append(val_loss.item())
avg_val_loss = sum(batch_val_losses) / (len(valset) // config.val_batch_size + 1)
val_losses.append(avg_val_loss)
lera.log('val_emd_loss', avg_val_loss)
print('Epoch %d completed. Averaged EMD loss on val set: %.4f.' % (epoch + 1, avg_val_loss))
# Use early stopping to monitor training
if avg_val_loss < init_val_loss:
init_val_loss = avg_val_loss
# save model weights if val loss decreases
print('Saving model...')
if not os.path.exists(config.ckpt_path):
os.makedirs(config.ckpt_path)
torch.save(model.state_dict(), os.path.join(config.ckpt_path, 'epoch-%d.pkl' % (epoch + 1)))
print('Done.\n')
# reset count
count = 0
elif avg_val_loss >= init_val_loss:
count += 1
if count == config.early_stopping_patience:
print('Val EMD loss has not decreased in %d epochs. Training terminated.' % config.early_stopping_patience)
break
print('Training completed.')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# input parameters
parser.add_argument('--train_img_path', type=str, default='/path/to/training/dataset')
parser.add_argument('--val_img_path', type=str, default='/path/to/validation/dataset')
parser.add_argument('--train_csv_file', type=str, default='/path/to/training/csv')
parser.add_argument('--val_csv_file', type=str, default='/path/to/validation/csv')
# training parameters
parser.add_argument('--train', type=bool, default=True)
parser.add_argument('--conv_base_lr', type=float, default=1e-5)
parser.add_argument('--dense_lr', type=float, default=1e-4)
parser.add_argument('--train_batch_size', type=int, default=256)
parser.add_argument('--val_batch_size', type=int, default=256)
parser.add_argument('--num_workers', type=int, default=2)
parser.add_argument('--epochs', type=int, default=100)
# misc
parser.add_argument('--ckpt_path', type=str, default='models')
parser.add_argument('--gpu_ids', type=list, default=1)
parser.add_argument('--warm_start', type=bool, default=False)
parser.add_argument('--warm_start_epoch', type=int, default=0)
parser.add_argument('--early_stopping_patience', type=int, default=5)
config = parser.parse_args()
main(config)