Machine Learning Image Class
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to do machine learning image class between images with no oil vs. images with a lot of oil. I need to create a train and test data for covariant matrices and class. I have this code so far but it is not working. What am I missing?
% try diff vis on holo images
clear all
close all
holo_dir = '~/This PC/My Passport (F:)/Hoops Oil/150ml hoops syringe/'
% load images:
imagefiles = dir([holo_dir '*.pgm']);
nfiles = length(imagefiles); % Number of files found
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
currentimage = imread([holo_dir currentfilename]);
cur_data = im2double(currentimage);
%if (ii==1)
% sum_image= cur_data;
% else
% sum_image=cur_data+sum_image;
%end
%pause
% display image:
images{ii} = currentimage;
end
%bgd_image = sum_image/nfiles;
for ii=1:nfiles
% how long to get cov?
tic
20*log10(abs(cov(cur_data)));
toc
currentfilename = imagefiles(ii).name;
currentimage = imread([holo_dir currentfilename]);
cur_data = im2double(currentimage)-bgd_image;
figure(1)
hold on
subplot(1,3,1)
pcolor(cur_data+bgd_image)
shading flat
colormap gray
title('Original')
subplot(1,3,2)
pcolor(cur_data)
caxis([-.2,.2])
shading flat
caxis([-.2,.2])
colormap gray
title('Corrected')
% disp the double fft:
subplot(1,3,3)
%pcolor(20*log10(abs(fft2(cur_data)))-20*log10(abs(fft2(bgd_image))))
pcolor(20*log10(abs(cov(cur_data))))
caxis([-80 -60])
title('Covariance (log)')
% caxis([-.2,.2])
shading flat
colormap gray
pause
% display image:
images_corrected{ii} = cur_data;
end
img_list = dir('*.pgm');
for file = img_list'
fprintf(1,'Doing something with %s\n',file.name)
img = imread(file.name);
imshow(img)
end
imds = imageDatastore(img_list, 'LabelSource', 'foldernames', 'IncludeSubfolders',true);
oil = find(imds.Labels == '*.pgm');
figure
imshow(readimage(imds,oil))
tbl = countEachLabel(imds)
% Determine the smallest amount of images in a category
minSetCount = min(tbl{:,2});
% Limit the number of images to reduce the time it takes
% run this example.
maxNumImages = 100;
minSetCount = min(maxNumImages,minSetCount);
% Use splitEachLabel method to trim the set.
imds = splitEachLabel(imds, minSetCount, 'randomize');
% Notice that each set now has exactly the same number of images.
countEachLabel(imds)
% Load pretrained network
net = resnet50();
% Visualize the first section of the network.
figure
plot(net)
title('First section of ResNet-50')
set(gca,'YLim',[150 170]);
% Inspect the first layer
net.Layers(1)
% Inspect the last layer
net.Layers(end)
% Number of class names for ImageNet classification task
numel(net.Layers(end).ClassNames)
[trainingSet, testSet] = splitEachLabel(imds, 0.3, 'randomize');
% Create augmentedImageDatastore from training and test sets to resize
% images in imds to the size required by the network.
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize, trainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize, testSet, 'ColorPreprocessing', 'gray2rgb');
% Get the network weights for the second convolutional layer
w1 = net.Layers(2).Weights;
% Scale and resize the weights for visualization
w1 = mat2gray(w1);
w1 = imresize(w1,5);
% Display a montage of network weights. There are 96 individual sets of
% weights in the first layer.
figure
montage(w1)
title('First convolutional layer weights')
featureLayer = 'fc1000';
trainingFeatures = activations(net, augmentedTrainingSet, featureLayer, ...
'MiniBatchSize', 32, 'OutputAs', 'columns');
% Get training labels from the trainingSet
trainingLabels = trainingSet.Labels;
% Train multiclass SVM classifier using a fast linear solver, and set
% 'ObservationsIn' to 'columns' to match the arrangement used for training
% features.
classifier = fitcecoc(trainingFeatures, trainingLabels, ...
'Learners', 'Linear', 'Coding', 'onevsall', 'ObservationsIn', 'columns');
% Extract test features using the CNN
testFeatures = activations(net, augmentedTestSet, featureLayer, ...
'MiniBatchSize', 32, 'OutputAs', 'columns');
% Pass CNN image features to trained classifier
predictedLabels = predict(classifier, testFeatures, 'ObservationsIn', 'columns');
% Get the known labels
testLabels = testSet.Labels;
% Tabulate the results using a confusion matrix.
confMat = confusionmat(testLabels, predictedLabels);
% Convert confusion matrix into percentage form
confMat = bsxfun(@rdivide,confMat,sum(confMat,2))
testImage = readimage(testSet,1);
testLabel = testSet.Labels(1)
% Create augmentedImageDatastore to automatically resize the image when
% image features are extracted using activations.
ds = augmentedImageDatastore(imageSize, testImage, 'ColorPreprocessing', 'gray2rgb');
% Extract image features using the CNN
imageFeatures = activations(net, ds, featureLayer, 'OutputAs', 'columns');
% Make a prediction using the classifier
predictedLabel = predict(classifier, imageFeatures, 'ObservationsIn', 'columns')
4 comentarios
Subhadeep Koley
el 5 de Ag. de 2019
Can you elaborate which line is giving you the error ?
Also you can use this link to see a demo of image classification using MATLAB and apply the same for your problem.
Respuestas (1)
Harsha Priya Daggubati
el 8 de Ag. de 2019
Hi,
Can you attach the error you get when you run the code, or explain how your output differs the expected one.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!