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)
modelpile(
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
/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'
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'
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
/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'
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'
This is indeed a bit tricky as several things here are mixed on the tf and keras level.
At best you use the model factories and setups via configs
The main problem here is that you should not pass a layer here but an Input tensor created from tf_keras.Input
.
Below I pointed you out with two ways to set up your model:
import tf_keras
def create_model():
input_specs = tf.keras.layers.InputSpec(shape=(None, None, IMG_SIZE, IMG_SIZE, 3))
# Setup Backbone
backbone = 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=input_specs,
)
# Variant 1 use functional API yourself
inputs = tf_keras.Input(shape=input_specs.shape[1:], name=input_specs.name)
endpoints = backbone(inputs)
x = endpoints[max(endpoints.keys())] # <- Use your own function API from here
... # set up your head and model manually
# -- OR ---
# Variant 2 Use a Classification Model with your backbone
base_model = tfm.vision.classification_model.ClassificationModel(
backbone=backbone,
num_classes=NUM_CLASSES,
input_specs=input_specs,
dropout_rate=0.3,
skip_logits_layer=False, # set to true to skip Droputout and final Dense Layer
add_head_batch_norm=True, # adds a batchNorm by default
)
tensorflow-models
library. Rather, it is related totf.keras.Input
. That being said, I would be grateful, if you could help me with this issue. – Siladittya Commented Feb 4 at 15:07