예제 #1
0
def getSuperPixelTrainingData(msrcDataDirectory,
                              nbSuperPixels,
                              superPixelCompactness,
                              scale,
                              trainSplit=0.6,
                              validationSplit=0.2,
                              testSplit=0.2):

    # Should probably make this a call to pomio in case the ordering changes in the future...
    voidClassLabel = pomio.getVoidIdx()

    # These could be user-specified
    if scale == None:
        scale = 0.05  # default to 10% of data

    msrcData = pomio.msrc_loadImages(msrcDataDirectory, None)

    print "Now generating superpixel classifier for MSRC data"

    #    splitData = pomio.splitInputDataset_msrcData(msrcData, datasetScale=scale, keepClassDistForTraining=True, trainSplit, validationSplit, testSplit )
    splitData = pomio.splitInputDataset_msrcData(msrcData, scale, True,
                                                 trainSplit, validationSplit,
                                                 testSplit)

    # prepare superpixel training data
    trainingMsrcImages = splitData[0]

    # Just use the above function to get superpixel features and labels for training data
    return SuperPixelClassifier.getSuperPixelData(trainingMsrcImages,
                                                  nbSuperPixels,
                                                  superPixelCompactness)
def evaluatePrediction(predictLabels, gtLabels, imageName):
    
    assert np.shape(predictLabels) == np.shape(gtLabels) , "Predict image and ground truth image are not the same size..."

    rows = np.shape(predictLabels)[1]
    cols = np.shape(gtLabels)[0]
    
    print "Evaluating image of size = [" , rows, " ," , cols, " ]"
    voidLabel = pomio.getVoidIdx()
    
    allPixels = 0
    voidGtPixels = 0
    correctPixels = 0
    incorrectPixels = 0

    # for each pixel, do a comparision of index    
    for r in range(0,rows):
        
        for c in range(cols):
        
            allPixels = allPixels + 1
            
            gtLabel = gtLabels[c][r]
            predictLabel = predictLabels[c][r]
                
            if gtLabel == voidLabel:
                voidGtPixels = voidGtPixels + 1
            else:
                # only compare if GT isnt void
                if (predictLabel != voidLabel) and (predictLabels[c][r] == gtLabels[c][r]):
                    correctPixels = correctPixels + 1
                else:
                    incorrectPixels = incorrectPixels + 1

    assert allPixels == (rows * cols) , "Total iterated pixels != (rows * cols) num pixels!"
    
    assert allPixels == (voidGtPixels + correctPixels + incorrectPixels) , "Some mismatch on pixel counts:: all" + str(allPixels) + " void=" + str(voidGtPixels) + " correct=" + str(correctPixels) + " incorrect=" + str(incorrectPixels)
    
    validGtPixels = allPixels - voidGtPixels
    
    percentage = float(correctPixels) / float(validGtPixels) * 100.0
    
    if percentage == 0 or percentage == 0.0:
        print "WARNING:: " + str(imageName) + " accuracy is 0%"
        
        data = "ImageName = " + str(imageName) + "\n\tTotal pixels =" + str(allPixels) + "\n\tVOID pixels  = " + str(voidGtPixels) + "\n\tCorrect pixels = " + str(correctPixels) + "\n\tIncorrect pixels=" + str(incorrectPixels) + "\n"
        
        #logFile.write(data)
        #zeroListFile.write(imageName + "\n")
        
    print "Pecentage accuracy = " + str( float(correctPixels) / float(validGtPixels) * 100.0 ) + str("%")
    return [int(correctPixels), int(validGtPixels), int(voidGtPixels), int(allPixels)]
    def countClassAdjacencies( self, nbClasses, allSPClassLabels ):
        counts = np.zeros( ( nbClasses, nbClasses ) )
        voidLabel = pomio.getVoidIdx()
        adjVoidCount = 0
        adjCount = len(self.m_edges)

        for (ei, ej) in self.m_edges:
            ci = allSPClassLabels[ ei ]
            cj = allSPClassLabels[ ej ]
            # Not doing stats for void
            if ci != voidLabel and cj != voidLabel:
                counts[ ci, cj ] += 1
                counts[ cj, ci ] += 1
            else:
                adjVoidCount += 1
        return (counts, adjVoidCount, adjCount)
    def countClassAdjacencies(self, nbClasses, allSPClassLabels):
        counts = np.zeros((nbClasses, nbClasses))
        voidLabel = pomio.getVoidIdx()
        adjVoidCount = 0
        adjCount = len(self.m_edges)

        for (ei, ej) in self.m_edges:
            ci = allSPClassLabels[ei]
            cj = allSPClassLabels[ej]
            # Not doing stats for void
            if ci != voidLabel and cj != voidLabel:
                counts[ci - 1, cj - 1] += 1
                counts[cj - 1, ci - 1] += 1
            else:
                adjVoidCount += 1
        return (counts, adjVoidCount, adjCount)
def getSuperPixelLabelledImage(image, superPixelMask, superPixelLabels):
    superPixels = np.unique(superPixelMask)
    numSuperPixels = np.shape(superPixels)[0]
    numSuperPixelLabels = np.shape(superPixelLabels)[0]
    assert numSuperPixels == numSuperPixelLabels , "The number of superpixels in mask != number superpixel labels:: " + str(numSuperPixels) + " vs. " + str(numSuperPixelLabels) 
    
    labelImage = np.zeros( np.shape(image[:,:,0]) )
    
    for idx in range(0, numSuperPixels):
        # Assume a consistent ordering between unique superpixel values and superpixel labels
        labelImage = labelImage + ( superPixelLabels[idx] * (superPixelMask==superPixels[idx]).astype(int) )
    
    assert np.unique(labelImage).all() == np.unique(superPixelLabels).all() , "List of unique class labels in image != image labels:: " + str(np.unique(labelImage)) + " vs. " + str(superPixelLabels)
    
    assert pomio.getVoidIdx() not in np.unique(superPixelLabels) , "The set of predicted labels for the image contains the void label.  It shouldn't."
    
    return labelImage
예제 #6
0
def evaluatePrediction(predictLabels, gtLabels, gtimageName, dbg=False):

    assert np.shape(predictLabels) == np.shape(
        gtLabels
    ), "Predict image and ground truth image are not the same size..."

    rows = np.shape(predictLabels)[1]
    cols = np.shape(gtLabels)[0]

    if dbg:
        print "Evaluating image of size = [", rows, " ,", cols, " ]"
    voidLabel = pomio.getVoidIdx()

    allPixels = 0
    voidGtPixels = 0
    correctPixels = 0
    incorrectPixels = 0

    # for each pixel, do a comparision of index
    allPixels = rows * cols
    voidGtPixels = np.count_nonzero(gtLabels == voidLabel)
    correctPixels = np.count_nonzero(
        np.logical_and(gtLabels != voidLabel, predictLabels == gtLabels))
    validGtPixels = allPixels - voidGtPixels
    incorrectPixels = validGtPixels - correctPixels

    assert allPixels == (
        rows * cols), "Total iterated pixels != (rows * cols) num pixels!"
    assert allPixels == (
        voidGtPixels + correctPixels +
        incorrectPixels), "Some mismatch on pixel counts:: all" + str(
            allPixels) + " void=" + str(voidGtPixels) + " correct=" + str(
                correctPixels) + " incorrect=" + str(incorrectPixels)

    percentage = float(correctPixels) / float(validGtPixels) * 100.0

    if percentage == 0 or percentage == 0.0:
        print "WARNING:: " + str(gtimageName) + " accuracy is 0%"

    if dbg:
        print "Pecentage accuracy = " + str(
            float(correctPixels) / float(validGtPixels) * 100.0) + str("%")
    return np.array([correctPixels, validGtPixels, voidGtPixels, allPixels],
                    dtype=int)
예제 #7
0
def evaluateConfusionMatrix(predictedImg, gtImg):

    assert np.shape(predictedImg) == np.shape(
        gtImg), "Predict image and ground truth image are not the same size..."

    numClasses = pomio.getNumClasses()

    confusionMatrix = np.zeros([numClasses, numClasses], int)
    # rows are actual, cols are predicted
    for cl in range(numClasses):
        clMask = (gtImg == cl)
        # It's easy, just histogram those values
        vals = predictedImg[clMask]
        assert np.all(np.logical_and(0 <= vals, vals < numClasses)), vals.max()
        confusionMatrix[cl, :] = np.histogram(vals, range(numClasses + 1))[0]

    assert confusionMatrix.sum() == np.count_nonzero(
        gtImg != pomio.getVoidIdx())

    return confusionMatrix
