Stable Diffusion:使用ControlNet为黑白照片上色

https://www.dong-blog.fun/post/2019

使用ControlNet的Recolor模型为黑白图片上色

1. Recolor Control-LoRA简介

用途 :专为黑白照片上色设计的轻量化模型
核心特点

• 老照片修复/历史影像彩色化的理想选择

• 基于ControlNet架构,采用LoRA(低秩适应)技术实现模型瘦身:

• 原版ControlNet:4.7GB → Control-LoRA:仅738MB/377MB

• 训练时覆盖多样化图像概念和比例,具备优秀泛化能力

技术文档:HuggingFace项目页

2. 模型下载

官方资源库:ControlNet模型集合

推荐下载以下2个核心模型:

复制代码
sai_xl_recolor_128lora.safetensors   396 MB
sai_xl_recolor_256lora.safetensors   774 MB  

我下载了sai_xl_recolor_256lora.safetensors放在models/ControlNet

SDXL 模型我选择了juggernautXL_v8Rundiffusion.safetensors,放到 models/Stable-diffusion

3. 快速部署指南

3.1 使用预配置Docker镜像

bash 复制代码
kevinchina/deeplearning:sdwebui-contorlnet

3.2 容器启动命令

bash 复制代码
docker run --net host \
    --gpus device=2 \
    -v ./models:/workspace/stable-diffusion-webui/models \
    -it kevinchina/deeplearning:sdwebui-contorlnet bash

3.3 启动WebUI服务

bash 复制代码
cd /workspace/stable-diffusion-webui
/opt/conda/bin/python3 webui.py \
    --enable-insecure-extension-access \
    --skip-python-version-check \
    --skip-torch-cuda-test \
    --skip-install \
    --no-half-vae \
    --timeout-keep-alive 300 \
    --port 7864 \
    --opt-sdp-no-mem-attention \
    --no-download-sd-model \
    --api --listen

4. 打开webui使用

依次按图设置参数:

设置ContorlNet:

recolor_luminance(重着色-亮度)

recolor_intensity(重着色-强度)

5. 使用API批量生图

执行 python generate_recolor_image.py 即可访问API生图

6. 使用 qwen2.5 vl 7b 进行图像描述

在生图的时候,如果有一个较好的图像描述,更有利于生成更好的图像,这里我们开一个 qwen2.5 vl 7b 的API。

教程在这里:https://www.dong-blog.fun/post/2009

这么启动:

复制代码
docker run -it --rm --gpus '"device=1"' \
--shm-size 16G \
--net host \
-v /ssd/xiedong/Qwen/Qwen2.5-VL-7B-Instruct:/Qwen2.5-VL-Instruct \
kevinchina/deeplearning:llamafactory20250311-3 bash

vllm serve /Qwen2.5-VL-Instruct --max-model-len 16384 --tensor-parallel-size 1 --mm-processor-kwargs '{"min_pixels": 784, "max_pixels": 2352000, "fps": 1}' --limit-mm-per-prompt "image=2,video=1"

运行 python request_qwen257b.py 即可请求API拿到图像的描述。

比如拿到:

复制代码
The image features a bouquet of flowers wrapped in white paper. The bouquet includes various types of flowers in soft pastel colors, such as peach, light yellow, and white. Green leaves and stems are interspersed among the flowers, adding contrast to the soft hues. The background includes a hint of a peach-colored object and a white surface.

7. 串联过程

输入一张图,先请求qwen2.5 vl 7b 进行图像描述,得到图像描述作为Prompt。

使用Prompt和图,去请求SDXL+ControlNet,得到结果图。

运行 python auto_recolor_with_description.py 即可得到 output_recolored.jpg

8. gradio sdwebui

python recolor_gradio_app.py

使用说明

  1. 点击上方图片区域上传图片或者拖放图片
  2. 点击开始处理按钮
  3. 等待系统处理,将显示原始调整大小的图片、L通道图片和重上色结果
  4. 处理过程包括:
  5. 将图像调整为1024x1024
  6. 提取LAB色彩空间的L通道
  7. 使用Qwen2.5-VL生成图像描述
  8. 使用SD WebUI ControlNet进行重上色

9. 批量处理测试数据集,生成待测试数据的结果图

python process_and_recolor_images.py

  • 新建/ssd/xiedong/image_color/sdwebuicolor_test
  • 读取/ssd/xiedong/image_color/ddcolor_test下的所有图
  • 图片名字中含有real_B_rgb的就是原图
  • 把原图复制到/ssd/xiedong/image_color/sdwebuicolor_test
  • 图片名字中含有real_A的图是L通道图
  • 把L通道图复制到/ssd/xiedong/image_color/sdwebuicolor_test
  • 图片名字中含有fake_B_rgb的不用管
  • 然后你需要用每张原图,都用这里的这个逻辑代码去生成一张图,并命名为fake_B_rgb的那种名字,存入/ssd/xiedong/image_color/sdwebuicolor_test
  • 最后,将/ssd/xiedong/image_color/sdwebuicolor_test里的图都resize到384*384。

10. 指标测试

bash 复制代码
docker run -it --gpus device=2 -v /ssd/xiedong/image_color:/ssd/xiedong/image_color kevinchina/deeplearning:2.5.1-cuda12.1-cudnn9-devel-ddcolor-webui-metric bash

cd /ssd/xiedong/image_color

python evaluate_colorization.py --results_dir /ssd/xiedong/image_color/sdwebuicolor_test --output_dir /ssd/xiedong/image_color/sdwebuicolor_test_metric  --use_fid

结果:

Metric Mean Std Min Max
ssim 0.722846 0.120499 0.274659 0.986491
psnr 17.418013 3.408277 8.719207 31.298785
mse 1588.597951 1364.569312 48.217181 8732.923037
mae 25.508724 11.242591 2.699956 71.070618
color_error 17.548058 8.445806 1.915768 61.184597
lpips 0.344167 0.109269 0.047365 0.669448
fid 58.473146 - - -
相关推荐
AIGC-Lison9 小时前
Stable Diffusion ComfyUI 基础教程(一) ComfyUI安装与常用插件
人工智能·stable diffusion·教程·ai绘画·sd·sd教程
shangyingying_110 小时前
关于AIGC stable diffusion 在图像超分上的算法整理
stable diffusion·aigc
未来之窗软件服务5 天前
Stable Diffusion win10 Cpu安装日志
stable diffusion
xuebodx09236 天前
私有部署stable-diffusion-webui
图像处理·pytorch·ai作画·stable diffusion·视觉检测·transformer·dall·e 2
AIGC_ZY6 天前
Stable Diffusion XL、SD3 与 Flux 模型常用优化器总结
stable diffusion
在线打码6 天前
Dify + Stable Diffusion实现文生图工作流【两种方式】
ai·ai作画·stable diffusion·aigc·dify
曲幽6 天前
Python本地部署Stable Diffusion实现在纯CPU环境下的实现
python·ai·stable diffusion·cpu·openvino·lcm
AIGC-Lison10 天前
【CSDN首发】Stable Diffusion从零到精通学习路线分享
人工智能·ai·stable diffusion·aigc·sd
AI绘画咪酱10 天前
Stable Diffusion|Ai赋能电商 Inpaint Anything
人工智能·ai·ai作画·stable diffusion·sd·ai教程·sd教程