【发布时间】:2021-08-24 21:30:17
【问题描述】:
我正在开展一个肺炎检测项目。我在 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)
错误回溯:
---------------------------------------------------------------------------
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.
【问题讨论】:
-
您正在混合使用 tf.keras 和不支持的 keras。
标签: tensorflow machine-learning keras google-colaboratory tensor