-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhourglass_single.py
149 lines (116 loc) · 4.22 KB
/
hourglass_single.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
from __future__ import print_function, absolute_import
import sys
# sys.path.insert(0, 'home/shantam/alexDarknet/darknet/hourglass/')
# sys.path.insert(0, 'home/shantam/alexDarknet/darknet/hourglass/pose')
sys.path.insert(0, "/home/rbccps2080ti/projects/VisTraSAS/pytorch-pose")
import os
# import argparse
# import time
# import matplotlib.pyplot as plt
import torch
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
# import torchvision.datasets as datasets
# from hourglass.pose.utils.logger import Logger, savefig
from pose.utils.evaluation import final_preds
# from hourglass.pose.utils.misc import save_checkpoint, save_pred, adjust_learning_rate
# from hourglass.pose.utils.osutils import mkdir_p, isfile, isdir, join
# from hourglass.pose.utils.imutils import batch_with_heatmap, im_to_numpy
# from hourglass.pose.utils.transforms import fliplr, flip_back
import pose.models as models
# import hourglass.pose.datasets as datasets
import pose.losses as losses
from pose.utils.transforms import *
import cv2, numpy as np
def highest(scores, values):
maxval = np.amax(values)
maxindex = np.argmax(values)
values = np.delete(values, maxindex)
return values, maxindex
class Hourglass():
def __init__(self):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
cudnn.benchmark = True
self.img_path=''
self.dataset = 'mpii'
self.image_path = ''
self.inp_res = 256
self.out_res = 64
self.arch = 'hg'
self.stacks = 2
self.blocks = 1
self.features = 256
self.resnet_layers = 50
self.solver = 'rms'
self.workers = 1
self.epochs = 100
self.test_batch=1
self.train_batch=1
self.lr = 2.5e-4
self.momentum=0
self.weight_decay=0
self.gamma=0.1
self.sigma=1.0
self.scale_factor=0.25
self.rot_factor=1
self.sigma_decay=0
#self.checkpoint=''
self.resume=''
self.njoints=24
self.model = models.hg(num_stacks=self.stacks, num_blocks=self.blocks, num_classes=self.njoints, resnet_layers=self.resnet_layers)
self.model = torch.nn.DataParallel(self.model).to(self.device)
self.criterion = losses.JointsMSELoss().to(self.device)
self.optimizer = torch.optim.RMSprop(self.model.parameters(), lr=self.lr, momentum=self.momentum, weight_decay=self.weight_decay)
# TODO changing hardcoded model path to parser
self.checkpoint = torch.load("/home/rbccps2080ti/projects/link_speed_estimation/ta_darknet/hourglass/checkpoint/mpii/hg_updated_21/checkpoint.pth.tar")
self.start_epoch = self.checkpoint['epoch']
self.model.load_state_dict(self.checkpoint['state_dict'])
self.model.eval()
def forward_pass(self, img):
#img=img.unsqueeze(0)
#img=[img]
#img=np.reshape(img, (1, img.shape[0], img.shape[1], img.shape[2]))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
points = []
pointers = []
#image = im_to_numpy(img)
c = [img.shape[1]/2, img.shape[2]/2]
s = float(img.shape[1]/200.0)
#print ("cropped", c, s)
img = crop(self.img_path, img, c, s, [self.inp_res, self.inp_res])
#image = im_to_numpy(img)
# while True:
# #print (type(image))
# image=img.cpu().numpy()
# image=np.moveaxis(image, 0, -1)
# image=cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# cv2.imshow('scaled', image)
# cv2.waitKey(10)
img=np.reshape(img, (1, img.shape[0], img.shape[1], img.shape[2]))
img = img.to(device, non_blocking = True)
output=self.model(img)
score_map = output[-1].cpu() if type(output) == list else output.cpu()
preds, vals = final_preds(score_map, [c], [s], [64, 64])
coords = np.squeeze(preds)
for m in range(0,len(coords)):
val = vals[0][m].detach().numpy()
print ("val",val)
if val>0.4: #threshold for confidence score
x,y = coords[m][0].cpu().detach().numpy(), coords[m][1].cpu().detach().numpy()
if ([x,y] != present for present in pointers):
#print ("coming in here")
pointers.append([x,y,m])
else:
for present in pointers:
if [present[0], present[1]]==[x,y]:
if val>present[2]:
pointers.remove(present)
pointers.append([x,y,m])
finalpoints=[]
finalpointers=[]
for j in pointers:
x,y,m = j[0], j[1], j[2]
finalpointers.append([j[0], j[1]])
finalpoints.append(j[2])
return finalpoints, finalpointers