一、摘要

  • 老鸟们有信用卡注册hugfacede的就不必看我的了。
  • 本文运用aistudio的PaddleNLP,运用网络爬取的少数我国美食图片进行用LoRA模型训练。
  • 另外运用 AI Studio 运用中心进行模型布置,运用GPU资源进行推理展示!

地址:aistudio.baidu.com/aistudio/pr…

二、模型训练

【赛题链接】:github.com/PaddlePaddl…

【PaddlePaddle Hackathon 第四期 使命总览】:github.com/PaddlePaddl…

【队名】:大江向东我向西画画队
【模型简介】:美食、我国美食、顶尖美食
【模型链接】: huggingface.co/livingbody/…
【AI Studio 项目地址】:aistudio.baidu.com/aistudio/pr… 【AI Studio 运用中心】:AIGC我国美食

1.构思简介&效果展示

  • 我国美食与花
  • a photo of Chinese cuisine with flowers

30行代码拥有个性stable-diffusion文生图在线GPU【免费】部署的应用
30行代码拥有个性stable-diffusion文生图在线GPU【免费】部署的应用

2.训练代码

2.1paddlenlp装置

# 请运行这儿装置所需求的依赖环境!!
!pip install "paddlenlp>=2.5.2" safetensors "ppdiffusers>=0.11.1" --user
from IPython.display import clear_output
clear_output() # 整理很长的内容

2.2 huggingface登陆

!git config --global credential.helper store
from huggingface_hub import login
login()

2.3. 解压缩数据集

# 解压缩数据集
!unzip -qoa data/data198584/food.zip -d food

2.4. 训练

!python train_dreambooth_lora.py \
  --pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5"  \
  --instance_data_dir="./food" \
  --output_dir="lora_outputs" \
  --instance_prompt="a photo of Chinese cuisine" \
  --resolution=512 \
  --train_batch_size=1 \
  --gradient_accumulation_steps=1 \
  --checkpointing_steps=100 \
  --learning_rate=1e-4 \
  --lr_scheduler="constant" \
  --lr_warmup_steps=0 \
  --max_train_steps=500 \
  --seed=0 \
  --lora_rank=4 \
  --push_to_hub=False \
  --validation_prompt="a photo of Chinese cuisine   with flowers" \
  --validation_steps=100 \
  --num_validation_images=4

2.5. 上传模型

from utils import upload_lora_folder
upload_dir                    = "lora_outputs"                   # 咱们需求上传的文件夹目录
repo_name                     = "Chinese_Food"                  # 咱们需求上传的repo名称
pretrained_model_name_or_path = "runwayml/stable-diffusion-v1-5" # 训练该模型所运用的根底模型
prompt                        = "a photo of Chinese cuisine  with flowers" # 调配该权重需求运用的Prompt文本
upload_lora_folder(
    upload_dir=upload_dir,
    repo_name=repo_name,
    pretrained_model_name_or_path=pretrained_model_name_or_path,
    prompt=prompt, 
)

30行代码拥有个性stable-diffusion文生图在线GPU【免费】部署的应用

huggingface.co/livingbody/…

三、模型gradio布置

很简单,写代码,然后布置

1.写代码

# 1.装置依赖
import os
os.system("pip install paddlenlp==2.5.2")
os.system("pip install ppdiffusers==0.11.1")
# 2.导入库文件
import gradio as gr
from ppdiffusers import DiffusionPipeline, DPMSolverMultistepScheduler
import paddle
# 3.功用函数
def quickstart(prompt):
    image = pipe(prompt).images[0]
    return image
# 4.款式规划
with gr.Blocks() as demo:
    gr.Markdown("# 用LoRA 和 DreamBooth画出你眼中的 我国美食吧")
    gr.Image("image/500.png")
    gr.Markdown("## promot 为: Chinese cuisine ,即我国美食的意思。")
    greet_btn = gr.Button("开端生成") 
    input_text=gr.Textbox(label="输入你想要描绘的我国美食")   
    # 5.接口调用
    greet_btn.click(quickstart, inputs=input_text, outputs=gr.Image())
# 6.加载模型
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe.unet.load_attn_procs("lora_outputs/", from_hf_hub=True)
# 7.发动
demo.launch()

2.点击布置

30行代码拥有个性stable-diffusion文生图在线GPU【免费】部署的应用

3.界面

30行代码拥有个性stable-diffusion文生图在线GPU【免费】部署的应用

30行代码拥有个性stable-diffusion文生图在线GPU【免费】部署的应用

4.生成图

30行代码拥有个性stable-diffusion文生图在线GPU【免费】部署的应用

地址:aistudio.baidu.com/aistudio/pr…

本文正在参加「金石计划」