from __future__ import annotations
from nbdev.showdoc import *
from fastcore.test import *
from fastcore.nb_imports import *
Transforms
Transform
and Pipeline
The classes here provide functionality for creating a composition of partially reversible functions. By “partially reversible” we mean that a transform can be decode
d, creating a form suitable for display. This is not necessarily identical to the original form (e.g. a transform that changes a byte tensor to a float tensor does not recreate a byte tensor when decoded, since that may lose precision, and a float tensor can be displayed already).
Classes are also provided and for composing transforms, and mapping them over collections. Pipeline
is a transform which composes several Transform
, knowing how to decode them or show an encoded item.
Transform
Transform (enc=None, dec=None, split_idx=None, order=None)
Delegates (__call__
,decode
,setup
) to (encodes
,decodes
,setups
) if split_idx
matches
A Transform
is the main building block of the fastai data pipelines. In the most general terms a transform can be any function you want to apply to your data, however the Transform
class provides several mechanisms that make the process of building them easy and flexible.
The main Transform
features:
- Type dispatch - Type annotations are used to determine if a transform should be applied to the given argument. It also gives an option to provide several implementations and it choses the one to run based on the type. This is useful for example when running both independent and dependent variables through the pipeline where some transforms only make sense for one and not the other. Another usecase is designing a transform that handles different data formats. Note that if a transform takes multiple arguments only the type of the first one is used for dispatch.
- Handling of tuples - When a tuple (or a subclass of tuple) of data is passed to a transform it will get applied to each element separately. You can opt out of this behavior by passing a list or an
L
, as only tuples gets this specific behavior. An alternative is to useItemTransform
defined below, which will always take the input as a whole. - Reversability - A transform can be made reversible by implementing the
decodes
method. This is mainly used to turn something like a category which is encoded as a number back into a label understandable by humans for showing purposes. Like the regular call method, thedecode
method that is used to decode will be applied over each element of a tuple separately. - Type propagation - Whenever possible a transform tries to return data of the same type it received. Mainly used to maintain semantics of things like
ArrayImage
which is a thin wrapper of pytorch’sTensor
. You can opt out of this behavior by adding->None
return type annotation. - Preprocessing - The
setup
method can be used to perform any one-time calculations to be later used by the transform, for example generating a vocabulary to encode categorical data. - Filtering based on the dataset type - By setting the
split_idx
flag you can make the transform be used only in a specificDataSource
subset like in training, but not validation. - Ordering - You can set the
order
attribute which thePipeline
uses when it needs to merge two lists of transforms. - Appending new behavior with decorators - You can easily extend an existing
Transform
by creatingencodes
ordecodes
methods for new data types. You can put those new methods outside the original transform definition and decorate them with the class you wish them patched into. This can be used by the fastai library users to add their own behavior, or multiple modules contributing to the same transform.
Defining a Transform
There are a few ways to create a transform with different ratios of simplicity to flexibility. - Extending the Transform
class - Use inheritence to implement the methods you want. - Passing methods to the constructor - Instantiate the Transform
class and pass your functions as enc
and dec
arguments. - @Transform decorator - Turn any function into a Transform
by just adding a decorator - very straightforward if all you need is a single encodes
implementation. - Passing a function to fastai APIs - Same as above, but when passing a function to other transform aware classes like Pipeline
or TfmdDS
you don’t even need a decorator. Your function will get converted to a Transform
automatically.
A simple way to create a Transform
is to pass a function to the constructor. In the below example, we pass an anonymous function that does integer division by 2:
= Transform(lambda o:o//2) f
If you call this transform, it will apply the transformation:
2), 1) test_eq_type(f(
Another way to define a Transform is to extend the Transform
class:
class A(Transform): pass
However, to enable your transform to do something, you have to define an encodes
method. Note that we can use the class name as a decorator to add this method to the original class.
@A
def encodes(self, x): return x+1
= A()
f1 1), 2) # f1(1) is the same as f1.encode(1) test_eq(f1(
In addition to adding an encodes
method, we can also add a decodes
method. This enables you to call the decode
method (without an s). For more information about the purpose of decodes
, see the discussion about Reversibility in the above section.
Just like with encodes, you can add a decodes
method to the original class by using the class name as a decorator:
class B(A): pass
@B
def decodes(self, x): return x-1
= B()
f2 2), 1)
test_eq(f2.decode(
1), 2) # uses A's encode method from the parent class test_eq(f2(
If you do not define an encodes
or decodes
method the original value will be returned:
class _Tst(Transform): pass
= _Tst() # no encodes or decodes method have been defined
f3 2.0), 2.0)
test_eq_type(f3.decode(2), 2) test_eq_type(f3(
Transforms can be created from class methods too:
class A:
@classmethod
def create(cls, x:int): return x+1
1), 2) test_eq(Transform(A.create)(
Defining Transforms With A Decorator
Transform
can be used as a decorator to turn a function into a Transform
.
@Transform
def f(x): return x//2
2), 1)
test_eq_type(f(2.0), 2.0)
test_eq_type(f.decode(
@Transform
def f(x): return x*2
2), 4)
test_eq_type(f(2.0), 2.0) test_eq_type(f.decode(
Typed Dispatch and Transforms
We can also apply different transformations depending on the type of the input passed by using TypedDispatch
. TypedDispatch
automatically works with Transform
when using type hints:
class A(Transform): pass
@A
def encodes(self, x:int): return x//2
@A
def encodes(self, x:float): return x+1
When we pass in an int
, this calls the first encodes method:
= A()
f 3), 1) test_eq_type(f(
When we pass in a float
, this calls the second encodes method:
2.), 3.) test_eq_type(f(
When we pass in a type that is not specified in encodes
, the original value is returned:
'a'), 'a') test_eq(f(
If the type annotation is a tuple, then any type in the tuple will match:
class MyClass(int): pass
class A(Transform):
def encodes(self, x:MyClass|float): return x/2
def encodes(self, x:str|list): return str(x)+'_1'
= A() f
The below two examples match the first encodes, with a type of MyClass
and float
, respectively:
2)), 1.) # input is of type MyClass
test_eq(f(MyClass(6.0), 3.0) # input is of type float test_eq(f(
The next two examples match the second encodes
method, with a type of str
and list
, respectively:
'a'), 'a_1') # input is of type str
test_eq(f('a','b','c']), "['a', 'b', 'c']_1") # input is of type list test_eq(f([
Casting Types With Transform
Without any intervention it is easy for operations to change types in Python. For example, FloatSubclass
(defined below) becomes a float
after performing multiplication:
class FloatSubclass(float): pass
3.0) * 2, 6.0) test_eq_type(FloatSubclass(
This behavior is often not desirable when performing transformations on data. Therefore, Transform
will attempt to cast the output to be of the same type as the input by default. In the below example, the output will be cast to a FloatSubclass
type to match the type of the input:
@Transform
def f(x): return x*2
3.0)), FloatSubclass(6.0)) test_eq_type(f(FloatSubclass(
We can optionally turn off casting by annotating the transform function with a return type of None
:
@Transform
def f(x)-> None: return x*2 # Same transform as above, but with a -> None annotation
3.0)), 6.0) # Casting is turned off because of -> None annotation test_eq_type(f(FloatSubclass(
However, Transform
will only cast output back to the input type when the input is a subclass of the output. In the below example, the input is of type FloatSubclass
which is not a subclass of the output which is of type str
. Therefore, the output doesn’t get cast back to FloatSubclass
and stays as type str
:
@Transform
def f(x): return str(x)
2.)), '2.0') test_eq_type(f(Float(
Just like encodes
, the decodes
method will cast outputs to match the input type in the same way. In the below example, the output of decodes
remains of type MySubclass
:
class MySubclass(int): pass
def enc(x): return MySubclass(x+1)
def dec(x): return x-1
= Transform(enc,dec)
f = f(1) # t is of type MySubclass
t 1)) # the output of decode is cast to MySubclass to match the input type. test_eq_type(f.decode(t), MySubclass(
Apply Transforms On Subsets With split_idx
You can apply transformations to subsets of data by specifying a split_idx
property. If a transform has a split_idx
then it’s only applied if the split_idx
param matches. In the below example, we set split_idx
equal to 1
:
def enc(x): return x+1
def dec(x): return x-1
= Transform(enc,dec)
f = 1 f.split_idx
The transformations are applied when a matching split_idx
parameter is passed:
1, split_idx=1),2)
test_eq(f(2, split_idx=1),1) test_eq(f.decode(
On the other hand, transformations are ignored when the split_idx
parameter does not match:
1, split_idx=0), 1)
test_eq(f(2, split_idx=0), 2) test_eq(f.decode(
Transforms on Lists
Transform operates on lists as a whole, not element-wise:
class A(Transform):
def encodes(self, x): return dict(x)
def decodes(self, x): return list(x.items())
= A()
f = [(1,2), (3,4)]
_inp = f(_inp)
t
dict(_inp))
test_eq(t, test_eq(f.decodes(t), _inp)
If you want a transform to operate on a list elementwise, you must implement this appropriately in the encodes
and decodes
methods:
class AL(Transform): pass
@AL
def encodes(self, x): return [x_+1 for x_ in x]
@AL
def decodes(self, x): return [x_-1 for x_ in x]
= AL()
f = f([1,2])
t
2,3])
test_eq(t, [1,2]) test_eq(f.decode(t), [
Transforms on Tuples
Unlike lists, Transform
operates on tuples element-wise.
def neg_int(x): return -x
= Transform(neg_int)
f
1,2,3)), (-1,-2,-3)) test_eq(f((
Transforms will also apply TypedDispatch
element-wise on tuples when an input type annotation is specified. In the below example, the values 1.0
and 3.0
are ignored because they are of type float
, not int
:
def neg_int(x:int): return -x
= Transform(neg_int)
f
1.0, 2, 3.0)), (1.0, -2, 3.0)) test_eq(f((
Another example of how Transform
can use TypedDispatch
with tuples is shown below:
class B(Transform): pass
@B
def encodes(self, x:int): return x+1
@B
def encodes(self, x:str): return x+'hello'
@B
def encodes(self, x): return str(x)+'!'
If the input is not an int
or str
, the third encodes
method will apply:
= B()
b 1]), '[1]!')
test_eq(b([1.0]), '[1.0]!') test_eq(b([
However, if the input is a tuple, then the appropriate method will apply according to the type of each element in the tuple:
'1',)), ('1hello',))
test_eq(b((1,2)), (2,3))
test_eq(b(('a',1.0)), ('ahello','1.0!')) test_eq(b((
Dispatching over tuples works recursively, by the way:
class B(Transform):
def encodes(self, x:int): return x+1
def encodes(self, x:str): return x+'_hello'
def decodes(self, x:int): return x-1
def decodes(self, x:str): return x.replace('_hello', '')
= B()
f = (1.,(2,'3'))
start = f(start)
t 1.,(3,'3_hello')))
test_eq_type(t, ( test_eq(f.decode(t), start)
Dispatching also works with typing
module type classes, like numbers.integral
:
@Transform
def f(x:numbers.Integral): return x+1
= f((1,'1',1))
t 2, '1', 2)) test_eq(t, (
InplaceTransform
InplaceTransform (enc=None, dec=None, split_idx=None, order=None)
A Transform
that modifies in-place and just returns whatever it’s passed
class A(InplaceTransform): pass
@A
def encodes(self, x:pd.Series): x.fillna(10, inplace=True)
= A()
f
1,2,None])),pd.Series([1,2,10],dtype=np.float64)) #fillna fills with floats. test_eq_type(f(pd.Series([
DisplayedTransform
DisplayedTransform (enc=None, dec=None, split_idx=None, order=None)
A transform with a __repr__
that shows its attrs
Transforms normally are represented by just their class name and a list of encodes and decodes implementations:
class A(Transform): encodes,decodes = noop,noop
= A()
f f
A:
encodes: (object,object) -> noop
decodes: (object,object) -> noop
A DisplayedTransform
will in addition show the contents of all attributes listed in the comma-delimited string self.store_attrs
:
class A(DisplayedTransform):
= noop
encodes def __init__(self, a, b=2):
super().__init__()
store_attr()
=1,b=2) A(a
A -- {'a': 1, 'b': 2}:
encodes: (object,object) -> noop
decodes:
ItemTransform
ItemTransform (enc=None, dec=None, split_idx=None, order=None)
A transform that always take tuples as items
ItemTransform
is the class to use to opt out of the default behavior of Transform
.
class AIT(ItemTransform):
def encodes(self, xy): x,y=xy; return (x+y,y)
def decodes(self, xy): x,y=xy; return (x-y,y)
= AIT()
f 1,2)), (3,2))
test_eq(f((3,2)), (1,2)) test_eq(f.decode((
If you pass a special tuple subclass, the usual retain type behavior of Transform
will keep it:
class _T(tuple): pass
= _T((1,2))
x 3,2))) test_eq_type(f(x), _T((
get_func
get_func (t, name, *args, **kwargs)
Get the t.name
(potentially partial-ized with args
and kwargs
) or noop
if not defined
This works for any kind of t
supporting getattr
, so a class or a module.
'neg', 2)(), -2)
test_eq(get_func(operator, '__call__')(2), -2)
test_eq(get_func(operator.neg, list, 'foobar')([2]), [2])
test_eq(get_func(= [2,1]
a list, 'sort')(a)
get_func(1,2]) test_eq(a, [
Transforms are built with multiple-dispatch: a given function can have several methods depending on the type of the object received. This is done directly with the TypeDispatch
module and type-annotation in Transform
, but you can also use the following class.
Func
Func (name, *args, **kwargs)
Basic wrapper around a name
with args
and kwargs
to call on a given type
You can call the Func
object on any module name or type, even a list of types. It will return the corresponding function (with a default to noop
if nothing is found) or list of functions.
'sqrt')(math), math.sqrt) test_eq(Func(
Sig
Sig (*args, **kwargs)
Sig
is just sugar-syntax to create a Func
object more easily with the syntax Sig.name(*args, **kwargs)
.
= Sig.sqrt()
f test_eq(f(math), math.sqrt)
compose_tfms
compose_tfms (x, tfms, is_enc=True, reverse=False, **kwargs)
Apply all func_nm
attribute of tfms
on x
, maybe in reverse
order
def to_int (x): return Int(x)
def to_float(x): return Float(x)
def double (x): return x*2
def half(x)->None: return x/2
def test_compose(a, b, *fs): test_eq_type(compose_tfms(a, tfms=map(Transform,fs)), b)
1, Int(1), to_int)
test_compose(1, Float(1), to_int,to_float)
test_compose(1, Float(2), to_int,to_float,double)
test_compose(2.0, 2.0, to_int,double,half) test_compose(
class A(Transform):
def encodes(self, x:float): return Float(x+1)
def decodes(self, x): return x-1
= [A(), Transform(math.sqrt)]
tfms = compose_tfms(3., tfms=tfms)
t 2.))
test_eq_type(t, Float(=tfms, is_enc=False), 1.)
test_eq(compose_tfms(t, tfms4., tfms=tfms, reverse=True), 3.) test_eq(compose_tfms(
= [A(), Transform(math.sqrt)]
tfms 9,3.), tfms=tfms), (3,2.)) test_eq(compose_tfms((
mk_transform
mk_transform (f)
Convert function f
to Transform
if it isn’t already one
gather_attrs
gather_attrs (o, k, nm)
Used in getattr to collect all attrs k
from self.{nm}
gather_attr_names
gather_attr_names (o, nm)
Used in dir to collect all attrs k
from self.{nm}
Pipeline
Pipeline (funcs=None, split_idx=None)
A pipeline of composed (for encode/decode) transforms, setup with types
add_docs(Pipeline,__call__="Compose `__call__` of all `fs` on `o`",
="Compose `decode` of all `fs` on `o`",
decode="Show `o`, a single item from a tuple, decoding as needed",
show="Add transforms `ts`",
add="Call each tfm's `setup` in order") setup
Pipeline
is a wrapper for compose_tfms
. You can pass instances of Transform
or regular functions in funcs
, the Pipeline
will wrap them all in Transform
(and instantiate them if needed) during the initialization. It handles the transform setup
by adding them one at a time and calling setup on each, goes through them in order in __call__
or decode
and can show
an object by applying decoding the transforms up until the point it gets an object that knows how to show itself.
# Empty pipeline is noop
= Pipeline()
pipe 1), 1)
test_eq(pipe(1,)), (1,))
test_eq(pipe((# Check pickle works
assert pickle.loads(pickle.dumps(pipe))
class IntFloatTfm(Transform):
def encodes(self, x): return Int(x)
def decodes(self, x): return Float(x)
=1
foo
=IntFloatTfm()
int_tfm
def neg(x): return -x
= Transform(neg, neg) neg_tfm
= Pipeline([neg_tfm, int_tfm])
pipe
= 2.0
start = pipe(start)
t -2))
test_eq_type(t, Int(
test_eq_type(pipe.decode(t), Float(start))lambda:pipe.show(t), '-2') test_stdout(
= Pipeline([neg_tfm, int_tfm])
pipe = pipe(start)
t lambda:pipe.show(pipe((1.,2.))), '-1\n-2')
test_stdout(1)
test_eq(pipe.foo, assert 'foo' in dir(pipe)
assert 'int_float_tfm' in dir(pipe)
You can add a single transform or multiple transforms ts
using Pipeline.add
. Transforms will be ordered by Transform.order
.
= Pipeline([neg_tfm, int_tfm])
pipe class SqrtTfm(Transform):
=-1
orderdef encodes(self, x):
return x**(.5)
def decodes(self, x): return x**2
pipe.add(SqrtTfm())4),-2)
test_eq(pipe(-2),4)
test_eq(pipe.decode(
pipe.add([SqrtTfm(),SqrtTfm()])256),-2)
test_eq(pipe(-2),256) test_eq(pipe.decode(
Transforms are available as attributes named with the snake_case version of the names of their types. Attributes in transforms can be directly accessed as attributes of the pipeline.
test_eq(pipe.int_float_tfm, int_tfm)1)
test_eq(pipe.foo,
= Pipeline([int_tfm, int_tfm])
pipe
pipe.int_float_tfm0], int_tfm)
test_eq(pipe.int_float_tfm[1,1]) test_eq(pipe.foo, [
# Check opposite order
= Pipeline([int_tfm,neg_tfm])
pipe = pipe(start)
t -2)
test_eq(t, lambda:pipe.show(t), '-2') test_stdout(
class A(Transform):
def encodes(self, x): return int(x)
def decodes(self, x): return Float(x)
= Pipeline([neg_tfm, A])
pipe = pipe(start)
t -2)
test_eq_type(t,
test_eq_type(pipe.decode(t), Float(start))lambda:pipe.show(t), '-2.0') test_stdout(
= (1,2)
s2 = Pipeline([neg_tfm, A])
pipe = pipe(s2)
t -1,-2))
test_eq_type(t, (1.),Float(2.)))
test_eq_type(pipe.decode(t), (Float(lambda:pipe.show(t), '-1.0\n-2.0') test_stdout(
from PIL import Image
class ArrayImage(ndarray):
= {'cmap':'viridis'}
_show_args def __new__(cls, x, *args, **kwargs):
if isinstance(x,tuple): super().__new__(cls, x, *args, **kwargs)
if args or kwargs: raise RuntimeError('Unknown array init args')
if not isinstance(x,ndarray): x = array(x)
return x.view(cls)
def show(self, ctx=None, figsize=None, **kwargs):
if ctx is None: _,ctx = plt.subplots(figsize=figsize)
**{**self._show_args, **kwargs})
ctx.imshow(im, 'off')
ctx.axis(return ctx
= Image.open(TEST_IMAGE)
im = ArrayImage(im) im_t
def f1(x:ArrayImage): return -x
def f2(x): return Image.open(x).resize((128,128))
def f3(x:Image.Image): return(ArrayImage(array(x)))
= Pipeline([f2,f3,f1])
pipe = pipe(TEST_IMAGE)
t type(t), ArrayImage)
test_eq(-array(f3(f2(TEST_IMAGE)))) test_eq(t,
= Pipeline([f2,f3])
pipe = pipe(TEST_IMAGE)
t = pipe.show(t) ax
#test_fig_exists(ax)
#Check filtering is properly applied
= B()
add1 = 1
add1.split_idx = Pipeline([neg_tfm, A(), add1])
pipe -2)
test_eq(pipe(start), =1
pipe.split_idx-1)
test_eq(pipe(start), =0
pipe.split_idx-2)
test_eq(pipe(start), for t in [None, 0, 1]:
=t
pipe.split_idx
test_eq(pipe.decode(pipe(start)), start)lambda: pipe.show(pipe(start)), "-2.0") test_stdout(
def neg(x): return -x
type(mk_transform(neg)), Transform)
test_eq(type(mk_transform(math.sqrt)), Transform)
test_eq(type(mk_transform(lambda a:a*2)), Transform)
test_eq(type(mk_transform(Pipeline([neg]))), Pipeline) test_eq(
Methods
#TODO: method examples
Pipeline.__call__
Pipeline.__call__ (o)
Call self as a function.
Pipeline.decode
Pipeline.decode (o, full=True)
Pipeline.setup
Pipeline.setup (items=None, train_setup=False)
During the setup, the Pipeline
starts with no transform and adds them one at a time, so that during its setup, each transform gets the items processed up to its point and not after.