本地部署推理TextDiffuser-2:释放语言模型用于文本渲染的力量

系列文章目录

文章目录

  • 系列文章目录
  • 一、模型下载和环境配置
  • 二、模型训练
    • (一)训练布局规划器
    • (二)训练扩散模型
  • 三、模型推理
    • (一)准备训练好的模型checkpoint
    • (二)全参数推理
    • (三)LoRA微调推理
  • 四、遇到的错误
    • (一)importerror,缺少某些库
    • (二)报错:libGL.so.1: cannot open shared object file: No such file or directory
    • (三)各种奇奇怪怪的错误(本质上是diffusers版本不对)
    • (四)各种库的版本不兼容
    • (五)RuntimeError: expected scalar type float Float bu found Half


一、模型下载和环境配置

  1. 将textdiffuser-2模型仓库克隆到本地
git clone https://github.com/microsoft/unilm/
cd unilm/textdiffuser-2
  1. 创建并激活虚拟环境,在textdiffuser-2目录下安装需要的软件包
conda create -n textdiffuser2 python=3.8
conda activate textdiffuser2
pip install -r requirements.txt
  1. 安装与系统版本和cuda版本相匹配的torch、torchvision、xformers (我的环境下cuda是12.2的,其他版本需要自己去官网查询)
    在这里插入图片描述
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xformers
  1. 如果想用FastChat训练布局规划器,还需要安装flash-attention:

先将flash-attention模型仓库克隆下来

git clone https://github.com/Dao-AILab/flash-attention.git

然后安装对应的软件包

pip install packaging
pip uninstall -y ninja && pip install ninja
conda install -c nvidia cuda
pip install flash-attn --no-build-isolation
  1. 为了训练文本修复任务,还需要安装 differs 包
pip install https://github.com/JingyeChen/diffusers_td2.git

二、模型训练

(一)训练布局规划器

  1. 需要先下载lmsys/vicuna-7b-v1.5模型和FastChat模型。

模型下载方式: 采用git远程clone下来,具体方式可以参考之前的内容:huggingface学习 | 云服务器使用git-lfs下载huggingface上的模型文件;

  1. 进行训练
CUDA_VISIBLE_DEVICES=4,5 torchrun --nproc_per_node=2 --master_port=50008 FastChat-main/fastchat/train/train_mem.py \--model_name_or_path vicuna-7b-v1.5  \--data_path data/layout_planner_data_5k.json \--bf16 True \--output_dir experiment_result \--num_train_epochs 6 \--per_device_train_batch_size 2 \--per_device_eval_batch_size 2 \--gradient_accumulation_steps 16 \--evaluation_strategy "no" \--save_strategy "steps" \--save_steps 500 \--save_total_limit 5 \--learning_rate 2e-5 \--weight_decay 0. \--warmup_ratio 0.03 \--lr_scheduler_type "cosine" \--logging_steps 1 \--fsdp "full_shard auto_wrap" \--fsdp_transformer_layer_cls_to_wrap 'LlamaDecoderLayer' \--tf32 True \--model_max_length 2048 \--gradient_checkpointing True \--lazy_preprocess True

(二)训练扩散模型

  1. 需要先准备需要训练的扩散模型:stable-diffusion-v1-5模型
  2. 对于全参数训练:
accelerate launch train_textdiffuser2_t2i_full.py \--pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \--train_batch_size=18 \--gradient_accumulation_steps=4 \--gradient_checkpointing \--mixed_precision="fp16" \--num_train_epochs=6 \--learning_rate=1e-5 \--max_grad_norm=1 \--lr_scheduler="constant" \--lr_warmup_steps=0 \--output_dir="diffusion_experiment_result" \--enable_xformers_memory_efficient_attention \--dataloader_num_workers=8 \--index_file_path='/path/to/train_dataset_index.txt' \--dataset_path='/path/to/laion-ocr-select/' \--granularity=128 \--coord_mode="ltrb" \--max_length=77 \--resume_from_checkpoint="latest"
  1. 对于 LoRA 训练:
