Ejemplo n.º 1
0
    def test_should_build_train_graph_with_sample_class_weights(
            self,
            parse_color_map_from_file_mock,
            parse_json_file_mock,
            read_examples_mock):

        parse_color_map_from_file_mock.return_value = SOME_COLOR_MAP
        parse_json_file_mock.return_value = SOME_CLASS_WEIGHTS
        read_examples_mock.return_value = EXAMPLE_PROPS_1
        args = create_args(
            DEFAULT_ARGS,
            base_loss=BaseLoss.SAMPLE_WEIGHTED_CROSS_ENTROPY,
            color_map=COLOR_MAP_FILENAME,
            channels=SOME_LABELS,
            use_separate_channels=True,
            use_unknown_class=True
        )
        model = Model(args)
        tensors = model.build_train_graph(DATA_PATH, BATCH_SIZE)
        n_output_channels = len(SOME_LABELS) + 1
        assert (
            tensors.separate_channel_annotation_tensor.shape.as_list() ==
            [BATCH_SIZE, model.image_height, model.image_width, n_output_channels]
        )
        assert tensors.pos_weight.shape.as_list(
        ) == [BATCH_SIZE, 1, 1, n_output_channels]
    def test_should_build_train_graph_with_defaults(
            self, parse_color_map_from_file_mock, read_examples_mock):

        parse_color_map_from_file_mock.return_value = SOME_COLOR_MAP
        read_examples_mock.return_value = EXAMPLE_PROPS_1
        args = create_args(DEFAULT_ARGS)
        model = Model(args)
        model.build_train_graph(DATA_PATH, BATCH_SIZE)
 def test_should_build_predict_graph_with_defaults(
         self, parse_color_map_from_file_mock, read_examples_mock):
     parse_color_map_from_file_mock.return_value = SOME_COLOR_MAP
     read_examples_mock.return_value = EXAMPLE_PROPS_1
     args = create_args(DEFAULT_ARGS)
     model = Model(args)
     tensors = model.build_predict_graph()
     n_output_channels = 3
     assert (tensors.pred.shape.as_list() == [
         None, model.image_height, model.image_width, n_output_channels
     ])
Ejemplo n.º 4
0
    def test_should_only_include_labels_with_non_zero_class_labels_by_default(
            self,
            parse_color_map_from_file_mock,
            parse_json_file_mock):

        parse_color_map_from_file_mock.return_value = {
            'a': some_color(1),
            'b': some_color(2),
            'c': some_color(3)
        }
        parse_json_file_mock.return_value = {
            'a': 0.1,
            'b': 0.0,
            'c': 0.3
        }
        args = create_args(
            DEFAULT_ARGS,
            base_loss=BaseLoss.WEIGHTED_CROSS_ENTROPY,
            color_map=COLOR_MAP_FILENAME,
            class_weights=CLASS_WEIGHTS_FILENAME,
            use_separate_channels=True,
            use_unknown_class=True
        )
        model = Model(args)
        assert model.dimension_labels == ['a', 'c']
        assert model.dimension_colors == [some_color(1), some_color(3)]
        assert model.pos_weight == [0.1, 0.3, DEFAULT_UNKNOWN_CLASS_WEIGHT]
Ejemplo n.º 5
0
    def test_parse_separate_channels_with_color_map_and_class_weights(
            self,
            parse_color_map_from_file_mock,
            parse_json_file_mock):

        parse_color_map_from_file_mock.return_value = {
            'a': some_color(1),
            'b': some_color(2),
            'c': some_color(3)
        }
        parse_json_file_mock.return_value = {
            'a': 0.1,
            'b': 0.2,
            'c': 0.3
        }
        args = create_args(
            DEFAULT_ARGS,
            base_loss=BaseLoss.WEIGHTED_CROSS_ENTROPY,
            color_map=COLOR_MAP_FILENAME,
            class_weights=CLASS_WEIGHTS_FILENAME,
            channels=['a', 'b'],
            use_separate_channels=True,
            use_unknown_class=True
        )
        model = Model(args)
        assert model.dimension_colors == [some_color(1), some_color(2)]
        assert model.dimension_labels == ['a', 'b']
        assert (
            model.dimension_colors_with_unknown == [
                some_color(1), some_color(2), UNKNOWN_COLOR]
        )
        assert model.dimension_labels_with_unknown == ['a', 'b', UNKNOWN_LABEL]
        assert model.pos_weight == [0.1, 0.2, DEFAULT_UNKNOWN_CLASS_WEIGHT]
    def test_should_build_train_graph_with_class_weights(
            self, parse_color_map_from_file_mock, parse_json_file_mock,
            read_examples_mock):

        parse_color_map_from_file_mock.return_value = SOME_COLOR_MAP
        parse_json_file_mock.return_value = SOME_CLASS_WEIGHTS
        read_examples_mock.return_value = EXAMPLE_PROPS_1
        args = create_args(DEFAULT_ARGS,
                           base_loss=BaseLoss.WEIGHTED_CROSS_ENTROPY,
                           color_map=COLOR_MAP_FILENAME,
                           class_weights=CLASS_WEIGHTS_FILENAME,
                           channels=['a', 'b'],
                           use_separate_channels=True,
                           use_unknown_class=True)
        model = Model(args)
        tensors = model.build_train_graph(DATA_PATH, BATCH_SIZE)
        assert tensors.pos_weight is not None
    def test_should_not_load_class_weights_for_sample_weighted_cross_entropy(
            self, parse_color_map_from_file_mock):

        parse_color_map_from_file_mock.return_value = SOME_COLOR_MAP
        args = create_args(DEFAULT_ARGS,
                           base_loss=BaseLoss.SAMPLE_WEIGHTED_CROSS_ENTROPY,
                           color_map=COLOR_MAP_FILENAME,
                           class_weights=CLASS_WEIGHTS_FILENAME,
                           use_separate_channels=True,
                           use_unknown_class=True)
        model = Model(args)
        assert model.pos_weight is None
    def test_should_use_unknown_class_weight_from_configuration(
            self, parse_color_map_from_file_mock, parse_json_file_mock):

        parse_color_map_from_file_mock.return_value = SOME_COLOR_MAP
        parse_json_file_mock.return_value = extend_dict(
            SOME_CLASS_WEIGHTS, {'unknown': 0.99})
        args = create_args(DEFAULT_ARGS,
                           base_loss=BaseLoss.WEIGHTED_CROSS_ENTROPY,
                           color_map=COLOR_MAP_FILENAME,
                           class_weights=CLASS_WEIGHTS_FILENAME,
                           use_separate_channels=True,
                           use_unknown_class=True)
        model = Model(args)
        assert model.pos_weight[-1] == 0.99
    def test_parse_separate_channels_with_color_map_without_class_weights(
            self, parse_color_map_from_file_mock):

        parse_color_map_from_file_mock.return_value = {
            'a': some_color(1),
            'b': some_color(2),
            'c': some_color(3)
        }
        args = create_args(DEFAULT_ARGS,
                           color_map=COLOR_MAP_FILENAME,
                           class_weights=None,
                           channels=['a', 'b'],
                           use_separate_channels=True,
                           use_unknown_class=True)
        model = Model(args)
        assert model.dimension_colors == [some_color(1), some_color(2)]
        assert model.dimension_labels == ['a', 'b']
        assert model.dimension_colors_with_unknown == [
            some_color(1), some_color(2), UNKNOWN_COLOR
        ]
        assert model.dimension_labels_with_unknown == ['a', 'b', UNKNOWN_LABEL]
        assert model.pos_weight is None