Tensorflow C ++占位符初始化

时间:2018-08-14 00:14:17

标签: c++ tensorflow

我试图在cpp上使用tensorflow执行图像字幕。我按照How to import an saved Tensorflow model train using tf.estimator and predict on input data上的指示进行尝试

int main(int argc, char**) {
....
    auto x = tf::Tensor(tf::DT_UINT8, tf::TensorShape({height, width, channel}));
    auto matrix = x.matrix<char>();
....
}

当我编译程序时,代码

> In file included from /usr/local/include/tensorflow/core/framework/tensor.h:26:0,
                 from /usr/local/include/tensorflow/core/public/session.h:23,
                 from /usr/local/include/tensorflow/cc/saved_model/loader.h:26,
                 from server.cpp:22:
/usr/local/include/tensorflow/core/framework/types.h: In instantiation of ‘struct tensorflow::DataTypeToEnum<char>’:
/usr/local/include/tensorflow/core/framework/tensor.h:530:46:   required from ‘typename tensorflow::TTypes<T, NDIMS>::Tensor tensorflow::Tensor::tensor() [with T = char; long unsigned int NDIMS = 2ul; typename tensorflow::TTypes<T, NDIMS>::Tensor = Eigen::TensorMap<Eigen::Tensor<char, 2, 1, long int>, 16, Eigen::MakePointer>]’
/usr/local/include/tensorflow/core/framework/tensor.h:240:25:   required from ‘typename tensorflow::TTypes<T>::Matrix tensorflow::Tensor::matrix() [with T = char; typename tensorflow::TTypes<T>::Matrix = Eigen::TensorMap<Eigen::Tensor<char, 2, 1, long int>, 16, Eigen::MakePointer>]’
server.cpp:121:34:   required from here
/usr/local/include/tensorflow/core/framework/types.h:138:3: error: static assertion failed: Specified Data Type not supported
   static_assert(IsValidDataType<T>::value, "Specified Data Type not supported");In file included from /usr/local/include/tensorflow/core/public/session.h:23:0,
             from /usr/local/include/tensorflow/cc/saved_model/loader.h:26,
             from server.cpp:22:
/usr/local/include/tensorflow/core/framework/tensor.h: In instantiation of ‘typename tensorflow::TTypes<T, NDIMS>::Tensor tensorflow::Tensor::tensor() [with T = char; long unsigned int NDIMS = 2ul; typename tensorflow::TTypes<T, NDIMS>::Tensor = Eigen::TensorMap<Eigen::Tensor<char, 2, 1, long int>, 16, Eigen::MakePointer>]’:
/usr/local/include/tensorflow/core/framework/tensor.h:240:25:   required from ‘typename tensorflow::TTypes<T>::Matrix tensorflow::Tensor::matrix() [with T = char; typename tensorflow::TTypes<T>::Matrix = Eigen::TensorMap<Eigen::Tensor<char, 2, 1, long int>, 16, Eigen::MakePointer>]’
server.cpp:121:34:   required from here
/usr/local/include/tensorflow/core/framework/tensor.h:530:46: error: ‘v’ is not a member of ‘tensorflow::DataTypeToEnum<char>’
   CheckTypeAndIsAligned(DataTypeToEnum<T>::v());

有人遇到过同样的问题吗?

2 个答案:

答案 0 :(得分:2)

即使您用初始化列表声明了TensorShape,您仍应指定矩阵的维数,因为在C ++中无法在编译时推导它。

auto x = tf::Tensor(tf::DT_UINT8, tf::TensorShape({height, width, channel}));
auto matrix = x.matrix<char, 3>();

答案 1 :(得分:2)

找到了解决方案。似乎存在两个问题。 (1)DT_UINT8在C ++中是uchar (2)使用张量代替矩阵

我将代码更改为

auto x = tf::Tensor(tf::DT_UINT8, tf::TensorShape(
                        {height, width,3}));
auto matrix = x.tensor<uchar, 3>();

并且工作了。从https://github.com/tensorflow/tensorflow/issues/19909

得到了这个想法