...
 
Commits (60)
......@@ -8,3 +8,4 @@ nppBackup
.vscode
*.iml
train.log
model
......@@ -9,7 +9,7 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-emadl-generator</artifactId>
<version>0.3.6-SNAPSHOT</version>
<version>0.3.7-SNAPSHOT</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
......@@ -17,11 +17,11 @@
<!-- .. SE-Libraries .................................................. -->
<emadl.version>0.2.10-SNAPSHOT</emadl.version>
<CNNTrain.version>0.3.6-SNAPSHOT</CNNTrain.version>
<CNNTrain.version>0.3.8-SNAPSHOT</CNNTrain.version>
<cnnarch-generator.version>0.0.4-SNAPSHOT</cnnarch-generator.version>
<cnnarch-mxnet-generator.version>0.2.17-SNAPSHOT</cnnarch-mxnet-generator.version>
<cnnarch-caffe2-generator.version>0.2.13-SNAPSHOT</cnnarch-caffe2-generator.version>
<cnnarch-gluon-generator.version>0.2.8-SNAPSHOT</cnnarch-gluon-generator.version>
<cnnarch-gluon-generator.version>0.2.9-SNAPSHOT</cnnarch-gluon-generator.version>
<cnnarch-tensorflow-generator.version>0.1.0-SNAPSHOT</cnnarch-tensorflow-generator.version>
<embedded-montiarc-math-opt-generator>0.1.4</embedded-montiarc-math-opt-generator>
......@@ -263,6 +263,7 @@
<maxmem>256m</maxmem>
<!-- aggregated reports for multi-module projects -->
<aggregate>true</aggregate>
<check/>
</configuration>
</plugin>
</plugins>
......
......@@ -10,7 +10,7 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instance
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstantiationSymbol;
import de.monticore.lang.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.SerialCompositeElementSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.NetworkInstructionSymbol;
import de.monticore.lang.monticar.cnnarch.generator.CNNArchGenerator;
import de.monticore.lang.monticar.cnnarch.generator.CNNTrainGenerator;
import de.monticore.lang.monticar.cnnarch.generator.DataPathConfigParser;
......@@ -451,9 +451,11 @@ public class EMADLGenerator {
}
contentMap.remove(executeKey);
String applyBeamSearchMethod = contentMap.get("BeamSearch_" + fullName);
String component = emamGen.generateString(taggingResolver, instance, (MathStatementsSymbol) null);
FileContent componentFileContent = new FileContent(
transformComponent(component, "CNNPredictor_" + fullName, executeMethod, architecture),
transformComponent(component, "CNNPredictor_" + fullName, applyBeamSearchMethod, executeMethod, architecture),
instance);
for (String fileName : contentMap.keySet()){
......@@ -463,7 +465,7 @@ public class EMADLGenerator {
fileContents.add(new FileContent(readResource("CNNTranslator.h", Charsets.UTF_8), "CNNTranslator.h"));
}
protected String transformComponent(String component, String predictorClassName, String executeMethod, ArchitectureSymbol architecture){
protected String transformComponent(String component, String predictorClassName, String applyBeamSearchMethod, String executeMethod, ArchitectureSymbol architecture){
//insert includes
component = component.replaceFirst("using namespace",
"#include \"" + predictorClassName + ".h" + "\"\n" +
......@@ -474,16 +476,17 @@ public class EMADLGenerator {
String networkAttributes = "public:";
int i = 0;
for (SerialCompositeElementSymbol stream : architecture.getStreams()) {
if (stream.isTrainable()) {
for (NetworkInstructionSymbol networkInstruction : architecture.getNetworkInstructions()) {
networkAttributes += "\n" + predictorClassName + "_" + i + " _predictor_" + i + "_;";
}
++i;
}
component = component.replaceFirst("public:", networkAttributes);
//insert BeamSearch method
//component = component.replaceFirst("void init\\(\\)", applyBeamSearchMethod + "\nvoid init()");
//insert execute method
component = component.replaceFirst("void execute\\(\\)\\s\\{\\s\\}",
"void execute(){\n" + executeMethod + "\n}");
......
......@@ -3,6 +3,9 @@
#define CNNTRANSLATOR_H
#include <armadillo>
#include <cassert>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
using namespace arma;
......@@ -108,6 +111,46 @@ public:
return cubeMatrix;
}
static ivec translateToIntCol(const vector<float> &source, const vector<size_t> &shape){
assert(shape.size() == 1);
ivec column(shape[0]);
for(size_t i = 0; i < source.size(); i++){
column(i) = (size_t) source[i];
}
return column;
}
static imat translateToIntMat(const vector<float> &source, const vector<size_t> &shape){
assert(shape.size() == 2);
imat matrix(shape[1], shape[0]); //create transposed version of the matrix
int startPos = 0;
int endPos = matrix.n_rows;
const vector<size_t> columnShape = {matrix.n_rows};
for(size_t i = 0; i < matrix.n_cols; i++){
vector<float> colSource(&source[startPos], &source[endPos]);
matrix.col(i) = translateToIntCol(colSource, columnShape);
startPos = endPos;
endPos += matrix.n_rows;
}
return matrix.t();
}
static icube translateToIntCube(const vector<float> &source, const vector<size_t> &shape){
assert(shape.size() == 3);
icube cubeMatrix(shape[1], shape[2], shape[0]);
const int matrixSize = shape[1] * shape[2];
const vector<size_t> matrixShape = {shape[1], shape[2]};
int startPos = 0;
int endPos = matrixSize;
for(size_t i = 0; i < cubeMatrix.n_slices; i++){
vector<float> matrixSource(&source[startPos], &source[endPos]);
cubeMatrix.slice(i) = translateToIntMat(matrixSource, matrixShape);
startPos = endPos;
endPos += matrixSize;
}
return cubeMatrix;
}
template<typename T> static vector<size_t> getShape(const Col<T> &source){
return {source.n_elem};
}
......
......@@ -8,6 +8,7 @@ import de.se_rwth.commons.logging.Finding;
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;
......@@ -81,7 +82,7 @@ public class GenerationTest extends AbstractSymtabTest {
assertTrue(Log.getFindings().isEmpty());
}
@Test
/*@Test
public void testThreeInputGeneration() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/", "-r", "ThreeInputCNN_M14", "-b", "MXNET", "-f", "n", "-c", "n"};
......@@ -95,7 +96,7 @@ public class GenerationTest extends AbstractSymtabTest {
String[] args = {"-m", "src/test/resources/models/", "-r", "MultipleOutputs", "-b", "MXNET", "-f", "n", "-c", "n"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().size() == 1);
}
}*/
@Test
public void testVGGGeneration() throws IOException, TemplateException {
......@@ -139,6 +140,7 @@ public class GenerationTest extends AbstractSymtabTest {
"CNNTrainer_mnist_mnistClassifier_net.py"));
}
@Ignore
@Test
public void testMnistClassifierForTensorflow() throws IOException, TemplateException {
Log.getFindings().clear();
......@@ -197,13 +199,6 @@ public class GenerationTest extends AbstractSymtabTest {
assertTrue(Log.getFindings().size() == 0);
}
@Test
public void testRNNtestForGluon() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/", "-r", "RNNtest", "-b", "GLUON", "-f", "n", "-c", "n"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().size() == 0);
}
@Test
public void testGluonReinforcementModelGymEnvironment() {
......
......@@ -16,8 +16,6 @@ import static org.junit.Assert.assertFalse;
public class IntegrationGluonTest extends IntegrationTest {
private Path multipleStreamsHashFile = Paths.get("./target/generated-sources-emadl/MultipleStreams.training_hash");
public IntegrationGluonTest() {
super("GLUON", "39253EC049D4A4E5FA0536AD34874B9D#1DBAEE1B1BD83FB7CB5F70AE91B29638#C4C23549E737A759721D6694C75D9771#5AF0CE68E408E8C1F000E49D72AC214A");
}
......@@ -26,7 +24,7 @@ public class IntegrationGluonTest extends IntegrationTest {
public void testMultipleStreams() {
Log.getFindings().clear();
deleteHashFile(multipleStreamsHashFile);
deleteHashFile(Paths.get("./target/generated-sources-emadl/MultipleStreams.training_hash"));
String[] args = {"-m", "src/test/resources/models/", "-r", "MultipleStreams", "-b", "GLUON"};
EMADLGeneratorCli.main(args);
......@@ -34,6 +32,45 @@ public class IntegrationGluonTest extends IntegrationTest {
assertTrue(Log.getFindings().isEmpty());
}
@Ignore
@Test
public void testRNNencdec() {
Log.getFindings().clear();
deleteHashFile(Paths.get("./target/generated-sources-emadl/rnnencdec/Network.training_hash"));
String[] args = {"-m", "src/test/resources/models", "-r", "rnnencdec.Main", "-b", "GLUON"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
}
@Ignore
@Test
public void testRNNsearch() {
Log.getFindings().clear();
deleteHashFile(Paths.get("./target/generated-sources-emadl/rnnsearch/Network.training_hash"));
String[] args = {"-m", "src/test/resources/models", "-r", "rnnsearch.Main", "-b", "GLUON"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
}
@Ignore
@Test
public void testShowAttendTell() {
Log.getFindings().clear();
deleteHashFile(Paths.get("./target/generated-sources-emadl/showAttendTell/Show_attend_tell.training_hash"));
String[] args = {"-m", "src/test/resources/models", "-r", "showAttendTell.Main", "-b", "GLUON"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
}
private void deleteHashFile(Path hashFile) {
try {
Files.delete(hashFile);
......
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.emadl;
import org.junit.Ignore;
public class IntegrationMXNetTest extends IntegrationTest {
public IntegrationMXNetTest() {
super("MXNET", "39253EC049D4A4E5FA0536AD34874B9D#1DBAEE1B1BD83FB7CB5F70AE91B29638#C4C23549E737A759721D6694C75D9771#5AF0CE68E408E8C1F000E49D72AC214A");
......
......@@ -4,7 +4,6 @@ package de.monticore.lang.monticar.emadl;
import de.monticore.lang.monticar.emadl.generator.EMADLGeneratorCli;
import de.se_rwth.commons.logging.Finding;
import de.se_rwth.commons.logging.Log;
import org.junit.Ignore;
import org.junit.Test;
import java.nio.file.Paths;
......@@ -12,13 +11,12 @@ import java.util.Arrays;
import java.util.stream.Collectors;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assume.assumeFalse;
/**
*
*/
public class IntegrationPythonWrapperTest extends AbstractSymtabTest {
@Test
public void testGluonReinforcementModelRosEnvironment() {
assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/reinforcementModel", "-r", "torcs.agent.TorcsAgent", "-b", "GLUON", "-f", "n", "-c", "n"};
EMADLGeneratorCli.main(args);
......@@ -64,11 +62,60 @@ public class IntegrationPythonWrapperTest extends AbstractSymtabTest {
"reinforcement_learning/cnnarch_logger.py"
)
);
/*
assertTrue(Paths.get(
"./target/generated-sources-emadl/reinforcement_learning/_torcs_agent_dqn_reward_executor.so")
.toFile().exists());
assertTrue(Paths.get(
"./target/generated-sources-emadl/reinforcement_learning/torcs_agent_dqn_reward_executor.py")
.toFile().exists());
*/
}
@Test
public void testTorcsTD3() {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/reinforcementModel/torcs_td3", "-r", "torcs.agent.TorcsAgent", "-b", "GLUON", "-f", "n", "-c", "n"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().stream().filter(Finding::isError).collect(Collectors.toList()).isEmpty());
checkFilesAreEqual(
Paths.get("./target/generated-sources-emadl"),
Paths.get("./src/test/resources/target_code/gluon/reinforcementModel/torcs_td3"),
Arrays.asList(
"CMakeLists.txt",
"CNNBufferFile.h",
"torcs_agent_torcsAgent.cpp",
"torcs_agent_torcsAgent.h",
"torcs_agent_torcsAgent_actor.h",
"CNNCreator_torcs_agent_torcsAgent_actor.py",
"CNNNet_torcs_agent_torcsAgent_actor.py",
"CNNPredictor_torcs_agent_torcsAgent_actor.h",
"CNNTrainer_torcs_agent_torcsAgent_actor.py",
"start_training.sh",
"reward/CMakeLists.txt",
"reward/torcs_agent_network_reward.cpp",
"reward/torcs_agent_network_reward.h",
"reward/pylib/CMakeLists.txt",
"reward/pylib/torcs_agent_network_reward_executor.cpp",
"reward/pylib/torcs_agent_network_reward_executor.h",
"reward/pylib/torcs_agent_network_reward_executor.i",
"reward/pylib/armanpy/armanpy.hpp",
"reward/pylib/armanpy/armanpy.i",
"reward/pylib/armanpy/armanpy_1d.i",
"reward/pylib/armanpy/armanpy_2d.i",
"reward/pylib/armanpy/armanpy_3d.i",
"reward/pylib/armanpy/numpy.i",
"reinforcement_learning/__init__.py",
"reinforcement_learning/strategy.py",
"reinforcement_learning/agent.py",
"reinforcement_learning/environment.py",
"reinforcement_learning/replay_memory.py",
"reinforcement_learning/util.py",
"reinforcement_learning/cnnarch_logger.py",
"reinforcement_learning/CNNCreator_torcs_agent_network_torcsCritic.py",
"reinforcement_learning/CNNNet_torcs_agent_network_torcsCritic.py"
)
);
}
}
......@@ -33,6 +33,7 @@ import java.nio.file.Paths;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;
@Ignore
public class IntegrationTensorflowTest extends IntegrationTest {
private Path multipleStreamsHashFile = Paths.get("./target/generated-sources-emadl/MultipleStreams.training_hash");
......
......@@ -99,8 +99,8 @@ public abstract class IntegrationTest extends AbstractSymtabTest {
String[] args = {"-m", "src/test/resources/models/", "-r", "simpleCifar10.Cifar10Classifier", "-b", this.backend};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().size() == 1);
assertTrue(Log.getFindings().get(0).getMsg().contains("skipped"));
//assertTrue(Log.getFindings().size() == 1);
//assertTrue(Log.getFindings().get(0).getMsg().contains("skipped"));
deleteHashFile();
}
......@@ -116,6 +116,7 @@ public abstract class IntegrationTest extends AbstractSymtabTest {
deleteInstanceTestCifarHashFile();
}
private void deleteInstanceTestCifarHashFile() {
final Path instanceTestCifarHasFile
= Paths.get("./target/generated-sources-emadl/instanceTestCifar/CifarNetwork.training_hash");
......
/* (c) https://github.com/MontiCore/monticore */
configuration RNNtest{
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
}
}
/* (c) https://github.com/MontiCore/monticore */
component RNNtest{
ports in Q(-oo:oo)^{50, 30001} source[2],
out Q(-oo:oo)^{50, 30001} target[2];
ports in Q(0:1)^{30000} source,
out Q(0:1)^{30000} target[5];
implementation CNN {
layer RNN(units=500, layers=2) encoder;
layer RNN(units=500, layers=2) decoder;
implementation CNN{
source -> Softmax() -> target[0];
source[0] ->
encoder;
encoder.output ->
target[0];
encoder.state ->
decoder.state;
source[1] ->
decoder ->
target[1];
timed <t> BeamSearch(max_length=5, width=2){
target[t-1] ->
Concatenate() ->
FullyConnected(units=30000) ->
Softmax() ->
target[t]
};
}
}
......@@ -12,4 +12,6 @@ MultipleInputs src/test/resources/training_data/MultipleInputs
MultipleOutputs src/test/resources/training_data/MultipleOutputs
MultipleStreams src/test/resources/training_data/MultipleStreams
Invariant src/test/resources/training_data/Invariant
RNNtest data/RNNtest
rnnencdec.Network src/test/resources/training_data/newstest
rnnsearch.Network src/test/resources/training_data/newstest
showAttendTell.Show_attend_tell src/test/resources/training_data/Show_attend_tell
/* (c) https://github.com/MontiCore/monticore */
package torcs.agent;
import torcs.agent.network.TorcsActor;
component TorcsAgent {
ports
in Q^{29} state,
out Q(-1:1)^{3} action;
instance TorcsActor actor;
connect state -> actor.state;
connect actor.commands -> action;
}
/* (c) https://github.com/MontiCore/monticore */
package torcs.agent;
conforms to de.monticore.lang.monticar.generator.roscpp.RosToEmamTagSchema;
tags TorcsAgent {
tag torcsAgent.state with RosConnection = {topic=(/torcs/state, std_msgs/Float32MultiArray)};
tag torcsAgent.action with RosConnection = {topic=(/torcs/step, std_msgs/Float32MultiArray)};
}
/* (c) https://github.com/MontiCore/monticore */
package torcs.agent.network;
component Reward {
ports
in Q^{29} state,
in B isTerminal,
out Q reward;
implementation Math {
Q speedX = state(22) * 300;
Q angle = state(1) * 3.1416;
Q trackPos = state(21);
if speedX < 0
speedX = 0;
end
reward = (speedX * cos(angle)) - (speedX * sin(angle)) - (speedX * abs(trackPos));
if abs(trackPos) > 1.0
reward = -200;
end
for i=2:20
if abs(state(i)) > 1.0
reward = -200;
end
end
}
}
/* (c) https://github.com/MontiCore/monticore */
configuration TorcsActor {
context : gpu
learning_method : reinforcement
agent_name: "TorcsAgent"
rl_algorithm: td3-algorithm
policy_noise: 0.2
noise_clip: 0.5
policy_delay: 2
critic: torcs.agent.network.torcsCritic
environment : ros_interface {
state_topic: "/torcs/state"
terminal_state_topic: "/torcs/terminal"
action_topic: "/torcs/step"
reset_topic: "/torcs/reset"
}
reward_function: torcs.agent.network.reward
num_episodes : 3500
discount_factor : 0.99
num_max_steps : 900000
training_interval : 1
start_training_at: 0
evaluation_samples: 1
soft_target_update_rate: 0.005
snapshot_interval : 150
replay_memory : buffer{
memory_size : 120000
sample_size : 100
}
strategy : ornstein_uhlenbeck{
epsilon : 1.0
min_epsilon : 0.0001
epsilon_decay_method: linear
epsilon_decay : 0.000008
epsilon_decay_start: 10
epsilon_decay_per_step: true
theta: (0.6, 1.0, 1.0)
mu: (0.0, 0.0, -1.2)
sigma: (0.3, 0.2, 0.05)
}
actor_optimizer: adam {
learning_rate: 0.001
}
critic_optimizer: adam {
learning_rate: 0.001
}
}
/* (c) https://github.com/MontiCore/monticore */
package torcs.agent.network;
component TorcsActor {
ports
in Q^{29} state,
out Q(-1:1)^{3} commands;
implementation CNN {
state ->
FullyConnected(units=300) ->
Relu() ->
FullyConnected(units=600) ->
Relu() ->
FullyConnected(units=3) ->
Tanh() ->
commands;
}
}
/* (c) https://github.com/MontiCore/monticore */
configuration TorcsActor {
context : gpu
learning_method : reinforcement
agent_name: "TorcsAgent"
rl_algorithm: ddpg-algorithm
critic: torcs.agent.network.torcsCritic
environment : ros_interface {
state_topic: "/torcs/state"
terminal_state_topic: "/torcs/terminal"
action_topic: "/torcs/step"
reset_topic: "/torcs/reset"
}
reward_function: torcs.agent.network.reward
num_episodes : 3500
discount_factor : 0.99
num_max_steps : 900000
training_interval : 1
start_training_at: 0
evaluation_samples: 1
soft_target_update_rate: 0.001
snapshot_interval : 150
replay_memory : buffer{
memory_size : 120000
sample_size : 32
}
strategy : ornstein_uhlenbeck{
epsilon : 1.0
min_epsilon : 0.0001
epsilon_decay_method: linear
epsilon_decay : 0.000008
epsilon_decay_start: 10
epsilon_decay_per_step: true
theta: (0.6, 1.0, 1.0)
mu: (0.0, 0.0, -1.2)
sigma: (0.3, 0.2, 0.05)
}
actor_optimizer: adam {
learning_rate: 0.0001
}
critic_optimizer: adam {
learning_rate: 0.001
}
}
/* (c) https://github.com/MontiCore/monticore */
package torcs.agent.network;
component TorcsCritic {
ports
in Q^{29} state,
in Q(-1:1)^{3} action,
out Q(-oo:oo)^{1} qvalues;
implementation CNN {
(state | action)->
Concatenate() ->
FullyConnected(units=300) ->
Relu() ->
FullyConnected(units=600) ->
Relu() ->
FullyConnected(units=1) ->
qvalues;
}
}
/* (c) https://github.com/MontiCore/monticore */
package torcs.agent.network;
component TorcsCritic {
ports
in Q^{29} state,
in Q(-1:1)^{3} action,
out Q(-oo:oo)^{1} qvalues;
implementation CNN {
(
state ->
FullyConnected(units=300) ->
Relu() ->
FullyConnected(units=600)
|
action ->
FullyConnected(units=600)
)->
Add() ->
FullyConnected(units=600) ->
Relu() ->
FullyConnected(units=1) ->
qvalues;
}
}
package rnnencdec;
component Main{
ports in Z(0:49999)^{30} source,
out Z(0:49999)^{1} target[30];
instance Network net;
connect source -> net.source;
connect net.target[:] -> target[:];
}
\ No newline at end of file
configuration Network{
num_epoch: 10
batch_size: 64
context: cpu
eval_metric: bleu{
exclude:[0, 2, 3]
}
optimizer: adadelta{
learning_rate: 0.0001
learning_rate_decay: 0.99
step_size: 100
learning_rate_minimum: 0.000000000001
epsilon: 0.000001
rho: 0.95
}
}
package rnnencdec;
component Network{
ports in Z(0:49999)^{30} source,
out Z(0:49999)^{1} target[30];
implementation CNN{
layer GRU(units=1000) encoder;
source ->
Embedding(output_dim=620) ->
encoder;
1 -> target[0];
layer GRU(units=1000) decoder;
encoder.state -> decoder.state;
timed<t> BeamSearch(max_length=30, width=3) {
target[t-1] ->
Embedding(output_dim=620) ->
decoder ->
FullyConnected(units=50000) ->
Softmax() ->
ArgMax() ->
target[t]
};
}
}
package rnnsearch;
component Main{
ports in Z(0:49999)^{30} source,
out Z(0:49999)^{1} target[30];
instance Network net;
connect source -> net.source;
connect net.target[:] -> target[:];
}
\ No newline at end of file
configuration Network{
num_epoch: 10
batch_size: 64
context: cpu
eval_metric: bleu{
exclude:[0, 2, 3]
}
optimizer: adadelta{
learning_rate: 0.0001
learning_rate_decay: 0.99
step_size: 100
learning_rate_minimum: 0.000000000001
epsilon: 0.000001
rho: 0.95
}
}
package rnnsearch;
component Network{
ports in Z(0:49999)^{30} source,
out Z(0:49999)^{1} target[30];
implementation CNN{
layer GRU(units=1000, bidirectional=true) encoder;
layer FullyConnected(units=1000, flatten=false) fc;
source -> Embedding(output_dim=620) -> encoder -> fc;
1 -> target[0];
layer GRU(units=1000) decoder;
encoder.state -> Split(n=2) -> [1] -> decoder.state;
timed<t> BeamSearch(max_length=30, width=3) {
(
(
(
decoder.state ->
Repeat(n=30, axis=0)
|
fc.output
) ->
Concatenate(axis=1) ->
FullyConnected(units=1000, flatten=false) ->
Tanh() ->
FullyConnected(units=30) ->
Softmax() ->
ExpandDims(axis=0)
|
fc.output
) ->
Dot()
|
target[t-1] ->
Embedding(output_dim=620)
) ->
Concatenate(axis=1) ->
decoder ->
FullyConnected(units=50000) ->
Softmax() ->
ArgMax() ->
target[t]
};
}
}
package showAttendTell;
component Main{
ports in Z(0:255)^{3, 224, 224} images,
in Z(-oo:oo)^{64,2048} data,
out Z(0:25316)^{1} target[25];
instance Show_attend_tell net;
connect images -> net.images;
connect net.target[:] -> target[:];
}
configuration Show_attend_tell{
num_epoch:2
batch_size:2
context:cpu
eval_metric:bleu
loss:softmax_cross_entropy_ignore_indices{
sparse_label:true
ignore_indices:2
}
use_teacher_forcing:true
save_attention_image:true
optimizer:adam{
learning_rate:0.005
learning_rate_decay:0.9
step_size:1000
weight_decay:0.0001
}
}
package showAttendTell;
component Show_attend_tell{
ports in Z(-oo:oo)^{64,2048} data,
in Z(0:255)^{3,224,224} images,
out Z(0:37758)^{1} target[25];
implementation CNN{
layer LSTM(units=512) decoder;
layer FullyConnected(units = 256) features;
layer FullyConnected(units = 1, flatten=false) attention;
0 -> target[0];
data -> features;
timed <t> GreedySearch(max_length=25){
(
(
(
features.output ->
FullyConnected(units=512, flatten=false)
|
decoder.state[0] ->
FullyConnected(units=512, flatten=false)
) ->
BroadcastAdd() ->
Tanh() ->
FullyConnected(units=1, flatten=false) ->
Softmax(axis=0) ->
Dropout(p=0.25) ->
attention
|
features.output
)->
BroadcastMultiply() ->
ReduceSum(axis=0) ->
ExpandDims(axis=0)
|
target[t-1] ->
Embedding(output_dim=256)
) ->
Concatenate(axis=1) ->
decoder ->
FullyConnected(units=37758) ->
Tanh() ->
Dropout(p=0.25) ->
Softmax() ->
ArgMax() ->
target[t]
};
}
}
package showAttendTell;
component Show_attend_tell_images_as_input{
ports in Z(-oo:oo)^{64,2048} data,
in Z(0:255)^{3,224,224} images,
out Z(0:37758)^{1} target[25];
implementation CNN{
layer LSTM(units=512) decoder;
layer FullyConnected(units = 256) features;
layer FullyConnected(units = 1, flatten=false) attention;
0 -> target[0];
images ->
Convolution(kernel=(7,7), channels=64, stride=(7,7), padding="valid") ->
Convolution(kernel=(4,4), channels=64, stride=(4,4), padding="valid") ->
GlobalPooling(pool_type="max") ->
features;
timed <t> GreedySearch(max_length=25){
(
(
(
features.output ->
FullyConnected(units=512, flatten=false)
|
decoder.state[0] ->
FullyConnected(units=512, flatten=false)
) ->
BroadcastAdd() ->
Tanh() ->
FullyConnected(units=1, flatten=false) ->
Softmax(axis=0) ->
Dropout(p=0.25) ->
attention
|
features.output
)->
BroadcastMultiply() ->
ReduceSum(axis=0) ->
ExpandDims(axis=0)
|
target[t-1] ->
Embedding(output_dim=256)
) ->
Concatenate(axis=1) ->
decoder ->
FullyConnected(units=37758) ->
Tanh() ->
Dropout(p=0.25) ->
Softmax() ->
ArgMax() ->
target[t]
};
}
}
......@@ -72,14 +72,14 @@ class CNNCreator_cifar10_cifar10Classifier_net:
train_h5[self._output_data_names_[0]],
batch_size=batch_size,
data_name=self._input_names_[0],
label_name=self._output_names_[0])
label_name=self._output_names_[0], shuffle=True)
test_iter = None
if test_h5 != None:
test_iter = mx.io.NDArrayIter(test_h5[self._input_data_names_[0]],
test_h5[self._output_data_names_[0]],
batch_size=batch_size,
data_name=self._input_names_[0],
label_name=self._output_names_[0])
label_name=self._output_names_[0], shuffle=True)
return train_iter, test_iter, data_mean, data_std
def load_h5_files(self):
......@@ -747,6 +747,7 @@ class CNNCreator_cifar10_cifar10Classifier_net:
softmax_ = mx.symbol.SoftmaxOutput(data=softmax32_,
name="softmax_")
self.module = mx.mod.Module(symbol=mx.symbol.Group([softmax_]),
data_names=self._input_names_,
label_names=self._output_names_,
......
......@@ -3,6 +3,9 @@
#define CNNTRANSLATOR_H
#include <armadillo>
#include <cassert>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
using namespace arma;
......@@ -108,6 +111,46 @@ public:
return cubeMatrix;
}
static ivec translateToIntCol(const vector<float> &source, const vector<size_t> &shape){
assert(shape.size() == 1);
ivec column(shape[0]);
for(size_t i = 0; i < source.size(); i++){
column(i) = (size_t) source[i];
}
return column;
}
static imat translateToIntMat(const vector<float> &source, const vector<size_t> &shape){
assert(shape.size() == 2);
imat matrix(shape[1], shape[0]); //create transposed version of the matrix
int startPos = 0;
int endPos = matrix.n_rows;
const vector<size_t> columnShape = {matrix.n_rows};
for(size_t i = 0; i < matrix.n_cols; i++){
vector<float> colSource(&source[startPos], &source[endPos]);
matrix.col(i) = translateToIntCol(colSource, columnShape);
startPos = endPos;
endPos += matrix.n_rows;
}
return matrix.t();
}
static icube translateToIntCube(const vector<float> &source, const vector<size_t> &shape){
assert(shape.size() == 3);
icube cubeMatrix(shape[1], shape[2], shape[0]);
const int matrixSize = shape[1] * shape[2];
const vector<size_t> matrixShape = {shape[1], shape[2]};
int startPos = 0;
int endPos = matrixSize;
for(size_t i = 0; i < cubeMatrix.n_slices; i++){
vector<float> matrixSource(&source[startPos], &source[endPos]);
cubeMatrix.slice(i) = translateToIntMat(matrixSource, matrixShape);
startPos = endPos;
endPos += matrixSize;
}
return cubeMatrix;
}
template<typename T> static vector<size_t> getShape(const Col<T> &source){
return {source.n_elem};
}
......
import mxnet as mx
import logging
import os
from CNNNet_mnist_mnistClassifier_net import Net_0
class CNNCreator_mnist_mnistClassifier_net:
......@@ -50,7 +51,7 @@ class CNNCreator_mnist_mnistClassifier_net:
self.networks[0] = Net_0(data_mean=data_mean, data_std=data_std)
self.networks[0].collect_params().initialize(self.weight_initializer, ctx=context)
self.networks[0].hybridize()
self.networks[0](mx.nd.zeros((1,1,28,28,), ctx=context))
self.networks[0](mx.nd.zeros((1, 1,28,28,), ctx=context))
if not os.path.exists(self._model_dir_):
os.makedirs(self._model_dir_)
......
......@@ -3,6 +3,8 @@ import h5py
import mxnet as mx
import logging
import sys
import numpy as np
import cv2
from mxnet import nd
class CNNDataLoader_mnist_mnistClassifier_net:
......@@ -12,41 +14,99 @@ class CNNDataLoader_mnist_mnistClassifier_net:
def __init__(self):
self._data_dir = "data/mnist.LeNetNetwork/"
def load_data(self, batch_size):
def load_data(self, train_batch_size, test_batch_size):
train_h5, test_h5 = self.load_h5_files()
train_data = {}
data_mean = {}
data_std = {}
train_images = {}
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)
if 'images' in train_h5:
train_images = train_h5['images']
train_label = {}
index = 0
for output_name in self._output_names_:
train_label[output_name] = train_h5[output_name]
train_label[index] = train_h5[output_name]
index += 1
train_iter = mx.io.NDArrayIter(data=train_data,
label=train_label,
batch_size=batch_size)
batch_size=train_batch_size)
train_test_iter = mx.io.NDArrayIter(data=train_data,
label=train_label,
batch_size=test_batch_size)
test_iter = None
if test_h5 != None:
test_data = {}
test_images = {}
for input_name in self._input_names_:
test_data[input_name] = test_h5[input_name]
if 'images' in test_h5:
test_images = test_h5['images']
test_label = {}
index = 0
for output_name in self._output_names_:
test_label[output_name] = test_h5[output_name]
test_label[index] = test_h5[output_name]
index += 1
test_iter = mx.io.NDArrayIter(data=test_data,
label=test_label,
batch_size=test_batch_size)
return train_iter, train_test_iter, test_iter, data_mean, data_std, train_images, test_images
def load_data_img(self, batch_size, img_size):
train_h5, test_h5 = self.load_h5_files()
width = img_size[0]
height = img_size[1]
comb_data = {}
data_mean = {}
data_std = {}
for input_name in self._input_names_:
train_data = train_h5[input_name][:]
test_data = test_h5[input_name][:]
train_shape = train_data.shape
test_shape = test_data.shape
comb_data[input_name] = mx.nd.zeros((train_shape[0]+test_shape[0], train_shape[1], width, height))
for i, img in enumerate(train_data):
img = img.transpose(1,2,0)
comb_data[input_name][i] = cv2.resize(img, (width, height)).reshape((train_shape[1],width,height))
for i, img in enumerate(test_data):
img = img.transpose(1, 2, 0)
comb_data[input_name][i+train_shape[0]] = cv2.resize(img, (width, height)).reshape((train_shape[1], width, height))
data_mean[input_name + '_'] = nd.array(comb_data[input_name][:].mean(axis=0))
data_std[input_name + '_'] = nd.array(comb_data[input_name][:].asnumpy().std(axis=0) + 1e-5)
comb_label = {}
for output_name in self._output_names_:
train_labels = train_h5[output_name][:]
test_labels = test_h5[output_name][:]
comb_label[output_name] = np.append(train_labels, test_labels, axis=0)
train_iter = mx.io.NDArrayIter(data=comb_data,
label=comb_label,
batch_size=batch_size)
test_iter = None
return train_iter, test_iter, data_mean, data_std
def load_h5_files(self):
......
......@@ -2,44 +2,6 @@ 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)
def hybrid_forward(self, F, x):
return F.softmax(x)
class Split(gluon.HybridBlock):
def __init__(self, num_outputs, axis=1, **kwargs):
super(Split, self).__init__(**kwargs)