muellerzr
(Zachary Mueller)
June 15, 2020, 1:07pm
2
Item Transform and Batch Transform are how we differentiate what is done on the CPU level (item/individual images) and the GPU level (batch/groups of images).
Also see this discussion: Item_tfms vs batch_tfms
I have a visualization there as well
(And it is fastai, 99.9% of everything in the fastai library is built on itself)
Also, just now realized if you want the source code for ItemTransform, it’s in fastcore (the foundation library for fastai2) here:
add_docs(Transform, decode="Delegate to `decodes` to undo transform", setup="Delegate to `setups` to set up transform")
# Cell
class InplaceTransform(Transform):
"A `Transform` that modifies in-place and just returns whatever it's passed"
def _call(self, fn, x, split_idx=None, **kwargs):
super()._call(fn,x,split_idx,**kwargs)
return x
# Cell
class ItemTransform(Transform):
"A transform that always take tuples as items"
_retain = True
def __call__(self, x, **kwargs): return self._call1(x, '__call__', **kwargs)
def decode(self, x, **kwargs): return self._call1(x, 'decode', **kwargs)
def _call1(self, x, name, **kwargs):
if not _is_tuple(x): return getattr(super(), name)(x, **kwargs)
y = getattr(super(), name)(list(x), **kwargs)
if not self._retain: return y
if is_listy(y) and not isinstance(y, tuple): y = tuple(y)
return retain_type(y, x)