Coordinates augmentation + display (json files)

Hi to all of you,

I am trying to apply transformations on images (.jpg) and their corresponding labels (hand keypoints coordinates stored in json files )… I stared by using “apply_tfms” method.
I run the code on my laptop and colab, but ended up with multiple errors messages.

here is my code :

from fastai import *
from fastai.vision import *
import json    


def get_pnt_ex():
    img = open_image(root_dir +'pic_1.jpg')
    pnts = json.load(open(root_dir +'pic_1.json'))
    pnts = array(pnts['hand_coords'])[:,-2::-1].copy()  **# the array transformation is performed  to make 'y' first as stated in the documentation**
    pnts = torch.tensor(pnts, dtype= torch.double)     **# without this line i get an error message : output with backend CPU and dtype Float doesn't match the desired backend CPU and dtype Double**
    return img, ImagePoints(FlowField(torch.tensor(img.size, dtype=torch.double),  pnts))   **# without the transformation i get the same error as above. In the documentation, FlowField accepts img.size without tensor conversion.**

tfms = get_transforms()
_, axs = plt.subplots(2,4,figsize=(12,6))
for ax in axs.flatten():
    img,pnts = get_pnt_ex()
    img = img.apply_tfms(tfms[0], size=224)
    pnts = pnts.apply_tfms(tfms[0], do_resolve=False, size=224)
    img.show(ax=ax, y=pnts)

with the following error message:

TypeError Traceback (most recent call last)

<ipython-input-101-1d28cbbf8c0e> in <module>() 4 img,pnts = get_pnt_ex() 5 img = img.apply_tfms(tfms[0], size=224) ----> 6 pnts = pnts.apply_tfms(tfms[0], do_resolve=False, size=224) 7 img.show(ax=ax, y=pnts)

1 frames

/usr/local/lib/python3.6/dist-packages/fastai/vision/image.py in apply_tfms(self, tfms, do_resolve, xtra, size, resize_method, mult, padding_mode, mode, remove_out) 111 crop_target = _get_crop_target(size, mult=mult) 112 if resize_method in (ResizeMethod.CROP,ResizeMethod.PAD): --> 113 target = _get_resize_target(x, crop_target, do_crop=(resize_method==ResizeMethod.CROP)) 114 x.resize(target) 115 elif resize_method==ResizeMethod.SQUISH: x.resize((x.shape[0],) + crop_target)

/usr/local/lib/python3.6/dist-packages/fastai/vision/image.py in _get_resize_target(img, crop_target, do_crop) 598 target_r,target_c = crop_target 599 ratio = (min if do_crop else max)(r/target_r, c/target_c) --> 600 return ch,int(round(r/ratio)),int(round(c/ratio)) #Sometimes those are numpy numbers and round doesn’t return an int. 601 602 def plot_flat(r, c, figsize):

TypeError: type Tensor doesn’t define round method

SEARCH STACK OVERFLOW

Could you please explain what is wrong with my code?

Thank you in advance

Rafik

resolved!!!
i loaded pnts as numpy array when i should have done it as List!

i implemented it this way :
coords = [ ]
for k in pnts[‘hand_coords’]:
coords.append([k[1],k[0]])
pnts = coords
pnts = torch.tensor(pnts)