3

I was trying to perform image captioning with tensorflow on cpp. I was following the direction on How to import an saved Tensorflow model train using tf.estimator and predict on input data and tried

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

When I compile the program, the code

> 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());

Did anybody came across the same problem?

Yoon JaeHong
  • 81
  • 1
  • 5

2 Answers2

2

Even though you declare your TensorShape with an initialization list, you should still specify the number of dimensions of your matrix, as it cannot be deduced at compilation time in C++.

auto x = tf::Tensor(tf::DT_UINT8, tf::TensorShape({height, width, channel}));
auto matrix = x.matrix<char, 3>();
Adrien Leravat
  • 2,731
  • 18
  • 32
  • I modified as suggested and got a new error wrong number of template arguments (2, should be 1) auto matrix = x.matrix(); One more thing, how does the matrix deal with height and width if not specified? Thanks. – Yoon JaeHong Aug 14 '18 at 01:30
  • Update in error. >::Matrix tensorflow::Tensor::matrix() typename TTypes::Matrix matrix() { ^ /usr/local/include/tensorflow/core/framework/tensor.h:239:30: note: template argument deduction/substitution failed: server.cpp:121:37: error: wrong number of template arguments (2, should be 1) auto matrix = x.matrix(); – Yoon JaeHong Aug 14 '18 at 01:37
  • Right, I can see that now, didn't tested. Upvoted your anwer ;) – Adrien Leravat Aug 14 '18 at 16:27
2

Found a solution. It seems like two issues were present. (1) DT_UINT8 is uchar in c++ (2) use tensor instead of matrix

I changed the code to

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

and worked. Got the idea from https://github.com/tensorflow/tensorflow/issues/19909

Yoon JaeHong
  • 81
  • 1
  • 5
  • is there something missing? the scope? i put it this way: auto x = Placeholder(rootScope, DT_FLOAT) – Dee May 19 '19 at 05:41