Replicating TensorFlows Conv2D Operating using Eigen Tensors

戏子无情 提交于 2019-12-06 12:22:13

So I eventually found how to perform a 2D convolution using just Eigen tensor function calls, without needing any loops. The code that helped me get here was the Tensorflow eigen_spatial_convolutions.h file @jdehesa linked me to. The lines I linked to have the eigen code required to do a Conv2D operation on both row-major and col-major data so you'll probably only need half of it.

Fundamentally you need to use the Eigen method extract_image_patches to extract the perceptive fields of each filter instance from the input tensor. Then you're reshaping the output of this and your kernel tensor into 2D tensors. This means that each kernel is a vertical column of the reshaped kernel tensor and each row of the reshaped image patches is each patch. You then perform a contraction which is actually a matrix multiplication of these two 2D tensors, and reshape the result back into the correct dimensions to produce the output.

This took me a while to get my head around at first, but it can be done.

outputTensor = inputTensor
.extract_image_patches(kern_w, kern_h, stride_w, stride_h, dilation_w, dilation_h, padding)
.reshape(Eigen::array<int, 2>({patch_count, kern_w*kern_h}))
.contract(kernalTensor.reshape(Eigen::array<int, 2>({kern_w*kern_h, kern_count})), {Eigen::IndexPair < int > (1, 0)})
.reshape(Eigen::array<int, 3>({ output_w, output_h, kern_count }));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!