File size: 1,334 Bytes
34005bf 18cf310 5ed07b2 f8529f5 34005bf da57eb0 18cf310 0ca102f 18cf310 0ca102f 18cf310 3fef248 0ca102f 7e64b5c 18cf310 7e64b5c 0ca102f 3fef248 39549f6 3fef248 9e14728 0ca102f 9e14728 f8529f5 3fef248 0ca102f 39549f6 0ca102f f8529f5 3fef248 9e14728 7e64b5c 18cf310 39549f6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import tensorflow as tf
from tensorflow.keras.layers import Dropout, Dense,GlobalAveragePooling2D, Input
from tensorflow.keras.applications import EfficientNetB7
from tensorflow.keras.models import Model
def create_effnetb2_model(num_classes=15, seed=42):
"""Creates an EfficientNetB7 model with additional layers for fine-tuning.
Args:
num_classes (int, optional): Number of classes in the classifier head.
Defaults to 3.
seed (int, optional): Random seed value. Defaults to 42.
Returns:
model (tensorflow.keras.Model): EffNetB7 model with additional layers.
"""
# Load the pretrained EffNetB7 model
base_model = EfficientNetB7(include_top=False, weights='imagenet', input_shape=(224, 224, 3))
# Freeze all layers in the base model except the last few
for layer in base_model.layers[:-10]:
layer.trainable = False
# Add additional layers
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.3)(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.3)(x)
x = Dense(128, activation='relu')(x)
# Add a dense layer for classification
outputs = Dense(num_classes, activation='softmax')(x)
# Create the final model
model = Model(inputs=base_model.input, outputs=outputs)
return model
|