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
392f2ba8
Commit
392f2ba8
authored
4 years ago
by
Hashim Sharif
Browse files
Options
Downloads
Patches
Plain Diff
Keras: Creating top level class for all Benchmarks
parent
317b22f6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
llvm/projects/keras/src/Benchmark.py
+25
-0
25 additions, 0 deletions
llvm/projects/keras/src/Benchmark.py
llvm/projects/keras/src/__init__.py
+0
-0
0 additions, 0 deletions
llvm/projects/keras/src/__init__.py
llvm/projects/keras/src/alexnet.py
+144
-136
144 additions, 136 deletions
llvm/projects/keras/src/alexnet.py
with
169 additions
and
136 deletions
llvm/projects/keras/src/Benchmark.py
0 → 100644
+
25
−
0
View file @
392f2ba8
class
Benchmark
:
def
__init__
(
self
):
return
def
buildModel
(
self
):
return
def
data_preprocess
(
self
):
return
def
trainModel
(
self
):
return
def
inference
(
self
):
return
def
run
(
self
):
return
This diff is collapsed.
Click to expand it.
llvm/projects/keras/src/__init__.py
0 → 100644
+
0
−
0
View file @
392f2ba8
This diff is collapsed.
Click to expand it.
llvm/projects/keras/src/alexnet.py
+
144
−
136
View file @
392f2ba8
...
...
@@ -18,176 +18,182 @@ import struct
import
keras
import
numpy
as
np
import
os
from
Benchmark
import
Benchmark
from
frontend.approxhpvm_translator
import
translate_to_approxhpvm
from
frontend.weight_utils
import
dumpCalibrationData
from
frontend.weight_utils
import
dumpHPVMToKerasModel
def
lr_schedule
(
epoch
):
lrate
=
0.001
if
epoch
>
20
:
lrate
=
0.0005
if
epoch
>
40
:
lrate
=
0.0003
if
epoch
>
60
:
lrate
=
0.0001
if
epoch
>
80
:
lrate
=
0.00005
return
lrate
class
AlexNet
(
Benchmark
):
def
buildModel
():
def
__init__
(
self
):
self
.
name
=
"
AlexNet
"
def
lr_schedule
(
self
,
epoch
):
lrate
=
0.001
if
epoch
>
20
:
lrate
=
0.0005
if
epoch
>
40
:
lrate
=
0.0003
if
epoch
>
60
:
lrate
=
0.0001
if
epoch
>
80
:
lrate
=
0.00005
activation_type
=
"
tanh
"
weight_decay
=
1e-4
model
=
Sequential
()
model
.
add
(
Conv2D
(
64
,
kernel_size
=
(
11
,
11
),
activation
=
activation_type
,
input_shape
=
(
3
,
32
,
32
),
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
),
strides
=
(
2
,
2
)
))
model
.
add
(
Dropout
(
0.2
))
model
.
add
(
Conv2D
(
192
,
kernel_size
=
(
5
,
5
),
activation
=
activation_type
,
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
),
strides
=
(
2
,
2
)
))
model
.
add
(
Dropout
(
0.3
))
model
.
add
(
Conv2D
(
384
,
kernel_size
=
(
3
,
3
),
activation
=
activation_type
,
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)
))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
activation_type
,
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)
))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
activation_type
,
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
),
strides
=
(
2
,
2
)
))
model
.
add
(
Dropout
(
0.4
))
model
.
add
(
Flatten
())
#model.add(Flatten())
#model.add(Dense(256))
model
.
add
(
Dense
(
10
))
model
.
add
(
Activation
(
'
softmax
'
))
return
model
return
lrate
def
buildModel_old
():
model
=
Sequential
()
model
.
add
(
Conv2D
(
128
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
input_shape
=
(
3
,
32
,
32
),
padding
=
'
same
'
))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
padding
=
'
same
'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
#model.add(Dropout(0.25))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
padding
=
'
same
'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
padding
=
'
same
'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
padding
=
'
same
'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
#model.add(Dropout(0.25))
model
.
add
(
Flatten
())
#model.add(Flatten())
model
.
add
(
Dense
(
4096
,
activation
=
'
tanh
'
))
#model.add(Dropout(0.5))
model
.
add
(
Dense
(
2048
,
activation
=
'
tanh
'
))
model
.
add
(
Dense
(
10
,
activation
=
'
tanh
'
))
model
.
add
(
Activation
(
'
softmax
'
))
return
model
def
buildModel
(
self
):
activation_type
=
"
tanh
"
weight_decay
=
1e-4
model
=
Sequential
()
model
.
add
(
Conv2D
(
64
,
kernel_size
=
(
11
,
11
),
activation
=
activation_type
,
input_shape
=
(
3
,
32
,
32
),
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
),
strides
=
(
2
,
2
)
))
model
.
add
(
Dropout
(
0.2
))
model
.
add
(
Conv2D
(
192
,
kernel_size
=
(
5
,
5
),
activation
=
activation_type
,
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
),
strides
=
(
2
,
2
)
))
model
.
add
(
Dropout
(
0.3
))
model
.
add
(
Conv2D
(
384
,
kernel_size
=
(
3
,
3
),
activation
=
activation_type
,
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)
))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
activation_type
,
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)
))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
activation_type
,
padding
=
'
same
'
,
kernel_regularizer
=
regularizers
.
l2
(
weight_decay
)
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
),
strides
=
(
2
,
2
)
))
model
.
add
(
Dropout
(
0.4
))
model
.
add
(
Flatten
())
#model.add(Flatten())
#model.add(Dense(256))
model
.
add
(
Dense
(
10
))
model
.
add
(
Activation
(
'
softmax
'
))
return
model
def
buildModel_old
():
model
=
Sequential
()
model
.
add
(
Conv2D
(
128
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
input_shape
=
(
3
,
32
,
32
),
padding
=
'
same
'
))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
padding
=
'
same
'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
#model.add(Dropout(0.25))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
padding
=
'
same
'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
padding
=
'
same
'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
model
.
add
(
Conv2D
(
256
,
kernel_size
=
(
3
,
3
),
activation
=
'
tanh
'
,
padding
=
'
same
'
))
model
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
#model.add(Dropout(0.25))
model
.
add
(
Flatten
())
#model.add(Flatten())
model
.
add
(
Dense
(
4096
,
activation
=
'
tanh
'
))
#model.add(Dropout(0.5))
model
.
add
(
Dense
(
2048
,
activation
=
'
tanh
'
))
model
.
add
(
Dense
(
10
,
activation
=
'
tanh
'
))
model
.
add
(
Activation
(
'
softmax
'
))
return
model
def
trainModel
(
model
):
(
X_train
,
Y_train
),
(
X_test
,
Y_test
)
=
cifar10
.
load_data
()
test_labels
=
Y_test
train_labels
=
Y_train
#X_train = X_train.astype('float32')
#X_test = X_test.astype('float32')
X_train
=
X_train
/
255.0
X_test
=
X_test
/
255.0
mean
=
np
.
mean
(
X_train
,
axis
=
(
0
,
1
,
2
,
3
))
std
=
np
.
std
(
X_train
,
axis
=
(
0
,
1
,
2
,
3
))
X_train
=
(
X_train
-
mean
)
/
(
std
+
1e-7
)
X_test
=
(
X_test
-
mean
)
/
(
std
+
1e-7
)
dir_prefix
=
"
/home/hsharif3/Gitlab/hpvm/llvm/projects/hpvm-tensor-rt/model_params/alexnet_cifar10/
"
#opt_rms = keras.optimizers.rmsprop(lr=0.001,decay=1e-6)
# Compile the model
model
.
compile
(
loss
=
'
categorical_crossentropy
'
,
optimizer
=
Adam
(
lr
=
0.0001
,
decay
=
1e-6
),
#optimizer = opt_rms,
metrics
=
[
'
accuracy
'
])
def
trainModel
(
self
,
model
):
#print to_categorical(Y_train, 10)
print
(
to_categorical
(
Y_train
))
(
X_train
,
Y_train
),
(
X_test
,
Y_test
)
=
cifar10
.
load_data
()
test_labels
=
Y_test
train_labels
=
Y_train
#X_train = X_train.astype('float32')
#X_test = X_test.astype('float32')
X_train
=
X_train
/
255.0
X_test
=
X_test
/
255.0
datagen
=
ImageDataGenerator
(
mean
=
np
.
mean
(
X_train
,
axis
=
(
0
,
1
,
2
,
3
))
std
=
np
.
std
(
X_train
,
axis
=
(
0
,
1
,
2
,
3
))
X_train
=
(
X_train
-
mean
)
/
(
std
+
1e-7
)
X_test
=
(
X_test
-
mean
)
/
(
std
+
1e-7
)
dir_prefix
=
"
/home/hsharif3/Gitlab/hpvm/llvm/projects/hpvm-tensor-rt/model_params/alexnet_cifar10/
"
#opt_rms = keras.optimizers.rmsprop(lr=0.001,decay=1e-6)
# Compile the model
model
.
compile
(
loss
=
'
categorical_crossentropy
'
,
optimizer
=
Adam
(
lr
=
0.0001
,
decay
=
1e-6
),
#optimizer = opt_rms,
metrics
=
[
'
accuracy
'
])
#print to_categorical(Y_train, 10)
print
(
to_categorical
(
Y_train
))
datagen
=
ImageDataGenerator
(
rotation_range
=
15
,
width_shift_range
=
0.1
,
height_shift_range
=
0.1
,
horizontal_flip
=
True
,
)
datagen
.
fit
(
X_train
)
)
datagen
.
fit
(
X_train
)
model
.
fit
(
X_train
,
to_categorical
(
Y_train
,
10
),
batch_size
=
128
,
shuffle
=
True
,
epochs
=
1
,
#epochs=100,
validation_data
=
(
X_test
,
to_categorical
(
Y_test
,
10
)),
callbacks
=
[
LearningRateScheduler
(
lr_schedule
)])
# Evaluate the model
scores
=
model
.
evaluate
(
X_test
,
to_categorical
(
Y_test
,
10
))
print
(
'
Loss: %.3f
'
%
scores
[
0
])
print
(
'
Accuracy: %.3f
'
%
scores
[
1
])
print
(
"
*** TRAINED MODEL ****
\n
"
)
#dumpCalibrationData("calibration_data/alexnet_calib.bin", X_train,
# "calibration_data/alexnet_train_labels.bin", train_labels)
model
.
fit
(
X_train
,
to_categorical
(
Y_train
,
10
),
batch_size
=
128
,
shuffle
=
True
,
epochs
=
1
,
#epochs=100,
validation_data
=
(
X_test
,
to_categorical
(
Y_test
,
10
)),
callbacks
=
[
LearningRateScheduler
(
self
.
lr_schedule
)])
# Evaluate the model
scores
=
model
.
evaluate
(
X_test
,
to_categorical
(
Y_test
,
10
))
print
(
'
Loss: %.3f
'
%
scores
[
0
])
print
(
'
Accuracy: %.3f
'
%
scores
[
1
])
print
(
"
*** TRAINED MODEL ****
\n
"
)
def
reloadKerasModel
(
model_path
):
return
model
model
=
load_model
(
model_path
)
score
=
model
.
evaluate
(
X_test
,
to_categorical
(
Y_test
,
10
),
verbose
=
0
)
print
(
'
Test loss2:
'
,
score
[
0
])
print
(
'
Test accuracy2:
'
,
score
[
1
])
def
data_preprocess
():
def
reloadKerasModel
(
model_path
):
(
X_train
,
Y_train
),
(
X_test
,
Y_test
)
=
cifar10
.
load_data
()
model
=
load_model
(
model_path
)
score
=
model
.
evaluate
(
X_test
,
to_categorical
(
Y_test
,
10
),
verbose
=
0
)
print
(
'
Test loss2:
'
,
score
[
0
])
print
(
'
Test accuracy2:
'
,
score
[
1
])
def
data_preprocess
(
self
):
(
X_train
,
Y_train
),
(
X_test
,
Y_test
)
=
cifar10
.
load_data
()
X_train
=
X_train
/
255.0
X_test
=
X_test
/
255.0
mean
=
np
.
mean
(
X_train
,
axis
=
(
0
,
1
,
2
,
3
))
std
=
np
.
std
(
X_train
,
axis
=
(
0
,
1
,
2
,
3
))
X_train
=
(
X_train
-
mean
)
/
(
std
+
1e-7
)
X_test
=
(
X_test
-
mean
)
/
(
std
+
1e-7
)
return
X_train
,
Y_train
,
X_test
,
Y_test
X_train
=
X_train
/
255.0
X_test
=
X_test
/
255.0
mean
=
np
.
mean
(
X_train
,
axis
=
(
0
,
1
,
2
,
3
))
std
=
np
.
std
(
X_train
,
axis
=
(
0
,
1
,
2
,
3
))
X_train
=
(
X_train
-
mean
)
/
(
std
+
1e-7
)
X_test
=
(
X_test
-
mean
)
/
(
std
+
1e-7
)
return
X_train
,
Y_train
,
X_test
,
Y_test
if
__name__
==
"
__main__
"
:
...
...
@@ -199,9 +205,11 @@ if __name__ == "__main__":
# Changing to NCHW format
K
.
set_image_data_format
(
'
channels_first
'
)
model
=
buildModel
()
alexnet
=
AlexNet
()
model
=
alexnet
.
buildModel
()
X_train
,
Y_train
,
X_test
,
Y_test
=
data_preprocess
()
X_train
,
Y_train
,
X_test
,
Y_test
=
alexnet
.
data_preprocess
()
reload_dir
=
"
/home/hsharif3/Gitlab/hpvm/llvm/projects/hpvm-tensor-rt/model_params/alexnet_cifar10/
"
keras_model_file
=
"
alexnet.h5
"
...
...
@@ -212,7 +220,7 @@ if __name__ == "__main__":
model
=
load_model
(
keras_model_file
)
if
sys
.
argv
[
1
]
==
"
train
"
:
model
=
trainModel
(
model
)
model
=
alexnet
.
trainModel
(
model
)
num_classes
=
10
score
=
model
.
evaluate
(
X_test
,
to_categorical
(
Y_test
,
num_classes
),
verbose
=
0
)
...
...
@@ -220,7 +228,7 @@ if __name__ == "__main__":
if
len
(
sys
.
argv
)
>
2
and
sys
.
argv
[
2
]
==
"
frontend
"
:
if
sys
.
argv
[
1
]
!
=
"
hpvm
_reload
"
:
if
sys
.
argv
[
1
]
=
=
"
keras
_reload
"
:
print
(
"
ERROR: Must load HPVM model to invoke frontend
"
)
sys
.
exit
(
1
)
...
...
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