예제 #1
0
def createSuperPixelRandomForestClassifier(msrcDataDirectory,
                                           classifierDirectory, nbSuperPixels,
                                           superPixelCompactness, scale):

    # Get training data
    superPixelTrainData = getSuperPixelTrainingData(msrcDataDirectory,
                                                    nbSuperPixels,
                                                    superPixelCompactness,
                                                    scale)
    superPixelTrainFeatures = superPixelTrainData[0]
    superPixelTrainLabels = superPixelTrainData[1]

    # now train random forest classifier on labelled superpixel data
    print '\n\n**Training a random forest on %d examples...' % len(
        superPixelTrainLabels)
    print "Training feature data shape=", np.shape(
        superPixelTrainFeatures), "type:", type(superPixelTrainFeatures)
    print "Training labels shape=", np.shape(
        superPixelTrainLabels), "type:", type(superPixelTrainLabels)

    numEstimators = 100

    classifier = sklearn.ensemble.RandomForestClassifier(
        n_estimators=numEstimators)
    classifier = classifier.fit(superPixelTrainData, superPixelTrainLabels)

    # Now serialise the classifier to file
    classifierFilename = classifierDirectory + "/" + "randForest_superPixel_nEst" + str(
        numEstimators) + ".pkl"

    pomio.pickleObject(classifier, classifierFilename)

    print "Rand forest classifier saved @ " + str(classifierFilename)
def createSuperPixelRandomForestClassifier(msrcDataDirectory, classifierDirectory,nbSuperPixels,superPixelCompactness, scale):
    
    # Get training data
    superPixelTrainData = getSuperPixelTrainingData(msrcDataDirectory,nbSuperPixels,superPixelCompactness, scale)
    superPixelTrainFeatures = superPixelTrainData[0]
    superPixelTrainLabels = superPixelTrainData[1]
    
    # now train random forest classifier on labelled superpixel data
    print '\n\n**Training a random forest on %d examples...' % len(superPixelTrainLabels)
    print "Training feature data shape=" , np.shape(superPixelTrainFeatures) , "type:", type(superPixelTrainFeatures)
    print "Training labels shape=" , np.shape(superPixelTrainLabels), "type:" , type(superPixelTrainLabels)

    numEstimators = 100
    
    classifier = sklearn.ensemble.RandomForestClassifier(n_estimators=numEstimators)
    classifier = classifier.fit( superPixelTrainData, superPixelTrainLabels)
    
    # Now serialise the classifier to file
    classifierFilename = classifierDirectory + "/" + "randForest_superPixel_nEst" + str(numEstimators) + ".pkl"
    
    pomio.pickleObject(classifier, classifierFilename)
    
    print "Rand forest classifier saved @ " + str(classifierFilename)
def createSuperPixelLogisticRegressionClassifier(msrcDataDirectory, classifierDirectory, scale):
    
    # Get training data
    superPixelTrainData = getSuperPixelTrainingData(msrcDataDirectory,nbSuperPixels,superPixelCompactness, scale)
    superPixelTrainFeatures = superPixelTrainData[0]
    superPixelTrainLabels = superPixelTrainData[1]
    
    # now train random forest classifier on labelled superpixel data
    print '\n\n**Training a LR classifier on %d examples...' % len(superPixelTrainLabels)
    print "Training feature data shape=" , np.shape(superPixelTrainFeatures) , "type:", type(superPixelTrainFeatures)
    print "Training labels shape=" , np.shape(superPixelTrainLabels), "type:" , type(superPixelTrainLabels)

    Cvalue = 0.5
    # sklearn.linear_model.LogisticRegression(penalty='l2', dual=False, tol=0.0001, C=1.0, fit_intercept=True, intercept_scaling=1, class_weight=None, random_state=None)
    classifier = LogisticRegression(penalty='l1' , dual=False, tol=0.0001, C=Cvalue, fit_intercept=True, intercept_scaling=1)
    classifier.fit(superPixelTrainFeatures, superPixelTrainLabels)
    
    
    # Now serialise the classifier to file
    classifierFilename = classifierDirectory + "/" + "LR_superPixel_C" + str(Cvalue) + ".pkl"
    
    pomio.pickleObject(classifier, classifierFilename)
    
    print "LR classifier saved @ " + str(classifierFilename)
