Commit 2e6dbdc1 authored by Julian Dierkes's avatar Julian Dierkes

finished tests for GANs and Preprocessing

parent 948dfb62
Pipeline #265183 failed with stages
......@@ -281,6 +281,76 @@ public class GenerationTest extends AbstractSymtabTest {
);
}
@Test
public void testGluonDefaultGANGeneration() {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/ganModel", "-r", "defaultGAN.DefaultGANConnector", "-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/ganModel/defaultGAN"),
Arrays.asList(
"gan/CNNCreator_defaultGAN_defaultGANDiscriminator.py",
"gan/CNNNet_defaultGAN_defaultGANDiscriminator.py",
"CNNCreator_defaultGAN_defaultGANConnector_predictor.py",
"CNNGanTrainer_defaultGAN_defaultGANConnector_predictor.py",
"CNNNet_defaultGAN_defaultGANConnector_predictor.py",
"CNNPredictor_defaultGAN_defaultGANConnector_predictor.h",
"CNNTrainer_defaultGAN_defaultGANConnector_predictor.py",
"defaultGAN_defaultGANConnector.cpp",
"defaultGAN_defaultGANConnector.h",
"defaultGAN_defaultGANConnector_predictor.h",
"defaultGAN_defaultGANConnector.cpp",
"defaultGAN_defaultGANConnector.h",
"defaultGAN_defaultGANConnector_predictor.h"
)
);
}
@Test
public void testGluonInfoGANGeneration() {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/ganModel", "-r", "infoGAN.InfoGANConnector", "-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/ganModel/infoGAN"),
Arrays.asList(
"gan/CNNCreator_infoGAN_infoGANDiscriminator.py",
"gan/CNNNet_infoGAN_infoGANDiscriminator.py",
"gan/CNNCreator_infoGAN_infoGANQNetwork.py",
"gan/CNNNet_infoGAN_infoGANQNetwork.py",
"CNNCreator_infoGAN_infoGANConnector_predictor.py",
"CNNDataLoader_infoGAN_infoGANConnector_predictor.py",
"CNNGanTrainer_infoGAN_infoGANConnector_predictor.py",
"CNNNet_infoGAN_infoGANConnector_predictor.py",
"CNNPredictor_infoGAN_infoGANConnector_predictor.h",
"CNNTrainer_infoGAN_infoGANConnector_predictor.py",
"infoGAN_infoGANConnector.cpp",
"infoGAN_infoGANConnector.h",
"infoGAN_infoGANConnector_predictor.h"
)
);
}
@Test
public void testGluonPreprocessingWithSupervised() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/", "-r", "PreprocessingNetwork", "-b", "GLUON", "-f", "n", "-c", "n"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().size() == 0);
}
@Test
public void testGluonPreprocessingWithGAN() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/ganModel", "-r", "defaultGANPreprocessing.GeneratorWithPreprocessing", "-b", "GLUON", "-f", "n", "-c", "n"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().size() == 0);
}
@Test
public void testAlexNetTagging() {
Log.getFindings().clear();
......
......@@ -70,6 +70,24 @@ public class IntegrationGluonTest extends IntegrationTest {
assertTrue(Log.getFindings().isEmpty());
}
@Test
public void testGluonPreprocessingWithSupervised() {
Log.getFindings().clear();
deleteHashFile(Paths.get("./target/generated-sources-emadl/PreprocessingNetwork.training_hash"));
String[] args = {"-m", "src/test/resources/models/", "-r", "PreprocessingNetwork", "-b", "GLUON"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().size() == 0);
}
@Test
public void testGluonPreprocessingWithGAN() {
Log.getFindings().clear();
deleteHashFile(Paths.get("./target/generated-sources-emadl/defaultGANPreprocessing/GeneratorWithPreprocessing.training_hash"));
String[] args = {"-m", "src/test/resources/models/ganModel", "-r", "defaultGANPreprocessing.GeneratorWithPreprocessing", "-b", "GLUON"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().size() == 0);
}
private void deleteHashFile(Path hashFile) {
try {
Files.delete(hashFile);
......
/* (c) https://github.com/MontiCore/monticore */
configuration PreprocessingNetwork{
num_epoch:1
batch_size:1
log_period: 1
normalize:false
preprocessing_name: PreprocessingProcessing
context:cpu
load_checkpoint:false
optimizer:sgd{
learning_rate:0.1
learning_rate_decay:0.85
step_size:1000
weight_decay:0.0
}
}
/* (c) https://github.com/MontiCore/monticore */
component PreprocessingNetwork {
ports in Z(0:255)^{3, 32, 32} data,
out Q(0:1)^{10} softmax;
implementation CNN {
def conv(channels, kernel=1, stride=1){
Convolution(kernel=(kernel,kernel),channels=channels) ->
Relu() ->
Pooling(pool_type="max", kernel=(2,2), stride=(stride,stride))
}
data ->
conv(kernel=5, channels=20, stride=2) ->
conv(kernel=5, channels=50, stride=2) ->
FullyConnected(units=500) ->
Relu() ->
Dropout() ->
FullyConnected(units=10) ->
Softmax() ->
softmax;
}
}
/* (c) https://github.com/MontiCore/monticore */
component PreprocessingProcessing
{
ports in Q(-oo:oo)^{3,32,32} data,
in Q(0:1) softmax_label,
out Q(-1:1)^{3,32,32} data_out,
out Q(0:1) softmax_label_out;
implementation Math
{
data = data * 2;
data_out = data - 1;
softmax_label_out = softmax_label;
}
}
/* (c) https://github.com/MontiCore/monticore */
package cNNCalculator;
component Network{
ports in Z(0:255)^{1, 28, 28} image,
out Q(0:1)^{10} predictions;
......
cifar10.CifarNetwork src/test/resources/training_data/Cifar
simpleCifar10.CifarNetwork src/test/resources/training_data/Cifar
cNNCalculator.Network src/test/resources/training_data/Cifar
PreprocessingNetwork src/test/resources/training_data/Cifar
InstanceTest.NetworkB data/InstanceTest.NetworkB
Alexnet data/Alexnet
ThreeInputCNN_M14 data/ThreeInputCNN_M14
......
defaultGANPreprocessing.GeneratorWithPreprocessing src/test/resources/training_data/Cifar
defaultGAN.DefaultGANGenerator src/test/resources/training_data/Cifar
infoGAN.InfoGANGenerator src/test/resources/training_data/Cifar
/* (c) https://github.com/MontiCore/monticore */
package defaultGAN;
component DefaultGANConnector {
ports in Q(0:1)^{100} noise,
out Q(0:1)^{1, 64, 64} res;
instance DefaultGANGenerator predictor;
connect noise -> predictor.noise;
connect predictor.data -> res;
}
/* (c) https://github.com/MontiCore/monticore */
package defaultGAN;
component DefaultGANDiscriminator{
ports in Q(-1:1)^{1, 64, 64} data,
out Q(-oo:oo)^{1} dis;
implementation CNN {
data ->
Convolution(kernel=(4,4),channels=64, stride=(2,2)) ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=128, stride=(2,2)) ->
BatchNorm() ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=256, stride=(2,2)) ->
BatchNorm() ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=512, stride=(2,2)) ->
BatchNorm() ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=1, stride=(1,1)) ->
Sigmoid() ->
dis;
}
}
/* (c) https://github.com/MontiCore/monticore */
configuration DefaultGANGenerator{
learning_method:gan
discriminator_name: defaultGAN.DefaultGANDiscriminator
num_epoch:10
batch_size:64
normalize:false
context:cpu
noise_input: "noise"
print_images: true
log_period: 10
load_checkpoint:false
optimizer:adam{
learning_rate:0.0002
beta1:0.5
}
discriminator_optimizer:adam{
learning_rate:0.0002
beta1:0.5
}
noise_distribution:gaussian{
mean_value:0
spread_value:1
}
}
/* (c) https://github.com/MontiCore/monticore */
package defaultGAN;
component DefaultGANGenerator{
ports in Q(0:1)^{100} noise,
out Q(-1:1)^{1, 64, 64} data;
implementation CNN {
noise ->
Reshape(shape=(100,1,1)) ->
UpConvolution(kernel=(4,4), channels=512, stride=(1,1), padding="valid", no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=256, stride=(2,2), no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=128, stride=(2,2), no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=64, stride=(2,2), no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=1, stride=(2,2), no_bias=true) ->
Tanh() ->
data;
}
}
/* (c) https://github.com/MontiCore/monticore */
package defaultGANPreprocessing;
component DiscriminatorWithPreprocessing{
ports in Q(-1:1)^{3, 64, 64} data,
out Q(-oo:oo)^{1} dis;
implementation CNN {
data ->
Convolution(kernel=(4,4),channels=64, stride=(2,2)) ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=128, stride=(2,2)) ->
BatchNorm() ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=256, stride=(2,2)) ->
BatchNorm() ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=512, stride=(2,2)) ->
BatchNorm() ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=1, stride=(1,1)) ->
Sigmoid() ->
dis;
}
}
/* (c) https://github.com/MontiCore/monticore */
configuration GeneratorWithPreprocessing{
learning_method:gan
discriminator_name: defaultGANPreprocessing.DiscriminatorWithPreprocessing
num_epoch:1
batch_size:1
normalize:false
preprocessing_name: defaultGANPreprocessing.ProcessingWithPreprocessing
context:cpu
noise_input: "noise"
print_images: false
log_period: 1
load_checkpoint:false
optimizer:adam{
learning_rate:0.0002
beta1:0.5
}
discriminator_optimizer:adam{
learning_rate:0.0002
beta1:0.5
}
noise_distribution:gaussian{
mean_value:0
spread_value:1
}
}
/* (c) https://github.com/MontiCore/monticore */
package defaultGANPreprocessing;
component GeneratorWithPreprocessing{
ports in Q(0:1)^{100} noise,
out Q(-1:1)^{3, 64, 64} data;
implementation CNN {
noise ->
Reshape(shape=(100,1,1)) ->
UpConvolution(kernel=(4,4), channels=512, stride=(1,1), padding="valid", no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=256, stride=(2,2), no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=128, stride=(2,2), no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=64, stride=(2,2), no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=3, stride=(2,2), no_bias=true) ->
Tanh() ->
data;
}
}
/* (c) https://github.com/MontiCore/monticore */
package defaultGANPreprocessing;
component ProcessingWithPreprocessing
{
ports in Q(-oo:oo)^{3,32,32} data,
in Q(0:1) softmax_label,
out Q(-1:1)^{3,64,64} data_out,
out Q(0:1) softmax_label_out;
implementation Math
{
data = data * 2;
data = data - 1;
data_out = scaleCube(data, 0, 64, 64);
softmax_label_out = softmax_label;
}
}
/* (c) https://github.com/MontiCore/monticore */
package infoGAN;
component InfoGANConnector {
ports in Q(0:1)^{62} noise,
in Z(0:9)^{10} c1,
out Q(0:1)^{1, 64, 64} res;
instance InfoGANGenerator predictor;
connect noise -> predictor.noise;
connect c1 -> predictor.c1;
connect predictor.data -> res;
}
/* (c) https://github.com/MontiCore/monticore */
package infoGAN;
component InfoGANDiscriminator{
ports in Q(-1:1)^{1, 28, 28} data,
out Q(-oo:oo)^{1024} features,
out Q(-oo:oo)^{1} dis;
implementation CNN {
data ->
Convolution(kernel=(4,4),channels=64, stride=(2,2)) ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=128, stride=(2,2)) ->
BatchNorm() ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=256, stride=(2,2)) ->
BatchNorm() ->
LeakyRelu(alpha=0.2) ->
Convolution(kernel=(4,4),channels=512, stride=(2,2)) ->
BatchNorm() ->
LeakyRelu(alpha=0.2) ->
(
Convolution(kernel=(4,4),channels=1,stride=(1,1)) ->
Sigmoid() ->
dis
|
features
);
}
}
/* (c) https://github.com/MontiCore/monticore */
configuration InfoGANGenerator{
learning_method:gan
discriminator_name: infoGAN.InfoGANDiscriminator
qnet_name: infoGAN.InfoGANQNetwork
num_epoch: 5
batch_size: 64
normalize: false
noise_input: "noise"
context: cpu
load_checkpoint: false
optimizer:adam{
learning_rate:0.0002
beta1:0.5
}
discriminator_optimizer:adam{
learning_rate:0.0002
beta1:0.5
}
noise_distribution:gaussian{
mean_value:0
spread_value:1
}
log_period: 10
print_images: true
}
/* (c) https://github.com/MontiCore/monticore */
package infoGAN;
component InfoGANGenerator{
ports in Q(0:1)^{62} noise,
in Z(0:9)^{10} c1,
out Q(-1:1)^{1, 64, 64} data;
implementation CNN {
(
noise
|
c1
) ->
Concatenate() ->
Reshape(shape=(72,1,1)) ->
UpConvolution(kernel=(4,4), channels=512, stride=(1,1), padding="valid", no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=256, stride=(2,2), no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=128, stride=(2,2), no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=64, stride=(2,2), no_bias=true) ->
BatchNorm() ->
Relu() ->
UpConvolution(kernel=(4,4), channels=1, stride=(2,2), no_bias=true) ->
Tanh() ->
data;
}
}
/* (c) https://github.com/MontiCore/monticore */
package infoGAN;
component InfoGANQNetwork{
ports in Q(-oo:oo)^{512, 4, 4} features,
out Q(-oo:oo)^{10} c1;
implementation CNN {
features ->
FullyConnected(units=128, no_bias=true) ->
BatchNorm() ->
Relu() ->
FullyConnected(units=10, no_bias=true) ->
Softmax() ->
c1;
}
}
import mxnet as mx
import logging
import os
from CNNNet_defaultGAN_defaultGANConnector_predictor import Net_0
class CNNCreator_defaultGAN_defaultGANConnector_predictor:
_model_dir_ = "model/defaultGAN.DefaultGANGenerator/"
_model_prefix_ = "model"
def __init__(self):
self.weight_initializer = mx.init.Normal()
self.networks = {}
def load(self, context):
earliestLastEpoch = None
for i, network in self.networks.items():
lastEpoch = 0
param_file = None
try:
os.remove(self._model_dir_ + self._model_prefix_ + "_" + str(i) + "_newest-0000.params")
except OSError:
pass
try:
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:
epochStr = file.replace(".params","").replace(self._model_prefix_ + "_" + str(i) + "-","")
epoch = int(epochStr)
if epoch > lastEpoch:
lastEpoch = epoch
param_file = file
if param_file is None:
earliestLastEpoch = 0
else:
logging.info("Loading checkpoint: " + param_file)
network.load_parameters(self._model_dir_ + param_file)
if earliestLastEpoch == None or lastEpoch < earliestLastEpoch:
earliestLastEpoch = lastEpoch
return earliestLastEpoch
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)
self.networks[0].hybridize()
self.networks[0](mx.nd.zeros((1, 100,), ctx=context))
if not os.path.exists(self._model_dir_):
os.makedirs(self._model_dir_)
for i, network in self.networks.items():
network.export(self._model_dir_ + self._model_prefix_ + "_" + str(i), epoch=0)
def getInputs(self):
inputs = {}
input_dimensions = (100,)
input_domains = (float,0.0,1.0,)
inputs["noise_"] = input_domains + (input_dimensions,)
return inputs
def getOutputs(self):
outputs = {}
output_dimensions = (1,64,64,)
output_domains = (float,-1.0,1.0,)
outputs["data_"] = output_domains + (output_dimensions,)
return outputs
import os
import h5py
import mxnet as mx
import logging
import sys
import numpy as np
import importlib
from mxnet import nd
class CNNDataLoader_defaultGAN_defaultGANConnector_predictor:
_input_names_ = ['noise']
_output_names_ = ['data_label']
def __init__(self):
self._data_dir = "data/"
def load_data(self, batch_size, shuffle=False):