accelerate launch train_textdiffuser2_t2i_lora.py \--pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \--train_batch_size=18 \--gradient_accumulation_steps=4 \--gradient_checkpointing \--mixed_precision="fp16" \--num_train_epochs=6 \--learning_rate=1e-4 \--text_encoder_learning_rate=1e-5 \--lr_scheduler="constant" \--output_dir="diffusion_experiment_result" \--enable_xformers_memory_efficient_attention \--dataloader_num_workers=8 \--index_file_path='/path/to/train_dataset_index.txt' \--dataset_path='/path/to/laion-ocr-select/' \--granularity=128 \--coord_mode="ltrb" \--max_length=77 \--resume_from_checkpoint="latest"

三、模型推理

(一)准备训练好的模型checkpoint

  1. 下载官网提供的模型checkpoint:layout planner、diffusion model (full parameter fine-tuning) 和diffusion model (lora fine-tuning)

  2. 准备stable-diffusion-v1-5模型

(二)全参数推理

CUDA_VISIBLE_DEVICES=4 accelerate launch inference_textdiffuser2_t2i_full.py \--pretrained_model_name_or_path="./stable-diffusion-v1-5" \--mixed_precision="fp16" \--output_dir="inference_results_1" \--enable_xformers_memory_efficient_attention \--resume_from_checkpoint="./textdiffuser2-full-ft" \--granularity=128 \--max_length=77 \--coord_mode="ltrb" \--cfg=7.5 \--sample_steps=20 \--seed=43555 \--m1_model_path="./textdiffuser2_layout_planner" \--input_format='prompt' \--input_prompt='a hotdog with mustard and other toppings on it'

推理结果:
在这里插入图片描述

(三)LoRA微调推理

CUDA_VISIBLE_DEVICES=4 accelerate launch inference_textdiffuser2_t2i_lora.py \--pretrained_model_name_or_path="./stable-diffusion-v1-5" \--gradient_accumulation_steps=4 \--gradient_checkpointing \--mixed_precision="fp16" \--output_dir="inference_results_2" \--enable_xformers_memory_efficient_attention \--resume_from_checkpoint="./textdiffuser2-lora-ft" \--granularity=128 \--coord_mode="ltrb" \--cfg=7.5 \--sample_steps=50 \--seed=43555 \--m1_model_path="./textdiffuser2_layout_planner" \--input_format='prompt' \--input_prompt='a stamp of u.s.a'

运行结果:
在这里插入图片描述

四、遇到的错误

(一)importerror,缺少某些库

在运行过程中出现了各种各样的importerror,于是就是缺少哪个库就下载那个库:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
pip install protobuf

(二)报错:libGL.so.1: cannot open shared object file: No such file or directory

pip uninstall opencv-python
pip install opencv-python-headless

