how to connect if conditions with CNN ?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Please help if you can, how can i connect the result of the first part of this program (if condtions) with the second part of the program (Convolutional Neural Network) , if the result is not identified so i want to tell it to enter its image to complete identifying with CNN the first part : %enter data of scan clc,clear PAs = 44; DAs = 91; As = 93; Bs = 1; if (PAs==0) & (DAs==0) & (As==0) & (Bs==0) disp('specimen is sound'); elseif (45<PAs) & (PAs<55) & (90<DAs) & (DAs>100) & (As>100) & (Bs<2); disp('incomplete penetration') else disp('not identified'); end
the second part : clc clear all close all outputFolder = fullfile('discontinuities'); rootfolder = fullfile(outputFolder,'discontinuities types'); types = {'HAZ','lof','porosity'}; imds = imageDatastore(fullfile(rootfolder,types),'Labelsource','foldernames'); tbl = countEachLabel(imds); minSetCount = min(tbl{:,2}); imds = splitEachLabel(imds, minSetCount, 'randomize'); countEachLabel(imds); HAZ = find(imds.Labels == 'HAZ', 1); lof = find(imds.Labels == 'lof', 1); porosity = find(imds.Labels == 'porosity', 1);
% figure % subplot(2,2,1); % imshow(readimage(imds,HAZ)); % subplot(2,2,2); % imshow(readimage(imds,lof)); % subplot(2,2,3); % imshow(readimage(imds,porosity));
net = resnet50();
% figure % plot(net) % title('Architecture of ResNet-50') % set(gca, 'YLim', [150 170]);
net.Layers(1); net.Layers(end); numel(net.Layers(end).ClassNames); [trainingSet, testSet] = splitEachLabel(imds, 0.3 , 'randomize'); imageSize = net.Layers(1).InputSize; augmentedTrainingSet = augmentedImageDatastore(imageSize, ... trainingSet, 'ColorPreprocessing', 'gray2rgb'); augmentedTestSet = augmentedImageDatastore(imageSize, ... testSet, 'ColorPreprocessing', 'gray2rgb'); wl = net.Layers(2).Weights; wl = mat2gray(wl);
% figure % montage(wl) % title('First Convolutional Layer Weight');
featureLayer = 'fc1000'; trainingFeatures = activations(net, ... augmentedTrainingSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns'); trainingLabels = trainingSet.Labels; classifier = fitcecoc(trainingFeatures, trainingLabels, ... 'Learner', 'Linear', 'Coding', 'onevsall', 'ObservationsIn', 'columns'); testFeatures = activations(net, ... augmentedTestSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
predictLabels = predict(classifier, testFeatures, 'ObservationsIn', 'columns');
testLabels = testSet.Labels; confMat = confusionmat(testLabels, predictLabels); confMat = bsxfun(@rdivide, confMat, sum(confMat,2));
mean(diag(confMat));
newImage = imread(fullfile('test4.PNG'));
ds = augmentedImageDatastore(imageSize,... newImage, 'ColorPreprocessing', 'gray2rgb');
imageFeatures = activations(net, ... ds, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
label = predict(classifier, imageFeatures, 'ObservationsIn', 'columns');
sprintf('The loaded image belongs to %s class', label)
1 Commento
Risposte (1)
Aastha
il 9 Giu 2025
As I understand, you want to use a CNN network to identify the class of the input image if the initial identification using logical conditions fails. You can achieve this in MATLAB by following the steps outlined below:
1. To allow the user to input an image for classification, you can use the "input" function in MATLAB to prompt for the image path. For more information on “input” function, kindly refer to the MathWorks documentation of “input” function linked below:
2. You can then initialize the CNN feature extractor and train a classifier. This trained model can then be used when the image class cannot be identified by the initial logic in your code.
3. The modified code is as follows:
% Load model and classifier once
[net, classifier, imageSize, featureLayer] = initializeCNN();
% Your initial condition logic
PAs = 44;
DAs = 91;
As = 93;
Bs = 1;
if (PAs==0) && (DAs==0) && (As==0) && (Bs==0)
disp('specimen is sound');
elseif (45 < PAs) && (PAs < 55) && (90 < DAs) && (DAs > 100) && (As > 100) && (Bs < 2)
disp('incomplete penetration');
else
disp('not identified');
imagePath = input('Enter path to image for CNN classification: ', 's');
newImage = imread(imagePath);
ds = augmentedImageDatastore(imageSize, newImage, 'ColorPreprocessing', 'gray2rgb');
imageFeatures = activations(net, ds, featureLayer, ...
'MiniBatchSize', 32, 'OutputAs', 'columns');
label = predict(classifier, imageFeatures, 'ObservationsIn', 'columns');
fprintf('The loaded image belongs to %s class\n', label);
end
I hope this is helpful!
0 Commenti
Vedere anche
Categorie
Scopri di più su Classification in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!