1
+ # Import layers
1
2
from keras .layers import Dense , Flatten
2
3
from keras .models import Sequential
3
4
from keras .callbacks import Callback
4
- from keras import backend as K
5
5
import pandas as pd
6
6
import numpy as np
7
7
import cv2
8
- from PIL import Image
9
8
import keras
10
9
import subprocess
11
10
import os
17
16
run = wandb .init ()
18
17
config = run .config
19
18
19
+ # set hyperparameters
20
20
config .batch_size = 32
21
21
config .num_epochs = 5
22
22
23
23
input_shape = (48 , 48 , 1 )
24
24
25
25
26
26
class Perf (Callback ):
27
+ """Performance callback for logging inference time"""
28
+
27
29
def __init__ (self , testX ):
28
30
self .testX = testX
29
31
@@ -38,6 +40,7 @@ def on_epoch_end(self, epoch, logs):
38
40
39
41
40
42
def load_fer2013 ():
43
+ """Load the emotion dataset"""
41
44
if not os .path .exists ("fer2013" ):
42
45
print ("Downloading the face emotion dataset..." )
43
46
subprocess .check_output (
@@ -64,32 +67,29 @@ def load_fer2013():
64
67
65
68
return train_faces , train_emotions , val_faces , val_emotions
66
69
67
- # loading dataset
68
-
69
70
71
+ # loading dataset
70
72
train_faces , train_emotions , val_faces , val_emotions = load_fer2013 ()
71
73
num_samples , num_classes = train_emotions .shape
72
74
73
75
train_faces /= 255.
74
76
val_faces /= 255.
75
77
78
+ # Define the model here, CHANGEME
76
79
model = Sequential ()
77
- model .add (keras .layers .Conv2D (
78
- 32 , (3 , 3 ), input_shape = (48 , 48 , 1 ), activation = "relu" ))
79
- model .add (keras .layers .MaxPooling2D ())
80
- model .add (keras .layers .Conv2D (64 , (3 , 3 ), activation = "relu" ))
81
- model .add (keras .layers .MaxPooling2D ())
82
80
model .add (Flatten (input_shape = input_shape ))
83
81
model .add (Dense (num_classes , activation = "softmax" ))
84
82
model .compile (optimizer = 'adam' , loss = 'categorical_crossentropy' ,
85
83
metrics = ['accuracy' ])
84
+
85
+ # log the number of total parameters
86
86
config .total_params = model .count_params ()
87
- print ("Total" , config .total_params )
88
87
model .fit (train_faces , train_emotions , batch_size = config .batch_size ,
89
88
epochs = config .num_epochs , verbose = 1 , callbacks = [
90
89
Perf (val_faces ),
91
90
WandbCallback (data_type = "image" , labels = [
92
91
"Angry" , "Disgust" , "Fear" , "Happy" , "Sad" , "Surprise" , "Neutral" ])
93
92
], validation_data = (val_faces , val_emotions ))
94
93
94
+ # save the model
95
95
model .save ("emotion.h5" )
0 commit comments