(三)各种奇奇怪怪的错误(本质上是diffusers版本不对)

  • RuntimeError: expected mat1 and mat2 to have the same dtype, but got: float != c10::Half
  • The deprecation tuple (‘LoRAXFormersAttnProcessor’, ‘0.26.0’, 'Make sure use XFormersAttnProcessor instead by settingLoRA layers to `self.
pip install diffusers==0.24.0 -i https://pypi.mirrors.ustc.edu.cn/simple/

(四)各种库的版本不兼容

由于作者在官网上提供了实验中使用的软件包列表可供参考,所以我直接将textdiffuser-2的assets文件夹下的refere_requirements.txt文件中的库一次性安装下来:

cd assets
pip install -r reference_requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple/

在这里插入图片描述

(五)RuntimeError: expected scalar type float Float bu found Half

这个错误是因为安装的diffusers包里有个文件需要用官网提供的新文件进行替换
可以先根据错误提示找到diffusers库包中attention_processor.py所在的位置,然后用assets文件夹下attention_processor.py进行替换即可解决问题。

在这里插入图片描述

参考:libGL.so.1: cannot open shared object file: No such file or directory

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/736260.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2021年江苏省职业院校技能大赛高职组 “信息安全管理与评估”赛项任务书

2021年江苏省职业院校技能大赛高职组 “信息安全管理与评估”赛项任务书 一、赛项时间:二、赛项信息三、竞赛内容:第一阶段任务书(300分)任务1:网络平台搭建(60分)任务2:网络安全设备…

Leetcode 3077. Maximum Strength of K Disjoint Subarrays

Leetcode 3077. Maximum Strength of K Disjoint Subarrays 1. 解题思路 1. 朴素思路2. 算法优化 2. 代码实现 题目链接:3077. Maximum Strength of K Disjoint Subarrays 1. 解题思路 这道题很惭愧没有搞定,思路上出现了差错,导致一直没能…

AI预测福彩3D第6弹【2024年3月11日预测--新算法重新开始计算日期】

由于周末休息了两天,没有更新文章,这两天也没有对福彩3D的预测。今天继续咱们使用AI算法来预测3D吧~ 前面我说过,我的目标是能让百十个各推荐7个号码,其中必有中奖号码,这就是7码定位,只要7码定位稳定了&am…

【前端系列】CSS 常见的选择器

CSS 常见的选择器 CSS(层叠样式表)是一种用于描述网页样式的标记语言,它定义了网页中各个元素的外观和布局。在 CSS 中,选择器是一种用于选择要应用样式的 HTML 元素的模式。选择器允许开发人员根据元素的类型、属性、关系等来选…

JVM3_数据库连接池虚引用ConnectionFinalizerPhantomReference引起的FullGC压力问题排查

背景 XOP服务运行期间,查看Grafana面板,发现堆内存周期性堆积,观察FullGC的时间,xxx,需要调查下原因 目录 垃圾收集器概述 常见的垃圾收集器分区收集策略为什么CMS没成为默认收集器 查看JVM运行时环境分析快照 Pha…

基于PCtoLCD实现OLED汉字取模方法

0 工具准备 PCtoLCD2002 NodeMCU(ESP8266)(验证OLED字模效果) 0.96寸OLED显示屏 1 基于PCtoLCD实现OLED汉字取模方法 1.1 基础知识介绍 0.96存OLED显示屏包含128x64个像素点,x轴方向为128个像素点,y轴方向…

Android打开、关闭网络

Android手机的网络一般有两种&#xff0c;WiFi网络和移动数据网络。 对网络进行操作前需要静态申请权限&#xff0c;因为并不会对隐私产生危害&#xff0c;所以只需要静态申请。 <!--查看数据网络状态&#xff0c;无需动态申请--> <uses-permission android:name&qu…

Linux教程(第5版)孟庆昌版 课后答案

思考题1 1.1 什么是软件&#xff1f;软件分为哪几种&#xff1f; 答&#xff1a;软件是相对硬件而言的&#xff0c;它是与数据处理系统操作有关的计算机程序和相关数据等的总称。 软件通常可分为三大类&#xff0c;即系统软件、应用软件和支撑软件。 1.2 根据你的理解&…

[AutoSar]BSW_Com011 CAN IF 模块配置

目录 关键词平台说明一、CanIfCtrlDrvCfgs二 、CanIfTrcvDrvCfgs三、CanIfDispatchCfg四、CanIfBufferCfgs五、CanIfHrhCfgs六、CanIfHthCfgs七、CanIfRxPduCfgs八、CanIfTxPduCfgs九、CanIfPrivateCfg十、CanIfPublicCfg 关键词 嵌入式、C语言、autosar、OS、BSW 平台说明 …

目前最强大语言模型!谷歌开源 | 开源日报 No.196

google/gemma_pytorch Stars: 3.4k License: Apache-2.0 gemma_pytorch 是 Google Gemma 模型的官方 PyTorch 实现。 提供了 Gemini 模型技术的轻量级、最新开放模型支持文本到文本、仅解码器大语言模型提供英文版本&#xff0c;包含开源权重、预训练变体和指导调整变体支持…

natfrp和FRP配置SSL的基本步骤和bug排查

获取免费/付费SSL 我直接买了一年的ssl证书 设置 主要参考&#xff1a;https://doc.natfrp.com/frpc/ssl.html 遇到的Bug root域名解析是ALIAS&#xff0c;不是CNAME不要用NATFRP &#xff08;SakuraFrp&#xff09;同步Joplin&#xff0c;会出现webdav错误导致大量笔记被…

javascript 对象的几种创建方式

话不多说&#xff0c;直接开干 第一种&#xff1a;Object 构造函数创建 var Person new Object(); Person.name "Nike"; Person.age 29;这行代码创建了 Object 引用类型的一个新实例&#xff0c;然后把实例保存在变量 Person 中。 第二种&#xff1a;使用对象字…

linux上安装fastdfs及配置

一、基础环境准备 1、所需软件 名称说明libfastcommonfastdfs分离出的一些公用函数包fastdfsfastdas软件包fastdfs-nginx-modulefastdfst和nginx的关联模块nginxnginxl软件包 2、编辑环境 安装一些基础的支持环境 yum install git gccc gcc-c make automake autoconf libto…

线性代数(一)——向量基础

向量基础 1、向量和线性组合2、向量的模和点乘3、矩阵4、参考 线性代数的核心是向量的加和乘两种运算的组合&#xff0c;本篇博客为线性代数的一个引子&#xff0c;主要从向量、线性组合和矩阵逐步引出线性代数的相关知识。 1、向量和线性组合 首先介绍的是向量相关&#xff0…

DHCP中继实验(思科)

华为设备参考&#xff1a;DHCP中继实验&#xff08;华为&#xff09; 一&#xff0c;技术简介 DHCP中继&#xff0c;可以实现在不同子网和物理网段之间处理和转发DHCP信息的功能。如果DHCP客户机与DHCP服务器在同一个物理网段&#xff0c;则客户机可以正确地获得动态分配的IP…

PCL 约束Delaunay三角网(版本二)

目录 一、算法概述二、代码实现三、结果展示四、测试数据本文由CSDN点云侠原创,原文链接。如果你不是在点云侠的博客中看到该文章,那么此处便是不要脸的爬虫与GPT。 一、算法概述 PCL 点云Delaunay三角剖分一文给出了PCL中Delaunay三角网算法的基础用法。本文在基础用法的基…

Python与FPGA——膨胀腐蚀

文章目录 前言一、膨胀腐蚀二、Python实现腐蚀算法三、Python实现膨胀算法四、Python实现阈值算法五、FPGA实现腐蚀算法总结 前言 腐蚀是指周围的介质作用下产生损耗与破坏的过程&#xff0c;如生锈、腐烂等。而腐蚀算法也类似一种能够产生损坏&#xff0c;抹去部分像素的算法。…

Open CASCADE学习|读取STEP模型文件到XDE中

目录 1、XDE组件简介 2、读取STEP模型文件到XDE中的步骤 3、案例 1、XDE组件简介 Open CASCADE的XDE&#xff08;扩展数据交换&#xff09;组件是一个关键的工具&#xff0c;它允许用户通过转换附加到几何BREP&#xff08;边界表示&#xff09;数据的附加数据来扩展数据交换…

Gafana Redis Overview dashboard

1. 简介 根据提供的 Redis 监控仪表盘 JSON 文件,包含的监控指标及其简要描述如下: redis_uptime_in_seconds: Redis 实例的运行时间(秒)。 redis_connected_clients: 当前连接到 Redis 实例的客户端数量。 redis_memory_used_bytes: Redis 实例使用的内存量(字节)。 redis_m…

SAP 消息编号 SG037

在日常外币银行结汇的时候&#xff0c;汇率小数点有可能是6位&#xff0c;但是SAP的汇率字段长度小数点后只有5位 所以&#xff0c;客户在F-02的时候&#xff0c;会出现一下报错“条目过长” 解决方法&#xff1a; 更改汇率的比率 OB08重新修改汇率 F-02界面 但是这种方法的风…