class OpEdgeTraining(Operator): # Shared across lanes DEFAULT_FEATURES = {"Grayscale": ["standard_edge_mean"]} FeatureNames = InputSlot(value=DEFAULT_FEATURES) FreezeClassifier = InputSlot(value=True) TrainRandomForest = InputSlot(value=False) # Lane-wise WatershedSelectedInput = InputSlot(level=1) EdgeLabelsDict = InputSlot(level=1, value={}) VoxelData = InputSlot(level=1) # stacked input with edge probabilities Superpixels = InputSlot(level=1) GroundtruthSegmentation = InputSlot(level=1, optional=True) RawData = InputSlot(level=1, optional=True) # Used by the GUI for display only Rag = OutputSlot(level=1) EdgeProbabilities = OutputSlot(level=1) EdgeProbabilitiesDict = OutputSlot(level=1) # A dict of id_pair -> probabilities NaiveSegmentation = OutputSlot(level=1) def __init__(self, *args, **kwargs): super(OpEdgeTraining, self).__init__(*args, **kwargs) self.opCreateRag = OpMultiLaneWrapper(OpCreateRag, parent=self) self.opCreateRag.Superpixels.connect(self.Superpixels) self.opRagCache = OpMultiLaneWrapper(OpValueCache, parent=self, broadcastingSlotNames=["fixAtCurrent"]) self.opRagCache.Input.connect(self.opCreateRag.Rag) self.opRagCache.name = "opRagCache" self.opComputeEdgeFeatures = OpMultiLaneWrapper( OpComputeEdgeFeatures, parent=self, broadcastingSlotNames=["FeatureNames", "TrainRandomForest"] ) self.opComputeEdgeFeatures.FeatureNames.connect(self.FeatureNames) self.opComputeEdgeFeatures.VoxelData.connect(self.VoxelData) self.opComputeEdgeFeatures.Rag.connect(self.opRagCache.Output) self.opComputeEdgeFeatures.TrainRandomForest.connect(self.TrainRandomForest) self.opComputeEdgeFeatures.WatershedSelectedInput.connect(self.WatershedSelectedInput) self.opEdgeFeaturesCache = OpMultiLaneWrapper(OpValueCache, parent=self, broadcastingSlotNames=["fixAtCurrent"]) self.opEdgeFeaturesCache.Input.connect(self.opComputeEdgeFeatures.EdgeFeaturesDataFrame) self.opEdgeFeaturesCache.name = "opEdgeFeaturesCache" self.opTrainEdgeClassifier = OpTrainEdgeClassifier(parent=self) self.opTrainEdgeClassifier.EdgeLabelsDict.connect(self.EdgeLabelsDict) self.opTrainEdgeClassifier.EdgeFeaturesDataFrame.connect(self.opEdgeFeaturesCache.Output) # classifier cache input is set after training. self.opClassifierCache = OpValueCache(parent=self) self.opClassifierCache.Input.connect(self.opTrainEdgeClassifier.EdgeClassifier) self.opClassifierCache.fixAtCurrent.connect(self.FreezeClassifier) self.opClassifierCache.name = "opClassifierCache" self.opPredictEdgeProbabilities = OpMultiLaneWrapper( OpPredictEdgeProbabilities, parent=self, broadcastingSlotNames=["EdgeClassifier", "TrainRandomForest"] ) self.opPredictEdgeProbabilities.EdgeClassifier.connect(self.opClassifierCache.Output) self.opPredictEdgeProbabilities.EdgeFeaturesDataFrame.connect(self.opEdgeFeaturesCache.Output) self.opPredictEdgeProbabilities.TrainRandomForest.connect(self.TrainRandomForest) self.opEdgeProbabilitiesCache = OpMultiLaneWrapper( OpValueCache, parent=self, broadcastingSlotNames=["fixAtCurrent"] ) self.opEdgeProbabilitiesCache.Input.connect(self.opPredictEdgeProbabilities.EdgeProbabilities) self.opEdgeProbabilitiesCache.name = "opEdgeProbabilitiesCache" self.opEdgeProbabilitiesCache.fixAtCurrent.connect(self.FreezeClassifier) self.opEdgeProbabilitiesDict = OpMultiLaneWrapper(OpEdgeProbabilitiesDict, parent=self) self.opEdgeProbabilitiesDict.Rag.connect(self.opRagCache.Output) self.opEdgeProbabilitiesDict.EdgeProbabilities.connect(self.opEdgeProbabilitiesCache.Output) self.opEdgeProbabilitiesDictCache = OpMultiLaneWrapper( OpValueCache, parent=self, broadcastingSlotNames=["fixAtCurrent"] ) self.opEdgeProbabilitiesDictCache.Input.connect(self.opEdgeProbabilitiesDict.EdgeProbabilitiesDict) self.opEdgeProbabilitiesDictCache.name = "opEdgeProbabilitiesDictCache" self.opNaiveSegmentation = OpMultiLaneWrapper(OpNaiveSegmentation, parent=self) self.opNaiveSegmentation.Superpixels.connect(self.Superpixels) self.opNaiveSegmentation.Rag.connect(self.opRagCache.Output) self.opNaiveSegmentation.EdgeProbabilities.connect(self.opEdgeProbabilitiesCache.Output) self.opNaiveSegmentationCache = OpMultiLaneWrapper( OpBlockedArrayCache, parent=self, broadcastingSlotNames=["CompressionEnabled", "fixAtCurrent", "BypassModeEnabled"], ) self.opNaiveSegmentationCache.CompressionEnabled.setValue(True) self.opNaiveSegmentationCache.Input.connect(self.opNaiveSegmentation.Output) self.opNaiveSegmentationCache.name = "opNaiveSegmentationCache" self.Rag.connect(self.opRagCache.Output) self.EdgeProbabilities.connect(self.opEdgeProbabilitiesCache.Output) self.EdgeProbabilitiesDict.connect(self.opEdgeProbabilitiesDictCache.Output) self.NaiveSegmentation.connect(self.opNaiveSegmentationCache.Output) # All input multi-slots should be kept in sync # Output multi-slots will auto-sync via the graph multiInputs = [s for s in list(self.inputs.values()) if s.level >= 1] for s1 in multiInputs: for s2 in multiInputs: if s1 != s2: def insertSlot(a, b, position, finalsize): a.insertSlot(position, finalsize) s1.notifyInserted(partial(insertSlot, s2)) def removeSlot(a, b, position, finalsize): a.removeSlot(position, finalsize) s1.notifyRemoved(partial(removeSlot, s2)) # If superpixels change, we have to delete our edge labels. # Since we're dealing with multi-lane slot, setting up dirty handlers is a two-stage process. # (1) React to lane insertion by subscribing to dirty signals for the new lane. # (2) React to each lane's dirty signal by deleting the labels for that lane. def subscribe_to_dirty_sp(slot, position, finalsize): # A new lane was added. Subscribe to it's dirty signal. assert slot is self.Superpixels self.Superpixels[position].notifyDirty(self.handle_dirty_superpixels) self.Superpixels[position].notifyReady(self.handle_dirty_superpixels) self.Superpixels[position].notifyUnready(self.handle_dirty_superpixels) # When a new lane is added, set up the listener for dirtyness. self.Superpixels.notifyInserted(subscribe_to_dirty_sp) def handle_dirty_superpixels(self, subslot, *args): """ Discards the labels for a given lane. NOTE: In addition to callers in this file, this function is also called from multicutWorkflow.py """ # Determine which lane triggered this and delete it's labels lane_index = self.Superpixels.index(subslot) old_labels = self.EdgeLabelsDict[lane_index].value if old_labels: logger.warning("Superpixels changed. Deleting all labels in lane {}.".format(lane_index)) logger.info("Old labels were: {}".format(old_labels)) self.EdgeLabelsDict[lane_index].setValue({}) def setupOutputs(self): for sp_slot, seg_cache_blockshape_slot in zip(self.Superpixels, self.opNaiveSegmentationCache.BlockShape): assert sp_slot.meta.dtype == np.uint32 assert sp_slot.meta.getAxisKeys()[-1] == "c" seg_cache_blockshape_slot.setValue(sp_slot.meta.shape) def execute(self, slot, subindex, roi, result): assert False, "Shouldn't get here, but requesting slot: {}".format(slot) def propagateDirty(self, slot, subindex, roi): pass def setEdgeLabelsFromGroundtruth(self, lane_index): """ For the given lane, read the ground truth volume and automatically determine edge label values. """ op_view = self.getLane(lane_index) if not op_view.GroundtruthSegmentation.ready(): raise RuntimeError("There is no Ground Truth data available for lane: {}".format(lane_index)) logger.info("Loading groundtruth for lane {}...".format(lane_index)) gt_vol = op_view.GroundtruthSegmentation[:].wait() gt_vol = vigra.taggedView(gt_vol, op_view.GroundtruthSegmentation.meta.axistags) gt_vol = gt_vol.withAxes("".join(tag.key for tag in op_view.Superpixels.meta.axistags)) gt_vol = gt_vol.dropChannelAxis() rag = op_view.opRagCache.Output.value logger.info("Computing edge decisions from groundtruth...") decisions = rag.edge_decisions_from_groundtruth(gt_vol, asdict=False) edge_labels = decisions.view(np.uint8) + 1 edge_ids = list(map(tuple, rag.edge_ids)) edge_labels_dict = dict(list(zip(edge_ids, edge_labels))) op_view.EdgeLabelsDict.setValue(edge_labels_dict) def addLane(self, laneIndex): numLanes = len(self.VoxelData) assert numLanes == laneIndex, "Image lanes must be appended." self.VoxelData.resize(numLanes + 1) def removeLane(self, laneIndex, finalLength): self.VoxelData.removeSlot(laneIndex, finalLength) def getLane(self, laneIndex): return OperatorSubView(self, laneIndex) def clear_caches(self, lane_index): self.opClassifierCache.resetValue() for cache in [ self.opRagCache, self.opEdgeProbabilitiesCache, self.opEdgeProbabilitiesDictCache, self.opEdgeFeaturesCache, ]: c = cache.getLane(lane_index) c.resetValue()
class OpPixelClassification( Operator ): """ Top-level operator for pixel classification """ name="OpPixelClassification" category = "Top-level" # Graph inputs InputImages = InputSlot(level=1) # Original input data. Used for display only. PredictionMasks = InputSlot(level=1, optional=True) # Routed to OpClassifierPredict.PredictionMask. See there for details. LabelInputs = InputSlot(optional = True, level=1) # Input for providing label data from an external source LabelsAllowedFlags = InputSlot(stype='bool', level=1) # Specifies which images are permitted to be labeled FeatureImages = InputSlot(level=1) # Computed feature images (each channel is a different feature) CachedFeatureImages = InputSlot(level=1) # Cached feature data. FreezePredictions = InputSlot(stype='bool') ClassifierFactory = InputSlot(value=ParallelVigraRfLazyflowClassifierFactory(100)) PredictionsFromDisk = InputSlot(optional=True, level=1) PredictionProbabilities = OutputSlot(level=1) # Classification predictions (via feature cache for interactive speed) PredictionProbabilityChannels = OutputSlot(level=2) # Classification predictions, enumerated by channel SegmentationChannels = OutputSlot(level=2) # Binary image of the final selections. LabelImages = OutputSlot(level=1) # Labels from the user NonzeroLabelBlocks = OutputSlot(level=1) # A list if slices that contain non-zero label values Classifier = OutputSlot() # We provide the classifier as an external output for other applets to use CachedPredictionProbabilities = OutputSlot(level=1) # Classification predictions (via feature cache AND prediction cache) HeadlessPredictionProbabilities = OutputSlot(level=1) # Classification predictions ( via no image caches (except for the classifier itself ) HeadlessUint8PredictionProbabilities = OutputSlot(level=1) # Same as above, but 0-255 uint8 instead of 0.0-1.0 float32 HeadlessUncertaintyEstimate = OutputSlot(level=1) # Same as uncertaintly estimate, but does not rely on cached data. UncertaintyEstimate = OutputSlot(level=1) SimpleSegmentation = OutputSlot(level=1) # For debug, for now # GUI-only (not part of the pipeline, but saved to the project) LabelNames = OutputSlot() LabelColors = OutputSlot() PmapColors = OutputSlot() NumClasses = OutputSlot() def setupOutputs(self): self.LabelNames.meta.dtype = object self.LabelNames.meta.shape = (1,) self.LabelColors.meta.dtype = object self.LabelColors.meta.shape = (1,) self.PmapColors.meta.dtype = object self.PmapColors.meta.shape = (1,) def __init__( self, *args, **kwargs ): """ Instantiate all internal operators and connect them together. """ super(OpPixelClassification, self).__init__(*args, **kwargs) # Default values for some input slots self.FreezePredictions.setValue(True) self.LabelNames.setValue( [] ) self.LabelColors.setValue( [] ) self.PmapColors.setValue( [] ) # SPECIAL connection: The LabelInputs slot doesn't get it's data # from the InputImages slot, but it's shape must match. self.LabelInputs.connect( self.InputImages ) # Hook up Labeling Pipeline self.opLabelPipeline = OpMultiLaneWrapper( OpLabelPipeline, parent=self, broadcastingSlotNames=['DeleteLabel'] ) self.opLabelPipeline.RawImage.connect( self.InputImages ) self.opLabelPipeline.LabelInput.connect( self.LabelInputs ) self.opLabelPipeline.DeleteLabel.setValue( -1 ) self.LabelImages.connect( self.opLabelPipeline.Output ) self.NonzeroLabelBlocks.connect( self.opLabelPipeline.nonzeroBlocks ) # Hook up the Training operator self.opTrain = OpTrainClassifierBlocked( parent=self ) self.opTrain.ClassifierFactory.connect( self.ClassifierFactory ) self.opTrain.Labels.connect( self.opLabelPipeline.Output ) self.opTrain.Images.connect( self.CachedFeatureImages ) self.opTrain.nonzeroLabelBlocks.connect( self.opLabelPipeline.nonzeroBlocks ) # Hook up the Classifier Cache # The classifier is cached here to allow serializers to force in # a pre-calculated classifier (loaded from disk) self.classifier_cache = OpValueCache( parent=self ) self.classifier_cache.name = "OpPixelClassification.classifier_cache" self.classifier_cache.inputs["Input"].connect(self.opTrain.outputs['Classifier']) self.classifier_cache.inputs["fixAtCurrent"].connect( self.FreezePredictions ) self.Classifier.connect( self.classifier_cache.Output ) # Hook up the prediction pipeline inputs self.opPredictionPipeline = OpMultiLaneWrapper( OpPredictionPipeline, parent=self ) self.opPredictionPipeline.FeatureImages.connect( self.FeatureImages ) self.opPredictionPipeline.CachedFeatureImages.connect( self.CachedFeatureImages ) self.opPredictionPipeline.Classifier.connect( self.classifier_cache.Output ) self.opPredictionPipeline.FreezePredictions.connect( self.FreezePredictions ) self.opPredictionPipeline.PredictionsFromDisk.connect( self.PredictionsFromDisk ) self.opPredictionPipeline.PredictionMask.connect( self.PredictionMasks ) def _updateNumClasses(*args): """ When the number of labels changes, we MUST make sure that the prediction image changes its shape (the number of channels). Since setupOutputs is not called for mere dirty notifications, but is called in response to setValue(), we use this function to call setValue(). """ numClasses = len(self.LabelNames.value) self.opTrain.MaxLabel.setValue( numClasses ) self.opPredictionPipeline.NumClasses.setValue( numClasses ) self.NumClasses.setValue( numClasses ) self.LabelNames.notifyDirty( _updateNumClasses ) # Prediction pipeline outputs -> Top-level outputs self.PredictionProbabilities.connect( self.opPredictionPipeline.PredictionProbabilities ) self.CachedPredictionProbabilities.connect( self.opPredictionPipeline.CachedPredictionProbabilities ) self.HeadlessPredictionProbabilities.connect( self.opPredictionPipeline.HeadlessPredictionProbabilities ) self.HeadlessUint8PredictionProbabilities.connect( self.opPredictionPipeline.HeadlessUint8PredictionProbabilities ) self.PredictionProbabilityChannels.connect( self.opPredictionPipeline.PredictionProbabilityChannels ) self.SegmentationChannels.connect( self.opPredictionPipeline.SegmentationChannels ) self.UncertaintyEstimate.connect( self.opPredictionPipeline.UncertaintyEstimate ) self.SimpleSegmentation.connect( self.opPredictionPipeline.SimpleSegmentation ) self.HeadlessUncertaintyEstimate.connect( self.opPredictionPipeline.HeadlessUncertaintyEstimate ) def inputResizeHandler( slot, oldsize, newsize ): if ( newsize == 0 ): self.LabelImages.resize(0) self.NonzeroLabelBlocks.resize(0) self.PredictionProbabilities.resize(0) self.CachedPredictionProbabilities.resize(0) self.InputImages.notifyResized( inputResizeHandler ) # Debug assertions: Check to make sure the non-wrapped operators stayed that way. assert self.opTrain.Images.operator == self.opTrain def handleNewInputImage( multislot, index, *args ): def handleInputReady(slot): self._checkConstraints( index ) self.setupCaches( multislot.index(slot) ) multislot[index].notifyReady(handleInputReady) self.InputImages.notifyInserted( handleNewInputImage ) # If any feature image changes shape, we need to verify that the # channels are consistent with the currently cached classifier # Otherwise, delete the currently cached classifier. def handleNewFeatureImage( multislot, index, *args ): def handleFeatureImageReady(slot): def handleFeatureMetaChanged(slot): if ( self.classifier_cache.fixAtCurrent.value and self.classifier_cache.Output.ready() and slot.meta.shape is not None ): classifier = self.classifier_cache.Output.value channel_names = slot.meta.channel_names if classifier and classifier.feature_names != channel_names: self.classifier_cache.resetValue() slot.notifyMetaChanged(handleFeatureMetaChanged) multislot[index].notifyReady(handleFeatureImageReady) self.FeatureImages.notifyInserted( handleNewFeatureImage ) def handleNewMaskImage( multislot, index, *args ): def handleInputReady(slot): self._checkConstraints( index ) multislot[index].notifyReady(handleInputReady) self.PredictionMasks.notifyInserted( handleNewMaskImage ) # All input multi-slots should be kept in sync # Output multi-slots will auto-sync via the graph multiInputs = filter( lambda s: s.level >= 1, self.inputs.values() ) for s1 in multiInputs: for s2 in multiInputs: if s1 != s2: def insertSlot( a, b, position, finalsize ): a.insertSlot(position, finalsize) s1.notifyInserted( partial(insertSlot, s2 ) ) def removeSlot( a, b, position, finalsize ): a.removeSlot(position, finalsize) s1.notifyRemoved( partial(removeSlot, s2 ) ) def setupCaches(self, imageIndex): numImages = len(self.InputImages) inputSlot = self.InputImages[imageIndex] # # Can't setup if all inputs haven't been set yet. # if numImages != len(self.FeatureImages) or \ # numImages != len(self.CachedFeatureImages): # return # # self.LabelImages.resize(numImages) self.LabelInputs.resize(numImages) # Special case: We have to set up the shape of our label *input* according to our image input shape shapeList = list(self.InputImages[imageIndex].meta.shape) try: channelIndex = self.InputImages[imageIndex].meta.axistags.index('c') shapeList[channelIndex] = 1 except: pass self.LabelInputs[imageIndex].meta.shape = tuple(shapeList) self.LabelInputs[imageIndex].meta.axistags = inputSlot.meta.axistags def _checkConstraints(self, laneIndex): """ Ensure that all input images have the same number of channels. """ if not self.InputImages[laneIndex].ready(): return thisLaneTaggedShape = self.InputImages[laneIndex].meta.getTaggedShape() # Find a different lane and use it for comparison validShape = thisLaneTaggedShape for i, slot in enumerate(self.InputImages): if slot.ready() and i != laneIndex: validShape = slot.meta.getTaggedShape() break if validShape['c'] != thisLaneTaggedShape['c']: raise DatasetConstraintError( "Pixel Classification", "All input images must have the same number of channels. "\ "Your new image has {} channel(s), but your other images have {} channel(s)."\ .format( thisLaneTaggedShape['c'], validShape['c'] ) ) if len(validShape) != len(thisLaneTaggedShape): raise DatasetConstraintError( "Pixel Classification", "All input images must have the same dimensionality. "\ "Your new image has {} dimensions (including channel), but your other images have {} dimensions."\ .format( len(thisLaneTaggedShape), len(validShape) ) ) mask_slot = self.PredictionMasks[laneIndex] input_shape = tuple(thisLaneTaggedShape.values()) if mask_slot.ready() and mask_slot.meta.shape[:-1] != input_shape[:-1]: raise DatasetConstraintError( "Pixel Classification", "If you supply a prediction mask, it must have the same shape as the input image."\ "Your input image has shape {}, but your mask has shape {}."\ .format( input_shape, mask_slot.meta.shape ) ) def setInSlot(self, slot, subindex, roi, value): # Nothing to do here: All inputs that support __setitem__ # are directly connected to internal operators. pass def propagateDirty(self, slot, subindex, roi): # Nothing to do here: All outputs are directly connected to # internal operators that handle their own dirty propagation. pass def addLane(self, laneIndex): numLanes = len(self.InputImages) assert numLanes == laneIndex, "Image lanes must be appended." self.InputImages.resize(numLanes+1) def removeLane(self, laneIndex, finalLength): self.InputImages.removeSlot(laneIndex, finalLength) def getLane(self, laneIndex): return OperatorSubView(self, laneIndex) def importLabels(self, laneIndex, slot): # Load the data into the cache new_max = self.getLane( laneIndex ).opLabelPipeline.opLabelArray.ingestData( slot ) # Add to the list of label names if there's a new max label old_names = self.LabelNames.value old_max = len(old_names) if new_max > old_max: new_names = old_names + map( lambda x: "Label {}".format(x), range(old_max+1, new_max+1) ) self.LabelNames.setValue(new_names) # Make some default colors, too default_colors = [(255,0,0), (0,255,0), (0,0,255), (255,255,0), (255,0,255), (0,255,255), (128,128,128), (255, 105, 180), (255, 165, 0), (240, 230, 140) ] label_colors = self.LabelColors.value pmap_colors = self.PmapColors.value self.LabelColors.setValue( label_colors + default_colors[old_max:new_max] ) self.PmapColors.setValue( pmap_colors + default_colors[old_max:new_max] ) def mergeLabels(self, from_label, into_label): for laneIndex in range(len(self.InputImages)): self.getLane( laneIndex ).opLabelPipeline.opLabelArray.mergeLabels(from_label, into_label)
class OpPixelClassification(Operator): """ Top-level operator for pixel classification """ name = "OpPixelClassification" category = "Top-level" # Graph inputs InputImages = InputSlot( level=1) # Original input data. Used for display only. PredictionMasks = InputSlot( level=1, optional=True ) # Routed to OpClassifierPredict.PredictionMask. See there for details. LabelInputs = InputSlot( optional=True, level=1) # Input for providing label data from an external source FeatureImages = InputSlot( level=1 ) # Computed feature images (each channel is a different feature) CachedFeatureImages = InputSlot(level=1) # Cached feature data. FreezePredictions = InputSlot(stype='bool') ClassifierFactory = InputSlot( value=ParallelVigraRfLazyflowClassifierFactory(100)) PredictionsFromDisk = InputSlot(optional=True, level=1) PredictionProbabilities = OutputSlot( level=1 ) # Classification predictions (via feature cache for interactive speed) PredictionProbabilitiesUint8 = OutputSlot( level=1) # Same thing, but converted to uint8 first PredictionProbabilityChannels = OutputSlot( level=2) # Classification predictions, enumerated by channel SegmentationChannels = OutputSlot( level=2) # Binary image of the final selections. LabelImages = OutputSlot(level=1) # Labels from the user NonzeroLabelBlocks = OutputSlot( level=1) # A list if slices that contain non-zero label values Classifier = OutputSlot( ) # We provide the classifier as an external output for other applets to use CachedPredictionProbabilities = OutputSlot( level=1 ) # Classification predictions (via feature cache AND prediction cache) HeadlessPredictionProbabilities = OutputSlot( level=1 ) # Classification predictions ( via no image caches (except for the classifier itself ) HeadlessUint8PredictionProbabilities = OutputSlot( level=1) # Same as above, but 0-255 uint8 instead of 0.0-1.0 float32 HeadlessUncertaintyEstimate = OutputSlot( level=1 ) # Same as uncertaintly estimate, but does not rely on cached data. UncertaintyEstimate = OutputSlot(level=1) SimpleSegmentation = OutputSlot(level=1) # For debug, for now # GUI-only (not part of the pipeline, but saved to the project) LabelNames = OutputSlot() LabelColors = OutputSlot() PmapColors = OutputSlot() NumClasses = OutputSlot() def setupOutputs(self): self.LabelNames.meta.dtype = object self.LabelNames.meta.shape = (1, ) self.LabelColors.meta.dtype = object self.LabelColors.meta.shape = (1, ) self.PmapColors.meta.dtype = object self.PmapColors.meta.shape = (1, ) def __init__(self, *args, **kwargs): """ Instantiate all internal operators and connect them together. """ super(OpPixelClassification, self).__init__(*args, **kwargs) # Default values for some input slots self.FreezePredictions.setValue(True) self.LabelNames.setValue([]) self.LabelColors.setValue([]) self.PmapColors.setValue([]) # SPECIAL connection: The LabelInputs slot doesn't get it's data # from the InputImages slot, but it's shape must match. self.LabelInputs.connect(self.InputImages) # Hook up Labeling Pipeline self.opLabelPipeline = OpMultiLaneWrapper( OpLabelPipeline, parent=self, broadcastingSlotNames=['DeleteLabel']) self.opLabelPipeline.RawImage.connect(self.InputImages) self.opLabelPipeline.LabelInput.connect(self.LabelInputs) self.opLabelPipeline.DeleteLabel.setValue(-1) self.LabelImages.connect(self.opLabelPipeline.Output) self.NonzeroLabelBlocks.connect(self.opLabelPipeline.nonzeroBlocks) # Hook up the Training operator self.opTrain = OpTrainClassifierBlocked(parent=self) self.opTrain.ClassifierFactory.connect(self.ClassifierFactory) self.opTrain.Labels.connect(self.opLabelPipeline.Output) self.opTrain.Images.connect(self.FeatureImages) self.opTrain.nonzeroLabelBlocks.connect( self.opLabelPipeline.nonzeroBlocks) # Hook up the Classifier Cache # The classifier is cached here to allow serializers to force in # a pre-calculated classifier (loaded from disk) self.classifier_cache = OpValueCache(parent=self) self.classifier_cache.name = "OpPixelClassification.classifier_cache" self.classifier_cache.inputs["Input"].connect( self.opTrain.outputs['Classifier']) self.classifier_cache.inputs["fixAtCurrent"].connect( self.FreezePredictions) self.Classifier.connect(self.classifier_cache.Output) # Hook up the prediction pipeline inputs self.opPredictionPipeline = OpMultiLaneWrapper(OpPredictionPipeline, parent=self) self.opPredictionPipeline.FeatureImages.connect(self.FeatureImages) self.opPredictionPipeline.CachedFeatureImages.connect( self.CachedFeatureImages) self.opPredictionPipeline.Classifier.connect( self.classifier_cache.Output) self.opPredictionPipeline.FreezePredictions.connect( self.FreezePredictions) self.opPredictionPipeline.PredictionsFromDisk.connect( self.PredictionsFromDisk) self.opPredictionPipeline.PredictionMask.connect(self.PredictionMasks) # Feature Selection Stuff self.opFeatureMatrixCaches = OpMultiLaneWrapper(OpFeatureMatrixCache, parent=self) self.opFeatureMatrixCaches.LabelImage.connect( self.opLabelPipeline.Output) self.opFeatureMatrixCaches.FeatureImage.connect(self.FeatureImages) self.opFeatureMatrixCaches.LabelImage.setDirty( ) # do I still need this? def _updateNumClasses(*args): """ When the number of labels changes, we MUST make sure that the prediction image changes its shape (the number of channels). Since setupOutputs is not called for mere dirty notifications, but is called in response to setValue(), we use this function to call setValue(). """ numClasses = len(self.LabelNames.value) self.opTrain.MaxLabel.setValue(numClasses) self.opPredictionPipeline.NumClasses.setValue(numClasses) self.NumClasses.setValue(numClasses) self.LabelNames.notifyDirty(_updateNumClasses) # Prediction pipeline outputs -> Top-level outputs self.PredictionProbabilities.connect( self.opPredictionPipeline.PredictionProbabilities) self.PredictionProbabilitiesUint8.connect( self.opPredictionPipeline.PredictionProbabilitiesUint8) self.CachedPredictionProbabilities.connect( self.opPredictionPipeline.CachedPredictionProbabilities) self.HeadlessPredictionProbabilities.connect( self.opPredictionPipeline.HeadlessPredictionProbabilities) self.HeadlessUint8PredictionProbabilities.connect( self.opPredictionPipeline.HeadlessUint8PredictionProbabilities) self.PredictionProbabilityChannels.connect( self.opPredictionPipeline.PredictionProbabilityChannels) self.SegmentationChannels.connect( self.opPredictionPipeline.SegmentationChannels) self.UncertaintyEstimate.connect( self.opPredictionPipeline.UncertaintyEstimate) self.SimpleSegmentation.connect( self.opPredictionPipeline.SimpleSegmentation) self.HeadlessUncertaintyEstimate.connect( self.opPredictionPipeline.HeadlessUncertaintyEstimate) def inputResizeHandler(slot, oldsize, newsize): if (newsize == 0): self.LabelImages.resize(0) self.NonzeroLabelBlocks.resize(0) self.PredictionProbabilities.resize(0) self.CachedPredictionProbabilities.resize(0) self.InputImages.notifyResized(inputResizeHandler) # Debug assertions: Check to make sure the non-wrapped operators stayed that way. assert self.opTrain.Images.operator == self.opTrain def handleNewInputImage(multislot, index, *args): def handleInputReady(slot): self._checkConstraints(index) self.setupCaches(multislot.index(slot)) multislot[index].notifyReady(handleInputReady) self.InputImages.notifyInserted(handleNewInputImage) # If any feature image changes shape, we need to verify that the # channels are consistent with the currently cached classifier # Otherwise, delete the currently cached classifier. def handleNewFeatureImage(multislot, index, *args): def handleFeatureImageReady(slot): def handleFeatureMetaChanged(slot): if (self.classifier_cache.fixAtCurrent.value and self.classifier_cache.Output.ready() and slot.meta.shape is not None): classifier = self.classifier_cache.Output.value channel_names = slot.meta.channel_names if classifier and classifier.feature_names != channel_names: self.classifier_cache.resetValue() slot.notifyMetaChanged(handleFeatureMetaChanged) multislot[index].notifyReady(handleFeatureImageReady) self.FeatureImages.notifyInserted(handleNewFeatureImage) def handleNewMaskImage(multislot, index, *args): def handleInputReady(slot): self._checkConstraints(index) multislot[index].notifyReady(handleInputReady) self.PredictionMasks.notifyInserted(handleNewMaskImage) # All input multi-slots should be kept in sync # Output multi-slots will auto-sync via the graph multiInputs = filter(lambda s: s.level >= 1, self.inputs.values()) for s1 in multiInputs: for s2 in multiInputs: if s1 != s2: def insertSlot(a, b, position, finalsize): a.insertSlot(position, finalsize) s1.notifyInserted(partial(insertSlot, s2)) def removeSlot(a, b, position, finalsize): a.removeSlot(position, finalsize) s1.notifyRemoved(partial(removeSlot, s2)) def setupCaches(self, imageIndex): numImages = len(self.InputImages) inputSlot = self.InputImages[imageIndex] # # Can't setup if all inputs haven't been set yet. # if numImages != len(self.FeatureImages) or \ # numImages != len(self.CachedFeatureImages): # return # # self.LabelImages.resize(numImages) self.LabelInputs.resize(numImages) # Special case: We have to set up the shape of our label *input* according to our image input shape shapeList = list(self.InputImages[imageIndex].meta.shape) try: channelIndex = self.InputImages[imageIndex].meta.axistags.index( 'c') shapeList[channelIndex] = 1 except: pass self.LabelInputs[imageIndex].meta.shape = tuple(shapeList) self.LabelInputs[imageIndex].meta.axistags = inputSlot.meta.axistags def _checkConstraints(self, laneIndex): """ Ensure that all input images have the same number of channels. """ if not self.InputImages[laneIndex].ready(): return thisLaneTaggedShape = self.InputImages[laneIndex].meta.getTaggedShape() # Find a different lane and use it for comparison validShape = thisLaneTaggedShape for i, slot in enumerate(self.InputImages): if slot.ready() and i != laneIndex: validShape = slot.meta.getTaggedShape() break if 't' in thisLaneTaggedShape: del thisLaneTaggedShape['t'] if 't' in validShape: del validShape['t'] if validShape['c'] != thisLaneTaggedShape['c']: raise DatasetConstraintError( "Pixel Classification", "All input images must have the same number of channels. "\ "Your new image has {} channel(s), but your other images have {} channel(s)."\ .format( thisLaneTaggedShape['c'], validShape['c'] ) ) if len(validShape) != len(thisLaneTaggedShape): raise DatasetConstraintError( "Pixel Classification", "All input images must have the same dimensionality. "\ "Your new image has {} dimensions (including channel), but your other images have {} dimensions."\ .format( len(thisLaneTaggedShape), len(validShape) ) ) mask_slot = self.PredictionMasks[laneIndex] input_shape = self.InputImages[laneIndex].meta.shape if mask_slot.ready() and mask_slot.meta.shape[:-1] != input_shape[:-1]: raise DatasetConstraintError( "Pixel Classification", "If you supply a prediction mask, it must have the same shape as the input image."\ "Your input image has shape {}, but your mask has shape {}."\ .format( input_shape, mask_slot.meta.shape ) ) def setInSlot(self, slot, subindex, roi, value): # Nothing to do here: All inputs that support __setitem__ # are directly connected to internal operators. pass def propagateDirty(self, slot, subindex, roi): # Nothing to do here: All outputs are directly connected to # internal operators that handle their own dirty propagation. pass def addLane(self, laneIndex): numLanes = len(self.InputImages) assert numLanes == laneIndex, "Image lanes must be appended." self.InputImages.resize(numLanes + 1) def removeLane(self, laneIndex, finalLength): self.InputImages.removeSlot(laneIndex, finalLength) def getLane(self, laneIndex): return OperatorSubView(self, laneIndex) def importLabels(self, laneIndex, slot): # Load the data into the cache new_max = self.getLane( laneIndex).opLabelPipeline.opLabelArray.ingestData(slot) # Add to the list of label names if there's a new max label old_names = self.LabelNames.value old_max = len(old_names) if new_max > old_max: new_names = old_names + map(lambda x: "Label {}".format(x), range(old_max + 1, new_max + 1)) self.LabelNames.setValue(new_names) # Make some default colors, too default_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (128, 128, 128), (255, 105, 180), (255, 165, 0), (240, 230, 140)] label_colors = self.LabelColors.value pmap_colors = self.PmapColors.value self.LabelColors.setValue(label_colors + default_colors[old_max:new_max]) self.PmapColors.setValue(pmap_colors + default_colors[old_max:new_max]) def mergeLabels(self, from_label, into_label): for laneIndex in range(len(self.InputImages)): self.getLane(laneIndex).opLabelPipeline.opLabelArray.mergeLabels( from_label, into_label) def clearLabel(self, label_value): for laneIndex in range(len(self.InputImages)): self.getLane(laneIndex).opLabelPipeline.opLabelArray.clearLabel( label_value)