-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemotions.py
77 lines (55 loc) · 2.4 KB
/
emotions.py
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
# -*- coding: utf-8 -*-
"""Emotions.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1PWL-JvW1u7GH4SsvOjHfwq55vrDNpXt8
"""
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
# Commented out IPython magic to ensure Python compatibility.
# %%bash
# pwd
# cd drive/'My Drive'/'Colab Notebooks'/'Deep Learning'/'Convolutional neural network'/'facial expressions'
# cp emodata.zip /content
# Commented out IPython magic to ensure Python compatibility.
# %%bash
# sudo apt-get install unzip
# unzip emodata.zip
train_datagen = ImageDataGenerator(
rescale=1./255, #(feature-scaling)
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
training_set = train_datagen.flow_from_directory('/content/train',
target_size=(128, 128),
batch_size=32,
color_mode='grayscale',
class_mode='categorical',
shuffle=True)
test_datagen = ImageDataGenerator(rescale=1./255)
testing_set = test_datagen.flow_from_directory('/content/validation',
target_size=(128, 128),
batch_size=32,
color_mode='grayscale',
class_mode='categorical')
cnn = tf.keras.models.Sequential()
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[128, 128, 1]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
cnn.add(tf.keras.layers.Dropout(0.2))
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
cnn.add(tf.keras.layers.Dropout(0.2))
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
cnn.add(tf.keras.layers.Dropout(0.2))
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
cnn.add(tf.keras.layers.Dropout(0.2))
cnn.add(tf.keras.layers.Flatten())
cnn.add(tf.keras.layers.Dense(units=128, activation='relu'))
cnn.add(tf.keras.layers.Dense(units=7, activation='softmax'))
cnn.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
train_samples = 28821
test_samples = 7066
cnn.fit_generator(training_set, steps_per_epoch=train_samples//32, epochs=25, validation_data=testing_set, validation_steps=test_samples//32 )
cnn.save('emotions_5layer.h5')