【问题标题】:Problem while loading git repo file into Tensor flow.load_model()将 git repo 文件加载到 Tensor flow.load_model() 时出现问题
【发布时间】:2021-08-18 11:18:22
【问题描述】:

我正在尝试将预训练的保存模型从 git 存储库获取到 python 中,并将它们作为保存的模型加载到 tf.load_model() 中以进行未来预测。尽管我成功连接并能够提取 repo 内容,但我的疑问是如何将内容文件加载到 tf 而不是使用路径加载?如果这不可能,在连接到我的 GitHub 帐户中存在的存储库后,我将如何获取存储库文件的路径?

我尝试将文件加载为路径,当然它无法读取它。寻找更好的方法

from credentials import *
from github import Github
import requests
g = Github("token for access")
repos = g.get_user().get_repos()
for repo in repos:
    print(repo)
    content = repo.get_contents(path)
    model = tf.keras.models.load_model(content[0],
                                   custom_objects=None,
                                   compile=True)

错误:

TypeError: expected str, bytes or os.PathLike object, not ContentFile

【问题讨论】:

    标签: python tensorflow github


    【解决方案1】:

    我认为您无法从 url 读取它。我猜你可以使用content[0].content 读取原始字节,但这也无济于事,因为load_model 需要一个文件路径:

    if (h5py is not None and
        (isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
        return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    

    如果您查看h5py docs,您只能从文件路径创建h5py.File 对象。

    我建议先将文件写入磁盘,然后再加载。也许是这样的? (未经测试)

    from pathlib import Path
    
    tmp = Path("/tmp/model.h5")
    tmp.write_bytes(content[0].content)
    model = tf.keras.models.load_model(tmp, custom_objects=None, compile=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 2019-05-25
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多