我想使用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'
但我覺得我做錯了什么。有人有解決方案嗎?
uj5u.com熱心網友回復:
您使用的 Resnet 模型存在問題。它很復雜,并且具有 Add 和 Concatenate 層(我猜是剩余層),它們將來自幾個“子網”的張量串列作為輸入。換句話說,網路不是線性的,所以你不能用一個簡單的回圈來遍歷模型。
關于您的錯誤,在您的代碼回圈中: layer 是一個層, updated_model_layers 是一個張量(功能 API)。因此,updated_model_layers.output 不存在。你把兩者混淆了一點
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/339247.html
