利用PaddleNLP和LoRA模型训练中国美食图片

释放双眼,带上耳机,听听看~!
本文利用PaddleNLP和LoRA模型,通过网络爬取的少量中国美食图片进行训练,利用AI Studio进行模型部署,并利用GPU资源进行推理展示。

一、摘要

  • 老鸟们有信用卡注册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

利用PaddleNLP和LoRA模型训练中国美食图片
利用PaddleNLP和LoRA模型训练中国美食图片

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, 
)

利用PaddleNLP和LoRA模型训练中国美食图片

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).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.点击部署

利用PaddleNLP和LoRA模型训练中国美食图片

3.界面

利用PaddleNLP和LoRA模型训练中国美食图片

利用PaddleNLP和LoRA模型训练中国美食图片

4.生成图

利用PaddleNLP和LoRA模型训练中国美食图片

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

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

本网站的内容主要来自互联网上的各种资源,仅供参考和信息分享之用,不代表本网站拥有相关版权或知识产权。如您认为内容侵犯您的权益,请联系我们,我们将尽快采取行动,包括删除或更正。
AI教程

国内服务器上搭建chat GPT和后端Spring Boot集成chat GPT教程

2023-12-19 21:50:14

AI教程

使用Landchain开发LLM应用:简介

2023-12-19 22:03:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索