File Exchange

image thumbnail

Convolution Neural Network - simple code - simple to use

version 1.4.0.0 (958 KB) by Ashutosh Kumar Upadhyay
This is a simple to use code of Convolution Neural Network -a deep learning tool.

139 Downloads

Updated 16 Feb 2017

View License

I wrote this code while learning CNN. It support different activation functions such as sigmoid, tanh, softmax, softplus, ReLU (rect). The MNIST example and instructions in BuildYourOwnCNN.m demonstrate how to use the code. One can also build only ANN network using this code. I also wrote a simple script to predict gender from face photograph totally for fun purpose. It predicts gender male or female and also predict if face is more similar to monkey rather than male or female human - totally for fun purpose.

Cite As

Ashutosh Kumar Upadhyay (2020). Convolution Neural Network - simple code - simple to use (https://www.mathworks.com/matlabcentral/fileexchange/59223-convolution-neural-network-simple-code-simple-to-use), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (46)

Geetha G

Please send the cnn code (to do a training on certain values ​​and then test those values)

Toshi Sinha

Sir,

Thanks for sharing the code. Can this code be changed in such a way that the output is compared directly by inpuy image and not labels.

Toshi Sinha

@Ashutosh Kumar Upadhyay
hi, can I use this code for voiceprint recognition? What functions do you need to build a CNN for voiceprint recognition?

Hello,
i am trying to train the code from scratch with a new batch of image (for different prediction type).
The question:
- to get the trained file "my_face_cnn5.mat" , shouldn't we add the function "savecnn.m" at the end of the script of "traincnn.m" or "Example_MNIST_CNN"?
Thank you

Shachi P

Thank you. It is very helpful for beginners like me.

tu

Sihua Fu

Hi, does anyone know why we don't multiply outputs by sigmoid's derivative in back propagation for this code? From my understanding, if sigmoid is used for last layer, we need to consider its derivative in back propagation. I tried to change cnn.CalcLastLayerActDerivative =1, but then error rate increased so I'm not sure whether I got it right.

The following code initialized cnn.CalcLastLayerActDerivative =0 in traincnn.m:

if cnn.loss_func == 'cros'
if cnn.layers{cnn.no_of_layers}.act_func == 'soft'
cnn.CalcLastLayerActDerivative =0;
elseif cnn.layers{cnn.no_of_layers}.act_func == 'sigm'
cnn.CalcLastLayerActDerivative =0;
end
end

swathi

Sir, I have a query on featuremap calculation. when you refer featuremap{k}, what does this mean? If I have 5 samples of 10 users, total of 50 sample, then in the convolution layer how many feature maps i will get? and how could I access them?

I ran my code and it gave me the error
Error using *
Inner matrix dimensions must agree.

Error in ffcnn (line 75)
cnn.layers{i}.outputs =
applyactfunccnn(cnn.layers{i}.W*zz +
repmat(cnn.layers{i}.b, 1, size(zz,2)),
cnn.layers{i}.act_func, 0);

As per your directions I tried setting a breakpoint at line 75 of ffcnn and found the size(zz) = 3808*16 and size(cnn.layers{i}.W}=16*4200. So its evident that this is where the problem arises but don't know what to do.
Please help.

GOKUL RAJ

i, i tried running the above code in Matlab2015a and got the error eventhough the file exists in the directory.

reading dataset...
Error using imread (line 349)
File "E:MATLABCNN\AshuCNN\Internet_Images\f(1).jpg" does not exist.

Error in Predict_gender_CNN (line 38)
I=imread(fullfile(Datapath1, st));
could you help me out of this

Drew IB

Shanthini s

hi, i tried running the above code in Matlab2014a and got the error eventhough the file exists in the directory.

reading dataset...
Error using imread (line 349)
File "C\Users\Lenovo\Downloads\AshuCNN\Internet_images" does not exist.

Error in Predict_gender_CNN (line 38)
I=imread(Datapath,'C\Users\Lenovo\Downloads\AshuCNN\Internet_images');

could you help me out of this

how to create .mat file ? plzz answer fast its urgent

Hi ashutosh ,

I want to train a pretrained network vgg16 with more then 1 dataset (for handwriting Recognition )at a time, can u please help me regarding this as u state How to build cnn step- wise

thank you !

DEENA hijam

Hello sir, in your example, is it that the network has been trained using the MNIST dataset and this trained network is being used for gender prediction? I am trying to use the network for my images of handwritten characters of a different script. Can i use the same trained network to test my dataset of images?

Hi Silpa, please use size() function on each individual element so that source of error can be identified. Put a breakpoint on that line and check size(zz), size(cnn.layers{i}), size(cnn.layres{i}.b), size(cnn.layers{i}.W*zz), size(repmat(cnn.layers{i}.b,1,size(zz,2)). Thanks.

SILPA C S

Sir,I am doing a project on lung cancer detection using CNN.First time I am trying CNN.I have train images of 512x512 size.I just tried your code for knowing CNN coding.
I am facing error Error using *
Inner matrix dimensions must agree.

Error in ffcnn (line 65)
cnn.layers{i}.outputs = applyactfunccnn(cnn.layers{i}.W*zz + repmat(cnn.layers{i}.b, 1, size(zz,2)),
cnn.layers{i}.act_func, 0);

Where I have to correct to eliminate this.Please help

1. rescale(x) = ( x - min(x(:)) )/ (max(x(:)) - min(x(:))). So if I forget to replace rescale(x) with this equation, please replace it. for 8 bit gray value image,it is simplly divided by 255.
2. gradient_checker() was used to test cnn implementation, and aftet that it has no use. It has no use in training & testing phase of cnn images.
3. for more featured use, please use theano/tensorflow/caffe etc.. THis code is written for only understanding the basic cnn implenataion and their inner working.
thanks

For the error of matrix dimensions, please use the size() function to get the size of matrices involved in the equation. Then it can give clue where mistake is happening and what to do for it.

Aarthi A

Hi sir,
How to create a network with padding?

Aarthi A

Hi sir,
Why you are using gradient_checker(cnn, xx, yy); function ini cnn?

hi there, i am trying to run the Predict_gender_CNN.m but u get an error that says" Undefined function 'rescale' for input arguments of type 'double'." i looked through the files but i didnt find a rescale user function, i hope you can help with that.

Ashiekaa a

How can I create train-images, train-labels, t10k-labels, t10k-images pls send link. Thanks

inam ramis

Hi Hi Ashutosh Kumar, I tried to implement your code using my self designed STFT based images for epileptic seizure detection. I had encountered all the issues but at the end I am stuck with this error

Matrix dimensions must agree.
Error in bpcnn (line 13)
er = ( cnn.layers{cnn.no_of_layers}.outputs - yy);
Error in traincnn (line 58)
cnn = bpcnn(cnn,yy);

Kindly eleborate why this error occurs and how can I resolve it?

Hi, As Has,
Yes to understand the basic working of CNN, you can use my code and later after the understanding of it, you may choose any library.

sir i am a beginner to use the convolution neural network, i am trying to use the matlab 2017Rb functions ( convolution2dlalyer , maxpoolinglayer,......fullyconectionlayer and so on)
but i don't know how to build the backpropagation error. and also i didn't use any library like teano or caffee ..should i start from your code , and i note that you didn't you the existing function in matlab 2017b

Duc Hai Ta

thank you!

Hi syahril, 506/batch_size = integer, 452/batch_size = integer. Change the value of batch_size accordingly. it seems batch_size=2 in your case. If you reduce no. of data to the nearest tens (i.e. to the value 500 & 450), then there will be various options for batch_size. Although, batch_size=2 may work well and you can try it. If it doesn't then, go for reduction of data, and put batch_size =10/5/or something else.

syahril Don

Hi Ashutosh Kumar, I tried to implement your code using my own data set (Train data = 28x28x506double),(train label=2x506double),(Test data=28x28x452double),(test label=2x452double) .However, I have encountered an error:
Error using traincnn (line 17)
no_of_batches should be integer

Hi Satish, I wrote code for learning purpose. The instructions how to use code is given in a file named - 'HowToBuildYourOwnCNN.m' and also read comments below. You may use code for a simple application which will require some sequential layers. For a complex application, it is better to use a standard tool such as theano, tensorflow, caffe, which will be faster too.

Namashkar Ashoutosh, i am working on a script identification from Indian document images, i got dataset like 1000 of english in the size of 128x128,512x512 each size have a 1000 images, like this i got 11 6 languages dataset. Now i wish to apply convolutional neural network on my dataset for identify the scripts. can you pls help me for how to use your code for my work. Thanks in advance.

Hi Ean, thanks for writing. While writing your own network, please follow the instructions given in HowToBuildYourOwnCNN.m. Since network contains pool layers and fully connected layers, the size of input images should be fixed and all images should be rescaled to this size. Please calculate the size of featuremaps (outputs) at each conv and pool layer. For conv layer it will be : input size - filter size +1. The subsampling factor of pool layer should divide fully the output size of previous conv layer.

In ffcnn (line 75):
Here, W is weight matrix of first fully connected layer and its size is [no of nodes no of input points]. no of input points is deduced from output of its previous layer (which is most probably pool layer) and is equal to (no of feature maps * width of feature map * height of feature map). The size of zz should be [no of input points 1].
Hope it helps.

Hi Sir, I have downloaded your code and tried to implement the convolution neural network. However, I have encountered an issue when running this code. The error is as below:

Error using *
Inner matrix dimensions must agree.

Error in ffcnn (line 75)
cnn.layers{i}.outputs = applyactfunccnn(cnn.layers{i}.W*zz + repmat(cnn.layers{i}.b, 1,
size(zz,2)), cnn.layers{i}.act_func, 0);

May I know what is W and what is its size? Thank you.

Nick Tsui

Hi Abo, sorry for your inconvenience. That line is not required and should be commented. (This line was added during testing and I forget to comment it). In newer versions of Matlab, setDefaultStream is no more supported, and setGlobalStream is introduced in place of it. But we need not to worry for our case, this line should be commented in our case. Thanks for writing me, and again sorry for inconvenience.

Abo

Hi, the error: The class RandStream has no Constant property or Static method named 'setDefaultStream' happens when working with Matlab2016b!!! no idea please?

There are two examples : Example_MNIST_CNN.m and Predict_gender_CNN.m

After downloading, add location to matlab path, and open the above files in matlab and run. if any problem, please post here......

angel lerma

hi, how I can run the script ?? thks....

Added an example of predicting gender using face photographs. Trained CNN network on internet images is included.

amr essam

Let me try!Thank you a lot

jie liu

Let me try!Thank you a lot

Updates

1.4.0.0

please see comment. one unnecessary line has been commented.

1.3.0.0

logo image updated

1.2.0.0

Funny example of predicting genders from face photographs is added. Network has already trained, and it can predict the gender. Just enjoy it.

1.1.0.0

Tags corrected.

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