HumanSignal / label-studio-ml-backend

Configs and boilerplates for Label Studio's Machine Learning backend
Apache License 2.0
534 stars 240 forks source link

Deploy ML : Error: [mobx-state-tree] No matching type for union (string[] | undefined?) #576

Open YusukeMimura opened 3 months ago

YusukeMimura commented 3 months ago

Hello,

I am currently trying to test an ML backend with a model I created myself. As a practice, I am trying to draw only rectangles in the images. However, when I register the model and open the images in the project, I get the following message on the screen:

Error: [mobx-state-tree] No matching type for union (string[] | undefined?)

I apologize for the inconvenience, but even after trying to resolve it, the issue persists. I found a similar post in a previous issue where it turned out to be a system error, so I am asking for help.

Below, I am attaching the project template and model.py that I am writing as a test.

template

<View>
  <Image name="image" value="$image"/>
  <RectangleLabels name="label" toName="image">

  <Label value="human" background="#FFA39E"/></RectangleLabels>
</View>

model.py

from typing import List, Dict, Optional
from label_studio_ml.model import LabelStudioMLBase
from label_studio_ml.response import ModelResponse
import requests
from PIL import Image
from io import BytesIO
import os

LS_URL = os.environ['LABEL_STUDIO_URL']
LS_API_TOKEN = os.environ['LABEL_STUDIO_API_KEY']

class NewModel(LabelStudioMLBase):

    def __init__(self, **kwargs):
        super(NewModel, self).__init__(**kwargs)
        self.setup()

    def setup(self):

        self.set("model_version", "0.0.1")
        self.base_url = os.getenv('LABEL_STUDIO_BASE_URL', '****')

        from_name, schema = list(self.parsed_label_config.items())[0]
        self.from_name = from_name
        self.to_name = schema['to_name'][0]
        self.labels = ['human']

    def predict(self, tasks: List[Dict], context: Optional[Dict] = None, **kwargs) -> ModelResponse:

        print(f"Run prediction on {tasks}")
        print(f"Received context: {context}")
        print(f"Project ID: {self.project_id}")
        print(f"Label config: {self.label_config}")
        print(f"Parsed JSON Label config: {self.parsed_label_config}")
        print(f"Extra params: {self.extra_params}")

        predictions = []

        for task in tasks:
            image_url = LS_URL + task['data']['image']
            print("image_url:", image_url)
            header = {"Authorization": "Token " + LS_API_TOKEN}
            response = requests.get(image_url, headers=header)
            image = Image.open(BytesIO(response.content))
            img_width, img_height = image.size

            rect_width = 50
            rect_height = 50

            x_center = img_width / 2
            y_center = img_height / 2

            x = x_center - rect_width / 2
            y = y_center - rect_height / 2

            print("str(task['id']:", str(task['id']))
            print("from_name:", self.from_name)
            print("x / img_width * 100:",  x / img_width * 100)
            print("x / img_height * 100:",  y / img_height * 100)
            print("width: rect_width / img_width * 100:", rect_width / img_width * 100)
            print("height: rect_height / img_height * 100:", rect_height / img_height * 100)
            print("[self.labels[0]]:",[self.labels[0]])

            predictions.append({
                "id": "result",
                "from_name": "label",
                "to_name": "image",
                "type": "rectanglelabels",
                "score": 0.90,
                "original_width": img_width,
                "original_height": img_height,
                "image_rotation": 0,
                "value": {
                    "rotation": 0,
                    "x": x / img_width * 100,
                    "y": y / img_height * 100,
                    "width": rect_width / img_width * 100,
                    "height": rect_height / img_height * 100,
                    "rectanglelabels": ["human"] 
                }
            })

        return [{"result": predictions, "score": 0.5, "model_version": "v8"}]
YusukeMimura commented 3 months ago

The Label Studio version is 1.12.0. Label Studio can access the ML Docker container, and I have confirmed that it can retrieve image sizes and other information.