一、摘要
- 老鸟们有信用卡注册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
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,
)
三、模型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).jpg>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.点击部署
3.界面
4.生成图
地址:aistudio.baidu.com/aistudio/pr…
本文正在参加「金石计划」