What could be the reason why my model does not give accurate result... (2024)

Hi everyone. First of all, thank you for your time. This will be my first question on the matlab platform. Please excuse me if I have any mistakes. If you understand the problem, you can already find the necessary files in the zip folder. If you want to view images in pgm format, you can use the GIMP application.

I am planning to design a MLP image processing model without using any toolbox.

I plan to train my model by reading one by one 32x30 scale images in the CMU face images dataset I obtained from the internet and then continue with testing process.

(I use imread function that is provided by MATLAB)

INPUT is a cell vector which contains image matrixes in each element. So each element represents an image actually. While processing samples one by one I get its images as column vector.

Here is the code for file operations and image reading:

clc;clear;close;

%*****************Reading Images**************

myFolder = ''; %% Images folder path

if ~isfolder(myFolder) %% Checking if the folder doesn't exist

errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);

uiwait(warndlg(errorMessage));

myFolder = uigetdir(); % Ask for a new one.

if myFolder == 0

% User clicked Cancel

return;

end

end

filePattern = fullfile(myFolder, '*.pgm');

theFiles = dir(filePattern);

% Define the number of image files in the folder

numImages = length(theFiles);

% Initialize a cell array to store the images

INPUT = cell(numImages, 1);

for k = 1 : length(theFiles)

baseFileName = theFiles(k).name;

fullFileName = fullfile(theFiles(k).folder, baseFileName);

INPUT{k} = imread(fullFileName);

imshow(INPUT{k}); % Display image.

drawnow; % Force display to update immediately.

end

%***********************************************

It is a user-interactive model, and firstly I get the number of:

Hidden layers:

Neurons in each hidden layer: (neuron numbers will be the same for each hidden layer)

Max iteration:

of my model from user.

Other definitions are shown in below code. I define NumberOfInput as 960 which comes from 32x30 because Weight matrix's size between input layer and first hidden layer needed to adjusted in that way.

Weight matrix values are assigned randomly.

My model should return 0 if person doesn't wear sunglasses and 1 if person wears with high accuracy. So it is scaler and there will be 1 output obviously.

I studied about MLP models and I found that finding perfect variables is a hard subject in machine learning and it depends on application and tests. So I defined my ETA with various values: 0.01,0.02,0.05,0.2,0.5....

In this type of model input to neurons are defined as netH and output of these neurons are defined as H except last connections. In there they become netO and O.

Also sigma size is defined in order to use after forward state (backward state starts).

My model is an example of Supervised Learning and it needs some outputs for training images like mentioned DESIRED as below. Images inside Model_Training are located as open-->sunglasses-->open-->sunglasses... so I decided to define desired with this order as shown below.

Here is the code:

%*******************VARİABLES*******************

NumberOfPatterns=numImages;

NumberOfInput=960;

NumberOfOutput=1;

LearningRate_ETA=0.5;

while true

NofLayers=input("Layer number: "); % Hidden layer number

Nofneurons=input("Neuron number: "); % Neuron number of each hidden layer

Max_iteration=input("Max iteration number: "); % Max iteration

if(Nofneurons<=0 || NofLayers<=0 || Max_iteration<=0)

fprintf("These values can't be accepted !");

fprintf("\nPlease enter again");

else

break;

end

end

W = cell(NofLayers+1,1);

H=cell(NofLayers,1);

sigma=cell(NofLayers+1,1);

%***********************************************

% Random values are assigned to Weights

for i=1:NofLayers+1

if i==1

W{i}=rand(NumberOfInput,Nofneurons);

elseif i==NofLayers+1

W{i}=rand(Nofneurons,NumberOfOutput);

else

W{i}=rand(Nofneurons,Nofneurons);

end

end

%***********************************************

DESIRED=zeros(NumberOfPatterns,1);

%****************Adjusting Desired Results******

%Training images are located in order. Ex:

%A_open.pgn

%A_sunglasses.pgn

for i=1:NumberOfPatterns

