img = PILImage(PILImage.create(TEST_IMAGE).resize((600,400)))
As for all Transform
you can pass encodes
and decodes
at init or subclass and implement them. You can do the same for the before_call
method that is called at each __call__
. Note that to have a consistent state for inputs and targets, a RandTransform
must be applied at the tuple level.
By default the before_call behavior is to execute the transform with probability p
(if subclassing and wanting to tweak that behavior, the attribute self.do
, if it exists, is looked for to decide if the transform is executed or not).
RandTransform
is only applied to the training set by default, so you have to pass split_idx=0
if you are calling it directly and not through a Datasets
. That behavior can be changed by setting the attr split_idx
of the transform to None
.def _add1(x): return x+1
dumb_tfm = RandTransform(enc=_add1, p=0.5)
start,d1,d2 = 2,False,False
for _ in range(40):
t = dumb_tfm(start, split_idx=0)
if dumb_tfm.do: test_eq(t, start+1); d1=True
else: test_eq(t, start) ; d2=True
assert d1 and d2
dumb_tfm
Calls @patch
'd flip_lr
behaviors for Image
, TensorImage
, TensorPoint
, and TensorBBox
tflip = FlipItem(p=1.)
test_eq(tflip(bbox,split_idx=0), tensor([[1.,0., 0.,1]]) -1)
Calls @patch
'd dihedral
behaviors for PILImage
, TensorImage
, TensorPoint
, and TensorBBox
By default each of the 8 dihedral transformations (including noop) have the same probability of being picked when the transform is applied. You can customize this behavior by passing your own draw
function. To force a specific flip, you can also pass an integer between 0 and 7.
_,axs = subplots(2, 4)
for ax in axs.flatten():
show_image(DihedralItem(p=1.)(img, split_idx=0), ctx=ax)
Calls @patch
'd crop_pad
behaviors for Image
, TensorImage
, TensorPoint
, and TensorBBox
_,axs = plt.subplots(1,3,figsize=(12,4))
for ax,sz in zip(axs.flatten(), [300, 500, 700]):
show_image(img.crop_pad(sz), ctx=ax, title=f'Size {sz}');
_,axs = plt.subplots(1,3,figsize=(12,4))
for ax,mode in zip(axs.flatten(), [PadMode.Zeros, PadMode.Border, PadMode.Reflection]):
show_image(img.crop_pad((600,700), pad_mode=mode), ctx=ax, title=mode);
_,axs = plt.subplots(1,3,figsize=(12,4))
f = RandomCrop(200)
for ax in axs: show_image(f(img), ctx=ax);
On the validation set, we take a center crop.
_,axs = plt.subplots(1,3,figsize=(12,4))
for ax in axs: show_image(f(img, split_idx=1), ctx=ax);