Пример #1
0
def splitRunSet( runset_pos, runset_neg, fold, chunksize, evalsize, rndidx ):
    '''Take parts of runset_pos and runset_neg and re-combine into
    a training set and an evaluation set.  For use by crossValidate().
    '''
    num_items = ( len(runset_pos), len(runset_neg) )
    evalidx_pos  = range( fold*chunksize[0], fold*chunksize[0]+evalsize[0] )
    evalidx_neg  = range( fold*chunksize[1], fold*chunksize[1]+evalsize[1] )
    trainidx_pos = range( 0, fold*chunksize[0] ) + range( fold*chunksize[0]+evalsize[0], num_items[0] )
    trainidx_neg = range( 0, fold*chunksize[1] ) + range( fold*chunksize[1]+evalsize[1], num_items[1] )
    # The following line selects those elements from runset_pos
    # that correspond to the randomized indices for the current
    # evaluation chunk.  Think of this, conceptually:
    # evalset_pos  = runset_pos[ rndidx[0][evalidx_pos] ]
    # Subsequent lines: equivalently, for runset_neg, and trainset pos/neg
    evalset_pos  = list( runset_pos[i] for i in list( rndidx[0][j] for j in evalidx_pos) )
    evalset_neg  = list( runset_neg[i] for i in list( rndidx[1][j] for j in evalidx_neg) )
    trainset_pos = list( runset_pos[i] for i in list( rndidx[0][j] for j in trainidx_pos) )
    trainset_neg = list( runset_neg[i] for i in list( rndidx[1][j] for j in trainidx_neg) )

    # create a RunSet with proper purposes
    trainset = cvac.RunSet()
    trainset.purposedLists = (cvac.PurposedLabelableSeq(easy.getPurpose("pos"), trainset_pos),
                              cvac.PurposedLabelableSeq(easy.getPurpose("neg"), trainset_neg))
    evalset  = cvac.RunSet()
    evalset.purposedLists = (cvac.PurposedLabelableSeq(easy.getPurpose("pos"), evalset_pos),
                             cvac.PurposedLabelableSeq(easy.getPurpose("neg"), evalset_neg))
    return trainset, evalset
Пример #2
0
    def isProperDetector(self,
                         configString,
                         detectorData=None,
                         detectorProperties=None):
        '''Try to create or use the specified detector.
        Create a test runset and call the detector with it.
        Check for proper handling of intentional problems.
        '''
        detector = easy.getDetector(configString)
        if not detector:
            print("cannot find or connect to detector")
            return False

        # make a runset including erroneous files
        rs = cvac.RunSet()
        file_normal = easy.getLabelable("testImg/italia.jpg")
        file_notexist = easy.getLabelable("testImg/doesnotexist.jpg")
        file_notconvertible = easy.getLabelable("testImg/notconvertible.xxx")
        easy.addToRunSet(rs, file_normal,
                         cvac.Purpose(cvac.PurposeType.UNPURPOSED, -1))
        easy.addToRunSet(rs, file_notexist,
                         cvac.Purpose(cvac.PurposeType.UNPURPOSED, -1))
        easy.addToRunSet(rs, file_notconvertible,
                         cvac.Purpose(cvac.PurposeType.UNPURPOSED, -1))
        results = easy.detect(detector, detectorData, rs, detectorProperties)

        # check for number of results: must be the same as runset length
        nRunset = 0
        for pur in rs.purposedLists:
            nRunset += len(pur.labeledArtifacts)
        if len(results) != nRunset:
            print("incorrect result set size")
            return False

        # check that the doesnotexist file caused hasLabel==False
        found = False
        for lbl in results:
            # for an erroneous file
            if lbl.original.sub == img_notexist.sub or lbl.original.sub == img_notconvertible.sub:
                for foundlbl in lbl.foundLabels:
                    if foundlbl.lab.hasLabel:
                        print(
                            "Incorrectly assigned label for an erroneous file."
                        )
                        return False
            else:  #for a normal file
                for foundlbl in lbl.foundLabels:
                    # confidence 0..1
                    if not 0.0 <= foundlbl.confidence and foundlbl.confidence <= 1.0:
                        print("Label confidence out of bounds ({0}).".format(
                            foundlbl.confidence))
                        return False
                    # check that either this was not assigned a label, or the
                    # assigned label is of proper syntax
                    if foundlbl.lab.hasLabel:
                        if not isinstance(foundlbl.lab.name, str):
                            print("Label name must be of string type.")
                            return False
        return True
