MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / LazyTransform

Class LazyTransform

tensorpack/dataflow/imgaug/transform.py:318–354  ·  view source on GitHub ↗

A transform that's instantiated at the first call to `apply_image`.

Source from the content-addressed store, hash-verified

316
317
318class LazyTransform(BaseTransform):
319 """
320 A transform that's instantiated at the first call to `apply_image`.
321 """
322 def __init__(self, get_transform):
323 """
324 Args:
325 get_transform (img -> Transform): a function which will be used to instantiate a Transform.
326 """
327 self.get_transform = get_transform
328 self._transform = None
329
330 def apply_image(self, img):
331 if not self._transform:
332 self._transform = self.get_transform(img)
333 return self._transform.apply_image(img)
334
335 def _apply(self, x, meth):
336 assert self._transform is not None, \
337 "LazyTransform.{} can only be called after the transform has been applied on an image!"
338 return getattr(self._transform, meth)(x)
339
340 def __getattr__(self, name):
341 if name.startswith("apply_"):
342 return lambda x: self._apply(x, name)
343 raise AttributeError("TransformList object has no attribute {}".format(name))
344
345 def __repr__(self):
346 if self._transform is None:
347 return "LazyTransform(get_transform={})".format(str(self.get_transform))
348 else:
349 return repr(self._transform)
350
351 __str__ = __repr__
352
353 def apply_coords(self, coords):
354 return self._apply(coords, "apply_coords")
355
356
357if __name__ == '__main__':

Callers 1

get_transformMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…