1 . 克隆代码https://github.com/open-mmlab/mmagic.git
git clone https://github.com/open-mmlab/mmagic.git
2. 创建环境和激活环境
conda create --name torch2.0.1 python=3.10.12 -y
conda activate torch2.0.1
3. 安装所需要的包
pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/rocm5.4.2
pip3 install openmim
mim install 'mmcv==2.0.0'
mim install 'mmengine'
mim install 'mmagic'
pip install -r requirements.txt
requirements.txt文件含有以下包,不管有没有用,先一键安装:
albumentations
-e git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1#egg=clip
imageio-ffmpeg==0.4.4
mmdet >= 3.0.0
open_clip_torch
PyQt5
albumentations
-e git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1#egg=clip
controlnet_aux
# codecov
# flake8
# isort==5.10.1
# onnxruntime
# pytest
# pytest-runner
# yapf
coverage < 7.0.0
imageio-ffmpeg==0.4.4
interrogate
mmdet >= 3.0.0
pytest
transformers>=4.27.4
av
av==8.0.3; python_version < '3.7'
click # required by mmagic/utils/io_utils.py
controlnet_aux
diffusers>=0.23.0
einops
face-alignment<=1.3.4
facexlib
lmdb
lpips
mediapipe
numpy
# MMCV depends opencv-python instead of headless, thus we install opencv-python
# Due to a bug from upstream, we skip this two version
# https://github.com/opencv/opencv-python/issues/602
# https://github.com/opencv/opencv/issues/21366
# It seems to be fixed in https://github.com/opencv/opencv/pull/21382
opencv-python!=4.5.5.62,!=4.5.5.64
pandas # required by mmagic/models/editors/disco_diffusion/guider.py
Pillow
resize_right
tensorboard
transformers>=4.27.4
4. 完成以上步骤后进入python交互窗口,检查环境是否安装成功?然后开始使用代码
成功安装 MMagic 后,你可以很容易地上手使用 MMagic!仅需几行代码,你就可以使用 MMagic 完成文本生成图像!
from mmagic.apis import MMagicInferencer
sd_inferencer = MMagicInferencer(model_name='stable_diffusion')
text_prompts = 'A panda is having dinner at KFC'
result_out_dir = 'output/sd_res.png'
sd_inferencer.infer(text=text_prompts, result_out_dir=result_out_dir)
提示:在进入python交互窗口时,依次输入以上指令,会在前两个指令出现很多的报错,以下是解决报错方案:
5. 解决报错
>>> from mmagic.apis import MMagicInferencer
ModuleNotFoundError: No module named 'platformdirs'
pip install platformdirs
>>> from mmagic.apis import MMagicInferencer
ModuleNotFoundError: No module named 'termcolor'
pip install termcolor
>>> from mmagic.apis import MMagicInferencer
AttributeError: module 'mmcv' has no attribute '__version__'
pip install termcolor
>>> from mmagic.apis import MMagicInferencer
ModuleNotFoundError: No module named 'termcolor'
>>> from mmagic.apis import MMagicInferencer
>ModuleNotFoundError: No module named 'mmagic.apis'
>>> from mmagic.apis import MMagicInferencer
No module named 'diffusers.pipelines.controlnet_xs'
pip install diffusers==0.24.0
重新导入模块,没有报错
>>> from mmagic.apis import MMagicInferencer
>>>
继续往下添加指令: