Answer To: digit recognition on MATLABDesign a fully convolutional neural network for digit classification and...
Kshitij answered on May 01 2021
DIGIT/checkToolboxes.m
function ret = checkToolboxes(req)
% reqToolboxes = {'Neural Network Toolbox'};
% checkToolboxes(reqToolboxes)
info = ver;
s=size(info);
flg = zeros(size(req));
reqSize = size(req,2);
for i=1:s(2)
for j=1:reqSize
if( strcmpi(info(1,i).Name,req{1,j}) )
flg(1,j)=1;
end
end
end
ret = prod(flg);
DIGIT/computer_generated.png
DIGIT/computer_generated_rotated.png
DIGIT/datasets/load_dataset.m
function [XTrain, YTrain, XTest, YTest] = load_dataset( name, dir )
if( ~exist( 'dir', 'var' ) )
dir = [fileparts(mfilename('fullpath')),'/'];
end
if( dir(end) ~= '/' )
dir = [dir, '/'];
end
if( strcmpi( name, 'mnist' ) )
[XTrain, YTrain, XTest, YTest] = load_mnist( dir );
elseif( strcmpi( name, 'cifar10' ) )
[XTrain, YTrain, XTest, YTest] = load_cifar10( dir );
else
msg = sptrinf( 'Could not find: %s', name );
error(msg);
end
end
DIGIT/datasets/load_mnist.m
function [XTrain, YTrain, XTest, YTest] = load_mnist( dir )
url = 'http://yann.lecun.com/exdb/mnist/';
datasetfile = 'mnist.mat';
if( ~exist( 'dir', 'var' ) )
dir = [fileparts(mfilename('fullpath')),'/'];
end
if( dir(end) ~= '/' )
dir = [dir, '/'];
end
if( isfile( [dir, datasetfile] ) )
load( [dir, datasetfile] );
else
fprintf( 'Downloading mnist dataset. It may takes several minitus.\n' );
filename = 'train-images-idx3-ubyte';
websave([dir,filename,'.gz'], [url,filename, '.gz']);
gunzip([dir,filename, '.gz']);
XTrain = readMnistX([dir,filename]);
filename = [ dir, filename, '*' ];
delete(filename);
filename = 'train-labels-idx1-ubyte';
websave([dir,filename,'.gz'], [url,filename, '.gz']);
gunzip([dir,filename, '.gz']);
YTrain = readMnistY([dir,filename]);
filename = [ dir, filename, '*' ];
delete(filename);
filename = 't10k-images-idx3-ubyte';
websave([dir,filename,'.gz'], [url,filename, '.gz']);
gunzip([dir,filename, '.gz']);
XTest = readMnistX([dir,filename]);
filename = [ dir, filename, '*' ];
delete(filename);
filename = 't10k-labels-idx1-ubyte';
websave([dir,filename,'.gz'], [url,filename, '.gz']);
gunzip([dir,filename, '.gz']);
YTest = readMnistY([dir,filename]);
filename = [ dir, filename, '*' ];
delete(filename);
save([dir,datasetfile], 'XTrain', 'YTrain', 'XTest', 'YTest');
end
XTrain = double(XTrain) / 255.0;
YTrain = categorical(YTrain);
XTest = double(XTest) / 255.0;
YTest = categorical(YTest);
end
function X = readMnistX( filename )
fid = fopen(filename, 'r', 'b');
header = fread(fid, 1, 'int32');
if header ~= 2051
error('Invalid image file header');
end
count = fread(fid, 1, 'int32');
h = fread(fid, 1, 'int32');
w = fread(fid, 1, 'int32');
X = fread(fid, h*w*count, 'uint8');
X = reshape( X, [h,w,1,count] );
X = permute( X, [2,1,3,4] );
X = uint8(X);
fclose(fid);
end
function Y = readMnistY( filename )
fid = fopen(filename, 'r', 'b');
header = fread(fid, 1, 'int32');
if header ~= 2049
error('Invalid image file header');
end
count = fread(fid, 1, 'int32');
Y = fread(fid, count, 'uint8');
Y = reshape( Y, [count,1] );
Y = uint8(Y);
fclose(fid);
end
DIGIT/datasets/load_train1000.m
function [XTrain, YTrain, XTest, YTest] = load_train1000( name, dir )
if( ~exist( 'dir', 'var' ) )
dir = [fileparts(mfilename('fullpath')),'/'];
end
if( dir(end) ~= '/' )
dir = [dir, '/'];
end
if( strcmpi( name, 'mnist' ) )
[XTrain0, YTrain0, XTest, YTest] = load_mnist( dir );
[XTrain, YTrain] = extract(XTrain0, YTrain0, 10, 100);
elseif( strcmpi( name, 'cifar10' ) )
[XTrain0, YTrain0, XTest, YTest] = load_cifar10( dir );
[XTrain, YTrain] = extract(XTrain0, YTrain0, 10, 100);
else
msg = sptrinf( 'Could not find: %s', name );
error(msg);
end
end
function [X, Y] = extract(X0, Y0, nb_classes, nb_per_class)
s = size(X0);
X = X0(:,:,:,1:nb_classes*nb_per_class);
Y = Y0(1:nb_classes*nb_per_class,:);
k = 1;
n = zeros(nb_classes,1);
for i=1:size(X0,1)
c = int32(Y0(i,1));
if( n(c) < nb_classes )
X(:,:,:,k) = X0(:,:,:,i);
Y(k,:) = Y0(i,:);
n(c) = n(c) + 1;
k = k + 1;
if( k > nb_classes*nb_per_class )
break;
end
end
end
end
DIGIT/datasets/mnist.mat
XTrain:[4D uint8 array]
YTrain:[60000x1 uint8 array]
XTest:[4D uint8 array]
YTest:[10000x1 uint8 array]
DIGIT/Digit classification.docx
Digit classification and recognition using convolution neural network using MNISt dataset , We have trained the data using CNN with manipulation of training options to get desired training option. NOn-maximum suppression and thresholding is implementation to obtain digit predictions, MNIST dataset of 60,000 small square 28×28 pixel grayscale images of handwritten single digits between 0 and 9.
Method:-
Convolution neural network could considered as gradable NN which generally works in extraction of features of images by choosing options of convolving input with the help gaggle of kernel filters. After that the process of pooling of obtained feature is executed and filter resolution of next layer following, Now implementing convolution neural network algorithm.
Above given condition tells us about the lth...