File Exchange

image thumbnail

CNN - Convolutional neural network class

version (613 KB) by Mihail Sirotenko
This project provides matlab class for implementation of convolutional neural networks.


Updated 24 Oct 2012

View Version History

View License

NOTE: Next versions of this library is hosted at
The reason is that now it's rather C++/CUDA library with Matlab front-end than a Matlab library.

This project provides matlab class for implementation of convolutional neural networks. This networks was developed by Yann LeCun and have sucessfully used in many practical applications, such as handwritten digits recognition, face detection, robot navigation and others (see references for more info). Because of some architectural features of convolutional networks, such as weight sharing it is imposible to implement it using Matlab Neural Network Toolbox without it's source modifications. That's why this class works almost independently from NN toolbox (coming soon full independence).

This release includes sample of handwritten digits recognition using CNN. If you just want to try it run cnet_tool. You'll see a simple GUI. It loads pretrained convolutional neural net from cnet.mat and recognizes image of digit either painted in painting area or downloaded from MNIST database.

Further information about the library can be found here:
and here:

Cite As

Mihail Sirotenko (2021). CNN - Convolutional neural network class (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (76)

yunil Z

Ahmed Zaafouri

The processing time of this algorithm is very high.
How can generalize it for 26 classes of English character
Thank you for sharing

Ahmed Zaafouri


thanks for sharing

tom tom


you must give input as 32 32, many places are hard coded as in size 32 32.

vijaya vennilaa

i am beginner learner of CNN,i need the codings for learning of patch quality comparator for single image dehazing using kindly help me for this....
thanking you


It really help me,i'm a beginning leaner of neural network,i read your code and learn each part,it really useful.Thank you!


Young Ma

Thanks for your sharing.

ping hsuan cheng


nice job,I hope cnn will help with my graduate paper work!thanks for CNN and myCNN.


you need to download all the four folders and save in MNIST folder. Then it can work.


nice for learning

naadiya khudabux

i have created the folder and save data in it still am getting the erro," Error using readMNIST (line 74)
Test images of MNIST not found. Please download it from and put to
./MNIST folder ".
how can i resolves this

Josué LIma

Would you have an example please?

Song Song

Hiba Rouahi

Dear Mihail Sirotenko,
Do you have an answer to this question ( can the CNN train with the features extracted from the images instead of the images themselves). In input of CNN, we have features?

mehmood ul hasan

Dear Mihail Sirotenko pleas this code is useful for spectral classification of some species pleas rep

Jun-Yan Chen


Sheon yan

Bibhu Prasad Pala

moahaimen talib

can i use it for pattern recognition ? and how to run it please



deep blue

xs chen

shabaz khan

may I get the code for finding false positive and false negative?


How can I implement solar radiation forecasting using CNN. Can anyone help me?

Murugesan Thangavel

I wish to run cnn in matlab 2016a for training 40 images using trainnetwork from help documentation. But I get error
convnet = trainNetwork(trainDigitData,layers,options); % running the CNN model
Caused by:
The CUDA driver could not be loaded. The library name used was 'nvcuda.dll'. The error was:
The specified module could not be found.
I don't want to use cuda now. What to do?

Mridusmita Sharma

how can i use this for speech classification?

Yan Li

Yan Li

Yifei Wu

wzy w

I apllied it to traffic sign recognition,it classified all classes to a same class,have you had any experience with this - if so what parameters might you suggest I change?How can


NetDragon wanglong

I want to change the last layer(output layer) to use linear regression, how to do this?


I need the structure of the CNN class, i.e. it's methods and functions, and it's parameters. I need information like the components: numSLayers, numInputs, Perf, num_dec which are found with cnet object in the form of cnet.numSLayers, cnet.numInputs, cnet.Pref ....etc


I have a problem with understanding the actual architecture of this network. Usually, LeCun et al have used different weights for the connections from different feature maps of a previous layer (something that looks like 3D kernel). Therefore, the number of weights of a convolution layer (assuming full map of connections) is kernelHeight*kernelWidth*numFeatMapsLayer(k)*numFeatMapsLayer(k-1). I am not sure, but it seems that there are kernelHeight*kernelWidth*numFeatMapsLayer(k) different weights used in this program. Does it means that the connections from different feature maps of a previous layer to a particular feature map of the next layer have the same weights? Or maybe, I misunderstand something?


I want to use this toolbox on my own sound data set. Each spectogram equivalent to 1 image has dimensions 13x500, where as each image in MNIST has dimensions 28x28. I want to change the input width and height to 13 and 500 respectively. How do I go about doing so?

pan junfu

I apllied it to traffic sign recognition,it classified all classes to a same class,have you had any experience with this - if so what parameters might you suggest I change?How can I train the cnn with the features extracted from the images instead of the images themselves?
Where should I change?


Hello, I have run train_cnn on Octave, however, I got a error message "max_recursion_depth exceeded". Is anybody know/experience same problem? How to solve it? Thanks.


Mihail Sirotenko

In this pure matlab version there's no max pooling. But you can find it in C++/CUDA version, which is on bitbucket repository.


I've been searching around for a bit but I can't find the max pooling layer code. Can someone point me in the right direction?


Hi, I am running the demo train-cnn which is provided to train the network from Dr. Lecun's paper. I do full training for over 20 epochs. The best MCR I get is .12 which corresponds to 12% error. How can I improve this? (The goal is to get .8% error)

Mihail Sirotenko

2Eliud Gonzalez:
1. the output of CNN is saved to workspace variable sinet. You can use matlab function "save" to save it on disk.
2. Hessian computation controlled on line 147:
sinet.HcalcMode = 0;
Default value 0 means running estimate of hessian. In this case you'll not notice it in gui. If you change it to 1, gui progress bar will show the hessian recomputation.

Eliud Gonzalez

Thanks, when training the CNN, how do i save it, or it saves itself? and why Hessian calculation is always 0?

Mihail Sirotenko

2 Eliud Gonzalez: usually this means that you don't have MNIST dataset. It is not distributed with the library, but you can download in from
Please dowload and put the files into the MNIST folder.

Eliud Gonzalez

I get this errors when running train_cnn.m
??? Error using ==> fread
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in ==> readMNIST at 25
magicNum = fread(fid,1,'int32'); %Magic number

Error in ==> train_cnn at 30
[I,labels,I_test,labels_test] = readMNIST(1000);


Eliud Gonzalez

What can i do if i open train_cnn.m and click run, and nothing happens, do i need to install CUDA, i'm sorry i'm new in this world :(

Mihail Sirotenko

2 yin: yes, it can. Actually you can feed in any data not only images.
2 Xiaolei Hu: As far as I know there's no support of Conv Nets in latest Matlab NN Toolbox.

Xiaolei Hu

Does anybody know whether the latest Matlab has changed its NN toolbox so CNN can be implemented?


can the cnn train with the features extracted from the images instead of the images themselves?

Mihail Sirotenko

Unfortunately I don't have Matlab 2011b. It seems that they changed something in NN toolbox. I'm going to remove the dependencies from NN toolbox in next release, which I'm trying to finalize now.

Atanasko Atanasov

I try to train the network but I get error:
Undefined function or variable 'boiler_transfer'.
Error in tansig_mod (line 64)

I am using MATLAB R2011b. Any help?

Mihail Sirotenko

Full training cycle needs about 10 epochs on full training set with progressively decreasing theta. As far as I understand you did 1 epoch.

Bo Hu

This is a great job. However I have a question. I test this program using the MNIST handwritten digit database. The mcr rate is very high (about 15%) even I train the cnn using 10000 input. If I tried to train the cnn using 60000 input, then the program would took fairly long time, about several hours to finish. Anyway, the mcr is always about 15%. I wonder what parameter we should change to make the prediction accuracy higher. Thanks a lot.

Mihail Sirotenko

Regarding several last questions. I'm going to put new release of the lib which will contain example with faces and would be more convenient to use with various data. It already works in general. But I need a week or two more to finalize it since I do it in my free time.

chipo josé

please can any one help as to adapt this code to face recognition

Zhipan Ren

Aron Sceidt

seeing it now... looks great :) is it possible to load a jpg or png in some way? what would be required?

Mihail Sirotenko

2Gaurav. It means that file train_cnn.m have comments for almost every line of code, so you can find parameter you want to change and actually change it before start training. Basically train_cnn.m contains settings for network architecture (number of layers, number of neurons etc)


in the read me file to train the network "
If you interested in training you should open train_cnn.m, set all parameters following to comments and start learning by runing it."
i cant understand the "set all parameters following to comments and start learning by runing it." part

please help

Jose M. Alvarez

Is it possible to train using color images? (from the example it says it is not). However, the version from Nikolay Chumerin can be trained using color patches.

Silvio Filipe

Sebastien PARIS

Mihail Sirotenko

Actually I had a plans to adapt this to face recognition, but unfortunately there're still only plans, because now I have absolutely no time to develop it.
So I'd advice to look closely on preprocessing algorithms and try to make architecture similar to face detection papers advices.


Excellent work!

I've attempted to get this working on face recognition but without success. The MCR remains very high. Have you had any experience with this - if so what parameters might you suggest I change?

pupu QQ

it's ok ,but only numbers.high quality .

Jonathan Masci

Mihail Sirotenko

Thanks a lot for the valuable comment. First bug is a result of merging different versions of CNN used in my experements. About second bug, the problem is in the error calculation. In the new version I'll fix these bugs.


22.10.2009 - in current Ver_0.72
- in file train_cnn.m Conv. Layer 4 should have 16 kernels not 6 (sinet.CLayer{4}.numKernels = 6 ) or sinet.CLayer{4}.ConMap shouldn't be 6x16 in size.
- net training result doesn't return "minimal error" net state. I.e. I can see lover error during training than final is. However I apply CNN to different task with my own image set.

Nikolay Chumerin

Well done! Good job. I think this is the first publicly available implementation of CNN training in Matlab. The source code is written in a pretty good style with extensive comments, which are really useful for such complex classes. This submission is an asset for computer vision Matlab community.

Mihail Sirotenko

New version coming soon. It will include:
1. Trained network for experements.
2. Simple GUI, visualising digits recognition.
3. Improoved performance.
4. Independence from NN Toolbox.

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: myCNN

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!