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
64a46597
Commit
64a46597
authored
5 years ago
by
Nathan Zhao
Browse files
Options
Downloads
Patches
Plain Diff
Adding VGG ImageNet
parent
2a59ce5e
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/vgg16_imagenet.py
+225
-0
225 additions, 0 deletions
llvm/projects/keras/src/vgg16_imagenet.py
with
225 additions
and
0 deletions
llvm/projects/keras/src/vgg16_imagenet.py
0 → 100644
+
225
−
0
View file @
64a46597
import
os
import
glob
import
scipy
import
scipy.io
import
cv2
import
keras
from
keras.models
import
Model
from
keras.layers
import
*
from
keras.applications.vgg16
import
VGG16
,
preprocess_input
from
keras.utils
import
to_categorical
from
frontend.approxhpvm_translator
import
translate_to_approxhpvm
from
frontend.weight_utils
import
dumpCalibrationData
data_format
=
'
channels_first
'
def
get_vgg16_nchw_keras
():
img_input
=
Input
(
shape
=
(
3
,
224
,
224
))
# Block 1
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
img_input
)
x
=
Conv2D
(
64
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
64
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
MaxPooling2D
((
2
,
2
),
strides
=
(
2
,
2
),
data_format
=
data_format
)(
x
)
# Block 2
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
128
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
128
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
MaxPooling2D
((
2
,
2
),
strides
=
(
2
,
2
),
data_format
=
data_format
)(
x
)
# Block 3
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
256
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
256
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
256
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
MaxPooling2D
((
2
,
2
),
strides
=
(
2
,
2
),
data_format
=
data_format
)(
x
)
# Block 4
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
512
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
512
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
512
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
MaxPooling2D
((
2
,
2
),
strides
=
(
2
,
2
),
data_format
=
data_format
)(
x
)
# Block 5
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
512
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
512
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
512
,
(
3
,
3
),
padding
=
'
valid
'
,
data_format
=
data_format
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
MaxPooling2D
((
2
,
2
),
strides
=
(
2
,
2
),
data_format
=
data_format
)(
x
)
x
=
Flatten
(
data_format
=
data_format
)(
x
)
x
=
Dense
(
4096
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
Dense
(
4096
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
Dense
(
1000
)(
x
)
x
=
Activation
(
'
softmax
'
)(
x
)
model
=
Model
(
img_input
,
x
)
return
model
def
load_image
(
x
):
try
:
x
=
x
.
decode
(
'
utf-8
'
)
except
:
pass
image
=
cv2
.
imread
(
x
)
height
,
width
,
_
=
image
.
shape
new_height
=
height
*
256
//
min
(
image
.
shape
[:
2
])
new_width
=
width
*
256
//
min
(
image
.
shape
[:
2
])
image
=
cv2
.
resize
(
image
,
(
new_width
,
new_height
),
interpolation
=
cv2
.
INTER_CUBIC
)
height
,
width
,
_
=
image
.
shape
startx
=
width
//
2
-
(
224
//
2
)
starty
=
height
//
2
-
(
224
//
2
)
image
=
image
[
starty
:
starty
+
224
,
startx
:
startx
+
224
]
image
=
image
[:,
:,
::
-
1
]
image
=
preprocess_input
(
image
.
astype
(
np
.
float32
))
image
=
np
.
transpose
(
image
,
(
2
,
0
,
1
))
return
image
.
astype
(
np
.
float32
)
if
__name__
==
'
__main__
'
:
os
.
environ
[
"
CUDA_VISIBLE_DEVICES
"
]
=
"
1
"
num_images
=
5000
val_size
=
100
model
=
VGG16
()
model_nchw
=
get_vgg16_nchw_keras
()
j
=
0
for
i
in
range
(
len
(
model_nchw
.
layers
)):
if
'
padding
'
in
model_nchw
.
layers
[
i
].
name
or
'
activation
'
in
model_nchw
.
layers
[
i
].
name
:
continue
try
:
model_nchw
.
layers
[
i
].
set_weights
(
model
.
layers
[
j
].
get_weights
())
except
:
print
(
i
,
model_nchw
.
layers
[
i
],
'
skipped
'
)
j
+=
1
classes
=
os
.
listdir
(
'
/home/nz11/ILSVRC2012/train
'
)
train_images
=
glob
.
glob
(
'
/home/nz11/ILSVRC2012/train/*/*
'
)
val_images
=
glob
.
glob
(
'
/home/nz11/ILSVRC2012/val/*/*
'
)
val_images
=
sorted
(
val_images
,
key
=
lambda
x
:
x
.
split
(
'
/
'
)[
-
1
].
split
(
'
_
'
)[
-
1
].
split
(
'
.
'
)[
0
])
idx
=
np
.
random
.
permutation
(
len
(
val_images
))[:
num_images
]
val_images
=
np
.
array
(
val_images
)[
idx
]
d
=
{
k
:
v
for
v
,
k
in
enumerate
(
classes
)}
X_test
=
[]
for
x
in
val_images
:
X_test
.
append
(
load_image
(
x
))
X_test
=
np
.
array
(
X_test
)
meta
=
scipy
.
io
.
loadmat
(
"
/home/nz11/ILSVRC2012/ILSVRC2012_devkit_t12/data/meta.mat
"
)
original_idx_to_synset
=
{}
synset_to_name
=
{}
for
i
in
range
(
1000
):
ilsvrc2012_id
=
int
(
meta
[
"
synsets
"
][
i
,
0
][
0
][
0
][
0
])
synset
=
meta
[
"
synsets
"
][
i
,
0
][
1
][
0
]
name
=
meta
[
"
synsets
"
][
i
,
0
][
2
][
0
]
original_idx_to_synset
[
ilsvrc2012_id
]
=
synset
synset_to_name
[
synset
]
=
name
synset_to_keras_idx
=
{}
keras_idx_to_name
=
{}
f
=
open
(
"
/home/nz11/ILSVRC2012/ILSVRC2012_devkit_t12/data/synset_words.txt
"
,
"
r
"
)
c
=
0
for
line
in
f
:
parts
=
line
.
split
(
"
"
)
synset_to_keras_idx
[
parts
[
0
]]
=
c
keras_idx_to_name
[
c
]
=
"
"
.
join
(
parts
[
1
:])
c
+=
1
f
.
close
()
def
convert_original_idx_to_keras_idx
(
idx
):
return
synset_to_keras_idx
[
original_idx_to_synset
[
idx
]]
f
=
open
(
"
/home/nz11/ILSVRC2012/ILSVRC2012_devkit_t12/data/ILSVRC2012_validation_ground_truth.txt
"
,
"
r
"
)
y_true
=
f
.
read
().
strip
().
split
(
"
\n
"
)
y_true
=
list
(
map
(
int
,
y_true
))
y_true
=
np
.
array
([
convert_original_idx_to_keras_idx
(
idx
)
for
idx
in
y_true
])[
idx
]
y_true
=
to_categorical
(
y_true
,
num_classes
=
1000
)
f
.
close
()
translate_to_approxhpvm
(
model_nchw
,
"
data/vgg16_imagenet/
"
,
X_test
[:
val_size
],
y_true
[:
val_size
],
1000
)
dumpCalibrationData
(
"
data/vgg16_imagenet/test_input.bin
"
,
X_test
,
"
data/vgg16_imagenet/test_labels.bin
"
,
y_true
)
y_pred
=
model_nchw
.
predict
(
X_test
[:
val_size
])
print
(
'
val accuracy
'
,
np
.
sum
(
np
.
argmax
(
y_pred
,
axis
=
1
)
==
np
.
argmax
(
y_true
[:
val_size
],
axis
=
1
))
/
val_size
)
\ 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
register
or
sign in
to comment