I just wanted to share my solution when running into padding mode problem using SegmentationItemList class.
Here is my piece of code for databunch init:
codes = np.array(['item', 'background'])
data = (SegmentationItemList.from_folder(path_img)
.split_none()
.label_from_func(get_mask_filename, classes=codes)
.transform([tfms, None], tfm_y=True, size=500, resize_method=2, padding_mode='border')`
.databunch(bs=4)
)
where my transformations are defined as
max_rotate = 180
max_warp = 0.2
max_zoom = 1.1
max_lighting = 0.5
p_affine = 0.75
p_lighting = 0.5
tfms = [pad(padding=500, mode='border'),
crop(size=500),
rotate(degrees=(-max_rotate, max_rotate), p=p_affine),
symmetric_warp(magnitude=(-max_warp,max_warp), p=p_affine, invert=False),
rand_zoom(scale=(1.,max_zoom), p=p_affine),
brightness(change=(0.5*(1-max_lighting), 0.5*(1+max_lighting)), p=p_lighting, use_on_y=False),
contrast(scale=(1-max_lighting, 1/(1-max_lighting)), p=p_lighting, use_on_y=False)]
Despite having set padding mode to border
in pad and .transform I kept getting reflection
padding which lead to incorrect scraps of items scattered around corners after transformations.
The solution to this behavior was modifying the order of transformations for rand_zoom and symmetric_warp from 4 and 5 to 11.
tfms[3].tfm.order = 11
tfms[4].tfm.order = 11
I didn’t find any information in the docs about the effect of transformation order on padding.
#Edit
After digging into it I found that this behaviour might be coming from here
def _image_maybe_add_crop_pad(img, tfms):
tfm_names = [tfm.__name__ for tfm in tfms]
return [crop_pad()] + tfms if 'crop_pad' not in tfm_names else tfms
Image._maybe_add_crop_pad = _image_maybe_add_crop_pad
When the user can’t specify padding_mode in crop_pad() in this part
if resize_method <= 2 and size is not None: tfms = self._maybe_add_crop_pad(tfms)
Thank you for your great work on fastai library
Best regards