added JUnit testMnistClassifier, modified JUnit testCifar10Generation

parent 7139b58f
......@@ -52,6 +52,7 @@ public class GenerationTest extends AbstractSymtabTest {
Paths.get("./target/generated-sources-emadl"),
Paths.get("./src/test/resources/target_code"),
Arrays.asList(
"cifar10_cifar10Classifier.cpp",
"cifar10_cifar10Classifier.h",
"CNNCreator_cifar10_cifar10Classifier_net.py",
"CNNBufferFile.h",
......@@ -125,4 +126,25 @@ public class GenerationTest extends AbstractSymtabTest {
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
}
@Test
public void testMnistClassifier() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/models/", "-r", "mnist.MnistClassifier", "-b", "CAFFE2"};
EMADLGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
checkFilesAreEqual(
Paths.get("./target/generated-sources-emadl"),
Paths.get("./src/test/resources/target_code"),
Arrays.asList(
"mnist_mnistClassifier.cpp",
"mnist_mnistClassifier.h",
"CNNCreator_mnist_mnistClassifier_net.py",
"CNNPredictor_mnist_mnistClassifier_net.h",
"mnist_mnistClassifier_net.h",
"CNNTranslator.h",
"mnist_mnistClassifier_calculateClass.h",
"CNNTrainer_mnist_mnistClassifier_net.py"));
}
}
#ifndef CNNPREDICTOR_MNIST_MNISTCLASSIFIER_NET
#define CNNPREDICTOR_MNIST_MNISTCLASSIFIER_NET
#include "caffe2/core/common.h"
#include "caffe2/utils/proto_utils.h"
#include "caffe2/core/workspace.h"
#include "caffe2/core/tensor.h"
#include "caffe2/core/init.h"
// Define USE_GPU for GPU computation. Default is CPU computation.
//#define USE_GPU
#ifdef USE_GPU
#include "caffe2/core/context_gpu.h"
#endif
#include <string>
#include <iostream>
#include <map>
CAFFE2_DEFINE_string(init_net, "./model/mnist_mnistClassifier_net/init_net.pb", "The given path to the init protobuffer.");
CAFFE2_DEFINE_string(predict_net, "./model/mnist_mnistClassifier_net/predict_net.pb", "The given path to the predict protobuffer.");
using namespace caffe2;
class CNNPredictor_mnist_mnistClassifier_net{
private:
TensorCPU input;
Workspace workSpace;
NetDef initNet, predictNet;
public:
const std::vector<TIndex> input_shapes = {{1,1,28,28}};
explicit CNNPredictor_mnist_mnistClassifier_net(){
init(input_shapes);
}
~CNNPredictor_mnist_mnistClassifier_net(){};
void init(const std::vector<TIndex> &input_shapes){
int n = 0;
char **a[1];
caffe2::GlobalInit(&n, a);
if (!std::ifstream(FLAGS_init_net).good()) {
std::cerr << "\nNetwork loading failure, init_net file '" << FLAGS_init_net << "' does not exist." << std::endl;
exit(1);
}
if (!std::ifstream(FLAGS_predict_net).good()) {
std::cerr << "\nNetwork loading failure, predict_net file '" << FLAGS_predict_net << "' does not exist." << std::endl;
exit(1);
}
std::cout << "\nLoading network..." << std::endl;
// Read protobuf
CAFFE_ENFORCE(ReadProtoFromFile(FLAGS_init_net, &initNet));
CAFFE_ENFORCE(ReadProtoFromFile(FLAGS_predict_net, &predictNet));
// Set device type
#ifdef USE_GPU
predictNet.mutable_device_option()->set_device_type(CUDA);
initNet.mutable_device_option()->set_device_type(CUDA);
std::cout << "== GPU mode selected " << " ==" << std::endl;
#else
predictNet.mutable_device_option()->set_device_type(CPU);
initNet.mutable_device_option()->set_device_type(CPU);
for(int i = 0; i < predictNet.op_size(); ++i){
predictNet.mutable_op(i)->mutable_device_option()->set_device_type(CPU);
}
for(int i = 0; i < initNet.op_size(); ++i){
initNet.mutable_op(i)->mutable_device_option()->set_device_type(CPU);
}
std::cout << "== CPU mode selected " << " ==" << std::endl;
#endif
// Load network
CAFFE_ENFORCE(workSpace.RunNetOnce(initNet));
CAFFE_ENFORCE(workSpace.CreateNet(predictNet));
std::cout << "== Network loaded " << " ==" << std::endl;
input.Resize(input_shapes);
}
void predict(const std::vector<float> &image, std::vector<float> &predictions){
//Note: ShareExternalPointer requires a float pointer.
input.ShareExternalPointer((float *) image.data());
// Get input blob
#ifdef USE_GPU
auto dataBlob = workSpace.GetBlob("data")->GetMutable<TensorCUDA>();
#else
auto dataBlob = workSpace.GetBlob("data")->GetMutable<TensorCPU>();
#endif
// Copy from input data
dataBlob->CopyFrom(input);
// Forward
workSpace.RunNet(predictNet.name());
// Get output blob
#ifdef USE_GPU
auto predictionsBlob = TensorCPU(workSpace.GetBlob("predictions")->Get<TensorCUDA>());
#else
auto predictionsBlob = workSpace.GetBlob("predictions")->Get<TensorCPU>();
#endif
predictions.assign(predictionsBlob.data<float>(),predictionsBlob.data<float>() + predictionsBlob.size());
google::protobuf::ShutdownProtobufLibrary();
}
};
#endif // CNNPREDICTOR_MNIST_MNISTCLASSIFIER_NET
from caffe2.python import workspace, core, model_helper, brew, optimizer
from caffe2.python.predictor import mobile_exporter
from caffe2.proto import caffe2_pb2
import numpy as np
import logging
import CNNCreator_mnist_mnistClassifier_net
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
handler = logging.FileHandler("train.log", "w", encoding=None, delay="true")
logger.addHandler(handler)
mnist_mnistClassifier_net = CNNCreator_mnist_mnistClassifier_net.CNNCreator_mnist_mnistClassifier_net()
mnist_mnistClassifier_net.train(
num_epoch=11,
batch_size=64,
context='gpu',
eval_metric='accuracy',
opt_type='adam',
epsilon=1.0E-8,
weight_decay=0.001,
beta1=0.9,
beta2=0.999,
policy='fixed',
base_learning_rate=0.001
)
#include "cifar10_cifar10Classifier.h"
\ No newline at end of file
#include "mnist_mnistClassifier.h"
\ No newline at end of file
#ifndef MNIST_MNISTCLASSIFIER
#define MNIST_MNISTCLASSIFIER
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo"
#include "mnist_mnistClassifier_net.h"
#include "mnist_mnistClassifier_calculateClass.h"
using namespace arma;
class mnist_mnistClassifier{
public:
icube image;
int classIndex;
double probability;
mnist_mnistClassifier_net net;
mnist_mnistClassifier_calculateClass calculateClass;
void init()
{
image = icube(1, 28, 28);
net.init();
calculateClass.init();
}
void execute()
{
net.image = image;
net.execute();
calculateClass.inputVector = net.predictions;
calculateClass.execute();
classIndex = calculateClass.maxIndex;
probability = calculateClass.maxValue;
}
};
#endif
#ifndef MNIST_MNISTCLASSIFIER_CALCULATECLASS
#define MNIST_MNISTCLASSIFIER_CALCULATECLASS
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo"
using namespace arma;
class mnist_mnistClassifier_calculateClass{
const int n = 10;
public:
colvec inputVector;
int maxIndex;
double maxValue;
void init()
{
inputVector=colvec(n);
}
void execute()
{
maxIndex = 0;
maxValue = inputVector(1-1);
for( auto i=2;i<=n;++i){
if((inputVector(i-1) > maxValue)){
maxIndex = i-1;
maxValue = inputVector(i-1);
}
}
}
};
#endif
#ifndef MNIST_MNISTCLASSIFIER_NET
#define MNIST_MNISTCLASSIFIER_NET
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo"
#include "CNNPredictor_mnist_mnistClassifier_net.h"
#include "CNNTranslator.h"
using namespace arma;
class mnist_mnistClassifier_net{
const int classes = 10;
public:
CNNPredictor_mnist_mnistClassifier_net _cnn_;
icube image;
colvec predictions;
void init()
{
image = icube(1, 28, 28);
predictions=colvec(classes);
}
void execute(){
vector<float> CNN_predictions(10);
_cnn_.predict(CNNTranslator::translate(image),
CNN_predictions);
predictions = CNNTranslator::translateToCol(CNN_predictions, std::vector<size_t> {10});
}
};
#endif
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