HumanSignal / label-studio-ml-backend

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

SAM annotation fails with `No connection adapters were found for ` #325

Closed jpodivin closed 1 year ago

jpodivin commented 1 year ago

I've set up a label studio instance with database and SAM ML backend. The model links with the label studio, and is reachable from the studio (Validate and Save passes). I've also set up the labeling with layout from the label studio SAM example . However, when I try to use a key point to label an object I get following error: No connection adapters were found for '172.20.0.4:8080/data/upload/5/b76sa08b.jpg'.

It seems to me that for some reason the model can't reach container with label studio instance. I've tried to set the LABEL_STUDIO_HOST var to other values, like name of the studio container. But with same results. I've also tried to run the SAM backend as a separate service, but that prevented me from even linking the model in label studio.

OS: Ubuntu 22.04 Docker compose version: 1.29.2 Label studio images: latest SAM image: build from master

Trace:

sam_server    | Traceback (most recent call last):
sam_server    |   File "/usr/local/lib/python3.8/site-packages/label_studio_ml/exceptions.py", line 39, in exception_f
sam_server    |     return f(*args, **kwargs)
sam_server    |   File "/usr/local/lib/python3.8/site-packages/label_studio_ml/api.py", line 61, in _predict
sam_server    |     predictions = model.predict(tasks, context=context, **params)
sam_server    |   File "/app/model.py", line 48, in predict
sam_server    |     predictor_results = PREDICTOR.predict(
sam_server    |   File "/app/sam_predictor.py", line 195, in predict
sam_server    |     return self.predict_sam(img_path, point_coords, point_labels, input_box)
sam_server    |   File "/app/sam_predictor.py", line 167, in predict_sam
sam_server    |     self.set_image(img_path, calculate_embeddings=False)
sam_server    |   File "/app/sam_predictor.py", line 83, in set_image
sam_server    |     image_path = get_image_local_path(
sam_server    |   File "/usr/local/lib/python3.8/site-packages/label_studio_ml/utils.py", line 64, in get_image_local_path
sam_server    |     image_local_path = get_local_path(
sam_server    |   File "/usr/local/lib/python3.8/site-packages/label_studio_tools/core/utils/io.py", line 104, in get_local_path
sam_server    |     r = requests.get(url, stream=True, headers=headers)
sam_server    |   File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 73, in get
sam_server    |     return request("get", url, params=params, **kwargs)
sam_server    |   File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 59, in request
sam_server    |     return session.request(method=method, url=url, **kwargs)
sam_server    |   File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 589, in request
sam_server    |     resp = self.send(prep, **send_kwargs)
sam_server    |   File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 697, in send
sam_server    |     adapter = self.get_adapter(url=request.url)
sam_server    |   File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 794, in get_adapter
sam_server    |     raise InvalidSchema(f"No connection adapters were found for {url!r}")
sam_server    | requests.exceptions.InvalidSchema: No connection adapters were found for '172.20.0.4:8080/data/upload/5/b76sa08b.jpg'

Compose File:

version: "3.9"
services:
  app:
    stdin_open: true
    tty: true
    build: .
    image: heartexlabs/label-studio:latest
    restart: unless-stopped
    ports:
      - "8080:8000"
    depends_on:
      - db
    environment:
      - DJANGO_DB=default
      - POSTGRE_NAME=postgres
      - POSTGRE_USER=postgres
      - POSTGRE_PASSWORD=
      - POSTGRE_PORT=5432
      - POSTGRE_HOST=db
      - LABEL_STUDIO_HOST=${LABEL_STUDIO_HOST:-}
      - JSON_LOG=1
      - LOG_LEVEL=DEBUG
    volumes:
      - ./mydata:/label-studio/data:rw
    command: label-studio-uwsgi
    networks:
      - label_studio_network 

  db:
    image: postgres:11.5
    hostname: db
    restart: unless-stopped
    # Optional: Enable TLS on PostgreSQL
    # Just drop your server.crt and server.key into folder 'deploy/pgsql/certs'
    # NOTE: Both files must have permissions u=rw (0600) or less
    #    command: >
    #      -c ssl=on
    #      -c ssl_cert_file=/var/lib/postgresql/certs/server.crt
    #      -c ssl_key_file=/var/lib/postgresql/certs/server.key
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
    volumes:
      - ${POSTGRES_DATA_DIR:-./postgres-data}:/var/lib/postgresql/data
      - ./deploy/pgsql/certs:/var/lib/postgresql/certs:ro
    networks:
      - label_studio_network

  sam_server:
    container_name: sam_server
    image: humansignal/sam:v0
    build:
      context: ./label-studio-ml-backend/label_studio_ml/examples/segment_anything_model 
      shm_size: '4gb'
    deploy:
      resources:
        limits:
            memory: 8G
        reservations:
            memory: 4G
    environment:
     # Change this to your model name
      - SAM_CHOICE=MobileSAM
      - LOG_LEVEL=DEBUG
      # Add these variables if you want to access the images stored in Label Studio
      - LABEL_STUDIO_HOST=127.0.0.1:8080
      - LABEL_STUDIO_ACCESS_TOKEN=2f38a3ec675504af5ce79f34ded5915bd7af4432
    ports:
      - 9090:9090
    volumes:
      - "./data/server:/data"
    networks:
      - label_studio_network

networks:
  label_studio_network:

Network layout:

[
    {
        "Name": "ls_label_studio_network",
        "Id": "ebef96f251c88cd339c6f78b6867383555bc2a0b444621bcf78fe7a8139f4e14",
        "Created": "2023-08-22T07:36:51.795383495+02:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "5b51297c3420fa5c480b36cc2eddaec25070dc6f41671617ee00b142a7eb6703": {
                "Name": "ls_db_1",
                "EndpointID": "f65dcf812b1da96102b5afd384eaa0a881f60a37738702dffcb41afc1f80660c",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            },
            "8bff368cb6a4b2c6d6a19993a3c83cc05ff015a172e24f0b52301883e0a67232": {
                "Name": "sam_server",
                "EndpointID": "f3a41a1fd1969b8dd8eb0840428e2fc62284034b7e1213a687d98b12af07fc89",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            },
            "c3adb62de75011a95b3404cdbbacec4864bce7fc7ede7937f4b5acd1c494876f": {
                "Name": "ls_app_1",
                "EndpointID": "4fb855607be498442819a22020b7cbefba13bb72a7d58e163acf50396920ed1c",
                "MacAddress": "02:42:ac:14:00:04",
                "IPv4Address": "172.20.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "label_studio_network",
            "com.docker.compose.project": "ls",
            "com.docker.compose.version": "1.29.2"
        }
    }
]
jpodivin commented 1 year ago

Eventually I managed to resolve the issue by switching to host network and setting the LABEL_STUDIO_HOSTNAME to http://localhost:8080. Now the model predicts masks as expected.