OpenCV 3.0 Can#39;t load neural network(OpenCV 3.0 无法加载神经网络)
问题描述
我需要在我的 OpenCV(3.0 版)项目中使用神经网络.我已经创建并训练了神经网络并且它可以工作,但是如果我想从 YML 文件加载神经网络,它不会预测.
I need to use a neural network in my OpenCV (version 3.0) project. I've created and trained neural network and it works, but if I want to load neural network from YML file, it doesn't predict.
这是我创建、训练和保存我的神经网络的代码:
This is a code where I creat, train and save my neural network:
FileStorage fs("nn.yml", FileStorage::WRITE);
int input_neurons = 7;
int hidden_neurons = 100;
int output_neurons = 5;
Ptr<TrainData> train_data = TrainData::loadFromCSV("data.csv", 10, 7, 12);
Ptr<ANN_MLP> neural_network = ANN_MLP::create();
neural_network->setTrainMethod(ANN_MLP::BACKPROP);
neural_network->setBackpropMomentumScale(0.1);
neural_network->setBackpropWeightScale(0.05);
neural_network->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)10000, 1e-6));
Mat layers = Mat(3, 1, CV_32SC1);
layers.row(0) = Scalar(input_neurons);
layers.row(1) = Scalar(hidden_neurons);
layers.row(2) = Scalar(output_neurons);
neural_network->setLayerSizes(layers);
neural_network->setActivationFunction(ANN_MLP::SIGMOID_SYM, 1, 1);
neural_network->train(train_data);
if (neural_network->isTrained()) {
neural_network->write(fs);
cout << "It's OK!" << endl;
}
但是下一次,如果我想从 YML 文件中加载它:
But next time, if I want to load it from YML file:
Ptr<ANN_MLP> neural_network = Algorithm::load<ANN_MLP>("nn.yml", "neural_network");
我得到输出:
[-1.#IND, -1.#IND, -1.#IND, -1.#IND, -1.#IND]
[-1.#IND, -1.#IND, -1.#IND, -1.#IND, -1.#IND]
[-1.#IND, 1.0263158, 1.0263158, 1.0263158, 1.0263158]
[-1.#IND, 1.0263158, 1.0263158, 1.0263158, 1.0263158]
[1.0263158, 1.0263158, 1.0263158, 1.0263158, 1.0263158]
[1.0263158, 1.0263158, 1.0263158, 1.0263158, 1.0263158]
[-1.#IND, -1.#IND, -1.#IND, -1.#IND, -1.#IND]
[-1.#IND, -1.#IND, -1.#IND, -1.#IND, -1.#IND]
Ptr<ANN_MLP> neural_network = Algorithm::load<ANN_MLP>("nn.yml");
这一行导致我收到错误:
This line cause that I get an error:
OpenCV 错误:未指明的错误(节点既不是地图也不是空集合on) 在 cvGetFileNodeByName,文件 C:uildsmaster_PackSlave-win64-vc12-sharedopencvmodulescoresrcpersistence.cpp,第 739 行
OpenCV Error: Unspecified error (The node is neither a map nor an empty collecti on) in cvGetFileNodeByName, file C:uildsmaster_PackSlave-win64-vc12-sharedop encvmodulescoresrcpersistence.cpp, line 739
我做错了什么?问题出在哪里?
What am I doing wrong? Where is the problem?
推荐答案
你可以使用 save
和 load
,或者 write
和 >阅读
,但你不应该把它们混在一起.
You can use save
and load
, or write
and read
, but you shouldn't mix them.
所以你要么需要做:
// Save
neural_network->save("nn.yml");
// Load
Ptr<ANN_MLP> nn = Algorithm::load<ANN_MLP>("nn.yml");
或:
// Write
neural_network->write(fs);
// Read
FileStorage ffs("nn.yml", FileStorage::READ);
Ptr<ANN_MLP> nn = Algorithm::read<ANN_MLP>(ffs.root());
这篇关于OpenCV 3.0 无法加载神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OpenCV 3.0 无法加载神经网络
- 从python回调到c++的选项 2022-11-16
- C++ 协变模板 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01