예제 #4
0
def createSuperPixelLogisticRegressionClassifier(msrcDataDirectory,
                                                 classifierDirectory, scale):

    # Get training data
    superPixelTrainData = getSuperPixelTrainingData(msrcDataDirectory,
                                                    nbSuperPixels,
                                                    superPixelCompactness,
                                                    scale)
    superPixelTrainFeatures = superPixelTrainData[0]
    superPixelTrainLabels = superPixelTrainData[1]

    # now train random forest classifier on labelled superpixel data
    print '\n\n**Training a LR classifier on %d examples...' % len(
        superPixelTrainLabels)
    print "Training feature data shape=", np.shape(
        superPixelTrainFeatures), "type:", type(superPixelTrainFeatures)
    print "Training labels shape=", np.shape(
        superPixelTrainLabels), "type:", type(superPixelTrainLabels)

    Cvalue = 0.5
    # sklearn.linear_model.LogisticRegression(penalty='l2', dual=False, tol=0.0001, C=1.0, fit_intercept=True, intercept_scaling=1, class_weight=None, random_state=None)
    classifier = LogisticRegression(penalty='l1',
                                    dual=False,
                                    tol=0.0001,
                                    C=Cvalue,
                                    fit_intercept=True,
                                    intercept_scaling=1)
    classifier.fit(superPixelTrainFeatures, superPixelTrainLabels)

    # Now serialise the classifier to file
    classifierFilename = classifierDirectory + "/" + "LR_superPixel_C" + str(
        Cvalue) + ".pkl"

    pomio.pickleObject(classifier, classifierFilename)

    print "LR classifier saved @ " + str(classifierFilename)
def createAndSaveFeatureLabelData(
      msrcData, outfileBase, dataType, outfileType, nbSuperPixels, superPixelCompactness
    ):

    if dataType == None or dataType == "":
        outfileFtrs = '%s_ftrs.%s' % ( outfileBase, outfileType )
        outfileLabs = '%s_labs.%s' % ( outfileBase, outfileType )
        outfileAdj  = '%s_adj.%s'  % ( outfileBase, outfileType )
    else:
        outfileFtrs = '%s_%s_ftrs.%s' % ( outfileBase, dataType, outfileType )
        outfileLabs = '%s_%s_labs.%s' % ( outfileBase, dataType, outfileType )
        outfileAdj  = '%s_%s_adj.%s'  % ( outfileBase, dataType, outfileType )

    # Check can write these files.
    f=open(outfileFtrs,'w')
    f.close()
    f=open(outfileLabs,'w')
    f.close()
    
    # Edited getSuperPixelTrainingData to allow user-defined data split ratios
    superPixelData     = SuperPixelClassifier.getSuperPixelData(msrcData,nbSuperPixels,superPixelCompactness)
    
    superPixelFeatures = superPixelData[0]
    superPixelLabels   = superPixelData[1].astype(np.int32)
    superPixelClassAdj = superPixelData[2]

    assert np.all( np.isfinite( superPixelFeatures ) )

    #print superPixelClassAdj

    # Output
    if outfileType == 'pkl':
        pomio.pickleObject( superPixelFeatures, outfileFtrs )
        pomio.pickleObject( superPixelLabels,   outfileLabs )
        pomio.pickleObject( superPixelClassAdj, outfileAdj )
    elif outfileType == 'csv':
        pomio.writeMatToCSV( superPixelFeatures, outfileFtrs )
        pomio.writeMatToCSV( superPixelLabels,   outfileLabs )
        pomio.writeMatToCSV( superPixelClassAdj, outfileAdj )
    else:
        assert False, 'unknown output file format ' + outfileType

    print 'Output written to file ', outfileFtrs, ' and ', outfileLabs, ' and ', outfileAdj
