Advertisement
Rexnime

FYP Code - working - CWT/DWT & classifier

Jun 1st, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.90 KB | None | 0 0
  1. rng(1)
  2. datafolder = "C:\Users\leere\Documents\MATLAB\Centrifugal Pump";
  3.  
  4. ads = audioDatastore(datafolder, ...
  5.     'IncludeSubfolders',true, ...
  6.     'FileExtensions','.wav', ...
  7.     'LabelSource','foldernames');
  8.  
  9. % save Image to folder, and create the relevant subfolder
  10. datafolder_scallogram = "datafolder/structure_Scallogram";
  11. catego=categories(ads.Labels);
  12. for i = 1:1:length(catego)
  13.     imgLoc = fullfile(datafolder_scallogram,char(catego{i}));
  14.     mkdir(imgLoc)
  15. end
  16.  
  17. helperCreateRGBfromTF(ads,datafolder_scallogram)
  18.  
  19.  
  20. allImages = imageDatastore(datafolder_scallogram,...
  21.     'IncludeSubfolders',true,...
  22.     'LabelSource','foldernames');
  23.  
  24. rng(1)
  25. [imgsTrain,imgsValidation,imgsTest] = splitEachLabel(allImages,0.6,0.2,'randomized');
  26. disp(['Number of training images: ',num2str(numel(imgsTrain.Files))]);
  27. countEachLabel(imgsTrain)
  28. disp(['Number of validation images: ',num2str(numel(imgsValidation.Files))]);
  29. countEachLabel(imgsValidation)
  30. disp(['Number of validation images: ',num2str(numel(imgsTest.Files))]);
  31. countEachLabel(imgsTest)
  32.  
  33.  
  34.  
  35. net = googlenet;
  36. net.Layers(1)
  37. inputSize = net.Layers(1).InputSize;
  38. if isa(net,'SeriesNetwork')
  39.   lgraph = layerGraph(net.Layers);
  40. else
  41.   lgraph = layerGraph(net);
  42. end
  43.  
  44. [learnableLayer,classLayer] = findLayersToReplace(lgraph);
  45. [learnableLayer,classLayer]
  46. numClasses = numel(categories(imgsTrain.Labels));
  47.  
  48. if isa(learnableLayer,'nnet.cnn.layer.FullyConnectedLayer')
  49.     newLearnableLayer = fullyConnectedLayer(numClasses, ...
  50.         'Name','new_fc', ...
  51.         'WeightLearnRateFactor',10, ...
  52.         'BiasLearnRateFactor',10);
  53.    
  54. elseif isa(learnableLayer,'nnet.cnn.layer.Convolution2DLayer')
  55.     newLearnableLayer = convolution2dLayer(1,numClasses, ...
  56.         'Name','new_conv', ...
  57.         'WeightLearnRateFactor',10, ...
  58.         'BiasLearnRateFactor',10);
  59. end
  60.  
  61. lgraph = replaceLayer(lgraph,learnableLayer.Name,newLearnableLayer);
  62. newClassLayer = classificationLayer('Name','new_classoutput');
  63. lgraph = replaceLayer(lgraph,classLayer.Name,newClassLayer);
  64.  
  65. figure('Units','normalized','Position',[0.3 0.3 0.4 0.4]);
  66. plot(lgraph)
  67. ylim([0,10])
  68.  
  69. miniBatchSize = 16;
  70. valFrequency = floor(numel(imgsTrain.Files)/miniBatchSize);
  71. options = trainingOptions('sgdm', ...
  72.     'MiniBatchSize',miniBatchSize, ...
  73.     'MaxEpochs',6, ...
  74.     'InitialLearnRate',3e-4, ...
  75.     'Shuffle','every-epoch', ...
  76.     'ValidationData',imgsValidation, ...
  77.     'ValidationFrequency',valFrequency, ...
  78.     'Verbose',false, ...
  79.     'Plots','training-progress');
  80.  
  81. reset(gpuDevice())
  82. net = trainNetwork(imgsTrain,lgraph,options);
  83.  
  84. [YPred,probs] = classify(net,imgsTest);
  85. accuracy = mean(YPred == imgsTest.Labels)
  86.  
  87. idx = randperm(numel(imgsTest.Files),4);
  88. figure
  89. for i = 1:4
  90.     subplot(2,2,i)
  91.     I = readimage(imgsTest,idx(i));
  92.     imshow(I)
  93.     label = YPred(idx(i));
  94.     title(string(label) + ", " + num2str(100*max(probs(idx(i),:)),3) + "%");
  95. end
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement