libffcv / ffcv

FFCV: Fast Forward Computer Vision (and other ML workloads!)
https://ffcv.io
Apache License 2.0
2.81k stars 180 forks source link

Support for scipy/ opencv transforms #172

Closed AmmaraRazzaq closed 2 years ago

AmmaraRazzaq commented 2 years ago

ffcv isn't supporting scipy / opencv transforms.

I have written the following class

class Rotate(Operation):
    def __init__(self, angle: int = 15):
        super().__init__()
        self.angle = angle

    def generate_code(self) -> Callable:
        my_range = Compiler.get_iterator()
        angle = self.angle

        def rotate(images, dst):
            for i in my_range(images.shape[0]):
                dst[i] = scipy.ndimage.rotate(images[0], angle=angle, reshape=False)
            return dst
        rotate.is_parallel = True
        return rotate

    def declare_state_and_memory(self, previous_state: State) -> Tuple[State, Optional[AllocationQuery]]:
        return (replace(previous_state, jit_mode=True),
                AllocationQuery(previous_state.shape, previous_state.dtype))

and I am getting the following error: Unknown attribute 'rotate' of type Module(<module 'scipy.ndimage' from '/nfs/users/ext_mohammed.haashir/miniconda3/envs/ffcv5/lib/python3.8/site-packages/scipy/ndimage/init.py'>)

Exception in thread Thread-7: Traceback (most recent call last): File "/nfs/users/ext_mohammed.haashir/miniconda3/envs/ffcv5/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/nfs/users/ext_mohammed.haashir/miniconda3/envs/ffcv5/lib/python3.8/site-packages/ffcv/loader/epoch_iterator.py", line 79, in run result = self.run_pipeline(b_ix, ixes, slot, events[slot]) File "/nfs/users/ext_mohammed.haashir/miniconda3/envs/ffcv5/lib/python3.8/site-packages/ffcv/loader/epoch_iterator.py", line 133, in run_pipeline result = code(*args) File "/nfs/users/ext_mohammed.haashir/miniconda3/envs/ffcv5/lib/python3.8/site-packages/numba/core/dispatcher.py", line 482, in _compile_for_args error_rewrite(e, 'typing') File "/nfs/users/ext_mohammed.haashir/miniconda3/envs/ffcv5/lib/python3.8/site-packages/numba/core/dispatcher.py", line 423, in error_rewrite raise e.with_traceback(None) numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend) Failed in nopython mode pipeline (step: nopython frontend) Unknown attribute 'rotate' of type Module(<module 'scipy.ndimage' from '/nfs/users/ext_mohammed.haashir/miniconda3/envs/ffcv5/lib/python3.8/site-packages/scipy/ndimage/init.py'>) File "../pyfiles/transformations.py", line 312: def rotate(images, dst):

for i in my_range(images.shape[0]): dst[i] = scipy.ndimage.rotate(images[0], angle=angle, reshape=False) ^ During: typing of get attribute at /nfs/users/ext_mohammed.haashir/projects/teacherstudentmodelv1.2/TMwithCheXpert/notebooks/ffcv/pyfiles/transformations.py (312) File "../pyfiles/transformations.py", line 312: def rotate(images, dst):

for i in my_range(images.shape[0]): dst[i] = scipy.ndimage.rotate(images[0], angle=angle, reshape=False) ^ During: resolving callee type: type(CPUDispatcher(<function Rotate.generate_code..rotate at 0x7f927c9e68b0>)) During: typing of call at (2) During: resolving callee type: type(CPUDispatcher(<function Rotate.generate_code..rotate at 0x7f927c9e68b0>)) During: typing of call at (2) File "/nfs/users/ext_mohammed.haashir/projects/teacherstudentmodelv1.2/TMwithCheXpert/notebooks/ffcv/notebooks", line 2: <source missing, REPL/exec in use?>

GuillaumeLeclerc commented 2 years ago

Hello,

FFCV uses numba under the hood. We can only compile what numba can. There is an open pull request from someone adding rotation to FFCV but you could also go to the scipy repository on github, copy/paste it and compile it with numba. It might work.

You can also re-implement it, it's pretty straightforward. Unfortunately we don't have the resources to implement every function that every user might need.