Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scikit-luc
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
Luc Giffon
scikit-luc
Commits
ee810747
Commit
ee810747
authored
7 years ago
by
Luc Giffon
Browse files
Options
Downloads
Patches
Plain Diff
example script keras lenet model mnist
parent
559784b2
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
skluc/examples/keras_fc_cnn_mnist.py
+84
-0
84 additions, 0 deletions
skluc/examples/keras_fc_cnn_mnist.py
with
84 additions
and
0 deletions
skluc/examples/keras_fc_cnn_mnist.py
0 → 100644
+
84
−
0
View file @
ee810747
import
numpy
as
np
from
keras
import
optimizers
from
keras.models
import
Sequential
from
keras.layers
import
Conv2D
,
Dense
,
Flatten
,
MaxPooling2D
,
Dropout
from
keras.callbacks
import
LearningRateScheduler
,
TensorBoard
from
keras.preprocessing.image
import
ImageDataGenerator
from
keras.regularizers
import
l2
import
skluc.data.mldatasets
as
dataset
batch_size
=
128
epochs
=
200
iterations
=
391
weight_decay
=
0.0001
log_filepath
=
'
./lenet_dp_da_wd
'
def
build_model
():
model
=
Sequential
()
model
.
add
(
Conv2D
(
32
,
(
5
,
5
),
padding
=
'
valid
'
,
activation
=
'
relu
'
,
kernel_initializer
=
'
he_normal
'
,
kernel_regularizer
=
l2
(
weight_decay
),
input_shape
=
(
28
,
28
,
1
),
name
=
"
conv_1
"
))
model
.
add
(
MaxPooling2D
((
2
,
2
),
strides
=
(
2
,
2
),
name
=
"
conv_pool_1
"
))
model
.
add
(
Conv2D
(
64
,
(
5
,
5
),
padding
=
'
valid
'
,
activation
=
'
relu
'
,
kernel_initializer
=
'
he_normal
'
,
kernel_regularizer
=
l2
(
weight_decay
),
name
=
"
conv_2
"
))
model
.
add
(
MaxPooling2D
((
2
,
2
),
strides
=
(
2
,
2
),
name
=
"
conv_pool_2
"
))
model
.
add
(
Flatten
(
name
=
'
conv_flatten
'
))
model
.
add
(
Dense
(
1024
,
activation
=
'
relu
'
,
kernel_initializer
=
'
he_normal
'
,
kernel_regularizer
=
l2
(
weight_decay
)))
model
.
add
(
Dropout
(
0.4
))
model
.
add
(
Dense
(
10
,
activation
=
'
softmax
'
,
kernel_initializer
=
'
he_normal
'
,
kernel_regularizer
=
l2
(
weight_decay
)))
sgd
=
optimizers
.
SGD
(
lr
=
.
001
,
momentum
=
0.9
,
nesterov
=
True
)
model
.
compile
(
loss
=
'
categorical_crossentropy
'
,
optimizer
=
sgd
,
metrics
=
[
'
accuracy
'
])
return
model
def
scheduler
(
epoch
):
if
epoch
<=
60
:
return
0.05
if
epoch
<=
120
:
return
0.01
if
epoch
<=
160
:
return
0.002
return
0.0004
if
__name__
==
'
__main__
'
:
# load data
validation_size
=
10000
seed
=
0
data
=
dataset
.
MnistDataset
(
validation_size
=
validation_size
,
seed
=
seed
)
data
.
load
()
data
.
normalize
()
data
.
to_one_hot
()
data
.
to_image
()
data
.
data_astype
(
np
.
float32
)
data
.
labels_astype
(
np
.
float32
)
(
x_train
,
y_train
),
(
x_test
,
y_test
)
=
data
.
train
,
data
.
test
# build network
model
=
build_model
()
print
(
model
.
summary
())
# set callback
tb_cb
=
TensorBoard
(
log_dir
=
log_filepath
,
histogram_freq
=
0
)
change_lr
=
LearningRateScheduler
(
scheduler
)
cbks
=
[
change_lr
,
tb_cb
]
# using real-time data augmentation
print
(
'
Using real-time data augmentation.
'
)
datagen
=
ImageDataGenerator
(
horizontal_flip
=
True
,
width_shift_range
=
0.125
,
height_shift_range
=
0.125
,
fill_mode
=
'
constant
'
,
cval
=
0.
)
datagen
.
fit
(
x_train
)
# start traing
model
.
fit_generator
(
datagen
.
flow
(
x_train
,
y_train
,
batch_size
=
batch_size
),
steps_per_epoch
=
iterations
,
epochs
=
epochs
,
callbacks
=
cbks
,
validation_data
=
(
x_test
,
y_test
))
# save model
model
.
save
(
'
lenet.h5
'
)
print
(
"
Final evaluation on test set: {}
"
.
format
(
model
.
evaluate
(
x_test
,
y_test
)))
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
register
or
sign in
to comment