# 安装Pytorch
!pip3 install install torch==1.10.1+cu113 torchvision==0.11.2+cu113 torchaudio==0.10.1+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
!pip3 install openmim
!mim install 'mmcv>=2.0.0'
!mim install 'mmengine'
!mim install 'mmagic'
!git clone https://github.com/open-mmlab/mmagic.git
import os
os.chdir('mmagic')
!pip3 install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install opencv-python pillow matplotlib seaborn tqdm -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install clip transformers gradio 'httpx[socks]' diffusers==0.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
!mim install 'mmdet>=3.0.0'
from mmagic.apis import MMagicInferencer
sd_inferencer = MMagicInferencer(model_name='stable_diffusion')
text_prompts = 'A panda is having dinner at KFC'
text_prompts = 'A Persian cat walking in the streets of New York'
sd_inferencer.infer(text=text_prompts, result_out_dir='output/sd_res.png')
``python import cv2 import numpy as np import mmcv from mmengine import Config from PIL import Image
from mmagic.registry import MODELS from mmagic.utils import register_all_modules
register_all_modules()
cfg = Config.fromfile('../../configs/controlnet/controlnet-canny.py') controlnet = MODELS.build(cfg.model).cuda()
control_img = mmcv.imread('edges.jpg') control = cv2.Canny(control_img, 100, 200) control = control[:, :, None] control = np.concatenate([control] * 3, axis=2) control = Image.fromarray(control)
prompt = 'Room with light light silver walls and a pale gold ceiling.'
output_dict = controlnet.infer(prompt, control=control) samples = output_dict['samples'] for idx, sample in enumerate(samples): sample.save(f'sample_{idx}.png') controls = output_dict['controls'] for idx, control in enumerate(controls): control.save(f'control_{idx}.png')