MATLAB Answers

Is there a way to apply random noise to 'imageInputLayer' during CNN training?

11 views (last 30 days)
jonghyun kim
jonghyun kim on 31 Mar 2017
Answered: Javier Pinzón on 20 Oct 2017
I checked that there are options, like random crop or random flip in imageInputLayer function. But, i'd like to add additive Gaussian random noise to my each input image for CNN to be invariant to noise.
Is there any way for this? Thank you for reading!

Answers (2)

Carl on 3 Apr 2017
Hi Jonghyun, there is no option built into the 'imageInputLayer' function for doing that, but you can manually edit the images before training your network. Images can be passed in via the 'imds' argument to the 'trainNetwork' function, where 'imds' is an ImageDatastore object with categorical labels.
Before training your network, you can add Gaussian noise to your images specified in the ImageDatastore. The 'imnoise' function may be helpful for this.

Javier Pinzón
Javier Pinzón on 20 Oct 2017
Hello Kim,
(Hope it is not reaaally late and might help others)
If you want to add some Noise during the training, you can do that in the @readFunction of the images, i mean:
- First the normal definition of the dataset:
location = 'train';
imds = imageDatastore(location,'IncludeSubfolders',1,'LabelSource','foldernames');
tbl = countEachLabel(imds);
minSetCount = min(tbl{:,2}); % determine the smallest amount of images in a category
% Use splitEachLabel method to trim the set.
imds = splitEachLabel(imds, minSetCount);
% Notice that each set now has exactly the same number of images.
trainingDS = imds;
% Convert labels to categoricals
trainingDS.Labels = categorical(trainingDS.Labels);
trainingDS.ReadFcn = @readFunctionTrain;
When you have defined it, with the @ReadFunctionTrain, in this function you might defined the reading of the image as follows:
function I = readFunctionTrain(filename)
I = imread(filename);
% Defines a random number to add the noise
k = randi([0, 100])
if k > 50
% Resize images to the size required by the network.
I = imresize(I, [64 64]);
% NOISE ADDITION TO THE IMAGE AS YOU WANT, then, resize the img, or if you want to
% resize and the add the noise, the order is your up to you
I = imresize(I, [64 64]);
You can add the noise function that you want in the image, so with the random number, the noise will be added dynamicly over the training, without affect the original image.
Best Regards,

Community Treasure Hunt

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

Start Hunting!