코어랩
코어랩

Reputation: 89

Error loading Tensorflow keras Model (.h5)

I trained a number image and made a model file.

The corresponding sauce is as follows.

import os
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical 
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense 
from tensorflow.keras.models import load_model
import cv2
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Flatten, Convolution2D, MaxPooling2D
from tensorflow.keras.layers import Dropout, Activation, Dense
from tensorflow.keras.layers import Conv2D

os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'

path = '/Users/animalman/Documents/test/cnn/'

trainPath = os.listdir(path+'train')
testPath = os.listdir(path+'test')

categories = ["5"]
length = len(categories)

width = 28
height = 28
label = [1 for i in range(length)]
X = []
Y = []
for idx, categorie in enumerate(categories):
    label = [0 for i in range(length)]
    label[idx] = 1
    
    fileDir = path + 'train' + '/' + categorie + '/'
    for t, dir, f in os.walk(fileDir):
        for filename in f:
            print(fileDir + filename)
            img = cv2.imread(fileDir + filename)
            img = cv2.resize(img, None, fx=width/img.shape[0],fy=height/img.shape[1])
            X.append(img)
            Y.append(label)
X = np.array(X)
Y = np.array(Y)
X_train, X_test,Y_train, Y_test = train_test_split(X,Y)
xy = (X_train, X_test, Y_train, Y_test)


X_train = X_train.astype("float") / 256
X_test  = X_test.astype("float")  / 256

model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=X_train.shape[1:], padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))

model.add(Conv2D(64, (3, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten()) 
model.add(Dense(512))  
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(length))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
    optimizer='rmsprop',
    metrics=['accuracy'])


hdf5_file = "./7obj-model.h5"
if os.path.exists(hdf5_file):
    model.load_weights(hdf5_file)
else:
    model.fit(X_train, Y_train, batch_size=32, epochs=1)
    model.save_weights(hdf5_file)

And then I brought in the model file that was saved.

loaded_model = tf.keras.models.load_model(hdf5_file)

However, this is where the error occurs. What's the reason?

Traceback (most recent call last): File "/Users/animalman/Documents/test/train.py", line 112, in loaded_model = tf.keras.models.load_model(hdf5_file) File "/Users/animalman/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py", line 206, in load_model return hdf5_format.load_model_from_hdf5(filepath, custom_objects, File "/Users/animalman/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 181, in load_model_from_hdf5 raise ValueError('No model found in config file.') ValueError: No model found in config file.

Upvotes: 7

Views: 22030

Answers (4)

Vaishnavi Kedar
Vaishnavi Kedar

Reputation: 1

give .keras instead of .h5

i.e model.keras

Upvotes: 0

Animesh Nayak
Animesh Nayak

Reputation: 107

model. save is geting me this error /usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via model.save(). This file format is considered legacy. We recommend using instead the native Keras format, e.g. model.save('my_model.keras'). saving_api.save_model(

Upvotes: 0

user11530462
user11530462

Reputation:

Adding to @Oscar response, for smaller and simple models, 'h5' format is sufficient but for complex models (Functional and subclassed) with custom_layers or custom metrics, it is better to save in 'tf' format (also called as SavedModel format)

Check here for more detailed guide on Keras webpage

Keras SavedModel format limitations:

The tracing done by SavedModel to produce the graphs of the layer call functions allows SavedModel be more portable than H5, but it comes with drawbacks.

Can be slower and bulkier than H5. Cannot serialize the ops generated from the mask argument (i.e. if a layer is called with layer(..., mask=mask_value), the mask argument is not saved to SavedModel). Does not save the overridden train_step() in subclassed models. Custom objects that use masks or have a custom training loop can still be saved and loaded from SavedModel, except they must override get_config()/from_config(), and the classes must be passed to the custom_objects argument when loading.

H5 limitations:

External losses & metrics added via model.add_loss() & model.add_metric() are not saved (unlike SavedModel). If you have such losses & metrics on your model and you want to resume training, you need to add these losses back yourself after loading the model. Note that this does not apply to losses/metrics created inside layers via self.add_loss() & self.add_metric(). As long as the layer gets loaded, these losses & metrics are kept, since they are part of the call method of the layer. The computation graph of custom objects such as custom layers is not included in the saved file. At loading time, Keras will need access to the Python classes/functions of these objects in order to reconstruct the model. See Custom objects. Does not support preprocessing layers.

Upvotes: 1

Oscar
Oscar

Reputation: 520

As mentioned in this post, your h5 file only contains weights. You need to save your model architecture in a json file and then use model_from_json, to load model configuration, hence, you can load weights with load_weights.

Another option may be to simply save your model (architecture + weights together) by replacing your last line by

model.save("model.h5")

and then to load, you can make use of

model = load_model('model.h5')

Upvotes: 4

Related Questions