How load a model and its constants through just importing a module?

断了今生、忘了曾经 提交于 2019-12-11 17:03:25

问题


I'd like to load a model automatically into a module by importing a module that loads the model and its constants into that module, somehow like this:

In model.py:

import pickle

from tensorflow import keras

def load_model_vars():
    return pickle.load(open('./file.pkl', 'rb'))

def load_model():
    return keras.models.load_model('./model.h5')

# model needs these two constants to be loaded successfully
a, b = load_model_vars()
model = load_model()

In another.py, where model.py will be imported into, and where the model will be used:

from model import *

if __name__ == '__main__':
        results = use_model(model, input)

However, I always encounter an error where the model can't find these two variables, and I can't figure out why.

I've tried playing around with the variables as global or local, but the model still can't find these two variables altogether, leading to not loading successfully.

Here's some of my attempts so far:

In model.py:

import pickle

from tensorflow import keras

def load_model()

    constants = pickle.load(open('./file.pkl', 'rb'))

    # these three doesn't work, and model throws an error saying these weren't defined; why?    
    a = constants[0]
    b = constants[1]

    globals()['a'] = constants[0]
    globals()['b'] = constants[1]

    # these causes another error
    locals()['a'] = constants[0]
    locals()['b'] = constants[1]

    globals()['model'] = keras.models.load_model('./model.h5')

load_model()

In another.py, the model still looks for the two variables and throws an error even if it was already loaded in the other module, and I can't find a way to point it.

Here's how I currently patch this problem:

In model.py:

import pickle

from tensorflow import keras

def load_model_vars():
    return pickle.load(open('./file.pkl', 'rb'))

def load_model():
    return keras.models.load_model('./model.h5')

In another.py:

from model import load_model_vars, load_model

if __name__ == '__main__':  
        a, b = load_model_vars()
        model = load_model()

        # model loaded from file by load_model() requires a and b to be initialized, 
        # or it will raise a NameError: name 'a' is not defined
        # model is then used here:
        results = use_model(model, input_data)

Basically, I expect there's a way to load both the model's needed variables and the model itself in a module, and expose the already loaded model for use in another module.

EDIT: I'd like to do to a model the same way this was done on these variables:

In a.py:

def load_variables():
    globals()['foo'] = 42
    globals()['bar'] = 21

load_variables()

In b.py:

import a

print(a.foo)

But the model needs the aforementioned variables to load successfully..

来源:https://stackoverflow.com/questions/57150156/how-load-a-model-and-its-constants-through-just-importing-a-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!