在Keras中为Segmentation_Models Resnet34添加Dropout层

我想使用Segmentation_Models UNet(使用ResNet34作为骨干网络)来进行不确定性估计,因此我想在上采样部分添加一些Dropout层。由于模型不是顺序模型,我认为我需要重新连接一些输出到新的Dropout层,并将后续层的输入连接到Dropout的输出上。

我还不确定正确的做法是什么。目前我正在尝试这样做:

# 创建模型model = sm.Unet('resnet34', classes=1, activation='sigmoid', encoder_weights='imagenet')# 定义优化器、损失函数和指标optim = tf.keras.optimizers.Adam(0.001)total_loss = sm.losses.binary_focal_dice_loss # 或者 sm.losses.categorical_focal_dice_lossmetrics = ['accuracy', sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)]# 获取输入层updated_model_layers = model.layers[0]# 遍历旧模型并在给定的卷积层后添加Dropoutfor layer in model.layers[1:]:    # 取旧层并添加到新模型    updated_model_layers = layer(updated_model_layers.output)    # 在某些卷积层后添加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):            # 在预测中激活dropout            next_layer = Dropout(0.1) (updated_model_layers, training=True)        else:            # 添加dropout层            next_layer = Dropout(0.1) (updated_model_layers)        # 添加重新连接的Dropout层        updated_model_layers = next_layermodel = Model(model.layers[0], updated_model_layers)

这会抛出以下错误: AttributeError: 'KerasTensor' object has no attribute 'output'

但我认为我做错了什么。有人有解决方案吗?


回答:

你使用的Resnet模型存在问题。它很复杂,包含Add和Concatenate层(我猜是残差层),这些层从多个“子网络”中接收张量列表作为输入。换句话说,网络不是线性的,所以你不能用一个简单的循环遍历模型。

关于你的错误,在你的代码循环中:layer是一个层,而updated_model_layers是一个张量(函数式API)。因此,updated_model_layers.output不存在。你有点混淆了这两者。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注