Я обучал CNN, используя наборы изображений собственных персонажей, которые я создал сам.И я следую за https://in.mathworks.com/help/deeplearning/examples/create-simple-deep-learning-network-for-classification.html.До этого я даю точность более 90%.В вышеупомянутом процессе упоминаний он просто разделился на две части из наборов данных для обучения и тестирования.Но я хочу обучить все наборы данных без разбиения и хочу распознавать символы во входных изображениях, содержащих числа слов.Я пытался сделать, но это дает результаты Вонг.Код для обучения CNN:
close all;
clear all;
clc;
%%%%-------Load and Explore Image Data----%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
digitDatasetPath = fullfile('abcd');
imds = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
labelCount = countEachLabel(imds);
numTrainFiles = 180;
imdsTrain = splitEachLabel(imds,numTrainFiles,'randomize'); %%%%%% I Used
all images for training
layers = [
imageInputLayer([28 28 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
mynet = trainNetwork(imdsTrain,layers,options);
save mynet;
Программа предварительной обработки и распознавания:
close all;
clear all;
clc;
%outputfolder='L:\cnn';
load mynet
aa=imread('0.tif');
% figure;
% imshow(a);
% title('Input Image');
bb=rgb2gray(aa);
% figure;
% imshow(b);
% title('gray image');
cc=imbinarize(bb);
%figure;
%imshow(cc);
% title('binary Image');
dd=~cc;
% figure;
%imshow(dd);
[x,y]=size(cc);
ee=sum(cc,2);
mat2=y-ee;
mat3=mat2~=0;
mat4=diff(mat3);
index1=find(mat4);
[q,w]=size(index1);%size of index2 matrix is q*w
kap=1;
lam=1;
while kap<((q/2)+1)%number of loops=number of lines
k=1;
mat5=([]);
for j=(index1(lam)+1):1:index1(lam+1)
mat5(k,:)=cc(j,:); %store the line segmented matrix
k=k+1;
end
lam=lam+2;
kap=kap+1;
%figure, imshow(mat5);
%imsave();
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% WORD SEGMENT%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ccc=imbinarize(mat5);
[xx,yy]=size(ccc);
eee=sum(ccc,1);
mat22=xx-eee;
mat33=mat22~=0;
mat44=diff(mat33);
index11=find(mat44);
[qq,ww]=size(index11);%size of index2 matrix is q*w
kap1=1;
lam1=1;
while kap1<((ww/2)+1)%number of loops=number of lines
kk=1;
mat55=([]);
for jj=(index11(lam1)+1):1:index11(lam1+1)
mat55(:,kk)=ccc(:,jj); %store the line segmented matrix
kk=kk+1;
end
lam1=lam1+2;
kap1=kap1+1;
%figure, imshow(mat55);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% REMOVE UNWANTED SPACE %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
invmat55=~mat55;
chasum=sum(invmat55,2);
measurements = regionprops(chasum == 0, 'Area',
'PixelIdxList');
fiveLongRegions = find([measurements.Area] >= 4);
theIndexes =
vertcat(measurements(fiveLongRegions).PixelIdxList);
cccc=mat55;
cccc(theIndexes,:)=0;
bbbb=cccc;
%figure, imshow(bbbb);
measurements = regionprops(bbbb, 'Area', 'BoundingBox');
allAreas = [measurements.Area];
% Crop out each word
a=001;
for blob = 1 : length(measurements)
% Get the bounding box.
thisBoundingBox = measurements(blob).BoundingBox;
% Crop it out of the original gray scale image.
thisWord = imcrop(mat55, thisBoundingBox);
RI2 = imresize(thisWord,[28 28]);
YPred = classify(mynet,RI2);
figure;
imshow(RI2);
label = YPred;
title(string(label));
end
end
end
Я хочу, чтобы моя программа состояла из двух частей, т.е. обучения и тестирования, как указано ниже для модели: TestВвод данных изображения содержит номера строк и количество слов.