def __init__(self, *args, **kwargs): super(OpLabelPipeline, self).__init__(*args, **kwargs) self.opInputShapeReader = OpShapeReader(parent=self) self.opInputShapeReader.Input.connect(self.RawImage) self.opLabelArray = OpBlockedSparseLabelArray(parent=self) self.opLabelArray.Input.connect(self.LabelInput) self.opLabelArray.shape.connect(self.opInputShapeReader.OutputShape) self.opLabelArray.eraser.setValue(100) self.opBoxArray = OpBlockedSparseLabelArray(parent=self) self.opBoxArray.Input.connect(self.BoxLabelInput) self.opBoxArray.shape.connect(self.opInputShapeReader.OutputShape) self.opBoxArray.eraser.setValue(100) # Initialize the delete input to -1, which means "no label". # Now changing this input to a positive value will cause label deletions. # (The deleteLabel input is monitored for changes.) self.opLabelArray.deleteLabel.setValue(-1) # Connect external outputs to their internal sources self.Output.connect(self.opLabelArray.Output) self.nonzeroBlocks.connect(self.opLabelArray.nonzeroBlocks) self.MaxLabel.connect(self.opLabelArray.maxLabel) self.BoxOutput.connect(self.opBoxArray.Output)
def test(shape, blockshape): g = Graph() opLabel = OpSparseLabelArray(graph=g) opLabelBlocked = OpBlockedSparseLabelArray(graph=g) opLabel.inputs["shape"].setValue(shape[:-1] + (1,)) opLabelBlocked.inputs["shape"].setValue(shape[:-1] + (1,)) opLabelBlocked.inputs["blockShape"].setValue(blockshape) opLabel.inputs["eraser"].setValue(100) opLabelBlocked.inputs["eraser"].setValue(100) niter = 100 for i in range(niter): value = numpy.random.randint(1, 10) key = randomKey(shape[:-1]) #key = (slice(0, 1, None), slice(4, 39, None), 0) #key = (slice(25, 49, None), slice(19, 50, None), slice(37, 50, None), 0) start, stop = sliceToRoi(key, shape) diff = stop - start valueshape = diff[:-1] valuearray = numpy.zeros(tuple(valueshape), dtype = numpy.uint8) valuearray[:] = value print i, key, valuearray.shape #opLabel.setInSlot(opLabel.inputs["Input"], key, valuearray) opLabel.Input[key] = valuearray #opLabelBlocked.setInSlot(opLabelBlocked.inputs["Input"], key, valuearray) opLabelBlocked.Input[key] = valuearray out = opLabel.outputs["Output"][:].allocate().wait() #print "first done" outblocked = opLabelBlocked.outputs["Output"][:].allocate().wait() #print "second done" assert_array_equal(out, outblocked) #print out #print outblocked nz1 = opLabel.outputs["nonzeroValues"][0].allocate().wait() nz2 = opLabelBlocked.outputs["nonzeroValues"][0].allocate().wait() for nz in nz1[0]: assert nz in nz2[0], "%r value not in blocked set"%nz for nz in nz2[0]: assert nz in nz1[0], "%r value not in non-blocked array"%nz print "done!"
def __init__(self, blockDims=None, *args, **kwargs): """ Instantiate all internal operators and connect them together. """ super(OpLabelingSingleLane, self).__init__(*args, **kwargs) # Configuration options if blockDims is None: blockDims = {'t': 1, 'x': 32, 'y': 32, 'z': 32, 'c': 1} assert isinstance(blockDims, dict) self._blockDims = blockDims # Create internal operators self.opInputShapeReader = OpShapeReader(parent=self) self.opLabelArray = OpBlockedSparseLabelArray(parent=self) # Set up label cache shape input self.opInputShapeReader.Input.connect(self.InputImage) # Note: 'shape' is a (poorly named) INPUT SLOT here self.opLabelArray.shape.connect(self.opInputShapeReader.OutputShape) # Set up other label cache inputs self.opLabelArray.Input.connect(self.LabelInput) self.opLabelArray.eraser.connect(self.LabelEraserValue) self.opLabelArray.deleteLabel.connect(self.LabelDelete) # Connect our internal outputs to our external outputs self.LabelImage.connect(self.opLabelArray.Output) self.NonzeroLabelBlocks.connect(self.opLabelArray.nonzeroBlocks) self.MaxLabelValue.connect(self.opLabelArray.maxLabel)
def veryRandomTest(shape, blockshape): g = Graph() opLabel = OpSparseLabelArray(graph=g) opLabelBlocked = OpBlockedSparseLabelArray(graph=g) opLabel.inputs["shape"].setValue(shape[:-1] + (1,)) opLabelBlocked.inputs["shape"].setValue(shape[:-1] + (1,)) opLabelBlocked.inputs["blockShape"].setValue(blockshape) opLabel.inputs["eraser"].setValue(100) opLabelBlocked.inputs["eraser"].setValue(100) niter = 100 for i in range(niter): value = numpy.random.randint(1, 10) key = randomKey(shape) #key = (slice(1, 41, None), slice(27, 50, None), slice(12, 50, None), 0) #key = (slice(7, 20, None), slice(7, 50, None), slice(35, 50, None), 0) start, stop = sliceToRoi(key, shape) diff = stop - start valueshape = diff[:-1] valuearray = numpy.zeros(tuple(valueshape), dtype = numpy.uint8) valuearray[:] = value #opLabel.setInSlot(opLabel.inputs["Input"], key, valuearray) opLabel.Input[key] = valuearray #opLabelBlocked.setInSlot(opLabelBlocked.inputs["Input"], key, valuearray) opLabelBlocked.Input[key] = valuearray key2 = randomKey(shape) #key2 = (slice(37, 49, None), slice(38, 50, None), slice(28, 50, None), 0) #key2 = (slice(7, 21, None), slice(21, 50, None), slice(10, 50, None)) print i, key, key2 out = opLabel.outputs["Output"][key2].allocate().wait() #print "first done" outblocked = opLabelBlocked.outputs["Output"][key2].allocate().wait() #print "second done" assert_array_equal(out, outblocked)
def testBlocks(shape, blockshape): g = Graph() opLabelBlocked = OpBlockedSparseLabelArray(graph=g) opLabelBlocked.inputs["shape"].setValue(shape[:-1] + (1,)) opLabelBlocked.inputs["blockShape"].setValue(blockshape) opLabelBlocked.inputs["eraser"].setValue(100) key = (slice(5, 15, None), slice(0, 10, None), 3, 3) value = numpy.zeros((10, 10, 1, 1), dtype=numpy.uint8) value[:] = 33 opLabelBlocked.setInSlot(opLabelBlocked.inputs["Input"], key, value) opLabelBlocked.Input[key] = value blocklist = opLabelBlocked.outputs["nonzeroBlocks"][:].allocate().wait() print blocklist offset = numpy.array(shape[:-1])/numpy.array(blockshape[:-1]) #offset = 0.5*step print offset nsteps = min(offset) for i in range(nsteps-1): start = offset + i*numpy.array(blockshape[:-1]) stop = offset+(i+1)*numpy.array(blockshape[:-1]) print start, stop key = roiToSlice(start, stop) print key valueshape = stop-start value = numpy.zeros(tuple(valueshape)+(1,), dtype=numpy.uint8) value[:] = 33 newkey = [x for x in key] newkey.append(0) #opLabelBlocked.setInSlot(opLabelBlocked.inputs["Input"], newkey, value) opLabelBlocked.Input[newkey] = value blocklist = opLabelBlocked.outputs["nonzeroBlocks"][:].allocate().wait() print blocklist
def setup(self): graph = Graph() op = OpBlockedSparseLabelArray(graph=graph) arrayshape = (1,100,100,10,1) op.inputs["shape"].setValue( arrayshape ) blockshape = (1,10,10,10,1) # Why doesn't this work if blockshape is an ndarray? op.inputs["blockShape"].setValue( blockshape ) op.eraser.setValue(100) dummyData = vigra.VigraArray(arrayshape, axistags=vigra.defaultAxistags('txyzc')) op.Input.setValue( dummyData ) slicing = sl[0:1, 1:15, 2:36, 3:7, 0:1] inDataShape = slicing2shape(slicing) inputData = ( 3*numpy.random.random(inDataShape) ).astype(numpy.uint8) op.Input[slicing] = inputData data = numpy.zeros(arrayshape, dtype=numpy.uint8) data[slicing] = inputData self.op = op self.slicing = slicing self.inData = inputData self.data = data
def test(self): graph = Graph() testVolumePath = 'tinyfib_volume.h5' # Unzip the data if necessary if not os.path.exists(testVolumePath): zippedTestVolumePath = testVolumePath + ".gz" assert os.path.exists(zippedTestVolumePath) os.system("gzip -d " + zippedTestVolumePath) assert os.path.exists(testVolumePath) f = h5py.File(testVolumePath, 'r') data = f['data'][...] data = data.view(vigra.VigraArray) data.axistags = vigra.defaultAxistags('txyzc') labels = f['labels'][...] assert data.shape[:-1] == labels.shape[:-1] assert labels.shape[-1] == 1 assert len(data.shape) == 5 f.close() scales = [0.3, 0.7, 1, 1.6, 3.5, 5.0, 10.0] featureIds = OpPixelFeaturesPresmoothed.DefaultFeatureIds # The following conditions cause this test to *usually* fail, but *sometimes* pass: # When using Structure Tensor EVs at sigma >= 3.5 (NaNs in feature matrix) # When using Gaussian Gradient Mag at sigma >= 3.5 (inf in feature matrix) # When using *any feature* at sigma == 10.0 (NaNs in feature matrix) # sigma: 0.3 0.7 1.0 1.6 3.5 5.0 10.0 selections = numpy.array([ [False, False, False, False, False, False, False], [False, False, False, False, False, False, False], [False, False, False, False, True, False, False], # ST EVs [False, False, False, False, False, False, False], [False, False, False, False, False, False, False], # GGM [False, False, False, False, False, False, False] ]) opFeatures = OpPixelFeaturesPresmoothed(graph=graph) opFeatures.Input.setValue(data) opFeatures.Scales.setValue(scales) opFeatures.FeatureIds.setValue(featureIds) opFeatures.Matrix.setValue(selections) opTrain = OpTrainRandomForestBlocked(graph=graph) opTrain.Images.resize(1) opTrain.Images[0].connect(opFeatures.Output) opTrain.Labels.resize(1) opTrain.nonzeroLabelBlocks.resize(1) # This test only fails when this flag is True. use_sparse_label_storage = True if use_sparse_label_storage: opLabelArray = OpBlockedSparseLabelArray(graph=graph) opLabelArray.inputs["shape"].setValue(labels.shape) opLabelArray.inputs["blockShape"].setValue((1, 32, 32, 32, 1)) opLabelArray.inputs["eraser"].setValue(100) opTrain.nonzeroLabelBlocks[0].connect(opLabelArray.nonzeroBlocks) # Slice the label data into the sparse array storage opLabelArray.Input[...] = labels[...] opTrain.Labels[0].connect(opLabelArray.Output) else: # Skip the sparse storage operator and provide labels as one big block opTrain.Labels[0].setValue(labels) # One big block opTrain.nonzeroLabelBlocks.resize(1) opTrain.nonzeroLabelBlocks[0].setValue([[slice(None, None, None)] * 5]) # Sanity check: Make sure we configured the training operator correctly. readySlots = [slot.ready() for slot in opTrain.inputs.values()] assert all(readySlots) # Generate the classifier classifier = opTrain.Classifier.value