Praveen Kulkarni
Praveen Kulkarni

Reputation: 3251

tensorflow 2.1 loading model when saved with `tf` format does not work

Check the code below to create and save model Github Issue Try the code here

# import necessary modules
import tensorflow as tf
import tensorflow.keras as tk
print(tf.__version__)

# define a custom model
class MyModel(tk.Model):
    ...

# Define a simple sequential model
def create_model():
    a = tk.Input(shape=(32,))
    b = tk.layers.Dense(32)(a)
    model = MyModel(inputs=a, outputs=b)

    model.compile(optimizer='adam',
                loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
                metrics=['accuracy'])
    return model


# create model
my_model = create_model()

# Display the model's architecture
my_model.summary()

# save model
my_model.save(filepath="./saved_model", save_format="tf")

Now I want to load back the weights from disk. When I use the code below I get the error.

# load back model
my_model.load_weights(filepath="./saved_model")

This gives the error:

/usr/local/lib/python3.6/dist-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
    171         if swmr and swmr_support:
    172             flags |= h5f.ACC_SWMR_READ
--> 173         fid = h5f.open(name, flags, fapl=fapl)
    174     elif mode == 'r+':
    175         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.open()

OSError: Unable to open file (file read failed: time = Tue Apr 28 15:30:40 2020
, filename = './saved_model', file descriptor = 57, errno = 21, error message = 'Is a directory', buf = 0x7fff093afd50, total read size = 8, bytes this sub-read = 8, bytes actually read = 18446744073709551615, offset = 0)

But then I debugged and found the TensorFlow library confuses itself in thinking that it is an h5 file. So I modified the code as below and now it works. I also get to use my custom MyModel

Basically I added extra path i.e. \\variables\\variables so that it detects the folder as tf checkpoint. Can anyone suggest a better approach?

my_model.load_weights(filepath="./saved_model/variables/variables")
print(my_model.__class__)

The other option is to use tk.models.load(...) as in the code below. But, the problem is I lose my sub-classed model MyModel

_loaded_my_model = tk.models.load_model("./saved_model")
print(_loaded_my_model.__class__)

Upvotes: 0

Views: 1477

Answers (1)

Vishnuvardhan Janapati
Vishnuvardhan Janapati

Reputation: 3288

Actually you need to use load_model to load the model that was saved earlier using model.save. If you want to save_weights and then load the weights back then you can use model.load_weights.

So you need to comment out the last line and use load_model as shown below.

# model.load_weights(filepath="saved_model") #
loaded_model = tf.keras.models.load_model('./saved_model')

Full code is here.

Upvotes: 1

Related Questions