10/01/2015

Deep learning Library to work easily with opencv on window environment.

I was wrapping caffe library to use easy with opencv and window os environment(named MareDeepDLL).

MareDeepDll is referenced on
http://caffe.berkeleyvision.org/installation.html
https://initialneil.wordpress.com/


The dll is made on this environment
Window 10 64bit,
VS 2013 64bit
OpenCV 3.0 64bit
cuda 6.5 64bit
tbb 64bit
..
All dependency is as follows:


This code is example to use the dll
...
#include < iostream>  
#include < stdio.h>
#include < vector>
#include < time.h>

#include <  opencv2\opencv.hpp>    
#include <  opencv2\core.hpp>  
#include <  opencv2\highgui.hpp>   
#include <  opencv2\videoio.hpp>    
#include <  opencv2\imgproc.hpp>  



#include "DeepDll_B.h"

#ifdef _DEBUG            
#pragma comment(lib, "opencv_core300d.lib")  
#pragma comment(lib, "opencv_highgui300d.lib")    
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_imgproc300d.lib") //line, circle  
#else    
#pragma comment(lib, "opencv_core300.lib")  
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_imgcodecs300.lib")  
#pragma comment(lib, "opencv_imgproc300.lib") //line, circle  

//DEEP lib
#pragma comment(lib, "MareDeepDLL.lib")  
#endif   

using namespace cv;
using namespace std;

void main()
{

 //DEEP Class
 MareDeepDll_B cDeep;

 //load model and structure
 cDeep.SetNet("lenet_test-memory-1.prototxt", "lenet_iter_10000.caffemodel");
 //gpu using on
 cDeep.GPU_using();
 

 for (int i = 1; i <  14; ++i)
 {
  // time check..
  unsigned long AAtime = 0, BBtime = 0;
  AAtime = getTickCount();


  //make file name
  char str[256];  
  sprintf_s(str, "%d.jpg", i);
  printf("%s\n", str);
  

  //img load and preprocessing
  Mat img = imread(str);
  resize(img, img, Size(28, 28));
  cvtColor(img, img, CV_BGR2GRAY);  


  ////////////
  //classify
  vector< double> rV;
  //image and class num (caution!! class num is dependented by learning condition.) lenet is classify one number in 10 digits.
  rV = cDeep.eval(img, 10);
  /////////////

  //result out
  for (int i = 0; i <  rV.size(); i++) {
   printf("Probability to be Number %d is %.3f\n", i, rV[i]);   
  }

  // processing time check.
  BBtime = getTickCount();
  printf("%.2lf sec / %.2lf fps\n", (BBtime - AAtime) / getTickFrequency(), 1 / ((BBtime - AAtime) / getTickFrequency()));

  //draw
  namedWindow("test", 0);
  imshow("test", img);
  waitKey(0);
 }

}


...
Lenet model was used to test the deep learning classification.
Many other models are introduced on github model zoo.
https://github.com/BVLC/caffe/wiki/Model-Zoo
You can apply other case, on code cDeep.SetNet("lenet_test-memory-1.prototxt", "lenet_iter_10000.caffemodel"); , first param means model structure and second param means the result of deep learning.




If you request to Google Plus to me, I will send the dll with the application code(project).




4 comments:

  1. Hii, can you post the training process? I have some problems to set training parameters

    ReplyDelete
    Replies
    1. I plan to post the code about learning method of deep learning.
      However, it is difficult for the parameter settings.
      because it require understanding of deep learning algorithm.
      I think tensor flow is good to study deep learning.
      I plan to post later about tensor flow.

      Can you open your learning data and parameter?
      Then I also try learning.

      Thank you.

      Delete
  2. ์•ˆ๋…•ํ•˜์„ธ์š”? ๋ธ”๋กœ๊ทธ ๋ฐ openCV๊ฐ•์ขŒ ์œ ์šฉํ•˜๊ฒŒ ์ž˜๋ณด๊ณ ์žˆ์Šต๋‹ˆ๋‹ค!! ๋‹ค๋ฆ„์ด์•„๋‹ˆ๋ผ caffe์„ค์น˜์— ์˜๋ฌธ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ github์—์„œ ๊ตฌ์„ฑ์š”์†Œ๋“ค์„ ๋ฐ›์•˜๋Š”๋ฐ caffe๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด opencv์„ค์น˜ํ• ๋•Œ ์‚ฌ์šฉํ•œ cmakeํ”„๋กœ๊ทธ๋žจ์—์„œ ๊ฒฝ๋กœ์ง€์ •ํ•ด์ฃผ๊ณ  ๋˜‘๊ฐ™์ด ์ง„ํ–‰ํ•˜๋ฉด ๋˜๋‚˜์š”? ๊ทธ๋ฆฌ๊ณ  include ๋””๋ ‰ํ† ๋ฆฌ๋Š” ๋ฐ”๋กœ ์ฐพ์•˜๋Š”๋ฐ caffe_libs๋Š” ๋ฌด์Šจ ํŒŒ์ผ๋กœ ์ง€์ •ํ•ด์ค˜์•ผํ• ์ง€ ๋ชจ๋ฅด๊ฒ ๋„ค์š”.. ์•„๋ฌด์ชผ๋ก ๋‹ต๋ณ€๊ธฐ๋‹ค๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

    ReplyDelete
  3. ์•ˆ๋…•ํ•˜์„ธ์š”. ์ €๋„ opencv ๋นŒ๋“œํ• ๋•Œ caffe๋ฅผ ๋„ฃ์–ด์„œ ํ•ด๋ณธ ์ ์ด ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฐ˜๋Œ€๋กœ caffe ๋นŒ๋“œํ• ๋•Œ, opencv๋ฅผ ํฌํ•จ์‹œ์ผœ์„œ ๋นŒ๋“œํ•œ ์ ์€ ์žˆ์Šต๋‹ˆ๋‹ค.
    ๊ทธ๋•Œ ๋‚จ๊ธด ๊ธ€์€ http://study.marearts.com/2015/10/deep-learning-library-to-work-easily.html, ์ฐธ๊ณ ํ•˜์„ธ์š”.
    ์—ฌ๊ธฐ https://initialneil.wordpress.com ์†Œ๊ฐœ๋œ ๋ฐฉ๋ฒ•์„ ๋”ฐ๋ผ ํ–ˆ์Šต๋‹ˆ๋‹ค.
    ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฒŒ 2015๋…„ ์ž๋ฃŒ๋ผ์„œ ๋” ์ตœ์‹  ์ž๋ฃŒ๋ฅผ ์ฐพ์•„ ๋ณด๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    ์ €๋„ opencv์— caffe๋ฅผ ๋„ฃ์–ด์„œ ๋นŒ๋“œ๋ฅผ ํ•ด๋ณด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.
    ์„ฑ๊ณตํ•˜๋ฉด ์ ˆ์ฐจ๋ฅผ ๋ธ”๋กœ๊ทธ์— ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
    changhun ๋‹˜๋„ ์ข‹์€ ์ •๋ณด ์žˆ์œผ๋ฉด ์•Œ๋ ค์ฃผ์„ธ์š”.
    ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

    ReplyDelete