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
CNNArch2Gluon
Commits
4cefb787
Commit
4cefb787
authored
Apr 13, 2020
by
Julian Treiber
Browse files
updated tests, pom.xml
parent
46d3230b
Changes
14
Hide whitespace changes
Inline
Side-by-side
pom.xml
View file @
4cefb787
...
...
@@ -18,11 +18,11 @@
<!-- .. SE-Libraries .................................................. -->
<CNNArch.version>
0.3.4-SNAPSHOT
</CNNArch.version>
<CNNTrain.version>
0.3.
9
-SNAPSHOT
</CNNTrain.version>
<CNNArch2X.version>
0.0.
5
-SNAPSHOT
</CNNArch2X.version>
<CNNTrain.version>
0.3.
10
-SNAPSHOT
</CNNTrain.version>
<CNNArch2X.version>
0.0.
6
-SNAPSHOT
</CNNArch2X.version>
<embedded-montiarc-math-opt-generator>
0.1.6
</embedded-montiarc-math-opt-generator>
<EMADL2PythonWrapper.version>
0.0.2-SNAPSHOT
</EMADL2PythonWrapper.version>
<!-- .. Libraries .................................................. -->
<guava.version>
18.0
</guava.version>
<junit.version>
4.12
</junit.version>
...
...
@@ -144,7 +144,7 @@
</dependency>
</dependencies>
<!-- == PROJECT BUILD SETTINGS =========================================== -->
...
...
src/main/resources/templates/gluon/CNNCreator.ftl
View file @
4cefb787
...
...
@@ -36,6 +36,7 @@ class ${tc.fileNameWithoutEnding}:
os.remove(self._model_dir_ + self._model_prefix_ + "_" + str(i) + "_newest-symbol.json")
except OSError:
pass
if os.path.isdir(self._model_dir_):
for file in os.listdir(self._model_dir_):
if ".params" in file and self._model_prefix_ + "_" + str(i) in file:
...
...
src/main/resources/templates/gluon/elements/Convolution.ftl
View file @
4cefb787
...
...
@@ -7,7 +7,6 @@
self.$
{
element
.name
}
= gluon.nn.Conv2D(channels=$
{
element
.channels
?
c
}
,
kernel_size=($
{
tc
.join
(
element
.kernel
,
","
)}
),
strides=($
{
tc
.join
(
element
.stride
,
","
)}
),
groups=$
{
element
.groups
?
c
}
,
use_bias=$
{
element
.noBias
?
string
(
"False"
,
"True"
)}
)
<#
include
"OutputShape.ftl">
<#
elseif
mode == "FORWARD_FUNCTION">
...
...
src/main/resources/templates/gluon/elements/UpConvolution.ftl
View file @
4cefb787
...
...
@@ -8,7 +8,6 @@
kernel_size=($
{
tc
.join
(
element
.kernel
,
","
)}
),
strides=($
{
tc
.join
(
element
.stride
,
","
)}
),
padding=self.$
{
element
.name
}
padding,
groups=$
{
element
.groups
?
c
}
,
use_bias=$
{
element
.noBias
?
string
(
"False"
,
"True"
)}
)
<#
include
"OutputShape.ftl">
<#
elseif
mode == "FORWARD_FUNCTION">
...
...
src/test/resources/target_code/CNNCreator_Alexnet.py
View file @
4cefb787
import
mxnet
as
mx
import
logging
import
os
import
shutil
from
CNNNet_Alexnet
import
Net_0
...
...
@@ -11,6 +12,7 @@ class CNNCreator_Alexnet:
def
__init__
(
self
):
self
.
weight_initializer
=
mx
.
init
.
Normal
()
self
.
networks
=
{}
self
.
_weights_dir_
=
None
def
load
(
self
,
context
):
earliestLastEpoch
=
None
...
...
@@ -47,6 +49,29 @@ class CNNCreator_Alexnet:
return
earliestLastEpoch
def
load_pretrained_weights
(
self
,
context
):
if
os
.
path
.
isdir
(
self
.
_model_dir_
):
shutil
.
rmtree
(
self
.
_model_dir_
)
if
self
.
_weights_dir_
is
not
None
:
for
i
,
network
in
self
.
networks
.
items
():
# param_file = self._model_prefix_ + "_" + str(i) + "_newest-0000.params"
param_file
=
None
if
os
.
path
.
isdir
(
self
.
_weights_dir_
):
lastEpoch
=
0
for
file
in
os
.
listdir
(
self
.
_weights_dir_
):
if
".params"
in
file
and
self
.
_model_prefix_
+
"_"
+
str
(
i
)
in
file
:
epochStr
=
file
.
replace
(
".params"
,
""
).
replace
(
self
.
_model_prefix_
+
"_"
+
str
(
i
)
+
"-"
,
""
)
epoch
=
int
(
epochStr
)
if
epoch
>
lastEpoch
:
lastEpoch
=
epoch
param_file
=
file
logging
.
info
(
"Loading pretrained weights: "
+
self
.
_weights_dir_
+
param_file
)
network
.
load_parameters
(
self
.
_weights_dir_
+
param_file
,
allow_missing
=
True
,
ignore_extra
=
True
)
else
:
logging
.
info
(
"No pretrained weights available at: "
+
self
.
_weights_dir_
+
param_file
)
def
construct
(
self
,
context
,
data_mean
=
None
,
data_std
=
None
):
self
.
networks
[
0
]
=
Net_0
(
data_mean
=
data_mean
,
data_std
=
data_std
)
self
.
networks
[
0
].
collect_params
().
initialize
(
self
.
weight_initializer
,
ctx
=
context
)
...
...
src/test/resources/target_code/CNNCreator_CifarClassifierNetwork.py
View file @
4cefb787
import
mxnet
as
mx
import
logging
import
os
import
shutil
from
CNNNet_CifarClassifierNetwork
import
Net_0
...
...
@@ -11,6 +12,7 @@ class CNNCreator_CifarClassifierNetwork:
def
__init__
(
self
):
self
.
weight_initializer
=
mx
.
init
.
Normal
()
self
.
networks
=
{}
self
.
_weights_dir_
=
None
def
load
(
self
,
context
):
earliestLastEpoch
=
None
...
...
@@ -47,6 +49,29 @@ class CNNCreator_CifarClassifierNetwork:
return
earliestLastEpoch
def
load_pretrained_weights
(
self
,
context
):
if
os
.
path
.
isdir
(
self
.
_model_dir_
):
shutil
.
rmtree
(
self
.
_model_dir_
)
if
self
.
_weights_dir_
is
not
None
:
for
i
,
network
in
self
.
networks
.
items
():
# param_file = self._model_prefix_ + "_" + str(i) + "_newest-0000.params"
param_file
=
None
if
os
.
path
.
isdir
(
self
.
_weights_dir_
):
lastEpoch
=
0
for
file
in
os
.
listdir
(
self
.
_weights_dir_
):
if
".params"
in
file
and
self
.
_model_prefix_
+
"_"
+
str
(
i
)
in
file
:
epochStr
=
file
.
replace
(
".params"
,
""
).
replace
(
self
.
_model_prefix_
+
"_"
+
str
(
i
)
+
"-"
,
""
)
epoch
=
int
(
epochStr
)
if
epoch
>
lastEpoch
:
lastEpoch
=
epoch
param_file
=
file
logging
.
info
(
"Loading pretrained weights: "
+
self
.
_weights_dir_
+
param_file
)
network
.
load_parameters
(
self
.
_weights_dir_
+
param_file
,
allow_missing
=
True
,
ignore_extra
=
True
)
else
:
logging
.
info
(
"No pretrained weights available at: "
+
self
.
_weights_dir_
+
param_file
)
def
construct
(
self
,
context
,
data_mean
=
None
,
data_std
=
None
):
self
.
networks
[
0
]
=
Net_0
(
data_mean
=
data_mean
,
data_std
=
data_std
)
self
.
networks
[
0
].
collect_params
().
initialize
(
self
.
weight_initializer
,
ctx
=
context
)
...
...
src/test/resources/target_code/CNNCreator_VGG16.py
View file @
4cefb787
import
mxnet
as
mx
import
logging
import
os
import
shutil
from
CNNNet_VGG16
import
Net_0
...
...
@@ -11,6 +12,7 @@ class CNNCreator_VGG16:
def
__init__
(
self
):
self
.
weight_initializer
=
mx
.
init
.
Normal
()
self
.
networks
=
{}
self
.
_weights_dir_
=
None
def
load
(
self
,
context
):
earliestLastEpoch
=
None
...
...
@@ -47,6 +49,29 @@ class CNNCreator_VGG16:
return
earliestLastEpoch
def
load_pretrained_weights
(
self
,
context
):
if
os
.
path
.
isdir
(
self
.
_model_dir_
):
shutil
.
rmtree
(
self
.
_model_dir_
)
if
self
.
_weights_dir_
is
not
None
:
for
i
,
network
in
self
.
networks
.
items
():
# param_file = self._model_prefix_ + "_" + str(i) + "_newest-0000.params"
param_file
=
None
if
os
.
path
.
isdir
(
self
.
_weights_dir_
):
lastEpoch
=
0
for
file
in
os
.
listdir
(
self
.
_weights_dir_
):
if
".params"
in
file
and
self
.
_model_prefix_
+
"_"
+
str
(
i
)
in
file
:
epochStr
=
file
.
replace
(
".params"
,
""
).
replace
(
self
.
_model_prefix_
+
"_"
+
str
(
i
)
+
"-"
,
""
)
epoch
=
int
(
epochStr
)
if
epoch
>
lastEpoch
:
lastEpoch
=
epoch
param_file
=
file
logging
.
info
(
"Loading pretrained weights: "
+
self
.
_weights_dir_
+
param_file
)
network
.
load_parameters
(
self
.
_weights_dir_
+
param_file
,
allow_missing
=
True
,
ignore_extra
=
True
)
else
:
logging
.
info
(
"No pretrained weights available at: "
+
self
.
_weights_dir_
+
param_file
)
def
construct
(
self
,
context
,
data_mean
=
None
,
data_std
=
None
):
self
.
networks
[
0
]
=
Net_0
(
data_mean
=
data_mean
,
data_std
=
data_std
)
self
.
networks
[
0
].
collect_params
().
initialize
(
self
.
weight_initializer
,
ctx
=
context
)
...
...
src/test/resources/target_code/CNNSupervisedTrainer_Alexnet.py
View file @
4cefb787
...
...
@@ -86,6 +86,53 @@ class DiceLoss(gluon.loss.Loss):
diceloss
=
self
.
dice_loss
(
F
,
pred
,
label
)
return
F
.
mean
(
loss
,
axis
=
self
.
_batch_axis
,
exclude
=
True
)
+
diceloss
class
SoftmaxCrossEntropyLossIgnoreLabel
(
gluon
.
loss
.
Loss
):
def
__init__
(
self
,
axis
=-
1
,
from_logits
=
False
,
weight
=
None
,
batch_axis
=
0
,
ignore_label
=
255
,
**
kwargs
):
super
(
SoftmaxCrossEntropyLossIgnoreLabel
,
self
).
__init__
(
weight
,
batch_axis
,
**
kwargs
)
self
.
_axis
=
axis
self
.
_from_logits
=
from_logits
self
.
_ignore_label
=
ignore_label
def
hybrid_forward
(
self
,
F
,
output
,
label
,
sample_weight
=
None
):
if
not
self
.
_from_logits
:
output
=
F
.
log_softmax
(
output
,
axis
=
self
.
_axis
)
valid_label_map
=
(
label
!=
self
.
_ignore_label
)
loss
=
-
(
F
.
pick
(
output
,
label
,
axis
=
self
.
_axis
,
keepdims
=
True
)
*
valid_label_map
)
loss
=
gluon
.
loss
.
_apply_weighting
(
F
,
loss
,
self
.
_weight
,
sample_weight
)
return
F
.
sum
(
loss
)
/
F
.
sum
(
valid_label_map
)
@
mx
.
metric
.
register
class
ACCURACY_IGNORE_LABEL
(
mx
.
metric
.
EvalMetric
):
"""Ignores a label when computing accuracy.
"""
def
__init__
(
self
,
axis
=
1
,
metric_ignore_label
=
255
,
name
=
'accuracy'
,
output_names
=
None
,
label_names
=
None
):
super
(
ACCURACY_IGNORE_LABEL
,
self
).
__init__
(
name
,
axis
=
axis
,
output_names
=
output_names
,
label_names
=
label_names
)
self
.
axis
=
axis
self
.
ignore_label
=
metric_ignore_label
def
update
(
self
,
labels
,
preds
):
mx
.
metric
.
check_label_shapes
(
labels
,
preds
)
for
label
,
pred_label
in
zip
(
labels
,
preds
):
if
pred_label
.
shape
!=
label
.
shape
:
pred_label
=
mx
.
nd
.
argmax
(
pred_label
,
axis
=
self
.
axis
,
keepdims
=
True
)
label
=
label
.
astype
(
'int32'
)
pred_label
=
pred_label
.
astype
(
'int32'
).
as_in_context
(
label
.
context
)
mx
.
metric
.
check_label_shapes
(
label
,
pred_label
)
correct
=
mx
.
nd
.
sum
(
(
label
==
pred_label
)
*
(
label
!=
self
.
ignore_label
)
).
asscalar
()
total
=
mx
.
nd
.
sum
(
(
label
!=
self
.
ignore_label
)
).
asscalar
()
self
.
sum_metric
+=
correct
self
.
num_inst
+=
total
@
mx
.
metric
.
register
class
BLEU
(
mx
.
metric
.
EvalMetric
):
N
=
4
...
...
@@ -221,6 +268,7 @@ class CNNSupervisedTrainer_Alexnet:
optimizer_params
=
((
'learning_rate'
,
0.001
),),
load_checkpoint
=
True
,
checkpoint_period
=
5
,
load_pretrained
=
False
,
log_period
=
50
,
context
=
'gpu'
,
save_attention_image
=
False
,
...
...
@@ -265,6 +313,8 @@ class CNNSupervisedTrainer_Alexnet:
begin_epoch
=
0
if
load_checkpoint
:
begin_epoch
=
self
.
_net_creator
.
load
(
mx_context
)
elif
load_pretrained
:
self
.
_net_creator
.
load_pretrained_weights
(
mx_context
)
else
:
if
os
.
path
.
isdir
(
self
.
_net_creator
.
_model_dir_
):
shutil
.
rmtree
(
self
.
_net_creator
.
_model_dir_
)
...
...
@@ -297,6 +347,10 @@ class CNNSupervisedTrainer_Alexnet:
elif
loss
==
'dice_loss'
:
loss_weight
=
loss_params
[
'loss_weight'
]
if
'loss_weight'
in
loss_params
else
None
loss_function
=
DiceLoss
(
axis
=
loss_axis
,
weight
=
loss_weight
,
sparse_label
=
sparseLabel
,
batch_axis
=
batch_axis
)
elif
loss
==
'softmax_cross_entropy_ignore_label'
:
loss_weight
=
loss_params
[
'loss_weight'
]
if
'loss_weight'
in
loss_params
else
None
loss_ignore_label
=
loss_params
[
'loss_ignore_label'
]
if
'loss_ignore_label'
in
loss_params
else
None
loss_function
=
SoftmaxCrossEntropyLossIgnoreLabel
(
axis
=
loss_axis
,
ignore_label
=
loss_ignore_label
,
weight
=
loss_weight
,
batch_axis
=
batch_axis
)
elif
loss
==
'l2'
:
loss_function
=
mx
.
gluon
.
loss
.
L2Loss
()
elif
loss
==
'l1'
:
...
...
@@ -475,7 +529,7 @@ class CNNSupervisedTrainer_Alexnet:
test_iter
.
reset
()
metric
=
mx
.
metric
.
create
(
eval_metric
,
**
eval_metric_params
)
for
batch_i
,
batch
in
enumerate
(
test_iter
):
if
True
:
if
True
:
labels
=
[
batch
.
label
[
i
].
as_in_context
(
mx_context
)
for
i
in
range
(
1
)]
data_
=
batch
.
data
[
0
].
as_in_context
(
mx_context
)
...
...
src/test/resources/target_code/CNNSupervisedTrainer_CifarClassifierNetwork.py
View file @
4cefb787
...
...
@@ -86,6 +86,53 @@ class DiceLoss(gluon.loss.Loss):
diceloss
=
self
.
dice_loss
(
F
,
pred
,
label
)
return
F
.
mean
(
loss
,
axis
=
self
.
_batch_axis
,
exclude
=
True
)
+
diceloss
class
SoftmaxCrossEntropyLossIgnoreLabel
(
gluon
.
loss
.
Loss
):
def
__init__
(
self
,
axis
=-
1
,
from_logits
=
False
,
weight
=
None
,
batch_axis
=
0
,
ignore_label
=
255
,
**
kwargs
):
super
(
SoftmaxCrossEntropyLossIgnoreLabel
,
self
).
__init__
(
weight
,
batch_axis
,
**
kwargs
)
self
.
_axis
=
axis
self
.
_from_logits
=
from_logits
self
.
_ignore_label
=
ignore_label
def
hybrid_forward
(
self
,
F
,
output
,
label
,
sample_weight
=
None
):
if
not
self
.
_from_logits
:
output
=
F
.
log_softmax
(
output
,
axis
=
self
.
_axis
)
valid_label_map
=
(
label
!=
self
.
_ignore_label
)
loss
=
-
(
F
.
pick
(
output
,
label
,
axis
=
self
.
_axis
,
keepdims
=
True
)
*
valid_label_map
)
loss
=
gluon
.
loss
.
_apply_weighting
(
F
,
loss
,
self
.
_weight
,
sample_weight
)
return
F
.
sum
(
loss
)
/
F
.
sum
(
valid_label_map
)
@
mx
.
metric
.
register
class
ACCURACY_IGNORE_LABEL
(
mx
.
metric
.
EvalMetric
):
"""Ignores a label when computing accuracy.
"""
def
__init__
(
self
,
axis
=
1
,
metric_ignore_label
=
255
,
name
=
'accuracy'
,
output_names
=
None
,
label_names
=
None
):
super
(
ACCURACY_IGNORE_LABEL
,
self
).
__init__
(
name
,
axis
=
axis
,
output_names
=
output_names
,
label_names
=
label_names
)
self
.
axis
=
axis
self
.
ignore_label
=
metric_ignore_label
def
update
(
self
,
labels
,
preds
):
mx
.
metric
.
check_label_shapes
(
labels
,
preds
)
for
label
,
pred_label
in
zip
(
labels
,
preds
):
if
pred_label
.
shape
!=
label
.
shape
:
pred_label
=
mx
.
nd
.
argmax
(
pred_label
,
axis
=
self
.
axis
,
keepdims
=
True
)
label
=
label
.
astype
(
'int32'
)
pred_label
=
pred_label
.
astype
(
'int32'
).
as_in_context
(
label
.
context
)
mx
.
metric
.
check_label_shapes
(
label
,
pred_label
)
correct
=
mx
.
nd
.
sum
(
(
label
==
pred_label
)
*
(
label
!=
self
.
ignore_label
)
).
asscalar
()
total
=
mx
.
nd
.
sum
(
(
label
!=
self
.
ignore_label
)
).
asscalar
()
self
.
sum_metric
+=
correct
self
.
num_inst
+=
total
@
mx
.
metric
.
register
class
BLEU
(
mx
.
metric
.
EvalMetric
):
N
=
4
...
...
@@ -221,6 +268,7 @@ class CNNSupervisedTrainer_CifarClassifierNetwork:
optimizer_params
=
((
'learning_rate'
,
0.001
),),
load_checkpoint
=
True
,
checkpoint_period
=
5
,
load_pretrained
=
False
,
log_period
=
50
,
context
=
'gpu'
,
save_attention_image
=
False
,
...
...
@@ -265,6 +313,8 @@ class CNNSupervisedTrainer_CifarClassifierNetwork:
begin_epoch
=
0
if
load_checkpoint
:
begin_epoch
=
self
.
_net_creator
.
load
(
mx_context
)
elif
load_pretrained
:
self
.
_net_creator
.
load_pretrained_weights
(
mx_context
)
else
:
if
os
.
path
.
isdir
(
self
.
_net_creator
.
_model_dir_
):
shutil
.
rmtree
(
self
.
_net_creator
.
_model_dir_
)
...
...
@@ -297,6 +347,10 @@ class CNNSupervisedTrainer_CifarClassifierNetwork:
elif
loss
==
'dice_loss'
:
loss_weight
=
loss_params
[
'loss_weight'
]
if
'loss_weight'
in
loss_params
else
None
loss_function
=
DiceLoss
(
axis
=
loss_axis
,
weight
=
loss_weight
,
sparse_label
=
sparseLabel
,
batch_axis
=
batch_axis
)
elif
loss
==
'softmax_cross_entropy_ignore_label'
:
loss_weight
=
loss_params
[
'loss_weight'
]
if
'loss_weight'
in
loss_params
else
None
loss_ignore_label
=
loss_params
[
'loss_ignore_label'
]
if
'loss_ignore_label'
in
loss_params
else
None
loss_function
=
SoftmaxCrossEntropyLossIgnoreLabel
(
axis
=
loss_axis
,
ignore_label
=
loss_ignore_label
,
weight
=
loss_weight
,
batch_axis
=
batch_axis
)
elif
loss
==
'l2'
:
loss_function
=
mx
.
gluon
.
loss
.
L2Loss
()
elif
loss
==
'l1'
:
...
...
@@ -475,7 +529,7 @@ class CNNSupervisedTrainer_CifarClassifierNetwork:
test_iter
.
reset
()
metric
=
mx
.
metric
.
create
(
eval_metric
,
**
eval_metric_params
)
for
batch_i
,
batch
in
enumerate
(
test_iter
):
if
True
:
if
True
:
labels
=
[
batch
.
label
[
i
].
as_in_context
(
mx_context
)
for
i
in
range
(
1
)]
data_
=
batch
.
data
[
0
].
as_in_context
(
mx_context
)
...
...
src/test/resources/target_code/CNNSupervisedTrainer_VGG16.py
View file @
4cefb787
...
...
@@ -86,6 +86,53 @@ class DiceLoss(gluon.loss.Loss):
diceloss
=
self
.
dice_loss
(
F
,
pred
,
label
)
return
F
.
mean
(
loss
,
axis
=
self
.
_batch_axis
,
exclude
=
True
)
+
diceloss
class
SoftmaxCrossEntropyLossIgnoreLabel
(
gluon
.
loss
.
Loss
):
def
__init__
(
self
,
axis
=-
1
,
from_logits
=
False
,
weight
=
None
,
batch_axis
=
0
,
ignore_label
=
255
,
**
kwargs
):
super
(
SoftmaxCrossEntropyLossIgnoreLabel
,
self
).
__init__
(
weight
,
batch_axis
,
**
kwargs
)
self
.
_axis
=
axis
self
.
_from_logits
=
from_logits
self
.
_ignore_label
=
ignore_label
def
hybrid_forward
(
self
,
F
,
output
,
label
,
sample_weight
=
None
):
if
not
self
.
_from_logits
:
output
=
F
.
log_softmax
(
output
,
axis
=
self
.
_axis
)
valid_label_map
=
(
label
!=
self
.
_ignore_label
)
loss
=
-
(
F
.
pick
(
output
,
label
,
axis
=
self
.
_axis
,
keepdims
=
True
)
*
valid_label_map
)
loss
=
gluon
.
loss
.
_apply_weighting
(
F
,
loss
,
self
.
_weight
,
sample_weight
)
return
F
.
sum
(
loss
)
/
F
.
sum
(
valid_label_map
)
@
mx
.
metric
.
register
class
ACCURACY_IGNORE_LABEL
(
mx
.
metric
.
EvalMetric
):
"""Ignores a label when computing accuracy.
"""
def
__init__
(
self
,
axis
=
1
,
metric_ignore_label
=
255
,
name
=
'accuracy'
,
output_names
=
None
,
label_names
=
None
):
super
(
ACCURACY_IGNORE_LABEL
,
self
).
__init__
(
name
,
axis
=
axis
,
output_names
=
output_names
,
label_names
=
label_names
)
self
.
axis
=
axis
self
.
ignore_label
=
metric_ignore_label
def
update
(
self
,
labels
,
preds
):
mx
.
metric
.
check_label_shapes
(
labels
,
preds
)
for
label
,
pred_label
in
zip
(
labels
,
preds
):
if
pred_label
.
shape
!=
label
.
shape
:
pred_label
=
mx
.
nd
.
argmax
(
pred_label
,
axis
=
self
.
axis
,
keepdims
=
True
)
label
=
label
.
astype
(
'int32'
)
pred_label
=
pred_label
.
astype
(
'int32'
).
as_in_context
(
label
.
context
)
mx
.
metric
.
check_label_shapes
(
label
,
pred_label
)
correct
=
mx
.
nd
.
sum
(
(
label
==
pred_label
)
*
(
label
!=
self
.
ignore_label
)
).
asscalar
()
total
=
mx
.
nd
.
sum
(
(
label
!=
self
.
ignore_label
)
).
asscalar
()
self
.
sum_metric
+=
correct
self
.
num_inst
+=
total
@
mx
.
metric
.
register
class
BLEU
(
mx
.
metric
.
EvalMetric
):
N
=
4
...
...
@@ -221,6 +268,7 @@ class CNNSupervisedTrainer_VGG16:
optimizer_params
=
((
'learning_rate'
,
0.001
),),
load_checkpoint
=
True
,
checkpoint_period
=
5
,
load_pretrained
=
False
,
log_period
=
50
,
context
=
'gpu'
,
save_attention_image
=
False
,
...
...
@@ -265,6 +313,8 @@ class CNNSupervisedTrainer_VGG16:
begin_epoch
=
0
if
load_checkpoint
:
begin_epoch
=
self
.
_net_creator
.
load
(
mx_context
)
elif
load_pretrained
:
self
.
_net_creator
.
load_pretrained_weights
(
mx_context
)
else
:
if
os
.
path
.
isdir
(
self
.
_net_creator
.
_model_dir_
):
shutil
.
rmtree
(
self
.
_net_creator
.
_model_dir_
)
...
...
@@ -297,6 +347,10 @@ class CNNSupervisedTrainer_VGG16:
elif
loss
==
'dice_loss'
:
loss_weight
=
loss_params
[
'loss_weight'
]
if
'loss_weight'
in
loss_params
else
None
loss_function
=
DiceLoss
(
axis
=
loss_axis
,
weight
=
loss_weight
,
sparse_label
=
sparseLabel
,
batch_axis
=
batch_axis
)
elif
loss
==
'softmax_cross_entropy_ignore_label'
:
loss_weight
=
loss_params
[
'loss_weight'
]
if
'loss_weight'
in
loss_params
else
None
loss_ignore_label
=
loss_params
[
'loss_ignore_label'
]
if
'loss_ignore_label'
in
loss_params
else
None
loss_function
=
SoftmaxCrossEntropyLossIgnoreLabel
(
axis
=
loss_axis
,
ignore_label
=
loss_ignore_label
,
weight
=
loss_weight
,
batch_axis
=
batch_axis
)
elif
loss
==
'l2'
:
loss_function
=
mx
.
gluon
.
loss
.
L2Loss
()
elif
loss
==
'l1'
:
...
...
@@ -475,7 +529,7 @@ class CNNSupervisedTrainer_VGG16:
test_iter
.
reset
()
metric
=
mx
.
metric
.
create
(
eval_metric
,
**
eval_metric_params
)
for
batch_i
,
batch
in
enumerate
(
test_iter
):
if
True
:
if
True
:
labels
=
[
batch
.
label
[
i
].
as_in_context
(
mx_context
)
for
i
in
range
(
1
)]
data_
=
batch
.
data
[
0
].
as_in_context
(
mx_context
)
...
...
src/test/resources/target_code/ddpg/reinforcement_learning/CNNCreator_CriticNetwork.py
View file @
4cefb787
import
mxnet
as
mx
import
logging
import
os
import
shutil
from
CNNNet_CriticNetwork
import
Net_0
...
...
@@ -11,6 +12,7 @@ class CNNCreator_CriticNetwork:
def
__init__
(
self
):
self
.
weight_initializer
=
mx
.
init
.
Normal
()
self
.
networks
=
{}
self
.
_weights_dir_
=
None
def
load
(
self
,
context
):
earliestLastEpoch
=
None
...
...
@@ -47,6 +49,29 @@ class CNNCreator_CriticNetwork:
return
earliestLastEpoch
def
load_pretrained_weights
(
self
,
context
):
if
os
.
path
.
isdir
(
self
.
_model_dir_
):
shutil
.
rmtree
(
self
.
_model_dir_
)
if
self
.
_weights_dir_
is
not
None
:
for
i
,
network
in
self
.
networks
.
items
():
# param_file = self._model_prefix_ + "_" + str(i) + "_newest-0000.params"
param_file
=
None
if
os
.
path
.
isdir
(
self
.
_weights_dir_
):
lastEpoch
=
0
for
file
in
os
.
listdir
(
self
.
_weights_dir_
):
if
".params"
in
file
and
self
.
_model_prefix_
+
"_"
+
str
(
i
)
in
file
:
epochStr
=
file
.
replace
(
".params"
,
""
).
replace
(
self
.
_model_prefix_
+
"_"
+
str
(
i
)
+
"-"
,
""
)
epoch
=
int
(
epochStr
)
if
epoch
>
lastEpoch
:
lastEpoch
=
epoch
param_file
=
file
logging
.
info
(
"Loading pretrained weights: "
+
self
.
_weights_dir_
+
param_file
)
network
.
load_parameters
(
self
.
_weights_dir_
+
param_file
,
allow_missing
=
True
,
ignore_extra
=
True
)
else
:
logging
.
info
(
"No pretrained weights available at: "
+
self
.
_weights_dir_
+
param_file
)
def
construct
(
self
,
context
,
data_mean
=
None
,
data_std
=
None
):
self
.
networks
[
0
]
=
Net_0
(
data_mean
=
data_mean
,
data_std
=
data_std
)
self
.
networks
[
0
].
collect_params
().
initialize
(
self
.
weight_initializer
,
ctx
=
context
)
...
...
src/test/resources/target_code/ros-ddpg/reinforcement_learning/CNNCreator_RosCriticNetwork.py
View file @
4cefb787
import
mxnet
as
mx
import
logging
import
os
import
shutil
from
CNNNet_RosCriticNetwork
import
Net_0
...
...
@@ -11,6 +12,7 @@ class CNNCreator_RosCriticNetwork:
def
__init__
(
self
):
self
.
weight_initializer
=
mx
.
init
.
Normal
()
self
.
networks
=
{}
self
.
_weights_dir_
=
None
def
load
(
self
,
context
):
earliestLastEpoch
=
None
...
...
@@ -47,6 +49,29 @@ class CNNCreator_RosCriticNetwork:
return
earliestLastEpoch
def
load_pretrained_weights
(
self
,
context
):
if
os
.
path
.
isdir
(
self
.
_model_dir_
):
shutil
.
rmtree
(
self
.
_model_dir_
)
if
self
.
_weights_dir_
is
not
None
:
for
i
,
network
in
self
.
networks
.
items
():
# param_file = self._model_prefix_ + "_" + str(i) + "_newest-0000.params"
param_file
=
None
if
os
.
path
.
isdir
(
self
.
_weights_dir_
):
lastEpoch
=
0
for
file
in
os
.
listdir
(
self
.
_weights_dir_
):
if
".params"
in
file
and
self
.
_model_prefix_
+
"_"
+
str
(
i
)
in
file
:
epochStr
=
file
.
replace
(
".params"
,
""
).
replace
(
self
.
_model_prefix_
+
"_"
+
str
(
i
)
+
"-"
,
""
)
epoch
=
int
(
epochStr
)
if
epoch
>
lastEpoch
:
lastEpoch
=
epoch
param_file
=
file
logging
.
info
(
"Loading pretrained weights: "
+
self
.
_weights_dir_
+
param_file
)
network
.
load_parameters
(
self
.
_weights_dir_
+
param_file
,
allow_missing
=
True
,
ignore_extra
=
True
)
else
:
logging
.
info
(
"No pretrained weights available at: "
+
self
.
_weights_dir_
+
param_file
)
def
construct
(
self
,
context
,
data_mean
=
None
,
data_std
=
None
):
self
.
networks
[
0
]
=
Net_0
(
data_mean
=
data_mean
,
data_std
=
data_std
)
self
.
networks
[
0
].
collect_params
().
initialize
(
self
.
weight_initializer
,
ctx
=
context
)
...
...
src/test/resources/target_code/td3/reinforcement_learning/CNNCreator_CriticNetwork.py
View file @
4cefb787
import
mxnet
as
mx
import
logging
import
os
import
shutil
from
CNNNet_CriticNetwork
import
Net_0
...
...
@@ -11,6 +12,7 @@ class CNNCreator_CriticNetwork:
def
__init__
(
self
):
self
.
weight_initializer
=
mx
.
init
.
Normal
()
self
.
networks
=
{}
self
.
_weights_dir_
=
None