在Keras中保存模型时引发“无法创建组(名称已存在)”错误

时间:2019-04-07 03:20:06

标签: python tensorflow keras deep-learning

我正在使用来自keras.applications的ResNet50和DenseNet121构建模型融合,但是在保存模型时会出现错误。 如果我仅使用ResNet50和DenseNet121的一个网络,例如仅DenseNet,则没问题

与ResNet50和DenseNet121融合:

img_input = Input(shape=input_shape)

densenet = app.DenseNet121(
    include_top=False,
    input_tensor=img_input,
    input_shape=input_shape,
    weights=base_weights)
resnet = app.ResNet50(
    include_top=False,
    input_tensor=img_input,
    input_shape=input_shape,
    weights=base_weights)

x1 = densenet.output
x1 = GlobalAveragePooling2D(name='dn_gap_last')(x1)
# then x1.shape is (batch, 1024)

x2 = resnet.output
x2 = Flatten()(x2)  # then x2.shape is (batch, 2048)

x = concatenate([x1, x2], axis=-1)
predictions = Dense(len(class_names), activation="sigmoid", name="predictions")(x)
model = Model(inputs=img_input, outputs=predictions)

并通过ModelCheckpoint保存模型

checkpoint = ModelCheckpoint(
                 output_weights_path,
                 save_weights_only=True,
                 save_best_only=True,
                 verbose=1,
            )

但是保存mdoel时会出现错误

Epoch 00001: val_loss improved from inf to 0.72018, saving model to ./experiments/8/weights.h5
Traceback (most recent call last):
  File "train.py", line 229, in <module>
    main()
  File "train.py", line 212, in main
    shuffle=False,
  File "/home/hqt/chest-x-ray-project/code/venv/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/hqt/chest-x-ray-project/code/venv/lib/python3.6/site-packages/keras/engine/training.py", line 2280, in fit_generator
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "/home/hqt/chest-x-ray-project/code/venv/lib/python3.6/site-packages/keras/callbacks.py", line 77, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "/home/hqt/chest-x-ray-project/code/venv/lib/python3.6/site-packages/keras/callbacks.py", line 445, in on_epoch_end
    self.model.save_weights(filepath, overwrite=True)
  File "/home/hqt/chest-x-ray-project/code/venv/lib/python3.6/site-packages/keras/engine/topology.py", line 2607, in save_weights
    save_weights_to_hdf5_group(f, self.layers)
  File "/home/hqt/chest-x-ray-project/code/venv/lib/python3.6/site-packages/keras/engine/topology.py", line 2878, in save_weights_to_hdf5_group
    g = f.create_group(layer.name)
  File "/home/hqt/chest-x-ray-project/code/venv/lib/python3.6/site-packages/h5py/_hl/group.py", line 50, in create_group
    gid = h5g.create(self.id, name, lcpl=lcpl)
  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/h5g.pyx", line 151, in h5py.h5g.create
ValueError: Unable to create group (name already exists)

0 个答案:

没有答案