Problem with implementing crop using xtra_tfms

I want to crop the images as a preprocessing step and then resize them to 224 224. The original images are 120 120 and the crop size should be 70*70. This is my code:

il = ImageList.from_folder(path).split_by_rand_pct(valid_pct = 0.2, seed = 42).label_from_folder().transform(get_transforms(do_flip=True, flip_vert=True, max_rotate=10, max_zoom=1.1, max_lighting=0, max_warp=0, p_affine=1.0, p_lighting=0.0, xtra_tfms=[RandTransform(tfm=TfmCrop (crop_pad), kwargs={‘size’: 70, ‘row_pct’: 0.5, ‘col_pct’: 0.5, ‘padding_mode’: ‘reflection’}, p=1.0, resolved={}, do_run=True, is_random=True, use_on_y=True)]),size=224)

data = il.databunch().normalize(imagenet_stats)

Unfortunately the code above does not crop images for me, because when I check them via data.show_batch() they are in original form rather than central cropped. Any recommendations?

1 Like

I know this is an old question, but I had the same need and your question helped me discover my own answer, so I’m posting my solution in case someone else is interested. I am training a model to detect the number of horizontal shots in 800x600 images. So I want a black boarder around the image, then I want to reduce the size down to 300x300 leaving the border intact, without making any other transforms to the image, because I’m not interested in the content of the image at this point, just the format. This worked for me;

tfms=get_transforms(do_flip=False, flip_vert=False, max_zoom=0., max_rotate=0., max_lighting=0.,  
max_warp=0., p_affine=0., p_lighting=0., 
xtra_tfms=[RandTransform(tfm=TfmCrop (crop_pad), kwargs={'size':850, 'row_pct': 0., 'col_pct': 0., 'padding_mode': 'zeros'}, \
p=1, resolved={}, do_run=True, is_random=False, use_on_y=True)])

data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.2,
    ds_tfms=tfms, size=64, resize_method=ResizeMethod.PAD,
    num_workers=0, bs=300, padding_mode='zeros').normalize(imagenet_stats)