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
33ee6eb4
Commit
33ee6eb4
authored
5 years ago
by
nz11
Browse files
Options
Downloads
Patches
Plain Diff
Update vgg16_imagenet.py
parent
c9fe8348
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
+98
-106
98 additions, 106 deletions
llvm/projects/keras/src/vgg16_imagenet.py
with
98 additions
and
106 deletions
llvm/projects/keras/src/vgg16_imagenet.py
+
98
−
106
View file @
33ee6eb4
...
...
@@ -4,102 +4,105 @@ import glob
import
scipy
import
scipy.io
import
cv2
import
numpy
as
np
import
keras
from
keras.models
import
Model
from
keras.models
import
Sequential
,
Model
from
keras.layers
import
*
from
keras.applications.vgg16
import
VGG16
,
preprocess_input
from
keras.utils
import
to_categorical
from
keras
import
backend
as
K
from
frontend.approxhpvm_translator
import
translate_to_approxhpvm
from
frontend.weight_utils
import
dumpCalibrationData
np
.
random
.
seed
(
0
)
os
.
environ
[
"
CUDA_VISIBLE_DEVICES
"
]
=
"
1
"
K
.
set_image_data_format
(
'
channels_first
'
)
num_images
=
5000
val_size
=
100
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
)
padding
=
'
same
'
,
data_format
=
data_format
)(
img_input
)
x
=
Activation
(
'
relu
'
)(
x
)
x
=
ZeroPadding2D
(
padding
=
(
1
,
1
),
data_format
=
data_format
)(
x
)
x
=
Conv2D
(
64
,
(
3
,
3
),
padding
=
'
valid
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
'
,
padding
=
'
same
'
,
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
=
Flatten
(
data_format
=
'
channels_last
'
)(
x
)
x
=
Dense
(
4096
)(
x
)
x
=
Activation
(
'
relu
'
)(
x
)
...
...
@@ -114,11 +117,6 @@ def get_vgg16_nchw_keras():
def
load_image
(
x
):
try
:
x
=
x
.
decode
(
'
utf-8
'
)
except
:
pass
image
=
cv2
.
imread
(
x
)
height
,
width
,
_
=
image
.
shape
...
...
@@ -132,88 +130,82 @@ def load_image(x):
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
))
image
=
preprocess_input
(
image
.
astype
(
np
.
float32
),
data_format
=
data_format
)
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
])
model
=
VGG16
()
model_nchw
=
get_vgg16_nchw_keras
()
idx
=
np
.
random
.
permutation
(
len
(
val_images
))[:
num_images
]
val_images
=
np
.
array
(
val_images
)[
idx
]
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
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
"
)
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
]]
with
open
(
"
/home/nz11/ILSVRC2012/ILSVRC2012_devkit_t12/data/ILSVRC2012_validation_ground_truth.txt
"
,
"
r
"
)
as
f
:
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
=
y_true
.
astype
(
np
.
uint32
)
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
)
\ No newline at end of file
y_true
=
np
.
expand_dims
(
y_true
,
axis
=-
1
)
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
)
pred
=
np
.
argmax
(
model_nchw
.
predict
(
X_test
),
axis
=
1
)
print
(
'
val accuracy
'
,
np
.
sum
(
pred
==
y_true
.
ravel
())
/
val_size
)
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