检查失败:1 == NumElements()(1比2)必须具有一个元素张量

时间:2018-08-13 05:29:59

标签: c++ tensorflow

我想通过使用Tensorflow C ++ API进行预测 但是,发生了错误:

  

检查失败:1 == NumElements()(1 vs.2)必须具有一个元素张量

它是准备输入的代码:

tensorflow::Tensor loadImage(tensorflow::string fname){
    tensorflow::int32 width = 224;
    tensorflow::int32 height = 224;
    tensorflow::int32 nData = 1;
    tensorflow::int32 nVec = width*height;
    tensorflow::int32 channels = 3;
    auto tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, height, width, channels}));
    auto mat = tensor.tensor<float, 4>();
    std::ifstream fin(fname, std::ios_base::in | std::ios_base::binary);
    assert(!fin.fail());
    boost::iostreams::filtering_istream s;
    s.push(fin);
    char c;
    for(int i=0;i<nData;i++){
        for(int j=0;j<channels;j++){
            for(int h=0;h<height;h++){
                for(int w=0;w<width;w++){
                    s.get(c);
                    mat(i, j, h, w) = static_cast<float>(static_cast<uint8_t>(c)) / 255.0;
                }
            }
        }
    }std::cout << "Image Loaded" << std::endl;
    return tensor;
}

执行此代码后,出现错误:

status = session->Run(inputs, {"output_node0"}, {}, &outputs);
auto output_c = outputs[0].scalar<float>(); // an error occurred

我在这里发现了类似的问题。 根据{{​​3}},看来我可以通过更改以下代码来解决。

来自

auto output_c = outputs[0].scalar<float>();

auto output_c = outputs[0].flat<float>();

但是,我遇到了构建错误。 有关更多信息...

error: static assertion failed: YOU_MADE_A_PROGRAMMING_MISTAKE
 #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);/tmp/tensorflow/include/unsupported/Eigen/CXX11/src/Tensor/TensorMap.h:224:7: note: in expansion of macro 'EIGEN_STATIC_ASSERT'
       EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)

错误原因是什么?

1 个答案:

答案 0 :(得分:1)

  

错误原因是什么?

哪个?你有多个。仅显示几行不相关的代码时,两者都无法回答。

loadImage看起来还不错。但是会话运行的呼叫看起来很奇怪。应该是

std::vector<tensorflow::Tensor> outputs;
tensorflow::ClientSession session(root);
TF_CHECK_OK(session.Run({some_operation}, &outputs));
float *result_float_data = outputs[0].flat<float>().data();

但这取决于您使用的实际图形。

无论如何,您的实现有点麻烦。我猜您只想将图像输入到图形中。有两种更简单的解决方案来读取C ++和TensorFlow中的图像。

纯TensorFlow版本就是

tensorflow::Scope root = tensorflow::Scope::NewRootScope();

std::string fn = "Grace_Hopper.png";

auto net1 = tensorflow::ops::ReadFile(root, fn);
auto net2 = tensorflow::ops::DecodePng(root, net1);
auto net3 = tensorflow::ops::Cast(root, net2, tensorflow::DT_FLOAT);
auto net4 = tensorflow::ops::ExpandDims(root, net3, 0);
// do something here
auto net5 = tensorflow::ops::ResizeBilinear(root, net4, tensorflow::ops::Const(root, {2 * 606, 2 * 517}));
auto net6 = tensorflow::ops::Reshape(root, net5, tensorflow::ops::Const(root, {2 * 606, 2 * 517, 3}));
// convert back
auto net7 = tensorflow::ops::Cast(root, net6, tensorflow::DT_UINT8);
auto net8 = tensorflow::ops::EncodeJpeg(root, net7);

std::vector<tensorflow::Tensor> outputs;
tensorflow::ClientSession session(root);

// Run and fetch v
TF_CHECK_OK(session.Run({net8}, &outputs));
std::ofstream("output.jpg", std::ios::binary) << outputs[0].scalar<std::string>()();

TensorFlow + OpenCV版本具有few more lines