Thisexampleshowshowtoclassifyhumanelectrocardiogram(ECG)signalsusingthecontinuouswavelettransform(CWT)andadeepconvolutionalneuralnetwork(CNN).
TrainingadeepCNNfromscratchiscomputationallyexpensiveandrequiresalargeamountoftrainingdata.Invariousapplications,asufficientamountoftrainingdataisnotavailable,andsynthesizingnewrealistictrainingexamplesisnotfeasible.Inthesecases,leveragingexistingneuralnetworksthathavebeentrainedonlargedatasetsforconceptuallysimilartasksisdesirable.Thisleveragingofexistingneuralnetworksiscalledtransferlearning.InthisexampleweadapttwodeepCNNs,GoogLeNetandSqueezeNet,pretrainedforimagerecognitiontoclassifyECGwaveformsbasedonatime-frequencyrepresentation.
AfterdownloadingthedatafromGitHub,unzipthefileinyourtemporarydirectory.
UnzipECGData.zipinphysionet-ECG_data-main.LoadthedatafileintoyourMATLABworkspace.
helperPlotReps(ECGData)
Aftermakingthefolders,createtime-frequencyrepresentationsoftheECGsignals.Theserepresentationsarecalledscalograms.AscalogramistheabsolutevalueoftheCWTcoefficientsofasignal.
Tocreatethescalograms,precomputeaCWTfilterbank.PrecomputingtheCWTfilterbankisthepreferredmethodwhenobtainingtheCWTofmanysignalsusingthesameparameters.
LoadthepretrainedGoogLeNetneuralnetwork.IfDeepLearningToolboxModelforGoogLeNetNetworksupportpackageisnotinstalled,thesoftwareprovidesalinktotherequiredsupportpackageintheAdd-OnExplorer.Toinstallthesupportpackage,clickthelink,andthenclickInstall.
InspectthefirstelementofthenetworkLayersproperty.ConfirmthatGoogLeNetrequiresRGBimagesofsize224-by-224-by-3.
Eachlayerinthenetworkarchitecturecanbeconsideredafilter.Theearlierlayersidentifymorecommonfeaturesofimages,suchasblobs,edges,andcolors.Subsequentlayersfocusonmorespecificfeaturesinordertodifferentiatecategories.GoogLeNetispretrainedtoclassifyimagesinto1000objectcategories.YoumustretrainGoogLeNetforourECGclassificationproblem.
Inspectthelastfourlayersofthenetwork.
Replacethefullyconnectedlayerloss3-classifierwithanewfullyconnectedlayerwiththenumberoffiltersequaltothenumberofclasses.Tolearnfasterinthenewlayersthaninthetransferredlayers,increasethelearningratefactorsofthefullyconnectedlayer.
Traininganeuralnetworkisaniterativeprocessthatinvolvesminimizingalossfunction.Tominimizethelossfunction,agradientdescentalgorithmisused.Ineachiteration,thegradientofthelossfunctionisevaluatedandthedescentalgorithmweightsareupdated.
Trainingcanbetunedbysettingvariousoptions.InitialLearnRatespecifiestheinitialstepsizeinthedirectionofthenegativegradientofthelossfunction.MiniBatchSizespecifieshowlargeofasubsetofthetrainingsettouseineachiteration.Oneepochisafullpassofthetrainingalgorithmovertheentiretrainingset.MaxEpochsspecifiesthemaximumnumberofepochstousefortraining.Choosingtherightnumberofepochsisnotatrivialtask.Decreasingthenumberofepochshastheeffectofunderfittingthemodel,andincreasingthenumberofepochsresultsinoverfitting.
EvaluateGoogLeNetAccuracy
Evaluatethenetworkusingthevalidationdata.
ExploreGoogLeNetActivations
EachlayerofaCNNproducesaresponse,oractivation,toaninputimage.However,thereareonlyafewlayerswithinaCNNthataresuitableforimagefeatureextraction.Inspectthefirstfivelayersofthetrainednetwork.
ExaminewhichareasintheconvolutionallayersactivateonanimagefromtheARRclass.Comparewiththecorrespondingareasintheoriginalimage.Eachlayerofaconvolutionalneuralnetworkconsistsofmany2-Darrayscalledchannels.Passtheimagethroughthenetworkandexaminetheoutputactivationsofthefirstconvolutionallayer,conv1-7x7_s2.
Findthestrongestchannelforthisimage.Comparethestrongestchannelwiththeoriginalimage.
SqueezeNetisadeepCNNwhosearchitecturesupportsimagesofsize227-by-227-by-3.EventhoughtheimagedimensionsaredifferentforGoogLeNet,youdonothavetogeneratenewRGBimagesattheSqueezeNetdimensions.YoucanusetheoriginalRGBimages.
Load
LoadthepretrainedSqueezeNetneuralnetwork.IfDeepLearningToolboxModelforSqueezeNetNetworksupportpackageisnotinstalled,thesoftwareprovidesalinktotherequiredsupportpackageintheAdd-OnExplorer.Toinstallthesupportpackage,clickthelink,andthenclickInstall.
ToretrainSqueezeNettoclassifynewimages,makechangessimilartothosemadeforGoogLeNet.
Inspectthelastfivenetworklayers.
augimgsTrain=augmentedImageDatastore([227227],imgsTrain);augimgsValidation=augmentedImageDatastore([227227],imgsValidation);SetTrainingOptionsandTrainSqueezeNet
CreateanewsetoftrainingoptionstousewithSqueezeNet,andtrainthenetwork.
EvaluateSqueezeNetAccuracy
helperCreateECGDataDirectoriescreatesadatadirectoryinsideaparentdirectory,thencreatesthreesubdirectoriesinsidethedatadirectory.ThesubdirectoriesarenamedaftereachclassofECGsignalfoundinECGData.
functionhelperCreateECGDirectories(ECGData,parentFolder,dataFolder)%ThisfunctionisonlyintendedtosupporttheECGAndDeepLearningExample.%Itmaychangeorberemovedinafuturerelease.rootFolder=parentFolder;localFolder=dataFolder;mkdir(fullfile(rootFolder,localFolder))folderLabels=unique(ECGData.Labels);fori=1:numel(folderLabels)mkdir(fullfile(rootFolder,localFolder,char(folderLabels(i))));endendhelperPlotRepsplotsthefirstthousandsamplesofarepresentativeofeachclassofECGsignalfoundinECGData.
HowtoGetBestSitePerformance
SelecttheChinasite(inChineseorEnglish)forbestsiteperformance.OtherMathWorkscountrysitesarenotoptimizedforvisitsfromyourlocation.