Commit d5f94b37 authored by Julian Treiber's avatar Julian Treiber

updated tests

parent 603ea865
Pipeline #267728 failed with stage
in 1 minute and 22 seconds
This diff is collapsed.
This diff is collapsed.
......@@ -414,13 +414,14 @@ public class EMADLGenerator {
protected String getWeightsPath(EMAComponentSymbol component, EMAComponentInstanceSymbol instance){
String weightsPath;
// TODO check if pretrained true, otherwise return null
Path weightsPathDefinition = Paths.get(getModelsPath(), "weights_paths.txt");
if (weightsPathDefinition.toFile().exists()) {
WeightsPathConfigParser newParserConfig = new WeightsPathConfigParser(getModelsPath() + "weights_paths.txt");
weightsPath = newParserConfig.getWeightsPath(component.getFullName());
} else {
Log.warn("No weights path definition found in " + weightsPathDefinition + " found: "
+ "No pretrained weights will be loaded.");
Log.info("No weights path definition found in " + weightsPathDefinition + ": "
+ "No pretrained weights will be loaded.", "EMADLGenerator");
weightsPath = null;
}
return weightsPath;
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_mnist_mnistClassifier_net import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_mnist_mnistClassifier_net:
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_mnist_mnistClassifier_net:
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)
......
......@@ -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_mnist_mnistClassifier_net:
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_mnist_mnistClassifier_net:
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_mnist_mnistClassifier_net:
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':
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_defaultGAN_defaultGANConnector_predictor import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_defaultGAN_defaultGANConnector_predictor:
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_defaultGAN_defaultGANConnector_predictor:
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)
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_defaultGAN_defaultGANDiscriminator import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_defaultGAN_defaultGANDiscriminator:
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_defaultGAN_defaultGANDiscriminator:
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)
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_infoGAN_infoGANConnector_predictor import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_infoGAN_infoGANConnector_predictor:
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_infoGAN_infoGANConnector_predictor:
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)
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_infoGAN_infoGANDiscriminator import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_infoGAN_infoGANDiscriminator:
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_infoGAN_infoGANDiscriminator:
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)
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_infoGAN_infoGANQNetwork import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_infoGAN_infoGANQNetwork:
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_infoGAN_infoGANQNetwork:
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)
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_cartpole_master_dqn import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_cartpole_master_dqn:
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_cartpole_master_dqn:
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)
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_mountaincar_master_actor import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_mountaincar_master_actor:
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_mountaincar_master_actor:
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)
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_mountaincar_agent_mountaincarCritic import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_mountaincar_agent_mountaincarCritic:
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_mountaincar_agent_mountaincarCritic:
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)
......
import mxnet as mx
import logging
import os
import shutil
from CNNNet_torcs_agent_torcsAgent_dqn import Net_0
......@@ -11,6 +12,7 @@ class CNNCreator_torcs_agent_torcsAgent_dqn:
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_torcs_agent_torcsAgent_dqn:
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)
......