-
Notifications
You must be signed in to change notification settings - Fork 1
/
cwp_project_1_train (1).py
67 lines (57 loc) · 2.26 KB
/
cwp_project_1_train (1).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
# -*- coding: utf-8 -*-
"""cwp_project 1_train.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1YSSSVq3WrlAaxhtuu934QB2UfFuqaG6E
"""
#importing necessary packages
import numpy as np
import cv2
from tensorflow.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Dropout, Flatten
from tensorflow.python.keras.layers import Conv2D
from tensorflow.keras.optimizers import Adam
from tensorflow.python.keras.layers import MaxPooling2D
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
#dividing the data set and training it accrodingly
train_dir = 'D:/Visual Studio/cwp_project/data/train'
val_dir = 'D:/Visual Studio/cwp_project/data/test'
train_datagen = ImageDataGenerator(rescale=1./255)
val_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(48,48),
batch_size=64,
color_mode="grayscale",
class_mode='categorical'
)
validation_generator = val_datagen.flow_from_directory(
val_dir,
target_size=(48,48),
batch_size=64,
color_mode="grayscale",
class_mode='categorical'
)
emotion_model = Sequential()
emotion_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(48,48,1)))
emotion_model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))
emotion_model.add(Flatten())
emotion_model.add(Dense(1024, activation='relu'))
emotion_model.add(Dropout(0.5))
emotion_model.add(Dense(7, activation='softmax'))
emotion_model.compile(loss='categorical_crossentropy',optimizer=Adam(lr=0.0001, decay=1e-6),metrics=['accuracy'])
emotion_model_info = emotion_model.fit_generator(
train_generator,
steps_per_epoch=28709 // 64, #448
epochs=10,
validation_data=validation_generator,
validation_steps=7178 // 64) #112
#saving the model
emotion_model.save_weights('model.h5')