Problem while loading git repo file into Tensor flow.load_model()

Viewed 49

I am trying to get the pre-trained saved models from git repository into python and load them as the saved models into the tf.load_model() to future prediction. All though I successfully connected and able to pull the repo contents but my doubt is how do I load the content files into tf rather then loading using path? If that's not possible how would I get the path of the repository file once after connecting to the repositories present in my GitHub account?

I tried to load the files as path, of course it cant read it. Looking for a better way of doing it

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)

Error:

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

I don't think you can read it from a url. I would have guessed you could read the raw bytes by using content[0].content, but that won't help either, because load_model expects a file path:

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)

And if you look at h5py docs, you can only create an h5py.File object from a filepath.

I would suggest writing the file to disk first, before loading it. Maybe something like this? (untested)

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)
Related