Commit f2579cdb authored by Sebastian Nickels's avatar Sebastian Nickels

Merge rnn into develop

parents 449d6a35 f02920c0
Pipeline #150621 failed with stages
......@@ -5,7 +5,6 @@ nppBackup
.classpath
.idea
.git
*.iml
.vscode
*.iml
train.log
{
"configurations": [
{
"type": "java",
"name": "CodeLens (Launch) - EMADLGeneratorCli",
"request": "launch",
"mainClass": "de.monticore.lang.monticar.emadl.generator.EMADLGeneratorCli",
"projectName": "embedded-montiarc-emadl-generator"
}
]
}
\ No newline at end of file
......@@ -15,10 +15,10 @@
<properties>
<!-- .. SE-Libraries .................................................. -->
<emadl.version>0.2.6</emadl.version>
<emadl.version>0.2.8-SNAPSHOT</emadl.version>
<CNNTrain.version>0.3.2-SNAPSHOT</CNNTrain.version>
<cnnarch-mxnet-generator.version>0.2.15-SNAPSHOT</cnnarch-mxnet-generator.version>
<cnnarch-caffe2-generator.version>0.2.11-SNAPSHOT</cnnarch-caffe2-generator.version>
<cnnarch-mxnet-generator.version>0.2.16-SNAPSHOT</cnnarch-mxnet-generator.version>
<cnnarch-caffe2-generator.version>0.2.12-SNAPSHOT</cnnarch-caffe2-generator.version>
<cnnarch-gluon-generator.version>0.2.1-SNAPSHOT</cnnarch-gluon-generator.version>
<embedded-montiarc-math-opt-generator>0.1.4</embedded-montiarc-math-opt-generator>
......
......@@ -370,6 +370,8 @@ public class EMADLGenerator {
EMADLCocos.checkAll(componentInstanceSymbol);
if (architecture.isPresent()){
cnnArchGenerator.check(architecture.get());
String dPath = null;
Path dataPathDefinition = Paths.get(getModelsPath(), "data_paths.txt");
if (dataPathDefinition.toFile().exists()) {
......
......@@ -9,9 +9,6 @@ import de.se_rwth.commons.logging.Log;
import java.io.IOException;
import java.util.Optional;
/**
*
*/
public class RewardFunctionCppGenerator implements RewardFunctionSourceGenerator {
public RewardFunctionCppGenerator() {
}
......
......@@ -43,7 +43,6 @@ import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;
public class GenerationTest extends AbstractSymtabTest {
@Before
public void setUp() {
// ensure an empty log
......@@ -143,7 +142,7 @@ public class GenerationTest extends AbstractSymtabTest {
}
@Test
public void testMnistClassifier() throws IOException, TemplateException {
public void testMnistClassifierForCaffe2() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/", "-r", "mnist.MnistClassifier", "-b", "CAFFE2", "-f", "n", "-c", "n"};
EMADLGeneratorCli.main(args);
......@@ -181,7 +180,7 @@ public class GenerationTest extends AbstractSymtabTest {
"CNNCreator_mnist_mnistClassifier_net.py",
"CNNPredictor_mnist_mnistClassifier_net.h",
"CNNDataLoader_mnist_mnistClassifier_net.py",
"supervised_trainer.py",
"CNNSupervisedTrainer_mnist_mnistClassifier_net.py",
"mnist_mnistClassifier_net.h",
"HelperA.h",
"CNNTranslator.h",
......
......@@ -20,28 +20,8 @@
*/
package de.monticore.lang.monticar.emadl;
import de.monticore.lang.monticar.emadl.generator.Backend;
import de.monticore.lang.monticar.emadl.generator.EMADLGenerator;
import de.monticore.lang.monticar.emadl.generator.EMADLGeneratorCli;
import de.se_rwth.commons.logging.Log;
import freemarker.template.TemplateException;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;
public class IntegrationCaffe2Test extends IntegrationTest {
public IntegrationCaffe2Test() {
super("CAFFE2", "39253EC049D4A4E5FA0536AD34874B9D#1DBAEE1B1BD83FB7CB5F70AE91B29638#13D139510DC5681639AA91D7250288D3#1A42D4842D0664937A9F6B727BD60CEF");
}
}
}
\ No newline at end of file
......@@ -20,8 +20,60 @@
*/
package de.monticore.lang.monticar.emadl;
import de.monticore.lang.monticar.emadl.generator.EMADLGeneratorCli;
import de.se_rwth.commons.logging.Log;
import org.junit.Test;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;
public class IntegrationGluonTest extends IntegrationTest {
private Path multipleInputsHashFile = Paths.get("./target/generated-sources-emadl/MultipleInputs.training_hash");
private Path multipleOutputsHashFile = Paths.get("./target/generated-sources-emadl/MultipleOutputs.training_hash");
public IntegrationGluonTest() {
super("GLUON", "39253EC049D4A4E5FA0536AD34874B9D#1DBAEE1B1BD83FB7CB5F70AE91B29638#C4C23549E737A759721D6694C75D9771#5AF0CE68E408E8C1F000E49D72AC214A");
}
@Test
public void testMultipleInputs() {
Log.getFindings().clear();
deleteHashFile(multipleInputsHashFile);
String[] args = {"-m", "src/test/resources/models/", "-r", "MultipleInputs", "-b", "GLUON"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
}
@Test
public void testMultipleOutputs() {
Log.getFindings().clear();
deleteHashFile(multipleOutputsHashFile);
String[] args = {"-m", "src/test/resources/models/", "-r", "MultipleOutputs", "-b", "GLUON"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
}
private void deleteHashFile(Path hashFile) {
try {
Files.delete(hashFile);
}
catch (NoSuchFileException e) {
}
catch(Exception e) {
assertFalse("Could not delete hash file", true);
}
}
}
......@@ -20,25 +20,6 @@
*/
package de.monticore.lang.monticar.emadl;
import de.monticore.lang.monticar.emadl.generator.Backend;
import de.monticore.lang.monticar.emadl.generator.EMADLGenerator;
import de.monticore.lang.monticar.emadl.generator.EMADLGeneratorCli;
import de.se_rwth.commons.logging.Log;
import freemarker.template.TemplateException;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;
public class IntegrationMXNetTest extends IntegrationTest {
public IntegrationMXNetTest() {
super("MXNET", "39253EC049D4A4E5FA0536AD34874B9D#1DBAEE1B1BD83FB7CB5F70AE91B29638#C4C23549E737A759721D6694C75D9771#5AF0CE68E408E8C1F000E49D72AC214A");
......
......@@ -41,7 +41,7 @@ component Alexnet{
fc(->=2) ->
FullyConnected(units=1000) ->
Softmax() ->
predictions
predictions;
}
}
\ No newline at end of file
......@@ -18,7 +18,7 @@ component NetworkB<Z classes = 10> (Z convChannels){
Dropout() ->
FullyConnected(units=classes) ->
Softmax() ->
predictions
predictions;
}
}
\ No newline at end of file
......@@ -27,6 +27,6 @@ component InvalidLayerInput{
fc() ->
FullyConnected(units=1000) ->
Softmax() ->
predictions
predictions;
}
}
\ No newline at end of file
configuration MultipleInputs{
num_epoch:10
batch_size:5
context:cpu
optimizer:adam{
learning_rate:0.01
learning_rate_decay:0.8
step_size:1000
weight_decay:0.0001
}
}
component MultipleInputs{
ports in Z(0:255)^{3, 32, 32} data[2],
out Q(0:1)^{10} softmax;
implementation CNN {
(
data[0] ->
Convolution(kernel=(5,5), channels=8, padding="valid") ->
Convolution(kernel=(5,5), channels=8, padding="valid") ->
FullyConnected(units=128) ->
Dropout()
|
data[1] ->
Convolution(kernel=(5,5), channels=8, padding="valid") ->
Convolution(kernel=(5,5), channels=8, padding="valid") ->
FullyConnected(units=128) ->
Dropout()
) ->
Concatenate() ->
FullyConnected(units=10) ->
Softmax() ->
softmax;
}
}
\ No newline at end of file
configuration MultipleOutputs{
num_epoch:10
batch_size:64
normalize:true
load_checkpoint:false
optimizer:adam{
learning_rate:0.01
learning_rate_decay:0.8
step_size:1000
}
num_epoch:10
batch_size:5
context:cpu
optimizer:adam{
learning_rate:0.01
learning_rate_decay:0.8
step_size:1000
weight_decay:0.0001
}
}
component MultipleOutputs{
ports in Q(-oo:+oo)^{10,1,1} data,
out Q(0:1)^{4} pred[2];
ports in Z(0:255)^{3, 32, 32} data,
out Q(0:1)^{10} pred[2];
implementation CNN {
data ->
FullyConnected(units=128, no_bias=true) ->
Tanh() ->
(
FullyConnected(units=16, no_bias=true) ->
Tanh() ->
FullyConnected(units=4, no_bias=true)
|
FullyConnected(units=16, no_bias=true) ->
Tanh() ->
FullyConnected(units=4, no_bias=true)
) ->
Convolution(kernel=(5,5), channels=8, padding="valid") ->
Convolution(kernel=(5,5), channels=8, padding="valid") ->
FullyConnected(units=128) ->
Dropout() ->
FullyConnected(units=10) ->
(
[0] ->
Softmax() ->
pred[0]
|
[1] ->
Softmax() ->
pred[1]
)
);
}
}
\ No newline at end of file
configuration MultipleOutputs{
num_epoch:10
batch_size:64
normalize:true
load_checkpoint:false
optimizer:adam{
learning_rate:0.01
learning_rate_decay:0.8
step_size:1000
}
}
component MultipleOutputs{
ports in Q(-oo:+oo)^{10,1,1} data,
out Q(0:1)^{4} pred[2];
implementation CNN {
data ->
FullyConnected(units=128, no_bias=true) ->
Tanh() ->
(
FullyConnected(units=16, no_bias=true) ->
Tanh() ->
FullyConnected(units=4, no_bias=true)
|
FullyConnected(units=16, no_bias=true) ->
Tanh() ->
FullyConnected(units=4, no_bias=true)
) ->
(
[0] ->
Softmax() ->
pred[0]
|
[1] ->
Softmax() ->
pred[1]
);
}
}
\ No newline at end of file
......@@ -40,6 +40,6 @@ component ResNeXt50{
GlobalPooling(pool_type="avg") ->
FullyConnected(units=1000) ->
Softmax() ->
predictions
predictions;
}
}
\ No newline at end of file
......@@ -34,6 +34,6 @@ component ResNet152{
GlobalPooling(pool_type="avg") ->
FullyConnected(units=1000) ->
Softmax() ->
predictions
predictions;
}
}
\ No newline at end of file
......@@ -32,6 +32,6 @@ component ResNet34{
GlobalPooling(pool_type="avg") ->
FullyConnected(units=1000) ->
Softmax() ->
predictions
predictions;
}
}
\ No newline at end of file
......@@ -24,7 +24,7 @@ component ThreeInputCNN_M14{
Relu() ->
FullyConnected(units=10) ->
Softmax() ->
predictions
predictions;
}
}
\ No newline at end of file
......@@ -29,6 +29,6 @@ component VGG16{
fc() ->
FullyConnected(units=1000) ->
Softmax() ->
predictions
predictions;
}
}
\ No newline at end of file
......@@ -14,7 +14,7 @@ component Network{
Relu() ->
FullyConnected(units=10) ->
Softmax() ->
predictions
predictions;
}
}
......@@ -34,7 +34,7 @@ component CifarNetwork<Z(2:oo) classes = 10>{
Dropout()->
FullyConnected(units=classes) ->
Softmax() ->
softmax
softmax;
}
}
\ No newline at end of file
cifar10.CifarNetwork src/test/resources/training_data
simpleCifar10.CifarNetwork src/test/resources/training_data
cNNCalculator.Network src/test/resources/training_data
MultipleOutputs data/MultipleOutputs
cifar10.CifarNetwork src/test/resources/training_data/Cifar
simpleCifar10.CifarNetwork src/test/resources/training_data/Cifar
cNNCalculator.Network src/test/resources/training_data/Cifar
InstanceTest.NetworkB data/InstanceTest.NetworkB
Alexnet data/Alexnet
ThreeInputCNN_M14 data/ThreeInputCNN_M14
VGG16 data/VGG16
ResNeXt50 data/ResNeXt50
instanceTestCifar.CifarNetwork src/test/resources/training_data
instanceTestCifar.CifarNetwork src/test/resources/training_data/Cifar
mnist.LeNetNetwork data/mnist.LeNetNetwork
MultipleInputs src/test/resources/training_data/MultipleInputs
MultipleOutputs src/test/resources/training_data/MultipleOutputs
\ No newline at end of file
......@@ -12,7 +12,7 @@ component CifarNetwork<Z(2:oo) classes = 10>{
Dropout()->
FullyConnected(units=classes) ->
Softmax() ->
softmax
softmax;
}
}
\ No newline at end of file
......@@ -14,7 +14,7 @@ component LeNetNetwork<Z(2:oo) classes = 10>{
Relu() ->
FullyConnected(units=classes) ->
Softmax() ->
predictions
predictions;
}
}
......@@ -12,6 +12,6 @@ component CartPoleDQN {
FullyConnected(units=256) ->
Tanh() ->
FullyConnected(units=2) ->
qvalues
qvalues;
}
}
\ No newline at end of file
......@@ -13,6 +13,6 @@ component MountaincarActor {
Relu() ->
FullyConnected(units=1) ->
Tanh() ->
action
action;
}
}
\ No newline at end of file
......@@ -8,5 +8,5 @@ implementation Critic(state, action) {
FullyConnected(units=300)
) ->
Add() ->
Relu()
Relu();
}
\ No newline at end of file
......@@ -12,6 +12,6 @@ component TorcsDQN<Z(1:oo) discrete_actions = 2> {
FullyConnected(units=256) ->
Tanh() ->
FullyConnected(units=discrete_actions) ->
qvalues
qvalues;
}
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ component CifarNetwork<Z(2:oo) classes = 10>{
Dropout()->
FullyConnected(units=classes) ->
Softmax() ->
softmax
softmax;
}
}
\ No newline at end of file
......@@ -18,7 +18,7 @@ class MyConstant(mx.init.Initializer):
class CNNCreator_cifar10_cifar10Classifier_net:
module = None
_data_dir_ = "src/test/resources/training_data/"
_data_dir_ = "src/test/resources/training_data/Cifar/"
_model_dir_ = "model/cifar10.CifarNetwork/"
_model_prefix_ = "model"
_input_names_ = ['data']
......
......@@ -43,12 +43,11 @@ class CNNCreator_mnist_mnistClassifier_net:
self.net.load_parameters(self._model_dir_ + param_file)
return lastEpoch
def construct(self, context, data_mean=None, data_std=None):
self.net = Net(data_mean=data_mean, data_std=data_std)
self.net.collect_params().initialize(self.weight_initializer, ctx=context)
self.net.hybridize()
self.net(mx.nd.zeros((1,)+self._input_shapes_[0], ctx=context))
self.net(mx.nd.zeros((1,) + self._input_shapes_[0], ctx=context))
if not os.path.exists(self._model_dir_):
os.makedirs(self._model_dir_)
......
......@@ -3,8 +3,9 @@ import h5py
import mxnet as mx
import logging
import sys
from mxnet import nd
class mnist_mnistClassifier_netDataLoader:
class CNNDataLoader_mnist_mnistClassifier_net:
_input_names_ = ['image']
_output_names_ = ['predictions_label']
......@@ -14,21 +15,38 @@ class mnist_mnistClassifier_netDataLoader:
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_data = {}
data_mean = {}
data_std = {}
for input_name in self._input_names_:
train_data[input_name] = train_h5[input_name]
data_mean[input_name] = nd.array(train_h5[input_name][:].mean(axis=0))
data_std[input_name] = nd.array(train_h5[input_name][:].std(axis=0) + 1e-5)
train_label = {}
for output_name in self._output_names_:
train_label[output_name] = train_h5[output_name]
train_iter = mx.io.NDArrayIter(data=train_data,
label=train_label,
batch_size=batch_size)
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])
test_data = {}
for input_name in self._input_names_:
test_data[input_name] = test_h5[input_name]
test_label = {}
for output_name in self._output_names_:
test_label[output_name] = test_h5[output_name]
test_iter = mx.io.NDArrayIter(data=test_data,
label=test_label,
batch_size=batch_size)
return train_iter, test_iter, data_mean, data_std
def load_h5_files(self):
......@@ -36,21 +54,39 @@ class mnist_mnistClassifier_netDataLoader:
test_h5 = None
train_path = self._data_dir + "train.h5"
test_path = self._data_dir + "test.h5"
if os.path.isfile(train_path):
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
for input_name in self._input_names_:
if not input_name in train_h5:
logging.error("The HDF5 file '" + os.path.abspath(train_path) + "' has to contain the dataset "
+ "'" + input_name + "'")
sys.exit(1)
for output_name in self._output_names_:
if not output_name in train_h5:
logging.error("The HDF5 file '" + os.path.abspath(train_path) + "' has to contain the dataset "
+ "'" + output_name + "'")
sys.exit(1)
if os.path.isfile(test_path):
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)
for input_name in self._input_names_:
if not input_name in test_h5:
logging.error("The HDF5 file '" + os.path.abspath(test_path) + "' has to contain the dataset "
+ "'" + input_name + "'")
sys.exit(1)
for output_name in self._output_names_:
if not output_name in test_h5:
logging.error("The HDF5 file '" + os.path.abspath(test_path) + "' has to contain the dataset "
+ "'" + output_name + "'")
sys.exit(1)
else:
logging.warning("Couldn't load test set. File '" + os.path.abspath(test_path) + "' does not exist.")
return train_h5, test_h5
else:
logging.error("Data loading failure. File '" + os.path.abspath(train_path) + "' does not exist.")
......
......@@ -2,6 +2,16 @@ import mxnet as mx
import numpy as np
from mxnet import gluon
class OneHot(gluon.HybridBlock):
def __init__(self, size, **kwargs):
super(OneHot, self).__init__(**kwargs)
with self.name_scope():
self.size = size
def hybrid_forward(self, F, x):
return F.one_hot(indices=F.argmax(data=x, axis=1), depth=self.size)
class Softmax(gluon.HybridBlock):
def __init__(self, **kwargs):
super(Softmax, self).__init__(**kwargs)
......@@ -71,12 +81,14 @@ class NoNormalization(gluon.HybridBlock):
class Net(gluon.HybridBlock):
def __init__(self, data_mean=None, data_std=None, **kwargs):
super(Net, self).__init__(**kwargs)
self.last_layers = {}
with self.name_scope():
if not data_mean is None:
assert(not data_std is None)
self.image_input_normalization = ZScoreNormalization(data_mean=data_mean, data_std=data_std)
if data_mean:
assert(data_std)
self.input_normalization_image = ZScoreNormalization(data_mean=data_mean['image'],
data_std=data_std['image'])