Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2023-286
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
1
Merge Requests
1
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
2023-286
2023-286
Commits
c9242f5e
Commit
c9242f5e
authored
May 27, 2023
by
Fernando M.I.N IT20016616
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added model training .py file for signs
parent
64f8ace8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
142 additions
and
0 deletions
+142
-0
.DS_Store
.DS_Store
+0
-0
Collecting_data/.DS_Store
Collecting_data/.DS_Store
+0
-0
Training_models/Preprocessing and Training_signs.py
Training_models/Preprocessing and Training_signs.py
+142
-0
No files found.
.DS_Store
View file @
c9242f5e
No preview for this file type
Collecting_data/.DS_Store
0 → 100644
View file @
c9242f5e
File added
Training_models/Preprocessing and Training_signs.py
0 → 100644
View file @
c9242f5e
# -*- coding: utf-8 -*-
"""Untitled6.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1-DVJ3LLIXgaCWW8Bb6CP-ei7O2k6tLwD
"""
import
keras
from
keras.models
import
Sequential
from
keras.layers
import
Dense
,
Dropout
,
Flatten
from
keras.layers
import
Conv2D
,
MaxPooling2D
from
keras.preprocessing.image
import
ImageDataGenerator
import
numpy
as
np
from
sklearn.model_selection
import
train_test_split
import
os
from
PIL
import
Image
# Load the data
data
=
[]
labels
=
[]
folder_names
=
[
'good'
,
'hello'
,
'home'
,
'love'
,
'me'
,
'more'
,
'mother'
,
'no'
,
'phone'
,
'stop'
,
'where'
,
'why'
,
'yes'
]
for
folder_index
,
folder_name
in
enumerate
(
folder_names
):
path
=
'drive/MyDrive/my_dataset/{}'
.
format
(
folder_name
)
for
filename
in
os
.
listdir
(
path
):
img
=
Image
.
open
(
os
.
path
.
join
(
path
,
filename
))
.
convert
(
'L'
)
img
=
img
.
resize
((
128
,
128
))
img
=
np
.
array
(
img
)
img
=
img
/
255
data
.
append
(
img
)
labels
.
append
(
folder_index
)
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
data
,
labels
,
random_state
=
42
,
test_size
=
0.2
)
# Convert the data to arrays
X_train
=
np
.
array
(
X_train
)
X_test
=
np
.
array
(
X_test
)
# Expand dimensions for grayscale images
X_train
=
np
.
expand_dims
(
X_train
,
axis
=
3
)
X_test
=
np
.
expand_dims
(
X_test
,
axis
=
3
)
# Convert the labels to categorical
num_classes
=
len
(
folder_names
)
y_train
=
keras
.
utils
.
to_categorical
(
y_train
,
num_classes
)
y_test
=
keras
.
utils
.
to_categorical
(
y_test
,
num_classes
)
# Create the model
model
=
Sequential
()
model
.
add
(
Conv2D
(
32
,
kernel_size
=
(
3
,
3
),
activation
=
'relu'
,
input_shape
=
(
128
,
128
,
1
)))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
model
.
add
(
Dropout
(
0.25
))
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
(
Dropout
(
0.25
))
model
.
add
(
Flatten
())
model
.
add
(
Dense
(
512
,
activation
=
'relu'
))
model
.
add
(
Dropout
(
0.5
))
model
.
add
(
Dense
(
num_classes
,
activation
=
'softmax'
))
model
.
compile
(
loss
=
'categorical_crossentropy'
,
optimizer
=
'Adam'
,
metrics
=
[
'accuracy'
])
# Data augmentation
datagen
=
ImageDataGenerator
(
rotation_range
=
30
,
width_shift_range
=
0.2
,
height_shift_range
=
0.2
,
zoom_range
=
0.2
,
horizontal_flip
=
True
,
fill_mode
=
'nearest'
)
# Define early stopping
early_stopping
=
keras
.
callbacks
.
EarlyStopping
(
monitor
=
'val_accuracy'
,
patience
=
10
,
restore_best_weights
=
True
,
min_delta
=
0.001
)
# Fit the model with data augmentation
batch_size
=
10
epochs
=
5
steps_per_epoch
=
len
(
X_train
)
//
batch_size
model
.
fit
(
datagen
.
flow
(
X_train
,
y_train
,
batch_size
=
batch_size
),
steps_per_epoch
=
steps_per_epoch
,
epochs
=
epochs
,
validation_data
=
(
X_test
,
y_test
),
callbacks
=
[
early_stopping
]
)
CNN_Score
=
model
.
evaluate
(
np
.
array
(
X_test
),
np
.
array
(
y_test
))
print
(
"Test Loss: {:.5f}"
.
format
(
CNN_Score
[
0
]))
print
(
"Test Accuracy: {:.2f}
%
"
.
format
(
CNN_Score
[
1
]
*
100
))
CNN_Score
=
model
.
evaluate
(
np
.
array
(
X_train
),
np
.
array
(
y_train
))
print
(
"Train Loss: {:.5f}"
.
format
(
CNN_Score
[
0
]))
print
(
"Train Accuracy: {:.2f}
%
"
.
format
(
CNN_Score
[
1
]
*
100
))
from
sklearn.metrics
import
confusion_matrix
import
seaborn
as
sns
import
matplotlib.pyplot
as
plt
# Predict labels for the test set
y_pred
=
model
.
predict
(
np
.
array
(
X_test
))
y_pred
=
np
.
argmax
(
y_pred
,
axis
=
1
)
# Convert true labels to multiclass format
y_true
=
np
.
argmax
(
np
.
array
(
y_test
),
axis
=
1
)
# Compute confusion matrix
cm
=
confusion_matrix
(
y_true
,
y_pred
)
# Plot confusion matrix
plt
.
figure
(
figsize
=
(
7
,
6
))
sns
.
heatmap
(
cm
,
annot
=
True
,
cmap
=
"Blues"
,
fmt
=
"d"
,
xticklabels
=
range
(
13
),
yticklabels
=
range
(
13
))
plt
.
xlabel
(
'Predicted'
)
plt
.
ylabel
(
'True'
)
plt
.
title
(
'Confusion Matrix'
)
plt
.
show
()
from
sklearn.metrics
import
classification_report
# Calculate and display the classification report
report
=
classification_report
(
y_true
,
y_pred
)
print
(
report
)
# Save the model
model
.
save
(
"trained_model.h5"
)
\ 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