我第一次尝试安装CLIP,采用的方法是pip的方法:
pip install clip
但是安装后,无法使用:
明确一下CLIP如何正确安装:
要安装CLIP,您可以按照以下步骤进行操作:
import torch
import clip
print("CLIP version:", clip.__version__)
如果您没有看到任何错误消息并且能够成功打印出CLIP的版本号,则表示CLIP已成功安装在您的环境中。
但是这样我的还不能成功!
我找到了CLIP的官方网站,在Github上:
https://github.com/openai/CLIP
首先,安装 PyTorch 1.7.1(或更高版本)和 torchvision,以及小的附加依赖项,然后将此存储库安装为 Python 包。 在 CUDA GPU 机器上,以下操作即可解决问题:
conda install --yes -c pytorch pytorch=1.7.1 torchvision cudatoolkit=11.0
pip install ftfy regex tqdm
pip install git+https://github.com/openai/CLIP.git
但是我们的安装出现了错误:
由于网络问题,我尝试安装CLIP时出现了错误。请尝试以下解决方案:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple git+https://github.com/openai/CLIP.git
上述命令将使用清华大学的PyPI镜像源来安装CLIP。
在终端中,导航到您想要将CLIP存储的目录。
执行以下命令克隆CLIP仓库:
git clone https://github.com/openai/CLIP.git
完成克隆后,执行以下命令进入CLIP目录:
cd CLIP
然后,执行以下命令进行安装:
pip install -e .
这将使用pip命令以编辑模式安装CLIP。
(wzk_base) wangzhenkuan@pc-System-Product-Name:~/CLIP-main$ python setup.py install
running install
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/setuptools/command/easy_install.py:144: EasyInstallDeprecationWarning: easy_install command is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running bdist_egg
running egg_info
creating clip.egg-info
writing clip.egg-info/PKG-INFO
writing dependency_links to clip.egg-info/dependency_links.txt
writing requirements to clip.egg-info/requires.txt
writing top-level names to clip.egg-info/top_level.txt
writing manifest file 'clip.egg-info/SOURCES.txt'
file clip.py (for module clip) not found
reading manifest file 'clip.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
adding license file 'LICENSE'
writing manifest file 'clip.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
file clip.py (for module clip) not found
creating build
creating build/lib
creating build/lib/clip
copying clip/clip.py -> build/lib/clip
copying clip/__init__.py -> build/lib/clip
copying clip/model.py -> build/lib/clip
copying clip/simple_tokenizer.py -> build/lib/clip
copying clip/bpe_simple_vocab_16e6.txt.gz -> build/lib/clip
file clip.py (for module clip) not found
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/clip
copying build/lib/clip/clip.py -> build/bdist.linux-x86_64/egg/clip
copying build/lib/clip/__init__.py -> build/bdist.linux-x86_64/egg/clip
copying build/lib/clip/model.py -> build/bdist.linux-x86_64/egg/clip
copying build/lib/clip/simple_tokenizer.py -> build/bdist.linux-x86_64/egg/clip
copying build/lib/clip/bpe_simple_vocab_16e6.txt.gz -> build/bdist.linux-x86_64/egg/clip
byte-compiling build/bdist.linux-x86_64/egg/clip/clip.py to clip.cpython-310.pyc
byte-compiling build/bdist.linux-x86_64/egg/clip/__init__.py to __init__.cpython-310.pyc
byte-compiling build/bdist.linux-x86_64/egg/clip/model.py to model.cpython-310.pyc
byte-compiling build/bdist.linux-x86_64/egg/clip/simple_tokenizer.py to simple_tokenizer.cpython-310.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying clip.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying clip.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying clip.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying clip.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying clip.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
clip.__pycache__.simple_tokenizer.cpython-310: module references __file__
creating dist
creating 'dist/clip-1.0-py3.10.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing clip-1.0-py3.10.egg
creating /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/clip-1.0-py3.10.egg
Extracting clip-1.0-py3.10.egg to /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Adding clip 1.0 to easy-install.pth file
Installed /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/clip-1.0-py3.10.egg
Processing dependencies for clip==1.0
Searching for regex
Reading https://pypi.org/simple/regex/
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-02-16 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-02-23 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-03-11 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-05-21 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-06-05 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-06-26 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-08-04 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-10-04 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-10-12 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-10-21 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-10-22 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-10-23 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-10-24 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-10-25 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-10-26 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-11-29 is an invalid version and will not be supported in a future release
warnings.warn(
/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 2013-12-31 is an invalid version and will not be supported in a future release
warnings.warn(
Downloading https://files.pythonhosted.org/packages/f4/59/de8f00338fa853b1616e3ea3a335565dea69057fd68c8d159055a90a563f/regex-2023.6.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl#sha256=2239d95d8e243658b8dbb36b12bd10c33ad6e6933a54d36ff053713f129aa536
Best match: regex 2023.6.3
Processing regex-2023.6.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Installing regex-2023.6.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl to /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Adding regex 2023.6.3 to easy-install.pth file
Installed /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/regex-2023.6.3-py3.10-linux-x86_64.egg
Searching for ftfy
Reading https://pypi.org/simple/ftfy/
Downloading https://files.pythonhosted.org/packages/e1/1e/bf736f9576a8979752b826b75cbd83663ff86634ea3055a766e2d8ad3ee5/ftfy-6.1.1-py3-none-any.whl#sha256=0ffd33fce16b54cccaec78d6ec73d95ad370e5df5a25255c8966a6147bd667ca
Best match: ftfy 6.1.1
Processing ftfy-6.1.1-py3-none-any.whl
Installing ftfy-6.1.1-py3-none-any.whl to /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Adding ftfy 6.1.1 to easy-install.pth file
Installing ftfy script to /home/miniconda3/envs/wzk_base/bin
Installed /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/ftfy-6.1.1-py3.10.egg
Searching for certifi>=2017.4.17
Reading https://pypi.org/simple/certifi/
Downloading https://files.pythonhosted.org/packages/9d/19/59961b522e6757f0c9097e4493fa906031b95b3ebe9360b2c3083561a6b4/certifi-2023.5.7-py3-none-any.whl#sha256=c6c2e98f5c7869efca1f8916fed228dd91539f9f1b444c314c06eef02980c716
Best match: certifi 2023.5.7
Processing certifi-2023.5.7-py3-none-any.whl
Installing certifi-2023.5.7-py3-none-any.whl to /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Adding certifi 2023.5.7 to easy-install.pth file
Installed /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/certifi-2023.5.7-py3.10.egg
Searching for wheel
Reading https://pypi.org/simple/wheel/
Downloading https://files.pythonhosted.org/packages/61/86/cc8d1ff2ca31a312a25a708c891cf9facbad4eae493b3872638db6785eb5/wheel-0.40.0-py3-none-any.whl#sha256=d236b20e7cb522daf2390fa84c55eea81c5c30190f90f29ae2ca1ad8355bf247
Best match: wheel 0.40.0
Processing wheel-0.40.0-py3-none-any.whl
Installing wheel-0.40.0-py3-none-any.whl to /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Adding wheel 0.40.0 to easy-install.pth file
Installing wheel script to /home/miniconda3/envs/wzk_base/bin
Installed /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/wheel-0.40.0-py3.10.egg
Searching for torchvision==0.15.1
Best match: torchvision 0.15.1
Adding torchvision 0.15.1 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for torch==2.0.0
Best match: torch 2.0.0
Adding torch 2.0.0 to easy-install.pth file
Installing convert-caffe2-to-onnx script to /home/miniconda3/envs/wzk_base/bin
Installing convert-onnx-to-caffe2 script to /home/miniconda3/envs/wzk_base/bin
Installing torchrun script to /home/miniconda3/envs/wzk_base/bin
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for tqdm==4.64.1
Best match: tqdm 4.64.1
Adding tqdm 4.64.1 to easy-install.pth file
Installing tqdm script to /home/miniconda3/envs/wzk_base/bin
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for Pillow==9.5.0
Best match: Pillow 9.5.0
Adding Pillow 9.5.0 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for requests==2.28.1
Best match: requests 2.28.1
Adding requests 2.28.1 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for numpy==1.24.2
Best match: numpy 1.24.2
Adding numpy 1.24.2 to easy-install.pth file
Installing f2py script to /home/miniconda3/envs/wzk_base/bin
Installing f2py3 script to /home/miniconda3/envs/wzk_base/bin
Installing f2py3.10 script to /home/miniconda3/envs/wzk_base/bin
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for triton==2.0.0
Best match: triton 2.0.0
Adding triton 2.0.0 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-nvtx-cu11==11.7.91
Best match: nvidia-nvtx-cu11 11.7.91
Adding nvidia-nvtx-cu11 11.7.91 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-nccl-cu11==2.14.3
Best match: nvidia-nccl-cu11 2.14.3
Adding nvidia-nccl-cu11 2.14.3 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-cusparse-cu11==11.7.4.91
Best match: nvidia-cusparse-cu11 11.7.4.91
Adding nvidia-cusparse-cu11 11.7.4.91 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-cusolver-cu11==11.4.0.1
Best match: nvidia-cusolver-cu11 11.4.0.1
Adding nvidia-cusolver-cu11 11.4.0.1 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-curand-cu11==10.2.10.91
Best match: nvidia-curand-cu11 10.2.10.91
Adding nvidia-curand-cu11 10.2.10.91 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-cufft-cu11==10.9.0.58
Best match: nvidia-cufft-cu11 10.9.0.58
Adding nvidia-cufft-cu11 10.9.0.58 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-cublas-cu11==11.10.3.66
Best match: nvidia-cublas-cu11 11.10.3.66
Adding nvidia-cublas-cu11 11.10.3.66 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-cudnn-cu11==8.5.0.96
Best match: nvidia-cudnn-cu11 8.5.0.96
Adding nvidia-cudnn-cu11 8.5.0.96 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-cuda-cupti-cu11==11.7.101
Best match: nvidia-cuda-cupti-cu11 11.7.101
Adding nvidia-cuda-cupti-cu11 11.7.101 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-cuda-runtime-cu11==11.7.99
Best match: nvidia-cuda-runtime-cu11 11.7.99
Adding nvidia-cuda-runtime-cu11 11.7.99 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for nvidia-cuda-nvrtc-cu11==11.7.99
Best match: nvidia-cuda-nvrtc-cu11 11.7.99
Adding nvidia-cuda-nvrtc-cu11 11.7.99 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for Jinja2==3.1.2
Best match: Jinja2 3.1.2
Adding Jinja2 3.1.2 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for networkx==3.1
Best match: networkx 3.1
Adding networkx 3.1 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for sympy==1.11.1
Best match: sympy 1.11.1
Adding sympy 1.11.1 to easy-install.pth file
Installing isympy script to /home/miniconda3/envs/wzk_base/bin
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for typing-extensions==4.5.0
Best match: typing-extensions 4.5.0
Adding typing-extensions 4.5.0 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for filelock==3.11.0
Best match: filelock 3.11.0
Adding filelock 3.11.0 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for wcwidth==0.2.6
Best match: wcwidth 0.2.6
Adding wcwidth 0.2.6 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for urllib3==1.26.14
Best match: urllib3 1.26.14
Adding urllib3 1.26.14 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for idna==3.4
Best match: idna 3.4
Adding idna 3.4 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for charset-normalizer==2.0.4
Best match: charset-normalizer 2.0.4
Adding charset-normalizer 2.0.4 to easy-install.pth file
Installing normalizer script to /home/miniconda3/envs/wzk_base/bin
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for lit==16.0.1
Best match: lit 16.0.1
Adding lit 16.0.1 to easy-install.pth file
Installing lit script to /home/miniconda3/envs/wzk_base/bin
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for cmake==3.26.3
Best match: cmake 3.26.3
Adding cmake 3.26.3 to easy-install.pth file
Installing cmake script to /home/miniconda3/envs/wzk_base/bin
Installing cpack script to /home/miniconda3/envs/wzk_base/bin
Installing ctest script to /home/miniconda3/envs/wzk_base/bin
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for setuptools==65.6.3
Best match: setuptools 65.6.3
Adding setuptools 65.6.3 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for MarkupSafe==2.1.2
Best match: MarkupSafe 2.1.2
Adding MarkupSafe 2.1.2 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Searching for mpmath==1.3.0
Best match: mpmath 1.3.0
Adding mpmath 1.3.0 to easy-install.pth file
Using /home/miniconda3/envs/wzk_base/lib/python3.10/site-packages
Finished processing dependencies for clip==1.0
要运行setup.py文件,通常是为了安装Python包或进行包的相关操作。以下是运行setup.py的基本步骤:
python setup.py <命令>
其中,<命令>是您要执行的具体命令,可以是以下常见的命令之一:
您可以根据需要选择适当的命令,并将其替换为<命令>部分。
请注意,具体的操作和可用的命令可能会因您使用的包和项目而有所不同。通常,包的作者会在其文档中提供详细的使用说明和指南,建议参阅相应包的文档以获取更准确的setup.py运行说明。
如果您不确定要运行的具体命令,可以尝试运行python setup.py --help命令,它将显示关于setup.py支持的命令和选项的帮助信息。
当我们使用CLIP库时,可以通过以下步骤来加载预训练的CLIP模型并对图像进行编码:
import torch
import clip
from PIL import Image
我们导入了torch、clip和PIL模块,分别用于处理张量操作、加载CLIP模型和处理图像。
device = "cuda" if torch.cuda.is_available() else "cpu"
我们检查是否有可用的GPU,并根据情况将设备设置为cuda或cpu。
model, preprocess = clip.load("ViT-B/32", device=device)
使用clip.load函数加载预训练的CLIP模型。在这个例子中,我们加载了一个名为"ViT-B/32"的模型,您也可以根据需要选择其他模型。
image_path = 'dog.jpg'
image = Image.open(image_path).convert('RGB')
image_input = preprocess(image).unsqueeze(0).to(device)
通过使用Image模块加载图像文件,并将其转换为RGB格式。然后,我们使用预定义的preprocess函数对图像进行预处理,以获得模型所需的输入格式。最后,我们将图像张量移动到之前定义的设备上。
with torch.no_grad():
image_features = model.encode_image(image_input)
在一个没有梯度计算的上下文中,使用model.encode_image函数对图像进行编码。该函数将图像输入CLIP模型,并返回图像的特征向量表示。
print("Image features shape:", image_features.shape)
最后,我们打印出图像特征的形状,以便查看编码后的图像向量表示。
请确保已经安装了CLIP库并具备所需的依赖项。此外,确保指定的图像文件存在,并根据需要调整文件路径。
这是使用CLIP库加载预训练模型和对图像进行编码的基本过程。您可以根据具体的应用场景进行调整和扩展。