bioimage-io / spec-bioimage-io

Specification for the bioimage.io model description file.
https://bioimage-io.github.io/spec-bioimage-io/
MIT License
18 stars 17 forks source link

codec error on v0_5.Https() using windows #633

Closed thodkatz closed 1 month ago

thodkatz commented 2 months ago

Hey!

from bioimageio.spec import ModelDescr, load_description, save_bioimageio_package_to_stream
from bioimageio.spec.model.v0_5 import Author, Doi, LicenseId, HttpUrl, BatchAxis, ChannelAxis, SpaceInputAxis, Identifier, AxisId, PytorchStateDictWeightsDescr, WeightsDescr, Version, SpaceOutputAxis, ArchitectureFromLibraryDescr, InputTensorDescr, OutputTensorDescr, FileDescr, TensorId, CiteEntry
import tempfile
import numpy as np
from torch import nn
import torch
import xarray as xr
from pathlib import Path
import io

_dummy_network_output = xr.DataArray(np.arange(2*10*10).reshape(1,2,10,10), dims=["batch", "channel", "x", "y"])

class _DummyNetwork(nn.Module):
    def forward(self, *args):
        return torch.from_numpy(_dummy_network_output.values)

input_axes = [
    BatchAxis(),
    ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
    SpaceInputAxis(id=AxisId("x"), size=10),
    SpaceInputAxis(id=AxisId("y"), size=10)
]

input_test_tensor = np.arange(1*2*10*10, dtype="float32").reshape(1,2,10,10)

dummy_model = _DummyNetwork()
expected_output = _dummy_network_output

with tempfile.NamedTemporaryFile(suffix=".pts", delete=False) as weights_file:
    torch.save(dummy_model.state_dict(), weights_file.name)

weights = WeightsDescr(
    pytorch_state_dict=PytorchStateDictWeightsDescr(
        source=Path(weights_file.name),
        architecture=ArchitectureFromLibraryDescr(
            import_from="tests.conftest",
            callable=Identifier(f"{_DummyNetwork.__name__}"),
        ),
        pytorch_version=Version("1.1.1"),
    )
)

with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as input_test_tensor_file:
    np.save(input_test_tensor_file.name, input_test_tensor)

output_test_tensor = np.arange(1*2*10*10, dtype="float32").reshape(1, 2, 10, 10)
output_axes = [
    BatchAxis(),
    ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
    SpaceOutputAxis(id=AxisId("x"), size=10),
    SpaceOutputAxis(id=AxisId("y"), size=10)
]

with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as output_test_tensor_file:
    np.save(output_test_tensor_file.name, output_test_tensor)

input_tensor = InputTensorDescr(
    id=TensorId("input"),
    axes=input_axes,
    description="",
    test_tensor=FileDescr(source=Path(input_test_tensor_file.name))
)

output_tensor = OutputTensorDescr(
    id=TensorId("output"),
    axes=output_axes,
    description="",
    test_tensor=FileDescr(source=Path(output_test_tensor_file.name))
)

model = ModelDescr(
    name="mocked v5 model",
    description="",
    authors=[Author(name="me", affiliation="my institute", github_user="bioimageiobot")],
    cite=[CiteEntry(text="test", doi=Doi("10.1234something"))],
    license=LicenseId("MIT"),
    documentation=HttpUrl("https://raw.githubusercontent.com/bioimage-io/spec-bioimage-io/main/README.md"),
    git_repo=HttpUrl("https://github.com/bioimage-io/spec-bioimage-io"),
    inputs=[input_tensor],
    outputs=[output_tensor],
    weights=weights
)

the above model descriptor definition will give the following validation error:

Exception has occurred: ValidationError
1 validation error for bioimage.io model specification
documentation
  Value error, 'charmap' codec can't decode byte 0x81 in position 5003: character maps to <undefined> [type=value_error, input_value='https://raw.githubuserco...image-io/main/README.md', input_type=HttpUrl]
    For further information visit https://errors.pydantic.dev/2.8/v/value_error
  File "C:\Users\katzalis\repos\spec-bioimage-io\test.py", line 72, in <module>
    model = ModelDescr(
pydantic_core._pydantic_core.ValidationError: 1 validation error for bioimage.io model specification
documentation
  Value error, 'charmap' codec can't decode byte 0x81 in position 5003: character maps to <undefined> [type=value_error, input_value='https://raw.githubuserco...image-io/main/README.md', input_type=HttpUrl]
    For further information visit https://errors.pydantic.dev/2.8/v/value_error

Initially I got this issue when testing tiktorch on a windows platform https://github.com/ilastik/tiktorch/actions/runs/10905374847/job/30264149619. Also I noticed we don't have window ci running, we plan to support all platforms right? It is worth noting that for macos and linux platforms, there isn't such validation error ...