# Function to build the model
def build_model(hp):
base_model = MobileNetV3Large(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
base_model.trainable = False # Freeze the base model
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(hp.Int('units', min_value=64, max_value=512, step=64), activation='relu')(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(inputs=base_model.input, outputs=x)
model.compile(optimizer=Adam(hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])),
loss='binary_crossentropy',
metrics=['accuracy'])
return model
# Hyperparameter tuning using Keras Tuner
tuner = RandomSearch(
build_model,
objective='val_accuracy',
max_trials=10,
executions_per_trial=1,
directory='tuner_logs',
project_name='mobilenetv3_fear_detection'
)
# Get the optimal hyperparameters
best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]
# Build the model with the optimal hyperparameters
model = build_model(best_hps)
# Unfreeze some layers in the base model for fine-tuning
base_model = model.layers[0]
base_model.trainable = True
# Fine-tune from layer n onwards (unfreeze the top layers)
fine_tune_at = len(base_model.layers) // 2
As the test accuracy was low I wanted to unfreeze the top half of the layers for fine-tuning. But I get an AttributeError: ‘InputLayer’ object has no attribute ‘layers’ on fine_tune_at = len(base_model.layers) // 2
I tried using base_model = model.get_layer(index=0)
instead of base_model = model.layers[0]
but I still see the error.
New contributor
Anushka Udayanga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.