【问题标题】:Tensorflow Facenet Classifier in C++C++ 中的 Tensorflow Facenet 分类器
【发布时间】:2017-06-21 07:04:21
【问题描述】:

我一直在尝试将最初用 python 编写的facenet classifier 实现到 C++ 中,并且在大多数情况下它运行良好。我已经用 opencv 读取图像并转换为 tensorflow 张量,但是在运行图表后,我的输出张量充满了 NaN 值。

代码部分如下:

string input_layer = "input:0";
string phase_train_layer = "phase_train:0";
string output_layer = "embeddings:0";

tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({input_Images.size(), height, width, channels}));
auto input_tensor_mapped = input_tensor.tensor<float, 4>();        

for (int i = 0; i < input_Images.size(); i++) {
    Mat image = input_Images[i];
    const float * source_data = (float*) image.data;
    for (int h = 0; h < image.rows; ++h) {
        const float* source_row = source_data + (h * image.cols * image.channels());
        for (int w = 0; w < image.cols; ++w) {
            const float* source_pixel = source_row + (w * image.channels());
            for (int c = 0; c < image.channels(); ++c) {
                const float* source_value = source_pixel + c;
                //std::cout << *source_value << std::endl;
                input_tensor_mapped(i, h, w, c) = *source_value;
            }
        }
    }
}

tensorflow::Tensor phase_tensor(tensorflow::DT_BOOL, tensorflow::TensorShape());
phase_tensor.scalar<bool>()() = false;

    cout << phase_tensor.DebugString() << endl;
    cout << input_tensor.DebugString() << endl;
std::vector<tensorflow::Tensor> outputs;

std::vector<std::pair<string, tensorflow::Tensor>> feed_dict = {
    {input_layer, input_tensor},  
    {phase_train_layer, phase_tensor},
};    

Status run_status = session->Run(feed_dict,
                            {output_layer}, {} , &outputs);
if (!run_status.ok()) {
    LOG(ERROR) << "\tRunning model failed: " << run_status << "\n";
    return -1;
}

cout << outputs[0].DebugString() << endl;

任何想法为什么会出现这种情况?

【问题讨论】:

    标签: c++ tensorflow


    【解决方案1】:

    所以,经过长时间的分心,我终于跳回了这个小项目。这意味着我忘记了自己为什么要做任何事情,因此需要从头开始看待事情。这意味着我很确定我已经弄清楚了为什么我得到了 NaN 值,以及为什么添加预白化/归一化步骤可以解决它。这是非常基本的,我很生气我第一次没有掌握它。

    所有 Inception 图都要求输入矩阵具有标准化值 [-1,1]。预白化/归一化步骤会执行此操作。

        Image = Image - cv::Vec3d(mean_pxl, mean_pxl, mean_pxl);
        Image = Image / stddev_pxl;
    

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 2018-02-16
      • 2017-01-08
      • 2018-07-06
      • 2021-11-04
      • 2019-10-03
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多