Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
monticore
EmbeddedMontiArc
generators
CNNArch2Caffe2
Commits
de0f235f
Commit
de0f235f
authored
Sep 28, 2018
by
Carlos Alfredo Yeverino Rodriguez
Browse files
Changed the parameter "device_opts" from the train function to "context"
parent
98f61d02
Changes
7
Hide whitespace changes
Inline
Side-by-side
src/main/resources/templates/caffe2/CNNCreator.ftl
View file @
de0f235f
...
...
@@ -99,11 +99,11 @@ ${tc.include(tc.architecture.body)}
accuracy = brew.accuracy(model, [output, label], "accuracy", top_k=3)
return accuracy
def train(self, num_epoch=1000, batch_size=64,
device_opts
='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if
device_opts
== 'cpu':
def train(self, num_epoch=1000, batch_size=64,
context
='gpu', eval_metric='accuracy', opt_type='adam', base_learning_rate=0.001, weight_decay=0.001, policy='fixed', stepsize=1, epsilon=1E-8, beta1=0.9, beta2=0.999, gamma=0.999, momentum=0.9) :
if
context
== 'cpu':
device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
print("CPU mode selected")
elif
device_opts
== 'gpu':
elif
context
== 'gpu':
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)
print("GPU mode selected")
...
...
src/test/resources/target_code/CNNCreator_Alexnet.py
View file @
de0f235f
...
...
@@ -192,11 +192,11 @@ class CNNCreator_Alexnet:
accuracy
=
brew
.
accuracy
(
model
,
[
output
,
label
],
"accuracy"
,
top_k
=
3
)
return
accuracy
def
train
(
self
,
num_epoch
=
1000
,
batch_size
=
64
,
device_opts
=
'gpu'
,
eval_metric
=
'accuracy'
,
opt_type
=
'adam'
,
base_learning_rate
=
0.001
,
weight_decay
=
0.001
,
policy
=
'fixed'
,
stepsize
=
1
,
epsilon
=
1E-8
,
beta1
=
0.9
,
beta2
=
0.999
,
gamma
=
0.999
,
momentum
=
0.9
)
:
if
device_opts
==
'cpu'
:
def
train
(
self
,
num_epoch
=
1000
,
batch_size
=
64
,
context
=
'gpu'
,
eval_metric
=
'accuracy'
,
opt_type
=
'adam'
,
base_learning_rate
=
0.001
,
weight_decay
=
0.001
,
policy
=
'fixed'
,
stepsize
=
1
,
epsilon
=
1E-8
,
beta1
=
0.9
,
beta2
=
0.999
,
gamma
=
0.999
,
momentum
=
0.9
)
:
if
context
==
'cpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CPU
,
0
)
print
(
"CPU mode selected"
)
elif
device_opts
==
'gpu'
:
elif
context
==
'gpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CUDA
,
0
)
print
(
"GPU mode selected"
)
...
...
src/test/resources/target_code/CNNCreator_CifarClassifierNetwork.py
View file @
de0f235f
...
...
@@ -278,11 +278,11 @@ class CNNCreator_CifarClassifierNetwork:
accuracy
=
brew
.
accuracy
(
model
,
[
output
,
label
],
"accuracy"
,
top_k
=
3
)
return
accuracy
def
train
(
self
,
num_epoch
=
1000
,
batch_size
=
64
,
device_opts
=
'gpu'
,
eval_metric
=
'accuracy'
,
opt_type
=
'adam'
,
base_learning_rate
=
0.001
,
weight_decay
=
0.001
,
policy
=
'fixed'
,
stepsize
=
1
,
epsilon
=
1E-8
,
beta1
=
0.9
,
beta2
=
0.999
,
gamma
=
0.999
,
momentum
=
0.9
)
:
if
device_opts
==
'cpu'
:
def
train
(
self
,
num_epoch
=
1000
,
batch_size
=
64
,
context
=
'gpu'
,
eval_metric
=
'accuracy'
,
opt_type
=
'adam'
,
base_learning_rate
=
0.001
,
weight_decay
=
0.001
,
policy
=
'fixed'
,
stepsize
=
1
,
epsilon
=
1E-8
,
beta1
=
0.9
,
beta2
=
0.999
,
gamma
=
0.999
,
momentum
=
0.9
)
:
if
context
==
'cpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CPU
,
0
)
print
(
"CPU mode selected"
)
elif
device_opts
==
'gpu'
:
elif
context
==
'gpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CUDA
,
0
)
print
(
"GPU mode selected"
)
...
...
src/test/resources/target_code/CNNCreator_VGG16.py
View file @
de0f235f
...
...
@@ -167,11 +167,11 @@ class CNNCreator_VGG16:
accuracy
=
brew
.
accuracy
(
model
,
[
output
,
label
],
"accuracy"
,
top_k
=
3
)
return
accuracy
def
train
(
self
,
num_epoch
=
1000
,
batch_size
=
64
,
device_opts
=
'gpu'
,
eval_metric
=
'accuracy'
,
opt_type
=
'adam'
,
base_learning_rate
=
0.001
,
weight_decay
=
0.001
,
policy
=
'fixed'
,
stepsize
=
1
,
epsilon
=
1E-8
,
beta1
=
0.9
,
beta2
=
0.999
,
gamma
=
0.999
,
momentum
=
0.9
)
:
if
device_opts
==
'cpu'
:
def
train
(
self
,
num_epoch
=
1000
,
batch_size
=
64
,
context
=
'gpu'
,
eval_metric
=
'accuracy'
,
opt_type
=
'adam'
,
base_learning_rate
=
0.001
,
weight_decay
=
0.001
,
policy
=
'fixed'
,
stepsize
=
1
,
epsilon
=
1E-8
,
beta1
=
0.9
,
beta2
=
0.999
,
gamma
=
0.999
,
momentum
=
0.9
)
:
if
context
==
'cpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CPU
,
0
)
print
(
"CPU mode selected"
)
elif
device_opts
==
'gpu'
:
elif
context
==
'gpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CUDA
,
0
)
print
(
"GPU mode selected"
)
...
...
src/test/resources/target_code/CNNTrainer_emptyConfig.py
View file @
de0f235f
from
caffe2.python
import
workspace
,
core
,
model_helper
,
brew
,
optimizer
from
caffe2.python.predictor
import
mobile_exporter
from
caffe2.proto
import
caffe2_pb2
import
numpy
as
np
import
cv2
import
logging
import
mxnet
as
mx
import
CNNCreator_emptyConfig
if
__name__
==
"__main__"
:
...
...
@@ -11,3 +16,28 @@ if __name__ == "__main__":
emptyConfig
=
CNNCreator_emptyConfig
.
CNNCreator_emptyConfig
()
emptyConfig
.
train
(
)
print
'
\n
********************************************'
print
(
"Loading Deploy model"
)
context
=
'gpu'
if
context
==
'cpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CPU
,
0
)
print
(
"CPU mode selected"
)
elif
context
==
'gpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CUDA
,
0
)
print
(
"GPU mode selected"
)
LeNet
.
load_net
(
LeNet
.
INIT_NET
,
LeNet
.
PREDICT_NET
,
device_opts
=
device_opts
)
img
=
cv2
.
imread
(
"3.jpg"
)
# Load test image
img
=
cv2
.
resize
(
img
,
(
28
,
28
))
# Resize to 28x28
img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_RGB2GRAY
)
# Covert to grayscale
img
=
img
.
reshape
((
1
,
1
,
28
,
28
)).
astype
(
'float32'
)
# Reshape to (1,1,28,28)
workspace
.
FeedBlob
(
"data"
,
img
,
device_option
=
device_opts
)
# FeedBlob
workspace
.
RunNet
(
'deploy_net'
,
num_iter
=
1
)
# Forward
print
(
"
\n
Input: {}"
.
format
(
img
.
shape
))
pred
=
workspace
.
FetchBlob
(
"predictions"
)
print
(
"Output: {}"
.
format
(
pred
))
print
(
"Output class: {}"
.
format
(
np
.
argmax
(
pred
)))
src/test/resources/target_code/CNNTrainer_fullConfig.py
View file @
de0f235f
from
caffe2.python
import
workspace
,
core
,
model_helper
,
brew
,
optimizer
from
caffe2.python.predictor
import
mobile_exporter
from
caffe2.proto
import
caffe2_pb2
import
numpy
as
np
import
cv2
import
logging
import
mxnet
as
mx
import
CNNCreator_fullConfig
if
__name__
==
"__main__"
:
...
...
@@ -10,25 +15,40 @@ if __name__ == "__main__":
fullConfig
=
CNNCreator_fullConfig
.
CNNCreator_fullConfig
()
fullConfig
.
train
(
batch_size
=
100
,
num_epoch
=
5
,
load_checkpoint
=
True
,
batch_size
=
100
,
context
=
'gpu'
,
normalize
=
True
,
eval_metric
=
'mse'
,
optimizer
=
'rmsprop'
,
optimizer_params
=
{
'weight_decay'
:
0.01
,
'centered'
:
True
,
'gamma2'
:
0.9
,
'gamma1'
:
0.9
,
'clip_weights'
:
10.0
,
'learning_rate_decay'
:
0.9
,
'epsilon'
:
1.0E-6
,
'rescale_grad'
:
1.1
,
'clip_gradient'
:
10.0
,
'learning_rate_minimum'
:
1.0E-5
,
'learning_rate_policy'
:
'step'
,
'learning_rate'
:
0.001
,
'step_size'
:
1000
}
opt_type
=
'rmsprop'
,
base_learning_rate
=
0.001
,
weight_decay
=
0.01
,
policy
=
'step'
,
stepsize
=
1000
,
epsilon
=
1.0E-6
,
gamma
=
0.9
,
)
print
'
\n
********************************************'
print
(
"Loading Deploy model"
)
context
=
'gpu'
if
context
==
'cpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CPU
,
0
)
print
(
"CPU mode selected"
)
elif
context
==
'gpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CUDA
,
0
)
print
(
"GPU mode selected"
)
LeNet
.
load_net
(
LeNet
.
INIT_NET
,
LeNet
.
PREDICT_NET
,
device_opts
=
device_opts
)
img
=
cv2
.
imread
(
"3.jpg"
)
# Load test image
img
=
cv2
.
resize
(
img
,
(
28
,
28
))
# Resize to 28x28
img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_RGB2GRAY
)
# Covert to grayscale
img
=
img
.
reshape
((
1
,
1
,
28
,
28
)).
astype
(
'float32'
)
# Reshape to (1,1,28,28)
workspace
.
FeedBlob
(
"data"
,
img
,
device_option
=
device_opts
)
# FeedBlob
workspace
.
RunNet
(
'deploy_net'
,
num_iter
=
1
)
# Forward
print
(
"
\n
Input: {}"
.
format
(
img
.
shape
))
pred
=
workspace
.
FetchBlob
(
"predictions"
)
print
(
"Output: {}"
.
format
(
pred
))
print
(
"Output class: {}"
.
format
(
np
.
argmax
(
pred
)))
src/test/resources/target_code/CNNTrainer_simpleConfig.py
View file @
de0f235f
from
caffe2.python
import
workspace
,
core
,
model_helper
,
brew
,
optimizer
from
caffe2.python.predictor
import
mobile_exporter
from
caffe2.proto
import
caffe2_pb2
import
numpy
as
np
import
cv2
import
logging
import
mxnet
as
mx
import
CNNCreator_simpleConfig
if
__name__
==
"__main__"
:
...
...
@@ -10,9 +15,33 @@ if __name__ == "__main__":
simpleConfig
=
CNNCreator_simpleConfig
.
CNNCreator_simpleConfig
()
simpleConfig
.
train
(
batch_size
=
100
,
num_epoch
=
50
,
optimizer
=
'adam'
,
opt
imizer_params
=
{
'
learning_rate
'
:
0.001
}
batch_size
=
100
,
opt
_type
=
'adam'
,
base_
learning_rate
=
0.001
,
)
print
'
\n
********************************************'
print
(
"Loading Deploy model"
)
context
=
'gpu'
if
context
==
'cpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CPU
,
0
)
print
(
"CPU mode selected"
)
elif
context
==
'gpu'
:
device_opts
=
core
.
DeviceOption
(
caffe2_pb2
.
CUDA
,
0
)
print
(
"GPU mode selected"
)
LeNet
.
load_net
(
LeNet
.
INIT_NET
,
LeNet
.
PREDICT_NET
,
device_opts
=
device_opts
)
img
=
cv2
.
imread
(
"3.jpg"
)
# Load test image
img
=
cv2
.
resize
(
img
,
(
28
,
28
))
# Resize to 28x28
img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_RGB2GRAY
)
# Covert to grayscale
img
=
img
.
reshape
((
1
,
1
,
28
,
28
)).
astype
(
'float32'
)
# Reshape to (1,1,28,28)
workspace
.
FeedBlob
(
"data"
,
img
,
device_option
=
device_opts
)
# FeedBlob
workspace
.
RunNet
(
'deploy_net'
,
num_iter
=
1
)
# Forward
print
(
"
\n
Input: {}"
.
format
(
img
.
shape
))
pred
=
workspace
.
FetchBlob
(
"predictions"
)
print
(
"Output: {}"
.
format
(
pred
))
print
(
"Output class: {}"
.
format
(
np
.
argmax
(
pred
)))
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment