Correct way to load multiple models in memory using Tensorflow 2

Viewed 438

I am trying to: Train, Save and Load different models in TensorFlow2. What I have issue with is to use a model that I have in memory, so I do not have to load it from file (which takes some time) the Class I have to train load and store is:

class TfTest():
def __init__(self, tf_graph, n_features=5, n_classes=3, n_samples=1000, seed=0, name="default"):
    self.tf_graph = tf_graph
    self.n_features = n_features
    self.n_classes = n_classes
    self.n_samples = n_samples
    self.seed = seed
    self.name = name
    self._model = None

def produce_model(self):
    X, y = make_classification(n_samples=self.n_samples,
                               n_features=self.n_features,
                               n_informative=self.n_features - 1,
                               n_redundant=0,
                               n_repeated=0,
                               n_classes=self.n_classes,
                               random_state=self.seed,
                               shuffle=True)

    train_samples = int(0.8 * self.n_samples)  # Samples used for training the models

    self.X_train = X[:train_samples]
    self.X_test = X[train_samples:]
    self.y_train = y[:train_samples]
    self.y_test = y[train_samples:]

    with self.tf_graph.as_default():
        # model is in memory
        self._model = tf.keras.Sequential([
            tf.keras.layers.Dense(10, activation='relu', input_shape=[self.n_features, ]),
            tf.keras.layers.Dense(15, activation='relu'),
            tf.keras.layers.Dense(10, activation='relu'),
            tf.keras.layers.Dense(self.n_classes, activation='softmax')
        ])
        self._model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
        history = self._model.fit(self.X_train, self.y_train, epochs=100, verbose=0)

        print(self.name + " test Results")
        self._model.evaluate(self.X_test, self.y_test)
        # Save the model
        MODEL_RAND = MODELS_DIR + self.name
        if not os.path.exists(MODEL_RAND):
            os.mkdir(MODEL_RAND)
        self._model.save(MODEL_RAND)

def load_model(self):
    # load the model from file
    MODEL_RAND = MODELS_DIR + self.name
    with self.tf_graph.as_default():
        rand_model = tf.keras.models.load_model(MODEL_RAND)
        print(self.name + " loaded Results")
        rand_model.evaluate(self.X_test, self.y_test)

def use_local_model(self):
    # todo: handle case the model is not in memory
    with self.tf_graph.as_default():
        print(self.name + " local Results")
        self._model.evaluate(self.X_test, self.y_test)

and the python script is that uses this class is the following: The function produce_model actually train the model, puts it in memory and saves it to file.

The function load_model get the model stored in the file (I tried also using this function in a different file)

The function use_local_model should use the model that is in memory, now it assumes the model is already in memory (that is true for this script) but later it will load the model from file and put in memory. This is the function that generates the error

import os
from sklearn.datasets import make_classification
import tensorflow as tf

MODELS_DIR = 'models/'
if not os.path.exists(MODELS_DIR):
    os.mkdir(MODELS_DIR)

g1 = tf.Graph()
g2 = tf.Graph()
g3 = tf.Graph()
g4 = tf.Graph()

print("TensorFlow version: {}".format(tf.__version__))
print("Eager execution: {}".format(tf.executing_eagerly()))

# Class definition

model1 = TfTest(g1, n_features=5, n_classes=3, seed=1, name="test1")
model2 = TfTest(g2, n_features=30, n_classes=2, seed=2, name="test2")
model3 = TfTest(g3, n_features=6, n_classes=6, seed=3, name="test3")
model4 = TfTest(g4, n_features=20, n_classes=3, seed=4, name="test4")

model1.produce_model()
model2.produce_model()
model3.produce_model()
model4.produce_model()

model1.load_model()
model2.load_model()
model3.load_model()
model4.load_model()

model1.use_local_model()

The error I have is:

Anaconda3\lib\site-packages\tensorflow_core\python\client\session.py", line 1474, in __call__
    run_metadata_ptr)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_2/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/dense_2/kernel/class tensorflow::Var does not exist.
     [[{{node dense_2/MatMul/ReadVariableOp}}]]

Process finished with exit code 1
0 Answers
Related