Пример #3
0
    def test_getLabelableList(self):
        print("testing getLabelableList")
        sys.stdout.flush()
        labelList = easy.getLabelableList("easyTestData")
        '''verify that the labels are correct
           They should be , the root directory "easyTestData",
           "kitchen" and "MITmountain"
           
        '''
        for entry in labelList:
            print("label name " + entry.lab.name)
            if entry.lab.name != "easyTestData" and \
               entry.lab.name != "kitchen" and \
               entry.lab.name != "MITmountain":
                raise RuntimeError(
                    "Not a valid label name in test getLabelableList")
            if entry.lab.name == "kitchen":
                # should find easyTestData, inside, house properties
                if "easyTestData" not in entry.lab.properties:
                    raise RuntimeError("easyTestData not in properties")
                if "inside" not in entry.lab.properties:
                    raise RuntimeError("inside not in properties")
                if "house" not in entry.lab.properties:
                    raise RuntimeError("house not in properties")
        ''' verify that we can make a runset and use it with the labelableList
        '''
        runset = cvac.RunSet()
        posPurpose = easy.getPurpose('pos')
        easy.addToRunSet(runset, labelList, posPurpose)
        if not easy.isProperRunSet(runset):
            raise RuntimeError(
                "test getLabelableList failed with an invalid runset")
        labelList = easy.getLabelableList("easyTestData", recursive=False)
        ''' should only have one label "easyTestData"
        '''
        for entry in labelList:
            if entry.lab.name != "easyTestData":
                raise RuntimeError(
                    "recursive option failed in test getLabelableList")
            if len(entry.lab.properties) > 0:
                raise RuntimeError("labelable should not have any properties")

        if not easy.isProperRunSet(runset):
            raise RuntimeError(
                "test getLabelableList failed with an invalid runset with non-recursive call"
            )
Пример #4
0
'''
Easy!  mini tutorial
Build a model for object detection
matz 6/18/2013
'''
import cvac
import easy
import zipfile

#
# create a RunSet from corporate logo images
#
print("==== Training runset: ====")
#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)
Пример #5
0
    def test_cvacdatadir(self):
        print("testing cvac data dir")
        if sys.platform == 'win32':
            datadir = os.getenv("CVAC_DATADIR", None)
            datadir = datadir.replace("/", "\\")
            easy.CVAC_DataDir = datadir
            print("Testing using back slashes")
            print("Using CVAC_DATADIR as " + datadir)
            testset = []
            easy.misc.searchDir(testset,
                                datadir + '\\testImg',
                                recursive=True,
                                video=False,
                                image=True)
            runset = cvac.RunSet()
            easy.addToRunSet(runset, testset, 'pos')
            modelfile = 'detectors/bowUSKOCA.zip'
            detector = easy.getDetector("BOW_Detector")
            props = easy.getDetectorProperties(detector)
            props.verbosity = 3
            results = easy.detect(detector,
                                  modelfile,
                                  runset,
                                  detectorProperties=props)
            easy.printResults(results)
        else:
            print("Skipping back slash test on this platform")

        #run it again with all forward slashes
        datadir = os.getenv("CVAC_DATADIR", None)
        datadir = datadir.replace("\\", "/")
        print("Testing using all forward slashes")
        print("Using CVAC_DATADIR as " + datadir)
        easy.CVAC_DataDir = datadir
        testset = []
        easy.misc.searchDir(testset,
                            datadir + '/testImg',
                            recursive=True,
                            video=False,
                            image=True)
        runset = cvac.RunSet()
        easy.addToRunSet(runset, testset, 'pos')
        modelfile = 'detectors/bowUSKOCA.zip'
        detector = easy.getDetector("BOW_Detector")
        props = easy.getDetectorProperties(detector)
        props.verbosity = 3
        results = easy.detect(detector,
                              modelfile,
                              runset,
                              detectorProperties=props)
        easy.printResults(results)

        #run it for forward slashes and relative path
        origDir = datadir
        curDir = os.getcwd()
        curDir = curDir.replace("\\", "/")
        datadir = datadir.replace("\\", "/")
        print("using relative paths for " + curDir + " in  data dir " +
              datadir)
        if datadir.startswith(curDir):
            datadir = datadir[len(curDir) + 1:]
            easy.CVAC_DataDir = datadir
            print("Using CVAC_DataDir as " + datadir)
            testset = []
            easy.misc.searchDir(testset,
                                origDir + '/testImg',
                                recursive=True,
                                video=False,
                                image=True)
            runset = cvac.RunSet()
            easy.addToRunSet(runset, testset, 'pos')
            modelfile = origDir + '/detectors/bowUSKOCA.zip'
            detector = easy.getDetector("BOW_Detector")
            props = easy.getDetectorProperties(detector)
            props.verbosity = 3
            results = easy.detect(detector,
                                  modelfile,
                                  runset,
                                  detectorProperties=props)
            easy.printResults(results)
        else:
            RuntimeError("Bad datadir")
Assign purposes in the addToRunSet method calls.

Similarly, the detector will report a label, but leave it to you to
decide on the purpose of such labels. Hence, the "found" labels
need to be mapped to their purposes in the foundMap.
Note that different methods will return different labels, so the
foundMap needs to be specific to the method.

Note that the particular detectors do not match the data, so the
code is illustrative, but the results are not very interesting.
'''

import easy, cvac
import easy.evaluate as ev

runset = cvac.RunSet()
easy.addToRunSet(runset, "trainImg/ca", "neg")
easy.addToRunSet(runset, "trainImg/kr", "pos")
#easy.printRunSetInfo( runset, printArtifacts=False, printLabels=True )

contenders = []

# Bag of Words
if (easy.getDetector("BOW_Detector") == None):
    print("BOW detector service is insufficiently configured, skipping.")
else:
    c = ev.Contender("BOW")
    c.detectorString = "BOW_Detector"
    c.detectorData = "detectors/bowUSKOCA.zip"
    c.foundMap = {
        'kr': easy.getPurpose('pos'),