蚂蚁集团AI书法神器——Calligrapher本地部署教程:扩散模型实现高精度文本图像定制

一、模型介绍

Calligrapher是一种创新的扩散模型框架,由蚂蚁集团与香港科技大学的研究团队共同开发,专门用于数字书法和设计应用中的文本图像定制与风格转换。Calligrapher可以提供个性化的文本图像定制,它允许用户使用各种风格化的图像和文本提示执行自定义,支持英文的修改。

image.png

该框架通过自蒸馏机制、局部风格注入框架和上下文生成机制三大核心技术,实现了对任意文本或非文本输入的高精度风格复制和排版设计。

自蒸馏机制:利用预训练的文本到图像生成模型和大型语言模型自动构建风格中心的排版基准数据集,无需手动注释即可实现风格学习。这一机制有效解决了数据稀缺问题,提高了模型训练效率。

局部风格注入框架:通过可训练的风格编码器从参考图像中提取强大的风格特征,并在去噪过程中直接嵌入这些特征,以增强目标风格的对齐精度。这一框架确保了生成图像在风格上与参考图像的高度一致。

上下文生成机制:将参考图像直接整合到去噪过程中,显著提高了生成输出与目标参考之间的风格保真度,使得Calligrapher能够处理复杂的风格迁移场景,并保持字符级别的精确排布。

二、模型部署步骤

部署环境

uibuntu22.04
cuda12.1.1
python3.10
NVIDIA CorporationA100 SXM4

模型运行所需显存较高,且只能选择单张卡,建议选择高显存卡

文档中建议Python 3.10 + PyTorch 2.5.0 + CUDA,注意版本需要相匹配

1.更新基础的软件包

查看系统版本信息

#查看系统的版本信息,包括ID(如ubuntu、centos等)、版本号、名称、版本号ID等
cat /etc/os-release

image.png

配置国内源

image.png

apt 配置阿里源

image.png

将以下内容粘贴进文件中

deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib

2.基础Miniconda3环境

查看系统是否有 miniconda 的环境

conda -V

image.png

显示如上输出,即安装了相应环境,若没有 miniconda 的环境,通过以下方法进行安装

#下载 Miniconda 安装脚本
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
#运行 Miniconda 安装脚本
bash Miniconda3-latest-Linux-x86_64.sh
#初次安装需要激活 base 环境
source ~/.bashrc

按下回车键(enter)

image.png

输入 yes

image.png

输入 yes

image.png

安装成功如下图所示

image.png

3.创建虚拟环境

创建名为Calligrapher的虚拟环境

conda create -n Calligrapher python=3.10 -y

c创建虚拟环境.png

激活虚拟环境

conda activate Calligrapher

4.从github仓库克隆项目

输入命令克隆仓库并进入到项目之中

git clone https://github.com/Calligrapher2025/Calligrapher.git

cgitclone.png

5.安装pytorch环境

toirch

出现successfully则安装成功

成功下载.png

6.下载模型依赖库

安装模型依赖库.png

运行模型下载工具,自定义模型下载位置

from huggingface_hub import snapshot_download
# Download the base model FLUX.1-Fill-dev (granted access needed)
snapshot_download("black-forest-labs/FLUX.1-Fill-dev", token="your_token")
# Download SigLIP image encoder (this model can also be automatically downloaded when running the code)
snapshot_download("google/siglip-so400m-patch14-384")
# Download Calligrapher model and test data
snapshot_download("Calligrapher2025/Calligrapher")

下载内容包括Calligrapher模型权重、基础模型FLUX.1-Fill-dev、SigLIP 图像编码器模型以及测试数据集

模型及数据下载.png

7.配置路径 path_dict.json

将路径修改为实际的存储地址

{
"data_dir": "path/to/Calligrapher_bench_testing",
"cli_save_dir": "path/to/cli_results",
"gradio_save_dir": "path/to/gradio_results",
"gradio_temp_dir": "path/to/gradio_tmp",
"base_model_path": "path/to/FLUX.1-Fill-dev",
"image_encoder_path": "path/to/siglip-so400m-patch14-384",
"calligrapher_path": "path/to/calligrapher.bin"
}

三、通过gradio进行演示

下载其他缺失依赖包

image.png

访问界面

# Basic Gradio demo
python gradio_demo.py

image.png

下面是Gradio演示界面

image.png

将自己选中的图片放进预选框,然后将图片放进编辑框

image.png

将图片拖入编辑框,并且选中画笔将需要修改的地方进行涂抹

image.png

输入想要修改的文字

image.png

根据需求调整

image.png

点击运行,等待过后得到结果

image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值