def getSuperPixelTrainingData(msrcDataDirectory, nbSuperPixels,superPixelCompactness, scale, trainSplit=0.6, validationSplit=0.2, testSplit=0.2):
    
    # Should probably make this a call to pomio in case the ordering changes in the future...
    voidClassLabel = pomio.getVoidIdx()
    
    # These could be user-specified
    if scale == None:
        scale = 0.05 # default to 10% of data
        
    msrcData = pomio.msrc_loadImages(msrcDataDirectory, None)

    print "Now generating superpixel classifier for MSRC data"
    
#    splitData = pomio.splitInputDataset_msrcData(msrcData, datasetScale=scale, keepClassDistForTraining=True, trainSplit, validationSplit, testSplit )
    splitData = pomio.splitInputDataset_msrcData(msrcData, scale, True, trainSplit, validationSplit, testSplit )
    
    # prepare superpixel training data
    trainingMsrcImages = splitData[0]
    
    # Just use the above function to get superpixel features and labels for training data
    return SuperPixelClassifier.getSuperPixelData(trainingMsrcImages,nbSuperPixels,superPixelCompactness)
def computeSuperPixelLabels( gtImage, superPixelObj ):
  n = superPixelObj.getNumSuperPixels()
  res = pomio.getVoidIdx() * np.ones( (n,), dtype=int )
  c = 0
  for sp in range( n ):
    try:
      res[sp] = assignClassLabelToSuperPixel( superPixelObj.getLabelImage()==sp, gtImage )
    except:
      # Keep a count of the undecided pixels.  Default label is void
      c += 1

  if c > 0:
    print 'WARNING: %d of %d superpixels have conflicting GT class labels. Setting to void.' % (c,len(res))
    if showDodgySPs:
      # Display for debug purposes.  set to false to turn it off.
      superPixels.displayImage( superPixels.generateImageWithSuperPixelBoundaries( pomio.msrc_convertLabelsToRGB(gtImage),\
                                                                                     superPixelObj.m_labels ), \
                                  imgTitle="Bad image graph", orientation="lower" )
      pdb.set_trace()
      plt.waitforbuttonpress()
                                                                                     
  return res
예제 #10
0
def assignClassLabelToSuperPixel(superPixelValueMask, imagePixelLabels):
    """This function provides basic logic for setting the overall class label for a superpixel"""
    voidIdx = pomio.getVoidIdx()

    # just adopt the most frequently occurring class label as the superpixel label
    superPixelConstituentLabels = imagePixelLabels[superPixelValueMask]

    labelCount = stats.itemfreq(superPixelConstituentLabels)

    maxLabelFreq = np.max(labelCount[:, 1])
    maxLabelIdx = (labelCount[:, 1] == maxLabelFreq)

    maxLabel = labelCount[maxLabelIdx]

    numSuperPixels = np.sum(labelCount[:, 1])

    percentOfPixelsRequired = 0.5
    requiredPixelThreshold = np.round(0.5 * numSuperPixels, 0).astype(int)

    maxLabelValue = voidIdx

    # what if the max count gives more than 1 match?  Naughty superpixel.
    # It would be nice to select the class that is least represented in the dataset so far...
    if np.shape(maxLabel)[0] > 1:

        for idx in range(0, np.shape(maxLabel)[0]):
            print "\t\tINFO:: max label:", maxLabel[idx, :]

            if maxLabel[idx, 0] != voidIdx and maxLabel[
                    idx, 1] >= requiredPixelThreshold:
                maxLabelValue = maxLabel[idx, 0]
                break
            # If no match, maxLabelValues is equal to void, and will be skipped in training

    else:
        maxLabelValue = maxLabel[0, 0]

    return maxLabelValue
