无法创建组(名称已经存在)

问题描述 投票:0回答:2
import tensorflow as tf
from ..models.encoder import encoder_build
from ..models.decoder import decoder_build

def compute_attention_maps(inputs,name,upsample=False):

    attMap = tf.reduce_sum(tf.square(inputs),axis=-1,keepdims=True,name= str(name)+"reducSum") 
    if upsample:
        attMap = tf.keras.layers.UpSampling2D(size=(2, 2), 
                                              interpolation='bilinear',
                                              name = str(name)+"bilinear")(attMap)
    attMap = tf.squeeze(attMap,axis=-1,name = str(name)+"squeeze")
    attMap = tf.reshape(attMap,
                        (tf.shape(attMap)[0],tf.shape(attMap)[1]*tf.shape(attMap)[2]),
                        name = str(name)+"reshape")
    attMap = tf.nn.softmax(attMap, 
                           axis=-1,
                           name = str(name)+"spatialSoftmax")
    return attMap

def compute_mse(x,y,name):

    diff = tf.math.squared_difference(x,y,name = str(name)+"squError")
    diff = tf.reduce_mean(diff,axis=0, name = str(name)+"mean")
    diff = tf.reduce_sum(diff, name = str(name)+"sum")
    return diff

def compute_distillation(attention_inputs):
    inp1,inp2,inp3,inp4 = attention_inputs 

    attMap1          = compute_attention_maps(inp1,"attmap1_")
    attMap2_upsample = compute_attention_maps(inp2,"attmap2UP_",upsample=True)
    attMap2          = compute_attention_maps(inp2,"attmap2_")
    attMap3_upsample = compute_attention_maps(inp3,"attmap3UP_",upsample=True)
    attMap3          = compute_attention_maps(inp3,"attmap3_")
    attMap4          = compute_attention_maps(inp4,"attmap4_")

    distillation1 = compute_mse(attMap1,attMap2_upsample,"distil1_")
    distillation2 = compute_mse(attMap2,attMap3_upsample,"distil2_")
    distillation3 = compute_mse(attMap3,attMap4,"distil3_")
    return tf.math.add_n([distillation1,distillation2,distillation3], name="distill_loss")

if __name__ == '__main__':
    inputs = tf.keras.layers.Input(shape=(None, None, 3), name='image')
    encoderTuple = encoder_build(inputs) # import from encoder.py file
    attention_inputs = encoderTuple[1]
    outputs = decoder_build(encoderTuple) # import from decoder.py file
    model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
    model.add_loss(compute_distillation(attention_inputs))
    model.summary()
    model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.00001, clipnorm=0.001), 
                     loss='binary_crossentropy',
                     metrics=['accuracy'])
    model.fit(x = train_generator,
          epochs=epochs, 
          verbose=1, 
          callbacks=callbacks,
          validation_data=validation_generator, 
          shuffle=True)

我已经创建了用于车道检测的keras分割模型(https://arxiv.org/pdf/1908.00821.pdf)。我能够为每个时期编译,开始训练并保存模型,而没有任何错误。但是,如果我将自定义损失添加到模型model.add_loss(compute_distillation(attention_inputs))>>模型中,模型训练了1个时期,那么该模型没有保存并显示以下错误。如何解决此错误?

374/375 [============================>.] - ETA: 0s - loss: 4.4717 - acc: 0.9781Epoch 1/50
 78/78[============================>.] - ETA: 37:38 - val_loss: 4.5855 - val_acc: 0.9758
Epoch 00001: saving model to /workspace/work/enet_sad_naiveresize/snapshot/enetNRSAD_Tusimple_L_4.4718_VL_4.5855.h5
Traceback (most recent call last):
  File "/workspace/work/enet_sad_naiveresize/bin/train.py", line 82, in <module>
    shuffle=True)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py", line 727, in fit
    use_multiprocessing=use_multiprocessing)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_generator.py", line 603, in fit
    steps_name='steps_per_epoch')
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_generator.py", line 332, in model_iteration
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/callbacks.py", line 299, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/callbacks.py", line 968, in on_epoch_end
    self._save_model(epoch=epoch, logs=logs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/callbacks.py", line 1015, in _save_model
    self.model.save(filepath, overwrite=True)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py", line 1171, in save
    signatures)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/saving/save.py", line 109, in save_model
    model, filepath, overwrite, include_optimizer)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 103, in save_model_to_hdf5
    save_weights_to_hdf5_group(model_weights_group, model_layers)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 619, in save_weights_to_hdf5_group
    g = f.create_group(layer.name)
  File "/usr/local/lib/python3.6/dist-packages/h5py/_hl/group.py", line 68, in create_group
    gid = h5g.create(self.id, name, lcpl=lcpl, gcpl=gcpl)
  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 161, in h5py.h5g.create
ValueError: Unable to create group (name already exists)

从..models.encoder导入tensorflow作为tf从..models.decoder导入encoder_build进口解码器_build def compute_attention_maps(inputs,name,upsample = False):attMap = tf.reduce_sum(tf ....

tensorflow image-segmentation tf.keras h5py attention-model
2个回答

0
投票
问题是因为您正在通过调用其他函数(如compute_attention_maps和compute_mse)在compute_distillation函数中堆叠层(并错误命名)。如果您还没有命名,那么您将拥有类似的图层,并且即使在命名它们之后错误仍然存​​在,这是因为h5模型期望使用某种格式的名称,如此处https://www.gitmemory.com/issue/keras-team/keras/12195/523749332所述。一个好的解决方案是在compute_distilation函数中使用keras lambda层来创建attMap1,attt2等,或定义您自己的自定义AttentionMap层,如下所示。
© www.soinside.com 2019 - 2024. All rights reserved.