使用来自 C++ 的训练有素的咖啡网的错误结果
Wrong results using trained caffe net from c++
我尝试将经过训练的 caffe net 与来自 C++ 的数据一起使用。我为部署实施了标准 caffe 示例 classification.cpp
。在使用 python 脚本的 train/test 阶段,网络达到了 accuracy = 0.93,但现在当我去部署时,我得到了一些奇怪的结果。我有两个 classes:
- 环境
- 对象
并且我需要获取对象检测的概率。我相信如果网络在 FC 层有两个输出 (prob1 + prob2 == 1.0f),结果将以 Softmax
输出 blob 中两个概率的形式呈现,但结果令人费解。在输出向量中,我为每个图像得到 两个相同的值。这是输入和输出层:
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);
此外,结果类似于随机(并且每个重复class),最小概率值为magic 0.443142。该值通常出现在输出向量中。我做错了什么?
所以,这个问题超出了主题的范围。这是关于 STL 和 Qt 向量之间的区别。
原码
std::vector<float> output(begin, end);
而不是
QVector<float> output(end - begin, *begin);
问题已解决。
我尝试将经过训练的 caffe net 与来自 C++ 的数据一起使用。我为部署实施了标准 caffe 示例 classification.cpp
。在使用 python 脚本的 train/test 阶段,网络达到了 accuracy = 0.93,但现在当我去部署时,我得到了一些奇怪的结果。我有两个 classes:
- 环境
- 对象
并且我需要获取对象检测的概率。我相信如果网络在 FC 层有两个输出 (prob1 + prob2 == 1.0f),结果将以 Softmax
输出 blob 中两个概率的形式呈现,但结果令人费解。在输出向量中,我为每个图像得到 两个相同的值。这是输入和输出层:
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);
此外,结果类似于随机(并且每个重复class),最小概率值为magic 0.443142。该值通常出现在输出向量中。我做错了什么?
所以,这个问题超出了主题的范围。这是关于 STL 和 Qt 向量之间的区别。 原码
std::vector<float> output(begin, end);
而不是
QVector<float> output(end - begin, *begin);
问题已解决。