You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mostly because I've found less bugs (alongside other students of the course, TK - Moophers GitHub).
Note: This doesn't necessarily mean EfficientNetV2B0 is the best model for the job, it performs well on many computer vision tasks, however, it's only used as an example. Best to experiment with other model versions in tf.keras.applications to find the best model for your own problem.
Because model_2 is used several times throughout Notebook 05, I made a reference function to recreate it.
That way we know whenever the function is called, we're getting a new instance of model_2 (rather than potentially reusing an old one).
defcreate_base_model(input_shape: tuple[int, int, int] = (224, 224, 3),
output_shape: int=10,
learning_rate: float=0.001,
training: bool=False) ->tf.keras.Model:
""" Create a model based on EfficientNetV2B0 with built-in data augmentation. Parameters: - input_shape (tuple): Expected shape of input images. Default is (224, 224, 3). - output_shape (int): Number of classes for the output layer. Default is 10. - learning_rate (float): Learning rate for the Adam optimizer. Default is 0.001. - training (bool): Whether the base model is trainable. Default is False. Returns: - tf.keras.Model: The compiled model with specified input and output settings. """# Create base modelbase_model=tf.keras.applications.efficientnet_v2.EfficientNetV2B0(include_top=False)
base_model.trainable=training# Setup model input and outputs with data augmentation built-ininputs=layers.Input(shape=input_shape, name="input_layer")
x=data_augmentation(inputs)
x=base_model(x, training=False) # pass augmented images to base model but keep it in inference modex=layers.GlobalAveragePooling2D(name="global_average_pooling_layer")(x)
outputs=layers.Dense(units=output_shape, activation="softmax", name="output_layer")(x)
model=tf.keras.Model(inputs, outputs)
# Compile modelmodel.compile(loss="categorical_crossentropy",
optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
metrics=["accuracy"])
returnmodel# Create an instance of model_2 with our new functionmodel_2=create_base_model()
This way, for experiment 3 and experiment 4, we can use a new instance of model_2, load the weights from experiment 2 and then fine-tune appropriately.
This helps to fix the issue in #544 (though this issue is also related to EfficientNetB0 being notoriously buggy across TensorFlow versions).
Change base_model -> model_2_base_model
Using the same base_model variable throughout the notebook got confusing.
So I updated it to be model_2_base_model so we know which base it relates to.
For example:
model_2=create_base_model()
# Unfreeze the top 10 layers in model_2's base_modelmodel_2_base_model=model_2.layers[2]
model_2_base_model.trainable=True# Freeze all layers except for the last 10forlayerinmodel_2_base_model.layers[:-10]:
layer.trainable=False
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The following note keeps track of some relatively minor changes in Notebook 05: Transfer Learning with TensorFlow Part 2: Fine-tuning to migitate a few issues.
The main one being:
tf.keras.applications.efficientnet
being notoriously buggy across TensorFlow versions.The following code fixes should fix all issues in Notebook 05 and has been test on TensorFlow versions 2.12.0 and 2.13.0
TODO
effnetb0
is noweffnetB0v2
Quick summary
EfficientNetB0
->EfficientNetV2B0
to fix Notebook 05:TypeError: Unable to serialize [2.0897 2.1129 2.1082] to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>
(fix inside) #553 (if you are getting errors with EfficientNetB0, try using EfficientNetV2B0)model_2
creation to fix Notebook 05: load_weights results in "Incompatible tensor with shape (1280, 10)..." #544base_model
clearerQuick links:
EfficienetNetB0 -> EfficientNetV2B0
In versions of TensorFlow 2.10+, the
tf.keras.applications.efficientnet.EfficientNetB0
(EfficientNetB0
for short) module seemed to be plagued with errors.To fix this, all uses of
EfficientNetB0
have been replaced withtf.keras.applications.efficientnet_v2.EfficientNetV2B0
(EfficientNetV2B0
for short).New:
Old:
Why?
Mostly because I've found less bugs (alongside other students of the course, TK - Moophers GitHub).
Note: This doesn't necessarily mean
EfficientNetV2B0
is the best model for the job, it performs well on many computer vision tasks, however, it's only used as an example. Best to experiment with other model versions intf.keras.applications
to find the best model for your own problem.This takes care of the issue in #553.
Functionizing
model_2
creationBecause
model_2
is used several times throughout Notebook 05, I made a reference function to recreate it.That way we know whenever the function is called, we're getting a new instance of
model_2
(rather than potentially reusing an old one).This way, for experiment 3 and experiment 4, we can use a new instance of
model_2
, load the weights from experiment 2 and then fine-tune appropriately.This helps to fix the issue in #544 (though this issue is also related to EfficientNetB0 being notoriously buggy across TensorFlow versions).
Change
base_model
->model_2_base_model
Using the same
base_model
variable throughout the notebook got confusing.So I updated it to be
model_2_base_model
so we know which base it relates to.For example:
Beta Was this translation helpful? Give feedback.
All reactions