def __init__(self, block_roi, halo_padding, *args, **kwargs): super(self.__class__, self).__init__(*args, **kwargs) self.block_roi = block_roi # In global coordinates self._halo_padding = halo_padding self._opBinarySubRegion = OpSubRegion(parent=self) self._opBinarySubRegion.Input.connect(self.BinaryImage) self._opRawSubRegion = OpSubRegion(parent=self) self._opRawSubRegion.Input.connect(self.RawImage) self._opExtract = OpObjectExtraction(parent=self) self._opExtract.BinaryImage.connect(self._opBinarySubRegion.Output) self._opExtract.RawImage.connect(self._opRawSubRegion.Output) self._opExtract.Features.connect(self.SelectedFeatures) self.BlockwiseRegionFeatures.connect( self._opExtract.BlockwiseRegionFeatures) self._opPredict = OpObjectPredict(parent=self) self._opPredict.Features.connect(self._opExtract.RegionFeatures) self._opPredict.SelectedFeatures.connect(self.SelectedFeatures) self._opPredict.Classifier.connect(self.Classifier) self._opPredict.LabelsCount.connect(self.LabelsCount) self._opPredictionImage = OpRelabelSegmentation(parent=self) self._opPredictionImage.Image.connect(self._opExtract.LabelImage) self._opPredictionImage.Features.connect( self._opExtract.RegionFeatures) self._opPredictionImage.ObjectMap.connect(self._opPredict.Predictions)
def __init__(self, block_roi, halo_padding, *args, **kwargs): super(self.__class__, self).__init__(*args, **kwargs) self.block_roi = block_roi # In global coordinates self._halo_padding = halo_padding self._opBinarySubRegion = OpSubRegion(parent=self) self._opBinarySubRegion.Input.connect(self.BinaryImage) self._opRawSubRegion = OpSubRegion(parent=self) self._opRawSubRegion.Input.connect(self.RawImage) self._opExtract = OpObjectExtraction(parent=self) self._opExtract.BinaryImage.connect(self._opBinarySubRegion.Output) self._opExtract.RawImage.connect(self._opRawSubRegion.Output) self._opExtract.Features.connect(self.SelectedFeatures) self.BlockwiseRegionFeatures.connect( self._opExtract.BlockwiseRegionFeatures) self._opExtract._opRegFeats._opCache.name = "blockwise-regionfeats-cache" self._opPredict = OpObjectPredict(parent=self) self._opPredict.Features.connect(self._opExtract.RegionFeatures) self._opPredict.SelectedFeatures.connect(self.SelectedFeatures) self._opPredict.Classifier.connect(self.Classifier) self._opPredict.LabelsCount.connect(self.LabelsCount) self.ObjectwisePredictions.connect(self._opPredict.Predictions) self._opPredictionImage = OpRelabelSegmentation(parent=self) self._opPredictionImage.Image.connect(self._opExtract.LabelImage) self._opPredictionImage.Features.connect( self._opExtract.RegionFeatures) self._opPredictionImage.ObjectMap.connect(self._opPredict.Predictions) self._opPredictionCache = OpArrayCache(parent=self) self._opPredictionCache.Input.connect(self._opPredictionImage.Output) self._opProbabilityChannelsToImage = OpMultiRelabelSegmentation( parent=self) self._opProbabilityChannelsToImage.Image.connect( self._opExtract.LabelImage) self._opProbabilityChannelsToImage.ObjectMaps.connect( self._opPredict.ProbabilityChannels) self._opProbabilityChannelsToImage.Features.connect( self._opExtract.RegionFeatures) self._opProbabilityChannelStacker = OpMultiArrayStacker(parent=self) self._opProbabilityChannelStacker.Images.connect( self._opProbabilityChannelsToImage.Output) self._opProbabilityChannelStacker.AxisFlag.setValue('c') self._opProbabilityCache = OpArrayCache(parent=self) self._opProbabilityCache.Input.connect( self._opProbabilityChannelStacker.Output)
def setUp(self): g = Graph() self.op = OpRelabelSegmentation(graph=g)
class OpSingleBlockObjectPrediction(Operator): RawImage = InputSlot() BinaryImage = InputSlot() SelectedFeatures = InputSlot(rtype=List, stype=Opaque) Classifier = InputSlot() LabelsCount = InputSlot() PredictionImage = OutputSlot() BlockwiseRegionFeatures = OutputSlot() # Indexed by (t,c) # Schematic: # # RawImage -----> opRawSubRegion ------ _______________________ # \ / \ # BinaryImage --> opBinarySubRegion --> opExtract --(features)--> opPredict --(map)--> opPredictionImage --via execute()--> PredictionImage # / \ / / # SelectedFeatures----- \ Classifier / # \ / # (labels)------------------------ # +----------------------------------------------------------------+ # | input_shape = RawImage.meta.shape | # | | # | | # | | # | | # | | # | | # | halo_shape = blockshape + 2*halo_padding | # | +------------------------+ | # | | halo_roi | | # | | (for internal pipeline)| | # | | | | # | | +------------------+ | | # | | | block_roi | | | # | | | (output shape) | | | # | | | | | | # | | | | | | # | | | | | | # | | +------------------+ | | # | | | | # | | | | # | | | | # | +------------------------+ | # | | # | | # | | # | | # | | # | | # | | # +----------------------------------------------------------------+ def __init__(self, block_roi, halo_padding, *args, **kwargs): super(self.__class__, self).__init__(*args, **kwargs) self.block_roi = block_roi # In global coordinates self._halo_padding = halo_padding self._opBinarySubRegion = OpSubRegion(parent=self) self._opBinarySubRegion.Input.connect(self.BinaryImage) self._opRawSubRegion = OpSubRegion(parent=self) self._opRawSubRegion.Input.connect(self.RawImage) self._opExtract = OpObjectExtraction(parent=self) self._opExtract.BinaryImage.connect(self._opBinarySubRegion.Output) self._opExtract.RawImage.connect(self._opRawSubRegion.Output) self._opExtract.Features.connect(self.SelectedFeatures) self.BlockwiseRegionFeatures.connect( self._opExtract.BlockwiseRegionFeatures) self._opPredict = OpObjectPredict(parent=self) self._opPredict.Features.connect(self._opExtract.RegionFeatures) self._opPredict.SelectedFeatures.connect(self.SelectedFeatures) self._opPredict.Classifier.connect(self.Classifier) self._opPredict.LabelsCount.connect(self.LabelsCount) self._opPredictionImage = OpRelabelSegmentation(parent=self) self._opPredictionImage.Image.connect(self._opExtract.LabelImage) self._opPredictionImage.Features.connect( self._opExtract.RegionFeatures) self._opPredictionImage.ObjectMap.connect(self._opPredict.Predictions) def setupOutputs(self): tagged_input_shape = self.RawImage.meta.getTaggedShape() self._halo_roi = self.computeHaloRoi( tagged_input_shape, self._halo_padding, self.block_roi) # In global coordinates # Output roi in our own coordinates (i.e. relative to the halo start) self._output_roi = self.block_roi - self._halo_roi[0] halo_start, halo_stop = map(tuple, self._halo_roi) self._opRawSubRegion.Start.setValue(halo_start) self._opRawSubRegion.Stop.setValue(halo_stop) # Binary image has only 1 channel. Adjust halo subregion. assert self.BinaryImage.meta.getTaggedShape()['c'] == 1 c_index = self.BinaryImage.meta.axistags.channelIndex binary_halo_roi = numpy.array(self._halo_roi) binary_halo_roi[:, c_index] = (0, 1) # Binary has only 1 channel. binary_halo_start, binary_halo_stop = map(tuple, binary_halo_roi) self._opBinarySubRegion.Start.setValue(binary_halo_start) self._opBinarySubRegion.Stop.setValue(binary_halo_stop) self.PredictionImage.meta.assignFrom( self._opPredictionImage.Output.meta) self.PredictionImage.meta.shape = tuple( numpy.subtract(self.block_roi[1], self.block_roi[0])) # Forward dirty regions to our own output self._opPredictionImage.Output.notifyDirty(self._handleDirtyPrediction) def execute(self, slot, subindex, roi, destination): assert slot == self.PredictionImage, "Unknown input slot" assert (numpy.array(roi.stop) <= self.PredictionImage.meta.shape).all(), "Roi is out-of-bounds" # Extract from the output (discard halo) halo_offset = numpy.subtract(self.block_roi[0], self._halo_roi[0]) adjusted_roi = (halo_offset + roi.start, halo_offset + roi.stop) return self._opPredictionImage.Output( *adjusted_roi).writeInto(destination).wait() def propagateDirty(self, slot, subindex, roi): """ Nothing to do here because dirty notifications are propagated through our internal pipeline and forwarded to our output via our notifyDirty handler. """ pass def _handleDirtyPrediction(self, slot, roi): """ Foward dirty notifications from our internal output slot to the external one, but first discard the halo and offset the roi to compensate for the halo. """ # Discard halo. dirtyRoi is in internal coordinates (i.e. relative to halo start) dirtyRoi = getIntersection((roi.start, roi.stop), self._output_roi, assertIntersect=False) if dirtyRoi is not None: halo_offset = numpy.subtract(self.block_roi[0], self._halo_roi[0]) adjusted_roi = dirtyRoi - halo_offset # adjusted_roi is in output coordinates (relative to output block start) self.PredictionImage.setDirty(*adjusted_roi) @classmethod def computeHaloRoi(cls, tagged_dataset_shape, halo_padding, block_roi): block_roi = numpy.array(block_roi) block_start, block_stop = block_roi channel_index = tagged_dataset_shape.keys().index('c') block_start[channel_index] = 0 block_stop[channel_index] = tagged_dataset_shape['c'] # Compute halo and clip to dataset bounds halo_start = block_start - halo_padding halo_start = numpy.maximum(halo_start, (0, ) * len(halo_start)) halo_stop = block_stop + halo_padding halo_stop = numpy.minimum(halo_stop, tagged_dataset_shape.values()) halo_roi = (halo_start, halo_stop) return halo_roi