My code snippet is below -
from tensorflow.keras.applications import InceptionResNetV2
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout, Input, Resizing
inputs = Input(shape=x_train.shape[1:])
x = Conv2D(filters=3, kernel_size=(1,1), activation='relu')(inputs)
x = Resizing(height=75, width=75)(x)
base_model = InceptionResNetV2(input_shape=(75, 75, 3), include_top=False, input_tensor=x, weights='imagenet')
The expcetion I am getting is -
ValueError Traceback (most recent call last)
/var/folders/26/0fbl62dn7zsd3x5t5f3zh1mr0000gn/T/ipykernel_90240/1249320150.py in <module>
7 # x = Resizing(height=75, width=75)(x)
8
----> 9 base_model = InceptionResNetV2(input_shape=(75, 75, 3), include_top=False, input_tensor=x, weights='imagenet')
10 base_model.trainable = False
11
/opt/homebrew/Caskroom/miniforge/base/envs/pattern-recognition/lib/python3.9/site-packages/keras/applications/inception_resnet_v2.py in InceptionResNetV2(include_top, weights, input_tensor, input_shape, pooling, classes, classifier_activation, **kwargs)
246 cache_subdir='models',
247 file_hash='d19885ff4a710c122648d3b5c3b684e4')
--> 248 model.load_weights(weights_path)
249 elif weights is not None:
250 model.load_weights(weights)
/opt/homebrew/Caskroom/miniforge/base/envs/pattern-recognition/lib/python3.9/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
/opt/homebrew/Caskroom/miniforge/base/envs/pattern-recognition/lib/python3.9/site-packages/keras/saving/hdf5_format.py in load_weights_from_hdf5_group(f, model)
717 layer_names = filtered_layer_names
718 if len(layer_names) != len(filtered_layers):
--> 719 raise ValueError(
720 f'Layer count mismatch when loading weights from file. '
721 f'Model expected {len(filtered_layers)} layers, found '
ValueError: Layer count mismatch when loading weights from file. Model expected 449 layers, found 448 saved layers.
I have tried this with multiple models found here, however, they all fail with the same error, ie: the saved layers are one less than expected. Please let me know if there is a workaround.
CodePudding user response:
Interestingly, the error comes from the absence of an Input
layer.
This for example would work:
import tensorflow as tf
x_train = tf.random.normal((100, 128, 128, 3))
inputs = tf.keras.layers.Input(shape=x_train.shape[1:])
base_model = tf.keras.applications.InceptionResNetV2(include_top=False, input_tensor=inputs, weights='imagenet')
model = tf.keras.Model(inputs, base_model.output)
So maybe try something like this:
import tensorflow as tf
x_train = tf.random.normal((100, 128, 128, 3))
inputs = tf.keras.layers.Input(shape=x_train.shape[1:])
x = tf.keras.layers.Conv2D(filters=3, kernel_size=(1,1), activation='relu')(inputs)
x = tf.keras.layers.Resizing(height=75, width=75)(x)
base_model = tf.keras.applications.InceptionResNetV2(input_shape=(75, 75, 3), include_top=False, weights='imagenet')
outputs = base_model(x)
model = tf.keras.Model(inputs, outputs)
Also, using the parameters input_shape
and input_tensor
does not make much sense.
CodePudding user response:
I assume you are doing image classification and that your labels are categorical. Then try this
img_shape= (128,128,3) # specify the image shape you have
inputs =tf.keras.Input(shape=img_shape)
x = Conv2D(filters=3, kernel_size=(1,1), activation='relu')(inputs)
x =tf.keras.layers.Resizing(height=75, width=75)(x)
x=base_model=tf.keras.applications.InceptionResNetV2(include_top=False,
weights="imagenet", pooling='max') (x)
# with pooling ='max base_model ouput is a vector you can use as input to a dense layer
num_of_classes = 10 # set this to the number of classes in the data set
output=Dense(num_of_classes, activation='softmax') (x)
model=Model(inputs=inputs, outputs=output)
optimizer=tf.keras.optimizers.Adam( learning_rate=0.001)
loss=tf.keras.losses.CategoricalCrossentropy ()
model.compile(optimizer, loss=loss, metrics=['accuracy'])
print (model.summary())
Why are you including the first Conv2D layer? Seems like the base model is more than capable without this layer.