|
|
from tensorflow.keras.applications import VGG16 |
|
|
from tensorflow.keras.models import Model |
|
|
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout |
|
|
from tensorflow.keras.optimizers import Adam |
|
|
|
|
|
def build_model(input_shape, num_classes): |
|
|
|
|
|
base_model = VGG16(weights='imagenet', include_top=False, input_shape=input_shape) |
|
|
|
|
|
|
|
|
x = base_model.output |
|
|
x = GlobalAveragePooling2D()(x) |
|
|
x = Dense(1024, activation='relu')(x) |
|
|
x = Dropout(0.5)(x) |
|
|
predictions = Dense(num_classes, activation='softmax')(x) |
|
|
|
|
|
|
|
|
model = Model(inputs=base_model.input, outputs=predictions) |
|
|
|
|
|
|
|
|
for layer in base_model.layers: |
|
|
layer.trainable = False |
|
|
|
|
|
|
|
|
model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy']) |
|
|
|
|
|
return model |
|
|
|