I built a neural network for pixel-wise classification of 3D-images.
The classification task is very simple and does not call for a convolutional network, instead I calculate a number of features (Gaussian, LoG, Sobel,etc...) and feed these together with the original value into a classical MLP. Since the calculation of this features is very slow and does not take advantage of my GPU, I thought a Tensorflow implementation might help:
First I read a binary file and create one batch with a 3D array and 1 channel:
data_dir="/Users/Me/Documents/Data/"
filenames = [os.path.join(data_dir,'File_%05d.bin' % i ) for i in range(100)]
filename_queue = tf.train.string_input_producer(filenames)
Stack= BinChunkReader(filename_queue) #custom reader
sess = tf.Session()
print(sess.run(tf.shape(Stack))) #outputs [1 100 100 100 1]
Then I create a 3D kernel using a custom function and define the 3D convolution:
kernel=np.ones((11,11,11,1,1),dtype='int32')
kernel[:,:,:,0,0]=Get3DKernel("LoG", Radius=6,Param=5) #custom function to produce a kernel
kernel_init=tf.constant(kernel)
TF_kernel=tf.get_variable('LoG_filter', initializer= kernel_init)
LoG=tf.nn.conv3d(Stack,TF_kernel,[1,1,1,1,1],"SAME")
but trying to run this
sess = tf.Session()
sess.run(LoG)
produces the following error:
InvalidArgumentError: No OpKernel was registered to support Op 'Conv3D' with these attrs. Registered devices: [CPU], Registered kernels:
device='CPU'; T in [DT_FLOAT]
device='CPU'; T in [DT_DOUBLE]
[[Node: Conv3D_1 = Conv3D[T=DT_INT32, padding="SAME", strides=[1, 1, 1, 1, 1]](Reshape, LoG_filter/read)]]
The first question: what does this error mean and how do I implement a 3D convolution?
The second question: am I right in my assumption that implementing this in tensorflow (currently implemented with scikit-image) will be of advantage for the execution speed?
I'm putting together the solution in the comments in a more orderly fashion in case someone else stumbles on this question.
Your error:
InvalidArgumentError: No OpKernel was registered to support Op 'Conv3D' with these attrs.
Registered devices: [CPU],
Registered kernels:
device='CPU'; T in [DT_FLOAT]
device='CPU'; T in [DT_DOUBLE]
[[Node: Conv3D_1 = Conv3D[T=DT_INT32, padding="SAME", strides=[1, 1, 1, 1, 1]](Reshape, LoG_filter/read)]]
No OpKernel was registered to support Op 'Conv3D' with these attrs means that the attributes you passed to the function call do not match any existing implementation for that function.
Node: Conv3D_1 = Conv3D[T=DT_INT32, padding="SAME", strides=[1, 1, 1, 1, 1]](Reshape, LoG_filter/read)] tells that for the Conv3D node in your graph that is raising the error, the input tensor has type int32.
Registered kernels:
device='CPU'; T in [DT_FLOAT]
device='CPU'; T in [DT_DOUBLE]
tells that for the Conv3D operation there are available 2 implementations on your machine. One runs on your CPU and has as input a float32 tensor (DT_FLOAT), while the other is also running on your CPU taking as input float64 tensors (DT_DOUBLE).
Note: Registered devices: [CPU], seems to point at the fact that your Tensorflow does not see your GPU (did you install a CPU-only build of Tensorflow?).
What does this error mean and how do I implement a 3D convolution?
I think the error has been explained enough in the previous section. You don't want to implement Conv3D on your own, but rather change the input type to something that already has an implementation. change the type of both Stack and TF_kernel to either float32 or float64 (e.g., define kernel with kernel=np.ones((11,11,11,1,1),dtype='float32')).
Am I right in my assumption that implementing this in tensorflow (currently implemented with scikit-image) will be of advantage for the execution speed?
Hard to say. Since both implementations are CPU-only, I guess trying and seeing if things improve is the best option (it would be great if you could then update your question letting us know if it did speed up after all). I'd also suggest to check if you're using the GPU Tensorflow build and, if not, switch to that one (that has a much higher chance to speed up your computation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With