Aufgrund einer Störung des s3 Storage, könnten in nächster Zeit folgende GitLab Funktionen nicht zur Verfügung stehen: LFS, Container Registry, Job Artifacs, Uploads (Wiki, Bilder, Projekt-Exporte). Wir bitten um Verständnis. Es wird mit Hochdruck an der Behebung des Problems gearbeitet. Weitere Informationen zur Störung des Object Storage finden Sie hier: https://maintenance.itc.rwth-aachen.de/ticket/status/messages/59-object-storage-pilot

Commit 87a112a5 authored by Thomas Michael Timmermanns's avatar Thomas Michael Timmermanns Committed by Thomas Michael Timmermanns
Browse files

Fixed a few code generation bugs.

Added dataset normalization to generated code.
parent 6a4fe6ec
......@@ -261,7 +261,7 @@ public class CNNArchTemplateController {
boolean isFirst = true;
for (Object element : iterable){
if (!isFirst){
stringBuilder.append(",");
stringBuilder.append(separator);
}
stringBuilder.append(elementPrefix);
stringBuilder.append(element.toString());
......
import mxnet as mx
import logging
import os
import errno
import shutil
import h5py
import sys
import numpy as np
@mx.init.register
class MyConstant(mx.init.Initializer):
def __init__(self, value):
super(MyConstant, self).__init__(value=value)
self.value = value
def _init_weight(self, _, arr):
arr[:] = mx.nd.array(self.value)
class ${tc.fileNameWithoutEnding}:
<#list tc.architectureInputs as input>
${input} = None
</#list>
<#list tc.architectureOutputs as output>
${output} = None
</#list>
outputGroup_ = None
module_ = None
begin_epoch_ = 0
train_iter_ = None
test_iter_ = None
context_ = None
checkpoint_period_ = 1
module = None
_data_dir_ = "data/${tc.fullArchitectureName}/"
_model_dir_ = "model/${tc.fullArchitectureName}/"
_model_prefix_ = "${tc.architectureName}"
......@@ -28,18 +25,20 @@ class ${tc.fileNameWithoutEnding}:
_input_shapes_ = [<#list tc.architecture.inputs as input>(${tc.join(input.definition.type.dimensions, ",")})</#list>]
_output_names_ = [${tc.join(tc.architectureOutputs, ",", "'", "_label'")}]
def __init__(self, context=mx.gpu()):
self.context_ = context
self.construct()
self.outputGroup_ = mx.symbol.Group([${tc.join(tc.architectureOutputs, ",", "self.", "")}])
self.module_ = mx.mod.Module(symbol=self.outputGroup_,
data_names=self._input_names_,
label_names=self._output_names_,
context=self.context_)
def load(self):
def load(self, context):
lastEpoch = 0
param_file = None
try:
os.remove(self._model_dir_ + self._model_prefix_ + "_newest-0000.params")
except OSError:
pass
try:
os.remove(self._model_dir_ + self._model_prefix_ + "_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_ in file:
......@@ -48,65 +47,99 @@ class ${tc.fileNameWithoutEnding}:
if epoch > lastEpoch:
lastEpoch = epoch
param_file = file
if param_file != None:
if param_file is None:
return 0
else:
logging.info("Loading checkpoint: " + param_file)
self.begin_epoch_ = lastEpoch
self.module_.load(prefix=self._model_dir_ + self._model_prefix_,
self.module.load(prefix=self._model_dir_ + self._model_prefix_,
epoch=lastEpoch,
data_names=self._input_names_,
label_names=self._output_names_,
context=self.context_)
context=context)
return lastEpoch
def getH5ArrayIter(self, batch_size):
def load_data(self, batch_size):
train_h5, test_h5 = self.load_h5_files()
data_mean = train_h5[self._input_names_[0]][:].mean(axis=0)
data_std = train_h5[self._input_names_[0]][:].std(axis=0) + 1e-5
train_iter = mx.io.NDArrayIter(train_h5[self._input_names_[0]],
train_h5[self._output_names_[0]],
batch_size=batch_size,
data_name=self._input_names_[0],
label_name=self._output_names_[0])
test_iter = None
if test_h5 != None:
test_iter = mx.io.NDArrayIter(test_h5[self._input_names_[0]],
test_h5[self._output_names_[0]],
batch_size=batch_size,
data_name=self._input_names_[0],
label_name=self._output_names_[0])
return train_iter, test_iter, data_mean, data_std
def load_h5_files(self):
train_h5 = None
test_h5 = None
train_path = self._data_dir_ + "train.h5"
test_path = self._data_dir_ + "test.h5"
if os.path.isfile(train_path):
train_file = h5py.File(train_path, 'r')
if self._input_names_[0] in train_file and self._output_names_[0] in train_file:
train_iter = mx.io.NDArrayIter(train_file[self._input_names_[0]],
train_file[self._output_names_[0]],
batch_size=batch_size,
data_name=self._input_names_[0],
label_name=self._output_names_[0])
else:
train_h5 = h5py.File(train_path, 'r')
if not (self._input_names_[0] in train_h5 and self._output_names_[0] in train_h5):
logging.error("The HDF5 file '" + os.path.abspath(train_path) + "' has to contain the datasets: "
+ "'" + self._input_names_[0] + "', '" + self._output_names_[0] + "'")
sys.exit(1)
test_iter = None
if os.path.isfile(test_path):
test_file = h5py.File(test_path, 'r')
if self._input_names_[0] in test_file and self._output_names_[0] in test_file:
test_iter = mx.io.NDArrayIter(test_file[self._input_names_[0]],
test_file[self._output_names_[0]],
batch_size=batch_size,
data_name=self._input_names_[0],
label_name=self._output_names_[0])
else:
test_h5 = h5py.File(test_path, 'r')
if not (self._input_names_[0] in test_h5 and self._output_names_[0] in test_h5):
logging.error("The HDF5 file '" + os.path.abspath(test_path) + "' has to contain the datasets: "
+ "'" + self._input_names_[0] + "', '" + self._output_names_[0] + "'")
sys.exit(1)
else:
logging.warning("Couldn't load test set. File '" + os.path.abspath(test_path) + "' does not exist.")
return train_iter, test_iter
return train_h5, test_h5
else:
logging.error("Data loading failure. File '" + os.path.abspath(train_path) + "' does not exist.")
sys.exit(1)
def train(self, batch_size,
train_iter=None,
test_iter=None,
num_epoch=10,
optimizer='adam',
optimizer_params=(('learning_rate', 0.001),),
load_checkpoint=True):
load_checkpoint=True,
context=mx.gpu(),
checkpoint_period=5,
normalize=True):
if train_iter == None:
train_iter, test_iter = self.getH5ArrayIter(batch_size)
if 'weight_decay' in optimizer_params:
optimizer_params['wd'] = optimizer_params['weight_decay']
del optimizer_params['weight_decay']
if 'learning_rate_decay' in optimizer_params:
min_learning_rate = 1e-08
if 'learning_rate_minimum' in optimizer_params:
min_learning_rate = optimizer_params['learning_rate_minimum']
del optimizer_params['learning_rate_minimum']
optimizer_params['lr_scheduler'] = mx.lr_scheduler.FactorScheduler(
optimizer_params['step_size'],
factor=optimizer_params['learning_rate_decay'],
stop_factor_lr=min_learning_rate)
del optimizer_params['step_size']
del optimizer_params['learning_rate_decay']
train_iter, test_iter, data_mean, data_std = self.load_data(batch_size)
if self.module == None:
if normalize:
self.construct(context, data_mean, data_std)
else:
self.construct(context)
begin_epoch = 0
if load_checkpoint:
self.load()
begin_epoch = self.load(context)
else:
if os.path.isdir(self._model_dir_):
shutil.rmtree(self._model_dir_)
......@@ -117,16 +150,22 @@ class ${tc.fileNameWithoutEnding}:
if not os.path.isdir(self._model_dir_):
raise
self.module_.fit(
self.module.fit(
train_data=train_iter,
eval_data=test_iter,
optimizer=optimizer,
optimizer_params=optimizer_params,
batch_end_callback=mx.callback.Speedometer(batch_size),
epoch_end_callback=mx.callback.do_checkpoint(prefix=self._model_dir_ + self._model_prefix_, period=self.checkpoint_period_),
begin_epoch=self.begin_epoch_,
num_epoch=num_epoch + self.begin_epoch_)
epoch_end_callback=mx.callback.do_checkpoint(prefix=self._model_dir_ + self._model_prefix_, period=checkpoint_period),
begin_epoch=begin_epoch,
num_epoch=num_epoch + begin_epoch)
self.module.save_checkpoint(self._model_dir_ + self._model_prefix_, num_epoch + begin_epoch)
self.module.save_checkpoint(self._model_dir_ + self._model_prefix_ + '_newest', 0)
def construct(self):
${tc.include(tc.architecture.body)}
\ No newline at end of file
def construct(self, context, data_mean=None, data_std=None):
${tc.include(tc.architecture.body)}
self.module = mx.mod.Module(symbol=mx.symbol.Group([${tc.join(tc.architectureOutputs, ",")}]),
data_names=self._input_names_,
label_names=self._output_names_,
context=context)
\ No newline at end of file
......@@ -7,12 +7,12 @@
#include <string>
#include <vector>
#include <CNNBufferfile.h>
#include <CNNBufferFile.h>
class ${tc.fileNameWithoutEnding}{
public:
const std::string json_file = "model/${tc.fullArchitectureName}/${tc.architectureName}-symbol.json";
const std::string param_file = "model/${tc.fullArchitectureName}/${tc.architectureName}.params";
const std::string json_file = "model/${tc.fullArchitectureName}/${tc.architectureName}_newest-symbol.json";
const std::string param_file = "model/${tc.fullArchitectureName}/${tc.architectureName}_newest-0000.params";
const std::vector<std::string> input_keys = {"data"};
//const std::vector<std::string> input_keys = {${tc.join(tc.architectureInputs, ",", "\"", "\"")}};
const std::vector<std::vector<mx_uint>> input_shapes = {<#list tc.architecture.inputs as input>{1,${tc.join(input.definition.type.dimensions, ",")}}<#if input?has_next>,</#if></#list>};
......
......@@ -3,19 +3,19 @@
vector<float> CNN_${tc.getName(output)}(<#list shape as dim>${dim?c}<#if dim?has_next>*</#if></#list>);
</#list>
CNN.predict(<#list tc.architecture.inputs as input>CNNTranslator::translate(${input.name}<#if input.arrayAccess.isPresent()>[${input.arrayAccess.get().intValue.get()?c}]</#if>),
_cnn_.predict(<#list tc.architecture.inputs as input>CNNTranslator::translate(${input.name}<#if input.arrayAccess.isPresent()>[${input.arrayAccess.get().intValue.get()?c}]</#if>),
</#list><#list tc.architecture.outputs as output>CNN_${tc.getName(output)}<#if output?has_next>,
</#if></#list>);
<#list tc.architecture.outputs as output>
<#assign shape = output.definition.type.dimensions>
<#if shape?size == 1>
${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToCol(CNN_${tc.getName(output)}, std::vector<int> {${shape[0]?c}});
${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToCol(CNN_${tc.getName(output)}, std::vector<size_t> {${shape[0]?c}});
</#if>
<#if shape?size == 2>
${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToMat(CNN_${tc.getName(output)}, std::vector<int> {${shape[0]?c}, ${shape[1]?c}});
${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToMat(CNN_${tc.getName(output)}, std::vector<size_t> {${shape[0]?c}, ${shape[1]?c}});
</#if>
<#if shape?size == 3>
${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToCube(CNN_${tc.getName(output)}, std::vector<int> {${shape[0]?c}, ${shape[1]?c}, ${shape[2]?c}});
${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToCube(CNN_${tc.getName(output)}, std::vector<size_t> {${shape[0]?c}, ${shape[1]?c}, ${shape[2]?c}});
</#if>
</#list>
\ No newline at end of file
......@@ -7,9 +7,8 @@
<#if widthIndex != 0><#assign indexList = indexList + [widthIndex]></#if>
<#assign dimensions = tc.currentLayer.outputTypes[0].dimensions>
<#if tc.targetLanguage == ".py">
self.${tc.currentName} = mx.sym.var("${tc.currentName}",
${tc.currentName} = mx.sym.var("${tc.currentName}",
shape=(0,${tc.join(dimensions, ",")}))
${tc.currentName} = self.${tc.currentName}
<#include "OutputShape.ftl">
<#if heightIndex != channelIndex + 1 || widthIndex != heightIndex + 1>
${tc.currentName} = mx.symbol.transpose(data=${tc.currentName},
......@@ -20,22 +19,12 @@
${tc.currentName} = mx.symbol.reshape(data=${tc.currentName},
shape=(0,${tc.currentLayer.outputTypes[0].channels?c},${tc.currentLayer.outputTypes[0].height?c},${tc.currentLayer.outputTypes[0].width?c}))
</#if>
<#elseif tc.targetLanguage == ".cpp">
m_${tc.currentName} = Symbol::Variable("${tc.currentName}");
m_${tc.currentName}.SetParam("shape", Shape(0,${tc.join(dimensions, ",")}));
auto ${tc.currentName} = m_${tc.currentName};
<#include "OutputShape.ftl">
<#if heightIndex != channelIndex + 1 || widthIndex != heightIndex + 1>
${tc.currentName} = Operator("transpose")
.SetParam("axes", Shape(0,${tc.join(indexList, ",")}))
.SetInput("data", ${tc.currentName})
.CreateSymbol();
</#if>
<#if indexList?size != 3>
${tc.currentName} = Operator("reshape")
.SetParam("shape", Shape(0,${tc.currentLayer.outputTypes[0].channels?c},${tc.currentLayer.outputTypes[0].height?c},${tc.currentLayer.outputTypes[0].width?c}))
.SetInput("data", ${tc.currentName})
.CreateSymbol();
</#if>
if not data_mean is None:
assert(not data_std is None)
_data_mean_ = mx.sym.Variable("_data_mean_", shape=(${tc.join(dimensions, ",")}), init=MyConstant(value=data_mean.tolist()))
_data_mean_ = mx.sym.BlockGrad(_data_mean_)
_data_std_ = mx.sym.Variable("_data_std_", shape=(${tc.join(dimensions, ",")}), init=MyConstant(value=data_mean.tolist()))
_data_std_ = mx.sym.BlockGrad(_data_std_)
${tc.currentName} = mx.symbol.broadcast_sub(${tc.currentName}, _data_mean_)
${tc.currentName} = mx.symbol.broadcast_div(${tc.currentName}, _data_std_)
</#if>
\ No newline at end of file
<#if tc.targetLanguage == ".py">
<#if tc.softmaxOutput>
self.${tc.currentName} = mx.symbol.SoftmaxOutput(data=${tc.currentInputs[0]},
${tc.currentName} = mx.symbol.SoftmaxOutput(data=${tc.currentInputs[0]},
name="${tc.currentName}")
<#elseif tc.logisticRegressionOutput>
self.${tc.currentName} = mx.symbol.LogisticRegressionOutput(data=${tc.currentInputs[0]},
${tc.currentName} = mx.symbol.LogisticRegressionOutput(data=${tc.currentInputs[0]},
name="${tc.currentName}")
<#elseif tc.linearRegressionOutput>
self.${tc.currentName} = mx.symbol.LinearRegressionOutput(data=${tc.currentInputs[0]},
${tc.currentName} = mx.symbol.LinearRegressionOutput(data=${tc.currentInputs[0]},
name="${tc.currentName}")
</#if>
<#elseif tc.targetLanguage == ".cpp">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment