Replicating TensorFlows Conv2D Operating using Eigen Tensors

后端 未结 1 1175
长情又很酷
长情又很酷 2021-01-07 01:44

I\'m trying to implement a lightweight (minimal library dependencies) version of a TensorFlow graph in c++ and I\'m trying to use Eigen Tensor objects to perform the graphs

相关标签:
1条回答
  • 2021-01-07 02:48

    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 }));
    
    0 讨论(0)
提交回复
热议问题