Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
21_22-J-02
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
21_22-J-02
21_22-J-02
Commits
bd490578
Commit
bd490578
authored
Jan 05, 2022
by
Kethmini Rupasinghe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:CNN Model
parent
0acfcd90
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
1 addition
and
0 deletions
+1
-0
Emotion_Video/emotion_classification_cnn.py
Emotion_Video/emotion_classification_cnn.py
+1
-0
No files found.
Emotion_Video/emotion_classification_cnn.py
0 → 100644
View file @
bd490578
{
"metadata"
:{
"kernelspec"
:{
"language"
:
"python"
,
"display_name"
:
"Python 3"
,
"name"
:
"python3"
},
"language_info"
:{
"pygments_lexer"
:
"ipython3"
,
"nbconvert_exporter"
:
"python"
,
"version"
:
"3.6.4"
,
"file_extension"
:
".py"
,
"codemirror_mode"
:{
"name"
:
"ipython"
,
"version"
:
3
},
"name"
:
"python"
,
"mimetype"
:
"text/x-python"
}},
"nbformat_minor"
:
4
,
"nbformat"
:
4
,
"cells"
:[{
"cell_type"
:
"code"
,
"source"
:
"#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T18:29:42.213352Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T18:29:42.213653Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T18:29:47.460037Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T18:29:42.213574Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T18:29:47.459214Z
\"
}}
\n
import matplotlib.pyplot as plt
\n
import numpy as np
\n
import pandas as pd
\n
import seaborn as sns
\n
import os
\n\n
# Importing Deep Learning Libraries
\n\n
from keras.preprocessing.image import load_img, img_to_array
\n
from keras.preprocessing.image import ImageDataGenerator
\n
from keras.layers import Dense,Input,Dropout,GlobalAveragePooling2D,Flatten,Conv2D,BatchNormalization,Activation,MaxPooling2D
\n
from keras.models import Model,Sequential
\n\n
#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T18:31:26.874049Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T18:31:26.87433Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T18:31:27.174872Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T18:31:26.874299Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T18:31:27.17414Z
\"
}}
\n
from tensorflow.keras.optimizers import Adam,SGD,RMSprop
\n\n
#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T18:31:32.493872Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T18:31:32.494434Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T18:31:32.498665Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T18:31:32.494395Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T18:31:32.497919Z
\"
}}
\n
picture_size = 48
\n
folder_path =
\"
../input/face-expression-recognition-dataset/images/
\"\n\n
#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T18:34:59.744327Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T18:34:59.744602Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T18:35:00.78588Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T18:34:59.744566Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T18:35:00.783248Z
\"
}}
\n
expression = 'disgust'
\n\n
plt.figure(figsize= (12,12))
\n
for i in range(1, 10, 1):
\n
plt.subplot(3,3,i)
\n
img = load_img(folder_path+
\"
train/
\"
+expression+
\"
/
\"
+
\n
os.listdir(folder_path +
\"
train/
\"
+ expression)[i], target_size=(picture_size, picture_size))
\n
plt.imshow(img)
\n
plt.show()
\n\n
#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T18:35:05.654001Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T18:35:05.65435Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T18:35:21.720425Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T18:35:05.654313Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T18:35:21.719017Z
\"
}}
\n
batch_size = 128
\n\n
datagen_train = ImageDataGenerator()
\n
datagen_val = ImageDataGenerator()
\n\n
train_set = datagen_train.flow_from_directory(folder_path+
\"
train
\"
,
\n
target_size = (picture_size,picture_size),
\n
color_mode =
\"
grayscale
\"
,
\n
batch_size=batch_size,
\n
class_mode='categorical',
\n
shuffle=True)
\n\n\n
test_set = datagen_val.flow_from_directory(folder_path+
\"
validation
\"
,
\n
target_size = (picture_size,picture_size),
\n
color_mode =
\"
grayscale
\"
,
\n
batch_size=batch_size,
\n
class_mode='categorical',
\n
shuffle=False)
\n\n
#
%%
[markdown]
\n
# Model Building
\n\n
#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T18:35:59.134798Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T18:35:59.135349Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T18:36:01.780141Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T18:35:59.135309Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T18:36:01.77949Z
\"
}}
\n
no_of_classes = 7
\n\n
model = Sequential()
\n\n
#1st CNN layer
\n
model.add(Conv2D(64,(3,3),padding = 'same',input_shape = (48,48,1)))
\n
model.add(BatchNormalization())
\n
model.add(Activation('relu'))
\n
model.add(MaxPooling2D(pool_size = (2,2)))
\n
model.add(Dropout(0.25))
\n\n
#2nd CNN layer
\n
model.add(Conv2D(128,(5,5),padding = 'same'))
\n
model.add(BatchNormalization())
\n
model.add(Activation('relu'))
\n
model.add(MaxPooling2D(pool_size = (2,2)))
\n
model.add(Dropout (0.25))
\n\n
#3rd CNN layer
\n
model.add(Conv2D(512,(3,3),padding = 'same'))
\n
model.add(BatchNormalization())
\n
model.add(Activation('relu'))
\n
model.add(MaxPooling2D(pool_size = (2,2)))
\n
model.add(Dropout (0.25))
\n\n
#4th CNN layer
\n
model.add(Conv2D(512,(3,3), padding='same'))
\n
model.add(BatchNormalization())
\n
model.add(Activation('relu'))
\n
model.add(MaxPooling2D(pool_size=(2, 2)))
\n
model.add(Dropout(0.25))
\n\n
model.add(Flatten())
\n\n
#Fully connected 1st layer
\n
model.add(Dense(256))
\n
model.add(BatchNormalization())
\n
model.add(Activation('relu'))
\n
model.add(Dropout(0.25))
\n\n\n
# Fully connected layer 2nd layer
\n
model.add(Dense(512))
\n
model.add(BatchNormalization())
\n
model.add(Activation('relu'))
\n
model.add(Dropout(0.25))
\n\n
model.add(Dense(no_of_classes, activation='softmax'))
\n\n\n\n
opt = Adam(lr = 0.0001)
\n
model.compile(optimizer=opt,loss='categorical_crossentropy', metrics=['accuracy'])
\n
model.summary()
\n\n
#
%%
[markdown]
\n
# Fitting the Model with Training and Validation Data
\n\n
#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T18:36:45.963735Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T18:36:45.963996Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T18:36:45.978165Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T18:36:45.963967Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T18:36:45.977455Z
\"
}}
\n
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
\n\n
checkpoint = ModelCheckpoint(
\"
./model.h5
\"
, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
\n\n
early_stopping = EarlyStopping(monitor='val_loss',
\n
min_delta=0,
\n
patience=3,
\n
verbose=1,
\n
restore_best_weights=True
\n
)
\n\n
reduce_learningrate = ReduceLROnPlateau(monitor='val_loss',
\n
factor=0.2,
\n
patience=3,
\n
verbose=1,
\n
min_delta=0.0001)
\n\n
callbacks_list = [early_stopping,checkpoint,reduce_learningrate]
\n\n
epochs = 48
\n\n
model.compile(loss='categorical_crossentropy',
\n
optimizer = Adam(lr=0.001),
\n
metrics=['accuracy'])
\n\n
#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T18:37:22.724599Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T18:37:22.724884Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T18:47:27.839479Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T18:37:22.724853Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T18:47:27.838695Z
\"
}}
\n
history = model.fit_generator(generator=train_set,
\n
steps_per_epoch=train_set.n//train_set.batch_size,
\n
epochs=epochs,
\n
validation_data = test_set,
\n
validation_steps = test_set.n//test_set.batch_size,
\n
callbacks=callbacks_list
\n
)
\n\n
#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T18:52:03.305144Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T18:52:03.305869Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T18:52:27.411667Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T18:52:03.305831Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T18:52:27.410996Z
\"
}}
\n
plt.style.use('dark_background')
\n\n
plt.figure(figsize=(20,10))
\n
plt.subplot(1, 2, 1)
\n
plt.suptitle('Optimizer : Adam', fontsize=10)
\n
plt.ylabel('Loss', fontsize=16)
\n
plt.plot(history.history['loss'], label='Training Loss')
\n
plt.plot(history.history['val_loss'], label='Validation Loss')
\n
plt.legend(loc='upper right')
\n\n
plt.subplot(1, 2, 2)
\n
plt.ylabel('Accuracy', fontsize=16)
\n
plt.plot(history.history['accuracy'], label='Training Accuracy')
\n
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
\n
plt.legend(loc='lower right')
\n
plt.show()
\n\n
#
%%
[code] {
\"
execution
\"
:{
\"
iopub.status.busy
\"
:
\"
2022-01-02T19:08:54.788074Z
\"
,
\"
iopub.execute_input
\"
:
\"
2022-01-02T19:08:54.788346Z
\"
,
\"
iopub.status.idle
\"
:
\"
2022-01-02T19:08:54.950179Z
\"
,
\"
shell.execute_reply.started
\"
:
\"
2022-01-02T19:08:54.788317Z
\"
,
\"
shell.execute_reply
\"
:
\"
2022-01-02T19:08:54.949443Z
\"
}}
\n
model.save('/kaggle/working/final_model.h5')"
,
"metadata"
:{
"_uuid"
:
"f7dbf1bc-5f64-4f46-a2c9-0831213bb3b1"
,
"_cell_guid"
:
"80c6e78c-ee66-49d7-bae1-88859794c451"
,
"collapsed"
:
false
,
"jupyter"
:{
"outputs_hidden"
:
false
},
"trusted"
:
true
},
"execution_count"
:
null
,
"outputs"
:[]}]}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment