本文的代码: https://github.com/xxddccaa/stable-diffusion-webui-contorlnet-recolor
用途:专为黑白照片上色设计的轻量化模型
核心特点:
• 老照片修复/历史影像彩色化的理想选择
• 基于ControlNet架构,采用LoRA(低秩适应)技术实现模型瘦身:
• 原版ControlNet:4.7GB → Control-LoRA:仅738MB/377MB
• 训练时覆盖多样化图像概念和比例,具备优秀泛化能力
技术文档:HuggingFace项目页
官方资源库: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
bashkevinchina/deeplearning:sdwebui-contorlnet
bashdocker run --net host \ --gpus device=2 \ -v ./models:/workspace/stable-diffusion-webui/models \ -it kevinchina/deeplearning:sdwebui-contorlnet bash
bashcd /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
依次按图设置参数:
设置ContorlNet:
recolor_luminance(重着色-亮度)
recolor_intensity(重着色-强度)
执行 python generate_recolor_image.py 即可访问API生图
在生图的时候,如果有一个较好的图像描述,更有利于生成更好的图像,这里我们开一个 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.
输入一张图,先请求qwen2.5 vl 7b 进行图像描述,得到图像描述作为Prompt。
使用Prompt和图,去请求SDXL+ControlNet,得到结果图。
运行 python auto_recolor_with_description.py 即可得到 output_recolored.jpg
python recolor_gradio_app.py
使用说明
python process_and_recolor_images.py
bashdocker 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 | - | - | - |
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!