def extractDataset(targetDatasetPath, paths, parameters): # Unpack multispectralImagePath, panchromaticImagePath, positiveLocationPath = paths windowGeoLength, negativeRatio, multispectralPixelShiftValue, shiftCount = parameters # Show feedback view.sendFeedback('Extracting dataset...\n\ttargetDatasetPath = %s' % targetDatasetPath) # Extract samples extractor = Extractor(targetDatasetPath, windowGeoLength, multispectralPixelShiftValue, shiftCount, negativeRatio) extractor.extractSamples(positiveLocationPath, multispectralImagePath, panchromaticImagePath) # Record targetDataset = extractor.getSampleDatabase() multispectralImage = image_store.load(multispectralImagePath) panchromaticImage = image_store.load(panchromaticImagePath) points, spatialReference = point_store.load(positiveLocationPath) store.saveInformation(targetDatasetPath, { 'multispectral image': { 'path': multispectralImagePath, 'pixel width': multispectralImage.getPixelWidth(), 'pixel height': multispectralImage.getPixelHeight(), 'geo transform': multispectralImage.getGeoTransform(), }, 'panchromatic image': { 'path': panchromaticImagePath, 'pixel width': panchromaticImage.getPixelWidth(), 'pixel height': panchromaticImage.getPixelHeight(), 'geo transform': panchromaticImage.getGeoTransform(), }, 'positive location': { 'path': positiveLocationPath, 'location count': len(points), 'spatial reference': spatialReference, }, 'windows': { 'path': targetDatasetPath, 'sample count': targetDataset.countSamples(), 'positive sample count': targetDataset.countPositiveSamples(), 'negative sample count': targetDataset.countNegativeSamples(), }, 'parameters': { 'window geo length': windowGeoLength, 'multispectral pixel shift value': multispectralPixelShiftValue, 'shift count': shiftCount, 'negative ratio': negativeRatio, } }) # Return return targetDataset
def extractSamples(self, positiveLocationPath, multispectralImagePath, panchromaticImagePath): view.sendFeedback('Loading images...\n\tmultispectralImagePath = %s\n\tpanchromaticImagePath = %s' % (multispectralImagePath, panchromaticImagePath)) multispectralImage = image_store.load(multispectralImagePath) panchromaticImage = image_store.load(panchromaticImagePath) view.sendFeedback('Generating geoCenters...\n\tmultispectralPixelShiftValue = %s\n\tshiftCount = %s\n\tnegativeRatio = %s' % (self.multispectralPixelShiftValue, self.shiftCount, self.negativeRatio)) positiveMultispectralPixelCenters = self.loadPixelCenters(multispectralImage, positiveLocationPath) negativeMultispectralPixelCenters = self.makePixelCenters(multispectralImage, positiveMultispectralPixelCenters) positiveGeoCenters = multispectralImage.convertPixelLocationsToGeoLocations(positiveMultispectralPixelCenters) negativeGeoCenters = multispectralImage.convertPixelLocationsToGeoLocations(negativeMultispectralPixelCenters) view.sendFeedback('Extracting positive samples...') self.extract(1, positiveGeoCenters, multispectralImage, panchromaticImage) view.sendFeedback('Extracting negative samples...') self.extract(0, negativeGeoCenters, multispectralImage, panchromaticImage)
def combineDatasets(targetDatasetPath, datasetPaths, datasetSize=None, positiveFraction=None): # If positiveFraction is defined, if positiveFraction: # Get positiveSamplePacks = [] negativeSamplePacks = [] for dataset in itertools.imap(sample_store.load, datasetPaths): positiveSamplePacks.extend((dataset, x) for x in dataset.getPositiveSampleIDs()) negativeSamplePacks.extend((dataset, x) for x in dataset.getNegativeSampleIDs()) # Shuffle random.shuffle(positiveSamplePacks) random.shuffle(negativeSamplePacks) # Count totalPositiveCount = len(positiveSamplePacks) totalNegativeCount = len(negativeSamplePacks) # Chop # if positiveFraction >= 0.5: multiplier = (1 - positiveFraction) / positiveFraction scaledNegativeCount = int(multiplier * totalPositiveCount) datasetSampleIDs = positiveSamplePacks + negativeSamplePacks[:scaledNegativeCount] # else: # multiplier = positiveFraction / (1 - positiveFraction) # scaledPositiveCount = int(multiplier * totalNegativeCount) # datasetSampleIDs = positiveSamplePacks[:scaledPositiveCount] + negativeSamplePacks # If positiveFraction is not defined, else: # Get sampleIDs datasetSampleIDs = [] for dataset in itertools.imap(sample_store.load, datasetPaths): datasetSampleIDs.extend((dataset, x) for x in dataset.getSampleIDs()) # Shuffle random.shuffle(datasetSampleIDs) # Subset selectedDatasetSampleIDs = datasetSampleIDs[:datasetSize] if datasetSize else datasetSampleIDs view.sendFeedback('Making dataset with %d samples...' % len(selectedDatasetSampleIDs)) return sample_store.save(targetDatasetPath, selectedDatasetSampleIDs)