开发者社区 问答 正文

在使用ModelScope导出CV模型时报错 AttributeError

错误提示如下:
File "D:\anaconda\envs\mood_detection38_gpu\lib\site-packages\modelscope\models\base\base_model.py", line 170, in from_pretrained
model_cfg.model_dir = local_model_dir
AttributeError: 'dict' object has no attribute 'model_dir'
我的代码如下:

from modelscope.exporters import Exporter
from modelscope.models import Model
model_id = 'damo/cv_manual_face-detection_mtcnn'
model = Model.from_pretrained(model_id)
output_files = Exporter.from_model(model).export_torch_script(output_dir='ptModel')
print(output_files)

展开
收起
鼠小宝 2023-10-12 11:13:22 191 分享 版权
2 条回答
写回答
取消 提交回答
  • 公众号:网络技术联盟站,InfoQ签约作者,阿里云社区签约作者,华为云 云享专家,BOSS直聘 创作王者,腾讯课堂创作领航员,博客+论坛:https://www.wljslmz.cn,工程师导航:https://www.wljslmz.com

    根据您提供的错误提示和代码,可以看出问题出现在from_pretrained方法中的model_cfg.model_dir这一行。错误提示表示AttributeError: 'dict' object has no attribute 'model_dir',即字典对象没有名为model_dir的属性。

    根据代码,可能是model_cfg这个字典对象没有包含您期望的model_dir键或键对应的值不正确。您可以检查一下model_cfg字典对象的内容,确保其中包含了正确的模型目录信息。
    from_pretrained方法通常用于导入预训练模型,所以在尝试导入模型之前,确保模型目录已经存在,并且该目录中包含了正确的模型文件和配置文件。

    2023-10-12 18:24:06
    赞同 展开评论
  • 面对过去,不要迷离;面对未来,不必彷徨;活在今天,你只要把自己完全展示给别人看。

    在使用ModelScope导出CV模型时,您可能会遇到AttributeError: 'dict' object has no attribute 'model_dir'的错误。这是因为ModelScope使用了一个字典对象来存储模型配置,而这个字典对象没有model_dir属性。
    为了解决这个问题,您可以尝试使用ModelScope的from_pretrained方法的model_cfg参数来指定模型配置。例如:

    from modelscope.models import CVModel
    
    model = CVModel.from_pretrained(
        model_name_or_path='your_model_name_or_path',
        model_cfg={
            'model_dir': 'your_model_dir',
            'pretrained': True
        }
    )
    

    在这个例子中,model_cfg参数是一个字典对象,其中包含model_dirpretrained属性。model_dir属性指定了模型的保存目录,pretrained属性指定了是否使用预训练模型。

    2023-10-12 13:32:38
    赞同 1 展开评论