Error : cache_latents_to_disk for anima_train (キャッシュ読込エラー)

用户在运行 Kohya SD Scripts (sd-scripts) 中的 anima_train.py 脚本时,启用了 --cache_latents 和 --cache_latents_to_disk 参数。首次训练成功创建 .npz 缓存文件,但第二次及以后的训练中,脚本无法读取已存在的 .

Error : cache_latents_to_disk for anima_train (キャッシュ読込エラー)

Error : cache_latents_to_disk for anima_train (キャッシュ読込エラー)

快速结论:此报错发生在 Kohya SD Scripts 的 anima_train.py 使用 --cache_latents_to_disk 参数时,首次训练成功但后续训练无法读取已存在的 .npz 缓存文件。优先检查 strategy_anima.pyget_latents_npz_path 方法是否存在文件名后缀重复问题。

问题场景

用户在运行 Kohya SD Scripts (sd-scripts) 中的 anima_train.py 脚本时,启用了 --cache_latents--cache_latents_to_disk 参数。首次训练成功创建 .npz 缓存文件,但第二次及以后的训练中,脚本无法读取已存在的 .npz 文件,抛出 PIL.UnidentifiedImageError。用户同时使用了 dataset.config(metadata json)配置数据集。

报错原文

INFO     caching latents...     train_util.py:1174
ERROR    Error loading file:  train_util.py:2964
    C:\ ## folder_name ### \ ### file_name ##_0602x0429_anima.npz

Traceback (most recent call last):
  File "\train_util.py", line 2632, in new_cache_latents
    dataset.new_cache_latents(model, accelerator)
  File "train_util.py", line 1215, in new_cache_latents
    submit_batch(batch, current_condition)
  File "\train_util.py", line 1159, in submit_batch
    info.image = info.image.result()  # future to image
...
・
PIL.UnidentifiedImageError: cannot identify image file 'C:\ ## folder_name ### \ ### file_name ##_0602x0429_anima.npz'

原因分析

问题根因位于 strategy_anima.py 中的 get_latents_npz_path 方法。该方法使用 os.path.splitext(absolute_path)[0] 获取文件名基底,但传入的 absolute_path 可能已经包含末尾的分辨率后缀和 “_anima”(例如 ABCDEF_0602x0429_anima),导致生成路径时后缀重复:ABCDEF_0602x0429_anima_0602x0429_anima.npz。实际磁盘上保存的是正确的单次后缀文件名,因此脚本无法匹配读取。

当使用 metadata json 数据集且 .npz 已存在时,会发生图片路径被覆写的情况,进一步导致从错误路径生成 .npz 路径。SDXL Full-FT 不触发此问题,说明这与 anima 策略(strategy)结构相关。

环境排查

  • 确认使用的 sd-scripts 分支版本:main 068bcd7, main 1a3ec9e, dev fb03a16 均受影响。
  • 检查传入 --train_data_dir--in_json 的路径是否存在特殊字符或已包含分辨率信息的文件名。
  • 确认 --dataset_repeats--enable_bucket 等参数是否导致同一图片被多个 dataset 引用。

解决步骤

  1. 永久修复(推荐):更新 sd-scripts 至包含 PR #2381 修复的版本(如 commit b2b4992 或更新)。该修复由开发者在 Issue 关闭前合并至 main 分支。
  2. 临时修复(可优先尝试):在无法立即更新时,手动修改 strategy_anima.py 中的 get_latents_npz_path 方法,添加后缀重复检测:
    def get_latents_npz_path(self, absolute_path: str, image_size: Tuple[int, int]) -> str:
        base_path = os.path.splitext(absolute_path)[0]
        suffix = f"_{image_size[0]:04d}x{image_size[1]:04d}_anima"
    
        if suffix in base_path:
            return absolute_path
    
        return os.path.splitext(absolute_path)[0] + f"_{image_size[0]:04d}x{image_size[1]:04d}" + self.ANIMA_LATENTS_NPZ_SUFFIX

    注意:此临时修复存在局限性,对同一图片被多个 Dataset 复用(特别是使用 skip_image_resolution)时仍可能出错;极小概率下 .npz 未创建时也可能出错。

  3. 如果使用临时修复,建议先删除旧的损坏 .npz 缓存文件(位于训练数据目录内),重新训练生成正确的缓存。

验证方法

重新运行 anima_train.py,观察日志中是否出现 “caching latents…” 且成功加载 .npz 文件,不再抛出 PIL.UnidentifiedImageError。可以确认首次训练(创建 .npz)和后续训练(读取 .npz)均正常。

参考来源

kohya-ss/sd-scripts #2362

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 8241

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注