I wanna extend the functionality of PILBase.create
so that it can read list of image paths (for image of more than 3 channels). So naturally I tried patching to achieve this:
@patch(cls_method=True)
def create(cls: PILBase, fn:(Path,str,Tensor,ndarray,bytes,list), **kwargs)->None:
if isinstance(fn,TensorImage): fn = fn.permute(1,2,0).type(torch.uint8)
if isinstance(fn, TensorMask): fn = fn.type(torch.uint8)
if isinstance(fn,Tensor): fn = fn.numpy()
# handle list of images for 4 channels
if isinstance(fn, list):
channel_imgs = [Image.open(img_path) for img_path in fn]
fn = np.stack(channel_imgs, axis = -1)
# return PILImage object for consistency
if isinstance(fn,ndarray):
return cls(Image.fromarray(fn))
if isinstance(fn,bytes): fn = io.BytesIO(fn)
return cls(load_image(fn, **merge(cls._open_args, kwargs)))
However, something strange happens after I patch PILBase.create
and then wrap it by Transform
:
path = Path('../hpa-2019-data/external-data/external_512/10580_1610_C1_1_red.jpg')
after_func = Transform(PILBase.create)
after_func(path)
>> Path('../hpa-2019-data/external-data/external_512/10580_1610_C1_1_red.jpg')
As shown above, after_func
should have returned an instance of PILImage
but ended up returning input itself. When I further inspect the signature(?) of the transformed function, I get the following which doesnt seem right:
after_func
>>> PILBase.create:
encodes: (PILBase,Path) -> create
(PILBase,str) -> create
(PILBase,Tensor) -> create
(PILBase,ndarray) -> create
(PILBase,bytes) -> create
(PILBase,list) -> createdecodes:
From my understanding, a correct signature of a transformed function should look like this:
PILBase.create:
encodes: (Path,object) -> create
(str,object) -> create
(Tensor,object) -> create
(ndarray,object) -> create
(bytes,object) -> createdecodes:
It is important for my patched PILBase.create
to work properly after being wrapped by Transform
because it is what is going on inside DataBlock
. I have no clue on what is causing the issue. It would be great if any fellow could shed some light on it!