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 无法加载神经网络
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01