Hi, I want to Normilze/Rescale the Dataset between 0 and 1
In Keras the Following Function is used to Normalize the Data between 0 and 1
train_image_generator = ImageDataGenerator(
rescale=1./255,)
%% in MATLAB i am using ImageDatastore
imageFolder = fullfile(downloadFolder,'Classdata');
imds = imageDatastore(imageFolder, 'LabelSource', 'foldernames', 'IncludeSubfolders',true);
How can i Normalize the ImageDatastore in MATLAB?

2 Kommentare

Image Analyst
Image Analyst am 26 Nov. 2021
Do you mean you just want to read in images one at a time and rescale them to a range of 0-1 with mat2gray() or rescale()?
hammad younas
hammad younas am 26 Nov. 2021
@Image Analyst mat2gray() convert image into grayscale. I want to rescale the whole imagedatastore between 0-1

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 26 Nov. 2021

1 Stimme

@hammad younas an imageDatastore is essentially just a structure that is a listing of file names. It is not image data itself. So you'd loop over the images and call rescale(thisImage, 0, 1) on each image, and then do something with it, like call image processing functions or something.
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
folder = pwd;
imds = imageDatastore(folder, "FileExtensions", [".jpg",".tif", ".png"])
numImages = length(imds.Files)
%numImages = 4; % For debugging.
plotRows = ceil(sqrt(numImages))
for k = 1 : numImages %length(imds.Files)
thisFile = imds.Files{k}
[f, baseFileName, ext] = fileparts(thisFile);
% Read in the image.
thisImage = imread(thisFile);
% Scale the min to 0 and the max to 1.
thisImage = rescale(thisImage, 0, 1);
% Now do something with the rescaled image.
subplot(plotRows, plotRows, k);
imshow(thisImage);
title(baseFileName, 'FontSize', fontSize);
text(10, 10, baseFileName, 'Color', 'r', ...
'BackgroundColor', 'y', 'FontSize', fontSize, 'VerticalAlignment', 'top')
end
g = gcf;
g.WindowState = 'maximized'

9 Kommentare

hammad younas
hammad younas am 26 Nov. 2021
@Image Analyst Thanks for you answer but How can i give this Loop Image for prediction of the trained network?
Image Analyst
Image Analyst am 26 Nov. 2021
I imagine you'd pass thisImage into a function like predict() or whatever it's called. Didn't your deep learning example show you both how to train the network, and how to apply it once it's been trained?
basically i am using keras trained model to MATLAB but my accuracy is different from keras network. The keras code apply Rescalling on Image Dataset and then predict on it.
Thats why i am doing rescalling in matlab to obtain the same result using the following code
imageFolder = fullfile(downloadFolder,'Classdata');
imds = imageDatastore(imageFolder, 'LabelSource', 'foldernames', 'IncludeSubfolders',true);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imds);
[YPred,scores] = classify(netTransfer,augimdsTrain);
Image Analyst
Image Analyst am 26 Nov. 2021
I don't think that normalizes the image. I think if they need to be normalized you will have to normalize the images individually and save them. Then they can be passed in to classify().
hammad younas
hammad younas am 27 Nov. 2021
@Image Analyst thanks for your reply i will check it. Can you please update the above code to save the rescale image with same name in the folder
Just add
imwrite(thisImage, thisFile);
hammad younas
hammad younas am 27 Nov. 2021
Bearbeitet: hammad younas am 27 Nov. 2021
@Image Analyst It will save image in same folder?
@Image Analyst In keras we normalize image using
img = cv2.resize(cv2.imread(path),(150,150))
img_normalized = img/255
How can i do this in above code?
% Read in the image.
thisImage = imread(thisFile);
% Resize to 150,150.
thisImage = imresize(thisImage, [150, 150]);
% Scale the max to 1, and everything else linearly scaled.
thisImage = thisImage / 255;
% Now do something with the resized and rescaled image.
Of course you could string that all together into one really long line of code (like your other code) if you want, though it might make it harder to read and follow.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Deep Learning Toolbox finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by