def inference(val_func, inputs, data_dict): image = data_dict['data'] ori_shape = image.shape if config.eval_resize == False: resized_img, scale = image, 1 else: resized_img, scale = dataset.resize_img_by_short_and_max_size( image, config.eval_image_short_size, config.eval_image_max_size) height, width = resized_img.shape[0:2] resized_img = resized_img.astype(np.float32) - config.image_mean resized_img = np.ascontiguousarray(resized_img[:, :, [2, 1, 0]]) im_info = np.array([[height, width, scale, ori_shape[0], ori_shape[1], 0]], dtype=np.float32) feed_dict = {inputs[0]: resized_img[None, :, :, :], inputs[1]: im_info} print('fd:\n', feed_dict) #st = time.time() _, scores, pred_boxes, rois = val_func(feed_dict=feed_dict) #ed = time.time() #print(ed -st) boxes = rois[:, 1:5] / scale if cfg.TEST.BBOX_REG: pred_boxes = bbox_transform_inv(boxes, pred_boxes) pred_boxes = clip_boxes(pred_boxes, ori_shape) pred_boxes = pred_boxes.reshape(-1, config.num_classes, 4) result_boxes = [] for j in range(1, config.num_classes): inds = np.where(scores[:, j] > config.test_cls_threshold)[0] cls_scores = scores[inds, j] cls_bboxes = pred_boxes[inds, j, :] cls_dets = np.hstack( (cls_bboxes, cls_scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(cls_dets, config.test_nms) cls_dets = np.array(cls_dets[keep, :], dtype=np.float, copy=False) for i in range(cls_dets.shape[0]): db = cls_dets[i, :] dbox = DetBox(db[0], db[1], db[2] - db[0], db[3] - db[1], tag=config.class_names[j], score=db[-1]) result_boxes.append(dbox) if len(result_boxes) > config.test_max_boxes_per_image: result_boxes = sorted( result_boxes, reverse=True, key=lambda t_res: t_res.score) \ [:config.test_max_boxes_per_image] result_dict = data_dict.copy() result_dict['result_boxes'] = result_boxes return result_dict
def inference(val_func, inputs, data_dict): image = data_dict['data'] ori_shape = image.shape if config.eval_resize == False: resized_img, scale = image, 1 else: resized_img, scale = dataset.resize_img_by_short_and_max_size( image, config.eval_image_short_size, config.eval_image_max_size) height, width = resized_img.shape[0:2] resized_img = resized_img.astype(np.float32) - config.image_mean resized_img = np.ascontiguousarray(resized_img[:, :, [2, 1, 0]]) im_info = np.array( [[height, width, scale, ori_shape[0], ori_shape[1], 0]], dtype=np.float32) feed_dict = {inputs[0]: resized_img[None, :, :, :], inputs[1]: im_info} #st = time.time() _, scores, pred_boxes, rois = val_func(feed_dict=feed_dict) #ed = time.time() #print(ed -st) boxes = rois[:, 1:5] / scale if cfg.TEST.BBOX_REG: pred_boxes = bbox_transform_inv(boxes, pred_boxes) pred_boxes = clip_boxes(pred_boxes, ori_shape) pred_boxes = pred_boxes.reshape(-1, config.num_classes, 4) result_boxes = [] for j in range(1, config.num_classes): inds = np.where(scores[:, j] > config.test_cls_threshold)[0] cls_scores = scores[inds, j] cls_bboxes = pred_boxes[inds, j, :] cls_dets = np.hstack((cls_bboxes, cls_scores[:, np.newaxis])).astype( np.float32, copy=False) keep = nms(cls_dets, config.test_nms) cls_dets = np.array(cls_dets[keep, :], dtype=np.float, copy=False) for i in range(cls_dets.shape[0]): db = cls_dets[i, :] dbox = DetBox( db[0], db[1], db[2] - db[0], db[3] - db[1], tag=config.class_names[j], score=db[-1]) result_boxes.append(dbox) if len(result_boxes) > config.test_max_boxes_per_image: result_boxes = sorted( result_boxes, reverse=True, key=lambda t_res: t_res.score) \ [:config.test_max_boxes_per_image] result_dict = data_dict.copy() result_dict['result_boxes'] = result_boxes return result_dict
def get_data(path): image = cv2.imread(path, cv2.IMREAD_COLOR) if config.eval_resize == False: resized_img, scale = image, 1 else: resized_img, scale = dataset.resize_img_by_short_and_max_size( image, config.eval_image_short_size, config.eval_image_max_size) original_height, original_width = image.shape[0:2] height, width = resized_img.shape[0:2] transposed_img = np.ascontiguousarray(resized_img.transpose( 2, 0, 1)[None, :, :, :], dtype=np.float32) im_info = np.array([height, width, scale, original_height, original_width], dtype=np.float32)[None, :] return image, transposed_img, im_info
def get_data(record, device): data = dataset.val_dataset(record) image, gt_boxes, ID = \ data['data'], data['boxes'], data['ID'] if config.eval_resize == False: resized_img, scale = image, 1 else: resized_img, scale = dataset.resize_img_by_short_and_max_size( image, config.eval_image_short_size, config.eval_image_max_size) original_height, original_width = image.shape[0:2] height, width = resized_img.shape[0:2] transposed_img = np.ascontiguousarray(resized_img.transpose( 2, 0, 1)[None, :, :, :], dtype=np.float32) im_info = np.array([height, width, scale, original_height, original_width], dtype=np.float32)[None, :] return transposed_img, gt_boxes, im_info, ID
def get_image_info(image, max_size=-1, short_size=-1): if config.eval_resize == False: resized_img, scale = image, 1 else: max_size = config.eval_image_max_size if max_size < 1 else max_size short_size = config.eval_image_short_size if short_size < 1 else short_size resized_img, scale = dataset.resize_img_by_short_and_max_size( image, short_size, max_size) original_height, original_width = image.shape[0:2] height, width = resized_img.shape[0:2] transposed_img = np.ascontiguousarray(resized_img.transpose( 2, 0, 1)[None, :, :, :], dtype=np.float32) im_info = np.array([height, width, scale, original_height, original_width], dtype=np.float32) image = torch.Tensor(transposed_img).cuda(0) im_info = torch.Tensor(im_info[None, :]).cuda(0) return image, im_info