在Keras Tensorflow模型中使用ModelCheckpoint时出现ValueError

c7rzv4ha  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(348)

我尝试加载一个预先训练好的模型,然后删除最后几层,并将其用于新模型中的迁移学习,如下所示:

img_input = Input((96,96,3), name='img_input')

# PRETRAINED IMAGE MODEL
img_model = keras.models.load_model("img_model/saved_model")
img_model.load_weights('img_model/checkpoint/mdl_wts.hdf5')
img_model.trainable = False
for layer in img_model.layers:
    layer._name = layer.name + str("_img")
        
new_img_model = keras.Model(inputs=img_model.input, outputs=img_model.layers[-4].output, name='img_model')
new_img_model = new_img_model(img_input)

img = Dense(128)(new_img_model)
img = Dense(16)(img)
img = ELU()(img)

# Output
output = Dense(1, activation='relu', name='Final_Output')(img)

模型开始训练,但在第一个时期结束时,我得到以下错误:

File "/home/al/tf/train.py", line 68, in <module>
    model.fit(train_dataset,
  File "/home/al/anaconda3/envs/tf/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/al/anaconda3/envs/tf/lib/python3.9/site-packages/h5py/_hl/group.py", line 149, in create_dataset
    dsid = dataset.make_new_dset(group, shape, dtype, data, name, **kwds)
  File "/home/al/anaconda3/envs/tf/lib/python3.9/site-packages/h5py/_hl/dataset.py", line 142, in make_new_dset
    dset_id = h5d.create(parent.id, name, tid, sid, dcpl=dcpl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5d.pyx", line 87, in h5py.h5d.create
ValueError: Unable to create dataset (name already exists)

我认为这与我的ModelCheckpoint回调有关:

tf.keras.callbacks.ModelCheckpoint('checkpoint/mdl_wts-{epoch:02d}-{val_loss:.2f}.h5', save_best_only=True, monitor='val_loss', mode='min')

如果我从新模型中排除加载/预训练的模型,我不会得到错误,所以它显然与此有关,但我不知道为什么。

hgb9j2n6

hgb9j2n61#

我设法通过在ModelCheckpoint中将.h5更改为.tf来修复此问题。

相关问题