AttributeError: 'Tuple' Objekt hat kein Attribut 'as_list' bei Verwendung von tf.keras.inputPython

Python-Programme
Guest
 AttributeError: 'Tuple' Objekt hat kein Attribut 'as_list' bei Verwendung von tf.keras.input

Post by Guest »

Ich versuche, ResNet3D aus TensorFlow-Models Bibliothek zu verwenden, aber ich erhalte diesen seltsamen Fehler, wenn ich versuche, den Block
auszuführen

Code: Select all

!pip install tf-models-official==2.17.0
Nach der Installation von TF-Models-official

Code: Select all

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()
< /code>
Wenn ich dies ausführe, erhalte ich den Fehler unten: < /p>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in ()
37
38 # Create and display model
---> 39 model = create_model()
40 model.summary()

 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'
Ich habe versucht, die Eingabe als Liste an das Argument zu übergeben, aber dennoch den gleichen Fehler zu erhalten.
Der Fehler ist mit diesem < /p>
auftreten

Code: Select all

!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())
< /code>
Fehler: < /p>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in ()
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'

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post