Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hpvm-release
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
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
llvm
hpvm-release
Commits
6a9b519c
Commit
6a9b519c
authored
5 years ago
by
Hashim Sharif
Browse files
Options
Downloads
Plain Diff
Merging
parents
802c501f
d188cfd9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
llvm/projects/keras/src/mobilenet_imagenet.py
+62
-10
62 additions, 10 deletions
llvm/projects/keras/src/mobilenet_imagenet.py
with
62 additions
and
10 deletions
llvm/projects/keras/src/mobilenet_imagenet.py
+
62
−
10
View file @
6a9b519c
import
os
import
glob
import
random
import
scipy
import
scipy.io
import
cv2
import
numpy
as
np
import
tensorflow
as
tf
import
keras
from
keras.models
import
Sequential
,
Model
from
keras.layers
import
*
...
...
@@ -35,21 +37,21 @@ VAL_SIZE = 100
#
def relu6(x):
#
return K.relu(x, max_value=6)
def
relu6
(
x
):
return
K
.
relu
(
x
,
max_value
=
6
)
def
_conv_block
(
inputs
,
filters
,
alpha
,
kernel
=
(
3
,
3
),
strides
=
(
1
,
1
)):
channel_axis
=
1
filters
=
int
(
filters
*
alpha
)
x
=
ZeroPadding2D
(
padding
=
((
0
,
1
),
(
0
,
1
)))(
inputs
)
x
=
ZeroPadding2D
(
padding
=
((
1
,
1
),
(
1
,
1
)))(
inputs
)
x
=
Conv2D
(
filters
,
kernel
,
padding
=
'
valid
'
,
use_bias
=
False
,
strides
=
strides
)(
x
)
x
=
BatchNormalization
(
axis
=
channel_axis
)(
x
)
return
Activation
(
'
relu
'
)(
x
)
return
Activation
(
relu
6
)(
x
)
def
_depthwise_conv_block
(
inputs
,
pointwise_conv_filters
,
alpha
,
...
...
@@ -58,7 +60,7 @@ def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha,
pointwise_conv_filters
=
int
(
pointwise_conv_filters
*
alpha
)
if
strides
!=
(
1
,
1
):
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
))(
inputs
)
x
=
ZeroPadding2D
(
padding
=
(
(
1
,
1
)
,
(
1
,
1
))
)(
inputs
)
else
:
x
=
inputs
...
...
@@ -68,14 +70,14 @@ def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha,
strides
=
strides
,
use_bias
=
False
)(
x
)
x
=
BatchNormalization
(
axis
=
channel_axis
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
Activation
(
relu
6
)(
x
)
x
=
Conv2D
(
pointwise_conv_filters
,
(
1
,
1
),
padding
=
'
same
'
,
use_bias
=
False
,
strides
=
(
1
,
1
))(
x
)
x
=
BatchNormalization
(
axis
=
channel_axis
)(
x
)
return
Activation
(
'
relu
'
)(
x
)
return
Activation
(
relu
6
)(
x
)
...
...
@@ -112,11 +114,13 @@ def get_mobilenet_nchw_keras():
x
=
_depthwise_conv_block
(
x
,
1024
,
alpha
,
depth_multiplier
,
block_id
=
13
)
x
=
GlobalAveragePooling2D
()(
x
)
x
=
Dropout
(
dropout
)(
x
)
x
=
Dense
(
1000
)(
x
)
x
=
AveragePooling2D
((
7
,
7
))(
x
)
x
=
Conv2D
(
1000
,
(
1
,
1
),
padding
=
'
same
'
)(
x
)
x
=
Flatten
()(
x
)
x
=
Activation
(
'
softmax
'
)(
x
)
model
=
Model
(
img_input
,
x
)
...
...
@@ -129,6 +133,7 @@ def get_mobilenet_nchw_keras():
try
:
model
.
layers
[
j
].
set_weights
(
original_model
.
layers
[
i
].
get_weights
())
print
(
j
,
'
loaded
'
)
# model.layers[j].trainable = False
j
+=
1
except
:
print
(
j
,
'
skipped
'
,
model
.
layers
[
j
])
...
...
@@ -205,6 +210,53 @@ y_true = np.array(y_true)
def
train_helper
(
x
):
try
:
x
=
x
.
decode
(
'
utf-8
'
)
except
:
pass
image
=
load_image
(
x
)
y
=
np
.
zeros
(
1000
,
dtype
=
np
.
uint8
)
y
[
synset_to_keras_idx
[
x
.
split
(
'
/
'
)[
-
2
]]]
=
1
return
image
,
y
train_images
=
glob
.
glob
(
IMAGENET_DIR
+
'
train/*/*
'
)
random
.
shuffle
(
train_images
)
dataset
=
tf
.
data
.
Dataset
().
from_tensor_slices
(
train_images
)
dataset
=
dataset
.
map
(
lambda
x
:
tf
.
py_func
(
train_helper
,
[
x
],
[
tf
.
float32
,
tf
.
uint8
]),
num_parallel_calls
=
16
)
dataset
=
dataset
.
shuffle
(
buffer_size
=
1000
)
dataset
=
dataset
.
batch
(
64
)
dataset
=
dataset
.
repeat
()
next_element
=
dataset
.
make_one_shot_iterator
().
get_next
()
sess
=
tf
.
Session
()
def
generate
():
while
True
:
yield
sess
.
run
(
next_element
)
model
.
compile
(
optimizer
=
keras
.
optimizers
.
Adam
(
lr
=
0.00001
),
loss
=
'
categorical_crossentropy
'
,
metrics
=
[
'
acc
'
])
model
.
fit_generator
(
generate
(),
steps_per_epoch
=
1000
,
validation_data
=
(
X_test
,
to_categorical
(
y_true
,
num_classes
=
1000
)),
epochs
=
5
)
translate_to_approxhpvm
(
model
,
OUTPUT_DIR
,
X_test
[:
VAL_SIZE
],
y_true
[:
VAL_SIZE
],
1000
)
dumpCalibrationData
(
OUTPUT_DIR
+
'
test_input.bin
'
,
X_test
,
OUTPUT_DIR
+
'
test_labels.bin
'
,
y_true
)
...
...
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