def testClassifier(classifierFilename, case): assert case=="lena" or case=="car" , "case parameter should be lena or car" superPixelClassifier = pomio.unpickleObject(classifierFilename) print "\n*Loaded classifier [ " , type(superPixelClassifier) , "] from:" , classifierFilename image = None orientation = None if case == "car": print "*Loading MSRC car image::" image = pomio.msrc_loadImages("/home/amb/dev/mrf/data/MSRC_ObjCategImageDatabase_v2", ['Images/7_3_s.bmp'] )[0].m_img orientation = "lower" elif case == "lena": print "*Loading Lena.jpg" image = skimage.data.lena() orientation = "upper" print "*Predicting superpixel labels in image::" numberSuperPixels = 400 superPixelCompactness = 10 [superPixelLabels, superPixelsMask] = predictSuperPixelLabels(superPixelClassifier, image,numberSuperPixels, superPixelCompactness) carSuperPixelLabels = getSuperPixelLabelledImage(image, superPixelsMask, superPixelLabels) plotSuperPixelImage(image, carSuperPixelLabels, orientation)
def test(): # TODO use reference classifier classifierName = "/home/amb/dev/mrf/classifiers/randomForest/superpixel/randyForest_superPixel_maxDepth15_0.6Data.pkl" classifier = pomio.unpickleObject(classifierName) carFile = "7_3_s.bmp" msrcData = "/home/amb/dev/mrf/data/MSRC_ObjCategImageDatabase_v2" car = pomio.msrc_loadImages(msrcData, ["Images/" + carFile])[0] groundTruth = car.m_gt mask = SuperPixels.getSuperPixels_SLIC(car.m_img, 400, 10) spLabels = SuperPixelClassifier.predictSuperPixelLabels( classifier, car.m_img, 400, 10)[0] prediction = SuperPixelClassifier.getSuperPixelLabelledImage( car.m_img, mask, spLabels) # save prediction to file pomio.writeMatToCSV( prediction, "/home/amb/dev/eval/test/predict/testPrediction1.labels") results = evaluatePrediction(prediction, groundTruth) print "\nINFO: Car test eval results::\n\t", results
def loadReferenceGroundTruthLabels(sourceData, imgName): gtFile = str(sourceData) + "/GroundTruth/" + str(imgName) if "_GT" in imgName: imgName = imgName.replace("_GT" , "") gtImgLabels = pomio.msrc_loadImages(sourceData , ["Images/" + imgName] )[0].m_gt return gtImgLabels
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 testClassifier(classifierFilename, case): assert case == "lena" or case == "car", "case parameter should be lena or car" superPixelClassifier = pomio.unpickleObject(classifierFilename) print "\n*Loaded classifier [ ", type( superPixelClassifier), "] from:", classifierFilename image = None orientation = None if case == "car": print "*Loading MSRC car image::" image = pomio.msrc_loadImages( "/home/amb/dev/mrf/data/MSRC_ObjCategImageDatabase_v2", ['Images/7_3_s.bmp'])[0].m_img orientation = "lower" elif case == "lena": print "*Loading Lena.jpg" image = skimage.data.lena() orientation = "upper" print "*Predicting superpixel labels in image::" numberSuperPixels = 400 superPixelCompactness = 10 [superPixelLabels, superPixelsMask] = predictSuperPixelLabels(superPixelClassifier, image, numberSuperPixels, superPixelCompactness) carSuperPixelLabels = getSuperPixelLabelledImage(image, superPixelsMask, superPixelLabels) plotSuperPixelImage(image, carSuperPixelLabels, orientation)
def test(): classifierLocation = "/home/amb/dev/mrf/classifiers/logisticRegression/superpixel/logReg_miniMSRC.pkl" classifier = pomio.unpickleObject(classifierLocation) carFile = "7_3_s.bmp" msrcData = "/home/amb/dev/mrf/data/MSRC_ObjCategImageDatabase_v2" car = pomio.msrc_loadImages(msrcData, ["Images/" + carFile])[0] groundTruth = car.m_gt mask = superPixels.getSuperPixels_SLIC(car.m_img, 400, 10) spLabels = SuperPixelClassifier.predictSuperPixelLabels( classifier, car.m_img, 400, 10, True)[0] prediction = SuperPixelClassifier.getSuperPixelLabelledImage( car.m_img, mask, spLabels) # save prediction to file pomio.writeMatToCSV(prediction, "/home/amb/dev/mrf/eval/testPrediction1.labels") results = evaluatePrediction(prediction, groundTruth, carFile) print "\nINFO: Car test eval results::\n\t", results classResults = evaluateClassPerformance(prediction, groundTruth) print "\nINFO: Car test eval class results::\n\t", classResults confusionResults = evaluateConfusionMatrix(prediction, groundTruth) print "\nINFO: Car test eval confusion matrix results::\n\t", "Just sum up entries... ", np.sum( confusionResults)
def loadReferenceGroundTruthLabels(sourceData, imgName): gtFile = str(sourceData) + "/GroundTruth/" + str(imgName) if "_GT" in imgName: imgName = imgName.replace("_GT", "") gtImgLabels = pomio.msrc_loadImages(sourceData, ["Images/" + imgName])[0].m_gt return gtImgLabels
def neighbourClassImageCount(msrcDataLocation, outputFileLocation): # for each non-matching class pair, count images where they are neighbours classes = pomio.msrc_classLabels totalClasses = np.size(classes) msrcImages = pomio.msrc_loadImages(msrcDataLocation) print "*Imported MSRC image data using pomio.py::" , np.shape(msrcImages) # numberOfPairs = factorial(totalClasses) / (factorial(totalClasses -2) * factorial(2)) result = None for startClassIdx in range(0, totalClasses): for endClassIdx in range(0, totalClasses): if (not startClassIdx == endClassIdx) and (endClassIdx > startClassIdx): # we have a class pair startClassValue = classes[startClassIdx] endClassValue = classes[endClassIdx] neighbourCount = 0 # loop over images, increase count when an image includes startClass-endClass neighbours for imageIdx in range(0,np.size(msrcImages)): imageGroundTruth = msrcImages[imageIdx].m_gt # Get binary representation of image for each class startClassChannel = (imageGroundTruth == startClassIdx) startClassChannel = startClassChannel.astype('int') endClassChannel = (imageGroundTruth == endClassIdx) endClassChannel= endClassChannel.astype('int') # get the difference (do not expect pixel to be in both classes!) classDifference = startClassChannel - endClassChannel # get x and y gradient dx_classDiff, dy_classDiff = np.gradient(classDifference) # if absolute value of x or y gradient = 2, neighbour pixels if(np.any(dx_classDiff == 2) or (np.any(dy_classDiff == 2))): neighbourCount = neighbourCount + 1 print "Pair:[" + str(startClassValue) + ", " + str(endClassValue) +"] count = " + str(neighbourCount) if result == None: result = np.array([ startClassValue, endClassValue, neighbourCount]) else: result = np.vstack([ result, [ startClassValue, endClassValue, str(neighbourCount)] ] ) print "Shape of result data::", np.shape(result) print "Class pair image count result::", result np.savetxt(outputFileLocation, result, delimiter=",", fmt="%.32f")
def areAllMsrcGroundTruthImagesTheSameSize(msrcDataLocation): msrcImages = pomio.msrc_loadImages(msrcDataLocation) size = int(msrcImages[0].m_gt.size) equalSize = True for imageIdx in range(1, np.size(msrcImages)): imageSize = int(msrcImages[imageIdx].m_gt.size) if not size == imageSize: equalSize = False print "Image#" + str(imageIdx+1) + " doesn't match first size (" + str(size) + "), imageSize=" + str(imageSize) return equalSize
def areAllMsrcGroundTruthImagesTheSameSize(msrcDataLocation): msrcImages = pomio.msrc_loadImages(msrcDataLocation) size = int(msrcImages[0].m_gt.size) equalSize = True for imageIdx in range(1, np.size(msrcImages)): imageSize = int(msrcImages[imageIdx].m_gt.size) if not size == imageSize: equalSize = False print "Image#" + str(imageIdx + 1) + " doesn't match first size (" + str( size) + "), imageSize=" + str(imageSize) return equalSize
def getUniqueImageSizes(msrcDataLocation): msrcImages = pomio.msrc_loadImages(msrcDataLocation) uniqueImageSizes = np.array([]) firstSize = msrcImages[0].m_gt.size uniqueImageSizes = np.append(uniqueImageSizes, firstSize) for imageIdx in range(1, np.size(msrcImages)): imageSize = msrcImages[imageIdx].m_gt.size if not imageSize in uniqueImageSizes: uniqueImageSizes = np.append([uniqueImageSizes], imageSize) return uniqueImageSizes
def getUniqueImageSizes(msrcDataLocation): msrcImages = pomio.msrc_loadImages(msrcDataLocation) uniqueImageSizes = np.array([]) firstSize = msrcImages[0].m_gt.size uniqueImageSizes = np.append(uniqueImageSizes, firstSize) for imageIdx in range(1, np.size(msrcImages)): imageSize = msrcImages[imageIdx].m_gt.size if not imageSize in uniqueImageSizes: uniqueImageSizes = np.append([uniqueImageSizes] , imageSize) return uniqueImageSizes
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 test(): # TODO use reference classifier classifierName = "/home/amb/dev/mrf/classifiers/randomForest/superpixel/randyForest_superPixel_maxDepth15_0.6Data.pkl" classifier = pomio.unpickleObject(classifierName) carFile = "7_3_s.bmp" msrcData = "/home/amb/dev/mrf/data/MSRC_ObjCategImageDatabase_v2" car = pomio.msrc_loadImages(msrcData , [ "Images/" + carFile ] )[0] groundTruth = car.m_gt mask = SuperPixels.getSuperPixels_SLIC(car.m_img, 400, 10) spLabels = SuperPixelClassifier.predictSuperPixelLabels(classifier, car.m_img,400,10)[0] prediction = SuperPixelClassifier.getSuperPixelLabelledImage(car.m_img, mask, spLabels) # save prediction to file pomio.writeMatToCSV(prediction, "/home/amb/dev/eval/test/predict/testPrediction1.labels") results = evaluatePrediction(prediction, groundTruth) print "\nINFO: Car test eval results::\n\t" , results
def imageCountPerClass(msrcDataLocation): classes = pomio.msrc_classLabels totalClasses = np.size(classes) classImageCount = np.arange(0, totalClasses) msrcImages = pomio.msrc_loadImages(msrcDataLocation) print "\t*Imported MSRC image data using pomio.py::", np.shape(msrcImages) for classIdx in range(0, totalClasses): classValue = classes[classIdx] imageCountForClass = 0 for imageIdx in range(0, np.size(msrcImages)): image = msrcImages[imageIdx] imageGroundTruth = image.m_gt if classIdx in imageGroundTruth: imageCountForClass = imageCountForClass + 1 # add total count to the class count result print "Class = " + str(classValue), ", image count=" + str( imageCountForClass) classImageCount[classIdx] = int(imageCountForClass) result = np.array([classes, classImageCount]) print "Image count per class::\n", result return result
def imageCountPerClass(msrcDataLocation): classes = pomio.msrc_classLabels totalClasses = np.size(classes) classImageCount = np.arange(0,totalClasses) msrcImages = pomio.msrc_loadImages(msrcDataLocation) print "\t*Imported MSRC image data using pomio.py::" , np.shape(msrcImages) for classIdx in range(0,totalClasses): classValue = classes[classIdx] imageCountForClass = 0; for imageIdx in range(0,np.size(msrcImages)): image = msrcImages[imageIdx] imageGroundTruth = image.m_gt if classIdx in imageGroundTruth: imageCountForClass = imageCountForClass + 1 # add total count to the class count result print "Class = " + str(classValue), ", image count=" + str(imageCountForClass) classImageCount[classIdx] = int(imageCountForClass) result = np.array([classes, classImageCount]) print "Image count per class::\n" , result return result
msrcImages = pomio.msrc_loadImages(msrcData, [\ 'Images/10_1_s.bmp',\ 'Images/10_2_s.bmp',\ 'Images/11_1_s.bmp',\ 'Images/11_2_s.bmp',\ 'Images/12_1_s.bmp',\ 'Images/12_2_s.bmp',\ 'Images/13_1_s.bmp',\ 'Images/13_2_s.bmp',\ 'Images/14_1_s.bmp',\ 'Images/14_2_s.bmp',\ 'Images/15_1_s.bmp',\ 'Images/15_2_s.bmp',\ 'Images/16_1_s.bmp',\ 'Images/16_2_s.bmp',\ 'Images/17_1_s.bmp',\ 'Images/17_2_s.bmp',\ 'Images/18_1_s.bmp',\ 'Images/18_2_s.bmp',\ 'Images/19_1_s.bmp',\ 'Images/19_2_s.bmp',\ 'Images/1_1_s.bmp',\ 'Images/1_2_s.bmp',\ 'Images/20_1_s.bmp',\ 'Images/20_2_s.bmp',\ 'Images/2_1_s.bmp',\ 'Images/2_2_s.bmp',\ 'Images/3_1_s.bmp',\ 'Images/3_2_s.bmp',\ 'Images/4_1_s.bmp',\ 'Images/4_2_s.bmp',\ 'Images/5_1_s.bmp',\ 'Images/5_2_s.bmp',\ 'Images/6_1_s.bmp',\ 'Images/6_2_s.bmp',\ 'Images/7_1_s.bmp',\ 'Images/7_2_s.bmp',\ 'Images/8_1_s.bmp',\ 'Images/8_2_s.bmp',\ 'Images/9_1_s.bmp',\ 'Images/9_2_s.bmp'])
if __name__ == "__main__": # Create network print "*Creating neural net" net = createDefaultNeuralNet() msrcData = "/home/amb/dev/mrf/data/MSRC_ObjCategImageDatabase_v2" print "\n*Creating training dataset" labeledData = createTrainingSupervisedDataSet(msrcData, 0.05, True) print "\n*Training network via backpropogation" trainingResult = trainNetworkBackprop(net, labeledData) net = trainingResult[0] trainer = trainingResult[1] predictImage = pomio.msrc_loadImages(msrcData)[1] print "\n*Read in an image from the MSRC dataset::", np.shape( predictImage.m_img) # todo: replace with features.computePixelFeatures JRS imageFeatures = FeatureGenerator.generatePixelFeaturesForImage( predictImage.m_img) print "\n*Using neural net to predict class label::" prediction = predictClass(imageFeatures, net) print prediction
#!/usr/bin/env python import sys import matplotlib.pyplot as plt # Give it the path to the data set as an arg import pomio X = pomio.msrc_loadImages(sys.argv[1]) print X # Display the images plt.ion() for img in X: plt.figure(1) plt.imshow(img.m_img) plt.title("Image %s" % img.m_imgFn) plt.figure(2) plt.imshow(img.m_gt) plt.title("labels") plt.draw() plt.pause(0.2) # plt.ioff() # plt.show()
def neighbourClassImageCount(msrcDataLocation, outputFileLocation): # for each non-matching class pair, count images where they are neighbours classes = pomio.msrc_classLabels totalClasses = np.size(classes) msrcImages = pomio.msrc_loadImages(msrcDataLocation) print "*Imported MSRC image data using pomio.py::", np.shape(msrcImages) # numberOfPairs = factorial(totalClasses) / (factorial(totalClasses -2) * factorial(2)) result = None for startClassIdx in range(0, totalClasses): for endClassIdx in range(0, totalClasses): if (not startClassIdx == endClassIdx) and (endClassIdx > startClassIdx): # we have a class pair startClassValue = classes[startClassIdx] endClassValue = classes[endClassIdx] neighbourCount = 0 # loop over images, increase count when an image includes startClass-endClass neighbours for imageIdx in range(0, np.size(msrcImages)): imageGroundTruth = msrcImages[imageIdx].m_gt # Get binary representation of image for each class startClassChannel = (imageGroundTruth == startClassIdx) startClassChannel = startClassChannel.astype('int') endClassChannel = (imageGroundTruth == endClassIdx) endClassChannel = endClassChannel.astype('int') # get the difference (do not expect pixel to be in both classes!) classDifference = startClassChannel - endClassChannel # get x and y gradient dx_classDiff, dy_classDiff = np.gradient(classDifference) # if absolute value of x or y gradient = 2, neighbour pixels if (np.any(dx_classDiff == 2) or (np.any(dy_classDiff == 2))): neighbourCount = neighbourCount + 1 print "Pair:[" + str(startClassValue) + ", " + str( endClassValue) + "] count = " + str(neighbourCount) if result == None: result = np.array( [startClassValue, endClassValue, neighbourCount]) else: result = np.vstack([ result, [startClassValue, endClassValue, str(neighbourCount)] ]) print "Shape of result data::", np.shape(result) print "Class pair image count result::", result np.savetxt(outputFileLocation, result, delimiter=",", fmt="%.32f")
#!/usr/bin/env python import sys import pomio import amntools import matplotlib.pyplot as plt import numpy as np import FeatureGenerator import matplotlib # Load all data print 'Loading all data...' # first arg should be msrc data path data = pomio.msrc_loadImages( sys.argv[1], ['Images/7_3_s.bmp']) # get particular image we like ex = data[0] plt.figure() plt.imshow(ex.m_img) plt.title('original image') plt.figure() clrs = [[z/255.0 for z in c[1]] for c in pomio.msrc_classToRGB] pomio.showLabels( ex.m_gt ) plt.title('ground truth labels' ) print 'unique class labels: ', np.unique(ex.m_gt) # generate features imagePixelFeatures = FeatureGenerator.generatePixelFeaturesForImage(ex.m_img)
if __name__ == "__main__": # Create network print "*Creating neural net" net = createDefaultNeuralNet() msrcData = "/home/amb/dev/mrf/data/MSRC_ObjCategImageDatabase_v2" print "\n*Creating training dataset" labeledData = createTrainingSupervisedDataSet(msrcData, 0.05, True) print "\n*Training network via backpropogation" trainingResult = trainNetworkBackprop(net, labeledData) net = trainingResult[0] trainer = trainingResult[1] predictImage = pomio.msrc_loadImages(msrcData)[1] print "\n*Read in an image from the MSRC dataset::" , np.shape(predictImage.m_img) imageFeatures = FeatureGenerator.generatePixelFeaturesForImage(predictImage.m_img) print "\n*Using neural net to predict class label::" prediction = predictClass(imageFeatures, net) print prediction
#!/usr/bin/env python import sys import matplotlib.pyplot as plt # Give it the path to the data set as an arg import pomio X = pomio.msrc_loadImages( sys.argv[1] ) print X # Display the images plt.ion() for img in X: plt.figure(1) plt.imshow( img.m_img ) plt.title('Image %s' % img.m_imgFn) plt.figure(2) plt.imshow( img.m_gt ) plt.title('labels') plt.draw() plt.pause(0.2) #plt.ioff() #plt.show()
if (trainSplit == 1.0) and (cvSplit == 0.0 and testSplit == 0.0): writeDataType = False else: # we get here if we have non-default splits writeDataType = True # Do the expected thing if we have a non-default split if args.v: print "Loading data" if writeDataType == True: # Get train, test and cv datasets print "\nSplitting data into sets: train =", trainSplit, "test =", testSplit, "cvSplit =", cvSplit keepClassDistForTraining = False #!! [trainData, cvData, testData] = pomio.splitInputDataset_msrcData( pomio.msrc_loadImages(msrcDataDirectory, subset=None), scaleFrac, keepClassDistForTraining, trainSplit, cvSplit, testSplit, ) assert trainData != None, "Training data object is null" assert len(trainData) > 0, "Training data contains no data" if testData == None or len(testData) == 0: print "WARNING: Testing data contains no data" print "Creating training set from %d images, and test set from %d images" % (len(trainData), len(testData))
if (trainSplit == 1.0) and (cvSplit == 0.0 and testSplit == 0.0): writeDataType = False else: # we get here if we have non-default splits writeDataType = True # Do the expected thing if we have a non-default split if args.v: print 'Loading data' if writeDataType == True: # Get train, test and cv datasets print "\nSplitting data into sets: train =", trainSplit, "test =", testSplit, "cvSplit =", cvSplit keepClassDistForTraining = False #!! [trainData, cvData, testData] = pomio.splitInputDataset_msrcData(\ pomio.msrc_loadImages(msrcDataDirectory, subset=None), scaleFrac, keepClassDistForTraining, trainSplit , cvSplit , testSplit) assert trainData != None, "Training data object is null" assert len(trainData) > 0, "Training data contains no data" if testData == None or len(testData) == 0: print 'WARNING: Testing data contains no data' print "Creating training set from %d images, and test set from %d images" % ( len(trainData), len(testData)) # Process and persist feature and labels for superpixels in image dataset print "Create & save training feature and label superpixel data" createAndSaveFeatureLabelData(trainData, outfileBase, "train", outfileType, numberSuperPixels, superPixelCompactness,
#!/usr/bin/env python import pomio import amntools import matplotlib.pyplot as plt import numpy as np from scipy.misc import imread import FeatureGenerator import matplotlib # Load all data print 'Loading all data...' data = pomio.msrc_loadImages('/home/jamie/data/MSRC_ObjCategImageDatabase_v2', \ ['Images/7_3_s.bmp']) # get particular image we like ex = data[0] plt.figure() plt.imshow(ex.m_img) plt.title('original image') plt.figure() clrs = [[z / 255.0 for z in c[1]] for c in pomio.msrc_classToRGB] pomio.showLabels(ex.m_gt) plt.title('ground truth labels') print 'unique class labels: ', np.unique(ex.m_gt) # generate features imagePixelFeatures = FeatureGenerator.generatePixelFeaturesForImage(ex.m_img) # For each feature, how many distinct values?