# Stable Diffusion WebUI Manual ## 規格說明 主要使用GPU 2、3,程式占用port: 7860 ## 建立環境 ### Python venv ``` python -m venv {venv_name} source {venv_name}/bin/activate pip install --upgrade pip pip install -r /home/diffusion/Documents/buildenv/environment.txt ``` environment.txt ```= albumentations==0.4.3 diffusers opencv-python==4.1.2.30 pudb==2019.2 invisible-watermark imageio==2.9.0 imageio-ffmpeg==0.4.2 pytorch-lightning==1.4.2 omegaconf==2.1.1 test-tube>=0.7.5 streamlit>=0.73.1 einops==0.3.0 torch-fidelity==0.3.0 transformers==4.19.2 torchmetrics==0.6.0 kornia==0.6 streamlit-drawable-canvas==0.8 -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers -e git+https://github.com/openai/CLIP.git@main#egg=clip -e . ``` ### Conda ``` conda env create -f /home/diffusion/Documents/buildenv/environment.yaml conda activate {env_name} ``` environment.yaml ```=yaml name: {env_name} channels: - pytorch - defaults dependencies: - python=3.8.5 - pip=20.3 - cudatoolkit=11.3 - pytorch=1.11.0 - torchvision=0.12.0 - numpy=1.19.2 - pip: - albumentations==0.4.3 - diffusers - opencv-python==4.1.2.30 - pudb==2019.2 - invisible-watermark - imageio==2.9.0 - imageio-ffmpeg==0.4.2 - pytorch-lightning==1.4.2 - omegaconf==2.1.1 - test-tube>=0.7.5 - streamlit>=0.73.1 - einops==0.3.0 - torch-fidelity==0.3.0 - transformers==4.19.2 - torchmetrics==0.6.0 - kornia==0.6 - streamlit-drawable-canvas==0.8 - -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers - -e git+https://github.com/openai/CLIP.git@main#egg=clip - -e . ``` ## 部屬步驟 ``` ## 啟動環境 source /home/diffusion/Documents/venv_inference/bin/activate ## 啟動WebUI nohup /home/diffusion/Documents/SD_webui/stable-diffusion-webui/webui.sh & ``` 輸入主機的hostname即可看到介面 Ex. 127.0.0.1 ## 檔案說明 ### 資料夾 dataset --> 使用者上傳資料夾的地方(使用者可自訂路徑) models --> WebUI上訓練完的模型輸出的地方(使用者可自訂路徑) instructPix2Pix_finetune --> 存放製作資料集必須的腳本 SD_webui --> 存放WebUI主要程式的地方 Text_to_image --> 存放所有Stable Diffusion訓練腳本的地方、有各種Example和Script buildenv --> 建置環境的檔案 venv_inference --> 虛擬環境 unused --> 過去使用的程式、暫時性保留 ### 訓練腳本 /home/diffusion/Documents/instructPix2Pix_finetune/Push_dataset_to_local_pix2pix.py --> 製作本地的訓練資料集 /home/diffusion/Documents/instructPix2Pix_finetune/Push_dataset_to_hub_pix2pix.py --> 製作HuggingFace的訓練資料集 /home/diffusion/Documents/Text-to-image/diffusers/examples/instruct_pix2pix/train_instruct_pix2pix_local.py --> 提供本地訓練的腳本 /home/diffusion/Documents/Text-to-image/diffusers/examples/instruct_pix2pix/train_instruct_pix2pix.py --> 提供HuggingFace訓練的腳本 /home/diffusion/Documents/Text-to-image/diffusers/scripts/convert_diffusers_to_original_stable_diffusion.py --> 轉換訓練模型變成WebUI可以使用的模式 ## img2img 1. img2img --> 無法編輯圖片,輸入整張就是整張輸出 2. Sketch --> 可以編輯圖片,輸入整張就是整張輸出 3. Inpaint --> 無法編輯圖片,只替換局部想要替換的區域 ## ## Extensions ![image](https://hackmd.io/_uploads/B1IptOXnp.png) 主要可以修改的內容都在 /home/diffusion/Documents/SD_webui/stable-diffusion-webui/extensions/stable-diffusion-webui-custom-train/scripts/template_on_tab.py ## Finetune InstructPix2Pix 1. Upload Dataset 讓使用者上傳自己的資料集 2. Upload Dataset(HuggingFace) 讓使用者上傳自己的資料集 3. Finetune 使用資料集微調模型 4. Convert Model 將微調完的模型轉成WebUI可以使用的格式 訓練的指令範例 ``` nohup accelerate launch --mixed_precision="fp16" --gpu_ids="2,3" --main_process_port=29500 --num_processes=2 /home/diffusion/Documents/Text-to-image/diffusers/examples/instruct_pix2pix/train_instruct_pix2pix_local.py --pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" --dataset_name="/home/diffusion/Documents/dataset/Test_Dataset_1K_0216" --use_ema --resolution=512 --train_batch_size=8 --gradient_accumulation_steps=1 --gradient_checkpointing --max_train_steps=5000 --checkpointing_steps=5000 --learning_rate=1e-05 --max_grad_norm=1 --seed=512 --lr_scheduler="constant" --lr_warmup_steps=100 --output_dir="/home/diffusion/Documents/models/Test-Model-0220-2" --original_image_column='mask_image' --edited_image_column='image' --edit_prompt_column='text' --random_flip > /home/diffusion/Documents/models/Test-Model-0220-2.log 2>&1 & ``` ### Upload Dataset | 欄位名稱 | Default | 說明 | | -------- | -------- | -------- | | Upload File | none | 上傳資料集的壓縮檔 | | Dataset Path | none | 輸入保存到主機上的路徑 | ![image](https://hackmd.io/_uploads/r1n7mNn36.png) ### Upload Dataset(HuggingFace) | 欄位名稱 | Default | 說明 | | -------- | -------- | -------- | | Upload File | none | 上傳資料集的壓縮檔 | | Huggingface Token | none | 可至huggingface上的帳號取得 | | Huggingface Dataset | none | huggingface上保存的名稱 | | Dataset Path | none | 輸入保存到主機上的路徑 | ![image](https://hackmd.io/_uploads/HJjbE4nh6.png) ### Finetune | 欄位名稱 | Default | 說明 | | -------- | -------- | -------- | | Model Name | none | 訓練的模型名稱 | | Output Directory | none | 訓練完後輸出的路徑 | | Dataset Path | none | 資料集的路徑 | | Dataset Type | Local | 資料集的類型 | | Resolution | 512 | 圖片Size | | Learning Rate | 1e-5 | 學習率 | | Batch Size | 8 | 批次大小 | | Step | 1000 | 訓練步數 | ![image](https://hackmd.io/_uploads/Bk_t4En3T.png) ### Convert Model | 欄位名稱 | Default | 說明 | | -------- | -------- | -------- | | Model Name | none | 訓練的模型名稱 | | Model Path | none | 輸出的路徑 | ![image](https://hackmd.io/_uploads/H1Hl3E3ha.png) ## 訓練參數說明 | 參數名稱 | Default | 說明 | | -------- | -------- | -------- | | mixed_precision | none | 採用哪種混合精度計算 | | gpu_ids | none | 選擇要訓練的GPU編號 | | main_process_port | 25900 | 設定訓練的程式的執行port | | num_processes | 1 | 平行運算的程式數量 | | resolution | 512 | 訓練圖片Size | | train_batch_size | 4 | 批次大小 | | max_train_steps | 1000 | 執行最大的Steps數 | | checkpointing_steps | 500 | 訓練到多少個Step時保存一份權重| | learning_rate | 1e-5 | 採用哪種混合精度計算 | | lr_scheduler | constant | 訓練策略 | | output_dir | none | 輸出的資料夾 | | pretrained_model_name_or\_path | none |選擇預訓練模型 | ## 資料集 資料集限制 1. 壓縮檔內的首個資料夾名稱必須和壓縮檔的名稱相同 2. 子資料夾可隨意分類,但一組資料必須包含 image、masked image、prompt,且檔名需一致 ![image](https://hackmd.io/_uploads/rkWBauX3p.png) ![image](https://hackmd.io/_uploads/B1UBad72p.png) 一組資料須包含: | {filename}.jpg | {filename}_mask.jpg | {filename}.txt | | -------- | -------- | -------- | 副檔名: .jpg/.png皆可以使用 本地訓練資料集需包含以下檔案跟資料夾格式 ![image](https://hackmd.io/_uploads/rkcUf__2p.png) | 檔案名稱 | -------- | | xxxx.arrow | | dataset_info.json | | state.json | | dataset_dict.json | 注意: 若選擇上傳到HuggingFace則不會有以上檔案 ## 模型 訓練完的模型都必須透過轉換才能放到WebUI上使用,放置轉換後模型的路徑如下: /stable-diffusion-webui/models/Stable-diffusion ![image](https://hackmd.io/_uploads/S1ESUu_na.png) ## 參考資料 說明影片 https://drive.google.com/file/d/1Lkz_NX_PSNOdyGECRn5REH5Z1H0OgYL7/view?usp=sharing 擴充套件是用gradio框架,可參考以下文件: https://www.gradio.app/docs/file Stable Diffusion: https://huggingface.co/docs/diffusers/v0.13.0/en/training/text2image https://github.com/AUTOMATIC1111/stable-diffusion-webui