将keras模型另存为.h5 [英] Save keras model as .h5

查看:422
本文介绍了将keras模型另存为.h5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将训练有素的keras模型另存为.h5文件.应该直截了当. 简短示例:

I want to save my trained keras model as .h5 file. Should be straight forward. Short example:

#%%
import tensorflow as tf
import numpy as np
from tensorflow.keras.callbacks import ModelCheckpoint
import matplotlib.pyplot as plt

print('TF version: ',tf.__version__)

#%%
#########################
# BATCH SIZE
BATCH_SIZE=100
########################

# create training data
X_train_set = np.random.random(size=(10000,10))
y_train_set = np.random.random(size=(10000))

# create validation data
X_val_set = np.random.random(size=(100,10))
y_val_set = np.random.random(size=(100))

# convert np.array to dataset
train_dataset = tf.data.Dataset.from_tensor_slices((X_train_set, y_train_set))
val_dataset = tf.data.Dataset.from_tensor_slices((X_val_set, y_val_set))

# batching
train_dataset=train_dataset.batch(BATCH_SIZE)
val_dataset = val_dataset.batch(BATCH_SIZE)

# set up the model
my_model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(10,)),
    tf.keras.layers.Dense(100, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(1)
])

#%%
# custom optimizer with learning rate
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
    initial_learning_rate=1e-2,
    decay_steps=10000,
    decay_rate=0.9)
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)


# compile the model
my_model.compile(optimizer=optimizer,loss='mse')

# define a checkpoint
checkpoint = ModelCheckpoint('./tf.keras_test',
                             monitor='val_loss',
                             verbose=1,
                             save_best_only=True,
                             mode='min',
                             save_freq='epoch')

callbacks = [checkpoint]

#%%
# train with datasets
history= my_model.fit(train_dataset,
             validation_data=val_dataset,
             #validation_steps=100,
             #callbacks=callbacks,
             epochs=10)

# save as .h5
my_model.save('my_model.h5',save_format='h5')

但是,my_model.save给了我一个TypeError:

Traceback (most recent call last):
  File "/home/max/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-a369340a62e1>", line 1, in <module>
    my_model.save('my_model.h5',save_format='h5')
  File "/home/max/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 975, in save
    signatures, options)
  File "/home/max/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/save.py", line 112, in save_model
    model, filepath, overwrite, include_optimizer)
  File "/home/max/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 109, in save_model_to_hdf5
    save_weights_to_hdf5_group(model_weights_group, model_layers)
  File "/home/max/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 631, in save_weights_to_hdf5_group
    param_dset = g.create_dataset(name, val.shape, dtype=val.dtype)
  File "/usr/local/lib/python3.6/dist-packages/h5py/_hl/group.py", line 143, in create_dataset
    if '/' in name:
TypeError: a bytes-like object is required, not 'str'

不确定是什么问题...这是TF2问题吗?使用TF1.X保存为.h5从来没有问题,并且仍然可以将其保存为.pb图.但是,我想将其作为.h5.

Not sure what's the problem... Is it a TF2 issue? Never had problems saving as .h5 with TF1.X and still can save it as .pb graph. However, I'd like to have it as .h5.

推荐答案

所以这似乎是 h 在h5py库中,它应该接受bytes或unicode str,但对于str实例将失败.它应该在下一个版本中修复.

So this seems to be a a bug in the h5py library, it should accept a bytes or a unicode str, but fails with a str instance. It should be fixed in the next release.

您可以在本地安装中降级h5py版本,它应该可以解决此问题.该问题是3.0.0版引入的,因此较早的版本应该可以使用.

You could downgrade the h5py version in your local installation and it should work around the problem. The problem was introduced by version 3.0.0, so earlier versions should work.

这篇关于将keras模型另存为.h5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