It is a coding function net1.net to detect images

조회 수: 2 (최근 30일)
Adi
Adi 2024년 7월 24일
답변: Harsh 2024년 7월 30일
clc;clear;close all;
image_folder = 'daftar uang';
filenames = dir(fullfile(image_folder, '*.jpg'));
total_images = numel(filenames);
for n = 1:total_images
full_name= fullfile(image_folder, filenames(n).name);
I = imread(full_name);
J = I(:,:,1);
K = imbinarize(J,.6);
L = imcomplement(K);
str = strel('disk',5);
M = imclose(L,str);
N = imfill(M,'holes');
O = bwareaopen(N,1000);
stats = regionprops(O,'Area','Perimeter','Eccentricity');
area(n) = stats.Area;
perimeter(n) = stats.Perimeter;
metric(n) = 4*pi*area(n)/(perimeter(n)^2);
eccentricity(n) = stats.Eccentricity;
end
input = [metric;eccentricity];
target = zeros(1,48);
target(:,1:12) = 1;
target(:,13:24) = 2;
target(:,25:36) = 3;
target(:,37:48) = 4;
net = newff(input,target,[10 5],{'logsig','logsig'},'trainlm');
net.trainParam.epochs = 1000;
net.trainParam.goal = 1e-6;
net = train(net,input,target);
output = round(sim(net,input));
save net1.mat net
[m,n] = find(output==target);
akurasi = sum(m)/total_images*100
"Why is the accuracy I get only 25%? help me please?"

답변 (1개)

Harsh
Harsh 2024년 7월 30일
Hi @Adi,
From what I can gather, the neural network mentioned above has the following characteristics:
  • Two hidden layers with 10 and 5 neurons, respectively.
  • The activation function being used is the logistic sigmoid.
  • The Levenberg-Marquardt backpropagation algorithm is used for training the network.
Additionally, it looks like there are four categories for classifying the images, and the number of images used for training is only 48.
To improve accuracy and create a more robust network, consider the following suggestions:
  • Increase the depth of the neural network, as the current one is too simple, as an example I have created 3 hidden layers in the following snippet of code with 20, 10 and 5 neurons respectively.
net = newff(input, target, [20 10 5], {'logsig', 'logsig', 'logsig'}, 'trainlm');
  • If the dataset is limited to 48 images, try creating new images using techniques like augmentation. For example, images can be skewed using the "imageDataAugmenter" function:
augmentedImages = imageDataAugmenter('RandRotation', [-20, 20], 'RandXTranslation', [-3, 3], 'RandYTranslation', [-3, 3]);
  • A pre-trained network like AlexNet can also be used for better results.
net = alexnet;
layersTransfer = net.Layers(1:end-3);
numClasses = 4;
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',10,'BiasLearnRateFactor',10)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', 'InitialLearnRate',0.001, 'MaxEpochs',20, 'MiniBatchSize',64, 'ValidationData',{valImages,valLabels}, 'ValidationFrequency',30, 'Verbose',false, 'Plots','training-progress');
netTransfer = trainNetwork(trainImages, trainLabels, layers, options);
It was also noticed that the same dataset is being used for both training and validation. Instead, split the dataset into two parts for better validation.
[trainInd, valInd, testInd] = dividerand(size(input, 2), 0.7, 0.15, 0.15);
net.divideFcn = 'divideind';
net.divideParam.trainInd = trainInd;
net.divideParam.valInd = valInd;
net.divideParam.testInd = testInd;
I hope this helps. Thanks!

카테고리

Help CenterFile Exchange에서 Pattern Recognition and Classification에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by