-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathinference.py
330 lines (300 loc) · 14.2 KB
/
inference.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""Script for single-gpu/multi-gpu demo."""
import argparse
import os
import platform
import sys
import time
import numpy as np
import torch
from tqdm import tqdm
import natsort
from detector.apis import get_detector
from trackers.tracker_api import Tracker
from trackers.tracker_cfg import cfg as tcfg
from trackers import track
from alphapose.models import builder
from alphapose.utils.config import update_config
from alphapose.utils.detector import DetectionLoader
from alphapose.utils.file_detector import FileDetectionLoader
from alphapose.utils.transforms import flip, flip_heatmap
from alphapose.utils.vis import getTime
from alphapose.utils.webcam_detector import WebCamDetectionLoader
from alphapose.utils.writer import DataWriter
"""----------------------------- Demo options -----------------------------"""
parser = argparse.ArgumentParser(description='AlphaPose Demo')
parser.add_argument('--cfg', type=str, default='./configs/coco/resnet/256x192_res50_lr1e-3_1x.yaml',
help='experiment configure file name')
parser.add_argument('--checkpoint', type=str, default='./pretrained_models/fast_res50_256x192.pth',
help='checkpoint file name')
parser.add_argument('--sp', default=False, action='store_true',
help='Use single process for pytorch')
parser.add_argument('--detector', dest='detector',
help='detector name', default="yolo")
parser.add_argument('--detfile', dest='detfile',
help='detection result file', default="")
parser.add_argument('--indir', dest='inputpath',
help='image-directory', default="./examples/demo")
parser.add_argument('--list', dest='inputlist',
help='image-list', default="")
parser.add_argument('--image', dest='inputimg',
help='image-name', default="")
parser.add_argument('--outdir', dest='outputpath',
help='output-directory', default="examples/res")
parser.add_argument('--save_img', default=False, action='store_true',
help='save result as image')
parser.add_argument('--vis', default=False, action='store_true',
help='visualize image')
parser.add_argument('--showbox', default=False, action='store_true',
help='visualize human bbox')
parser.add_argument('--profile', default=False, action='store_true',
help='add speed profiling at screen output')
parser.add_argument('--format', type=str,
help='save in the format of cmu or coco or openpose, option: coco/cmu/open')
parser.add_argument('--min_box_area', type=int, default=0,
help='min box area to filter out')
parser.add_argument('--detbatch', type=int, default=5,
help='detection batch size PER GPU')
parser.add_argument('--posebatch', type=int, default=80,
help='pose estimation maximum batch size PER GPU')
parser.add_argument('--eval', dest='eval', default=False, action='store_true',
help='save the result json as coco format, using image index(int) instead of image name(str)')
parser.add_argument('--gpus', type=str, dest='gpus', default="0",
help='choose which cuda device to use by index and input comma to use multi gpus, e.g. 0,1,2,3. (input -1 for cpu only)')
parser.add_argument('--qsize', type=int, dest='qsize', default=1024,
help='the length of result buffer, where reducing it will lower requirement of cpu memory')
parser.add_argument('--flip', default=False, action='store_true',
help='enable flip testing')
parser.add_argument('--debug', default=False, action='store_true',
help='print detail information')
"""----------------------------- Video options -----------------------------"""
parser.add_argument('--video', dest='video',
help='video-name', default="")
parser.add_argument('--webcam', dest='webcam', type=int,
help='webcam number', default=-1)
parser.add_argument('--save_video', dest='save_video',
help='whether to save rendered video', default=False, action='store_true')
parser.add_argument('--vis_fast', dest='vis_fast',
help='use fast rendering', action='store_true', default=False)
"""----------------------------- Tracking options -----------------------------"""
parser.add_argument('--pose_flow', dest='pose_flow',
help='track humans in video with PoseFlow', action='store_true', default=False)
parser.add_argument('--pose_track', dest='pose_track',
help='track humans in video with reid', action='store_true', default=False)
args = parser.parse_args()
cfg = update_config(args.cfg)
if platform.system() == 'Windows':
args.sp = True
args.gpus = [int(i) for i in args.gpus.split(',')] if torch.cuda.device_count() >= 1 else [-1]
args.device = torch.device("cuda:" + str(args.gpus[0]) if args.gpus[0] >= 0 else "cpu")
args.detbatch = args.detbatch * len(args.gpus)
args.posebatch = args.posebatch * len(args.gpus)
args.tracking = args.pose_track or args.pose_flow or args.detector == 'tracker'
if not args.sp:
torch.multiprocessing.set_start_method('forkserver', force=True)
torch.multiprocessing.set_sharing_strategy('file_system')
def check_input():
# for wecam
if args.webcam != -1:
args.detbatch = 1
return 'webcam', int(args.webcam)
# for video
if len(args.video):
if os.path.isfile(args.video):
videofile = args.video
return 'video', videofile
else:
raise IOError('Error: --video must refer to a video file, not directory.')
# for detection results
if len(args.detfile):
if os.path.isfile(args.detfile):
detfile = args.detfile
return 'detfile', detfile
else:
raise IOError('Error: --detfile must refer to a detection json file, not directory.')
# for images
if len(args.inputpath) or len(args.inputlist) or len(args.inputimg):
inputpath = args.inputpath
inputlist = args.inputlist
inputimg = args.inputimg
if len(inputlist):
im_names = open(inputlist, 'r').readlines()
elif len(inputpath) and inputpath != '/':
for root, dirs, files in os.walk(inputpath):
im_names = files
im_names = natsort.natsorted(im_names)
elif len(inputimg):
args.inputpath = os.path.split(inputimg)[0]
im_names = [os.path.split(inputimg)[1]]
return 'image', im_names
else:
raise NotImplementedError
def print_finish_info():
print('===========================> Finish Model Running.')
if (args.save_img or args.save_video) and not args.vis_fast:
print('===========================> Rendering remaining images in the queue...')
print(
'===========================> If this step takes too long, you can enable the --vis_fast flag to use fast rendering (real-time).')
def loop():
n = 0
while True:
yield n
n += 1
if __name__ == "__main__":
# mode = 'video', input_source = './videos/blCode_action1_scene1.avi'
mode, input_source = check_input()
if not os.path.exists(args.outputpath):
os.makedirs(args.outputpath)
# Load detection loader
if mode == 'webcam':
det_loader = WebCamDetectionLoader(input_source, get_detector(args), cfg, args)
det_worker = det_loader.start()
elif mode == 'detfile':
det_loader = FileDetectionLoader(input_source, cfg, args)
det_worker = det_loader.start()
else:
# 加载yolov4检测器(将视频流放到yolo中,用于检测人物的位置)
det_loader = DetectionLoader(input_source, get_detector(args), cfg, args, batchSize=args.detbatch, mode=mode,
queueSize=args.qsize)
det_worker = det_loader.start()
# Load pose model
# 加载姿态检测模型
pose_model = builder.build_sppe(cfg.MODEL, preset_cfg=cfg.DATA_PRESET)
# 打印模型
# print(pose_model)
print('Loading pose model from %s...' % (args.checkpoint,))
# 加载权重
pose_model.load_state_dict(torch.load(args.checkpoint, map_location=args.device))
# 构建数据集 alphapose/datasets/mscoco.py文件下进行数据集的读取
pose_dataset = builder.retrieve_dataset(cfg.DATASET.TRAIN)
if args.pose_track:
tracker = Tracker(tcfg, args)
if len(args.gpus) > 1:
pose_model = torch.nn.DataParallel(pose_model, device_ids=args.gpus).to(args.device)
else:
pose_model.to(args.device)
# 进行验证
pose_model.eval()
runtime_profile = {
'dt': [],
'pt': [],
'pn': []
}
# Init data writer
queueSize = 2 if mode == 'webcam' else args.qsize
# 保存检测好的视频数据
if args.save_video and mode != 'image':
from alphapose.utils.writer import DEFAULT_VIDEO_SAVE_OPT as video_save_opt
if mode == 'video':
video_save_opt['savepath'] = os.path.join(args.outputpath, 'AlphaPose_' + os.path.basename(input_source))
else:
video_save_opt['savepath'] = os.path.join(args.outputpath, 'AlphaPose_webcam' + str(input_source) + '.mp4')
video_save_opt.update(det_loader.videoinfo)
writer = DataWriter(cfg, args, save_video=True, video_save_opt=video_save_opt, queueSize=queueSize).start()
else:
writer = DataWriter(cfg, args, save_video=False, queueSize=queueSize).start()
if mode == 'webcam':
print('Starting webcam demo, press Ctrl + C to terminate...')
sys.stdout.flush()
im_names_desc = tqdm(loop())
else:
# 视频执行这边
data_len = det_loader.length
# 使用进度条进行检测进度的更新
im_names_desc = tqdm(range(data_len), dynamic_ncols=True)
batchSize = args.posebatch
if args.flip:
batchSize = int(batchSize / 2)
try:
for i in im_names_desc:
start_time = getTime()
with torch.no_grad():
# inps: torch.Size([1, 3, 256, 192])
# orig_img: (1080, 1920, 3)
# im_name: '0.jpg'
# boxes: torch.Size([1, 4])
# scores: 0.997
# cropped_boxes: torch.Size([1, 4])
(inps, orig_img, im_name, boxes, scores, ids, cropped_boxes) = det_loader.read()
if orig_img is None:
break
if boxes is None or boxes.nelement() == 0:
writer.save(None, None, None, None, None, orig_img, im_name)
continue
if args.profile:
ckpt_time, det_time = getTime(start_time)
runtime_profile['dt'].append(det_time)
# Pose Estimation
inps = inps.to(args.device)
datalen = inps.size(0)
leftover = 0
if (datalen) % batchSize:
leftover = 1
num_batches = datalen // batchSize + leftover
hm = []
# 对num_batches中的每张图像进行处理: num_batches = 1
for j in range(num_batches):
inps_j = inps[j * batchSize:min((j + 1) * batchSize, datalen)]
if args.flip:
inps_j = torch.cat((inps_j, flip(inps_j)))
# 进行推理
# print("inps_j.shape = {}".format(inps_j.shape))
# inps_j = [1, 3, 256, 192]
ta = time.time()
hm_j = pose_model(inps_j)
tb = time.time()
print('-----------------------------------')
print(' AlphaPose inference time: %f' % (tb - ta))
print('-----------------------------------')
# hm_j = [1, 17, 64, 48]
# print("hm_j.shape = {}".format(hm_j.shape))
if args.flip:
hm_j_flip = flip_heatmap(hm_j[int(len(hm_j) / 2):], pose_dataset.joint_pairs, shift=True)
hm_j = (hm_j[0:int(len(hm_j) / 2)] + hm_j_flip) / 2
hm.append(hm_j)
hm = torch.cat(hm)
if args.profile:
ckpt_time, pose_time = getTime(ckpt_time)
runtime_profile['pt'].append(pose_time)
# 默认是不进行跟踪的
if args.pose_track:
# 进行追踪
boxes, scores, ids, hm, cropped_boxes = track(tracker, args, orig_img, inps, boxes, hm,
cropped_boxes, im_name, scores)
hm = hm.cpu()
writer.save(boxes, scores, ids, hm, cropped_boxes, orig_img, im_name)
if args.profile:
ckpt_time, post_time = getTime(ckpt_time)
runtime_profile['pn'].append(post_time)
if args.profile:
# TQDM
im_names_desc.set_description(
'det time: {dt:.4f} | pose time: {pt:.4f} | post processing: {pn:.4f}'.format(
dt=np.mean(runtime_profile['dt']), pt=np.mean(runtime_profile['pt']),
pn=np.mean(runtime_profile['pn']))
)
print_finish_info()
while (writer.running()):
time.sleep(1)
print('===========================> Rendering remaining ' + str(writer.count()) + ' images in the queue...')
writer.stop()
det_loader.stop()
except Exception as e:
print(repr(e))
print('An error as above occurs when processing the images, please check it')
pass
except KeyboardInterrupt:
print_finish_info()
# Thread won't be killed when press Ctrl+C
if args.sp:
det_loader.terminate()
while (writer.running()):
time.sleep(1)
print('===========================> Rendering remaining ' + str(
writer.count()) + ' images in the queue...')
writer.stop()
else:
# subprocesses are killed, manually clear queues
det_loader.terminate()
writer.terminate()
writer.clear_queues()
det_loader.clear_queues()