Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Autonomous_Vehicles_with_embedded_intelligence
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
kpp55
Autonomous_Vehicles_with_embedded_intelligence
Commits
eae5c864
Commit
eae5c864
authored
Nov 25, 2018
by
kpp55
Browse files
Options
Downloads
Patches
Plain Diff
Completed Demo code
parent
0f3c22a8
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Fall_Demo/Demo_mnist.py
+106
-0
106 additions, 0 deletions
Fall_Demo/Demo_mnist.py
Fall_Demo/Demo_training_mnist.py
+42
-0
42 additions, 0 deletions
Fall_Demo/Demo_training_mnist.py
with
148 additions
and
0 deletions
Fall_Demo/Demo_mnist.py
0 → 100644
+
106
−
0
View file @
eae5c864
from
__future__
import
absolute_import
,
division
,
print_function
import
tensorflow
as
tf
from
tensorflow
import
keras
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
serial
import
time
ser
=
serial
.
Serial
(
# Setup the serial port
port
=
'
/dev/ttyACM0
'
,
baudrate
=
19200
,
parity
=
serial
.
PARITY_ODD
,
stopbits
=
serial
.
STOPBITS_TWO
,
bytesize
=
serial
.
SEVENBITS
)
mnist
=
keras
.
datasets
.
mnist
(
train_images
,
train_labels
),
(
test_images
,
test_labels
)
=
mnist
.
load_data
()
train_images
=
train_images
/
255.0
test_images
=
test_images
/
255.0
plt
.
rcParams
[
'
interactive
'
]
==
True
class_names
=
[
'
0
'
,
'
1
'
,
'
2
'
,
'
3
'
,
'
4
'
,
'
5
'
,
'
6
'
,
'
7
'
,
'
8
'
,
'
9
'
]
plt
.
figure
(
figsize
=
(
10
,
10
))
for
i
in
range
(
25
):
plt
.
subplot
(
5
,
5
,
i
+
1
)
plt
.
xticks
([])
plt
.
yticks
([])
plt
.
grid
(
False
)
plt
.
imshow
(
train_images
[
i
],
cmap
=
plt
.
cm
.
binary
)
plt
.
xlabel
(
class_names
[
train_labels
[
i
]])
plt
.
show
()
new_model
=
keras
.
models
.
load_model
(
'
MNIST_model.h5
'
)
# Load in the trained model.
print
(
new_model
.
summary
())
new_model
.
compile
(
optimizer
=
tf
.
train
.
AdamOptimizer
(),
loss
=
'
sparse_categorical_crossentropy
'
,
metrics
=
[
'
accuracy
'
])
loss
,
acc
=
new_model
.
evaluate
(
test_images
,
test_labels
)
print
(
"
Restored model, accuracy: {:5.2f}%
"
.
format
(
100
*
acc
))
predictions
=
new_model
.
predict
(
test_images
)
def
plot_image
(
i
,
predictions_array
,
true_label
,
img
):
predictions_array
,
true_label
,
img
=
predictions_array
[
i
],
true_label
[
i
],
img
[
i
]
plt
.
grid
(
False
)
plt
.
xticks
([])
plt
.
yticks
([])
plt
.
imshow
(
img
,
cmap
=
plt
.
cm
.
binary
)
predicted_label
=
np
.
argmax
(
predictions_array
)
if
predicted_label
==
true_label
:
color
=
'
blue
'
else
:
color
=
'
red
'
plt
.
xlabel
(
"
{} {:2.0f}% ({})
"
.
format
(
class_names
[
predicted_label
],
100
*
np
.
max
(
predictions_array
),
class_names
[
true_label
]),
color
=
color
)
def
plot_value_array
(
i
,
predictions_array
,
true_label
):
predictions_array
,
true_label
=
predictions_array
[
i
],
true_label
[
i
]
plt
.
grid
(
False
)
plt
.
xticks
([])
plt
.
yticks
([])
thisplot
=
plt
.
bar
(
range
(
10
),
predictions_array
,
color
=
"
#777777
"
)
plt
.
ylim
([
0
,
1
])
predicted_label
=
np
.
argmax
(
predictions_array
)
thisplot
[
predicted_label
].
set_color
(
'
red
'
)
thisplot
[
true_label
].
set_color
(
'
blue
'
)
i
=
0
plt
.
figure
(
figsize
=
(
6
,
3
))
plt
.
subplot
(
1
,
2
,
1
)
plot_image
(
i
,
predictions
,
test_labels
,
test_images
)
plt
.
subplot
(
1
,
2
,
2
)
plot_value_array
(
i
,
predictions
,
test_labels
)
plt
.
show
()
num_rows
=
5
num_cols
=
3
num_images
=
num_rows
*
num_cols
plt
.
figure
(
figsize
=
(
2
*
2
*
num_cols
,
2
*
num_rows
))
for
i
in
range
(
num_images
):
plt
.
subplot
(
num_rows
,
2
*
num_cols
,
2
*
i
+
1
)
plot_image
(
i
,
predictions
,
test_labels
,
test_images
)
plt
.
subplot
(
num_rows
,
2
*
num_cols
,
2
*
i
+
2
)
plot_value_array
(
i
,
predictions
,
test_labels
)
# Single image testing
img
=
test_images
[
0
]
print
(
img
.
shape
)
img
=
(
np
.
expand_dims
(
img
,
0
))
print
(
img
.
shape
)
predictions_single
=
new_model
.
predict
(
img
)
print
(
predictions_single
)
print
(
np
.
argmax
(
predictions_single
[
0
]))
for
i
in
range
(
0
,
len
(
test_images
)):
img
=
test_images
[
i
]
img
=
(
np
.
expand_dims
(
img
,
0
))
predictions_single
=
new_model
.
predict
(
img
)
predict
=
int
(
np
.
argmax
(
predictions_single
[
0
]))
time
.
sleep
(
2
)
print
(
predict
)
ser
.
write
(
b
'
%d
'
%
predict
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Fall_Demo/Demo_training_mnist.py
0 → 100644
+
42
−
0
View file @
eae5c864
from
__future__
import
absolute_import
,
division
,
print_function
import
tensorflow
as
tf
from
tensorflow
import
keras
import
numpy
as
np
import
os
mnist
=
keras
.
datasets
.
mnist
(
train_images
,
train_labels
),
(
test_images
,
test_labels
)
=
mnist
.
load_data
()
train_images
=
train_images
/
255.0
test_images
=
test_images
/
255.0
def
create_model
():
model
=
keras
.
Sequential
([
keras
.
layers
.
Flatten
(
input_shape
=
(
28
,
28
)),
#Transforms the format of the images from a 2d-array (of 28 by 28 pixels), to a 1d-array of 28 * 28 = 784 pixels
keras
.
layers
.
Dense
(
128
,
activation
=
tf
.
nn
.
relu
),
#This is the fully connected layers
keras
.
layers
.
Dense
(
10
,
activation
=
tf
.
nn
.
softmax
)
#Output layer
])
model
.
compile
(
optimizer
=
tf
.
train
.
AdamOptimizer
(),
loss
=
'
sparse_categorical_crossentropy
'
,
metrics
=
[
'
accuracy
'
])
return
model
model
=
create_model
()
model
.
fit
(
train_images
,
train_labels
,
epochs
=
5
)
model
.
save
(
'
MNIST_model.h5
'
)
new_model
=
keras
.
models
.
load_model
(
'
MNIST_model.h5
'
)
print
(
"
Original model summary:
"
)
print
(
model
.
summary
())
print
(
"
New model summary:
"
)
print
(
new_model
.
summary
())
new_model
.
compile
(
optimizer
=
tf
.
train
.
AdamOptimizer
(),
loss
=
'
sparse_categorical_crossentropy
'
,
metrics
=
[
'
accuracy
'
])
loss
,
acc
=
model
.
evaluate
(
test_images
,
test_labels
)
print
(
"
Restored model, accuracy: {:5.2f}%
"
.
format
(
100
*
acc
))
loss
,
acc
=
new_model
.
evaluate
(
test_images
,
test_labels
)
print
(
"
Restored model, accuracy: {:5.2f}%
"
.
format
(
100
*
acc
))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment