【问题标题】:Save keras model as .h5将 keras 模型另存为 .h5
【发布时间】:2021-02-18 06:33:32
【问题描述】:

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

#%%
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

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    所以这似乎是 h5py 库中的 bug,它应该接受 bytes 或 unicode str,但由于 str 实例而失败。它应该在下一个版本中修复。

    您可以在本地安装中降级 h5py 版本,它应该可以解决该问题。该问题是 3.0.0 版本引入的,因此早期版本应该可以工作。

    【讨论】:

    • 坦克,这是我的问题。我已经安装了 h5py > 3,然后我降级到版本 2.10.0
    猜你喜欢
    • 2019-08-04
    • 2021-05-16
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 2020-09-10
    • 2019-08-29
    • 2019-12-05
    • 2019-09-29
    相关资源
    最近更新 更多