Commit dd6a63da authored by Evgeny Kusmenko's avatar Evgeny Kusmenko

Merge branch 'develop' into 'master'

Implemented layer variables and RNN layer

See merge request !25
parents 3a697803 4b9440cf
Pipeline #171324 passed with stages
in 8 minutes and 13 seconds
...@@ -15,12 +15,12 @@ ...@@ -15,12 +15,12 @@
<properties> <properties>
<!-- .. SE-Libraries .................................................. --> <!-- .. SE-Libraries .................................................. -->
<emadl.version>0.2.8-SNAPSHOT</emadl.version> <emadl.version>0.2.9-SNAPSHOT</emadl.version>
<CNNTrain.version>0.3.6-SNAPSHOT</CNNTrain.version> <CNNTrain.version>0.3.6-SNAPSHOT</CNNTrain.version>
<cnnarch-generator.version>0.0.2-SNAPSHOT</cnnarch-generator.version> <cnnarch-generator.version>0.0.3-SNAPSHOT</cnnarch-generator.version>
<cnnarch-mxnet-generator.version>0.2.16-SNAPSHOT</cnnarch-mxnet-generator.version> <cnnarch-mxnet-generator.version>0.2.17-SNAPSHOT</cnnarch-mxnet-generator.version>
<cnnarch-caffe2-generator.version>0.2.12-SNAPSHOT</cnnarch-caffe2-generator.version> <cnnarch-caffe2-generator.version>0.2.13-SNAPSHOT</cnnarch-caffe2-generator.version>
<cnnarch-gluon-generator.version>0.2.6-SNAPSHOT</cnnarch-gluon-generator.version> <cnnarch-gluon-generator.version>0.2.7-SNAPSHOT</cnnarch-gluon-generator.version>
<embedded-montiarc-math-opt-generator>0.1.4</embedded-montiarc-math-opt-generator> <embedded-montiarc-math-opt-generator>0.1.4</embedded-montiarc-math-opt-generator>
<!-- .. Libraries .................................................. --> <!-- .. Libraries .................................................. -->
......
...@@ -448,7 +448,7 @@ public class EMADLGenerator { ...@@ -448,7 +448,7 @@ public class EMADLGenerator {
int i = 0; int i = 0;
for (SerialCompositeElementSymbol stream : architecture.getStreams()) { for (SerialCompositeElementSymbol stream : architecture.getStreams()) {
if (stream.isNetwork()) { if (stream.isTrainable()) {
networkAttributes += "\n" + predictorClassName + "_" + i + " _predictor_" + i + "_;"; networkAttributes += "\n" + predictorClassName + "_" + i + " _predictor_" + i + "_;";
} }
......
...@@ -198,6 +198,14 @@ public class GenerationTest extends AbstractSymtabTest { ...@@ -198,6 +198,14 @@ public class GenerationTest extends AbstractSymtabTest {
assertTrue(Log.getFindings().size() == 0); 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 @Test
public void testGluonReinforcementModelGymEnvironment() { public void testGluonReinforcementModelGymEnvironment() {
Log.getFindings().clear(); Log.getFindings().clear();
......
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
}
}
component RNNtest{
ports in Q(-oo:oo)^{50, 30001} source[2],
out Q(-oo:oo)^{50, 30001} target[2];
implementation CNN {
layer RNN(units=500, layers=2) encoder;
layer RNN(units=500, layers=2) decoder;
source[0] ->
encoder;
encoder.output ->
target[0];
encoder.state ->
decoder.state;
source[1] ->
decoder ->
target[1];
}
}
\ No newline at end of file
...@@ -11,4 +11,5 @@ mnist.LeNetNetwork data/mnist.LeNetNetwork ...@@ -11,4 +11,5 @@ mnist.LeNetNetwork data/mnist.LeNetNetwork
MultipleInputs src/test/resources/training_data/MultipleInputs MultipleInputs src/test/resources/training_data/MultipleInputs
MultipleOutputs src/test/resources/training_data/MultipleOutputs MultipleOutputs src/test/resources/training_data/MultipleOutputs
MultipleStreams src/test/resources/training_data/MultipleStreams MultipleStreams src/test/resources/training_data/MultipleStreams
Invariant src/test/resources/training_data/Invariant Invariant src/test/resources/training_data/Invariant
\ No newline at end of file RNNtest data/RNNtest
\ No newline at end of file
...@@ -21,9 +21,12 @@ class CNNCreator_cifar10_cifar10Classifier_net: ...@@ -21,9 +21,12 @@ class CNNCreator_cifar10_cifar10Classifier_net:
_data_dir_ = "src/test/resources/training_data/Cifar/" _data_dir_ = "src/test/resources/training_data/Cifar/"
_model_dir_ = "model/cifar10.CifarNetwork/" _model_dir_ = "model/cifar10.CifarNetwork/"
_model_prefix_ = "model" _model_prefix_ = "model"
_input_names_ = ['data'] _input_names_ = ['data_']
_input_shapes_ = [(3,32,32)] _input_shapes_ = [(3,32,32)]
_output_names_ = ['softmax_label'] _output_names_ = ['softmax__label']
_input_data_names_ = ['data']
_output_data_names_ = ['softmax_label']
def load(self, context): def load(self, context):
...@@ -62,18 +65,18 @@ class CNNCreator_cifar10_cifar10Classifier_net: ...@@ -62,18 +65,18 @@ class CNNCreator_cifar10_cifar10Classifier_net:
def load_data(self, batch_size): def load_data(self, batch_size):
train_h5, test_h5 = self.load_h5_files() train_h5, test_h5 = self.load_h5_files()
data_mean = train_h5[self._input_names_[0]][:].mean(axis=0) data_mean = train_h5[self._input_data_names_[0]][:].mean(axis=0)
data_std = train_h5[self._input_names_[0]][:].std(axis=0) + 1e-5 data_std = train_h5[self._input_data_names_[0]][:].std(axis=0) + 1e-5
train_iter = mx.io.NDArrayIter(train_h5[self._input_names_[0]], train_iter = mx.io.NDArrayIter(train_h5[self._input_data_names_[0]],
train_h5[self._output_names_[0]], train_h5[self._output_data_names_[0]],
batch_size=batch_size, batch_size=batch_size,
data_name=self._input_names_[0], data_name=self._input_names_[0],
label_name=self._output_names_[0]) label_name=self._output_names_[0])
test_iter = None test_iter = None
if test_h5 != None: if test_h5 != None:
test_iter = mx.io.NDArrayIter(test_h5[self._input_names_[0]], test_iter = mx.io.NDArrayIter(test_h5[self._input_data_names_[0]],
test_h5[self._output_names_[0]], test_h5[self._output_data_names_[0]],
batch_size=batch_size, batch_size=batch_size,
data_name=self._input_names_[0], data_name=self._input_names_[0],
label_name=self._output_names_[0]) label_name=self._output_names_[0])
...@@ -86,16 +89,16 @@ class CNNCreator_cifar10_cifar10Classifier_net: ...@@ -86,16 +89,16 @@ class CNNCreator_cifar10_cifar10Classifier_net:
test_path = self._data_dir_ + "test.h5" test_path = self._data_dir_ + "test.h5"
if os.path.isfile(train_path): if os.path.isfile(train_path):
train_h5 = h5py.File(train_path, 'r') train_h5 = h5py.File(train_path, 'r')
if not (self._input_names_[0] in train_h5 and self._output_names_[0] in train_h5): if not (self._input_data_names_[0] in train_h5 and self._output_data_names_[0] in train_h5):
logging.error("The HDF5 file '" + os.path.abspath(train_path) + "' has to contain the datasets: " logging.error("The HDF5 file '" + os.path.abspath(train_path) + "' has to contain the datasets: "
+ "'" + self._input_names_[0] + "', '" + self._output_names_[0] + "'") + "'" + self._input_data_names_[0] + "', '" + self._output_data_names_[0] + "'")
sys.exit(1) sys.exit(1)
test_iter = None test_iter = None
if os.path.isfile(test_path): if os.path.isfile(test_path):
test_h5 = h5py.File(test_path, 'r') test_h5 = h5py.File(test_path, 'r')
if not (self._input_names_[0] in test_h5 and self._output_names_[0] in test_h5): if not (self._input_data_names_[0] in test_h5 and self._output_data_names_[0] in test_h5):
logging.error("The HDF5 file '" + os.path.abspath(test_path) + "' has to contain the datasets: " logging.error("The HDF5 file '" + os.path.abspath(test_path) + "' has to contain the datasets: "
+ "'" + self._input_names_[0] + "', '" + self._output_names_[0] + "'") + "'" + self._input_data_names_[0] + "', '" + self._output_data_names_[0] + "'")
sys.exit(1) sys.exit(1)
else: else:
logging.warning("Couldn't load test set. File '" + os.path.abspath(test_path) + "' does not exist.") logging.warning("Couldn't load test set. File '" + os.path.abspath(test_path) + "' does not exist.")
...@@ -254,9 +257,9 @@ class CNNCreator_cifar10_cifar10Classifier_net: ...@@ -254,9 +257,9 @@ class CNNCreator_cifar10_cifar10Classifier_net:
def construct(self, context, data_mean=None, data_std=None): def construct(self, context, data_mean=None, data_std=None):
data = mx.sym.var("data", data_ = mx.sym.var("data_",
shape=(0,3,32,32)) shape=(0,3,32,32))
# data, output shape: {[3,32,32]} # data_, output shape: {[3,32,32]}
if not data_mean is None: if not data_mean is None:
assert(not data_std is None) assert(not data_std is None)
...@@ -264,9 +267,9 @@ class CNNCreator_cifar10_cifar10Classifier_net: ...@@ -264,9 +267,9 @@ class CNNCreator_cifar10_cifar10Classifier_net:
_data_mean_ = mx.sym.BlockGrad(_data_mean_) _data_mean_ = mx.sym.BlockGrad(_data_mean_)
_data_std_ = mx.sym.Variable("_data_std_", shape=(3,32,32), init=MyConstant(value=data_mean.tolist())) _data_std_ = mx.sym.Variable("_data_std_", shape=(3,32,32), init=MyConstant(value=data_mean.tolist()))
_data_std_ = mx.sym.BlockGrad(_data_std_) _data_std_ = mx.sym.BlockGrad(_data_std_)
data = mx.symbol.broadcast_sub(data, _data_mean_) data_ = mx.symbol.broadcast_sub(data_, _data_mean_)
data = mx.symbol.broadcast_div(data, _data_std_) data_ = mx.symbol.broadcast_div(data_, _data_std_)
conv2_1_ = mx.symbol.pad(data=data, conv2_1_ = mx.symbol.pad(data=data_,
mode='constant', mode='constant',
pad_width=(0,0,0,0,1,1,1,1), pad_width=(0,0,0,0,1,1,1,1),
constant_value=0) constant_value=0)
...@@ -300,7 +303,7 @@ class CNNCreator_cifar10_cifar10Classifier_net: ...@@ -300,7 +303,7 @@ class CNNCreator_cifar10_cifar10Classifier_net:
batchnorm3_1_ = mx.symbol.BatchNorm(data=conv3_1_, batchnorm3_1_ = mx.symbol.BatchNorm(data=conv3_1_,
fix_gamma=True, fix_gamma=True,
name="batchnorm3_1_") name="batchnorm3_1_")
conv2_2_ = mx.symbol.Convolution(data=data, conv2_2_ = mx.symbol.Convolution(data=data_,
kernel=(1,1), kernel=(1,1),
stride=(1,1), stride=(1,1),
num_filter=8, num_filter=8,
...@@ -741,10 +744,10 @@ class CNNCreator_cifar10_cifar10Classifier_net: ...@@ -741,10 +744,10 @@ class CNNCreator_cifar10_cifar10Classifier_net:
softmax32_ = mx.symbol.softmax(data=fc32_, softmax32_ = mx.symbol.softmax(data=fc32_,
axis=1, axis=1,
name="softmax32_") name="softmax32_")
softmax = mx.symbol.SoftmaxOutput(data=softmax32_, softmax_ = mx.symbol.SoftmaxOutput(data=softmax32_,
name="softmax") name="softmax_")
self.module = mx.mod.Module(symbol=mx.symbol.Group([softmax]), self.module = mx.mod.Module(symbol=mx.symbol.Group([softmax_]),
data_names=self._input_names_, data_names=self._input_names_,
label_names=self._output_names_, label_names=self._output_names_,
context=context) context=context)
...@@ -72,9 +72,9 @@ class CNNCreator_mnist_mnistClassifier_net: ...@@ -72,9 +72,9 @@ class CNNCreator_mnist_mnistClassifier_net:
def create_model(self, model, data, device_opts, is_test): def create_model(self, model, data, device_opts, is_test):
with core.DeviceScope(device_opts): with core.DeviceScope(device_opts):
image = data image_ = data
# image, output shape: {[1,28,28]} # image_, output shape: {[1,28,28]}
conv1_ = brew.conv(model, image, 'conv1_', dim_in=1, dim_out=20, kernel=5, stride=1) conv1_ = brew.conv(model, image_, 'conv1_', dim_in=1, dim_out=20, kernel=5, stride=1)
# conv1_, output shape: {[20,24,24]} # conv1_, output shape: {[20,24,24]}
pool1_ = brew.max_pool(model, conv1_, 'pool1_', kernel=2, stride=2) pool1_ = brew.max_pool(model, conv1_, 'pool1_', kernel=2, stride=2)
# pool1_, output shape: {[20,12,12]} # pool1_, output shape: {[20,12,12]}
...@@ -87,9 +87,9 @@ class CNNCreator_mnist_mnistClassifier_net: ...@@ -87,9 +87,9 @@ class CNNCreator_mnist_mnistClassifier_net:
relu2_ = brew.relu(model, fc2_, fc2_) relu2_ = brew.relu(model, fc2_, fc2_)
fc3_ = brew.fc(model, relu2_, 'fc3_', dim_in=500, dim_out=10) fc3_ = brew.fc(model, relu2_, 'fc3_', dim_in=500, dim_out=10)
# fc3_, output shape: {[10,1,1]} # fc3_, output shape: {[10,1,1]}
predictions = brew.softmax(model, fc3_, 'predictions') predictions_ = brew.softmax(model, fc3_, 'predictions_')
return predictions return predictions_
# this adds the loss and optimizer # this adds the loss and optimizer
def add_training_operators(self, model, output, label, device_opts, loss, opt_type, base_learning_rate, policy, stepsize, epsilon, beta1, beta2, gamma, momentum) : def add_training_operators(self, model, output, label, device_opts, loss, opt_type, base_learning_rate, policy, stepsize, epsilon, beta1, beta2, gamma, momentum) :
...@@ -150,10 +150,10 @@ class CNNCreator_mnist_mnistClassifier_net: ...@@ -150,10 +150,10 @@ class CNNCreator_mnist_mnistClassifier_net:
# == Training model == # == Training model ==
train_model= model_helper.ModelHelper(name="train_net", arg_scope=arg_scope) train_model= model_helper.ModelHelper(name="train_net", arg_scope=arg_scope)
data, label, train_dataset_size = self.add_input(train_model, batch_size=batch_size, db=os.path.join(self._data_dir_, 'train_lmdb'), db_type='lmdb', device_opts=device_opts) data, label, train_dataset_size = self.add_input(train_model, batch_size=batch_size, db=os.path.join(self._data_dir_, 'train_lmdb'), db_type='lmdb', device_opts=device_opts)
predictions = self.create_model(train_model, data, device_opts=device_opts, is_test=False) predictions_ = self.create_model(train_model, data, device_opts=device_opts, is_test=False)
self.add_training_operators(train_model, predictions, label, device_opts, loss, opt_type, base_learning_rate, policy, stepsize, epsilon, beta1, beta2, gamma, momentum) self.add_training_operators(train_model, predictions_, label, device_opts, loss, opt_type, base_learning_rate, policy, stepsize, epsilon, beta1, beta2, gamma, momentum)
if not loss == 'euclidean': if not loss == 'euclidean':
self.add_accuracy(train_model, predictions, label, device_opts, eval_metric) self.add_accuracy(train_model, predictions_, label, device_opts, eval_metric)
with core.DeviceScope(device_opts): with core.DeviceScope(device_opts):
brew.add_weight_decay(train_model, weight_decay) brew.add_weight_decay(train_model, weight_decay)
...@@ -185,9 +185,9 @@ class CNNCreator_mnist_mnistClassifier_net: ...@@ -185,9 +185,9 @@ class CNNCreator_mnist_mnistClassifier_net:
# == Testing model. == # == Testing model. ==
test_model= model_helper.ModelHelper(name="test_net", arg_scope=arg_scope, init_params=False) test_model= model_helper.ModelHelper(name="test_net", arg_scope=arg_scope, init_params=False)
data, label, test_dataset_size = self.add_input(test_model, batch_size=batch_size, db=os.path.join(self._data_dir_, 'test_lmdb'), db_type='lmdb', device_opts=device_opts) data, label, test_dataset_size = self.add_input(test_model, batch_size=batch_size, db=os.path.join(self._data_dir_, 'test_lmdb'), db_type='lmdb', device_opts=device_opts)
predictions = self.create_model(test_model, data, device_opts=device_opts, is_test=True) predictions_ = self.create_model(test_model, data, device_opts=device_opts, is_test=True)
if not loss == 'euclidean': if not loss == 'euclidean':
self.add_accuracy(test_model, predictions, label, device_opts, eval_metric) self.add_accuracy(test_model, predictions_, label, device_opts, eval_metric)
workspace.RunNetOnce(test_model.param_init_net) workspace.RunNetOnce(test_model.param_init_net)
workspace.CreateNet(test_model.net, overwrite=True) workspace.CreateNet(test_model.net, overwrite=True)
......
...@@ -13,8 +13,9 @@ class CNNPredictor_cifar10_cifar10Classifier_net_0{ ...@@ -13,8 +13,9 @@ class CNNPredictor_cifar10_cifar10Classifier_net_0{
public: public:
const std::string json_file = "model/cifar10.CifarNetwork/model_newest-symbol.json"; const std::string json_file = "model/cifar10.CifarNetwork/model_newest-symbol.json";
const std::string param_file = "model/cifar10.CifarNetwork/model_newest-0000.params"; const std::string param_file = "model/cifar10.CifarNetwork/model_newest-0000.params";
//const std::vector<std::string> input_keys = {"data"}; const std::vector<std::string> input_keys = {
const std::vector<std::string> input_keys = {"data"}; "data"
};
const std::vector<std::vector<mx_uint>> input_shapes = {{1,3,32,32}}; const std::vector<std::vector<mx_uint>> input_shapes = {{1,3,32,32}};
const bool use_gpu = false; const bool use_gpu = false;
...@@ -28,10 +29,9 @@ public: ...@@ -28,10 +29,9 @@ public:
if(handle) MXPredFree(handle); if(handle) MXPredFree(handle);
} }
void predict(const std::vector<float> &data, void predict(const std::vector<float> &data_,
std::vector<float> &softmax){ std::vector<float> &softmax_){
MXPredSetInput(handle, "data", data.data(), data.size()); MXPredSetInput(handle, input_keys[0].c_str(), data_.data(), data_.size());
//MXPredSetInput(handle, "data", data.data(), data.size());
MXPredForward(handle); MXPredForward(handle);
...@@ -44,8 +44,8 @@ public: ...@@ -44,8 +44,8 @@ public:
MXPredGetOutputShape(handle, output_index, &shape, &shape_len); MXPredGetOutputShape(handle, output_index, &shape, &shape_len);
size = 1; size = 1;
for (mx_uint i = 0; i < shape_len; ++i) size *= shape[i]; for (mx_uint i = 0; i < shape_len; ++i) size *= shape[i];
assert(size == softmax.size()); assert(size == softmax_.size());
MXPredGetOutput(handle, 0, &(softmax[0]), softmax.size()); MXPredGetOutput(handle, 0, &(softmax_[0]), softmax_.size());
} }
......
...@@ -85,9 +85,9 @@ class CNNPredictor_mnist_mnistClassifier_net_0{ ...@@ -85,9 +85,9 @@ class CNNPredictor_mnist_mnistClassifier_net_0{
input.Resize(input_shapes); input.Resize(input_shapes);
} }
void predict(const std::vector<float> &image, std::vector<float> &predictions){ void predict(const std::vector<float> &image_, std::vector<float> &predictions_){
//Note: ShareExternalPointer requires a float pointer. //Note: ShareExternalPointer requires a float pointer.
input.ShareExternalPointer((float *) image.data()); input.ShareExternalPointer((float *) image_.data());
// Get input blob // Get input blob
#ifdef USE_GPU #ifdef USE_GPU
...@@ -104,11 +104,11 @@ class CNNPredictor_mnist_mnistClassifier_net_0{ ...@@ -104,11 +104,11 @@ class CNNPredictor_mnist_mnistClassifier_net_0{
// Get output blob // Get output blob
#ifdef USE_GPU #ifdef USE_GPU
auto predictionsBlob = TensorCPU(workSpace.GetBlob("predictions")->Get<TensorCUDA>()); auto predictions_Blob = TensorCPU(workSpace.GetBlob("predictions")->Get<TensorCUDA>());
#else #else
auto predictionsBlob = workSpace.GetBlob("predictions")->Get<TensorCPU>(); auto predictions_Blob = workSpace.GetBlob("predictions")->Get<TensorCPU>();
#endif #endif
predictions.assign(predictionsBlob.data<float>(),predictionsBlob.data<float>() + predictionsBlob.size()); predictions_.assign(predictions_Blob.data<float>(),predictions_Blob.data<float>() + predictions_Blob.size());
google::protobuf::ShutdownProtobufLibrary(); google::protobuf::ShutdownProtobufLibrary();
} }
......
...@@ -19,12 +19,12 @@ data = icube(3, 32, 32); ...@@ -19,12 +19,12 @@ data = icube(3, 32, 32);
softmax=colvec(classes); softmax=colvec(classes);
} }
void execute(){ void execute(){
vector<float> CNN_softmax(10); vector<float> CNN_softmax_(10);
_predictor_0_.predict(CNNTranslator::translate(data), _predictor_0_.predict(CNNTranslator::translate(data),
CNN_softmax); CNN_softmax_);
softmax = CNNTranslator::translateToCol(CNN_softmax, std::vector<size_t> {10}); softmax = CNNTranslator::translateToCol(CNN_softmax_, std::vector<size_t> {10});
} }
......
...@@ -50,7 +50,7 @@ class CNNCreator_mnist_mnistClassifier_net: ...@@ -50,7 +50,7 @@ class CNNCreator_mnist_mnistClassifier_net:
self.networks[0] = Net_0(data_mean=data_mean, data_std=data_std) 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].collect_params().initialize(self.weight_initializer, ctx=context)
self.networks[0].hybridize() 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_): if not os.path.exists(self._model_dir_):
os.makedirs(self._model_dir_) os.makedirs(self._model_dir_)
......
...@@ -21,8 +21,8 @@ class CNNDataLoader_mnist_mnistClassifier_net: ...@@ -21,8 +21,8 @@ class CNNDataLoader_mnist_mnistClassifier_net:
for input_name in self._input_names_: for input_name in self._input_names_:
train_data[input_name] = train_h5[input_name] train_data[input_name] = train_h5[input_name]
data_mean[input_name] = nd.array(train_h5[input_name][:].mean(axis=0)) 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) data_std[input_name + '_'] = nd.array(train_h5[input_name][:].std(axis=0) + 1e-5)
train_label = {} train_label = {}
for output_name in self._output_names_: for output_name in self._output_names_:
......
...@@ -85,10 +85,10 @@ class Net_0(gluon.HybridBlock): ...@@ -85,10 +85,10 @@ class Net_0(gluon.HybridBlock):
with self.name_scope(): with self.name_scope():
if data_mean: if data_mean:
assert(data_std) assert(data_std)
self.input_normalization_image = ZScoreNormalization(data_mean=data_mean['image'], self.input_normalization_image_ = ZScoreNormalization(data_mean=data_mean['image_'],
data_std=data_std['image']) data_std=data_std['image_'])
else: else:
self.input_normalization_image = NoNormalization() self.input_normalization_image_ = NoNormalization()
self.conv1_ = gluon.nn.Conv2D(channels=20, self.conv1_ = gluon.nn.Conv2D(channels=20,
kernel_size=(5,5), kernel_size=(5,5),
...@@ -123,10 +123,9 @@ class Net_0(gluon.HybridBlock): ...@@ -123,10 +123,9 @@ class Net_0(gluon.HybridBlock):
self.softmax3_ = Softmax() self.softmax3_ = Softmax()
def hybrid_forward(self, F, image): def hybrid_forward(self, F, image_):
outputs = [] image_ = self.input_normalization_image_(image_)
image = self.input_normalization_image(image) conv1_ = self.conv1_(image_)
conv1_ = self.conv1_(image)
pool1_ = self.pool1_(conv1_) pool1_ = self.pool1_(conv1_)
conv2_ = self.conv2_(pool1_) conv2_ = self.conv2_(pool1_)
pool2_ = self.pool2_(conv2_) pool2_ = self.pool2_(conv2_)
...@@ -135,6 +134,7 @@ class Net_0(gluon.HybridBlock): ...@@ -135,6 +134,7 @@ class Net_0(gluon.HybridBlock):
relu2_ = self.relu2_(fc2_) relu2_ = self.relu2_(fc2_)
fc3_ = self.fc3_(relu2_) fc3_ = self.fc3_(relu2_)
softmax3_ = self.softmax3_(fc3_) softmax3_ = self.softmax3_(fc3_)
outputs.append(softmax3_) predictions_ = softmax3_
return predictions_
return outputs[0]
...@@ -29,9 +29,9 @@ public: ...@@ -29,9 +29,9 @@ public:
if(handle) MXPredFree(handle); if(handle) MXPredFree(handle);
} }
void predict(const std::vector<float> &image, void predict(const std::vector<float> &in_image_,
std::vector<float> &predictions){ std::vector<float> &out_predictions_){
MXPredSetInput(handle, "data", image.data(), static_cast<mx_uint>(image.size())); MXPredSetInput(handle, input_keys[0].c_str(), in_image_.data(), static_cast<mx_uint>(in_image_.size()));
MXPredForward(handle); MXPredForward(handle);
...@@ -44,8 +44,8 @@ public: ...@@ -44,8 +44,8 @@ public:
MXPredGetOutputShape(handle, output_index, &shape, &shape_len); MXPredGetOutputShape(handle, output_index, &shape, &shape_len);
size = 1; size = 1;
for (mx_uint i = 0; i < shape_len; ++i) size *= shape[i]; for (mx_uint i = 0; i < shape_len; ++i) size *= shape[i];
assert(size == predictions.size()); assert(size == out_predictions_.size());
MXPredGetOutput(handle, 0, &(predictions[0]), predictions.size()); MXPredGetOutput(handle, 0, &(out_predictions_[0]), out_predictions_.size());
} }
......
...@@ -132,14 +132,15 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net: ...@@ -132,14 +132,15 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
for epoch in range(begin_epoch, begin_epoch + num_epoch): for epoch in range(begin_epoch, begin_epoch + num_epoch):
train_iter.reset() train_iter.reset()
for batch_i, batch in enumerate(train_iter): for batch_i, batch in enumerate(train_iter):
image_data = batch.data[0].as_in_context(mx_context) image_ = batch.data[0].as_in_context(mx_context)
predictions_label = batch.label[0].as_in_context(mx_context) predictions_label = batch.label[0].as_in_context(mx_context)
with autograd.record(): with autograd.record():
predictions_output = self._networks[0](image_data)
predictions_ = self._networks[0](image_)
loss = \ loss = \
loss_function(predictions_output, predictions_label) loss_function(predictions_, predictions_label)
loss.backward() loss.backward()
...@@ -164,17 +165,18 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net: ...@@ -164,17 +165,18 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
train_iter.reset() train_iter.reset()
metric = mx.metric.create(eval_metric) metric = mx.metric.create(eval_metric)
for batch_i, batch in enumerate(train_iter): for batch_i, batch in enumerate(train_iter):
image_data = batch.data[0].as_in_context(mx_context) image_ = batch.data[0].as_in_context(mx_context)
labels = [ labels = [
batch.label[0].as_in_context(mx_context) batch.label[0].as_in_context(mx_context)
] ]
if True: # Fix indentation if True:
predictions_output = self._networks[0](image_data)
predictions_ = self._networks[0](image_)
predictions = [ predictions = [
mx.nd.argmax(predictions_output, axis=1) mx.nd.argmax(predictions_, axis=1)
] ]
metric.update(preds=predictions, labels=labels) metric.update(preds=predictions, labels=labels)
...@@ -183,17 +185,18 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net: ...@@ -183,17 +185,18 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
test_iter.reset() test_iter.reset()
metric = mx.metric.create(eval_metric) metric = mx.metric.create(eval_metric)
for batch_i, batch in enumerate(test_iter): for batch_i, batch in enumerate(test_iter):
image_data = batch.data[0].as_in_context(mx_context) image_ = batch.data[0].as_in_context(mx_context)
labels = [ labels = [
batch.label[0].as_in_context(mx_context) batch.label[0].as_in_context(mx_context)
] ]
if True: # Fix indentation if True:
predictions_output = self._networks[0](image_data)
predictions_ = self._networks[0](image_)