TL.DR. Is there a 3-dimensional friendly implementation of theano.tensor.nnet.neighbours.images2neibs
?
I would like to perform voxel-wise classification of a volume (NxNxN) using a neural network that takes in a nxnxn image, where N>n. To classify each voxel in the volume, I have to iterate through each voxel. For each iterration, I obtain and pass the neighborhood voxels as the input to the neural network. This is simply a sliding window operation, which the operation is the neural network.
While my neural network is implemented in Theano, the sliding window implementation is in python/numpy. Since this is not a pure Theano operation, the classification takes forever (> 3 hours) to classify all voxels in one volume. For 2d sliding window operation, Theano has a helper method, theano.tensor.nnet.neighbours.images2neibs
, is there a similar implementation for 3-dimensional images?
Edit:
There are existing numpy solutions (1 and 2) for n-d sliding window, both use np.lib.stride_tricks.as_strided
to provide "views of the sliding window", thus preventing memory issues. In my implementation, the sliding window arrays are being passed from numpy (Cython) to Python and then to Theano. To boost performance, it's likely I have to bypass Python.
sklearn.feature_extraction.image.extract_patches
. This can give you a view onto the desirednxnxn
cubes without making a copy of the data. Combine it with annp.einsum
which also doesn't copy and you may get something that runs in acceptable time (no guarantee, never tried) – Shellackingnp.einsum
! – Archaicsklearn.feature_extraction.image.extract_patches
also uses stride tricks to do its work. It is just a few lines of code and calculation to get the right shape of the views. – Shellacking