Example #1
0
def test_model_schema_accepts_run_mode(model_dict):
    from bioimageio.spec.model.schema import Model

    model_schema = Model()
    model_dict.update({"run_mode": {"name": "special_run_mode", "kwargs": dict(marathon=True)}})
    validated_data = model_schema.load(model_dict)
    assert validated_data
Example #2
0
def test_model_schema_accepts_valid_weight_formats(model_dict, format):
    from bioimageio.spec.model.schema import Model

    model_schema = Model()
    model_dict.update({"weights": {format: {"source": "local_weights"}}})
    if format == "pytorch_state_dict":
        model_dict["weights"][format]["architecture"] = "file.py:Model"
        model_dict["weights"][format]["architecture_sha256"] = "0" * 64  # dummy sha256

    validated_data = model_schema.load(model_dict)
    assert validated_data
Example #3
0
def test_model_has_parent_with_id(model_dict):
    from bioimageio.spec.model.schema import Model

    model_dict["parent"] = dict(id="10.5281/zenodo.5764892")

    valid_data = Model().load(model_dict)
    assert valid_data
Example #4
0
def test_model_has_parent_with_uri(model_dict):
    from bioimageio.spec.model.schema import Model

    model_dict["parent"] = dict(uri="https://doi.org/10.5281/zenodo.5744489")

    valid_data = Model().load(model_dict)
    assert valid_data
Example #5
0
def test_model_does_not_accept_unknown_fields(unet2d_nuclei_broad_latest):
    from bioimageio.spec.model.schema import Model

    unet2d_nuclei_broad_latest["unknown_additional_field"] = "shouldn't be here"

    with pytest.raises(ValidationError):
        Model().load(unet2d_nuclei_broad_latest)
Example #6
0
def test_model_0_4_raises_on_duplicate_tensor_names(invalid_rdf_v0_4_0_duplicate_tensor_names):
    from bioimageio.spec.model.schema import Model
    from bioimageio.spec.model.v0_3.schema import Model as Model_v03

    model_schema = Model()
    with pytest.raises(ValidationError):
        model_schema.load(invalid_rdf_v0_4_0_duplicate_tensor_names)

    # as 0.3 the model should still be valid with some small changes
    model_schema = Model_v03()
    data = dict(invalid_rdf_v0_4_0_duplicate_tensor_names)
    data["format_version"] = "0.3.3"
    data["language"] = "python"
    data["framework"] = "pytorch"
    data["source"] = data["weights"]["pytorch_state_dict"].pop("architecture")
    data["kwargs"] = data["weights"]["pytorch_state_dict"].pop("kwargs")
    data["sha256"] = data["weights"]["pytorch_state_dict"].pop("architecture_sha256")

    valid_data = model_schema.load(data)
    assert valid_data
Example #7
0
def test_output_ref_shape_too_small(model_dict):
    from bioimageio.spec.model.schema import Model

    model_dict["outputs"] = [
        {
            "name": "output_1",
            "description": "Output 1",
            "data_type": "float32",
            "axes": "xyc",
            "shape": {"reference_tensor": "input_1", "scale": [1, 2, 3], "offset": [0, 0, 0]},
            "halo": [256, 128, 0],
        }
    ]

    with pytest.raises(ValidationError) as e:
        Model().load(model_dict)

    assert e.value.messages == {
        "_schema": ["Minimal shape [128. 256.   9.] of output output_1 is too small for halo [256, 128, 0]."]
    }
Example #8
0
def test_output_fixed_shape_too_small(model_dict):
    from bioimageio.spec.model.schema import Model

    model_dict["outputs"] = [
        {
            "name": "output_1",
            "description": "Output 1",
            "data_type": "float32",
            "axes": "xyc",
            "shape": [128, 128, 3],
            "halo": [32, 128, 0],
        }
    ]

    with pytest.raises(ValidationError) as e:
        Model().load(model_dict)

    assert e.value.messages == {
        "_schema": ["Minimal shape [128 128   3] of output output_1 is too small for halo [32, 128, 0]."]
    }
Example #9
0
def test_output_ref_shape_mismatch(model_dict):
    from bioimageio.spec.model.schema import Model

    model_dict["outputs"] = [
        {
            "name": "output_1",
            "description": "Output 1",
            "data_type": "float32",
            "axes": "xyc",
            "shape": {"reference_tensor": "input_1", "scale": [1, 2, 3, 4], "offset": [0, 0, 0, 0]},
        }
    ]

    with pytest.raises(ValidationError) as e:
        Model().load(model_dict)

    assert e.value.messages == {
        "_schema": [
            "Referenced tensor input_1 with 3 dimensions does not match output tensor output_1 with 4 dimensions."
        ]
    }