Update model.py
Browse files
model.py
CHANGED
|
@@ -1,38 +1,27 @@
|
|
| 1 |
-
from tensorflow.keras.
|
| 2 |
-
from tensorflow.keras.
|
| 3 |
-
from tensorflow.keras.layers import
|
| 4 |
-
from tensorflow.keras.
|
| 5 |
|
| 6 |
-
def build_model(
|
| 7 |
-
model
|
| 8 |
-
|
| 9 |
-
BatchNormalization(),
|
| 10 |
-
MaxPooling2D((2, 2)),
|
| 11 |
-
|
| 12 |
-
Conv2D(64, (3, 3), activation='relu',padding='same',kernel_regularizer='l2'),
|
| 13 |
-
BatchNormalization(),
|
| 14 |
-
MaxPooling2D((2, 2)),
|
| 15 |
-
|
| 16 |
-
Conv2D(128, (3, 3), activation='relu',padding='same',kernel_regularizer='l2'),
|
| 17 |
-
BatchNormalization(),
|
| 18 |
-
MaxPooling2D((2, 2)),
|
| 19 |
-
|
| 20 |
-
Conv2D(256, (3, 3), activation='relu',padding='same',kernel_regularizer='l2'),
|
| 21 |
-
BatchNormalization(),
|
| 22 |
-
MaxPooling2D((2, 2)),
|
| 23 |
-
|
| 24 |
-
Conv2D(256, (3, 3), activation='relu',padding='same',kernel_regularizer='l2'),
|
| 25 |
-
BatchNormalization(),
|
| 26 |
-
MaxPooling2D((2, 2)),
|
| 27 |
-
|
| 28 |
-
Flatten(),
|
| 29 |
-
Dense(512, activation='relu',kernel_regularizer='l2'),
|
| 30 |
-
#BatchNormalization(),
|
| 31 |
-
Dropout(0.5),
|
| 32 |
-
Dense(256, activation='relu',kernel_regularizer='l2'),
|
| 33 |
-
#BatchNormalization(),
|
| 34 |
-
Dropout(0.5),
|
| 35 |
-
Dense(3, activation='softmax') # Assuming 3 classes
|
| 36 |
-
])
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from tensorflow.keras.applications import VGG16
|
| 2 |
+
from tensorflow.keras.models import Model
|
| 3 |
+
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
|
| 4 |
+
from tensorflow.keras.optimizers import Adam
|
| 5 |
|
| 6 |
+
def build_model(num_classes):
|
| 7 |
+
# Load VGG16 model without the top layers
|
| 8 |
+
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Adding additional layers
|
| 11 |
+
x = base_model.output
|
| 12 |
+
x = GlobalAveragePooling2D()(x)
|
| 13 |
+
x = Dense(1024, activation='relu')(x)
|
| 14 |
+
x = Dropout(0.5)(x)
|
| 15 |
+
predictions = Dense(num_classes, activation='softmax')(x)
|
| 16 |
+
|
| 17 |
+
# Creating the final model
|
| 18 |
+
model = Model(inputs=base_model.input, outputs=predictions)
|
| 19 |
+
|
| 20 |
+
# Freezing the layers except the last layers
|
| 21 |
+
for layer in base_model.layers:
|
| 22 |
+
layer.trainable = False
|
| 23 |
+
|
| 24 |
+
# Compile the model
|
| 25 |
+
model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
|
| 26 |
+
|
| 27 |
+
return model
|