Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
21_22-J 38
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 38
21_22-J 38
Commits
2732e225
Commit
2732e225
authored
Jan 05, 2022
by
Lihinikaduwa D.N.R.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Trained model
parent
18cb69af
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
0 deletions
+101
-0
backend/IT18257632/model.h5
backend/IT18257632/model.h5
+0
-0
backend/IT18257632/train.py
backend/IT18257632/train.py
+101
-0
No files found.
backend/IT18257632/model.h5
0 → 100644
View file @
2732e225
File added
backend/IT18257632/train.py
0 → 100644
View file @
2732e225
import
json
import
numpy
as
np
import
tensorflow.keras
as
keras
from
sklearn.model_selection
import
train_test_split
DATA_PATH
=
"data.json"
SAVE_MODEL_PATH
=
"model.h5"
LEARNING_RATE
=
0.0001
EPOCHS
=
40
BATCH_SIZE
=
32
NUM_KEYWORDS
=
15
def
load_dataset
(
data_path
):
with
open
(
data_path
,
"r"
)
as
fp
:
data
=
json
.
load
(
fp
)
# extract input and targets
X
=
np
.
array
(
data
[
"MFCCs"
])
y
=
np
.
array
(
data
[
"labels"
])
return
X
,
y
def
get_data_splits
(
data_path
,
test_size
=
0.1
,
test_validation
=
0.1
):
# load dataset
X
,
y
=
load_dataset
(
data_path
)
# create train/validation/test splits
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
test_size
)
X_train
,
X_validation
,
y_train
,
y_validation
=
train_test_split
(
X_train
,
y_train
,
test_size
=
test_validation
)
# convert inputs from 2D TO 3D array
X_train
=
X_train
[
...
,
np
.
newaxis
]
X_validation
=
X_validation
[
...
,
np
.
newaxis
]
X_test
=
X_test
[
...
,
np
.
newaxis
]
return
X_train
,
X_validation
,
X_test
,
y_train
,
y_validation
,
y_test
def
build_model
(
input_shape
,
learning_rate
,
error
=
"sparse_categorical_crossentropy"
):
# build network
model
=
keras
.
Sequential
()
# copy layer 1
model
.
add
(
keras
.
layers
.
Conv2D
(
64
,
(
3
,
3
),
activation
=
"relu"
,
input_shape
=
input_shape
,
kernel_regularizer
=
keras
.
regularizers
.
l2
(
0.001
)))
model
.
add
(
keras
.
layers
.
BatchNormalization
())
model
.
add
(
keras
.
layers
.
MaxPool2D
((
3
,
3
),
strides
=
(
2
,
2
),
padding
=
"same"
))
# copy layer 2
model
.
add
(
keras
.
layers
.
Conv2D
(
32
,
(
3
,
3
),
activation
=
"relu"
,
kernel_regularizer
=
keras
.
regularizers
.
l2
(
0.001
)))
model
.
add
(
keras
.
layers
.
BatchNormalization
())
model
.
add
(
keras
.
layers
.
MaxPool2D
((
3
,
3
),
strides
=
(
2
,
2
),
padding
=
"same"
))
# copy layer 3
model
.
add
(
keras
.
layers
.
Conv2D
(
32
,
(
2
,
2
),
activation
=
"relu"
,
kernel_regularizer
=
keras
.
regularizers
.
l2
(
0.001
)))
model
.
add
(
keras
.
layers
.
BatchNormalization
())
model
.
add
(
keras
.
layers
.
MaxPool2D
((
2
,
2
),
strides
=
(
2
,
2
),
padding
=
"same"
))
# flatten the output feed it into a dense layer
model
.
add
(
keras
.
layers
.
Flatten
())
model
.
add
(
keras
.
layers
.
Dense
(
64
,
activation
=
"relu"
))
model
.
add
(
keras
.
layers
.
Dropout
(
0.3
))
# softmax classifier
model
.
add
(
keras
.
layers
.
Dense
(
NUM_KEYWORDS
,
activation
=
"softmax"
))
# compile the model
optimiser
=
keras
.
optimizers
.
Adam
(
learning_rate
=
learning_rate
)
model
.
compile
(
optimizer
=
optimiser
,
loss
=
error
,
metrics
=
[
"accuracy"
])
# print model overview
model
.
summary
()
return
model
def
main
():
# load train/validation/test data splits
X_train
,
X_validation
,
X_test
,
y_train
,
y_validation
,
y_test
=
get_data_splits
(
DATA_PATH
)
# build the CNN model
input_shape
=
(
X_train
.
shape
[
1
],
X_train
.
shape
[
2
],
X_train
.
shape
[
3
],)
model
=
build_model
(
input_shape
,
LEARNING_RATE
)
# train the model
model
.
fit
(
X_train
,
y_train
,
epochs
=
EPOCHS
,
batch_size
=
BATCH_SIZE
,
validation_data
=
(
X_validation
,
y_validation
))
# evaluate the model
test_error
,
test_accuracy
=
model
.
evaluate
(
X_test
,
y_test
)
print
(
f
"Test error: {test_error}, test accuracy: {test_accuracy}"
)
# save the model
model
.
save
(
SAVE_MODEL_PATH
)
if
__name__
==
"__main__"
:
main
()
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