MNN/docs/transformers/diffusion.md

79 lines
3.0 KiB
Markdown
Raw Normal View History

# 扩散模型
2024-07-04 11:53:45 +08:00
## 模型支持与下载
2024-07-22 19:51:53 +08:00
1. runwayml/stable-diffusion-v1-5
```
2024-07-04 11:53:45 +08:00
https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main
2024-07-22 19:51:53 +08:00
```
2. chilloutmix
```
https://modelscope.cn/models/wyj123456/chilloutmix
```
3. IDEA-CCNL/Taiyi-Stable-Diffusion-1B-Chinese-v0.1
```
2024-07-04 11:53:45 +08:00
https://huggingface.co/IDEA-CCNL/Taiyi-Stable-Diffusion-1B-Chinese-v0.1/tree/main
2024-07-22 19:51:53 +08:00
```
2024-07-04 11:53:45 +08:00
## 模型转换
### 将Huggingface的Stable Diffusion模型 转为onnx模型
2024-07-22 19:51:53 +08:00
```sh
cd mnn_path/transformers/diffusion/
2024-07-04 11:53:45 +08:00
python export/onnx_export.py \
--model_path hf_sd_load_path \
--output_path onnx_save_path
2024-07-22 19:51:53 +08:00
```
上述脚本需要依赖torch/onnx/diffusers等库可以安装conda环境
```
conda env create -f env.yaml
conda activate ldm
```
在conda环境中执行模型转换脚本
2024-07-04 11:53:45 +08:00
### 将onnx模型转为mnn模型
新建diffusion mnn模型文件夹将转好的mnn文件放在该文件夹下。
2024-07-22 19:51:53 +08:00
1. 实现encoder从onnx模型 -> mnn模型
```
2024-07-04 11:53:45 +08:00
./MNNConvert -f ONNX --modelFile onnx_save_path/text_encoder/model.onnx --MNNModel mnn_save_path/text_encoder.mnn --weightQuantBits 8 --bizCode biz
2024-07-22 19:51:53 +08:00
```
2. 实现denoiser从onnx模型 -> mnn模型
```
2024-07-04 11:53:45 +08:00
./MNNConvert -f ONNX --modelFile onnx_save_path/unet/model.onnx --MNNModel mnn_save_path/unet.mnn --transformerFuse --weightQuantBits 8 --bizCode biz
2024-07-22 19:51:53 +08:00
```
3. 实现decoder从onnx模型 -> mnn模型
```
2024-07-04 11:53:45 +08:00
./MNNConvert -f ONNX --modelFile onnx_save_path/vae_decoder/model.onnx --keepInputFormat --MNNModel mnn_save_path/vae_decoder.mnn --weightQuantBits 8 --bizCode biz
2024-07-22 19:51:53 +08:00
```
2024-07-04 11:53:45 +08:00
## 编译Diffusion Demo
### Linux/MAC/Windows上
2024-07-22 19:51:53 +08:00
```
cd mnn_path
mkdir build
cd build
2024-07-04 11:53:45 +08:00
cmake .. -DMNN_BUILD_DIFFUSION=ON -DMNN_BUILD_OPENCV=ON -DMNN_IMGCODECS=ON -DMNN_OPENCL=ON -DMNN_SEP_BUILD=OFF -DMNN_SUPPORT_TRANSFORMER_FUSE=ON
2024-07-22 19:51:53 +08:00
make -j32
```
2024-07-04 11:53:45 +08:00
### Android上
2024-07-22 19:51:53 +08:00
```
cd mnn_path/project/android/build
2024-07-04 11:53:45 +08:00
../build_64.sh -DMNN_BUILD_DIFFUSION=ON -DMNN_BUILD_OPENCV=ON -DMNN_IMGCODECS=ON -DMNN_OPENCL=ON -DMNN_SEP_BUILD=OFF -DMNN_SUPPORT_TRANSFORMER_FUSE=ON
2024-07-22 19:51:53 +08:00
../updateTest.sh
```
2024-07-04 11:53:45 +08:00
## 运行Diffusion Demo
2024-07-22 19:51:53 +08:00
```
2024-07-04 11:53:45 +08:00
./diffusion_demo <resource_path> <model_type> <output_image_name> <input_text>
2024-07-22 19:51:53 +08:00
```
其中resource_path 就是mnn模型文件的路径除了mnn文件还需要:
1. 将MNN目录transformers/diffusion/scheduler/alphas.txt文件拷贝到该文件夹下。
2. 针对stable-diffusion-v1-5模型需要将huggingfacetokenizer目录下merges.txt和vocab.json拷贝到该文件夹中。
3. 针对Taiyi-Stable-Diffusion模型需要将huggingfacetokenizer目录下vocab.txt拷贝到该文件夹中。
4. model_type是目前支持的两种diffusion模型的类别。如果是stable-diffusion-v1-5模型设为0如果是Taiyi-Stable-Diffusion模型设为1。
5. output_image_name是生成图片的名字默认图片位置在当前运行目录下。
6. input_text是文生图的prompt如果是stable-diffusion-v1-5模型建议英文prompt如果是Taiyi-Stable-Diffusion建议中文prompt。
2024-07-04 11:53:45 +08:00
运行指令例如:
2024-07-22 19:51:53 +08:00
```
./diffusion_demo mnn_sd1.5_path 0 demo.jpg "a cute cat"
./diffusion_demo mnn_chilloutmix_path 0 demo.jpg "a pure girl"
./diffusion_demo mnn_taiyi_path 1 demo.jpg "一只可爱的猫"
```