can't load digits trained caffe model with opencv readnetfromcaffe - c++

I've built digits from this tutorial recently, everything is ok and I finally trained my AlexNet model (also trained a SqueezNet so that I can upload the model here) ! the problem is when I download my model from Digits, I can not load it into my program for testing!I have tested my program with GoogleNet downloaded from this link and it's working fine!
I'm using OpenCV readNetFromCaffe in this function to load Caffe model
void deepNetwork::loadModel( cv::String model ,cv::String weight ,string lablesPath,int ps){
patchSize=ps;
labeslPath=lablesPath;
try
{
net = dnn::readNetFromCaffe(weight,model);
cerr<<"loaded succ"<<endl;
}
catch (cv::Exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
}}
I get the following error loading my model
OpenCV Error: Assertion failed (pbBlob.raw_data_type() ==
caffe::FLOAT16) in blo
bFromProto, file
/home/nvidia/build-opencv/opencv/modules/dnn/src/caffe/caffe_im
porter.cpp, line 242 Exception:
/home/nvidia/build-opencv/opencv/modules/dnn/src/caffe/caffe_importer
.cpp:242: error: (-215) pbBlob.raw_data_type() == caffe::FLOAT16 in
function blo
bFromProto
OpenCV Error: Requested object was not found (Requested blob "data"
not found) i
n setInput, file
/home/nvidia/build-opencv/opencv/modules/dnn/src/dnn.cpp, line
1606 terminate called after throwing an instance of 'cv::Exception'
what():
/home/nvidia/build-opencv/opencv/modules/dnn/src/dnn.cpp:1606: error:
(-204) Requested blob "data" not found in function setInput
Aborted (core dumped)
any help would be appreciated <3
opencv version 3.3.1 also tested on (3.3.0 ,3.4.1) same error!
testing on a system without Cuda, Cudnn or Caffe just pure c++ and OpenCv...
but i've trained my model on a aws ec2 instance (p3.2xlarge ) with Cuda,Cudnn and caffe !
you can download the trained squeezNet model (.prototxt and .caffemodel) here

finally, I found the problem!
it's a version problem I have digits 6.1.1 working with nvcaffe 0.17.0 for training which is not compatible with previous Caffe and OpenCv libraries ! you have to downgrade NvCaffe to version 0.15.14 and it will open with OpenCv easily!

OpenCV DNN model expect caffemodel in BVLC format. But, NVCaffe stores the caffe model in more efficient format which different than BVLC Caffe.
If you want model compatible with both BVLC/Caffe as well as NVcaffe.
Add this flag in solver.prototxt
store_blobs_in_old_format = true
Please read the DIGITS NVCaffe Documentation.
NVCaffe Documenation - store_blobs_in_old_format

Related

Failed to load pre trained onnx models in OpenCV C++

