为什么 flatten() 在合作实验室中不起作用,而它在其他用户发布的 kaggle-notebook 中起作用?

Why flatten() is not working in co-lab whereas it worked in kaggle-notebook posted by other user?

我正在做一个肺炎检测项目。我已经查看了 kaggle 上相同的笔记本。有一个用户堆叠了两个预训练模型 densenet169 和 mobilenet。我从用户那里复制了整个 kaggle 笔记本,他没有得到任何错误,但是当我 运行 它在 google colab 中时,我在这部分得到了这个错误:

错误部分:

    from keras.layers.merge import concatenate
    from keras.layers import Input
    import tensorflow as tf

    input_shape = (224,224,3)
    input_layer = Input(shape = (224, 224, 3))
    
    #first model
    base_mobilenet = MobileNetV2(weights = 'imagenet', include_top = False, input_shape = input_shape)
    base_densenet = DenseNet169(weights = 'imagenet', include_top = False, input_shape = input_shape)
    
    for layer in base_mobilenet.layers:
        layer.trainable =  False
    for layer in base_densenet.layers:
        layer.trainable = False
        
    model_mobilenet = base_mobilenet(input_layer)
    model_mobilenet = GlobalAveragePooling2D()(model_mobilenet)
    output_mobilenet = Flatten()(model_mobilenet)
    
    model_densenet = base_densenet(input_layer)
    model_densenet = GlobalAveragePooling2D()(model_densenet)
    output_densenet = Flatten()(model_densenet)
    
    merged = tf.keras.layers.Concatenate()([output_mobilenet, output_densenet]) 
    
    x = BatchNormalization()(merged)
    x = Dense(256,activation = 'relu')(x)
    x = Dropout(0.5)(x)
    x = BatchNormalization()(x)
    x = Dense(128,activation = 'relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(1, activation = 'sigmoid')(x)
    stacked_model = tf.keras.models.Model(inputs = input_layer, outputs = x)

Error Traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-35-69c389bc7252> in <module>()
     18 model_mobilenet = base_mobilenet(input_layer)
     19 model_mobilenet = GlobalAveragePooling2D()(model_mobilenet)
---> 20 output_mobilenet = Flatten(data_format=None)(model_mobilenet)
     21 
     22 model_densenet = base_densenet(input_layer)

5 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
   1028         with autocast_variable.enable_auto_cast_variables(
   1029             self._compute_dtype_object):
-> 1030           outputs = call_fn(inputs, *args, **kwargs)
   1031 
   1032         if self._activity_regularizer:

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/core.py in call(self, inputs)
    672       # Full static shape is guaranteed to be available.
    673       # Performance: Using `constant_op` is much faster than passing a list.
--> 674       flattened_shape = constant_op.constant([inputs.shape[0], -1])
    675       return array_ops.reshape(inputs, flattened_shape)
    676     else:

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
    263   """
    264   return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 265                         allow_broadcast=True)
    266 
    267 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
    274       with trace.Trace("tf.constant"):
    275         return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 276     return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
    277 
    278   g = ops.get_default_graph()

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
    299 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
    300   """Implementation of eager constant."""
--> 301   t = convert_to_eager_tensor(value, ctx, dtype)
    302   if shape is None:
    303     return t

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     97   ctx.ensure_initialized()
---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
     99 
    100 

ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.

你有点混淆了你的导入。

这是您的代码的固定版本

from tensorflow.keras.layers import concatenate
from tensorflow.keras.layers import Input, GlobalAveragePooling2D, Flatten, BatchNormalization, Dense, Dropout
from tensorflow.keras.applications import MobileNetV2, DenseNet169
import tensorflow as tf

input_shape = (224,224,3)
input_layer = Input(shape = (224, 224, 3))

#first model
base_mobilenet = MobileNetV2(weights = 'imagenet', include_top = False, input_shape = input_shape)
base_densenet = DenseNet169(weights = 'imagenet', include_top = False, input_shape = input_shape)

for layer in base_mobilenet.layers:
    layer.trainable =  False
for layer in base_densenet.layers:
    layer.trainable = False

model_mobilenet = base_mobilenet(input_layer)
model_mobilenet = GlobalAveragePooling2D()(model_mobilenet)
output_mobilenet = Flatten()(model_mobilenet)

model_densenet = base_densenet(input_layer)
model_densenet = GlobalAveragePooling2D()(model_densenet)
output_densenet = Flatten()(model_densenet)

merged = tf.keras.layers.Concatenate()([output_mobilenet, output_densenet]) 

x = BatchNormalization()(merged)
x = Dense(256,activation = 'relu')(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
x = Dense(128,activation = 'relu')(x)
x = Dropout(0.5)(x)
x = Dense(1, activation = 'sigmoid')(x)
stacked_model = tf.keras.models.Model(inputs = input_layer, outputs = x)