예제 #6
0
def createAndSaveFeatureLabelData(msrcData, outfileBase, dataType, outfileType,
                                  nbSuperPixels, superPixelCompactness):

    if dataType == None or dataType == "":
        outfileFtrs = '%s_ftrs.%s' % (outfileBase, outfileType)
        outfileLabs = '%s_labs.%s' % (outfileBase, outfileType)
        outfileAdj = '%s_adj.%s' % (outfileBase, outfileType)
    else:
        outfileFtrs = '%s_%s_ftrs.%s' % (outfileBase, dataType, outfileType)
        outfileLabs = '%s_%s_labs.%s' % (outfileBase, dataType, outfileType)
        outfileAdj = '%s_%s_adj.%s' % (outfileBase, dataType, outfileType)

    # Check can write these files.
    f = open(outfileFtrs, 'w')
    f.close()
    f = open(outfileLabs, 'w')
    f.close()

    # Edited getSuperPixelTrainingData to allow user-defined data split ratios
    superPixelData = SuperPixelClassifier.getSuperPixelData(
        msrcData, nbSuperPixels, superPixelCompactness)

    superPixelFeatures = superPixelData[0]
    superPixelLabels = superPixelData[1].astype(np.int32)
    superPixelClassAdj = superPixelData[2]

    assert np.all(np.isfinite(superPixelFeatures))

    #print superPixelClassAdj

    # Output
    if outfileType == 'pkl':
        pomio.pickleObject(superPixelFeatures, outfileFtrs)
        pomio.pickleObject(superPixelLabels, outfileLabs)
        pomio.pickleObject(superPixelClassAdj, outfileAdj)
    elif outfileType == 'csv':
        pomio.writeMatToCSV(superPixelFeatures, outfileFtrs)
        pomio.writeMatToCSV(superPixelLabels, outfileLabs)
        pomio.writeMatToCSV(superPixelClassAdj, outfileAdj)
    else:
        assert False, 'unknown output file format ' + outfileType

    print 'Output written to file ', outfileFtrs, ' and ', outfileLabs, ' and ', outfileAdj
reportAccuracy('Training set', labs, predlabs)

# optionally test classifier on hold-out test set
if infileFtrsTest != None and infileLabsTest != None:
    # Load the features and labels
    if infileFtrsTest.endswith('.pkl'):
        ftrsTest = pomio.unpickleObject(infileFtrsTest)
    else:
        ftrsTest = pomio.readMatFromCSV(infileFtrsTest)

    if infileLabsTest.endswith('.pkl'):
        labsTest = pomio.unpickleObject(infileLabsTest)
    else:
        labsTest = pomio.readMatFromCSV(infileLabsTest).astype(np.int32)

    ntest = len(labsTest)
    assert ntest == ftrsTest.shape[0], 'Error: for TEST set, there are %d labels and %d features' \
        % ( ntest, ftrsTest.shape[0] )

    assert np.all(np.isfinite(ftrsTest))

    predlabs = getlabs(clfr, ftrsTest)
    reportAccuracy('Test set', labsTest, predlabs)

# Write the classifier
if clfr != None and outfile != None:
    pomio.pickleObject(clfr, outfile)
    print 'Output written to file ', outfile
else:
    print "No classifier to persist or no output filename; review input parameters."
예제 #8
0
def createAndSaveFeatureLabelData(
    msrcData, outfileBase, dataType, outfileType, nbSuperPixels, superPixelCompactness, ftype, aggtype, verbose
):

    if verbose:
        print "Creating feature set ", outfileBase

    if dataType == None or dataType == "":
        outfileFtrs = "%s_ftrs.%s" % (outfileBase, outfileType)
        outfileLabs = "%s_labs.%s" % (outfileBase, outfileType)
    else:
        outfileFtrs = "%s_%s_ftrs.%s" % (outfileBase, dataType, outfileType)
        outfileLabs = "%s_%s_labs.%s" % (outfileBase, dataType, outfileType)

    # Check can write these files.
    f = open(outfileFtrs, "w")
    f.close()
    f = open(outfileLabs, "w")
    f.close()

    if verbose:
        print "  - computing superpixels"
    allSuperPixels = superPixels.computeSuperPixelGraphMulti(
        [z.m_img for z in msrcData], "slic", [nbSuperPixels, superPixelCompactness], nbCores=args.nbCores
    )
    if verbose:
        print "  - computing features"
    superPixelFeatures = features.computeSuperPixelFeaturesMulti(
        [z.m_img for z in msrcData], allSuperPixels, ftype, aggtype, asMatrix=True, nbCores=args.nbCores
    )
    if verbose:
        print "  - extracting labels"
    superPixelLabels = classification.computeSuperPixelLabelsMulti([z.m_gt for z in msrcData], allSuperPixels)

    assert np.all(np.isfinite(superPixelFeatures))

    # Don't save features with void labels.
    good = superPixelLabels != pomio.getVoidIdx()
    if not np.all(good):
        if verbose:
            print "   - discarding %d superpixels with void labels" % np.count_nonzero(np.logical_not(good))
        superPixelLabels = superPixelLabels[good]
        superPixelFeatures = superPixelFeatures[good, :]

    if verbose:
        print "  - writing %d feature vectors of dimension %d to output files" % (
            superPixelFeatures.shape[0],
            superPixelFeatures.shape[1],
        )

    # Output
    if outfileType == "pkl":
        pomio.pickleObject(superPixelFeatures, outfileFtrs)
        pomio.pickleObject(superPixelLabels, outfileLabs)
    elif outfileType == "csv":
        pomio.writeMatToCSV(superPixelFeatures, outfileFtrs)
        pomio.writeMatToCSV(superPixelLabels, outfileLabs)
    else:
        assert False, "unknown output file format " + outfileType

    print "Output written to file ", outfileFtrs, " and ", outfileLabs
예제 #9
0
def createAndSaveFeatureLabelData(msrcData, outfileBase, dataType, outfileType,
                                  nbSuperPixels, superPixelCompactness, ftype,
                                  aggtype, verbose):

    if verbose:
        print 'Creating feature set ', outfileBase

    if dataType == None or dataType == "":
        outfileFtrs = '%s_ftrs.%s' % (outfileBase, outfileType)
        outfileLabs = '%s_labs.%s' % (outfileBase, outfileType)
    else:
        outfileFtrs = '%s_%s_ftrs.%s' % (outfileBase, dataType, outfileType)
        outfileLabs = '%s_%s_labs.%s' % (outfileBase, dataType, outfileType)

    # Check can write these files.
    f = open(outfileFtrs, 'w')
    f.close()
    f = open(outfileLabs, 'w')
    f.close()

    if verbose:
        print '  - computing superpixels'
    allSuperPixels = superPixels.computeSuperPixelGraphMulti( \
      [ z.m_img for z in msrcData ],
      'slic',
      [nbSuperPixels, superPixelCompactness], nbCores=args.nbCores )
    if verbose:
        print '  - computing features'
    superPixelFeatures = features.computeSuperPixelFeaturesMulti(
        [z.m_img for z in msrcData],
        allSuperPixels,
        ftype,
        aggtype,
        asMatrix=True,
        nbCores=args.nbCores)
    if verbose:
        print '  - extracting labels'
    superPixelLabels = classification.computeSuperPixelLabelsMulti( \
      [z.m_gt for z in msrcData], allSuperPixels )

    assert np.all(np.isfinite(superPixelFeatures))

    # Don't save features with void labels.
    good = (superPixelLabels != pomio.getVoidIdx())
    if not np.all(good):
        if verbose:
            print '   - discarding %d superpixels with void labels' % np.count_nonzero(
                np.logical_not(good))
        superPixelLabels = superPixelLabels[good]
        superPixelFeatures = superPixelFeatures[good, :]

    if verbose:
        print '  - writing %d feature vectors of dimension %d to output files' % \
            (superPixelFeatures.shape[0], superPixelFeatures.shape[1])

    # Output
    if outfileType == 'pkl':
        pomio.pickleObject(superPixelFeatures, outfileFtrs)
        pomio.pickleObject(superPixelLabels, outfileLabs)
    elif outfileType == 'csv':
        pomio.writeMatToCSV(superPixelFeatures, outfileFtrs)
        pomio.writeMatToCSV(superPixelLabels, outfileLabs)
    else:
        assert False, 'unknown output file format ' + outfileType

    print 'Output written to file ', outfileFtrs, ' and ', outfileLabs
