def verifyProxies(self, proxies): ''' Take as input dictionary of {name:proxystring} and verify that we can communicate with the server (by getting the detector or trainer). Return a dictionary with 'detector running' or 'trainer running' if running and 'configured' if not. Also print now so with long config files user sees output. ''' res = {} for key, value in proxies.iteritems(): # add short timeout value = value + ' -t 100' try: detector = easy.getDetector(value) res[key] = 'detector running' print("{0} {1}".format(key, res[key])) continue except: pass try: trainer = easy.getTrainer(value) res[key] = 'trainer running' print("{0} {1}".format(key, res[key])) continue except: pass try: trainer = easy.getFileServer(value) res[key] = 'FileServer running' print("{0} {1}".format(key, res[key])) continue except: pass try: trainer = easy.getCorpusServer(value) res[key] = 'Corpus running' print("{0} {1}".format(key, res[key])) continue except: pass res[key] = 'not running' print("{0} {1}".format(key, res[key])) return res
doWithNegativeSample = True if doWithNegativeSample: ############################################################### # With background data ''' Execute jousting for generating ROC points ''' contenders = [] for nWord in list_nWord: c1 = evaluate.Contender("bowROC_binary_"+str(nWord)) c1.trainerString = strTrainer c1.detectorString = strDetector trainer = easy.getTrainer(c1.trainerString) trainerProps = easy.getTrainerProperties(trainer) trainerProps.props["NumWords"] = str(nWord) c1.trainerProps = trainerProps c1.foundMap = {'1':easy.getPurpose('pos'), '0':easy.getPurpose('neg')} contenders.append(c1) sortedperfdata, perfdata = evaluate.joust( contenders, runset, folds=3 ) easy.showROCPlot(perfdata) ''' Extract only optimal ROC points ''' rocData_full,optimalIndices = easy.discardSuboptimal(perfdata) #rocData_full,optimalIndices = easy.discardSuboptimal(perfdata,"sboxes/BOW_Trainer_127_0_0_1")
Easy! mini tutorial Compare the performance of several trainers and/or detectors matz 11/26/2013 ''' import easy import easy.evaluate as ev # perform a comparative detector evaluation including # training of a model # easy.CVAC_ClientVerbosity = 4 posPurpose = easy.getPurpose('pos') negPurpose = easy.getPurpose('neg') contenders = [] # Bag of Words if (easy.getTrainer("BOW_Trainer")==None): print("BOW service(s) are insufficiently configured, skipping.") else: c = ev.Contender("BOW") c.trainerString = "BOW_Trainer" c.detectorString = "BOW_Detector" c.foundMap = {'1':posPurpose, '0':negPurpose} contenders.append( c ); # Histogram of Oriented Gradients if (easy.getTrainer("HOG_Trainer")==None): print("HOG service(s) are insufficiently configured, skipping.") else: c = ev.Contender("HOG") c.trainerString = "HOG_Trainer" c.detectorString = "HOGTest"
# it is the client's responsibility to upload them if not. # The putResult contains information about which files were actually transferred. # fileserver = easy.getFileServer( "FileService:default -p 10110 " + host ) putResult = easy.putAllFiles( fileserver, rs1 ) modelfile = "detectors/haarcascade_frontalface_alt.xml" if not fileserver.exists( easy.getCvacPath(modelfile) ): easy.putFile( fileserver, easy.getCvacPath(modelfile) ) # # detect remotely: note the host specification # print("------- Remote detection, local result display: -------") detector = easy.getDetector( "OpenCVCascadeDetector:default -p 10102 "+host ) results = easy.detect( detector, modelfile, rs1 ) easy.printResults( results ) # # Example 2: # Train on a remote machine, obtain the model file, and test locally. # Assume the files are on the remote machine, or transfer with putAllFiles. # trainer = easy.getTrainer( "bowTrain:default -p 10103 "+ host) # remote trainset = easy.createRunSet( "trainImg" ); trainedModel = easy.train( trainer, trainset ) easy.getFile( fileserver, trainedModel.file ) # downloads the model from remote print("{0}".format(trainedModel)) detector = easy.getDetector( "bowTest:default -p 10104" ) # local service testset = easy.createRunSet("testImg","UNPURPOSED" ) results = easy.detect( detector, trainedModel, testset )
def getTrainer( self ): if not self.trainer: self.trainer = easy.getTrainer( self.trainerString ) return self.trainer
#trainset = easy.createRunSet( "trainImg" ) trainset = cvac.RunSet() caset = easy.createRunSet("trainImg/ca") krset = easy.createRunSet("trainImg/kr") usset = easy.createRunSet("trainImg/us") easy.addToRunSet(trainset, caset, cvac.Purpose(cvac.PurposeType.MULTICLASS, 0)) easy.addToRunSet(trainset, krset, cvac.Purpose(cvac.PurposeType.MULTICLASS, 1)) easy.addToRunSet(trainset, usset, cvac.Purpose(cvac.PurposeType.MULTICLASS, 2)) easy.printRunSetInfo(trainset, printLabels=True) # # Connect to the trainer for a Bag of Words algorithm, then # train with the given runset # print("starting training, this might take a few minutes...") trainer = easy.getTrainer("BOW_Trainer") trainedModel = easy.train(trainer, trainset) # # Display information about the file in which the model is stored; # this is generally no concern to algorithm users and only of # interest to algorithm developers since it is algorithm-specific # zipfname = easy.getFSPath(trainedModel) print("{0}".format(zipfname)) zipf = zipfile.ZipFile(zipfname) print("Training model stored in file {0}".format(zipfname)) # print("file contents:\n{0}".format(zipf.namelist())) # # test the trained model on a separate set of images
Easy! mini tutorial Compare the performance of several trainers and/or detectors matz 11/26/2013 ''' import easy import easy.evaluate as ev # perform a comparative detector evaluation including # training of a model # easy.CVAC_ClientVerbosity = 4 posPurpose = easy.getPurpose('pos') negPurpose = easy.getPurpose('neg') contenders = [] # Bag of Words if (easy.getTrainer("BOW_Trainer") == None): print("BOW service(s) are insufficiently configured, skipping.") else: c = ev.Contender("BOW") c.trainerString = "BOW_Trainer" c.detectorString = "BOW_Detector" c.foundMap = {'1': posPurpose, '0': negPurpose} contenders.append(c) # Histogram of Oriented Gradients if (easy.getTrainer("HOG_Trainer") == None): print("HOG service(s) are insufficiently configured, skipping.") else: c = ev.Contender("HOG") c.trainerString = "HOG_Trainer" c.detectorString = "HOGTest"
# pick a subset: all license plates license_plates = categories['license plate'] print("There are {0} license plate labels.".format( len(license_plates) )) # another subset: all labels starting with "car" cars = [] for key in categories.keys(): if key.startswith("car"): cars.append( categories[key] ) print("There are {0} car-related labels.".format( len(cars) )) # Note that Labels are cached in the CorpusServer, but the corpus currently # needs to re-mirror if the CorpusServer is restarted because Labels are # not stored to disk. Images are stored to disk. quit() # done for now # Train a detector on license plates trainer = easy.getTrainer( "BOW_Trainer:default -p 10103 ") trainset = easy.createRunSet( license_plates, "pos" ) easy.printRunSetInfo( trainset ) licenseplateModel = easy.train( trainer, trainset ) # test the license plate detector on the known locations of cars; # this will only try to detect within the boundaries of cars. testset = easy.createRunSet( cars, "unpurposed" ) detector = easy.getDetector( "BOW_Detector:default -p 10104" ) results = easy.detect( detector, licenseplateModel, testset ) printResults( results )
# # detect remotely: note the host specification # detector = easy.getDetector("OpenCVCascadeDetector:default -p 10102 " + host) results = easy.detect(detector, modelfile, rs1) easy.printResults(results) # # Example 2: # Train on a remote machine, obtain the model file, and test locally. # Assume the files are on the remote machine, or transfer with putAllFiles. # If no local services are installed, this will be skipped. # print("------- Remote training, local detection: -------") try: detector = easy.getDetector( "BOW_Detector:default -p 10104") # local service trainer = easy.getTrainer("BOW_Trainer:default -p 10103 " + host) # remote trainset = easy.createRunSet("trainImg") trainedModel = easy.train(trainer, trainset) easy.getFile(fileserver, trainedModel) # downloads the model from remote print("obtained trained detector, stored in file {0}".format( easy.getFSPath(trainedModel))) testset = easy.createRunSet("testImg", "UNPURPOSED") results = easy.detect(detector, trainedModel, testset) easy.printResults(results) except: print("Cannot connect to local detector. Have you started the services?\n"\ "This part of the demo does not work with the client-only distribution.")
# it is the client's responsibility to upload them if not. # The putResult contains information about which files were actually transferred. # fileserver = easy.getFileServer( "FileService:default -p 10110 " + host ) putResult = easy.putAllFiles( fileserver, rs1 ) modelfile = "detectors/haarcascade_frontalface_alt.xml" if not fileserver.exists( easy.getCvacPath(modelfile) ): easy.putFile( fileserver, easy.getCvacPath(modelfile) ) # # detect remotely: note the host specification # print("------- Remote detection, local result display: -------") detector = easy.getDetector( "OpenCVCascadeDetector:default -p 10102 "+host ) results = easy.detect( detector, modelfile, rs1 ) easy.printResults( results ) # # Example 2: # Train on a remote machine, obtain the model file, and test locally. # Assume the files are on the remote machine, or transfer with putAllFiles. # trainer = easy.getTrainer( "BOW_Trainer:default -p 10103 "+ host) # remote trainset = easy.createRunSet( "trainImg" ); trainedModel = easy.train( trainer, trainset ) easy.getFile( fileserver, trainedModel ) # downloads the model from remote print("{0}".format(trainedModel)) detector = easy.getDetector( "BOW_Detector:default -p 10104" ) # local service testset = easy.createRunSet("testImg","UNPURPOSED" ) results = easy.detect( detector, trainedModel, testset )
''' Easy! mini tutorial Repeatedly train and evaluate for efficient label use; bootstrap. matz 6/21/2013 ''' import os import easy # # Create a training set from one sample each of 9 corporate logos # trainset1 = easy.createRunSet( "corporate_logos" ) # train, round 1 trainer = easy.getTrainer( "BOW_Trainer") model1 = easy.train( trainer, trainset1 ) # evaluate the model on a separate test set, images and videos # in DataDir/testdata1 testset1 = easy.createRunSet( "testImg", "UNPURPOSED" ) easy.printRunSetInfo( testset1 ) detector = easy.getDetector( "BOW_Detector" ) result1 = easy.detect( detector, model1, testset1 ) easy.printResults(result1) # sort the images from the testdata1 folder into subfolders of # "testresults1" corresponding to the found labels; # if multiple labels were found per original, consider only # the label with the highest confidence easy.sortIntoFolders( result1, outfolder="testresults1", multi="highest")
# pick a subset: all license plates license_plates = categories['license plate'] print("There are {0} license plate labels.".format( len(license_plates) )) # another subset: all labels starting with "car" cars = [] for key in categories.keys(): if key.startswith("car"): cars.append( categories[key] ) print("There are {0} car-related labels.".format( len(cars) )) # Note that Labels are cached in the CorpusServer, but the corpus currently # needs to re-mirror if the CorpusServer is restarted because Labels are # not stored to disk. Images are stored to disk. quit() # done for now # Train a detector on license plates trainer = easy.getTrainer( "bowTrain:default -p 10103 ") trainset = easy.createRunSet( license_plates, "pos" ) easy.printRunSetInfo( trainset ) licenseplateModel = easy.train( trainer, trainset ) # test the license plate detector on the known locations of cars; # this will only try to detect within the boundaries of cars. testset = easy.createRunSet( cars, "unpurposed" ) detector = easy.getDetector( "bowTest:default -p 10104" ) results = easy.detect( detector, licenseplateModel, testset ) printResults( results )
# pick a subset: all license plates license_plates = categories['license plate'] print("There are {0} license plate labels.".format(len(license_plates))) # another subset: all labels starting with "car" cars = [] for key in categories.keys(): if key.startswith("car"): cars.append(categories[key]) print("There are {0} car-related labels.".format(len(cars))) # Note that Labels are cached in the CorpusServer, but the corpus currently # needs to re-mirror if the CorpusServer is restarted because Labels are # not stored to disk. Images are stored to disk. quit() # done for now # Train a detector on license plates trainer = easy.getTrainer("BOW_Trainer:default -p 10103 ") trainset = easy.createRunSet(license_plates, "pos") easy.printRunSetInfo(trainset) licenseplateModel = easy.train(trainer, trainset) # test the license plate detector on the known locations of cars; # this will only try to detect within the boundaries of cars. testset = easy.createRunSet(cars, "unpurposed") detector = easy.getDetector("BOW_Detector:default -p 10104") results = easy.detect(detector, licenseplateModel, testset) printResults(results)
list_nWord = [5, 10, 15, 20] doWithNegativeSample = True if doWithNegativeSample: ############################################################### # With background data ''' Execute jousting for generating ROC points ''' contenders = [] for nWord in list_nWord: c1 = evaluate.Contender("bowROC_binary_" + str(nWord)) c1.trainerString = strTrainer c1.detectorString = strDetector trainer = easy.getTrainer(c1.trainerString) trainerProps = easy.getTrainerProperties(trainer) trainerProps.props["NumWords"] = str(nWord) c1.trainerProps = trainerProps c1.foundMap = { '1': easy.getPurpose('pos'), '0': easy.getPurpose('neg') } contenders.append(c1) sortedperfdata, perfdata = evaluate.joust(contenders, runset, folds=3) easy.showROCPlot(perfdata) ''' Extract only optimal ROC points ''' rocData_full, optimalIndices = easy.discardSuboptimal(perfdata)