def assignClassLabelToSuperPixel(superPixelValueMask, imagePixelLabels):
    """This function provides basic logic for setting the overall class label for a superpixel"""
    voidIdx = pomio.getVoidIdx()
    
    # just adopt the most frequently occurring class label as the superpixel label
    superPixelConstituentLabels = imagePixelLabels[superPixelValueMask]
    
    labelCount = stats.itemfreq(superPixelConstituentLabels)
    
    maxLabelFreq = np.max(labelCount[:, 1])
    maxLabelIdx = (labelCount[:,1] == maxLabelFreq)
    
    maxLabel = labelCount[maxLabelIdx]
    
    numSuperPixels = np.sum(labelCount[:,1])
    
    percentOfPixelsRequired = 0.5
    requiredPixelThreshold = np.round(0.5*numSuperPixels , 0).astype(int)
    
    maxLabelValue = voidIdx
    
    # what if the max count gives more than 1 match?  Naughty superpixel.
    # It would be nice to select the class that is least represented in the dataset so far...
    if np.shape(maxLabel)[0] > 1:
    
        for idx in range(0, np.shape(maxLabel)[0]):
            print "\t\tINFO:: max label:" , maxLabel[idx,:]
            
            if maxLabel[idx,0] != voidIdx and maxLabel[idx,1] >= requiredPixelThreshold:
                maxLabelValue = maxLabel[idx,0]
                break
            # If no match, maxLabelValues is equal to void, and will be skipped in training
                
    else:
        maxLabelValue = maxLabel[0,0]
    
    return maxLabelValue
예제 #12
0
def getSuperPixelLabelledImage(image, superPixelMask, superPixelLabels):
    superPixels = np.unique(superPixelMask)
    numSuperPixels = np.shape(superPixels)[0]
    numSuperPixelLabels = np.shape(superPixelLabels)[0]
    assert numSuperPixels == numSuperPixelLabels, "The number of superpixels in mask != number superpixel labels:: " + str(
        numSuperPixels) + " vs. " + str(numSuperPixelLabels)

    labelImage = np.zeros(np.shape(image[:, :, 0]))

    for idx in range(0, numSuperPixels):
        # Assume a consistent ordering between unique superpixel values and superpixel labels
        labelImage = labelImage + (
            superPixelLabels[idx] *
            (superPixelMask == superPixels[idx]).astype(int))

    assert np.unique(labelImage).all() == np.unique(superPixelLabels).all(
    ), "List of unique class labels in image != image labels:: " + str(
        np.unique(labelImage)) + " vs. " + str(superPixelLabels)

    assert pomio.getVoidIdx() not in np.unique(
        superPixelLabels
    ), "The set of predicted labels for the image contains the void label.  It shouldn't."

    return labelImage
def evaluatePrediction(predictLabels, gtLabels, imageName):

    assert np.shape(predictLabels) == np.shape(
        gtLabels
    ), "Predict image and ground truth image are not the same size..."

    rows = np.shape(predictLabels)[1]
    cols = np.shape(gtLabels)[0]

    print "Evaluating image of size = [", rows, " ,", cols, " ]"
    voidLabel = pomio.getVoidIdx()

    allPixels = 0
    voidGtPixels = 0
    correctPixels = 0
    incorrectPixels = 0

    # for each pixel, do a comparision of index
    for r in range(0, rows):

        for c in range(cols):

            allPixels = allPixels + 1

            gtLabel = gtLabels[c][r]
            predictLabel = predictLabels[c][r]

            if gtLabel == voidLabel:
                voidGtPixels = voidGtPixels + 1
            else:
                # only compare if GT isnt void
                if (predictLabel != voidLabel) and (predictLabels[c][r]
                                                    == gtLabels[c][r]):
                    correctPixels = correctPixels + 1
                else:
                    incorrectPixels = incorrectPixels + 1

    assert allPixels == (
        rows * cols), "Total iterated pixels != (rows * cols) num pixels!"

    assert allPixels == (
        voidGtPixels + correctPixels +
        incorrectPixels), "Some mismatch on pixel counts:: all" + str(
            allPixels) + " void=" + str(voidGtPixels) + " correct=" + str(
                correctPixels) + " incorrect=" + str(incorrectPixels)

    validGtPixels = allPixels - voidGtPixels

    percentage = float(correctPixels) / float(validGtPixels) * 100.0

    if percentage == 0 or percentage == 0.0:
        print "WARNING:: " + str(imageName) + " accuracy is 0%"

        data = "ImageName = " + str(imageName) + "\n\tTotal pixels =" + str(
            allPixels) + "\n\tVOID pixels  = " + str(
                voidGtPixels) + "\n\tCorrect pixels = " + str(
                    correctPixels) + "\n\tIncorrect pixels=" + str(
                        incorrectPixels) + "\n"

        #logFile.write(data)
        #zeroListFile.write(imageName + "\n")

    print "Pecentage accuracy = " + str(
        float(correctPixels) / float(validGtPixels) * 100.0) + str("%")
    return [
        int(correctPixels),
        int(validGtPixels),
        int(voidGtPixels),
        int(allPixels)
    ]
