前言:本篇博客总结本人在复现 CVPR 2024 论文 AEROBLADE 过程中遇到的一些问题及解决方案。
注:仅仅使用了论文github源码中的Quickstart部分。
论文链接🔗:AEROBLADE: Training-Free Detection of Latent Diffusion Images Using Autoencoder Reconstruction Error
github链接🔗:https://github.com/jonasricker/aeroblade/tree/main
1. 环境配置
把github整个项目下载下来之后,首先按readme.md中的步骤新建conda环境,注意使用Python 3.10,因为使用其他python版本可能会在程序运行时报错。
本人使用的是3.10.14版本,顺利运行。
2. 脚本运行
运行 run_aeroblade.py
脚本,分别使用各个生成模型生成重建图像并计算与原始图像的距离。
2.1 图像尺寸统一
这涉及parse_args()
函数中的 --files-or-dirs
参数,修改成自己数据所在的路径。这里主要注意两点:
1)每个目录下的所有图像尺寸要一致,即: ( H , W ) i = ( H , W ) j , ∀ i ∀ j ∈ D i r e c t o r y (H,W)_{i} = (H,W)_{j},\forall i ~\forall j \in Directory (H,W)i=(H,W)j,∀i ∀j∈Directory,不然会报如下错误:
File "/newdata/RelatedWorks/AIGCForensics/aeroblade-main/scripts/run_aeroblade.py", line 93, in <module>main(parse_args())File "/newdata/RelatedWorks/AIGCForensics/aeroblade-main/scripts/run_aeroblade.py", line 17, in maindistances = compute_distances(File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/high_level_funcs.py", line 73, in compute_distancesFile "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_contextreturn func(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/distances.py", line 44, in computeFile "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/distances.py", line 194, in _computeFile "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/joblib/memory.py", line 655, in __call__return self._cached_call(args, kwargs)[0]File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/joblib/memory.py", line 598, in _cached_callout, metadata = self.call(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/joblib/memory.py", line 856, in calloutput = self.func(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/distances.py", line 149, in _compute_lpipsFile "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_implreturn self._call_impl(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_implreturn forward_call(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/distances.py", line 85, in forward
RuntimeError: The size of tensor a (510) must match the size of tensor b (504) at non-singleton dimension 3
2)每张图像的长和宽也要一致,即: H i = W i , ∀ i ∈ D i r e c t o r y H_{i} = W_{i}, \forall i \in Directory Hi=Wi,∀i∈Directory,不然会报如下错误:
Setting up [LPIPS] perceptual loss: trunk [vgg], v[0.1], spatial [on]
Loading model from: /root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/lpips/weights/v0.1/vgg.pth
Computing LPIPS: 0%| | 0/1 [00:00<?, ?it/s]
Traceback (most recent call last): | 0/1 [00:00<?, ?it/s]File "/newdata/RelatedWorks/AIGCForensics/aeroblade-main/scripts/run_aeroblade.py", line 93, in <module>main(parse_args())File "/newdata/RelatedWorks/AIGCForensics/aeroblade-main/scripts/run_aeroblade.py", line 17, in maindistances = compute_distances(File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/high_level_funcs.py", line 73, in compute_distancesFile "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_contextreturn func(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/distances.py", line 44, in computeFile "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/distances.py", line 194, in _computeFile "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/joblib/memory.py", line 655, in __call__return self._cached_call(args, kwargs)[0]File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/joblib/memory.py", line 598, in _cached_callout, metadata = self.call(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/joblib/memory.py", line 856, in calloutput = self.func(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/distances.py", line 149, in _compute_lpipsFile "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_implreturn self._call_impl(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_implreturn forward_call(*args, **kwargs)File "/root/anaconda3/envs/aeroblade/lib/python3.10/site-packages/aeroblade-0.0.0-py3.10.egg/aeroblade/distances.py", line 85, in forward
RuntimeError: The size of tensor a (510) must match the size of tensor b (504) at non-singleton dimension 3
2.2 模型参数加载
这涉及parse_args()
函数中的--autoencoders
参数。由于网络原因,可以预先把用到的生成模型下载下来,然后替换成绝对路径。
该参数默认的三个模型及版本分别是CompVis/stable-diffusion-v1-1
、stabilityai/stable-diffusion-2-base
以及kandinsky-community/kandinsky-2-1
.
下载方式有两种,1)在huggingface官网手动下载,2)使用huggingface的镜像下载,后者方式如下:
# linux环境下,依次在终端执行下列命令:
1 pip install -U huggingface_hub
2. export HF_ENDPOINT=https://hf-mirror.com
3. huggingface-cli download --resume-download 模型名称 --local-dir 模型保存路径
其中,模型名称
是CompVis/stable-diffusion-v1-1
,不能简写成stable-diffusion-v1-1
,因为要和huggingface官网上的模型名称对应才能准确找到下载的模型。模型保存路径
就是--autoencoders
参数中要给出的路径,每个模型对应一个路径。
本人在下载kandinsky-2-1
时尝试多次,最后不知怎么就好了……可能是由于网络原因,遇到相关问题建议大家多尝试几次。