-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify_train.py
More file actions
88 lines (68 loc) · 3.46 KB
/
classify_train.py
File metadata and controls
88 lines (68 loc) · 3.46 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization
train_dir = '/content/emotionclass/train'
validation_dir = '/content/emotionclass/validation'
train_generator = ImageDataGenerator(rescale= 1./255,
horizontal_flip = True,
fill_mode = 'nearest',
shear_range = 0.3,
width_shift_range=0.4,
height_shift_range=0.4,
rotation_range = 30)
train_gen = train_generator.flow_from_directory(train_dir,
target_size = (48, 48),
batch_size = 32,
class_mode = 'categorical',
color_mode = 'grayscale',
shuffle = True)
validation_generator = ImageDataGenerator(rescale = 1./255)
validation_gen = validation_generator.flow_from_directory(validation_dir,
target_size = (48, 48),
batch_size = 32,
class_mode = 'categorical',
color_mode = 'grayscale',
shuffle = True)
emotion = Sequential()
emotion.add(Conv2D(32,(3,3), padding = 'same', input_shape = (48, 48, 1), activation= 'relu'))
emotion.add(Conv2D(32,(3,3), padding = 'same', activation= 'relu'))
emotion.add(BatchNormalization())
emotion.add(MaxPooling2D(pool_size=(2,2), strides= (2,2)))
emotion.add(Dropout(0.2))
emotion.add(Conv2D(64,(3,3), padding = 'same', activation= 'relu'))
emotion.add(Conv2D(64,(3,3), padding = 'same', activation= 'relu'))
emotion.add(BatchNormalization())
emotion.add(MaxPooling2D(pool_size=(2,2), strides= (2,2)))
emotion.add(Dropout(0.2))
emotion.add(Conv2D(128,(3,3), padding = 'same', activation= 'relu'))
emotion.add(Conv2D(128,(3,3), activation= 'relu'))
emotion.add(BatchNormalization())
emotion.add(MaxPooling2D(pool_size=(2,2), strides= (2,2)))
emotion.add(Dropout(0.2))
emotion.add(Conv2D(256,(3,3), padding = 'same', activation= 'relu'))
emotion.add(Conv2D(256,(3,3), padding = 'same', activation= 'relu'))
emotion.add(BatchNormalization())
emotion.add(MaxPooling2D(pool_size=(2,2), strides= (2,2)))
emotion.add(Dropout(0.2))
emotion.add(Flatten())
emotion.add(Dense(1024, activation = 'relu'))
emotion.add(Dropout(0.4))
emotion.add(Dense(2048, activation = 'relu'))
emotion.add(Dropout(0.4))
emotion.add(Dense(4096, activation = 'relu'))
emotion.add(BatchNormalization())
emotion.add(Dense(5, activation = 'softmax'))
emotion.compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
history = emotion.fit(train_gen,
steps_per_epoch = 24282 // 32,
epochs = 150,
validation_data = validation_gen,
validation_steps = 5937 // 32)
emotion.save('Emotion_saved.h5')