CASIA-IVA-Lab/FastSAM

Model returns None when used in Google Colab

novenix opened this issue · 0 comments

I'm encountering an issue where the FastSAM model returns None when I run it in Google Colab, while it works correctly in my local environment. I've provided the relevant code snippets and observations below.

Steps to reproduce:

  1. Import necessary libraries:

    import torch
    from fast_sam import FastSAM
  2. Load the model:

    model = FastSAM('/content/FastSAM-s.pt')  
  3. Set device: (IN ANOTHER CELL FROM GOOGLE COLAB)

       DEVICE = torch.device(
       "cuda"
       if torch.cuda.is_available()
       else "mps"
       if torch.backends.mps.is_available()
       else "cpu"
      )
  4. Define a function to process frames: (IN ANOTHER CELL FROM GOOGLE COLAB)

   
   def segment_images(frame):
       DEVICE = globals()['DEVICE']
       print(DEVICE,"DEVICE") #<-prints something (cuda )
       model = globals()['model']
       print(model,"model") #<-prints something (<fastsam.model.FastSAM object at 0x7bdc58433880>)
       everything_results = model(
           source=frame,
           device=DEVICE,
           retina_masks=True,
           imgsz=1024,
           conf=0.4,
           iou=0.9,
       )
       print(everything_results)  # This prints `None`
       print(len(everything_results))  # TpeError: object of type 'NoneType' has no len()
  1. Call the function with a sample frame:

    program says

   cuda DEVICE

<fastsam.model.FastSAM object at 0x7bdc58433880> model
None
INFO:     185.216.73.95:0 - "POST /FastSamVideoProcess HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/uvicorn/protocols/http/h11_impl.py", line 408, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/usr/local/lib/python3.10/dist-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/dist-packages/fastapi/applications.py", line 1106, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.10/dist-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.10/dist-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/usr/local/lib/python3.10/dist-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.10/dist-packages/starlette/middleware/cors.py", line 83, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/dist-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/usr/local/lib/python3.10/dist-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.10/dist-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
    raise e
  File "/usr/local/lib/python3.10/dist-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/dist-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.10/dist-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/dist-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/usr/local/lib/python3.10/dist-packages/fastapi/routing.py", line 274, in app
    raw_response = await run_endpoint_function(
  File "/usr/local/lib/python3.10/dist-packages/fastapi/routing.py", line 193, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/usr/local/lib/python3.10/dist-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/usr/local/lib/python3.10/dist-packages/anyio/to_thread.py", line 33, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/usr/local/lib/python3.10/dist-packages/anyio/_backends/_asyncio.py", line 877, in run_sync_in_worker_thread
    return await future
  File "/usr/lib/python3.10/asyncio/futures.py", line 285, in __await__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.10/asyncio/tasks.py", line 304, in __wakeup
    future.result()
  File "/usr/lib/python3.10/asyncio/futures.py", line 201, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "/usr/local/lib/python3.10/dist-packages/anyio/_backends/_asyncio.py", line 807, in run
    result = context.run(func, *args)
  File "<ipython-input-40-b8ae95391ef7>", line 22, in video_process
    frame,img= segment_images(Frame)
  File "<ipython-input-41-95099d41015b>", line 42, in segment_images
    print(len( everything_results))
TypeError: object of type 'NoneType' has no len()

Expected behavior:

The everything_results variable should contain the model's output, as it does in my local environment.

Actual behavior:

The everything_results variable is None in Colab.

Additional information:

  • FastSAM version: TAKEN FROM DEMO
  • Colab environment: T4 GPU
  • Python version: PYTHON 3
  • my google colab
    The google colab code works as a server of fast sam, and i send info from the camera from local, and suppose to return the info segmented by fastsam from the server:
import json
from fastsam import FastSAM, FastSAMPrompt
#import torch
import numpy as np
import cv2
import time
import requests



cap = cv2.VideoCapture(0)

while cap.isOpened():
    
   
    
    start = time.perf_counter()
     ##llama api
    ##recive de api elframe y el img
    url = "https://ngrokurl.ngrok-free.app/FastSamVideoProcess"
    print(url,"url")
    suc, frame = cap.read()
    data = {
        "Frame":frame,
    }
    print("data: ",data)
    response = requests.post(url, data=data)
    print("response: ",response)
    data = response.json()  
    print(data)
    print("antes de frame")
    frame = data["frame"]
    img = data["img"]
    
    end = time.perf_counter()
    total_time = end - start
    fps = 1 / total_time
   
   
    cv2.imshow('frame', frame)
    cv2.imshow('img', img)
    
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    
cv2.destroyAllWindows()
cap.release()

Request:

I'd appreciate any assistance in identifying the cause of this issue and potential solutions to make the model work correctly in Google Colab.

i only need to resolve the NONE issue, it doesnt matter if for the moment is not returning well the google colab, i first need google colab to resolve the segmentation and need change de NONE issue only, thanks