A Convolution Neural Network forward code for caffe implemented in C++.
-
Features
- Load model converted from
*.caffemodel, model encrypt is supported. - Define layers' topology simply.
- Using Intel's TBB which makes convolution faster on multicore CPU.
- Supported layers currently:
INPUT,CONVOLUTION,POOLING,DENSE(orINNER_PRODUCT),RELU. - Easy to be compiled into a single
.exe,.dll, or.so, it can be executed without any additional library. - For example, you can use this code to break some captcha code like what was used at
xk.fudan.edu.cn.
- Load model converted from
-
How to use
- In
main.cppthere is a example:- First initialize a
CnnNetobjectnet, and then callnet.init('model', 'key'), which will load the model namedmodeland the blowfish key iskey. - Then call
net.forward('test.jpg', GRAY), which will read the filetest.jpginGRAYmode and do the net forward. - Finally you can get the result and process it by yourself, or use
net.argmax(). The functionargmaxis not really a argmax, and its result is not between 0 to 1, in fact, it will fetch all layers' max values whoseoutputis defined astrueand return them in vector. - Since this example does a captcha recognition job, I call a simple function in
utils.cppto convert the numbers in the vector above to letters.
- First initialize a
- Define net's topology.
- In
CnnNet.cpp, we can define net inCnnNet::init. JustnewaLayerConfigand push_back it's address. - If the
INPUTlayer's size(w, h) is set, all images will be resized when doing forward. Leave blank or set to0means pass the resize process. - Some information is read from model, and here we don't need to define them, for example, the kernel size of convolution.
- When you don't set a layer's parent, it will be set to its previously pushed layer. If you want to set it, you can use string or vector to set it.
- In
- The model can be converted using
model_convertor.py.
- In
-
Todo jobs
- Make it faster and faster, maybe support GPU.
- Separate the net's weights and the images calculated to make it threadsafe.
- Add more layer support, such as LRN.
- Zip the model file.
- More secure model encryption.
-
Copyright
- Open-source now. Please make sure you keep the copyright acknowledgement in source code.
- Not for commercial use(If you did this or you want to do so please contact me).