示例#1
0
    def testBasic(self):
        superpixels = generate_random_voronoi((100, 100, 100), 100)
        superpixels = superpixels.insertChannelAxis()

        voxel_data = superpixels.astype(np.float32)

        graph = Graph()
        multilane_op = OpEdgeTraining(graph=graph)
        multilane_op.VoxelData.resize(1)  # resizes all level-1 slots.
        op_view = multilane_op.getLane(0)

        op_view.VoxelData.setValue(voxel_data,
                                   extra_meta={'channel_names': ['Grayscale']})
        op_view.Superpixels.setValue(superpixels)

        multilane_op.FeatureNames.setValue(
            {"Grayscale": ['standard_edge_mean', 'standard_edge_count']})

        assert op_view.Rag.ready()

        # Pick some edges to label
        rag = op_view.Rag.value
        edge_A = tuple(rag.edge_ids[0])
        edge_B = tuple(rag.edge_ids[1])
        edge_C = tuple(rag.edge_ids[2])
        edge_D = tuple(rag.edge_ids[3])

        labels = {
            edge_A: 1,  # OFF
            edge_B: 1,
            edge_C: 2,  # ON
            edge_D: 2
        }

        op_view.EdgeLabelsDict.setValue(labels)
        op_view.FreezeClassifier.setValue(False)

        assert op_view.EdgeProbabilities.ready()
        assert op_view.EdgeProbabilitiesDict.ready()
        assert op_view.NaiveSegmentation.ready()

        edge_prob_dict = op_view.EdgeProbabilitiesDict.value

        # OFF
        assert edge_prob_dict[edge_A] < 0.5, "Expected < 0.5, got {}".format(
            edge_prob_dict[edge_A])
        assert edge_prob_dict[edge_B] < 0.5, "Expected < 0.5, got {}".format(
            edge_prob_dict[edge_B])

        # ON
        assert edge_prob_dict[edge_C] > 0.5, "Expected > 0.5, got {}".format(
            edge_prob_dict[edge_C])
        assert edge_prob_dict[edge_D] > 0.5, "Expected > 0.5, got {}".format(
            edge_prob_dict[edge_D])
示例#2
0
    def testBasic(self):
        superpixels = generate_random_voronoi( (100,100,100), 100 )
        superpixels = superpixels.insertChannelAxis()
        
        voxel_data = superpixels.astype(np.float32)
                
        graph = Graph()
        multilane_op = OpEdgeTraining(graph=graph)
        multilane_op.VoxelData.resize(1) # resizes all level-1 slots.
        op_view = multilane_op.getLane(0)
        
        op_view.VoxelData.setValue( voxel_data, extra_meta={'channel_names': ['Grayscale']} )
        op_view.Superpixels.setValue( superpixels )
        
        multilane_op.FeatureNames.setValue( { "Grayscale": ['standard_edge_mean', 'standard_edge_count'] } )
        
        assert op_view.Rag.ready()

        # Pick some edges to label
        rag = op_view.Rag.value
        edge_A = tuple(rag.edge_ids[0])
        edge_B = tuple(rag.edge_ids[1])
        edge_C = tuple(rag.edge_ids[2])
        edge_D = tuple(rag.edge_ids[3])

        labels = { edge_A : 1, # OFF
                   edge_B : 1,
                   edge_C : 2, # ON
                   edge_D : 2 }

        op_view.EdgeLabelsDict.setValue( labels )
        op_view.FreezeClassifier.setValue(False)
        
        assert op_view.EdgeProbabilities.ready()
        assert op_view.EdgeProbabilitiesDict.ready()
        assert op_view.NaiveSegmentation.ready()

        edge_prob_dict = op_view.EdgeProbabilitiesDict.value
        
        # OFF
        assert edge_prob_dict[edge_A] < 0.5, "Expected < 0.5, got {}".format(edge_prob_dict[edge_A])
        assert edge_prob_dict[edge_B] < 0.5, "Expected < 0.5, got {}".format(edge_prob_dict[edge_B])
        
        # ON
        assert edge_prob_dict[edge_C] > 0.5, "Expected > 0.5, got {}".format(edge_prob_dict[edge_C])
        assert edge_prob_dict[edge_D] > 0.5, "Expected > 0.5, got {}".format(edge_prob_dict[edge_D])
