You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TensorFlow ResNet3D骨干模型时出现AttributeError: 'tuple' object has no attribute 'as_list'的问题求助

使用TensorFlow ResNet3D骨干模型时出现AttributeError: 'tuple' object has no attribute 'as_list'的问题求助

大家好,我在尝试用tensorflow-models库中的ResNet3D模型时遇到了一个奇怪的错误,想请各位帮忙看看。

我的环境是Kaggle notebook,TensorFlow版本是2.18,先执行了安装命令:

!pip install tf-models-official==2.17.0

之后我写了创建模型的代码:

from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling3D, Input
from tensorflow.keras.optimizers import AdamW
import tensorflow_models as tfm

def create_model():
    base_model = tfm.vision.backbones.ResNet3D(model_id = 50,
        temporal_strides= [3,3,3,3],
        temporal_kernel_sizes = [(5,5,5),(5,5,5,5),(5,5,5,5,5,5),(5,5,5)],
        input_specs=tf.keras.layers.InputSpec(shape=(None, None, IMG_SIZE, IMG_SIZE, 3))
    )
    
    # Unfreeze the base model layers
    base_model.trainable = True
    
    # Create the model
    inputs = Input(shape=[None, None, IMG_SIZE, IMG_SIZE, 3])
    x = base_model(inputs) # B,1,7,7,2048
    x = GlobalAveragePooling3D(data_format="channels_last", keepdims=False)(x)
    x = Dense(1024, activation='relu')(x)
    x = tf.keras.layers.Dropout(0.3)(x)  # Add dropout to prevent overfitting
    outputs = Dense(NUM_CLASSES, activation='softmax')(x)
    
    model = Model(inputs, outputs)
    
    # Compile the model with class weights
    optimizer = AdamW(learning_rate=1e-4, weight_decay=1e-5)
    model.compile(
        optimizer=optimizer,
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy', tf.keras.metrics.AUC()]
    )
    
    return model

# Create and display model
model = create_model()
model.summary()

运行这段代码后,我得到了如下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-56-363271b4dda8> in <cell line: 39>()
     37 
     38 # Create and display model
---> 39 model = create_model()
     40 model.summary()

<ipython-input-56-363271b4dda8> in create_model()
     18     # Create the model
     19     inputs = Input(shape=(None, None, IMG_SIZE, IMG_SIZE, 3))
---> 20     x = base_model(inputs) # B,1,7,7,2048

/usr/local/lib/python3.10/dist-packages/tf_keras/src/engine/training.py in __call__(self, *args, **kwargs)
    586             layout_map_lib._map_subclass_model_variable(self, self._layout_map)
    587 
---> 588         return super().__call__(*args, **kwargs)

/usr/local/lib/python3.10/dist-packages/tf_keras/src/engine/base_layer.py in __call__(self, *args, **kwargs)
   1101             training=training_mode,
   1102         ):
-> 1103             input_spec.assert_input_compatibility(
   1104                 self.input_spec, inputs, self.name
   1105             )

/usr/local/lib/python3.10/dist-packages/tf_keras/src/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    300                             "incompatible with the layer: "
    301                             f"expected shape={spec.shape}, "
--> 302                             f"found shape={display_shape(x.shape)}"
    303                         )
    304 

/usr/local/lib/python3.10/dist-packages/tf_keras/src/engine/input_spec.py in display_shape(shape)
    305 
    306 def display_shape(shape):
--> 307     return str(tuple(shape.as_list()))
    308 
    309 

AttributeError: 'tuple' object has no attribute 'as_list'

我尝试把Input的shape参数改成列表形式,但还是出现同样的错误。甚至我单独测试这段简单的代码也会报错:

import tensorflow as tf

inputs = tf.keras.Input(shape=[None, None, IMG_SIZE, IMG_SIZE, 3])
print(inputs.shape.as_list())

错误信息是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-39-6e88680ff7df> in <cell line: 2>()
      1 inputs = tf.keras.Input(shape=[None, None, IMG_SIZE, IMG_SIZE, 3])
----> 2 print(inputs.shape.as_list())

AttributeError: 'tuple' object has no attribute 'as_list'

有没有大佬知道这是哪里出问题了?怎么解决呢?

备注:内容来源于stack exchange,提问作者Siladittya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 08:43:03