简体   繁体   English

使用 Tensorflow 进行预处理的 3D 卷积

[英]3D convolution for preprocessing with Tensorflow

I built a neural network for pixel-wise classification of 3D-images.我为 3D 图像的像素级分类构建了一个神经网络。

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.分类任务非常简单,不需要卷积网络,而是计算一些特征(高斯、LoG、Sobel 等)并将这些与原始值一起输入到经典 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:由于此功能的计算速度非常慢并且没有利用我的 GPU,我认为 Tensorflow 实现可能会有所帮助:

First I read a binary file and create one batch with a 3D array and 1 channel:首先,我读取一个二进制文件并使用 3D 数组和 1 个通道创建一个批次:

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:然后我使用自定义函数创建一个 3D 内核并定义 3D 卷积:

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?第一个问题:这个错误是什么意思,如何实现3D卷积?

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?第二个问题:我是否正确地假设在 tensorflow 中实现这个(目前使用 scikit-image 实现)将有利于执行速度?

I'm putting together the solution in the comments in a more orderly fashion in case someone else stumbles on this question.我正在以更有序的方式将解决方案放在评论中,以防其他人偶然发现这个问题。

Deciphering the error message:破译错误信息:

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. No OpKernel was registered to support Op 'Conv3D' with these attrs属性No OpKernel was registered to support Op 'Conv3D' with these attrs意味着您传递给函数调用的属性与该函数的任何现有实现都不匹配。

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 . Node: Conv3D_1 = Conv3D[ T=DT_INT32 , padding="SAME", strides=[1, 1, 1, 1, 1]](Reshape, LoG_filter/read)] T=DT_INT32 , padding="SAME", strides=[1, 1, 1, 1, 1]](Reshape, LoG_filter/read)]告诉你图中的Conv3D节点正在上升错误,输入张量的类型为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.告诉对于Conv3D操作,您的机器上有 2 个可用的实现。 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 ).一个在您的 CPU 上运行,并以float32张量 ( DT_FLOAT ) 作为输入,而另一个也在您的 CPU 上运行,将float64张量 ( 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?).注意: Registered devices: [CPU],似乎表明您的 Tensorflow没有看到您的 GPU (您是否安装了仅使用 CPU 构建的 Tensorflow?)。

The answer to your questions:你的问题的答案:

What does this error mean and how do I implement a 3D convolution?这个错误是什么意思,我如何实现 3D 卷积?

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.您不想自己实现 Conv3D,而是将输入类型更改为已有实现的内容。 change the type of both Stack and TF_kernel to either float32 or float64 (eg, define kernel with kernel=np.ones((11,11,11,1,1),dtype='float32') ).StackTF_kernel的类型更改为float32float64 (例如,使用kernel=np.ones((11,11,11,1,1),dtype='float32')定义kernel )。

Am I right in my assumption that implementing this in tensorflow (currently implemented with scikit-image) will be of advantage for the execution speed?我是否正确地假设在 tensorflow 中实现它(目前使用 scikit-image 实现)将有利于执行速度?

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).由于这两种实现都只使用 CPU,我想尝试看看情况是否有所改善是最好的选择(如果您可以更新您的问题,让我们知道它是否确实加快了速度,那就太好了)。 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).我还建议检查您是否正在使用 GPU Tensorflow 构建,如果没有,请切换到那个(这有更高的机会加速您的计算)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM