-
Notifications
You must be signed in to change notification settings - Fork 0
/
unipose.py
292 lines (213 loc) · 10.1 KB
/
unipose.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# -*-coding:UTF-8-*-
import argparse
import time
import torch.optim
import torch.nn as nn
import torch.backends.cudnn as cudnn
import sys
import numpy as np
import cv2
import math
sys.path.append("..")
from utils.utils import get_model_summary
from utils.utils import adjust_learning_rate as adjust_learning_rate
from utils.utils import save_checkpoint as save_checkpoint
from utils.utils import printAccuracies as printAccuracies
from utils.utils import guassian_kernel as guassian_kernel
from utils.utils import get_parameters as get_parameters
from utils import Mytransforms as Mytransforms
from utils.utils import getDataloader as getDataloader
from utils.utils import getOutImages as getOutImages
from utils.utils import AverageMeter as AverageMeter
from utils.utils import draw_paint as draw_paint
from utils import evaluate as evaluate
from utils.utils import get_kpts as get_kpts
from utils import mpii_data as mpii_data
from model.unipose import unipose
from tqdm import tqdm
import torch.nn.functional as F
from collections import OrderedDict
from torchsummary import summary
from PIL import Image
class Trainer(object):
def __init__(self, args):
self.args = args
self.train_dir = args.train_dir
self.val_dir = args.val_dir
self.model_arch = args.model_arch
self.dataset = args.dataset
self.workers = 1
self.weight_decay = 0.0005
self.momentum = 0.9
self.batch_size = 8
self.lr = 0.0001
self.gamma = 0.333
self.step_size = 13275
self.sigma = 3
self.stride = 8
cudnn.benchmark = True
if self.dataset == "LSP":
self.numClasses = 14
elif self.dataset == "MPII":
self.numClasses = 15
# self.train_loader, self.val_loader = getDataloader(self.dataset, self.train_dir, self.val_dir, self.val_dir, self.sigma, self.stride, self.workers, self.batch_size)
# self.test_loader = torch.utils.data.DataLoader(
# mpii_data.mpii (self.train_dir, self.sigma, "Val",
# Mytransforms.Compose([Mytransforms.TestResized(368),])),
# batch_size = 1, shuffle=False,
# num_workers = 1, pin_memory=True)
model = unipose(self.dataset, num_classes=self.numClasses, backbone='resnet', output_stride=16, sync_bn=True, freeze_bn=False, stride=self.stride)
self.model = model.cuda()
self.criterion = nn.MSELoss().cuda()
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=self.lr)
self.best_model = 12345678.9
self.iters = 0
if self.args.pretrained is not None:
self.model.load_state_dict(torch.load(self.args.pretrained))
self.model.eval()
# checkpoint = torch.load(self.args.pretrained)
# # print(self.model)
# # print(checkpoint.keys()); raise Exception
# p = checkpoint
# state_dict = self.model.state_dict()
# model_dict = {}
# for k,v in p.items():
# if k in state_dict and v.size() == state_dict[k].size():
# model_dict[k] = v
# state_dict.update(model_dict)
# self.model.load_state_dict(state_dict)
self.isBest = 0
self.bestPCK = 0
self.bestPCKh = 0
# Print model summary and metrics
# dump_input = torch.rand((1, 3, 368, 368))
# print(get_model_summary(self.model, dump_input))
def training(self, epoch):
train_loss = 0.0
self.model.train()
print("Epoch " + str(epoch) + ':')
tbar = tqdm(self.train_loader)
for i, (input, heatmap, centermap, img_path) in enumerate(tbar):
learning_rate = adjust_learning_rate(self.optimizer, self.iters, self.lr, policy='step',
gamma=self.gamma, step_size=self.step_size)
input_var = input.cuda()
heatmap_var = heatmap.cuda()
self.optimizer.zero_grad()
heat = self.model(input_var)
loss_heat = self.criterion(heat, heatmap_var)
loss = loss_heat
train_loss += loss_heat.item()
loss.backward()
self.optimizer.step()
tbar.set_description('Train loss: %.6f' % (train_loss / ((i + 1)*self.batch_size)))
self.iters += 1
if i == 10000:
break
def validation(self, epoch):
self.model.eval()
tbar = tqdm(self.val_loader, desc='\r')
val_loss = 0.0
AP = np.zeros(self.numClasses+1)
PCK = np.zeros(self.numClasses+1)
PCKh = np.zeros(self.numClasses+1)
count = np.zeros(self.numClasses+1)
cnt = 0
for i, (input, heatmap, centermap, img_path) in enumerate(tbar):
cnt += 1
input_var = input.cuda()
heatmap_var = heatmap.cuda()
self.optimizer.zero_grad()
heat = self.model(input_var)
loss_heat = self.criterion(heat, heatmap_var)
loss = loss_heat
val_loss += loss_heat.item()
tbar.set_description('Val loss: %.6f' % (val_loss / ((i + 1)*self.batch_size)))
acc, acc_PCK, acc_PCKh, cnt, pred, visible = evaluate.accuracy(heat.detach().cpu().numpy(), heatmap_var.detach().cpu().numpy(),0.2,0.5, self.dataset)
AP[0] = (AP[0] *i + acc[0]) / (i + 1)
PCK[0] = (PCK[0] *i + acc_PCK[0]) / (i + 1)
PCKh[0] = (PCKh[0]*i + acc_PCKh[0]) / (i + 1)
for j in range(1,self.numClasses+1):
if visible[j] == 1:
AP[j] = (AP[j] *count[j] + acc[j]) / (count[j] + 1)
PCK[j] = (PCK[j] *count[j] + acc_PCK[j]) / (count[j] + 1)
PCKh[j] = (PCKh[j]*count[j] + acc_PCKh[j]) / (count[j] + 1)
count[j] += 1
mAP = AP[1:].sum()/(self.numClasses)
mPCK = PCK[1:].sum()/(self.numClasses)
mPCKh = PCKh[1:].sum()/(self.numClasses)
printAccuracies(mAP, AP, mPCKh, PCKh, mPCK, PCK, self.dataset)
PCKhAvg = PCKh.sum()/(self.numClasses+1)
PCKAvg = PCK.sum()/(self.numClasses+1)
if mAP > self.isBest:
self.isBest = mAP
save_checkpoint({'state_dict': self.model.state_dict()}, self.isBest, self.args.model_name)
print("Model saved to "+self.args.model_name)
if mPCKh > self.bestPCKh:
self.bestPCKh = mPCKh
if mPCK > self.bestPCK:
self.bestPCK = mPCK
print("Best AP = %.2f%%; PCK = %2.2f%%; PCKh = %2.2f%%" % (self.isBest*100, self.bestPCK*100,self.bestPCKh*100))
def inference(self, img_path):
self.model.eval()
print("Inference")
for idx in range(1):
center = [184, 184]
img = np.array(cv2.resize(cv2.imread(img_path),(368,368)), dtype=np.float32)
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img)
mean = [128.0, 128.0, 128.0]
std = [256.0, 256.0, 256.0]
for t, m, s in zip(img, mean, std):
t.sub_(m).div_(s)
img = torch.unsqueeze(img, 0)
self.model.eval()
input_var = img.cuda()
heat = self.model(input_var)
# np.save("unipose_input.npy", input_var.cpu().numpy())
# print(input_var.shape, heat ); raise Exception
'''
# Export here
torch.onnx.export(self.model, input_var, "unipose_argmax.onnx", verbose=True, opset_version=11)
'''
# raise Exception
# heat = F.interpolate(heat, size=input_var.size()[2:], mode='bilinear', align_corners=True)
# kpts = get_kpts(heat, img_h=368.0, img_w=368.0)
kpts = heat.cpu().numpy()
draw_paint(img_path, kpts, idx, 0, self.model_arch, self.dataset)
# heat = heat.detach().cpu().numpy()
# heat = heat[0].transpose(1,2,0)
# for i in range(heat.shape[0]):
# for j in range(heat.shape[1]):
# for k in range(heat.shape[2]):
# if heat[i,j,k] < 0:
# heat[i,j,k] = 0
# im = cv2.resize(cv2.imread(img_path),(368,368))
# heatmap = []
# for i in range(self.numClasses+1):
# heatmap = cv2.applyColorMap(np.uint8(255*heat[:,:,i]), cv2.COLORMAP_JET)
# im_heat = cv2.addWeighted(im, 0.6, heatmap, 0.4, 0)
# cv2.imwrite('samples/heat/unipose'+str(i)+'.png', im_heat)
parser = argparse.ArgumentParser()
parser.add_argument('--pretrained', default="pretrained/UniPose_MPII.pth", type=str, dest='pretrained')
parser.add_argument('--dataset', type=str, dest='dataset', default='MPII')
parser.add_argument('--train_dir', default='/PATH/TO/TRAIN',type=str, dest='train_dir')
parser.add_argument('--val_dir', type=str, dest='val_dir', default='/PATH/TO/LSP/VAL')
parser.add_argument('--model_name', default=None, type=str)
parser.add_argument('--model_arch', default='unipose', type=str)
parser.add_argument('--img_path', default='data/002465606.jpg', type=str)
starter_epoch = 0
epochs = 100
args = parser.parse_args()
# if args.dataset == 'LSP':
# args.train_dir = '/PATH/TO/LSP/TRAIN'
# args.val_dir = '/PATH/TO/LSP/VAL'
# args.pretrained = '/PATH/TO/WEIGHTS'
# elif args.dataset == 'MPII':
# args.train_dir = '/PATH/TO/MPIII/TRAIN'
# args.val_dir = '/PATH/TO/MPIII/VAL'
trainer = Trainer(args)
# for epoch in range(starter_epoch, epochs):
# trainer.training(epoch)
# trainer.validation(epoch)
# Uncomment for inference, demo, and samples for the trained model:
trainer.inference(args.img_path)