-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.py
More file actions
67 lines (55 loc) · 1.86 KB
/
Copy pathTrain.py
File metadata and controls
67 lines (55 loc) · 1.86 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.optimizers import Adam
# === Settings ===
IMG_SIZE = 48
BATCH_SIZE = 64
EPOCHS = 100
DATA_PATH = 'Emotion_dataset' # Replace with your actual folder path
# === Load Dataset ===
datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2, horizontal_flip=True)
train_generator = datagen.flow_from_directory(
DATA_PATH,
target_size=(IMG_SIZE, IMG_SIZE),
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
subset='training'
)
val_generator = datagen.flow_from_directory(
DATA_PATH,
target_size=(IMG_SIZE, IMG_SIZE),
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
subset='validation'
)
# === Build CNN Model ===
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_SIZE, IMG_SIZE, 1)),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dropout(0.5),
Dense(128, activation='relu'),
Dense(train_generator.num_classes, activation='softmax')
])
# === Compile ===
model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
# === Train ===
history = model.fit(train_generator, validation_data=val_generator, epochs=EPOCHS)
# === Save the Model ===
model.save("emotion.h5")
print("✅ Model saved as 'emotion.h5'")
# === Plot Accuracy ===
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.legend()
plt.title('Model Accuracy')
plt.show()