Spaces:
No application file
No application file
| import tensorflow as tf | |
| from tensorflow.keras import layers, Model | |
| from tensorflow.keras.applications import MobileNetV3Small | |
| def build_feature_extractor(): | |
| base_model = MobileNetV3Small( | |
| weights='imagenet', | |
| include_top=False, | |
| input_shape=(224, 224, 3), | |
| alpha=1.0 | |
| ) | |
| base_model.trainable = False | |
| inputs = tf.keras.Input(shape=(224, 224, 3)) | |
| x = base_model(inputs, training=False) | |
| x = layers.GlobalAveragePooling2D()(x) | |
| x = layers.Dense(128, activation='relu')(x) | |
| x = layers.Dropout(0.3)(x) | |
| outputs = layers.Dense(64, activation='relu', name='features')(x) | |
| model = Model(inputs, outputs, name='DrowsinessFeatureExtractor') | |
| return model, base_model | |
| def fine_tune_model(model, base_model, unfreeze_layers=20): | |
| base_model.trainable = True | |
| for layer in base_model.layers[:-unfreeze_layers]: | |
| layer.trainable = False | |
| model.compile( | |
| optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5), | |
| loss='categorical_crossentropy', | |
| metrics=['accuracy'] | |
| ) | |
| return model | |