c++ - 使用来自 C++ 的训练有素的 caffe net 得到错误的结果

标签 c++ machine-learning neural-network deep-learning caffe

我尝试将经过训练的咖啡网络与来自 C++ 的数据结合使用。我实现了用于部署的标准 caffe 示例 classification.cpp。在使用 python 脚本的训练/测试阶段,网络达到了准确度 = 0.93,但现在当我去部署时,我得到了一些奇怪的结果。我有两门课:

  • 环境
  • 对象

我需要得到物体检测的概率。我相信如果网络在 FC 层中有两个输出 (prob1 + prob2 == 1.0f),结果将以 Softmax 输出 blob 中的两个概率的形式呈现,但结果令人费解。在输出 vector 中,我为每个图像获得两个相同的值。以下是输入层和输出层:

layer {
    name: "data"
    top:  "data"
    type: "Input"
    input_param { shape: { dim: 1 dim: 3 dim: 227 dim: 227 }}
}
layer {
    name: "fc6"
    top:  "fc6"
    type: "InnerProduct"
    bottom: "drop5"
    inner_product_param {
        num_output: 2
        weight_filler {
            type: "xavier"
            std: 0.1
        }
    }
}
layer {
    name: "prob"
    top:  "prob"
    type: "Softmax"
    bottom: "fc6"
}

我的常规使用的 C++ 代码示例:

Blob<float>* input_layer = m_net->input_blobs()[0];
input_layer->Reshape(1, m_numChannels, m_inputGeometry.height, m_inputGeometry.width);
m_net->Reshape();
std::vector<cv::Mat> input_channels;
Blob<float>* input_layer = m_net->input_blobs()[0];
int width = input_layer->width();
int height = input_layer->height();
float* input_data = input_layer->mutable_cpu_data();

for(int i = 0; i < input_layer->channels(); ++i){
    cv::Mat channel(height, width, CV_32FC1, input_data);
    input_channels->push_back(channel);
    input_data += width * height;
}

cv::split(image_float, *input_channels);
m_net->Forward();
Blob<float>* output_layer = m_net->output_blobs()[0];
const float* begin = output_layer->cpu_data();
const float* end = begin + output_layer->channels();
QVector<float> output = QVector<float>(end - begin, *begin);

此外,结果与随机类似(并且为每个类别重复),最小概率值为 magic 0.443142。该值通常可以在输出 vector 中找到。我做错了什么?

最佳答案

所以,这个问题超出了主题的范围。这是关于STL和Qt vector 之间的差异。 原始代码

std::vector<float> output(begin, end);

而不是

QVector<float> output(end - begin, *begin);

解决了问题。

关于c++ - 使用来自 C++ 的训练有素的 caffe net 得到错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38529902/

相关文章:

C++模板模板

python - 使用 Python 和机器学习 (LSTM) 预测 future 'x' 天的股票价格

java - 神经网络中的大数处理

c++ - 合并排序问题,在方法之间传递数组?

c++ - 如何在 C++ 中从文件中读取 ADTS header ?

c++ - 在 BMP 像素 RGB 阵列上应用模糊

algorithm - 如何计算给定算法的时间复杂度(岭回归)?

python - 数据时间专业和批处理专业有什么区别?

python - 在纯 Tensorflow 中混合分类数据和连续数据

machine-learning - 使用循环神经网络进行时间序列预测