-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. add doc for opencv and diffusers 2. tiny fix of transformers
- Loading branch information
1 parent
6b501f5
commit b3355d8
Showing
13 changed files
with
427 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Diffusers | ||
=========== | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
install.rst | ||
quick_start.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
安装指南 | ||
============== | ||
|
||
本教程面向使用 Diffusers & 昇腾开发者,帮助完成昇腾环境下 Diffusers 的安装。 | ||
|
||
昇腾环境安装 | ||
------------ | ||
|
||
请根据已有昇腾产品型号及CPU架构等按照 :doc:`快速安装昇腾环境指引 <../ascend/quick_install>` 进行昇腾环境安装,或直接获取对应产品的昇腾环境镜像 `cosdt/cann <https://hub.docker.com/r/cosdt/cann/tags>`_ 。 | ||
|
||
.. warning:: | ||
CANN 最低版本为 8.0.rc1,安装 CANN 时,请同时安装 Kernel 算子包。 | ||
|
||
Diffusers 安装 | ||
------------------ | ||
|
||
Python 环境创建 | ||
------------------ | ||
|
||
.. code-block:: shell | ||
:linenos: | ||
# 创建名为 diffusers 的 python 3.10 的虚拟环境 | ||
conda create -n diffusers python=3.10 | ||
# 激活虚拟环境 | ||
conda activate diffusers | ||
pip 安装 | ||
------------------ | ||
|
||
通过以下指令安装 Diffusers 及 torch-npu: | ||
|
||
.. code-block:: shell | ||
:linenos: | ||
pip install diffusers torch==2.2.0 torch-npu==2.2.0 torchvision | ||
安装校验 | ||
------------------ | ||
|
||
执行以下代码,若无任何报错,仅打印模型下载过程,即说明安装成功: | ||
|
||
.. code-block:: python | ||
:linenos: | ||
from diffusers import DiffusionPipeline | ||
import torch | ||
pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16) | ||
pipeline.to("npu") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
快速开始 | ||
================== | ||
|
||
本示例以文生图 Diffusers 库中文生图任务为样例,展示如何进行文生图模型 stable-diffusion-xl-base-1.0 的基于 LoRA 的微调及动态合并 LoRA 的推理。 | ||
|
||
文生图 | ||
------------- | ||
|
||
.. _download: | ||
|
||
模型及数据集下载 | ||
~~~~~~~~~~~~~~~~~~~~ | ||
|
||
1. 请提前下载 `stabilityai/stable-diffusion-xl-base-1.0 <https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0>`_ 模型至自定义路径 | ||
|
||
2. 请提前下载 `madebyollin/sdxl-vae-fp16-fix <https://huggingface.co/madebyollin/sdxl-vae-fp16-fix>`_ 模型至自定义路径 | ||
|
||
3. 请提前下载 `reach-vb/pokemon-blip-captions <https://huggingface.co/datasets/reach-vb/pokemon-blip-captions>`_ 数据集至自定义路径 | ||
|
||
|
||
.. _finetune: | ||
|
||
基于 LoRA 的微调 | ||
~~~~~~~~~~~~~~~~~~~~ | ||
|
||
进入 Diffusers 项目目录,新建并执行以下脚本: | ||
|
||
.. note:: | ||
|
||
请根据 :ref:`download` 中模型及数据集的实际缓存路径指定 stable-diffusion-xl-base-1.0 模型缓存路径 ``MODEL_NAME``,sdxl-vae-fp16-fix 模型缓存路径 ``VAE_NAME`` 和。 | ||
|
||
.. code-block:: shell | ||
:linenos: | ||
:emphasize-lines: 1,2,3 | ||
export MODEL_NAME="./models_ckpt/stable-diffusion-xl-base-1.0/" | ||
export VAE_NAME="./ckpt/sdxl-vae-fp16-fix" | ||
export TRAIN_DIR="~/diffusers/data/pokemon-blip-captions/pokemon" | ||
python3 ./examples/text_to_image/train_text_to_image_lora_sdxl.py \ | ||
--pretrained_model_name_or_path=$MODEL_NAME \ | ||
--pretrained_vae_model_name_or_path=$VAE_NAME \ | ||
--dataset_name=$DATASET_NAME --caption_column="text" \ | ||
--resolution=1024 \ | ||
--random_flip \ | ||
--train_batch_size=1 \ | ||
--num_train_epochs=2 \ | ||
--checkpointing_steps=500 \ | ||
--learning_rate=1e-04 \ | ||
--lr_scheduler="constant" \ | ||
--lr_warmup_steps=0 \ | ||
--mixed_precision="no" \ | ||
--seed=42 \ | ||
--output_dir="sd-pokemon-model-lora-sdxl" \ | ||
--validation_prompt="cute dragon creature" | ||
微调过程无报错,并且终端显示 ``Steps: 100%`` 的进度条说明微调成功。 | ||
|
||
|
||
动态合并 LoRA 的推理 | ||
~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. note:: | ||
|
||
请根据 :ref:`download` 中模型实际缓存路径指定 ``model_path`` | ||
|
||
根据 :ref:`finetune` 中指定的 LoRA 模型路径 ``output_dir`` 指定 ``lora_model_path`` | ||
|
||
[可选] 修改 ``prompt`` 可使得生成图像改变 | ||
|
||
.. code-block:: python | ||
:linenos: | ||
:emphasize-lines: 9 | ||
from diffusers import DiffusionPipeline | ||
import torch | ||
lora_model_path = "path/to/sd-pokemon-model-lora-sdxl/checkpoint-800/" | ||
model_path = "./models_ckpt/stable-diffusion-xl-base-1.0/" | ||
pipe = DiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16) | ||
# 将模型放到 NPU 上 | ||
pipe.to("npu") | ||
# 加载 LoRA 权重 | ||
pipe.load_lora_weights(lora_model_path) | ||
# 输入 prompt | ||
prompt = "Sylveon Pokemon with elegant features, magical design, \ | ||
light purple aura, extremely detailed and intricate markings, \ | ||
photo realistic, unreal engine, octane render" | ||
# 推理 | ||
image = pipe(prompt, num_inference_steps=30, guidance_scale=7.5).images[0] | ||
image.save("pokemon-finetuned-inference-generation.png") | ||
微调过程无报错,并且终端显示 ``Loading pipeline components...: 100%`` 的进度条说明微调成功。 | ||
查看当前目录下保存的 ``pokemon-finetuned-inference-generation.png`` 图像,可根据 ``prompt`` 生成内容相关的图像说明推理成功。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
OpenCV | ||
=========== | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
install.rst | ||
quick_start.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
安装指南 | ||
============== | ||
|
||
OpenCV 4.9.0 版本开始,增加了图像处理相关高频接口的昇腾原生支持,本教程面向使用 OpenCV & 昇腾开发者,帮助完成昇腾环境下 OpenCV 的安装。 | ||
|
||
昇腾环境安装 | ||
------------ | ||
|
||
请根据已有昇腾产品型号及CPU架构等按照 :doc:`快速安装昇腾环境指引 <../ascend/quick_install>` 进行昇腾环境安装,或直接获取对应产品的昇腾环境镜像 `cosdt/cann <https://hub.docker.com/r/cosdt/cann/tags>`_ 。 | ||
|
||
.. warning:: | ||
CANN 最低版本为 8.0.rc1,安装 CANN 时,请同时安装 Kernel 算子包。 | ||
|
||
OpenCV 安装 | ||
---------------------- | ||
|
||
|
||
请遵循以下版本控制: | ||
|
||
======= ========== ========== | ||
lib 最低版本 推荐版本 | ||
======= ========== ========== | ||
OpenCV 4.9.0 latest | ||
Python 3.9 3.9 | ||
GCC 9.4.0 9.4.0 | ||
======= ========== ========== | ||
|
||
Python 环境创建 | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. code-block:: shell | ||
:linenos: | ||
# 创建名为 opencv 的 python 3.10 的虚拟环境 | ||
conda create -n opencv python=3.10 | ||
# 激活虚拟环境 | ||
conda activate opencv | ||
源码编译 | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
1. 下载 OpenCV 和 opencv_contrib | ||
|
||
.. code-block:: shell | ||
:linenos: | ||
git clone https://github.com/opencv/opencv.git | ||
cd opencv | ||
git clone https://github.com/opencv/opencv_contrib.git | ||
2. 编译带有 opencv_contrib 的 OpenCV | ||
|
||
.. TODO: check for the simplest cmake config | ||
.. code-block:: shell | ||
:linenos: | ||
# 在 opencv 项目目录中创建并进入 build 目录 | ||
mkdir build | ||
cd build | ||
# cmake & make | ||
cmake -D CMAKE_BUILD_TYPE=RELEASE | ||
-D CMAKE_INSTALL_PREFIX=pwd/install \ | ||
-D WITH_DEBUG=0 \ | ||
-D OPENCV_EXTRA_MODULES_PATH=/path/to/opencv/opencv_contrib/modules \ | ||
-D DWITH_CUDA=0 \ | ||
-D DWITH_CANN=1 \ | ||
-D DPYTHON3_EXECUTABLE=/path/to/miniconda3/envs/opencv/bin/python \ | ||
-D DPYTHON_LIBRARY=/path/to/miniconda3/envs/opencv \ | ||
-D PYTHON_INCLUDE_DIR=/path/to/miniconda3/envs/opencv/include/python3.10 \ | ||
-D BUILD_opencv_wechat_qrcode=OFF \ | ||
-D BUILD_opencv_xfeatures2d=OFF \ | ||
-D BUILD_opencv_face=OFF \ | ||
-D BUILD_opencv_dnn=OFF \ | ||
-D BUILD_opencv_features2d=OFF \ | ||
-D WITH_CAROTENE=OFF \ | ||
-D WITH_IPP=OFF \ | ||
-D BUILD_DOCS=ON \ | ||
-D BUILD_EXAMPLES=ON .. | ||
make -j5 | ||
当编译出现以下关键回显信息时,说明编译成功。 | ||
|
||
.. code-block:: shell | ||
# xxx 为 OpenCV 中某模块名称 | ||
[100%] Built target xxx | ||
安装校验 | ||
---------------------- | ||
|
||
通过以下指令执行昇腾算子单元测试: | ||
|
||
.. code-block:: shell | ||
:linenos: | ||
cd path/to/opencv/build/bin | ||
./opencv_test_cannops | ||
出现以下关键回显说明安装成功: | ||
|
||
.. code-block:: shell | ||
[==========] 72 tests from 4 test cases ran. (40937 ms total) | ||
[ PASSED ] 72 tests. | ||
Oops, something went wrong.