Skip to content

Empty output when using facebook/sam-vit-base with automatic mask generation pipeline #26728

Description

@sunhaozhepy

System Info

  • transformers version: 4.35.0.dev0
  • Platform: Linux-5.4.15-1.el7.elrepo.x86_64-x86_64-with-glibc2.27
  • Python version: 3.11.4
  • Huggingface_hub version: 0.17.3
  • Safetensors version: 0.4.0
  • Accelerate version: 0.23.0
  • Accelerate config: not found
  • PyTorch version (GPU?): 2.1.0+cu121 (True)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?: yes
  • Using distributed or parallel set-up in script?: yes (but I grabbed 1 GPU only to my workspace bash terminal)

Who can help?

@amyeroberts @Narsil

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Following the example notebook of automatic mask generation using SAM model, I used the following code snippet to do image segmentation:

import numpy as np
import matplotlib.pyplot as plt
import gc
from transformers import pipeline
from PIL import Image
import requests

def show_mask(mask, ax, random_color=False):
    if random_color:
        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
    else:
        color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
    h, w = mask.shape[-2:]
    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
    ax.imshow(mask_image)
    del mask
    gc.collect()

def show_masks_on_image(raw_image, masks):
  plt.imshow(np.array(raw_image))
  ax = plt.gca()
  ax.set_autoscale_on(False)
  for mask in masks:
      show_mask(mask, ax=ax, random_color=True)
  plt.axis("off")
  plt.show()
  del mask
  gc.collect()

generator = pipeline("mask-generation", model="facebook/sam-vit-base", device=0)

raw_image = Image.open("test.png")

outputs = generator(raw_image, points_per_batch=64)

masks = outputs["masks"]
show_masks_on_image(raw_image, masks)

The original code is provided in the form of a notebook, which I arrange as a script that can be run in the terminal.

Expected behavior

The output is empty, and no mask is shown on the image. When I insert print(outputs) into the code snippet, the output in the terminal is {'masks': [], 'scores': tensor([])}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions