Creating an imdb structure

4 visualizaciones (últimos 30 días)
Sivaramakrishnan Rajaraman
Sivaramakrishnan Rajaraman el 6 de Mzo. de 2017
Editada: Walter Roberson el 25 de Mzo. de 2018
Hi,
I have written a new createIMDB function following an online tutorial on Deep Learning by German Ros. The original imdb was written for a binary classification problem. I attempted to change it for a three class problem. Each of my class 'cat', 'dog' and 'rat' contain say, three images, for simplicity. My function goes like this:
function imdb = createIMDB_RAM1(folder)
% imdb is a matlab struct with several fields, such as:
% - images: contains data, labels, ids dataset mean, etc.
% - meta: contains meta info useful for statistics and visualization
% - any other you want to add
imdb = struct();
% let's assume we have a folder with three
% subfolders "cat" "dog" and "rat" containing images
% for a multi-class problem
positives = dir([folder '/cat/*.jpg']);
negatives = dir([folder '/dog/*.jpg']);
neutral = dir([folder '/rat/*.jpg']);
imref = imread([folder '/cat/', positives(1).name]);
[H, W, CH] = size(imref);
% number of images
NPos = numel(positives);
NNeg = numel(negatives);
NNeu = numel(neutral);
N = NPos + NNeg + NNeu;
% we can initialize part of the structures already
meta.sets = {'train', 'val'};
meta.classes = {'cat', 'dog', 'rat'};
% images go here
images.data = zeros(H, W, CH, N, 'single');
% this will contain the mean of the training set
images.data_mean = zeros(H, W, CH, 'single');
% a label per image
images.labels = zeros(1, N);
% vector indicating to which set an image belong, i.e.,
% training, validation, etc.
images.set = zeros(1, N);
numImgsTrain = 0;
% loading positive samples
for i=1:numel(positives)
im = single(imread([folder '/cat/', positives(i).name]));
images.data(:,:,:, i) = im;
images.labels(i) = 2;
% in this case we select the set (train/val) randomly
if(randi(10, 1) > 6) % 60% for training and 40% for validation
images.set(i) = 1;
images.data_mean = images.data_mean + im;
numImgsTrain = numImgsTrain + 1;
else
images.set(i) = 2;
end
end
% loading negative samples
for i=1:numel(negatives)
im = single(imread([folder '/dog/', negatives(i).name]));
images.data(:,:,:, NPos+NNeu+i) = im;
images.labels(NPos+NNeu+i) = 1;
% in this case we select the set (train/val) randomly
if(randi(10, 1) > 6)
images.set(NPos+NNeu+i) = 1;
images.data_mean = images.data_mean + im;
numImgsTrain = numImgsTrain + 1;
else
images.set(NPos+NNeu+i) = 2;
end
end
% loading neutral samples
for i=1:numel(neutral)
im = single(imread([folder '/rat/', neutral(i).name]));
images.data(:,:,:, NPos+NNeg+i) = im;
images.labels(NPos+NNeu+i) = 3;
% in this case we select the set (train/val) randomly
if(randi(10, 1) > 6)
images.set(NPos+NNeg+i) = 1;
images.data_mean = images.data_mean + im;
numImgsTrain = numImgsTrain + 1;
else
images.set(NPos+NNeu+i) = 2;
end
end
% let's finish to compute the mean
images.data_mean = images.data_mean ./ numImgsTrain;
% now let's add some randomization
indices = randperm(N);
images.data(:,:,:,:) = images.data(:,:,:,indices);
images.labels(:) = images.labels(indices);
images.set(:) = images.set(indices);
imdb.meta = meta;
imdb.images = images;
end
I'm trying to give labels '1', '2', '3' for 'cat', 'dog' and 'rat' respectively. When i run the function, all the other meta-parameters including the number of sets (train, val), labels ('cat', 'dog', 'rat') are declared properly however the 'labels' and 'sets' are improperly assigned to the images. I'm pretty sure I am committing mistake in assigning the labels and sets inside the 'for' loop.
This is an issue with creating a structure and I'm sure the experts out here can solve my problem in no time. Kindly help rectifying the same with the modified peice of code. Thanks for your assistance.
  9 comentarios
Greg Heath
Greg Heath el 7 de Mzo. de 2017
The classical approach to NN classification of non-overlapping classes is to use {0,1} unit vector targets.
Then the outputs can be interpreted as conditional posterior probabilities.
See any text on pattern recognition.
Hope this helps.
Greg
Sivaramakrishnan Rajaraman
Sivaramakrishnan Rajaraman el 7 de Mzo. de 2017
Hi Roberson, Many thanks for your assistance. As you mentioned, I found that the above code is working fine to create a structure of three classes with their respective labels.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Image Data Workflows en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by