我正在尝试运行 kosmos 2 模型进行图像识别和描述。我正在尝试执行 Huggingface 中提供的代码来执行它,但出现错误:
name 'Kosmos2Tokenizer' is not defined
它似乎与 Kosmos2Tokenizer 类有关。出现此错误的原因是 AutoProcessor 尝试实例化模型
ydshieh/kosmos-2-patch14-224
的分词器,但找不到所需的分词器类。我尝试执行拥抱脸中提供的代码(https://huggingface.co/ydshieh/kosmos-2-patch14-224)。
代码:
import requests
from PIL import Image
from transformers import AutoProcessor, AutoModelForVision2Seq
# Load model and processor
model = AutoModelForVision2Seq.from_pretrained("ydshieh/kosmos-2-patch14-224", trust_remote_code=True)
processor = AutoProcessor.from_pretrained("ydshieh/kosmos-2-patch14-224", trust_remote_code=True)
结果:
NameError Traceback (most recent call last)
<ipython-input-6-728393258997> in <cell line: 8>()
6
7 model = AutoModelForVision2Seq.from_pretrained("ydshieh/kosmos-2-patch14-224", trust_remote_code=True)
----> 8 processor = AutoProcessor.from_pretrained("ydshieh/kosmos-2-patch14-224", trust_remote_code=True)
9
10 prompt = "<grounding>An image of"
14 frames
~/.cache/huggingface/modules/transformers_modules/ydshieh/kosmos-2-patch14-224/ada61181d314e7e7c5ff5656322e03a3f1912ba8/tokenization_kosmos2_fast.py in Kosmos2TokenizerFast()
104 max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
105 model_input_names = ["input_ids", "attention_mask"]
--> 106 slow_tokenizer_class = Kosmos2Tokenizer
107
108 def __init__(
NameError: name 'Kosmos2Tokenizer' is not defined
为了让它运行,您需要按照 kosmos 官方存储库中提到的设置步骤:
从存储库中,步骤如下:
- 克隆存储库:
bash git clone https://github.com/microsoft/unilm.git cd unilm/kosmos-2
- 创建 conda env 并安装软件包:
bash conda create -n kosmos-2 python=3.9 conda activate kosmos-2 pip3 install -r requirements.txt
- 安装NVIDIA/apex
# if pip >= 23.1 (ref: https://pip.pypa.io/en/stable/news/#v23-1) which supports multiple `--config-settings` with the same key... pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" ./ # otherwise pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --global-option="--cpp_ext" --global-option="--cuda_ext" ./ ``` 4. Install the packages: ```bash bash vl_setup_xl.sh ```
我正在尝试在 colab 中运行代码,但不能。我正在使用以下完整代码,但等待很长时间(使用 CPU 和 als GPU)后它不起作用。我的想法是用图像检查模型。 这是完整的管道。
克隆存储库
!git clone https://github.com/microsoft/unilm.git
%cd unilm/kosmos-2
安装所需的软件包
!pip install -r requirements.txt
安装NVIDIA/apex
!git clone https://github.com/NVIDIA/apex
%cd apex
安装顶点
!pip install -v --disable-pip-version-check --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./
移回原目录
%cd ..
设置套餐(如果适用)
!bash vl_setup_xl.sh