Commit d8a17512 authored by Cédric Traizet's avatar Cédric Traizet

ENH: replace CvMat by cv::mat (perf and open CV 4 compatibility)

parent 89bbc92a
Pipeline #2593 passed with stages
in 64 minutes and 32 seconds
......@@ -210,7 +210,7 @@ private:
int m_MaxIter;
double m_Epsilon;
CvMat* m_CvMatOfLabels;
cv::Mat m_MatrixOfLabels;
MapOfLabelsType m_MapOfLabels;
};
......
......@@ -46,8 +46,7 @@ NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::NeuralNetworkMachi
m_RegPropDWMin(FLT_EPSILON),
m_TermCriteriaType(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS),
m_MaxIter(1000),
m_Epsilon(0.01),
m_CvMatOfLabels(nullptr)
m_Epsilon(0.01)
{
this->m_ConfidenceIndex = true;
this->m_IsRegressionSupported = true;
......@@ -59,7 +58,6 @@ NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::~NeuralNetworkMach
#ifndef OTB_OPENCV_3
delete m_ANNModel;
#endif
cvReleaseMat(&m_CvMatOfLabels);
}
/** Sets the topology of the NN */
......@@ -108,18 +106,13 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::LabelsToMat(c
unsigned int nbClasses = m_MapOfLabels.size();
if (m_CvMatOfLabels)
{
cvReleaseMat(&m_CvMatOfLabels);
}
m_CvMatOfLabels = cvCreateMat(1, nbClasses, CV_32SC1);
m_MatrixOfLabels = cv::Mat(1,nbClasses, CV_32FC1);
unsigned int itLabel = 0;
for (auto& kv : m_MapOfLabels)
{
classLabel = kv.first;
kv.second = itLabel;
m_CvMatOfLabels->data.i[itLabel] = classLabel;
m_MatrixOfLabels.at<float>(0,itLabel) = classLabel;
++itLabel;
}
......@@ -253,9 +246,8 @@ typename NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TargetSam
// MODE CLASSIFICATION : find the highest response
float secondResponse = -1e10;
target[0] = m_CvMatOfLabels->data.i[0];
unsigned int nbClasses = m_CvMatOfLabels->cols;
target[0] = m_MatrixOfLabels.at<TOutputValue>(0);
unsigned int nbClasses = m_MatrixOfLabels.size[1];
for (unsigned itLabel = 1; itLabel < nbClasses; ++itLabel)
{
currentResponse = response.at<float> (0, itLabel);
......@@ -263,7 +255,7 @@ typename NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TargetSam
{
secondResponse = maxResponse;
maxResponse = currentResponse;
target[0] = m_CvMatOfLabels->data.i[itLabel];
target[0] = m_MatrixOfLabels.at<TOutputValue>(itLabel);
}
else
{
......@@ -287,15 +279,14 @@ typename NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TargetSam
template<class TInputValue, class TOutputValue>
void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Save(const std::string & filename,
const std::string & name)
{
{std::cout << "save!!!!!" << std::endl;
#ifdef OTB_OPENCV_3
cv::FileStorage fs(filename, cv::FileStorage::WRITE);
fs << (name.empty() ? m_ANNModel->getDefaultName() : cv::String(name)) << "{";
m_ANNModel->write(fs);
if (m_CvMatOfLabels != nullptr)
if (!m_MatrixOfLabels.empty())
{
std::string labelsName("class_labels");
fs.writeObj(labelsName,m_CvMatOfLabels);
fs << "class_labels" << m_MatrixOfLabels;
}
fs << "}";
fs.release();
......@@ -312,11 +303,15 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Save(const st
}
m_ANNModel->write(fs, lname);
if (m_CvMatOfLabels != nullptr)
cvWrite(fs, "class_labels", m_CvMatOfLabels);
if (!m_MatrixOfLabels.empty())
{
// cvWrite can't write cv::Mat
auto tmpMat = CvMat(m_MatrixOfLabels);
cvWrite(fs, "class_labels", &tmpMat);
}
cvReleaseFileStorage(&fs);
#endif
std::cout << "endsave!!!!!" << std::endl;
}
template<class TInputValue, class TOutputValue>
......@@ -327,7 +322,7 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Load(const st
cv::FileStorage fs(filename, cv::FileStorage::READ);
cv::FileNode model_node(name.empty() ? fs.getFirstTopLevelNode() : fs[name]);
m_ANNModel->read(model_node);
m_CvMatOfLabels = (CvMat*)cvReadByName( *fs, *model_node, "class_labels" );
model_node["class_labels"] >> m_MatrixOfLabels;
fs.release();
#else
const char* lname = nullptr;
......@@ -353,8 +348,7 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Load(const st
}
m_ANNModel->read(*fs,*model_node);
m_CvMatOfLabels = (CvMat*)cvReadByName( *fs, *model_node, "class_labels" );
model_node["class_labels"] >> m_MatrixOfLabels;
fs.release();
#endif
}
......@@ -382,7 +376,6 @@ bool NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::CanReadFile(c
#endif
)
{
//std::cout << "Reading a " << CV_TYPE_NAME_ML_ANN_MLP << " model" << std::endl;
return true;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment