[Python] AttributeError: 'tuple' object has no attribute 'as_list' when using tf.keras.Input

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use ResNet3D from tensorflow-models library but I am getting this weird error when trying to run the block

    !pip install tf-models-official==2.17.0


    Tensorflow version is 2.18 on the Kaggle notebook.

    After installing tf-models-official

    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()


    When I run this, I get the error below:

    ---------------------------------------------------------------------------
    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
    21 x = GlobalAveragePooling3D(data_format="channels_last", keepdims=False)(x)
    22 x = Dense(1024, activation='relu')(x)

    /usr/local/lib/python3.10/dist-packages/tf_keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs)
    59 def error_handler(*args, **kwargs):
    60 if not tf.debugging.is_traceback_filtering_enabled():
    ---> 61 return fn(*args, **kwargs)
    62
    63 filtered_tb = None

    /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)
    589
    590 @doc_controls.doc_in_current_and_subclasses

    /usr/local/lib/python3.10/dist-packages/tf_keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs)
    59 def error_handler(*args, **kwargs):
    60 if not tf.debugging.is_traceback_filtering_enabled():
    ---> 61 return fn(*args, **kwargs)
    62
    63 filtered_tb = None

    /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'


    I have tried passing the input to the shape argument as a list, but still getting the same error.

    The error is occurring with this

    !pip install tf-models-official==2.17.0

    import tensorflow as tf

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


    Error:

    ---------------------------------------------------------------------------
    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'

    Continue reading...

Compartilhe esta Página