HOG feature Extraction with CNN for Handwritten Recognition
3 views (last 30 days)
Show older comments
Hi im trying to combine HOG feature extraction with CNN and below is the script that im working on right now. But the script gave me an error saying:
Error using trainNetwork (line 183)
Number of observations in X and Y disagree.
Error in HOGfeature (line 62)
net = trainNetwork(trainingfeatures,trainingLabels,layers,options); %Network Training
Can somone help me with this btw the dataset that im using for this project are MNIST.
close all
clear
clc
path1='D:\CNN test\Imagedb\HOGtrainset';
path2='D:\CNN test\Imagedb\HOGtestset';
traindb = imageDatastore(path1,'IncludeSubfolders' ,true,'LabelSource','foldernames');
testdb = imageDatastore(path2,'IncludeSubfolders' ,true,'LabelSource','foldernames');
%training
img = readimage(traindb,1);
CS=[8,8]; %cellsize
[hogfv,hogvis] = extractHOGFeatures(img,'CellSize',CS);
hogfeaturesize = length(hogfv);
totaltrainimages = numel(traindb.Files);
trainingfeatures = zeros(totaltrainimages, hogfeaturesize,'single');
for i = 1:totaltrainimages
img = readimage(traindb,i);
trainingfeatures(i, :) = extractHOGFeatures(img,'CellSize',CS);
end
trainingLabels = traindb.Labels;
%% Building CNN
layers=[
imageInputLayer([28 28 1],'Name','Input')
convolution2dLayer(3,8,'Padding','same','Name','Conv_1')
batchNormalizationLayer('Name','BN_1')
reluLayer('Name','Relu_1')
maxPooling2dLayer(2,'Stride',2,'Name','MaxPool_1')
convolution2dLayer(3,16,'Padding','same','Name','Conv_2')
batchNormalizationLayer('Name','BN_2')
reluLayer('Name','Relu_2')
maxPooling2dLayer(2,'Stride',2,'Name','MaxPool_2')
convolution2dLayer(3,32,'Padding','same','Name','Conv_3')
batchNormalizationLayer('Name','BN_3')
reluLayer('Name','Relu_3')
maxPooling2dLayer(2,'Stride',2,'Name','Maxpool_3')
convolution2dLayer(3,64,'Padding','same','Name','Conv_4')
batchNormalizationLayer('Name','BN_4')
reluLayer('Name','Relu_4')
fullyConnectedLayer(10,'Name','FC')
softmaxLayer('Name','Softmax');
classificationLayer('Name','Output Classification');
];
%Igraph = layerGraph(layers);
%plot(Igraph); %Plotting Network Structure
%-----------------------------------Training Options-----------------
options = trainingOptions('sgdm','InitialLearnRate',0.01,'MaxEpochs',4,'Shuffle','every-epoch','ValidationData',testdb,'ValidationFrequency',30,'Verbose',false,'Plots','training-progress');
net = trainNetwork(trainingfeatures,trainingLabels,layers,options); %Network Training
Ypred = classify(net,testdb); %Recognizing Digits
YValidation = testdb.Labels; %Getting Labels
accuracy = sum(Ypred == YValidation)/numel(YValidation); %Finding %age accuracy
0 Comments
Answers (0)
See Also
Categories
Find more on Recognition, Object Detection, and Semantic Segmentation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!