-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_phpm.py
185 lines (163 loc) · 6.81 KB
/
train_phpm.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
import os
import numpy as np
import torch
import torch.nn as nn
import cv2
import torch.utils.data as data
import torch.optim as optim
import torchvision.utils
from torch.autograd import Variable
import torchgeometry as tgm
from tensorboardX import SummaryWriter
import torch.nn.functional as F
from models import networks, dataset
import argparse
device = "cuda"
from distributed import (
get_rank,
synchronize,
)
parser = argparse.ArgumentParser(description="Pose with Style trainer")
parser.add_argument("--local_rank", type=int, default=0, help="local rank for distributed training")
parser.add_argument("--batchSize", type=int, default=8)
parser.add_argument("--dataroot", type=str, default="data")
parser.add_argument("--datapairs", type=str, default="train_pairs.txt")
parser.add_argument("--phase", type=str, default="train")
parser.add_argument("--beta1", type=float, default=0.5)
opt_train = parser.parse_args()
torch.distributed.init_process_group(backend="nccl", init_method="env://")
torch.cuda.set_device(opt_train.local_rank)
synchronize()
def data_sampler(dataset, shuffle, distributed):
if distributed:
return data.distributed.DistributedSampler(dataset)
if shuffle:
return data.RandomSampler(dataset)
else:
return data.SequentialSampler(dataset)
train_dataset = dataset.BaseDataset(opt_train)
sampler = data_sampler(train_dataset, shuffle=True, distributed=True)
dataloader = torch.utils.data.DataLoader(
train_dataset,
batch_size=opt_train.batchSize,
sampler=sampler,
shuffle=False)
if get_rank() == 0:
if not os.path.isdir('checkpoint_phpm'):
os.mkdir('checkpoint_phpm')
writer = SummaryWriter('runs/phpm')
G1 = networks.PHPM_OLD(7, 4).to(device)
optimizerG = optim.Adam(G1.parameters(), lr=0.0002, betas=(opt_train.beta1, 0.999))
G1 = nn.parallel.DistributedDataParallel(
G1,
device_ids=[opt_train.local_rank],
output_device=opt_train.local_rank,
broadcast_buffers=False
)
def extractChannel(label):
up = nn.Upsample(size=(256, 192), mode='bilinear')
gauss = tgm.image.GaussianBlur((15, 15), (3, 3))
gauss.to(device)
parse_pred = gauss(up(label))
parse_pred = parse_pred.argmax(dim=1)[:, None]
parse_old = torch.zeros(parse_pred.size(0), 14, 256, 192, dtype=torch.float).to(device)
parse_old.scatter_(1, parse_pred, 1.0)
labels = {
0: ['background', [0]],
1: ['cloth', [4]],
2: ['arm1', [11]],
3: ['arm2', [13]]
}
parse = torch.zeros(parse_pred.size(0), 4, 256, 192, dtype=torch.float).to(device)
for j in range(len(labels)):
for label in labels[j][1]:
parse[:, j] += parse_old[:, label]
return parse
def generate_discrete_label(inputs, label_nc, onehot=True):
pred_batch = []
size = inputs.size()
for input in inputs:
input = input.view(1, label_nc, size[2], size[3])
pred = np.squeeze(input.data.max(1)[1].cpu().numpy(), axis=0)
pred_batch.append(pred)
pred_batch = np.array(pred_batch)
pred_batch = torch.from_numpy(pred_batch)
label_map = []
for p in pred_batch:
p = p.view(1, 256, 192)
label_map.append(p)
label_map = torch.stack(label_map, 0)
if not onehot:
return label_map.float().cuda()
size = label_map.size()
oneHot_size = (size[0], label_nc, size[2], size[3])
input_label = torch.FloatTensor(torch.Size(oneHot_size)).zero_().to(device)
input_label = input_label.scatter_(1, label_map.data.long().to(device), 1.0)
return input_label
def gen_noise(shape):
noise = np.zeros(shape, dtype=np.uint8)
noise = cv2.randn(noise, 0, 255)
noise = np.asarray(noise / 255, dtype=np.uint8)
noise = torch.tensor(noise, dtype=torch.float32)
return noise.to(device)
def cross_entropy2d(input, target, weight=None, size_average=True):
n, c, h, w = input.size()
nt, _,ht, wt = target.size()
# Handle inconsistent size between input and target
if h != ht or w != wt:
input = F.interpolate(input, size=(ht, wt), mode="bilinear", align_corners=True)
input = input.transpose(1, 2).transpose(2, 3).contiguous().view(-1, c)
target = target.view(-1)
target = target.type(torch.int64)
loss = F.cross_entropy(input, target)
return loss
sigmoid = nn.Sigmoid()
step = 0
for epoch in range(20):
for data in dataloader: #training
mask_clothes = (data['label'] == 4).float().to(device)
mask_hair = (data['label'] == 1).float().to(device)
mask_bottom = (data['label'] == 8).float().to(device)
mask_head = (data['label'] == 12).float().to(device)
mask_fore = (data['label'] > 0).float().to(device)
in_label = Variable(data['label'].to(device))
in_edge = Variable(data['edge'].to(device))
in_mask_clothes = Variable(mask_clothes.to(device))
in_color = Variable(data['color'].to(device))
in_image = Variable(data['image'].to(device))
in_skeleton = Variable(data['skeleton'].to(device))
in_mask_fore = Variable(mask_fore.to(device))
in_blurry = Variable(data['blurry'].to(device))
pre_clothes_mask = (in_edge > 0.5).float().to(device)
img_fore = in_image * mask_fore
in_img_fore = Variable(img_fore.to(device))
shape = pre_clothes_mask.shape
clothes = in_color*pre_clothes_mask
shape = pre_clothes_mask.shape
G1_in = torch.cat([in_blurry, clothes, in_skeleton], dim=1)
arm_label = G1(G1_in)
arm_label = sigmoid(arm_label)
size = in_label.size()
wanted_feature = (in_label * (1 - mask_hair) * (1 - mask_bottom) *
(1 - mask_head))
# Despite removing some labels, cross-entropy will see this as having 14 channels
oneHot_size = (size[0], 14, size[2], size[3])
input_label = torch.FloatTensor(torch.Size(oneHot_size)).zero_().to(device)
ground_truth_14 = input_label.scatter_(1, wanted_feature.data.long().to(device), 1.0)
ground_truth_4 = extractChannel(ground_truth_14) # changes 14 channel to 4
ground_truth = generate_discrete_label(ground_truth_4.detach(), 4, False)
CE_loss = cross_entropy2d(arm_label, ground_truth) * 10
armlabel_map = generate_discrete_label(arm_label.detach(), 4, False)
loss_G = CE_loss
if get_rank() == 0:
if step % 60 == 0:
writer.add_scalar('loss_G', loss_G, step)
if step % 450 == 0:
writer.add_image('torso_label', torchvision.utils.make_grid(armlabel_map), step)
writer.add_image('gt', torchvision.utils.make_grid(ground_truth), step)
optimizerG.zero_grad()
loss_G.backward()
optimizerG.step()
step += 1
if get_rank() == 0:
torch.save(G1.module.state_dict(), "checkpoint_phpm/phpm_"+str(epoch)+".pth")