# optionally test classifier on hold-out test set
if infileFtrsTest != None and infileLabsTest != None:
    # Load the features and labels
    if infileFtrsTest.endswith('.pkl'):
        ftrsTest = pomio.unpickleObject( infileFtrsTest )
    else:
        ftrsTest = pomio.readMatFromCSV( infileFtrsTest )
    
    if infileLabsTest.endswith('.pkl'):
        labsTest = pomio.unpickleObject( infileLabsTest )
    else:
        labsTest = pomio.readMatFromCSV( infileLabsTest ).astype(np.int32)
    
    ntest = len(labsTest)
    assert ntest == ftrsTest.shape[0], 'Error: for TEST set, there are %d labels and %d features' \
        % ( ntest, ftrsTest.shape[0] )
    
    assert np.all( np.isfinite( ftrsTest ) )

    predlabs = getlabs(clfr,ftrsTest)
    reportAccuracy( 'Test set', labsTest, predlabs )

# Write the classifier
if clfr != None and outfile != None:
    pomio.pickleObject( clfr, outfile )
    print 'Output written to file ', outfile
else:
    print "No classifier to persist or no output filename; review input parameters."

예제 #11
0
    image, 'slic', [numberSuperPixels, superPixelCompactness])
[spClassPreds, spClassProbs
 ] = classification.classifyImageSuperPixels(image, clfr, spGraph, ftype,
                                             aggtype, makeProbs)
spClassPredsImage = spGraph.imageFromSuperPixelData(
    spClassPreds.reshape((len(spClassPreds), 1)))

if args.verbose:
    plt.subplot(1, 2, 1)
    plt.imshow(image)
    plt.title(args.infile)
    plt.subplot(1, 2, 2)
    pomio.showLabels(spClassPredsImage)
    plt.waitforbuttonpress()

if args.outfile and len(args.outfile) > 0:
    print 'Writing output label file %s' % args.outfile
    outimg = pomio.msrc_convertLabelsToRGB(spClassPredsImage)
    skimage.io.imsave(args.outfile, outimg)
    print '   done.'

if makeProbs:
    print 'Writing output (superpixels, class probabilities) to pickle file %s' % \
        args.outprobsfile
    assert spClassProbs != None
    pomio.pickleObject((spGraph, spClassProbs), args.outprobsfile)

if args.verbose:
    plt.interactive(0)
    plt.show()
print 'Classifying file ', args.infile
image = amntools.readImage(args.infile)
spGraph = superPixels.computeSuperPixelGraph(image,'slic',[numberSuperPixels, superPixelCompactness])
[spClassPreds, spClassProbs] = classification.classifyImageSuperPixels( image, clfr, spGraph, ftype, aggtype, makeProbs)
spClassPredsImage = spGraph.imageFromSuperPixelData( spClassPreds.reshape( (len(spClassPreds),1) ) )

if args.verbose:
    plt.subplot(1,2,1)
    plt.imshow(image)
    plt.title(args.infile)
    plt.subplot(1,2,2)
    pomio.showLabels(spClassPredsImage)
    plt.waitforbuttonpress()

if args.outfile and len(args.outfile)>0:
    print 'Writing output label file %s' % args.outfile
    outimg = pomio.msrc_convertLabelsToRGB( spClassPredsImage )
    skimage.io.imsave(args.outfile, outimg)
    print '   done.'

if makeProbs:
    print 'Writing output (superpixels, class probabilities) to pickle file %s' % \
        args.outprobsfile
    assert spClassProbs != None
    pomio.pickleObject( (spGraph,spClassProbs), args.outprobsfile )

if args.verbose:
    plt.interactive(0)
    plt.show()