@@ -122,6 +122,7 @@ class StandarizerPreprocessor: public ImagePreprocessor{
122
122
// void set_mean_(Mat M){}
123
123
124
124
void preprocess_ (const Mat& input,Mat& output,Size outputSize,int outputChannels){
125
+
125
126
// TODO put all the logic of channel and depth conversions in ImageProcessor class
126
127
CV_Assert (outputChannels==1 || outputChannels==3 );
127
128
CV_Assert (input.channels ()==1 || input.channels ()==3 );
@@ -433,6 +434,7 @@ class DeepCNNCaffeImpl: public DeepCNN{
433
434
CV_Assert (int (inputImageList.size ())<=this ->minibatchSz_ );
434
435
CV_Assert (outputMat.isContinuous ());
435
436
437
+
436
438
#ifdef HAVE_CAFFE
437
439
net_->input_blobs ()[0 ]->Reshape (inputImageList.size (), this ->channelCount_ ,this ->inputGeometry_ .height ,this ->inputGeometry_ .width );
438
440
net_->Reshape ();
@@ -450,16 +452,19 @@ class DeepCNNCaffeImpl: public DeepCNN{
450
452
input_channels.push_back (netInputWraped);
451
453
// input_data += width * height;
452
454
inputData+=(this ->inputGeometry_ .height *this ->inputGeometry_ .width );
455
+
453
456
}
454
457
this ->preprocess (inputImageList[imgNum],preprocessed);
455
458
split (preprocessed, input_channels);
456
459
460
+
457
461
}
458
462
this ->net_ ->ForwardPrefilled ();
459
463
const float * outputNetData=net_->output_blobs ()[0 ]->cpu_data ();
460
464
this ->outputGeometry_ = Size (net_->output_blobs ()[0 ]->width (),net_->output_blobs ()[0 ]->height ());
461
465
int outputSz = this ->outputSize_ * this ->outputGeometry_ .height * this ->outputGeometry_ .width ;
462
466
467
+
463
468
// outputMat.resize(this->outputGeometry_.height * this->outputGeometry_.width);
464
469
float *outputMatData=(float *)(outputMat.data );
465
470
memcpy (outputMatData,outputNetData,sizeof (float )*outputSz*inputImageList.size ());
@@ -470,9 +475,10 @@ class DeepCNNCaffeImpl: public DeepCNN{
470
475
#ifdef HAVE_CAFFE
471
476
Ptr<caffe::Net<float > > net_;
472
477
#endif
473
- // Size inputGeometry_;
478
+ // Size inputGeometry_;//=Size(100,32);
474
479
int minibatchSz_;// The existence of the assignment operator mandates this to be nonconst
475
480
int outputSize_;
481
+ // Size outputGeometry_;
476
482
public:
477
483
DeepCNNCaffeImpl (const DeepCNNCaffeImpl& dn):
478
484
minibatchSz_ (dn.minibatchSz_),outputSize_(dn.outputSize_){
@@ -608,7 +614,7 @@ class DeepCNNOpenCvDNNImpl: public DeepCNN{
608
614
preProcessedImList.push_back (preprocessed);
609
615
}
610
616
// set input data blob in dnn::net
611
- net_->setInput (blobFromImages (preProcessedImList,1 , Size ( 100 , 32 ) ), " data" );
617
+ net_->setInput (blobFromImages (preProcessedImList,1 , this -> inputGeometry_ ), " data" );
612
618
613
619
float *outputMatData=(float *)(outputMat.data );
614
620
// Mat outputNet(inputImageList.size(),this->outputSize_,CV_32FC1,outputMatData) ;
@@ -625,9 +631,16 @@ class DeepCNNOpenCvDNNImpl: public DeepCNN{
625
631
#ifdef HAVE_DNN
626
632
Ptr<Net> net_;
627
633
#endif
628
- // Size inputGeometry_;
634
+ // hard coding input image size. anything in DNN library to get that from prototxt??
635
+ // Size inputGeometry_;//=Size(100,32);
629
636
int minibatchSz_;// The existence of the assignment operator mandates this to be nonconst
630
637
int outputSize_;
638
+ // Size outputGeometry_;//= Size(1,1);
639
+ // int channelCount_;
640
+ // int inputChannel_ ;//=1;
641
+ const int _inputHeight =32 ;
642
+ const int _inputWidth =100 ;
643
+ const int _inputChannel =1 ;
631
644
public:
632
645
DeepCNNOpenCvDNNImpl (const DeepCNNOpenCvDNNImpl& dn):
633
646
minibatchSz_ (dn.minibatchSz_),outputSize_(dn.outputSize_){
@@ -678,33 +691,17 @@ class DeepCNNOpenCvDNNImpl: public DeepCNN{
678
691
// std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
679
692
exit (-1 );
680
693
}
681
- // find a wa to check the followings in cv::dnn ???
682
- // CV_Assert(net_->num_inputs()==1);
683
- // CV_Assert(net_->num_outputs()==1);
684
- // CV_Assert(this->net_->input_blobs()[0]->channels()==1
685
- // ||this->net_->input_blobs()[0]->channels()==3);
686
- // this->channelCount_=this->net_->input_blobs()[0]->channels();
687
-
688
-
689
694
690
- // this->net_->CopyTrainedLayersFrom(modelWeightsFilename);
691
695
692
- // caffe::Blob<float>* inputLayer = this->net_->input_blobs()[0];
693
- // inputLayerId = net_->getLayerId('data');
694
-
695
- // inputLayerShape = net_->getLayerShapes(const MatShape& netInputShape,
696
- // inputLayerId,
697
- // std::vector<MatShape>* inLayerShapes,
698
- // std::vector<MatShape>* outLayerShapes) const;
699
- // should not be hard coded ideally
700
-
701
- this ->inputGeometry_ =Size (100 ,32 );// Size(inputLayer->width(), inputLayer->height());
702
- this ->channelCount_ = 1 ;// inputLayer->channels();
696
+ this ->inputGeometry_ =Size (_inputWidth,_inputHeight);// Size(inputLayer->width(), inputLayer->height());
697
+ this ->channelCount_ = _inputChannel;// inputLayer->channels();
703
698
704
699
// inputLayer->Reshape(this->minibatchSz_,this->channelCount_,this->inputGeometry_.height, this->inputGeometry_.width);
705
- // net_->Reshape();
706
- this ->outputSize_ =88172 ;// net_->output_blobs()[0]->channels();
707
- this ->outputGeometry_ = Size (1 ,1 );// Size(net_->output_blobs()[0]->width(),net_->output_blobs()[0]->height());
700
+ Ptr< Layer > outLayer= net_->getLayer (net_->getLayerId (net_->getLayerNames ()[net_->getLayerNames ().size ()-2 ]));
701
+ // std::vector<Mat> blobs = outLayer->blobs;
702
+
703
+ this ->outputSize_ =(outLayer->blobs )[1 ].size [0 ] ;// net_->output_blobs()[0]->channels();
704
+ // this->outputGeometry_ = Size(1,1);//Size(net_->output_blobs()[0]->width(),net_->output_blobs()[0]->height());
708
705
709
706
710
707
@@ -732,7 +729,7 @@ class DeepCNNOpenCvDNNImpl: public DeepCNN{
732
729
size_t minibatchSize=size_t (this ->minibatchSz_ );// temporary variable to avoid int to size_t arithmentic
733
730
classProbabilities.create (Size (int (outputSize),int (allImageVector.size ())),CV_32F);
734
731
Mat outputMat = classProbabilities.getMat ();
735
- printf ( " ekhane " );
732
+
736
733
for (size_t imgNum=0 ;imgNum<allImageVector.size ();imgNum+=minibatchSize)
737
734
{
738
735
size_t rangeEnd=imgNum+std::min<size_t >(allImageVector.size ()-imgNum,minibatchSize);
@@ -832,6 +829,22 @@ Ptr<DeepCNN> DeepCNN::createDictNet(String archFilename,String weightsFilename,i
832
829
}
833
830
834
831
namespace cnn_config {
832
+ std::vector<std::string> getAvailableBackends ()
833
+ {
834
+ std::vector<std::string> backends;
835
+
836
+ #ifdef HAVE_CAFFE
837
+ backends.push_back (" CAFFE, OCR_HOLISTIC_BACKEND_CAFFE" ); // dnn backend opencv_dnn
838
+
839
+ #endif
840
+ #ifdef HAVE_DNN
841
+ backends.push_back (" DNN, OCR_HOLISTIC_BACKEND_DNN" );// opencv_dnn based backend"
842
+ #endif
843
+ return backends;
844
+
845
+
846
+ }
847
+
835
848
namespace caffe_backend {
836
849
837
850
#ifdef HAVE_CAFFE
@@ -856,7 +869,7 @@ bool getCaffeAvailable()
856
869
{
857
870
return true ;
858
871
}
859
- #elif defined(HAVE_DNN)
872
+ #else
860
873
861
874
bool getCaffeGpuMode ()
862
875
{
@@ -873,32 +886,23 @@ void setCaffeGpuMode(bool useGpu)
873
886
bool getCaffeAvailable (){
874
887
return 0 ;
875
888
}
876
- bool getDNNAvailable (){
877
- return true ;
878
- }
879
889
890
+ #endif
880
891
881
- #else
892
+ }// namespace caffe
893
+ namespace dnn_backend {
894
+ #ifdef HAVE_DNN
882
895
883
- bool getCaffeGpuMode ()
884
- {
885
- CV_Error (Error::StsError," Caffe not available during compilation!" );
886
- return 0 ;
887
- }
888
896
889
- void setCaffeGpuMode (bool useGpu)
890
- {
891
- CV_Error (Error::StsError," Caffe not available during compilation!" );
892
- CV_Assert (useGpu==1 );// Compilation directives force
897
+ bool getDNNAvailable (){
898
+ return true ;
893
899
}
894
-
895
- bool getCaffeAvailable (){
900
+ # else
901
+ bool getDNNAvailable (){
896
902
return 0 ;
897
903
}
898
-
899
904
#endif
900
-
901
- }// namespace caffe
905
+ }// namspace dnn_backend
902
906
}// namespace cnn_config
903
907
904
908
class OCRHolisticWordRecognizerImpl : public OCRHolisticWordRecognizer {
@@ -931,6 +935,7 @@ class OCRHolisticWordRecognizerImpl: public OCRHolisticWordRecognizer{
931
935
getOutputs (buffer,nbOutputs,tmp);
932
936
classNum=tmp[0 ].wordIdx ;
933
937
confidence=tmp[0 ].probabillity ;
938
+
934
939
}
935
940
};
936
941
protected:
@@ -972,6 +977,7 @@ class OCRHolisticWordRecognizerImpl: public OCRHolisticWordRecognizer{
972
977
{
973
978
Mat netOutput;
974
979
this ->classifier_ ->classifyBatch (inputImageList,netOutput);
980
+
975
981
for (int k=0 ;k<netOutput.rows ;k++)
976
982
{
977
983
int classNum;
0 commit comments