简体   繁体   English

升级到 Tensorflow 2.5 现在使用预训练的 Keras 应用程序模型时会出现 Lambda 层错误

[英]Upgraded to Tensorflow 2.5 now get a Lambda Layer error when using pretrained Keras Applications Models

I followed this tutorial to build a siamese network for my problem.我按照本教程为我的问题构建了一个连体网络。 I was using Tensorflow 2.4.1 and now upgraded我使用的是 Tensorflow 2.4.1 现在升级了

This code worked wonderfully before这段代码以前工作得很好

base_cnn = resnet.ResNet50(
    weights="imagenet", input_shape=target_shape + (3,), include_top=False
)

flatten = layers.Flatten()(base_cnn.output)
dense1 = layers.Dense(512, activation="relu")(flatten)
dense1 = layers.BatchNormalization()(dense1)
dense2 = layers.Dense(256, activation="relu")(dense1)
dense2 = layers.BatchNormalization()(dense2)
output = layers.Dense(256)(dense2)

embedding = Model(base_cnn.input, output, name="Embedding")

trainable = False
for layer in base_cnn.layers:
    if layer.name == "conv5_block1_out":
        trainable = True
    layer.trainable = trainable

Now each resnet layer or mobilenet or efficient net (tried them all) throws these errors:现在每个 resnet 层或 mobilenet 或高效网络(都尝试过)都会抛出这些错误:

WARNING:tensorflow:
The following Variables were used a Lambda layer's call (tf.nn.convolution_620), but
are not present in its tracked objects:
  <tf.Variable 'stem_conv/kernel:0' shape=(3, 3, 3, 48) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.

It compiles and seems to fit.它编译并且似乎适合。

But do we have to initialize the models somewhat differently in 2.5?但是我们必须在 2.5 中以不同的方式初始化模型吗?

Thanks for any pointers!感谢您的任何指点!

Here there is no need to revert back to TF2.4.1 .此处无需恢复到TF2.4.1 I would always recommend try with latest version because it addressed many of the performance issues and new features.我总是建议尝试使用最新版本,因为它解决了许多性能问题和新功能。

I was able to execute above code without any issues in TF2.5 .我能够在TF2.5中执行上述代码而没有任何问题。

import tensorflow as tf
print(tf.__version__)
from tensorflow.keras.applications import ResNet50
from tensorflow.keras import layers, Model


img_width, img_height = 224, 224
target_shape = (img_width, img_height, 3)


base_cnn = ResNet50(
    weights="imagenet", input_shape=target_shape, include_top=False
)

flatten = layers.Flatten()(base_cnn.output)
dense1 = layers.Dense(512, activation="relu")(flatten)
dense1 = layers.BatchNormalization()(dense1)
dense2 = layers.Dense(256, activation="relu")(dense1)
dense2 = layers.BatchNormalization()(dense2)
output = layers.Dense(256)(dense2)

embedding = Model(base_cnn.input, output, name="Embedding")

trainable = False
for layer in base_cnn.layers:
    if layer.name == "conv5_block1_out":
        trainable = True
    layer.trainable = trainable

Output: Output:

2.5.0
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
94773248/94765736 [==============================] - 1s 0us/step

As per @Olli, Restarting and clearing the session the kernel has resolved the problem.根据@Olli,重新启动并清除 session kernel 已解决问题。

I'm not sure what's the main reason for your issue as it's not reproducible generally.我不确定您的问题的主要原因是什么,因为它通常无法重现。 But here are some notes about that warning message.但这里有一些关于该警告信息的说明。 The traceback shown in your question is not from ResNet but from EfficientNet .您的问题中显示的回溯不是来自ResNet而是来自EfficientNet

Now, we know that the Lambda layer exists so that arbitrary expressions can be used as a Layer when constructing Sequential and Functional API models.现在,我们知道存在Lambda层,因此在构建顺序功能API 模型时,可以使用任意表达式作为Layer Lambda layers are best suited for simple operations or quick experimentation. Lambda层最适合简单操作或快速实验。 While it is possible to use Variables with Lambda layers, this practice is discouraged as it can easily lead to bugs .虽然可以将变量与 Lambda 层一起使用,但不鼓励这种做法,因为它很容易导致错误 For example:例如:

import tensorflow as tf 

x_input = tf.range(12.).numpy().reshape(-1, 4)
weights = tf.Variable(tf.random.normal((4, 2)), name='w')
bias = tf.ones((1, 2), name='b')

# lambda custom layer
mylayer1 = tf.keras.layers.Lambda(lambda x: tf.add(tf.matmul(x, weights),
                                                           bias), name='lambda1')
mylayer1(x_input)
WARNING:tensorflow:
The following Variables were used a Lambda layer's call (lambda1), but
are not present in its tracked objects:
  <tf.Variable 'w:0' shape=(4, 2) dtype=float32, numpy=
array([[-0.753139  , -1.1668463 ],
       [-1.3709341 ,  0.8887151 ],
       [ 0.3157893 ,  0.01245957],
       [-1.3878908 , -0.38395467]], dtype=float32)>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.
<tf.Tensor: shape=(3, 2), dtype=float32, numpy=
array([[ -3.903028 ,   0.7617702],
       [-16.687727 ,  -1.8367348],
       [-29.472424 ,  -4.43524  ]], dtype=float32)>

It's because the mylayer1 layer doesn't trace the tf.Variables directly and so that those parameter won't appear in mylayer1.trainable_weights .这是因为mylayer1层不直接跟踪tf.Variables ,因此这些参数不会出现在mylayer1.trainable_weights中。

mylayer1.trainable_weights
[]

In general, Lambda layers can be convenient for simple stateless computation , but anything more complex should use a subclass Layer instead.一般来说, Lambda层可以方便简单的无状态计算,但更复杂的应该使用子类层来代替。 From your traceback, it seems like there can be such a possible scenario with the step_conv layer.从您的回溯来看, step_conv层似乎可能存在这种情况。

for layer in EfficientNetB0(weights=None).layers:
    if layer.name == 'stem_conv':
        print(layer)
<tensorflow.python.keras.layers.convolutional.Conv2D object.. 

Quick surveying on source code of tf.compat.v1.nn.conv2d , lead to a lambda expression that might be the cause.快速调查tf.compat.v1.nn.conv2d的源代码,导致可能是原因的lambda 表达式

pip install tensorflow==2.3.0, worked for me instead of tf 2.5 I was facing the issue related to using Lambda layer pip 安装 tensorflow==2.3.0,为我工作而不是 tf 2.5 我遇到了与使用 Lambda 层相关的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Tensorflow Keras API从预训练的模型复制特定的图层权重? - How do I copy specific layer weights from pretrained models using Tensorflow Keras api? TensorFlow、Keras:替换预训练 model 中的激活层 - TensorFlow, Keras: Replace Activation layer in pretrained model 使用Tensorflow服务提供预训练的keras接收模型时发生失败的前提条件错误 - Failed precondition error when using Tensorflow serving to serve pretrained keras xception model 在Keras中使用Lambda层时的符号张量值错误 - Symbolic tensor value error when using Lambda layer in Keras 如果然后在 keras/tensorflow 中执行的 Lambda 层 - Lambda layer to perform if then in keras/tensorflow 使用预训练模型(Keras、Tensorflow)的 Mask R-CNN、Faster R-CNN 的最佳图像大小 - Optimal image sizes for Mask R-CNN, Faster R-CNN, using pretrained models (Keras, Tensorflow) 如何在Keras模型Lambda层中预处理字符串? - How to preprocess strings in Keras models Lambda layer? TensorFlow / Keras:如何从Keras的应用程序模块中获取缺失的模型(ResNet101,ResNeXt等)? - TensorFlow/Keras: How to get missing models (ResNet101, ResNeXt, etc.) from Keras' applications module? 如何检查Tensorflow中Keras是否捕获了lambda层 - How to check if lambda layer is captured by Keras in Tensorflow 即使在Keras中使用Lambda层,也没有属性&#39;_inbound_nodes&#39;错误 - no attribute '_inbound_nodes' error even when using Lambda layer in Keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM