def inference(base_model_name, path_to_npz, data_format, input_files, plot): model_func = get_model(base_model_name) height, width = (368, 432) e = measure( lambda: TfPoseEstimator2(path_to_npz, model_func, target_size=(width, height), data_format=data_format), 'create TfPoseEstimator2') t0 = time.time() for idx, img_name in enumerate(input_files): image = measure( lambda: read_imgfile( img_name, width, height, data_format=data_format), 'read_imgfile') humans, heatMap, pafMap = measure(lambda: e.inference(image), 'e.inference') tl.logging.info('got %d humans from %s' % (len(humans), img_name)) if humans: for h in humans: tl.logging.debug(h) if plot: if data_format == 'channels_first': image = image.transpose([1, 2, 0]) plot_humans(image, heatMap, pafMap, humans, '%02d' % (idx + 1)) tot = time.time() - t0 mean = tot / len(input_files) tl.logging.info('inference all took: %f, mean: %f, FPS: %f' % (tot, mean, 1.0 / mean))
def inference(base_model_name, path_to_npz, data_format, input_files, plot): def model_func(n_pos, target_size): full_model = get_full_model_func(base_model_name) return full_model(n_pos, target_size, data_format=data_format) height, width = (368, 432) e = measure( lambda: TfPoseEstimator2( path_to_npz, model_func, target_size=(width, height)), 'create TfPoseEstimator2') t0 = time.time() for idx, img_name in enumerate(input_files): image = measure( lambda: read_imgfile( img_name, width, height, data_format=data_format), 'read_imgfile') humans = measure(lambda: e.inference(image, resize_out_ratio=8.0), 'e.inference') tl.logging.info('got %d humans from %s' % (len(humans), img_name)) if humans: for h in humans: tl.logging.debug(h) if plot: plot_humans(e, image, humans, '%02d' % (idx + 1)) tot = time.time() - t0 mean = tot / len(input_files) tl.logging.info('inference all took: %f, mean: %f, FPS: %f' % (tot, mean, 1.0 / mean))
def main(): args = parse_args() height, width, channel = 368, 432, 3 images = [] for name in args.images.split(','): x = read_imgfile( name, width, height, 'channels_first') # channels_first is required for tensorRT images.append(x) model_func = _get_model_func(args.base_model) model_inputs, model_outputs = model_func() input_names = [p.name[:-2] for p in model_inputs] output_names = [p.name[:-2] for p in model_outputs] print('input names: %s' % ','.join(input_names)) print('output names: %s' % ','.join(output_names)) # outputs/conf,outputs/paf # with tf.Session() as sess: sess = tf.InteractiveSession() measure(lambda: tl.files.load_and_assign_npz_dict(args.path_to_npz, sess), 'load npz') frozen_graph = tf.graph_util.convert_variables_to_constants( sess, sess.graph_def, output_names) tf_model = tf.graph_util.remove_training_nodes(frozen_graph) uff_model = measure(lambda: uff.from_tensorflow(tf_model, output_names), 'uff.from_tensorflow') print('uff model created') parser = uffparser.create_uff_parser() inputOrder = 0 # NCHW, https://docs.nvidia.com/deeplearning/sdk/tensorrt-api/c_api/_nv_uff_parser_8h_source.html parser.register_input(input_names[0], (channel, height, width), inputOrder) for name in output_names: parser.register_output(name) G_LOGGER = trt.infer.ConsoleLogger(trt.infer.LogSeverity.INFO) max_batch_size = 1 max_workspace_size = 1 << 30 engine = measure( lambda: trt.utils.uff_to_trt_engine( G_LOGGER, uff_model, parser, max_batch_size, max_workspace_size), 'trt.utils.uff_to_trt_engine') print('engine created') f_height, f_width = (height / 8, width / 8 ) # TODO: derive from model_outputs post_process = PostProcessor((height, width), (f_height, f_width), 'channels_first') for idx, x in enumerate(images): conf, paf = measure(lambda: infer(engine, x, 1), 'infer') humans, heat_up, paf_up = measure(lambda: post_process(conf, paf), 'post_process') print('got %d humans' % (len(humans))) plot_humans(x.transpose([1, 2, 0]), heat_up, paf_up, humans, '%02d' % (idx + 1))
def inference(path_to_freezed_model, input_files): h, w = 368, 432 e = measure( lambda: TfPoseEstimator2Loader(path_to_freezed_model, target_size=(w, h)), 'create TfPoseEstimator2Loader') for idx, img_name in enumerate(input_files): image = read_imgfile(img_name, w, h) humans, heatMap, pafMap = measure(lambda: e.inference(image), 'inference') print('got %d humans from %s' % (len(humans), img_name)) if humans: for h in humans: print(h) plot_humans(image, heatMap, pafMap, humans, '%02d' % (idx + 1))
def inference(path_to_freezed_model, input_files): e = measure( lambda: TfPoseEstimator2Loader(path_to_freezed_model, target_size=(432, 368)), 'create TfPoseEstimator2Loader') for idx, img_name in enumerate(input_files): image = read_imgfile(img_name, None, None) humans = measure(lambda: e.inference(image, resize_out_ratio=8.0), 'inference') print('got %d humans from %s' % (len(humans), img_name)) if humans: for h in humans: print(h) plot_humans(e, image, humans, '%02d' % (idx + 1))
type=str, default='cmu', help='cmu / mobilenet_thin') parser.add_argument('--cocoyear', type=str, default='2017') parser.add_argument('--coco-dir', type=str, default='') parser.add_argument('--data-idx', type=int, default=-5) parser.add_argument('--multi-scale', type=bool, default=True) parser.add_argument('--net_type', type=str, default='full_normal') parser.add_argument('--base_dir', type=str, default='./data/media') parser.add_argument('--path-to-npz', type=str, required=True) return parser.parse_args() if __name__ == '__main__': args = parse_args() w, h = model_wh(args.resize) if args.net_type == 'full_normal': e = TfPoseEstimator2(args.path_to_npz, full_model, target_size=(w, h)) imglist = os.listdir(args.base_dir) for idx, image_name in enumerate(imglist): img_name = os.path.join(args.base_dir, image_name) image = read_imgfile(img_name, None, None) # inference the image with the specified network humans = e.inference(image, resize_to_default=(w > 0 and h > 0), resize_out_ratio=args.resize_out_ratio) plot_humans(e, image, humans, '%02d' % (idx + 1))