【发布时间】:2021-12-12 00:15:20
【问题描述】:
我想使用Segmentation_Models UNet(带有 ResNet34 Backbone)进行不确定性估计,所以我想在上采样部分添加一些 Dropout 层。模型不是顺序的,所以我认为我必须将一些输出重新连接到新的 Dropout 层,并将下一层输入重新连接到 Dropout 的输出。
我不确定,这样做的正确方法是什么。我目前正在尝试这个:
# create model
model = sm.Unet('resnet34', classes=1, activation='sigmoid', encoder_weights='imagenet')
# define optimizer, loss and metrics
optim = tf.keras.optimizers.Adam(0.001)
total_loss = sm.losses.binary_focal_dice_loss # or sm.losses.categorical_focal_dice_loss
metrics = ['accuracy', sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)]
# get input layer
updated_model_layers = model.layers[0]
# iterate over old model and add Dropout after given Convolutions
for layer in model.layers[1:]:
# take old layer and add to new Model
updated_model_layers = layer(updated_model_layers.output)
# after some convolutions, add Dropout
if layer.name in ['decoder_stage0b_conv', 'decoder_stage0a_conv', 'decoder_stage1a_conv', 'decoder_stage1b_conv', 'decoder_stage2a_conv',
'decoder_stage2b_conv', 'decoder_stage3a_conv', 'decoder_stage3b_conv', 'decoder_stage4a_conv']:
if (uncertain):
# activate dropout in predictions
next_layer = Dropout(0.1) (updated_model_layers, training=True)
else:
# add dropout layer
next_layer = Dropout(0.1) (updated_model_layers)
# add reconnected Droput Layer
updated_model_layers = next_layer
model = Model(model.layers[0], updated_model_layers)
这会引发以下错误:AttributeError: 'KerasTensor' object has no attribute 'output'
但我认为我做错了什么。有人对此有解决方案吗?
【问题讨论】:
标签: python tensorflow machine-learning keras tf.keras