将OpenCV灰度Mat转换为Caffe blob

时间:2015-08-25 13:06:54

标签: c++ opencv machine-learning deep-learning caffe

我一直following an example我被提到如何将OpenCV Mat转换为我可以做出预测的Caffe对象。据我所知,第一部分缩放图像,然后初始化caffe类TransformationParameter

const float img_to_net_scale = 0.0039215684;
TransformationParameter input_xform_param;
input_xform_param.set_scale( img_to_net_scale );
DataTransformer<float> input_xformer( input_xform_param, TEST );

然后,OpenCV Mat“补丁”被转换为“input_blob”。我已经改变了这个部分,因为我已经在我的图像中加载了灰度而不是颜色。

cv::Mat patch = cv::imread( input_file_str, CV_LOAD_IMAG_GRAYSCALE  );
Blob<float> input_blob;
input_blob.Reshape(1, patch.channels(), patch.rows, patch.cols );
input_xformer.Transform( patch, &input_blob );

最后,我不太清楚这部分是做什么的 - 如果我已经将我的OpenCV Mat转换为Caffe blob,为什么我需要推回“输入”向量并将其传递给网络?我不能直接将input_blob传递到网络中以获得我的预测吗?

std::vector<Blob<float>*> input;
input.push_back( &input_blob );

std::vector<Blob<float>*> output = net->Forward( input );

1 个答案:

答案 0 :(得分:2)

您需要push_back input_blob net将其传递给net,因为std::vector期望其输入为 Blobs of net(原则上,input_blob可能需要多个输入blob来生成输出。)
请注意,您不是将 */ class Parent class Child extends Parent class GrandChild extends Child object main{ def test[B >: Child](x : B) = x; // B should be of type Child or Parent def main(args: Array[String]): Unit = { test(new Parent); //works. B == Parent test(new Child); //works. B == Child test (new GrandChild) // works!!! Surprise!!! B == GrandParent. This should not work, right? } } 复制到输入向量中,而是将指针传递给它。