我已经针对自己的图像推理案例微调了moondream模型。我使用了官方仓库提供的微调脚本。在学会如何将脚本应用于我的数据集后,一切顺利。
当我使用”save_pretrained()”时,脚本生成了以下文件:
- config.json;
- generation_config.json;
- model.safetensors
现在我想加载它并对一些图像进行推理。我该怎么做?我尝试了许多方法但都没有成功,例如:
model = AutoModelForCausalLM.from_pretrained( pretrained_model_name_or_path=model_root_dir, use_safetensors=True, state_dict=load_file(model_path), config=model_config, # 提供模型的配置文件 trust_remote_code=True,)
但我遇到了如下错误:
AttributeError: module 'transformers_modules.vikhyatk.moondream2.fb2293ab7450beb1dae536b069f5966becf58e5c.moondream' has no attribute 'Moondream'
我该如何解决这个错误?
回答:
这个问题可能是因为moondream模型的配置和在HF仓库中的重命名发生了重大结构变化,如这个提交中所见
https://huggingface.co/vikhyatk/moondream2/commit/05d640e6da70c37b2473e0db8fef0233c0709ce4
如果你使用save_pretrained()保存了你的检查点,它将不再与仓库的新结构对齐。
这是我暂时解决问题的方法。也许长期来看,需要一个新的”Legacy” moondream模型来解决这些变化。
解决方法:将HuggingFace中破坏性更改之前的提交内容复制到保存模型的目录中。
从config.json中删除任何远程引用
例如,将:
"AutoModelForCausalLM": "vikhyatk/moondream2--moondream.MoondreamModel"
替换为:
"AutoModelForCausalLM": "moondream.MoondreamModel"
如果模型抱怨缺少.py文件,将它们粘贴到本地目录中(在我这里是huggingface缓存文件夹中)。
现在,使用更新后的文件加载模型:
from transformers import AutoModelForCausalLMmodel = AutoModelForCausalLM.from_pretrained( "myawesomemodel", trust_remote_code=True,)
我知道这不是一个完美的解决方案,但也许它能帮到你。
如果你有更好的方法,请告诉我!