This is my first time with ONNX models and I’m not sure if I’m having a newbie problem so sorry in advance!
I’ve just tried to load a couple of models and I have the same assert always:
[ERROR:0#0.460] global onnx_importer.cpp:1054 cv::dnn::dnn4_v20221220::ONNXImporter::handleNode DNN/ONNX: ERROR during processing node with 3 inputs and 1 outputs: [Concat]:(onnx_node!Concat_2) from domain='ai.onnx'
OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(4.7.0-dev) Error: Unspecified error (> Node [Concat#ai.onnx]:(onnx_node!Concat_2) parse error: OpenCV(4.7.0-dev) C:\GHA-OCV-2\_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\dnn\src\layers\concat_layer.cpp:105: error: (-215:Assertion failed) curShape.size() == outputs[0].size() in function 'cv::dnn::ConcatLayerImpl::getMemoryShapes'
> ) in cv::dnn::dnn4_v20221220::ONNXImporter::handleNode, file C:\GHA-OCV-2\_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\dnn\src\onnx\onnx_importer.cpp, line 1073
Both models come from https://github.com/PeterL1n/RobustVideoMatting and they are “rvm_resnet50_fp32.onnx” and “rvm_mobilenetv3_fp32.onnx”
Obviously I’m loading them with
robustNN = cv::dnn::readNetFromONNX(robustNNPath);
Thank you in advance for any tip!

OpenCV faceDetecter yaml model loading error

I have an error loading a .yaml model to FacemarkLBF from openCV.
cv_landmarks = cv::face::FacemarkLBF::create();
std::cout << "Loading OpenCV model for landmark detection." << std::endl;
cv_landmarks->loadModel("lbfmodel.yaml");
faceDetector.load("haarcascade_frontalface_alt2.xml");
Im getting this error:
loading data from : lbfmodel.yaml
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(4.3.0) /tmp/opencv-20200408-5080-l00ytm/opencv-4.3.0/opencv_contrib/modules/face/src/facemarkLBF.cpp:487: error: (-5:Bad argument) No valid input file was given, please check the given filename. in function 'loadModel'
This model works fine on visual studio, but I need to make the project using Xcode to use it later for iOS.
PS: I tried different models, and I got always the same error.
Provide absolute path for reading the model and it will work then.

OpenCv: Using Yolov3

I used the OpenCV Code from https://docs.opencv.org/3.4.1/da/d9d/tutorial_dnn_yolo.html and made it work on my machine with the yolov2 config/weights. When I try to run the program with yolov3 config/weights (https://pjreddie.com/media/files/yolov3.weights, https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg. both working, when used in ubuntu / not in opencv), it fails on the line
dnn::Net net = readNetFromDarknet(modelConfiguration, modelBinary);
The Exception is:
Unhandled exception at 0xyz in xyz.exe: Microsoft C++ exception: cv::Exception at memory location 0xyz.
The expected behavior would be, that it shows the recognition results, like it does with the yolov2 cfg/weights.
Is yolov3 even usable in opencv?
Thanks,
Michel
Update: Question was answered below. I made a issue in github: https://github.com/opencv/opencv/issues/11310
Update v2: yolov3 is now supported in opencv. -> https://github.com/opencv/opencv/issues/11310 includes an example of the usage
Only Yolo v2 is supported as of 2018/04/11, see the corresponding pull request.

Tensorflow c++ api failed set gpu/cpu number: SetDefaultDevice error: Duplicate registration of device factory for type GPU with the same priority 210

I am loading a trianed tensorflow model and runs it. When I try to set the gpu number, an error raises.
F tensorflow/core/common_runtime/device_factory.cc:77] Duplicate
registration of device factory for type GPU with the same priority 210
Code I use is like:
tensorflow::GraphDef graph_def;
tensorflow::Status graphLoadedStatus = ReadBinaryProto(tensorflow::Env::Default(),model_path, &graph_def);
if (!graphLoadedStatus.ok()) {
std::cerr <<"Model path : " << graphLoadedStatus.ToString() << std::endl;
return graphLoadedStatus;
}
// set device to be on gpu
tensorflow::graph::SetDefaultDevice("/gpu:3", &graph_def);
Google it and no result except the tensoeflow's source code: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/common_runtime/device_factory.cc
Any one have met this? Or can tf's contributor give me some clues?
It looks like a problem of priority, so I tried to run with root, but no use.
Btw, I am using c++. If I use python, I can set the gpu number with no error.
I rebuild tensorflow from the source and use the new libtensorflow_cc.so and head files. Then this problem vanishes.
I think this is caused by my libtensorflow_cc.so and head files are not the same version.

Error when using Decision Trees in OpenCV 3.0.0-rc1

I am doing some machine learning in OpenCV and i'm using Decision Trees. I am currently using OpenCV 3.0.0-rc1. Whenever i attempt to train Decision Trees with my training data and labels, i get either
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
or
Segmentation fault
Depending on what i put into setMaxDepth(); if the number is larger than 22, it's bad_alloc, else it's seg fault.
Here's my source code:
//import data
Mat trainData=imread("/home/jetson/Documents/CB/ml/td.jpg",CV_LOAD_IMAGE_GRAYSCALE);
Mat labels=imread("/home/jetson/Documents/CB/ml/lab.jpg",CV_LOAD_IMAGE_GRAYSCALE);
//convert to the right type
trainData.convertTo(trainData,CV_32FC1);
labels.convertTo(labels,CV_32SC1);
transpose(trainData,trainData);
Ptr<ml::TrainData> tData = ml::TrainData::create(trainData, ml::ROW_SAMPLE, labels);
cout <<"Training data ready\n";
Ptr<ml::DTrees> dec_trees = ml::DTrees::create();
//params
dec_trees->setMaxDepth(1);
dec_trees->setMinSampleCount(10);
dec_trees->setRegressionAccuracy(0.01f);
dec_trees->setUseSurrogates(false);
dec_trees->setMaxCategories(2);
dec_trees->setCVFolds(10);
dec_trees->setUse1SERule(true);
dec_trees->setTruncatePrunedTree(true);
dec_trees->setPriors(Mat());
cout <<"Params set\n";
dec_trees->train(tData);
cout <<"Done!\n";`
In addition to this, when i try to train a SVM model with the same data, using the same steps (below) it works just fine.
Ptr<ml::SVM> svm = ml::SVM::create();
//params
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::POLY);
svm->setGamma(3);
svm->setDegree(0.1);
cout <<"Params set\n";
svm->train(tData);
cout <<"Done!\n";
I need to point out that the error occurs when i try to train the model. I'm using the default parameters for decision trees, as suggested on the OpenCV documentation page.
Does anybody know what's wrong here and how to go about fixing my problem?
Thanks in advance.
EDIT: I upgraded OpenCV to version 3.0.0 and the issues stay the same