English
segment anything

Error "name 'clip' is not defined" when using CLIP with FastSam

#1
by RobbieTheRobot - opened

I am following some tutorials and have the following code in Google Colab:

!pip install matplotlib==3.7.3
!git clone https://github.com/CASIA-IVA-Lab/FastSAM.git
!pip -q install -r FastSAM/requirements.txt

install CLIP ----------

#!pip -q install git+https://github.com/openai/CLIP.git
!pip -q install openai-clip

!wget -P {HOME}/FastSAM/weights -q https://huggingface.co/spaces/An-
619/FastSAM/resolve/main/weights/FastSAM.pt

FAST_SAM_CHECKPOINT_PATH = f"{HOME}/FastSAM/weights/FastSAM.pt"
DOGImage_PATH = f"{HOME}/FastSAM/images/dogs.jpg"

%cd {HOME}/FastSAM
import os
import cv2
import torch
import numpy as np
import matplotlib
import clip
from fastsam import FastSAM, FastSAMPrompt

model = FastSAM(FAST_SAM_CHECKPOINT_PATH)
everything_results = model(DOGImage_PATH, device=DEVICE, retina_masks=True, imgsz=1024,
conf=0.4, iou=0.9,)
prompt_process = FastSAMPrompt(DOGImage_PATH, everything_results, device=DEVICE)

ann = prompt_process.everything_prompt()
prompt_process.plot(annotations=ann,output_path='./output/dogSegmented.jpg',)

ann = prompt_process.text_prompt(text='a photo of a dog')
prompt_process.plot(annotations=ann,output_path='./output/dogPromptPhoto.jpg',)
everything_prompt works fine, and I see the generated image in the folder. However, the text_prompt causes errors

NameError Traceback (most recent call last)
in <cell line: 0>()
1 # text prompt
----> 2 ann = prompt_process.text_prompt(text='a photo of a dog')
3 prompt_process.plot(annotations=ann,output_path='./output/dogPromptPhoto.jpg',)

/content/drive/MyDrive/Vision/FastSAM/fastsam/prompt.py in text_prompt(self, text)
443 format_results = self._format_results(self.results[0], 0)
444 cropped_boxes, cropped_images, not_crop, filter_id, annotations =
self._crop_image(format_results)
--> 445 clip_model, preprocess = clip.load('ViT-B/32', device=self.device)
446 scores = self.retrieve(clip_model, preprocess, cropped_boxes, text,
device=self.device)
447 max_idx = scores.argsort()

NameError: name 'clip' is not defined
Initially I used !pip -q install git+https://github.com/openai/CLIP.git and then changed it to !pip -q install openai-clip but the issue continues. I check !python -c "import clip; print('CLIP is installed!')"

and the result is fine. Any help is extremely appreciated.

Sign up or log in to comment