deep-high-resolution-net.pytorch
deep-high-resolution-net.pytorch copied to clipboard
test a single image to get pose,but visualize not so correct,what is the error in my test code?
i refer to the test.py code and write a single image test code demo. the demo code will forward a single image and get the preds points, finally i draw the points on origin image, all points shape a right pose, but the position of all points shift some distance to error position.
the main code is as follow:
1)some function
`def xywh2cs(x, y, w, h,aspect_ratio,pixel_std=200): center = np.zeros((2), dtype=np.float32) center[0] = x + w * 0.5 center[1] = y + h * 0.5
if w > aspect_ratio * h:
h = w * 1.0 / aspect_ratio
elif w < aspect_ratio * h:
w = h * aspect_ratio
scale = np.array([w * 1.0 / pixel_std, h * 1.0 / pixel_std],dtype=np.float32)
if center[0] != -1:
scale = scale * 1.25
return center, scale
def get_input_data(image_file,cfg,center,scale,transform=None): data_numpy = cv2.imread(image_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION) data_numpy = cv2.cvtColor(data_numpy, cv2.COLOR_BGR2RGB) if data_numpy is None: logging.error('=> fail to read {}'.format(image_file)) raise ValueError('Fail to read {}'.format(image_file))
image_size=np.array(cfg.MODEL.IMAGE_SIZE)
r=0
trans = get_affine_transform(center, scale, r, image_size)
input = cv2.warpAffine(data_numpy,trans, (int(image_size[0]), int(image_size[1])),flags=cv2.INTER_LINEAR)
if transform:
input = transform(input).unsqueeze(dim=0)
return input
def draw_circles(img,preds): for pred in preds: for point in pred: print(point) cv2.circle(img,tuple(point),5,(255,0,0),2) cv2.imwrite('test.jpg',img)`
2) test pipeline
`model = eval('models.'+cfg.MODEL.NAME+'.get_pose_net')(cfg, is_train=False)
if cfg.TEST.MODEL_FILE: logging.info('=> loading model from {}'.format(cfg.TEST.MODEL_FILE)) model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE,,map_location='cpu'), strict=False) else: model_state_file = os.path.join( final_output_dir, 'final_state.pth') logging.info('=> loading model from {}'.format(model_state_file)) model.load_state_dict(torch.load(model_state_file))
model.eval()
transformer=transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
img=cv2.imread('1.jpg') h,w,c=img.shape x, y, w, h = [0,0,w,h] aspect_ratio=cfg.MODEL.IMAGE_SIZE[0]*1.0/cfg.MODEL.IMAGE_SIZE[1] center,scale=xywh2cs(x,y,w,h,aspect_ratio) input=get_input_data('1.jpg',cfg,center,scale,transformer) output=model(input) preds, maxvals = get_final_preds(cfg, output.detach().cpu().numpy(), center, scale) draw_circles(img,preds)`
hello, have you fixed the problem you met??? I also meet some problem in inference single image
The same question, I also meet same problem.
after you get the model output, to use get_max_preds() and save_batch_image_with_joints() function, you will get the correct keypoint @1093842024
I also meet this problem, do you solve it?
@mutouoa how can i use save_batch_image_with_joints() function, i know there is a function in utils.?
@mutouoa you mean the correct points are obtained by save_batch_image_with_joints(), and then we need to plot these points by ourselves?
@mutouoa you mean the correct points are obtained by save_batch_image_with_joints(), and then we need to plot these points by ourselves?
get_max_pred() function is to get the correct keypoints, and save_batch_image_with_joints() function is to save the keypoints Image.
@mutouoa i got and tried it....
@mutouoa how about if i use my own custom images,,,but with size 256 x 256 same as mpii validation?
@mutouoa how about if i use my own custom images,,,but with size 256 x 256 same as mpii validation?
I tested the code, the coordinates returned are based on input image size of 256x256, therefore in order to visualize the coordinates on the original input image, you need to scale back.
@mutouoa @cteckwee there are two points, need to be considered, (i) offocurse scale, (ii) transform of images, means the images are cropped, affine transform and and resie to 256 x 256, in this case the images input to model is not zero starting, in contrast are placed within a box of 256 x 256,,,therefore, re-scale can be done easily for transform images,,bit, with images starting from (0,0) may not work well even with scale becuase of mis-alignemt of origin.
why pixel_std is equal to 200?