python - Error "name 'clip' is not defined" when using CLIP with FastSam - Stack Overflow

admin2025-04-17  3

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

!pip install matplotlib==3.7.3
!git clone .git
!pip -q install -r FastSAM/requirements.txt

# install CLIP ----------
#!pip -q install git+.git
!pip -q install openai-clip  

!wget -P {HOME}/FastSAM/weights -q 
                                   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)
 <ipython-input-19-01ce47655680> 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+.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.

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)
 <ipython-input-19-01ce47655680> 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.

Share Improve this question edited Jan 31 at 11:20 Christoph Rackwitz 15.9k5 gold badges39 silver badges51 bronze badges asked Jan 31 at 5:29 user84209user84209 134 bronze badges 3
  • Can you also share line 445 from content/drive/MyDrive/Vision/FastSAM/fastsam/prompt.py it looks like clip is not define there not in your current snippet. – Onuralp Arslan Commented Jan 31 at 7:14
  • Was my answer helpful? – dev_light Commented Feb 1 at 23:56
  • @dev_light thank you, but unfortunately it did not resolve the issue – user84209 Commented Feb 6 at 10:13
Add a comment  | 

1 Answer 1

Reset to default 0

You installed openai-clip but from the commands you ran, it seems like what you are looking for is clip-openai. Please install the correct package and refer the GitHub repo which is similar to what you included.

转载请注明原文地址:http://anycun.com/QandA/1744879206a88916.html