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
a39dd418
Commit
a39dd418
authored
4 years ago
by
nz11
Browse files
Options
Downloads
Patches
Plain Diff
Update vgg16_imagenet.py
parent
f9d15930
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
+78
-60
78 additions, 60 deletions
llvm/projects/keras/src/vgg16_imagenet.py
with
78 additions
and
60 deletions
llvm/projects/keras/src/vgg16_imagenet.py
+
78
−
60
View file @
a39dd418
import
os
import
os
import
glob
import
glob
import
random
import
scipy
import
scipy
import
scipy.io
import
scipy.io
import
cv2
import
cv2
import
numpy
as
np
import
numpy
as
np
import
tensorflow
as
tf
import
keras
import
keras
from
keras.models
import
Sequential
,
Model
from
keras.models
import
Sequential
,
Model
from
keras.layers
import
*
from
keras.layers
import
*
from
keras.applications.vgg16
import
VGG16
,
preprocess_input
from
keras.utils
import
to_categorical
from
keras.utils
import
to_categorical
from
keras.applications.vgg16
import
VGG16
,
preprocess_input
from
keras
import
backend
as
K
from
keras
import
backend
as
K
from
frontend.approxhpvm_translator
import
translate_to_approxhpvm
from
frontend.approxhpvm_translator
import
translate_to_approxhpvm
from
frontend.weight_utils
import
dumpCalibrationData
from
frontend.weight_utils
import
dumpCalibrationData
2
np
.
random
.
seed
(
0
)
np
.
random
.
seed
(
202
0
)
os
.
environ
[
"
CUDA_VISIBLE_DEVICES
"
]
=
"
1
"
os
.
environ
[
"
CUDA_VISIBLE_DEVICES
"
]
=
"
1
"
K
.
set_image_data_format
(
'
channels_first
'
)
K
.
set_image_data_format
(
'
channels_first
'
)
data_format
=
'
channels_first
'
num_images
=
5000
val_size
=
100
data_format
=
'
channels_first
'
IMAGENET_DIR
=
'
/home/nz11/ILSVRC2012/
'
OUTPUT_DIR
=
'
data/vgg16_imagenet_tune/
'
NUM_CLASSES
=
200
IMAGES_PER_CLASS
=
40
# VAL_SIZE = 100
...
@@ -111,12 +116,28 @@ def get_vgg16_nchw_keras():
...
@@ -111,12 +116,28 @@ def get_vgg16_nchw_keras():
x
=
Dense
(
1000
)(
x
)
x
=
Dense
(
1000
)(
x
)
x
=
Activation
(
'
softmax
'
)(
x
)
x
=
Activation
(
'
softmax
'
)(
x
)
model
=
Model
(
img_input
,
x
)
model
_nchw
=
Model
(
img_input
,
x
)
return
model
model
=
VGG16
()
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
return
model_nchw
def
load_image
(
x
):
def
load_image
(
x
):
image
=
cv2
.
imread
(
x
)
image
=
cv2
.
imread
(
x
)
height
,
width
,
_
=
image
.
shape
height
,
width
,
_
=
image
.
shape
...
@@ -136,76 +157,73 @@ def load_image(x):
...
@@ -136,76 +157,73 @@ def load_image(x):
return
image
.
astype
(
np
.
float32
)
return
image
.
astype
(
np
.
float32
)
meta
=
scipy
.
io
.
loadmat
(
IMAGENET_DIR
+
'
ILSVRC2012_devkit_t12/data/meta.mat
'
)
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
=
{}
original_idx_to_synset
=
{}
synset_to_name
=
{}
synset_to_name
=
{}
for
i
in
range
(
1000
):
for
i
in
range
(
1000
):
ilsvrc2012_id
=
int
(
meta
[
"
synsets
"
][
i
,
0
][
0
][
0
][
0
])
ilsvrc2012_id
=
int
(
meta
[
'
synsets
'
][
i
,
0
][
0
][
0
][
0
])
synset
=
meta
[
"
synsets
"
][
i
,
0
][
1
][
0
]
synset
=
meta
[
'
synsets
'
][
i
,
0
][
1
][
0
]
name
=
meta
[
"
synsets
"
][
i
,
0
][
2
][
0
]
name
=
meta
[
'
synsets
'
][
i
,
0
][
2
][
0
]
original_idx_to_synset
[
ilsvrc2012_id
]
=
synset
original_idx_to_synset
[
ilsvrc2012_id
]
=
synset
synset_to_name
[
synset
]
=
name
synset_to_name
[
synset
]
=
name
synset_to_keras_idx
=
{}
synset_to_keras_idx
=
{}
keras_idx_to_name
=
{}
keras_idx_to_name
=
{}
f
=
open
(
"
/home/nz11/ILSVRC2012/
ILSVRC2012_devkit_t12/data/synset_words.txt
"
,
"
r
"
)
f
=
open
(
IMAGENET_DIR
+
'
ILSVRC2012_devkit_t12/data/synset_words.txt
'
,
'
r
'
)
c
=
0
c
=
0
for
line
in
f
:
for
line
in
f
:
parts
=
line
.
split
(
"
"
)
parts
=
line
.
split
(
'
'
)
synset_to_keras_idx
[
parts
[
0
]]
=
c
synset_to_keras_idx
[
parts
[
0
]]
=
c
keras_idx_to_name
[
c
]
=
"
"
.
join
(
parts
[
1
:])
keras_idx_to_name
[
c
]
=
'
'
.
join
(
parts
[
1
:])
c
+=
1
c
+=
1
f
.
close
()
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
)
y_true
=
np
.
expand_dims
(
y_true
,
axis
=-
1
)
model
=
get_vgg16_nchw_keras
()
X_tune
,
X_test
=
[],
[]
y_tune
,
y_true
=
[],
[]
classes
=
glob
.
glob
(
IMAGENET_DIR
+
'
val/*
'
)
for
c
in
np
.
random
.
permutation
(
len
(
classes
))[:
NUM_CLASSES
]:
x
=
glob
.
glob
(
classes
[
c
]
+
'
/*
'
)
x
=
np
.
array
(
x
)
idx
=
np
.
random
.
permutation
(
len
(
x
))
idx
=
idx
[:
max
(
len
(
idx
),
IMAGES_PER_CLASS
)]
synset
=
classes
[
c
].
split
(
'
/
'
)[
-
1
]
images
=
list
(
map
(
lambda
x
:
load_image
(
x
),
x
[
idx
]))
labels
=
[
synset_to_keras_idx
[
synset
]]
*
len
(
x
[
idx
])
X_test
+=
images
[:
IMAGES_PER_CLASS
//
2
]
y_true
+=
labels
[:
IMAGES_PER_CLASS
//
2
]
X_tune
+=
images
[
IMAGES_PER_CLASS
//
2
:]
y_tune
+=
labels
[
IMAGES_PER_CLASS
//
2
:]
X_test
=
np
.
array
(
X_test
)
y_true
=
np
.
array
(
y_true
)
X_tune
=
np
.
array
(
X_tune
)
y_tune
=
np
.
array
(
y_tune
)
translate_to_approxhpvm
(
model
,
OUTPUT_DIR
,
X_tune
,
y_tune
,
1000
)
# dumpCalibrationData2(OUTPUT_DIR + 'test_input_10K.bin', X_test, OUTPUT_DIR + 'test_labels_10K.bin', y_true)
dumpCalibrationData2
(
OUTPUT_DIR
+
'
tune_input.bin
'
,
X_tune
,
OUTPUT_DIR
+
'
tune_labels.bin
'
,
y_tune
)
dumpCalibrationData2
(
OUTPUT_DIR
+
'
test_input.bin
'
,
X_test
,
OUTPUT_DIR
+
'
test_labels.bin
'
,
y_true
)
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
)
pred
=
np
.
argmax
(
model
.
predict
(
X_test
),
axis
=
1
)
print
(
'
val accuracy
'
,
np
.
sum
(
pred
==
y_true
.
ravel
())
/
val_size
)
print
(
'
val accuracy
'
,
np
.
sum
(
pred
==
y_true
.
ravel
())
/
len
(
X_test
))
pred
=
np
.
argmax
(
model
.
predict
(
X_tune
),
axis
=
1
)
print
(
'
val accuracy
'
,
np
.
sum
(
pred
==
y_tune
.
ravel
())
/
len
(
X_tune
))
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