def getSuperPixelData(msrcImages,numberSuperPixels, superPixelCompactness):
    
    # Should probably make this a call to pomio in case the ordering changes in the future...
    voidClassLabel = pomio.getVoidIdx()
    
    numberImages = len(msrcImages)    
    
    # for each image:
    #   determine superpixel label (discard if void)
    #   compute superpixel features of valid superpixels
    #   append features to cumulative array of all super pixel features
    #   append label to array of all labels
    
    superPixelFeatures = None
    superPixelLabels = np.array([], int) # used for superpixel labels
    numberVoidSuperPixels = 0   # keep track of void superpixels

    nbClasses = pomio.getNumClasses()
    classAdjCounts = np.zeros( (nbClasses, nbClasses) )
    adjCountsTotal = 0
    adjVoidCountsTotal = 0

    for imgIdx in range(0, numberImages):
    
        superPixelIgnoreList = np.array([], int) # this is used to skip over the superpixel in feature processing
    
        print "\n**Processing Image#" , (imgIdx + 1) , " of" , numberImages
    
        # get raw image and ground truth labels
        img = msrcImages[imgIdx].m_img
        imgPixelLabels = msrcImages[imgIdx].m_gt
        
        # create superpixel map and graph for image
        spgraph = SuperPixels.computeSuperPixelGraph( img, 'slic', [numberSuperPixels, superPixelCompactness] )
        imgSuperPixelMask = spgraph.m_labels
        imgSuperPixels = spgraph.m_nodes
        numberImgSuperPixels = spgraph.getNumSuperPixels()
    
        # create superpixel exclude list & superpixel label array
        allSPClassLabels = []
        for spIdx in range(0, numberImgSuperPixels):
            
            superPixelValue = imgSuperPixels[spIdx]
            #print "\tINFO: Processing superpixel =", superPixelValue , " of" , numberImgSuperPixels, " in image"
            
            
            # Assume superpixel labels are sequence of integers
            superPixelValueMask = (imgSuperPixelMask == superPixelValue ) # Boolean array for indexing superpixel-pixels
            superPixelLabel = assignClassLabelToSuperPixel(superPixelValueMask, imgPixelLabels)
            allSPClassLabels.append( superPixelLabel)

            if(superPixelLabel == voidClassLabel):
            
                # add to ignore list, increment void count & do not add to superpixel label array
                superPixelIgnoreList = np.append(superPixelIgnoreList, superPixelValue)
                numberVoidSuperPixels = numberVoidSuperPixels + 1
                
            else:
                superPixelLabels = np.append(superPixelLabels, superPixelLabel)
        
        assert len(allSPClassLabels) == numberImgSuperPixels
        (theseClassAdjCounts,adjVoidCount,adjCount) = spgraph.countClassAdjacencies( nbClasses, allSPClassLabels )
        classAdjCounts     += theseClassAdjCounts
        adjCountsTotal     += adjCount
        adjVoidCountsTotal += adjVoidCount

        # Now we have the superpixel labels, and an ignore list of void superpixels - time to get the features!
        imgSuperPixelFeatures = FeatureGenerator.generateSuperPixelFeatures(img, imgSuperPixelMask, excludeSuperPixelList=superPixelIgnoreList)
        
        if superPixelFeatures == None:        
            superPixelFeatures = imgSuperPixelFeatures;
        else:
            # stack the superpixel features into a single list
            superPixelFeatures = np.vstack( [ superPixelFeatures, imgSuperPixelFeatures ] )
    
    
    assert np.shape(superPixelFeatures)[0] == np.shape(superPixelFeatures)[0] , "Number of samples != number labels"
    print "\n**Processed total of" , numberImages, "images"
    print "  %d out of %d adjacencies were ignored due to void (%.2f %%)" % \
        (adjVoidCountsTotal, adjCountsTotal, \
             100.0*adjVoidCountsTotal/adjCountsTotal)

    # Now return the results
    return [ superPixelFeatures, superPixelLabels, classAdjCounts ]
예제 #15
0
def getSuperPixelData(msrcImages, numberSuperPixels, superPixelCompactness):

    # Should probably make this a call to pomio in case the ordering changes in the future...
    voidClassLabel = pomio.getVoidIdx()

    numberImages = len(msrcImages)

    # for each image:
    #   determine superpixel label (discard if void)
    #   compute superpixel features of valid superpixels
    #   append features to cumulative array of all super pixel features
    #   append label to array of all labels

    superPixelFeatures = None
    superPixelLabels = np.array([], int)  # used for superpixel labels
    numberVoidSuperPixels = 0  # keep track of void superpixels

    nbClasses = pomio.getNumClasses()
    classAdjCounts = np.zeros((nbClasses, nbClasses))
    adjCountsTotal = 0
    adjVoidCountsTotal = 0

    for imgIdx in range(0, numberImages):

        superPixelIgnoreList = np.array(
            [], int
        )  # this is used to skip over the superpixel in feature processing

        print "\n**Processing Image#", (imgIdx + 1), " of", numberImages

        # get raw image and ground truth labels
        img = msrcImages[imgIdx].m_img
        imgPixelLabels = msrcImages[imgIdx].m_gt

        # create superpixel map and graph for image
        spgraph = SuperPixels.computeSuperPixelGraph(
            img, 'slic', [numberSuperPixels, superPixelCompactness])
        imgSuperPixelMask = spgraph.m_labels
        imgSuperPixels = spgraph.m_nodes
        numberImgSuperPixels = spgraph.getNumSuperPixels()

        # create superpixel exclude list & superpixel label array
        allSPClassLabels = []
        for spIdx in range(0, numberImgSuperPixels):

            superPixelValue = imgSuperPixels[spIdx]
            #print "\tINFO: Processing superpixel =", superPixelValue , " of" , numberImgSuperPixels, " in image"

            # Assume superpixel labels are sequence of integers
            superPixelValueMask = (
                imgSuperPixelMask == superPixelValue
            )  # Boolean array for indexing superpixel-pixels
            superPixelLabel = assignClassLabelToSuperPixel(
                superPixelValueMask, imgPixelLabels)
            allSPClassLabels.append(superPixelLabel)

            if (superPixelLabel == voidClassLabel):

                # add to ignore list, increment void count & do not add to superpixel label array
                superPixelIgnoreList = np.append(superPixelIgnoreList,
                                                 superPixelValue)
                numberVoidSuperPixels = numberVoidSuperPixels + 1

            else:
                superPixelLabels = np.append(superPixelLabels, superPixelLabel)

        assert len(allSPClassLabels) == numberImgSuperPixels
        (theseClassAdjCounts, adjVoidCount,
         adjCount) = spgraph.countClassAdjacencies(nbClasses, allSPClassLabels)
        classAdjCounts += theseClassAdjCounts
        adjCountsTotal += adjCount
        adjVoidCountsTotal += adjVoidCount

        # Now we have the superpixel labels, and an ignore list of void superpixels - time to get the features!
        imgSuperPixelFeatures = FeatureGenerator.generateSuperPixelFeatures(
            img, imgSuperPixelMask, excludeSuperPixelList=superPixelIgnoreList)

        if superPixelFeatures == None:
            superPixelFeatures = imgSuperPixelFeatures
        else:
            # stack the superpixel features into a single list
            superPixelFeatures = np.vstack(
                [superPixelFeatures, imgSuperPixelFeatures])

    assert np.shape(superPixelFeatures)[0] == np.shape(
        superPixelFeatures)[0], "Number of samples != number labels"
    print "\n**Processed total of", numberImages, "images"
    print "  %d out of %d adjacencies were ignored due to void (%.2f %%)" % \
        (adjVoidCountsTotal, adjCountsTotal, \
             100.0*adjVoidCountsTotal/adjCountsTotal)

    # Now return the results
    return [superPixelFeatures, superPixelLabels, classAdjCounts]
예제 #16
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
예제 #17
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