【问题标题】:Tensorflow C++ Placeholder initializationTensorflow C++ 占位符初始化
【发布时间】:2018-08-14 00:14:17
【问题描述】:

我试图在 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());

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

【问题讨论】:

    标签: c++ tensorflow


    【解决方案1】:

    找到了解决办法。似乎存在两个问题。 (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得到想法

    【讨论】:

    • 有什么遗漏吗?范围?我这样说:auto x = Placeholder(rootScope, DT_FLOAT)
    【解决方案2】:

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

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

    【讨论】:

    • 我按照建议进行了修改,得到了一个新的错误错误的模板参数数量(2,应该是 1)auto matrix = x.matrix();还有一件事,如果未指定,矩阵如何处理高度和宽度?谢谢。
    • 更新错误。 >::Matrix tensorflow::Tensor::matrix() typename TTypes::Matrix matrix() { ^ /usr/local/include/tensorflow/core/framework/tensor.h:239:30: 注意:模板参数推导/替换失败:server.cpp:121:37:错误:模板参数的数量错误(2,应为 1)自动矩阵 = x.matrix();
    • 对,我现在可以看到,没有测试。支持你的回答;)
    猜你喜欢
    • 2017-10-12
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    相关资源
    最近更新 更多