Update Training/Code/train.py
Browse files- Training/Code/train.py +57 -72
Training/Code/train.py
CHANGED
@@ -1,72 +1,57 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
|
4 |
-
import
|
5 |
-
from keras.
|
6 |
-
from
|
7 |
-
from keras.
|
8 |
-
from
|
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 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
#
|
57 |
-
|
58 |
-
EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True),
|
59 |
-
ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True)
|
60 |
-
]
|
61 |
-
|
62 |
-
# Train the model
|
63 |
-
emotion_model_info = emotion_model.fit(
|
64 |
-
train_generator,
|
65 |
-
epochs=50,
|
66 |
-
validation_data=validation_generator,
|
67 |
-
callbacks=callbacks
|
68 |
-
)
|
69 |
-
|
70 |
-
# Save the full model
|
71 |
-
emotion_model.save("emotion_model.keras")
|
72 |
-
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
from keras.models import Model
|
4 |
+
from keras.layers import Dense, Dropout, GlobalAveragePooling2D, Input
|
5 |
+
from keras.optimizers import Adam
|
6 |
+
from keras_preprocessing.image import ImageDataGenerator
|
7 |
+
from keras.applications import MobileNetV2
|
8 |
+
from keras.callbacks import EarlyStopping, ModelCheckpoint
|
9 |
+
|
10 |
+
# Define paths
|
11 |
+
base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))
|
12 |
+
train_dir = os.path.join(base_dir, 'Data/train')
|
13 |
+
val_dir = os.path.join(base_dir, 'Data/test')
|
14 |
+
|
15 |
+
# Image generators with augmentation
|
16 |
+
train_datagen = ImageDataGenerator(
|
17 |
+
rescale=1./255,
|
18 |
+
rotation_range=30,
|
19 |
+
zoom_range=0.2,
|
20 |
+
horizontal_flip=True,
|
21 |
+
shear_range=0.2,
|
22 |
+
width_shift_range=0.2,
|
23 |
+
height_shift_range=0.2
|
24 |
+
)
|
25 |
+
val_datagen = ImageDataGenerator(rescale=1./255)
|
26 |
+
|
27 |
+
train_generator = train_datagen.flow_from_directory(
|
28 |
+
train_dir, target_size=(96, 96), batch_size=32, color_mode='rgb', class_mode='categorical')
|
29 |
+
|
30 |
+
validation_generator = val_datagen.flow_from_directory(
|
31 |
+
val_dir, target_size=(96, 96), batch_size=32, color_mode='rgb', class_mode='categorical')
|
32 |
+
|
33 |
+
# Load base model
|
34 |
+
base_model = MobileNetV2(include_top=False, input_shape=(96, 96, 3), weights='imagenet')
|
35 |
+
base_model.trainable = False # Freeze base layers
|
36 |
+
|
37 |
+
# Add custom layers
|
38 |
+
x = base_model.output
|
39 |
+
x = GlobalAveragePooling2D()(x)
|
40 |
+
x = Dense(256, activation='relu')(x)
|
41 |
+
x = Dropout(0.5)(x)
|
42 |
+
predictions = Dense(7, activation='softmax')(x)
|
43 |
+
|
44 |
+
model = Model(inputs=base_model.input, outputs=predictions)
|
45 |
+
model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
|
46 |
+
|
47 |
+
# Callbacks
|
48 |
+
callbacks = [
|
49 |
+
EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True),
|
50 |
+
ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True)
|
51 |
+
]
|
52 |
+
|
53 |
+
# Train the model
|
54 |
+
model.fit(train_generator, validation_data=validation_generator, epochs=30, callbacks=callbacks)
|
55 |
+
|
56 |
+
# Save model
|
57 |
+
model.save("emotion_model.keras")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|