A practical guide to anomaly detection using Anomalib
文章推薦指數: 80 %
A short guide on unsupervised anomaly detection and how to apply it ... The idea of PatchCore is that if a single patch is anomalous the ...
OpeninappHomeNotificationsListsStoriesWritePublishedinML6teamApracticalguidetoimage-basedanomalydetectionusingAnomalibAshortintroductiontounsupervisedanomalydetectionandhowtoapplyit.Humanintelligence-basedanomalydetectioninTheSimpsonsInindustrialmanufacturingprocesses,qualityassuranceisanimportanttopic.Therefore,smalldefectsduringproductionneedtobedetectedreliably.Thisiswhatanomalydetectionaimsfor,detectinganomalousanddefectivepatternswhicharedifferentfromthenormalsamples.Thisproblemfacesanumberofuniquechallenges:ItisoftendifficulttoobtainalargeamountofanomalousdataThedifferencebetweenanormalsampleandananomaloussamplecanbeverysmallThetypeofanomaliesisnotalwaysknownbeforehandThesechallengesmaketrainingatraditionalclassifierdifficultandrequirespecialmethodsinordertosolvethem.Unsupervisedanomalydetectionandlocalizationmethodscanbecategorizedasdiscriminativeandgenerativemethods.Discriminativemethodsattempttomodelthedecisionboundarybetweenanomaloussamplesandnominalsamples.Thesemethodsgenerallyextracttheembeddingsfromanimageandcomparethemtothereferenceembeddingsfromthe“good”images.Thedistanceisusedastheanomalyscore.Thesemethodsgivedecentresultsforanomalydetectionbutoftenlackinterpretabilityasyoudon’tknowwhichpartoftheimagecausedtheimagetobeanomalous.AnexampleofsuchamethodisSPADEwhichrunsK-nearestneighbor(K-NN)clusteringonthecompletesetofembeddingvectorsattesttime.Thismeansthattheinferencecomplexityscaleslinearlywiththetrainingsetsize.Highinferencespeedisoftenimportantinmanufacturingwhichreducestheusefulnessofthismethodgreatly.Generativemethodsattempttomodeltheactualdistributionofeachclassfromwhichcanthenbesamplede.g.togeneratenewimages.Anomalydetectionapproachesusingthesemodelsarebasedontheideathattheanomaliescannotbegeneratedsincetheydonotexistinthetrainingset.Autoencoder-basedapproachestrytodetectanomaliesbycomparingtheoutputofanautoencodertoitsinput.Ahighreconstructionerrorshouldindicateananomalousregion.GAN-basedapproachesassumethatonlypositivesamplescanbegenerated.Althoughthesegenerativemethodsareveryintuitiveandinterpretable,theirperformanceislimitedbythefactthattheysometimeshavegoodreconstructionresultsforanomalousimagestoo.StateoftheartmethodsThissectionwilldiscussthreestate-of-the-artmethodsmoreindepth.Twodiscriminativeapproaches,andonegenerativeapproacharedescribed.Thesemethodswerechosenastheyrepresentthestate-of-the-artinanomalydetectionwhilehavingapracticalimplementationavailable.PaDiMBeforePaDiM,severaldiscriminativeapproacheshadbeenproposedwhicheitherrequiredeepneuralnetworktrainingwhichcanbecumbersomeortheyuseK-NNonalargedatasetwhichreducestheinferencespeedgreatly.Thesetwochallengesmighthinderthedeploymentofthealgorithmsinanindustrialenvironment.PatchDistributionModeling(PaDiM)aimstosolvethesechallenges.Theyuseapre-trainedCNN(ResNet,Wide-ResNet,oranEfficientNet)forembeddingextractionbasedonImageNetclassification.Theimagegetsdividedintopatchesandembeddingsareextractedforeachpatch.PaDiMusesallofthelayersofthepre-trainedCNN.Thisisdoneinordertocapturebothglobalcontextsandfinegraineddetails.Astheremightbealotofredundantinformationintheretheysubsampletheembeddingsbyrandomselection.Interestingly,thisworkedasgoodasdimensionalityreductiontechniqueslikePCAwhilebeingfaster.TheassumptionisthatallembeddingvectorsaresampledfromamultivariateGaussiandistribution.Theyestimatethesamplemeanandsamplecovarianceparametersofthisdistributionforeverypatch.TheresultisthateachpatchinthesetoftrainingimagesisdescribedbyamultivariateGaussiandistribution.PaDiMarchitectureoverview.TheanomalyscoreduringinferencenowgetsassignedbasedontheMahalanobisdistancebetweentheembeddingofatestpatchandthelearneddistributionforthatpatchlocation.Thefinalanomalyscoreisthemaximumoftheanomalymap.TheresultisanalgorithmwhichdoesnothavethescalabilityissueoftheKNNbasedmethodsasthereisnoneedtosortalargeamountofdistancevaluestogettheanomalyscoreofapatch.PatchCoreSimilarlytoPaDiM,PatchCoredividestheimagesintopatches.TheideaofPatchCoreisthatifasinglepatchisanomalousthewholeimagecanbeclassifiedasanomalous.PatchCoretriestosolvethesamechallengesPaDiMfaces.ThegoalofPatchCoreisthreefold:Maximizenominalinformationavailableattesttime.PaDiMlimitspatchlevelanomalydetectiontoMahalanobisdistancemeasuresspecificforeachpatch.InPatchCore,thefeaturesextractedduringtrainingphasearestoredinamemorybankwhichisequallyavailabletoallpatchesattesttime.ReducingbiasestowardsImageNetclasses.SimilartoPaDiM,apre-trainedCNNisusedfortheembeddingextraction.AdownsideofthisisthebiasestowardsImageNetclasses.Toreducethisbiasonlymid-levelfeaturesareusedaslowerlevelfeaturesaregenerallytoobroadandhigherlevelfeaturesaretospecifictoImageNet.Retainhighinferencespeeds.PatchCoreintroducescoresetsubsamplingwhichapproximatesthestructureoftheoriginaldatasetwhilereducingthesizegreatly.Thisdecreasesthecostofaanearestneighborsearchresultinginincreasedinferencespeeds.Duringtraining,embeddingsareextractedusingapre-trainedCNN,sub-sampledusingcoresetsubsampling,andstoredinamemorybank.Duringinferenceanearestneighborsearchisperformedonthememorybank.Thisarchitectureisdepictedintheimagebelow.PatchCorearchitectureoverview.CFlow-ADSofarwehavetalkedaboutdiscriminativemodels.Thelastmodelinthiscomparisonisadifferenttype,itisagenerativemodel.Agenerativemodeltellsyouhowlikelytheoccurrenceofagivenexampleis.Forexample,modelsthatpredictthenextwordinasequencearetypicallygenerativemodelsbecausetheycanassignaprobabilitytoasequenceofwords.TypesofgenerativenetworksusedforanomalydetectionincludeVariationalAutoEncoders(VAE),GenerativeAdversarialNetworks(GANs),andnormalizedflows.CFlow-ADisbasedonthelasttypeofnetworks,normalizedflows.CFlow-ADisbasedonaconditionalnormalizingflownetwork.NormalizedflownetworkscanbecomparedtoVAEswithacoupleoffavorablemathematicalproperties.Foranexcellentexplanationofnormalizedflows,seethisblog.Similartothepreviousapproachesanencoderisusedwhichispre-trainedonImageNet.Theembeddingvectorsarethenencodedusingaconventionalpositionalencoding(PE)intoconditionalvectors,henceConditionalFlow.Thedecoderisanormalizedflowdecoderwhichestimatesthelikelihoodoftheencodedfeatures.Theestimatedmulti-scalelikelihoodsareupsampledtoinputsizeandsummedtoproducetheanomalymap.Thisprocessisdepictedbelow.CFlow-ADarchitectureoverview.PerformancetestsOfficialimplementationsforallofthesemethodsareavailableonGitHub.However,thereisanovelopen-sourcePythonlibrarycalledAnomalibwhichimplementsalloftheabovealgorithmsinaneasytoaccessmanner.Anomalibcontainsasetofanomalydetectionalgorithms,asubsetofwhichwaspresentedabove.Thelibraryaimstoprovidecomponentstodesigncustomalgorithmsforspecificneeds,experimenttrackers,visualizers,andhyperparameteroptimizersallaimedatanomalydetection.DatasetApopulardatasetforanomalydetectioninmanufacturingprocessesistheMVTecdatasetwithfactorydefects.Itcontainsover5000high-resolutionimagesdividedintotendifferentobjectandfivetexturecategories.Eachcategorycomprisesasetofdefect-freetrainingimagesandatestsetofimageswithvariouskindsofdefectsaswellasimageswithoutdefects.TheexperimentsbelowwillbeconductedontheScrewobjectandtheCarpettexturecategories.MetricThemetricusedforcomparisonisthetheAreaUnderReceiverOperatingCharacteristiccurve(AUROC)wherethetruepositiverateisthepercentageofpixelscorrectlyclassifiedasanomalous.GettingstartedwithAnomalibInordertouseAnomalibyouwillneedatleastPython3.8+andacloneoftherepository.Installtherequirementslocatedintherequirementsfolder.Itisalsopossibletoinstallthelibraryusingpipinstallanomalib,howeverduetotheactivedevelopmentofthelibrarythisisnotrecommendeduntilreleasev0.2.5.Themodelsarelocatedinanomalib\models\ModelNamewhereeachofthemodelsareimplementedandthereisanaccompanyingconfig.yamlThisconfigfilecontainsinformationaboutthedataset(bydefaultMVTec),modelparameters,andthetrain/testparameters.Fortheexperimentsbelowthedefaultmodel,train,andtestparameterswereused.BydefaultallmodelsexpecttheMVTecdatasetindatasets\MVTec.Youcandownloadthedatasethere.Afterinstallingtherequirements,settingupthedataset,andmodifyingtheconfigfileasdesiredyoucantrainaspecificmodelusing:pythontools/train.py--model
延伸文章資訊
- 1通天塔Towards Total Recall in Industrial Anomaly Detection
PatchCore提供了有竞争力的推理时间,同时在检测和本地化方面实现了最先进的性能。在标准数据集上,MVTec AD PatchCore实现了图像级异常检测AUROC得分为99.1%,比第二优...
- 2Towards Total Recall in Industrial Anomaly Detection
In this paper, we extend on this line of work and propose PatchCore, which uses a maximally repre...
- 3amazon-research/patchcore-inspection - GitHub
Towards Total Recall in Industrial Anomaly Detection. This repository contains the implementation...
- 4异常检测(四): Towards Total Recall in Industrial Anomaly ...
PatchCore提供了有竞争力的推理时间,同时在检测和定位方面实现了最先进的 ... patchcore: Towards Total Recall in Industrial Anomaly...
- 5A practical guide to anomaly detection using Anomalib
A short guide on unsupervised anomaly detection and how to apply it ... The idea of PatchCore is ...