if(mod(i,2)==1)

DESIRED(i)=0;

else

DESIRED(i)=1;

end

end

%************************************************

Right now processing starts. I need to mention that I use sigmoid function as activation function.

In order not to prolong the topic further I will share directly code in here.

%***********************Processing***************

for a=1:Max_iteration

totalerr=0;

for i = 1:NumberOfPatterns

ImageVector = reshape(INPUT{i}, [], 1);

X = double(ImageVector);

for lay=1:NofLayers+1

if(lay==1) %First connections

netH=W{lay}'*X;

H{lay}=sigmoid(netH);%%%

elseif (lay==NofLayers+1) %Last connections

netO=W{lay}'*H{lay-1};

O=sigmoid(netO);

else % between connections layers

netH=W{lay}'*H{lay-1}; %

H{lay}=sigmoid(netH);%

end

end

err=DESIRED(i)-O;

for j=1:NumberOfOutput

sigma{NofLayers+1}=err*O(j)*(1-O(j)); %Last sigma value

end

for l=1:NofLayers

for k=1:Nofneurons

[rowsigma colsigma]=size(sigma{NofLayers-l+2});

[rowW colsW]=size(W{NofLayers-l+2}(k,:));

%These conditions satisfies proper matrix multiplciation

if(colsigma==rowW)

sigma{NofLayers-l+1}=sigma{NofLayers-l+2}*W{NofLayers-l+2}(k,:) *H{NofLayers+1-l}(k)*(1-H{NofLayers+1-l}(k));

else

sigma{NofLayers-l+1}=sigma{NofLayers-l+2}*W{NofLayers-l+2}(k,:)'*H{NofLayers+1-l}(k)*(1-H{NofLayers+1-l}(k));

end

end

end

for z=1:NofLayers+1

%Weights are updated at this part

if((NofLayers+2-z)==1)

W{NofLayers+2-z}=W{NofLayers+2-z}+LearningRate_ETA*X*sigma{NofLayers+2-z};

else

W{NofLayers+2-z}=W{NofLayers+2-z}+LearningRate_ETA*H{NofLayers+1-z}*sigma{NofLayers+2-z};

end

end

totalerr=totalerr+0.5*err^2;

end

cost(a)=totalerr;

end

plot(cost);

%%*****************Test Case********************

%Getting test image address from user

fileFilter = '*.pgm';

[filename, pathname] = uigetfile(fileFilter, 'Select a PGM file', '');

if isequal(filename, 0)

disp('Program has stopped');

else

fullFilePath = fullfile(pathname, filename);

end

%**************Test Sample Operations*******

testSample=imread(fullFilePath); %

testSample=reshape(testSample,[],1);

X=double(testSample);

for lay=1:NofLayers+1

if(lay==1) %First connections

netH=W{lay}'*X;

H{lay}=sigmoid(netH);

elseif (lay==NofLayers+1) %Last connections

netO=W{lay}'*H{lay-1};

Out=round(sigmoid(netO));

else % between connections layers

netH=W{lay}'*H{lay-1};

H{lay}=sigmoid(netH);

end

end

fprintf('Result is: %d\n', Out);

%**********************Helper Functions*********

%Sigmoid Activation Function

function y = sigmoid(x)

y = 1 ./ (1 + exp(-x));

end

You can run and test it with the files that provided in zip file. In this kind of model as I know I need to try it with high number of layer and neuron. I tried with 4-20 5-30 5-35 ... Generally it returns 1 and this is the problem that I am struggling with.

If you can give any comment, feedback I would appreciate it. Again thank you for giving a time.

What could be the reason why my model does not give accurate result... (2024)

FAQs

Why is my model accuracy so low? ›

Overfitting and underfitting are common problems that can lead to low model accuracy. Overfitting occurs when the model is trained too well on the training data, to the point that it memorizes the data rather than learning the underlying patterns. This results in poor generalization to new data.

How can I make my model more accurate? ›

Adding more data to your training set can improve model performance and reduce reliance on assumptions. Treating missing and outlier values is essential for reducing bias and enhancing model accuracy. Feature engineering enables the creation of new variables that better explain the variance in the data.

How to tell if a model is accurate? ›

Accuracy is calculated by using the number of correct predictions or all predictions made. The score levels are based on the documents provided. Using the best practices for model building ensures better confidence scores.

How accurate does a model need to be? ›

Generally speaking, industry standards for good accuracy is above 70%. However, depending on the model objectives, good accuracy may demand 99% accuracy and up.

Why accuracy is insufficient for determining how good a model is? ›

In short, while its simplicity is appealing, the most significant reason why accuracy is not a good measure for imbalanced data is that it doesn't consider the nuances of classification. Measured in a vacuum, it simply provides a limited view of the model's true dependability.

Why is my model giving 100% accuracy? ›

As to the 100% accuracy, how big is your validation set? If the validation set is too small then the model might just 'get lucky' and get 100% on those few datapoints. The general size of the validation set is 20% of the train set.

How can accuracy be improved in an experiment? ›

  1. 1 Train and monitor staff. One of the most important factors that affect the accuracy of experimental data is the human factor. ...
  2. 2 Calibrate and maintain equipment. ...
  3. 3 Implement and follow SOPs. ...
  4. 4 Use and validate methods. ...
  5. 5 Manage and analyze data. ...
  6. 6 Review and report results. ...
  7. 7 Here's what else to consider.
Oct 18, 2023

How to improve predictive accuracy? ›

Ways to Improve Predictive Models
  1. Add more data: Having more data is always a good idea. ...
  2. Feature Engineering: Adding new feature decreases bias on the expense of variance of the model. ...
  3. Feature Selection: This is one of the most important aspects of predictive modelling.
Mar 28, 2018

What is acceptable model accuracy? ›

Industry standards are between 70% and 90%.

Everything above 70% is acceptable as a realistic and valuable model data output. It is important for a models' data output to be realistic since that data can later be incorporated into models used for various businesses and sectors' needs.

How do I know if my model is overfitting or underfitting? ›

We can determine whether a predictive model is underfitting or overfitting the training data by looking at the prediction error on the training data and the evaluation data. Your model is underfitting the training data when the model performs poorly on the training data.

How do you know if a model is adequate? ›

Adequacy Check #3: Determine if the residuals as a function of x show nonlinearity. This is an indication that the model is not adequate. Adequacy Check #4: Determine if 95% of the values of scaled residuals are within [-2,2]. If so, this is an indication that the model may be adequate.

How to increase the accuracy of a model? ›

Improving Model Accuracy
  1. Collect data: Increase the number of training examples.
  2. Feature processing: Add more variables and better feature processing.
  3. Model parameter tuning: Consider alternate values for the training parameters used by your learning algorithm.

What is an accurate model? ›

An accurate model will closely match the verification data even though these data were not used to set the model's parameters. Retrieved from Wikipedia CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0/ Source URL: https://en.wikipedia.org/wiki/Mathematical model.

How to improve the precision of a model? ›

Now we'll check out the proven way to improve the accuracy of a model:
  1. Add More Data.
  2. Treat Missing and Outlier Values.
  3. Feature Engineering.
  4. Feature Selection.
  5. Multiple Algorithms.
  6. Algorithm Tuning.
  7. Ensemble Methods.
  8. Cross Validation.

How to increase the accuracy of a classification model? ›

Answer: To increase the accuracy of classifiers, optimize hyperparameters, perform feature engineering, and use ensemble methods.
  1. Optimize Hyperparameters: ...
  2. Feature Engineering: ...
  3. Data Preprocessing: ...
  4. Ensemble Methods: ...
  5. Cross-Validation: ...
  6. Ensemble of Models: ...
  7. Error Analysis: ...
  8. Regularization:
Feb 14, 2024

Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 6316

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.