-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtrain_texture.py
115 lines (101 loc) · 4.37 KB
/
train_texture.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
import argparse
import numpy as np
import os
import tensorboardX
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam
from torch.utils.data import DataLoader
import config
from dataset.uv_dataset import UVDataset
from model.texture import Texture
parser = argparse.ArgumentParser()
parser.add_argument('--texturew', type=int, default=config.TEXTURE_W)
parser.add_argument('--textureh', type=int, default=config.TEXTURE_H)
parser.add_argument('--texture_dim', type=int, default=config.TEXTURE_DIM)
parser.add_argument('--use_pyramid', type=bool, default=config.USE_PYRAMID)
parser.add_argument('--data', type=str, default=config.DATA_DIR, help='directory to data')
parser.add_argument('--checkpoint', type=str, default=config.CHECKPOINT_DIR, help='directory to save checkpoint')
parser.add_argument('--logdir', type=str, default=config.LOG_DIR, help='directory to save checkpoint')
parser.add_argument('--train', default=config.TRAIN_SET)
parser.add_argument('--epoch', type=int, default=config.EPOCH)
parser.add_argument('--cropw', type=int, default=config.CROP_W)
parser.add_argument('--croph', type=int, default=config.CROP_H)
parser.add_argument('--batch', type=int, default=config.BATCH_SIZE)
parser.add_argument('--lr', type=float, default=config.LEARNING_RATE)
parser.add_argument('--betas', type=str, default=config.BETAS)
parser.add_argument('--l2', type=str, default=config.L2_WEIGHT_DECAY)
parser.add_argument('--eps', type=float, default=config.EPS)
parser.add_argument('--load', type=str, default=config.LOAD)
parser.add_argument('--load_step', type=int, default=config.LOAD_STEP)
parser.add_argument('--epoch_per_checkpoint', type=int, default=config.EPOCH_PER_CHECKPOINT)
args = parser.parse_args()
def adjust_learning_rate(optimizer, epoch, original_lr):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
if epoch <= 3:
lr = original_lr * 0.33 * epoch
elif epoch < 5:
lr = original_lr
elif epoch < 10:
lr = 0.1 * original_lr
else:
lr = 0.01 * original_lr
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def main():
named_tuple = time.localtime()
time_string = time.strftime("%m_%d_%Y_%H_%M", named_tuple)
log_dir = os.path.join(args.logdir, time_string)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
writer = tensorboardX.SummaryWriter(logdir=log_dir)
checkpoint_dir = args.checkpoint + time_string
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
dataset = UVDataset(args.data, args.train, args.croph, args.cropw, False)
dataloader = DataLoader(dataset, batch_size=args.batch, shuffle=True, num_workers=4)
if args.load:
print('Loading Saved Model')
model = torch.load(os.path.join(args.checkpoint, args.load))
step = args.load_step
else:
model = Texture(args.texturew, args.textureh, 3, use_pyramid=args.use_pyramid)
step = 0
l2 = args.l2.split(',')
l2 = [float(x) for x in l2]
betas = args.betas.split(',')
betas = [float(x) for x in betas]
betas = tuple(betas)
optimizer = Adam([
{'params': model.layer1, 'weight_decay': l2[0]},
{'params': model.layer2, 'weight_decay': l2[1]},
{'params': model.layer3, 'weight_decay': l2[2]},
{'params': model.layer4, 'weight_decay': l2[3]}],
lr=args.lr, betas=betas, eps=args.eps)
model = model.to('cuda')
model.train()
torch.set_grad_enabled(True)
criterion = nn.L1Loss()
print('Training started')
for i in range(1, 1+args.epoch):
print('Epoch {}'.format(i))
adjust_learning_rate(optimizer, i, args.lr)
for samples in dataloader:
images, uv_maps, masks = samples
step += images.shape[0]
optimizer.zero_grad()
preds = model(uv_maps.cuda()).cpu()
preds = torch.masked_select(preds, masks)
images = torch.masked_select(images, masks)
loss = criterion(preds, images)
loss.backward()
optimizer.step()
writer.add_scalar('train/loss', loss.item(), step)
print('loss at step {}: {}'.format(step, loss.item()))
# save checkpoint
print('Saving checkpoint')
torch.save(model, args.checkpoint+time_string+'/epoch_{}.pt'.format(i))
if __name__ == '__main__':
main()