def test_serializer_roundtrip(graph, inputs, empty_project_file, superpixels,
                              rag, content_lane, empty_lane):
    op = OpEdgeTraining(graph=graph)
    op.VoxelData.connect(inputs.VoxelData)
    op.Superpixels.connect(inputs.Superpixels)
    op.WatershedSelectedInput.connect(inputs.WatershedSelectedInput)

    # set level0 slots
    op.TrainRandomForest.setValue(False)
    op.FeatureNames.setValue({"test0": [b"test_feature_0", b"test_feature_1"]})

    # set level1 slots
    laneop = op.getLane(content_lane)
    edge_features_reference = pandas.DataFrame({
        "col1": [1, 2],
        "col2": [3, 4]
    })
    laneop.opEdgeFeaturesCache.forceValue(edge_features_reference,
                                          set_dirty=False)

    edge_labels_reference = {(0, 1): 0, (0, 2): 1, (1, 2): 0}
    laneop.EdgeLabelsDict.setValue(edge_labels_reference)

    laneop.opRagCache.forceValue(rag, set_dirty=False)

    serializer = EdgeTrainingSerializer(op, "EdgeTraining")
    serializer.serializeToHdf5(empty_project_file, empty_project_file.name)

    # round trip
    op_load = OpEdgeTraining(graph=graph)

    op_load.VoxelData.connect(inputs.VoxelData)
    op_load.Superpixels.connect(inputs.Superpixels)
    op_load.WatershedSelectedInput.connect(inputs.WatershedSelectedInput)

    deserializer = EdgeTrainingSerializer(op_load, "EdgeTraining")
    deserializer.deserializeFromHdf5(empty_project_file,
                                     empty_project_file.name)

    # check level0 slots
    assert not op_load.TrainRandomForest.value
    assert all(a == b for a, b in zip(op_load.FeatureNames.value["test0"],
                                      [b"test_feature_0", b"test_feature_1"]))

    # check level1 slots
    # empty lane
    op_load_empty_lane = op_load.getLane(empty_lane)
    assert op_load_empty_lane.opRagCache._value is None
    assert op_load_empty_lane.EdgeLabelsDict.value == {}
    assert op_load_empty_lane.opEdgeFeaturesCache._value is None

    # lane w content
    op_load_content_lane = op_load.getLane(content_lane)
    edge_features_loaded = op_load_content_lane.opEdgeFeaturesCache.Output.value
    assert edge_features_loaded.equals(edge_features_reference)

    assert op_load_content_lane.EdgeLabelsDict.value == edge_labels_reference

    assert op_load_content_lane.opRagCache._value is not None
def test_serializer_01_02(graph, inputs, empty_project_file,
                          serializer_version, train_rf,
                          expect_cached_features):
    op = OpEdgeTraining(graph=graph)
    op.VoxelData.connect(inputs.VoxelData)
    op.Superpixels.connect(inputs.Superpixels)
    op.WatershedSelectedInput.connect(inputs.WatershedSelectedInput)
    op.TrainRandomForest.setValue(train_rf)

    laneop = op.getLane(0)
    edge_features_reference = pandas.DataFrame({
        "col1": [1, 2],
        "col2": [3, 4]
    })
    laneop.opEdgeFeaturesCache.forceValue(edge_features_reference,
                                          set_dirty=False)

    serializer = EdgeTrainingSerializer(op, "EdgeTraining")
    serializer.version = serializer_version
    serializer.serializeToHdf5(empty_project_file, empty_project_file.name)

    op_load = OpEdgeTraining(graph=graph)
    op_load.VoxelData.connect(inputs.VoxelData)
    op_load.Superpixels.connect(inputs.Superpixels)
    op_load.WatershedSelectedInput.connect(inputs.WatershedSelectedInput)
    deserializer = EdgeTrainingSerializer(op_load, "EdgeTraining")
    deserializer.deserializeFromHdf5(empty_project_file,
                                     empty_project_file.name)

    op_load_lane0 = op_load.getLane(0)
    assert (op_load_lane0.opEdgeFeaturesCache._value
            is not None) == expect_cached_features