Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2021-005
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
Hasini Piumika Alwis
2021-005
Commits
4bf1d2e1
Commit
4bf1d2e1
authored
Nov 26, 2021
by
Hasini Piumika Alwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
model_training.py file
parent
48bad758
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
0 deletions
+64
-0
Backend/classifer.py
Backend/classifer.py
+64
-0
No files found.
Backend/classifer.py
0 → 100644
View file @
4bf1d2e1
import
cv2
from
tensorflow.keras.models
import
Sequential
from
tensorflow.keras.layers
import
Conv2D
,
MaxPooling2D
,
Dense
,
Dropout
,
Flatten
from
tensorflow.keras.optimizers
import
Adam
from
tensorflow.keras.preprocessing.image
import
ImageDataGenerator
# Initialize image data generator with rescaling
train
=
ImageDataGenerator
(
rescale
=
1.
/
255
)
validation
=
ImageDataGenerator
(
rescale
=
1.
/
255
)
# Preprocess all test images
trainGen
=
train
.
flow_from_directory
(
'data/train'
,
target_size
=
(
48
,
48
),
batch_size
=
64
,
color_mode
=
"grayscale"
,
class_mode
=
'categorical'
)
# Preprocess all train images
valGen
=
validation
.
flow_from_directory
(
'data/test'
,
target_size
=
(
48
,
48
),
batch_size
=
64
,
color_mode
=
"grayscale"
,
class_mode
=
'categorical'
)
# create model structure
model
=
Sequential
()
model
.
add
(
Conv2D
(
32
,
kernel_size
=
(
3
,
3
),
activation
=
'relu'
,
input_shape
=
(
48
,
48
,
1
)))
model
.
add
(
Conv2D
(
64
,
kernel_size
=
(
3
,
3
),
activation
=
'relu'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
model
.
add
(
Dropout
(
0.25
))
model
.
add
(
Conv2D
(
128
,
kernel_size
=
(
3
,
3
),
activation
=
'relu'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
model
.
add
(
Conv2D
(
128
,
kernel_size
=
(
3
,
3
),
activation
=
'relu'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
model
.
add
(
Dropout
(
0.25
))
model
.
add
(
Flatten
())
model
.
add
(
Dense
(
1024
,
activation
=
'relu'
))
model
.
add
(
Dropout
(
0.5
))
model
.
add
(
Dense
(
7
,
activation
=
'softmax'
))
cv2
.
ocl
.
setUseOpenCL
(
False
)
model
.
compile
(
loss
=
'categorical_crossentropy'
,
optimizer
=
Adam
(
lr
=
0.0001
,
decay
=
1e-6
),
metrics
=
[
'accuracy'
])
# Train the neural network/model
model_info
=
model
.
fit_generator
(
trainGen
,
steps_per_epoch
=
28709
//
64
,
epochs
=
5
,
validation_data
=
valGen
,
validation_steps
=
7178
//
64
)
# save model structure in jason file
model_json
=
model
.
to_json
()
with
open
(
"emotion_model.json"
,
"w"
)
as
json_file
:
json_file
.
write
(
model_json
)
# save trained model weight in .h5 file
model
.
save_weights
(
'emotion_model.h5'
)
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