-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_stage_1.py
167 lines (131 loc) · 6.37 KB
/
train_stage_1.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
import torch
import datasets, models, losses, utils
from tqdm import tqdm
def get_args_parser():
import argparse
parser = argparse.ArgumentParser(description='Deep Face Drawing: Train Stage 1')
parser.add_argument('--dataset', type=str, required=True, help='Path to training dataset.')
parser.add_argument('--dataset_validation', type=str, default=None, help='Path to validation dataset.')
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--epochs', type=int, required=True)
parser.add_argument('--resume', type=str, default=None, help='Path to load model weights.')
parser.add_argument('--output', type=str, default=None, help='Path to save weights.')
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--comet', type=str, default=None, help='comet.ml API')
parser.add_argument('--comet_log_image', type=str, default=None, help='Path to model input image to be inference and log the result to comet.ml. Skipped if --comet is not given.')
args = parser.parse_args()
return args
def validation_parser(args):
if not args.comet:
if args.comet_log_image: print('args.comet_log_image will be skipped.')
def main(args):
device = torch.device(args.device)
print(f'Device : {device}')
if args.comet:
from comet_ml import Experiment
experiment = Experiment(
api_key=args.comet,
project_name="Deep Face Drawing: Training Stage 1",
workspace="xu-justin",
log_code=True
)
if args.comet_log_image:
log_image_sketch = datasets.dataloader.load_one_sketch(args.comet_log_image).unsqueeze(0).to(device)
model = models.DeepFaceDrawing(
CE=True, CE_encoder=True, CE_decoder=True,
FM=False, FM_decoder=False,
IS=False, IS_generator=False, IS_discriminator=False,
manifold=False
)
if args.comet:
experiment.set_model_graph(model)
if args.resume:
model.load(args.resume, map_location=device)
model.to(device)
train_dataloader = datasets.dataloader.dataloader(args.dataset, batch_size=args.batch_size, load_photo=False, augmentation=False)
if args.dataset_validation:
validation_dataloader = datasets.dataloader.dataloader(args.dataset_validation, batch_size=args.batch_size, load_photo=False)
optimizer = torch.optim.Adam(model.CE.parameters(), lr=0.0002, betas=(0.5, 0.999))
mse = losses.MSE()
for epoch in range(args.epochs):
running_loss = {
'loss_left_eye' : 0,
'loss_right_eye' : 0,
'loss_nose' : 0,
'loss_mouth' : 0,
'loss_background' : 0
}
model.train()
for sketches in tqdm(train_dataloader, desc=f'Epoch - {epoch+1} / {args.epochs}'):
iteration_loss = {
'loss_left_eye_it' : 0,
'loss_right_eye_it' : 0,
'loss_nose_it' : 0,
'loss_mouth_it' : 0,
'loss_background_it' : 0
}
optimizer.zero_grad()
sketches = sketches.to(device)
patches = model.CE.crop(sketches)
repatches = model.CE.decode(model.CE.encode(patches))
for key in model.components:
loss = mse.compute(repatches[key], patches[key])
loss.backward()
iteration_loss[f'loss_{key}_it'] += loss.item()
optimizer.step()
for key, loss in iteration_loss.items():
running_loss[key[:-3]] += loss * len(sketches) / len(train_dataloader.dataset)
if args.comet:
experiment.log_metrics(iteration_loss)
if args.dataset_validation:
validation_running_loss = {
'val_loss_left_eye' : 0,
'val_loss_right_eye' : 0,
'val_loss_nose' : 0,
'val_loss_mouth' : 0,
'val_loss_background' : 0
}
model.eval()
with torch.no_grad():
for sketches in tqdm(validation_dataloader, desc=f'Validation Epoch - {epoch+1} / {args.epochs}'):
validation_iteration_loss = {
'val_loss_left_eye_it' : 0,
'val_loss_right_eye_it' : 0,
'val_loss_nose_it' : 0,
'val_loss_mouth_it' : 0,
'val_loss_background_it' : 0
}
sketches = sketches.to(device)
patches = model.CE.crop(sketches)
repatches = model.CE.decode(model.CE.encode(patches))
for key in model.components:
loss = mse.compute(repatches[key], patches[key])
validation_iteration_loss[f'val_loss_{key}_it'] += loss.item()
for key, loss in validation_iteration_loss.items():
validation_running_loss[key[:-3]] += loss * len(sketches) / len(validation_dataloader.dataset)
if args.comet:
experiment.log_metrics(validation_iteration_loss)
def print_dict_loss(dict_loss):
for key, loss in dict_loss.items():
print(f'Loss {key:12} : {loss:.6f}')
print()
print(f'Epoch - {epoch+1} / {args.epochs}')
print_dict_loss(running_loss)
if args.dataset_validation: print_dict_loss(validation_running_loss)
print()
if args.comet:
experiment.log_metrics(running_loss, step=epoch+1)
if args.dataset_validation: experiment.log_metrics(validation_running_loss, step=epoch+1)
if args.comet_log_image:
log_image_patches = model.CE(log_image_sketch)
log_image_patches = utils.patches2PIL(log_image_patches)[0]
experiment.log_image(log_image_patches, step=epoch+1)
if args.output:
model.save(args.output)
if args.comet:
experiment.end()
if __name__ == '__main__':
args = get_args_parser()
print(args)
validation_parser(args)
main(args)