What is Image Recognition and How it is Used?
文章推薦指數: 80 %
Image recognition refers to technologies that identify places, logos, people, objects, buildings, and several other variables in digital ... AllCourses DataScienceCourses Master’sinDataScience M.TechinDataScience(Bangalore) DataScienceSpecialization PGinBusinessAnalytics DecisionMakingUsingDataScience DataScienceandMachineLearning ArtificialIntelligenceCourses PGP–ArtificialIntelligenceandMachineLearning(Online) PGPinArtificialIntelligenceandMachineLearning(Classroom) PGP–ArtificialIntelligenceforLeaders PGinMachineLearning PGDiplomainComputerScienceandAI–IIITDelhi AIforHighSchoolStudents ManagementCourses ExecutivePGinManagement MBAinDigitalMarketing&DataSciencefromJAIN OnlineMBAinShivNadar BachelorofBusinessfromDeakinUniversity BBADegreeFromJainUniversity ExecutiveMBAfromPES NUSFutureLeadersProgram PGinProductManagementandAnalytics BachelorofBusinessAnalyticsFromDeakin CloudComputingCourses PGPinCloudComputingCourse AdvancedSoftwareEngineeringCourseIITMadras CyberSecurityCourses PGPrograminCyberSecurity DigitalMarketing&SalesCourses PGPinStrategicDigitalMarketingCourse AdvancedCertificateinStrategicDigitalMarketing AdvancedCertificatePrograminSales SoftwareEngineeringCourses SoftwareDevelopmentIITRoorkee JainOnlineMCA AdvancedSoftwareEngineeringIITMadras DesignThinkingCourses StanfordExecutiveDesignThinking PGinUserExperienceDesignIIT-Bombay FreeCourses DataScienceFreeCourses DataScienceFoundations ProbabilityforDataScience LinearProgrammingforDataScience IntroductiontoR DataVisualizationusingTableau MachineLearningFreeCourses BasicsofMachineLearning StatisticsforMachineLearning FakeNewsDetectionusingMachineLearning ProductCategorizationusingMachineLearning PythonforMachineLearning ArtificialIntelligenceFreeCourses IntroductiontoArtificialIntelligence ArtificialIntelligenceProjects IntroductiontoDeepLearning FoundationsofAI IntroductiontoNaturalLanguageProcessing CyberSecurityFreeCourses IntroductiontoCyberSecurity TypesofCyberSecurity CyberSecurityThreats CyberSecurityForLeadership CyberForensics CloudComputingFreeCourses CloudFoundations CloudComputingServiceModels BestCloudCertifications CloudComputingArchitecture ApplicationsofCloudComputing BigDataFreeCourses IntroductiontoBigDataandHadoop MasteringBigDataAnalytics HadoopMapReduce IntroductiontoApacheHive SparkBasics MarketingFreeCourses IntroductiontoDigitalMarketing SEOforBeginners ContentMarketingBasics SearchEngineMarketing AffiliateMarketing EmailMarketing InterviewQuestions PythonInterviewQuestions JavaInterviewQuestions SQLInterviewQuestions MachineLearningInterviewQuestions SeleniumInterviewQuestions NLPInterviewQuestions C++InterviewQuestions AWSInterviewQuestions RPAInterviewQuestions CloudComputingInterviewQuestions Career CareerTopics CareerinAI Careerinpython CareerinCyberSecurity CareerinDigitalMarketer CareerinCloudArchitect CareerinBusinessAnalytics CareerinEthicalHacking Salary DataScientistSalary PythonDeveloperSalary CyberSecuritySalary DataAnalystSalary BusinessAnalystSalary MachineLearningSalary BlockchainDeveloperSalary DigitalMarketingSalary DevOpsSalary Skills DataScientistSkills BigDataAnalyticsSkills CloudArchitectSkills ManagementSkills CloudEngineerSkills AdministrativeSkills DigitalMarketingSkills DevOpsEngineerSkills Resume WebDeveloperResume BusinessAnalystResume DataScienceResume JavaDeveloperResume DigitalMarketingResume PythonDeveloperResume DevOpsEngineerResume ArtificialIntelligenceResume MachineLearningResume Jobs HighestPayingJobsintheWorld HighestPayingFinanceJobs HighestPayingFreelancingJobs HighestPayingITJobs Non–TechnicalJobs CoverLetter CareerBreakCoverLetter DevOpsCoverLetter ContentManagerCoverLetter SEOManagerCoverLetter AndroidDeveloperCoverLetter OtherResources ArtificialIntelligence MachineLearning PythonProgramming DataScience CloudComputing CyberSecurity BusinessManagement VisualStories Search AllCourses DataScienceCourses Master’sinDataScience M.TechinDataScience(Bangalore) DataScienceSpecialization PGinBusinessAnalytics DecisionMakingUsingDataScience DataScienceandMachineLearning ArtificialIntelligenceCourses PGP–ArtificialIntelligenceandMachineLearning(Online) PGPinArtificialIntelligenceandMachineLearning(Classroom) PGP–ArtificialIntelligenceforLeaders PGinMachineLearning PGDiplomainComputerScienceandAI–IIITDelhi AIforHighSchoolStudents ManagementCourses ExecutivePGinManagement MBAinDigitalMarketing&DataSciencefromJAIN OnlineMBAinShivNadar BachelorofBusinessfromDeakinUniversity BBADegreeFromJainUniversity ExecutiveMBAfromPES NUSFutureLeadersProgram PGinProductManagementandAnalytics BachelorofBusinessAnalyticsFromDeakin CloudComputingCourses PGPinCloudComputingCourse AdvancedSoftwareEngineeringCourseIITMadras CyberSecurityCourses PGPrograminCyberSecurity DigitalMarketing&SalesCourses PGPinStrategicDigitalMarketingCourse AdvancedCertificateinStrategicDigitalMarketing AdvancedCertificatePrograminSales SoftwareEngineeringCourses SoftwareDevelopmentIITRoorkee JainOnlineMCA AdvancedSoftwareEngineeringIITMadras DesignThinkingCourses StanfordExecutiveDesignThinking PGinUserExperienceDesignIIT-Bombay FreeCourses DataScienceFreeCourses DataScienceFoundations ProbabilityforDataScience LinearProgrammingforDataScience IntroductiontoR DataVisualizationusingTableau MachineLearningFreeCourses BasicsofMachineLearning StatisticsforMachineLearning FakeNewsDetectionusingMachineLearning ProductCategorizationusingMachineLearning PythonforMachineLearning ArtificialIntelligenceFreeCourses IntroductiontoArtificialIntelligence ArtificialIntelligenceProjects IntroductiontoDeepLearning FoundationsofAI IntroductiontoNaturalLanguageProcessing CyberSecurityFreeCourses IntroductiontoCyberSecurity TypesofCyberSecurity CyberSecurityThreats CyberSecurityForLeadership CyberForensics CloudComputingFreeCourses CloudFoundations CloudComputingServiceModels BestCloudCertifications CloudComputingArchitecture ApplicationsofCloudComputing BigDataFreeCourses IntroductiontoBigDataandHadoop MasteringBigDataAnalytics HadoopMapReduce IntroductiontoApacheHive SparkBasics MarketingFreeCourses IntroductiontoDigitalMarketing SEOforBeginners ContentMarketingBasics SearchEngineMarketing AffiliateMarketing EmailMarketing InterviewQuestions PythonInterviewQuestions JavaInterviewQuestions SQLInterviewQuestions MachineLearningInterviewQuestions SeleniumInterviewQuestions NLPInterviewQuestions C++InterviewQuestions AWSInterviewQuestions RPAInterviewQuestions CloudComputingInterviewQuestions Career CareerTopics CareerinAI Careerinpython CareerinCyberSecurity CareerinDigitalMarketer CareerinCloudArchitect CareerinBusinessAnalytics CareerinEthicalHacking Salary DataScientistSalary PythonDeveloperSalary CyberSecuritySalary DataAnalystSalary BusinessAnalystSalary MachineLearningSalary BlockchainDeveloperSalary DigitalMarketingSalary DevOpsSalary Skills DataScientistSkills BigDataAnalyticsSkills CloudArchitectSkills ManagementSkills CloudEngineerSkills AdministrativeSkills DigitalMarketingSkills DevOpsEngineerSkills Resume WebDeveloperResume BusinessAnalystResume DataScienceResume JavaDeveloperResume DigitalMarketingResume PythonDeveloperResume DevOpsEngineerResume ArtificialIntelligenceResume MachineLearningResume Jobs HighestPayingJobsintheWorld HighestPayingFinanceJobs HighestPayingFreelancingJobs HighestPayingITJobs Non–TechnicalJobs CoverLetter CareerBreakCoverLetter DevOpsCoverLetter ContentManagerCoverLetter SEOManagerCoverLetter AndroidDeveloperCoverLetter OtherResources ArtificialIntelligence MachineLearning PythonProgramming DataScience CloudComputing CyberSecurity BusinessManagement VisualStories More Search AllCourses DataScienceCourses Master’sinDataScience M.TechinDataScience(Bangalore) DataScienceSpecialization PGinBusinessAnalytics DecisionMakingUsingDataScience DataScienceandMachineLearning ArtificialIntelligenceCourses PGP–ArtificialIntelligenceandMachineLearning(Online) PGPinArtificialIntelligenceandMachineLearning(Classroom) PGP–ArtificialIntelligenceforLeaders PGinMachineLearning PGDiplomainComputerScienceandAI–IIITDelhi AIforHighSchoolStudents ManagementCourses ExecutivePGinManagement MBAinDigitalMarketing&DataSciencefromJAIN OnlineMBAinShivNadar BachelorofBusinessfromDeakinUniversity BBADegreeFromJainUniversity ExecutiveMBAfromPES NUSFutureLeadersProgram PGinProductManagementandAnalytics BachelorofBusinessAnalyticsFromDeakin CloudComputingCourses PGPinCloudComputingCourse AdvancedSoftwareEngineeringCourseIITMadras CyberSecurityCourses PGPrograminCyberSecurity DigitalMarketing&SalesCourses PGPinStrategicDigitalMarketingCourse AdvancedCertificateinStrategicDigitalMarketing AdvancedCertificatePrograminSales SoftwareEngineeringCourses SoftwareDevelopmentIITRoorkee JainOnlineMCA AdvancedSoftwareEngineeringIITMadras DesignThinkingCourses StanfordExecutiveDesignThinking PGinUserExperienceDesignIIT-Bombay FreeCourses DataScienceFreeCourses DataScienceFoundations ProbabilityforDataScience LinearProgrammingforDataScience IntroductiontoR DataVisualizationusingTableau MachineLearningFreeCourses BasicsofMachineLearning StatisticsforMachineLearning FakeNewsDetectionusingMachineLearning ProductCategorizationusingMachineLearning PythonforMachineLearning ArtificialIntelligenceFreeCourses IntroductiontoArtificialIntelligence ArtificialIntelligenceProjects IntroductiontoDeepLearning FoundationsofAI IntroductiontoNaturalLanguageProcessing CyberSecurityFreeCourses IntroductiontoCyberSecurity TypesofCyberSecurity CyberSecurityThreats CyberSecurityForLeadership CyberForensics CloudComputingFreeCourses CloudFoundations CloudComputingServiceModels BestCloudCertifications CloudComputingArchitecture ApplicationsofCloudComputing BigDataFreeCourses IntroductiontoBigDataandHadoop MasteringBigDataAnalytics HadoopMapReduce IntroductiontoApacheHive SparkBasics MarketingFreeCourses IntroductiontoDigitalMarketing SEOforBeginners ContentMarketingBasics SearchEngineMarketing AffiliateMarketing EmailMarketing InterviewQuestions PythonInterviewQuestions JavaInterviewQuestions SQLInterviewQuestions MachineLearningInterviewQuestions SeleniumInterviewQuestions NLPInterviewQuestions C++InterviewQuestions AWSInterviewQuestions RPAInterviewQuestions CloudComputingInterviewQuestions Career CareerTopics CareerinAI Careerinpython CareerinCyberSecurity CareerinDigitalMarketer CareerinCloudArchitect CareerinBusinessAnalytics CareerinEthicalHacking Salary DataScientistSalary PythonDeveloperSalary CyberSecuritySalary DataAnalystSalary BusinessAnalystSalary MachineLearningSalary BlockchainDeveloperSalary DigitalMarketingSalary DevOpsSalary Skills DataScientistSkills BigDataAnalyticsSkills CloudArchitectSkills ManagementSkills CloudEngineerSkills AdministrativeSkills DigitalMarketingSkills DevOpsEngineerSkills Resume WebDeveloperResume BusinessAnalystResume DataScienceResume JavaDeveloperResume DigitalMarketingResume PythonDeveloperResume DevOpsEngineerResume ArtificialIntelligenceResume MachineLearningResume Jobs HighestPayingJobsintheWorld HighestPayingFinanceJobs HighestPayingFreelancingJobs HighestPayingITJobs Non–TechnicalJobs CoverLetter CareerBreakCoverLetter DevOpsCoverLetter ContentManagerCoverLetter SEOManagerCoverLetter AndroidDeveloperCoverLetter OtherResources ArtificialIntelligence MachineLearning PythonProgramming DataScience CloudComputing CyberSecurity BusinessManagement VisualStories More Search HomeArtificialIntelligenceAIProjectsWhatisImageRecognitionandHowitisUsed? ArtificialIntelligenceAIProjectsDeepLearningNeuralNetworks Share FacebookTwitterWhatsApp WhatisImagerecognition?HowdoesImagerecognitionwork?WorkingofConvolutionalandPoolinglayersImagerecognitionusingPythonImagerecognitionwithapre-trainednetwork ThevisualperformanceofHumansismuchbetterthanthatofcomputers,probablybecauseofsuperiorhigh-levelimageunderstanding,contextualknowledge,andmassivelyparallelprocessing.Buthumancapabilitiesdeterioratedrasticallyafteranextendedperiodofsurveillance,alsocertainworkingenvironmentsareeitherinaccessibleortoohazardousforhumanbeings.Soforthesereasons,automaticrecognitionsystemsaredevelopedforvariousapplications.Drivenbyadvancesincomputingcapabilityandimageprocessingtechnology,computermimicryofhumanvisionhasrecentlygainedgroundinanumberofpracticalapplications. LearnwithIndia’s#1programinAIMLandbuildjob-readyskills.DesignedincollaborationwiththeUniversityofTexasatAustin,thisprogramoffersacomprehensivecurriculumtohelpprofessionalsupskillfast.Youwillpickupindustry-valuedskillsinalltheAIMLconceptslikeMachineLearning,ComputerVision,NaturalLanguageProcessing,NeuralNetworks,andmore.Thisprogramalsoincludesseveralguidedprojectstohelpyoubecomeexperts.GreatLearningalsoofferspersonalizedcareercoachingandinterviewpreparationtohelpyouacetherecruitingprocess. WhatisImagerecognition? Imagerecognitionreferstotechnologiesthatidentifyplaces,logos,people,objects,buildings,andseveralothervariablesindigitalimages.Itmaybeveryeasyforhumanslikeyouandmetorecognisedifferentimages,suchasimagesofanimals.Wecaneasilyrecognisetheimageofacatanddifferentiateitfromanimageofahorse.Butitmaynotbesosimpleforacomputer. Adigitalimageisanimagecomposedofpictureelements,alsoknownaspixels,eachwithfinite,discretequantitiesofnumericrepresentationforitsintensityorgreylevel.Sothecomputerseesanimageasnumericalvaluesofthesepixelsandinordertorecogniseacertainimage,ithastorecognisethepatternsandregularitiesinthisnumericaldata. Animageofadogrepresentedby40x40pixels. Imagerecognitionshouldnotbeconfusedwithobjectdetection.Inobjectdetection,weanalyseanimageandfinddifferentobjectsintheimagewhileimagerecognitiondealswithrecognisingtheimagesandclassifyingthemintovariouscategories. HowdoesImagerecognitionwork? Typicallythetaskofimagerecognitioninvolvesthecreationofaneuralnetworkthatprocessestheindividualpixelsofanimage.Thesenetworksarefedwithasmanypre-labelledimagesaswecan,inorderto“teach”themhowtorecognizesimilarimages. Soletmebreaktheprocessforyouinsomesimplesteps: Weneedadatasetcontainingimageswiththeirrespectivelabels.Forexample,animageofadogmustbelabelledasadogorsomethingthatwecanunderstand.Next,theseimagesaretobefedintoaNeuralNetworkandthentrainedonthem.Usually,forthetasksconcernedwithimages,weuseconvolutionalneuralnetworks.ThesenetworksconsistofconvolutionallayersandpoolinglayersinadditiontoMultiperceptronlayers(MLP).Theworkingofconvolutionalandpoolinglayersareexplainedinthebelow.Wefeedintheimagethatisnotinthetrainingsetandgetpredictions. Inthecomingsections,byfollowingthesesimplestepswewillmakeaclassifierthatcanrecogniseRGBimagesof10differentkindsofanimals. Note:Themodelwillonlybeabletorecogniseanimalsthatareinthedataset.Forexample,amodeltrainedtorecognisedogsandcatcannotrecogniseboats WorkingofConvolutionalandPoolinglayers ConvolutionallayersandPoolinglayersarethemajorbuildingblocksusedinconvolutionalneuralnetworks.Letusseethemindetail HowdoesConvolutionalLayerwork? Theconvolutionallayer’sparametersconsistofasetoflearnablefilters(orkernels),whichhaveasmallreceptivefield.Thesefiltersscanthroughimagepixelsandgatherinformationinthebatchofpictures/photos.Convolutionallayersconvolvetheinputandpassitsresulttothenextlayer.Thisisliketheresponseofaneuroninthevisualcortextoaspecificstimulus. Convolutionoperation Belowisanexampleofhowconvolutionoperationisdoneonanimage.Asimilarprocessisdoneforallthepixels. Hereisanexampleofanimageinourtestsetthathasbeenconvolutedwithfourdifferentfiltersandhencewegetfourdifferentimages. HowdoesPoolingLayerwork? Thepoolingoperationinvolvesslidingatwo-dimensionalfilterovereachchannelofthefeaturemapandsummarisingthefeatureslyingwithintheregioncoveredbythefilter.Apoolinglayerisusuallyincorporatedbetweentwosuccessiveconvolutionallayers.Thepoolinglayerreducesthenumberofparametersandcomputationbydown-samplingtherepresentation.Thepoolingfunctioncanbeeithermaxoraverage.Maxpoolingiscommonlyusedasitworksbetter Thepoolingoperationinvolvesslidingatwo-dimensionalfilterovereachchannelofthefeaturemapandsummarisingthefeatureslyingwithintheregioncoveredbythefilter.Thisprocessisillustratedbelow. Whenpassingthefourimageswegotafterconvolutionthroughamax-poolinglayerofdimension2×2,wegetthisasoutput Aswecansee,thedimensionshavedecreasedbyonehalfbuttheinformationintheimageisstillpreserved. ImagerecognitionusingPython HereIamgoingtousedeeplearning,morespecificallyconvolutionalneuralnetworksthatcanrecogniseRGBimagesoftendifferentkindsofanimals.AnRGBimagecanbeviewedasthreedifferentimages(aredscaleimage,agreenscaleimageandabluescaleimage)stackedontopofeachother,andwhenfedintothered,greenandblueinputsofacolourmonitor,itproducesacolourimageonthescreen.Weuseadatasetknownas Animals-10fromKaggle. So,letusstartmakingaclassifierusingPythonandKeras.WearegoingtoimplementtheprograminColabasweneedalotofprocessingpowerandGoggleColabprovidesfreeGPUs.Theoverallstructureoftheneuralnetworkwearegoingtousecanbeseeninthisimage TheveryfirststepistogetdataonyourColabnotebook.Youdon’tneedahigh-speedinternetforthisasitisdirectlydownloadedintogooglecloudfromtheKagglecloud. Forgettingthedata,followthesesteps: GotoyourKaggleaccountandclickonmyaccounts.Incaseyoudon’thaveaKaggleaccount,createone,itisfree.Next,downloadthekaggle.jsonfilebyclickingonthebutton‘createnewAPItoken’.GotoyourColabnotebookandstartcoding Inthistutorial,weareusingImageGeneratortolabeltheimages.So,incaseyouareusingsomeotherdataset,besuretoputallimagesofthesameclassinthesamefolder.Andthenplaceallthefoldersinthefolder. #Thesestepsaretobefollowedwhenusinggooglecolab #andimportingdatafromkaggle fromgoogle.colabimportfiles #InstallKagglelibrary !pipinstall-qkaggle fromgoogle.colabimportfiles #uploadthekaggle.jsonfile uploaded=files.upload() #makeadiectoryinwhichkajggle.jsonisstored #!mkdir~/.kaggle !cpkaggle.json~/.kaggle/ #downloadthedatasetintothecolab !kaggledatasetsdownload-dalessiocorrado99/animals10 #unzipthedata !unzip/content/animals10.zip #Incaseyouareusingalocalmachine,startfromhere. importtensorflowastf fromtensorflow.keras.preprocessing.imageimportImageDataGenerator fromtensorflow.keras.layersimportInput,Dense fromtensorflow.kerasimportSequential,Model fromtensorflow.keras.layersimportBatchNormalization,Dropout,Flatten fromtensorflow.keras.layersimportConv2D fromtensorflow.keras.layersimportMaxPooling2D fromtensorflow.keras.layersimportGlobalAveragePooling2D fromtensorflow.keras.preprocessingimportimage importnumpyasnp importos importcv2 train_data_dir='/kaggle/input/animals10/raw-img/' img_height=128 img_width=128 batch_size=64 nb_epochs=20 train_datagen=ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, validation_split=0.2)#setvalidationsplit train_generator=train_datagen.flow_from_directory( train_data_dir, target_size=(img_height,img_width), batch_size=batch_size, class_mode='categorical', subset='training')#setastrainingdata validation_generator=train_datagen.flow_from_directory( train_data_dir,#samedirectoryastrainingdata target_size=(img_height,img_width), batch_size=batch_size, class_mode='categorical', subset='validation')#setasvalidationdata model=Sequential() inputShape=(128,128,3) model.add(Conv2D(64,(3,3),padding="same",activation='relu',input_shape=inputShape)) model.add(BatchNormalization()) model.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu')) model.add(MaxPooling2D((2,2))) model.add(Dropout(0.4)) model.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu')) model.add(MaxPooling2D((2,2))) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Flatten()) model.add(Dropout(0.4)) model.add(Dense(64,activation='relu')) model.add(BatchNormalization()) model.add(Dense(10,activation='softmax')) model.summary() #compilethemodel model.compile(optimizer="adam",loss="categorical_crossentropy",metrics=["accuracy"]) #trainthemodel,thissteptakesalotoftime(hours) model.fit_generator( train_generator, steps_per_epoch=train_generator.samples//batch_size, validation_data=validation_generator, validation_steps=validation_generator.samples//batch_size, epochs=nb_epochs) #savethemodelforlateruse model.save('path\nameofmodel') #orderoftheanimalsarrayisimportant #animals=["dog","horse","elephant","butterfly","chicken","cat","cow","sheep","spider","squirrel"] bio_animals=sorted(os.listdir('/content/raw-img')) categories={'cane':'dog',"cavallo":"horse","elefante":"elephant","farfalla":"butterfly","gallina":"chicken","gatto":"cat","mucca":"cow","pecora":"sheep","scoiattolo":"squirrel","ragno":"spider"} defrecognise(pred): animals=[categories.get(item,item)foriteminbio_animals] print("Theimageconsistof",animals[pred]) fromtensorflow.keras.preprocessingimportimage importnumpyasnp img=image.load_img("https://d1m75rqqgidzqn.cloudfront.net/kaggle/input/testttt/OIF-e2bexWrojgtQnAPPcUfOWQ.jpeg",target_size=(128,128)) x=image.img_to_array(img) x=np.expand_dims(x,axis=0) prediction=model.predict(x) #prediction recognise(np.argmax(prediction)) test_data_path="/content/testdata/test_animals" files=sorted(os.listdir(test_data_path)) files=files[1:] forimginfiles: x=cv2.imread(os.path.join(test_data_path,img)) cv2_imshow(x) recognise(np.argmax(predict[files.index(img)])) print("") Output:Idownloadedsomeimagesfromgoogleandusedthismodeltolabelthem.Herearetheresults TopredictImages,weneedtouploadthemtotheColab(getsdeletedautomaticallyafterthesessionisended)oryoucanevendownloadthemtoyourgoogledrivepermanently. Followthestepsbelowtocreateadirectoryfortestdata CreateanewfoldercalledtestdataNext,createanotherfolderinthisfoldernamedtestanimalsUploadyourimagestothisfolder. Aswecansee,thismodeldidadecentjobandpredictedallimagescorrectlyexcepttheonewithahorse.Thisisbecausethesizeofimagesisquitebigandtogetdecentresults,themodelhastobetrainedforatleast100epochs.Butduetothelargesizeofthedatasetandimages,Icouldonlytrainitfor20epochs(took4hoursonColab). Toincreasetheaccuracyandgetanaccurateprediction,wecanuseapre-trainedmodelandthencustomisethataccordingtoourproblem. ImageRecognitionwithapre-trainedmodel Inthisexample,IamgoingtousetheXceptionmodelthathasbeenpre-trainedonImagenetdataset.ThistechniqueisbasicallycalledTransferlearning. XceptionModelisproposedbyFrancoisChollet.XceptionisanextensionoftheinceptionArchitecturewhichreplacesthestandardInceptionmoduleswithdepthwiseSeparableConvolutions.ThismodelisavailableonKerasandwejustneedtoimportit.Solet’sstartcoding fromgoogle.colabimportfiles #InstallKagglelibrary !pipinstall-qkaggle fromgoogle.colabimportfiles #uploadthekaggle.jsonfile uploaded=files.upload() #makeadiectoryinwhichkajggle.jsonisstored #!mkdir~/.kaggle !cpkaggle.json~/.kaggle/ #downloadthedatasetintothecolab !kaggledatasetsdownload-dalessiocorrado99/animals10 #unzipthedata !unzip/content/animals10.zip importtensorflowastf fromtensorflow.keras.preprocessing.imageimportImageDataGenerator fromtensorflow.keras.layersimportInput,Dense fromtensorflow.kerasimportSequential,Model fromtensorflow.keras.layersimportBatchNormalization,Dropout,Flatten fromtensorflow.keras.layersimportConv2D fromtensorflow.keras.layersimportMaxPooling2D fromtensorflow.keras.layersimportGlobalAveragePooling2D fromtensorflow.keras.preprocessingimportimage fromtensorflow.keras.layersimportGlobalAveragePooling2D importnumpyasnp importos importcv2 train_data_dir='/kaggle/input/animals10/raw-img/' img_height=299 img_width=299 batch_size=64 nb_epochs=20 train_datagen=ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, validation_split=0.2)#setvalidationsplit train_generator=train_datagen.flow_from_directory( train_data_dir, target_size=(img_height,img_width), batch_size=batch_size, class_mode='categorical', subset='training')#setastrainingdata validation_generator=train_datagen.flow_from_directory( train_data_dir,#samedirectoryastrainingdata target_size=(img_height,img_width), batch_size=batch_size, class_mode='categorical', subset='validation')#setasvalidationdata #importapre-trainedmodel,withoutthetoplayers.Wewillcustomise #thetoplayersforourproblem base_model=tf.keras.applications.Xception(include_top=False,input_shape=(299,299,3)) #Fornowfreezetheinitiallayersanddonottrainthem forlayerinbase_model.layers: layer.trainable=False #createacustomtopclassifier x=base_model.output x=GlobalAveragePooling2D()(x) x=Dense(516,activation='relu')(x) #sinceourproblemhas10differntanimalswehave10classes #thuswekeep10nodesinthelastlayer predictions=Dense(10,activation='softmax')(x) model=Model(inputs=base_model.inputs,outputs=predictions) model.summary() model.compile(optimizer="adam",loss="categorical_crossentropy",metrics=["accuracy"]) model.fit_generator( train_generator, steps_per_epoch=train_generator.samples//batch_size, validation_data=validation_generator, validation_steps=validation_generator.samples//batch_size, epochs=nb_epochs) #Nowunfreezethelayersandtrainthewholemodel forlayerinbase_model.layers: layer.trainable=True history=model.fit_generator( train_generator, steps_per_epoch=train_generator.samples//batch_size, validation_data=validation_generator, validation_steps=validation_generator.samples//batch_size, epochs=nb_epochs) model.save('path\nameofmodel') #orderoftheanimalsarrayisimportant #animals=["dog","horse","elephant","butterfly","chicken","cat","cow","sheep","spider","squirrel"] bio_animals=sorted(os.listdir('/content/raw-img')) categories={'cane':'dog',"cavallo":"horse","elefante":"elephant","farfalla":"butterfly","gallina":"chicken","gatto":"cat","mucca":"cow","pecora":"sheep","scoiattolo":"squirrel","ragno":"spider"} defrecognise(pred): animals=[categories.get(item,item)foriteminbio_animals] print("Theimageconsistof",animals[pred]) fromtensorflow.keras.preprocessingimportimage importnumpyasnp img=image.load_img("https://d1m75rqqgidzqn.cloudfront.net/kaggle/input/testttt/OIF-e2bexWrojgtQnAPPcUfOWQ.jpeg",target_size=(299,299)) x=image.img_to_array(img) x=np.expand_dims(x,axis=0) prediction=model.predict(x) #prediction recognise(np.argmax(prediction)) test_data_path="/content/testdata/test_animals" files=sorted(os.listdir(test_data_path)) files=files[1:] forimginfiles: x=cv2.imread(os.path.join(test_data_path,img)) cv2_imshow(x) recognise(np.argmax(predict[files.index(img)])) print("") Output: Aswecanseethemodelmakesaccuratepredictionsonallofthedatainourtestdataset.Ihavesavedthismodel,henceitcanbeusedatanytimebyusingthefunctionshownbelow: fromtensorflowimportkeras model=keras.models.load_model('path.h5') #e.g.model=keras.models.load_model('/content/simpleconvkag.h5') Incaseyouwantthecopyofthetrainedmodelorhaveanyqueriesregardingthecode,feelfreetodropacomment. Thisbringsustotheendofthisarticle.Wehavelearnedhowimagerecognitionworksandclassifieddifferentimagesofanimals. IfyouwishtolearnmoreaboutPythonandtheconceptsofMachinelearning,upskillwithGreatLearning’sPGProgramArtificialIntelligenceandMachineLearning. RELATEDARTICLESMOREFROMAUTHOR ArtificialIntelligence TopDeepLearningCoursesOnline-2022 ComputerVision Real-timeFacedetection|FaceMaskDetectionusingOpenCV ComputerVision HowToApplyMachineLearningtoRecogniseHandwriting|HowtoRecognisehandwriting 2COMMENTS heyhowdidyoudecidearrayorderingoflabels Reply HiAshutosh, ItdependsonthedatasetweareworkingonandheremydatasetcontainedfoldersofeachanimalintheorderIhavementionedhere.Ifyouarefacingproblemsregardingthelabels,itismostprobablythatyourfoldersarenotinthesameorderasitwasinmycase.Youcanchangethelabelarrayaccordingtotheorderofyourfolders. Reply LEAVEAREPLYCancelreply Pleaseenteryourcomment! Pleaseenteryournamehere Youhaveenteredanincorrectemailaddress! Pleaseenteryouremailaddresshere Savemyname,email,andwebsiteinthisbrowserforthenexttimeIcomment. Pleaseenterananswerindigits:two×4= Δ GreatLearningisaned-techcompanythatoffersimpactfulandindustry-relevantprogramsinhigh-growthareas.Withastrongpresenceacrosstheglobe,wehaveempowered10,000+learnersfromover50countriesinachievingpositiveoutcomesfortheircareers.KnowMore FacebookInstagramLinkedinPinterestTwitter ©2021GreatLearningAllrightsreserved
延伸文章資訊
- 1What Is Image Recognition? | by Chris Kuo/Dr. Dataman
A common example of image recognition is optical character recognition (OCR). A scanner can ident...
- 2Image Recognition : A Complete Guide - Deepomatic
Image recognition, a subcategory of Computer Vision and Artificial Intelligence, represents a set...
- 3What Is Image Recognition? Definition from SearchEnterpriseAI
Image recognition, in the context of machine vision, is the ability of software to identify objec...
- 4關於AI 的A 到Z:I 代表圖片辨識(Image Recognition) - Google
圖片辨識(Image recognition) · 圖片辨識技術可讓AI 辨識我們周遭世界的物件並加以分類。 · 圖片辨識系統有時也稱為「電腦視覺」,能夠透過研究各式各樣的參考圖片,學會辨識 ...
- 5Vision AI | Derive Image Insights via ML | Cloud Vision API
Document classification