-
Notifications
You must be signed in to change notification settings - Fork 88
/
TrainSPN.py
executable file
·141 lines (118 loc) · 4.84 KB
/
TrainSPN.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
from __future__ import print_function
import os
import argparse
from libs.SPN import SPN
from libs.Loader import Dataset
from libs.models import encoder4
from libs.models import decoder4
from libs.utils import print_options
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.utils as vutils
import torch.backends.cudnn as cudnn
import torchvision.transforms as transforms
parser = argparse.ArgumentParser()
parser.add_argument("--vgg_dir", default='models/vgg_r41.pth',
help='pre-trained encoder path')
parser.add_argument("--decoder_dir", default='models/dec_r41.pth',
help='pre-trained decoder path')
parser.add_argument("--contentPath", default="/home/xtli/DATA/MSCOCO/train2014/images/",
help='path to MSCOCO dataset')
parser.add_argument("--outf", default="trainingSPNOutput/",
help='folder to output images and model checkpoints')
parser.add_argument("--layer", default="r41",
help='layers for content')
parser.add_argument("--batchSize", type=int,default=8,
help='batch size')
parser.add_argument("--niter", type=int,default=100000,
help='iterations to train the model')
parser.add_argument('--loadSize', type=int, default=512,
help='scale image size')
parser.add_argument('--fineSize', type=int, default=256,
help='crop image size')
parser.add_argument("--lr", type=float, default=1e-3,
help='learning rate')
parser.add_argument("--log_interval", type=int, default=500,
help='log interval')
parser.add_argument("--save_interval", type=int, default=5000,
help='checkpoint save interval')
parser.add_argument("--spn_num", type=int, default=1,
help='number of spn filters')
################# PREPARATIONS #################
opt = parser.parse_args()
opt.cuda = torch.cuda.is_available()
print_options(opt)
os.makedirs(opt.outf, exist_ok = True)
cudnn.benchmark = True
################# DATA #################
content_dataset = Dataset(opt.contentPath,opt.loadSize,opt.fineSize)
content_loader_ = torch.utils.data.DataLoader(dataset=content_dataset,
batch_size = opt.batchSize,
shuffle = True,
num_workers = 4,
drop_last = True)
content_loader = iter(content_loader_)
################# MODEL #################
spn = SPN(spn=opt.spn_num)
if(opt.layer == 'r31'):
vgg = encoder3()
dec = decoder3()
elif(opt.layer == 'r41'):
vgg = encoder4()
dec = decoder4()
vgg.load_state_dict(torch.load(opt.vgg_dir))
dec.load_state_dict(torch.load(opt.decoder_dir))
for param in vgg.parameters():
param.requires_grad = False
for param in dec.parameters():
param.requires_grad = False
################# LOSS & OPTIMIZER #################
criterion = nn.MSELoss(size_average=False)
#optimizer_spn = optim.SGD(spn.parameters(), opt.lr)
optimizer_spn = optim.Adam(spn.parameters(), opt.lr)
################# GLOBAL VARIABLE #################
contentV = torch.Tensor(opt.batchSize,3,opt.fineSize,opt.fineSize)
################# GPU #################
if(opt.cuda):
vgg.cuda()
dec.cuda()
spn.cuda()
contentV = contentV.cuda()
################# TRAINING #################
def adjust_learning_rate(optimizer, iteration):
for param_group in optimizer.param_groups:
param_group['lr'] = opt.lr / (1+iteration*1e-5)
spn.train()
for iteration in range(1,opt.niter+1):
optimizer_spn.zero_grad()
try:
content,_ = content_loader.next()
except IOError:
content,_ = content_loader.next()
except StopIteration:
content_loader = iter(content_loader_)
content,_ = content_loader.next()
except:
continue
contentV.resize_(content.size()).copy_(content)
# forward
cF = vgg(contentV)
transfer = dec(cF['r41'])
propagated = spn(transfer,contentV)
loss = criterion(propagated,contentV)
# backward & optimization
loss.backward()
#nn.utils.clip_grad_norm(spn.parameters(), 1000)
optimizer_spn.step()
print('Iteration: [%d/%d] Loss: %.4f Learng Rate is %.6f'
%(opt.niter,iteration,loss,optimizer_spn.param_groups[0]['lr']))
adjust_learning_rate(optimizer_spn,iteration)
if((iteration) % opt.log_interval == 0):
transfer = transfer.clamp(0,1)
propagated = propagated.clamp(0,1)
vutils.save_image(transfer,'%s/%d_transfer.png'%(opt.outf,iteration))
vutils.save_image(propagated,'%s/%d_propagated.png'%(opt.outf,iteration))
if(iteration > 0 and (iteration) % opt.save_interval == 0):
torch.save(spn.state_dict(), '%s/%s_spn.pth' % (opt.outf,opt.layer))