...
 
Commits (6)
......@@ -8,16 +8,16 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>cnnarch-caffe2-generator</artifactId>
<version>0.2.12-SNAPSHOT</version>
<version>0.2.13-SNAPSHOT</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
<properties>
<!-- .. SE-Libraries .................................................. -->
<CNNArch.version>0.3.1-SNAPSHOT</CNNArch.version>
<CNNTrain.version>0.3.0-SNAPSHOT</CNNTrain.version>
<CNNArch2MXNet.version>0.2.16-SNAPSHOT</CNNArch2MXNet.version>
<CNNArch.version>0.3.2-SNAPSHOT</CNNArch.version>
<CNNTrain.version>0.3.8-SNAPSHOT</CNNTrain.version>
<CNNArch2X.version>0.0.3-SNAPSHOT</CNNArch2X.version>
<embedded-montiarc-math-opt-generator>0.1.4</embedded-montiarc-math-opt-generator>
<!-- .. Libraries .................................................. -->
......@@ -62,8 +62,8 @@
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>cnnarch-mxnet-generator</artifactId>
<version>${CNNArch2MXNet.version}</version>
<artifactId>cnnarch-generator</artifactId>
<version>${CNNArch2X.version}</version>
</dependency>
<dependency>
......
......@@ -20,10 +20,10 @@
*/
package de.monticore.lang.monticar.cnnarch.caffe2generator;
import de.monticore.lang.monticar.cnnarch.mxnetgenerator.CNNArch2MxNet;
import de.monticore.lang.monticar.cnnarch.mxnetgenerator.Target;
import de.monticore.lang.monticar.cnnarch.generator.CNNArchGenerator;
import de.monticore.lang.monticar.cnnarch.generator.DataPathConfigParser;
import de.monticore.lang.monticar.cnnarch.generator.Target;
import de.monticore.lang.monticar.cnnarch.DataPathConfigParser;
import de.monticore.lang.monticar.cnnarch._cocos.CNNArchCocos;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureElementSymbol;
......@@ -38,7 +38,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class CNNArch2Caffe2 extends CNNArch2MxNet {
public class CNNArch2Caffe2 extends CNNArchGenerator {
public CNNArch2Caffe2() {
architectureSupportChecker = new CNNArch2Caffe2ArchitectureSupportChecker();
......
package de.monticore.lang.monticar.cnnarch.caffe2generator;
import de.monticore.lang.monticar.cnnarch.mxnetgenerator.ArchitectureSupportChecker;
import de.monticore.lang.monticar.cnnarch.generator.ArchitectureSupportChecker;
public class CNNArch2Caffe2ArchitectureSupportChecker extends ArchitectureSupportChecker {
......
......@@ -20,8 +20,8 @@
*/
package de.monticore.lang.monticar.cnnarch.caffe2generator;
import de.monticore.lang.monticar.cnnarch.CNNArchGenerator;
import de.monticore.lang.monticar.cnnarch.mxnetgenerator.GenericCNNArchCli;
import de.monticore.lang.monticar.cnnarch.generator.CNNArchGenerator;
import de.monticore.lang.monticar.cnnarch.generator.GenericCNNArchCli;
public class CNNArch2Caffe2Cli {
public static void main(String[] args) {
......
package de.monticore.lang.monticar.cnnarch.caffe2generator;
import de.monticore.lang.monticar.cnnarch.predefined.AllPredefinedLayers;
import de.monticore.lang.monticar.cnnarch.mxnetgenerator.LayerSupportChecker;
import de.monticore.lang.monticar.cnnarch.generator.LayerSupportChecker;
public class CNNArch2Caffe2LayerSupportChecker extends LayerSupportChecker {
......
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.monticar.cnnarch.caffe2generator;
import de.monticore.lang.monticar.cnnarch.generator.TrainParamSupportChecker;
import de.monticore.lang.monticar.cnntrain._ast.*;
import de.monticore.lang.monticar.cnntrain._visitor.CNNTrainVisitor;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
import java.util.List;
public class TrainParamSupportChecker implements CNNTrainVisitor {
private List<String> unsupportedElemList = new ArrayList();
private void printUnsupportedEntryParam(String nodeName){
Log.warn("Unsupported training parameter " + "'" + nodeName + "'" + " for the backend CAFFE2. It will be ignored.");
}
private void printUnsupportedOptimizer(String nodeName){
Log.warn("Unsupported optimizer parameter " + "'" + nodeName + "'" + " for the backend CAFFE2. It will be ignored.");
}
private void printUnsupportedOptimizerParam(String nodeName){
Log.warn("Unsupported training optimizer parameter " + "'" + nodeName + "'" + " for the backend CAFFE2. It will be ignored.");
}
public TrainParamSupportChecker() {
}
public static final String unsupportedOptFlag = "unsupported_optimizer";
public List getUnsupportedElemList(){
return this.unsupportedElemList;
}
//Empty visit method denotes that the corresponding training parameter is supported.
//To set a training parameter as unsupported, add the corresponding node to the unsupportedElemList
public void visit(ASTNumEpochEntry node){}
public void visit(ASTBatchSizeEntry node){}
public class CNNArch2Caffe2TrainParamSupportChecker extends TrainParamSupportChecker {
public void visit(ASTLoadCheckpointEntry node){
printUnsupportedEntryParam(node.getName());
......@@ -47,18 +35,6 @@ public class TrainParamSupportChecker implements CNNTrainVisitor {
this.unsupportedElemList.add(node.getName());
}
public void visit(ASTTrainContextEntry node){}
public void visit(ASTEvalMetricEntry node){}
public void visit(ASTSGDOptimizer node){}
public void visit(ASTAdamOptimizer node){}
public void visit(ASTRmsPropOptimizer node){}
public void visit(ASTAdaGradOptimizer node){}
public void visit(ASTNesterovOptimizer node){
printUnsupportedOptimizer(node.getName());
this.unsupportedElemList.add(this.unsupportedOptFlag);
......@@ -69,19 +45,11 @@ public class TrainParamSupportChecker implements CNNTrainVisitor {
this.unsupportedElemList.add(this.unsupportedOptFlag);
}
public void visit(ASTLearningRateEntry node){}
public void visit(ASTMinimumLearningRateEntry node){
printUnsupportedOptimizerParam(node.getName());
this.unsupportedElemList.add(node.getName());
}
public void visit(ASTWeightDecayEntry node){}
public void visit(ASTLRDecayEntry node){}
public void visit(ASTLRPolicyEntry node){}
public void visit(ASTRescaleGradEntry node){
printUnsupportedOptimizerParam(node.getName());
this.unsupportedElemList.add(node.getName());
......@@ -92,18 +60,6 @@ public class TrainParamSupportChecker implements CNNTrainVisitor {
this.unsupportedElemList.add(node.getName());
}
public void visit(ASTStepSizeEntry node){}
public void visit(ASTMomentumEntry node){}
public void visit(ASTBeta1Entry node){}
public void visit(ASTBeta2Entry node){}
public void visit(ASTEpsilonEntry node){}
public void visit(ASTGamma1Entry node){}
public void visit(ASTGamma2Entry node){
printUnsupportedOptimizerParam(node.getName());
this.unsupportedElemList.add(node.getName());
......@@ -119,6 +75,4 @@ public class TrainParamSupportChecker implements CNNTrainVisitor {
this.unsupportedElemList.add(node.getName());
}
public void visit(ASTRhoEntry node){}
}
}
\ No newline at end of file
......@@ -20,8 +20,8 @@
*/
package de.monticore.lang.monticar.cnnarch.caffe2generator;
import de.monticore.lang.monticar.cnnarch.mxnetgenerator.LayerNameCreator;
import de.monticore.lang.monticar.cnnarch.mxnetgenerator.Target;
import de.monticore.lang.monticar.cnnarch.generator.LayerNameCreator;
import de.monticore.lang.monticar.cnnarch.generator.Target;
import de.monticore.lang.monticar.cnnarch._symboltable.*;
import de.monticore.lang.monticar.cnnarch.predefined.Sigmoid;
......@@ -119,16 +119,16 @@ public class CNNArchTemplateController {
public List<String> getArchitectureInputs(){
List<String> list = new ArrayList<>();
for (IOSymbol ioElement : getArchitecture().getInputs()){
list.add(nameManager.getName(ioElement));
for (VariableSymbol element : getArchitecture().getInputs()){
list.add(nameManager.getName(element));
}
return list;
}
public List<String> getArchitectureOutputs(){
List<String> list = new ArrayList<>();
for (IOSymbol ioElement : getArchitecture().getOutputs()){
list.add(nameManager.getName(ioElement));
for (VariableSymbol element : getArchitecture().getOutputs()){
list.add(nameManager.getName(element));
}
return list;
}
......@@ -149,18 +149,18 @@ public class CNNArchTemplateController {
TemplateConfiguration.processTemplate(ftlContext, templatePath, writer);
}
public void include(IOSymbol ioElement, Writer writer){
public void include(VariableSymbol element, Writer writer){
ArchitectureElementData previousElement = getCurrentElement();
setCurrentElement(ioElement);
setCurrentElement(element);
if (ioElement.isAtomic()){
if (ioElement.isInput()){
if (element.isAtomic()){
if (element.isInput()){
include(TEMPLATE_ELEMENTS_DIR_PATH, "Input", writer);
} else {
include(TEMPLATE_ELEMENTS_DIR_PATH, "Output", writer);
}
} else {
include(ioElement.getResolvedThis().get(), writer);
include(element.getResolvedThis().get(), writer);
}
setCurrentElement(previousElement);
......@@ -200,7 +200,7 @@ public class CNNArchTemplateController {
} else if (architectureElement instanceof LayerSymbol){
include((LayerSymbol) architectureElement, writer);
} else {
include((IOSymbol) architectureElement, writer);
include((VariableSymbol) architectureElement, writer);
}
}
......
package de.monticore.lang.monticar.cnnarch.caffe2generator;
import de.monticore.io.paths.ModelPath;
import de.monticore.lang.monticar.cnntrain.CNNTrainGenerator;
import de.monticore.lang.monticar.cnnarch.generator.CNNTrainGenerator;
import de.monticore.lang.monticar.cnntrain._ast.ASTCNNTrainNode;
import de.monticore.lang.monticar.cnntrain._ast.ASTOptimizerEntry;
import de.monticore.lang.monticar.cnntrain._cocos.CNNTrainCocos;
......@@ -18,96 +18,13 @@ import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
public class CNNTrain2Caffe2 implements CNNTrainGenerator {
private String generationTargetPath;
private String instanceName;
private void supportCheck(ConfigurationSymbol configuration){
checkEntryParams(configuration);
checkOptimizerParams(configuration);
}
private void checkEntryParams(ConfigurationSymbol configuration){
TrainParamSupportChecker funcChecker = new TrainParamSupportChecker();
Iterator it = configuration.getEntryMap().keySet().iterator();
while (it.hasNext()) {
String key = it.next().toString();
ASTCNNTrainNode astTrainEntryNode = (ASTCNNTrainNode) configuration.getEntryMap().get(key).getAstNode().get();
astTrainEntryNode.accept(funcChecker);
}
it = configuration.getEntryMap().keySet().iterator();
while (it.hasNext()) {
String key = it.next().toString();
if (funcChecker.getUnsupportedElemList().contains(key)) {
it.remove();
}
}
}
private void checkOptimizerParams(ConfigurationSymbol configuration){
TrainParamSupportChecker funcChecker = new TrainParamSupportChecker();
if (configuration.getOptimizer() != null) {
ASTOptimizerEntry astOptimizer = (ASTOptimizerEntry) configuration.getOptimizer().getAstNode().get();
astOptimizer.accept(funcChecker);
if (funcChecker.getUnsupportedElemList().contains(funcChecker.unsupportedOptFlag)) {
OptimizerSymbol adamOptimizer = new OptimizerSymbol("adam");
configuration.setOptimizer(adamOptimizer); //Set default as adam optimizer
}else {
Iterator it = configuration.getOptimizer().getOptimizerParamMap().keySet().iterator();
while (it.hasNext()) {
String key = it.next().toString();
if (funcChecker.getUnsupportedElemList().contains(key)) {
it.remove();
}
}
}
}
}
private static void quitGeneration(){
Log.error("Code generation is aborted");
System.exit(1);
}
public class CNNTrain2Caffe2 extends CNNTrainGenerator {
public CNNTrain2Caffe2() {
setGenerationTargetPath("./target/generated-sources-cnnarch/");
}
public String getInstanceName() {
String parsedInstanceName = this.instanceName.replace('.', '_').replace('[', '_').replace(']', '_');
parsedInstanceName = parsedInstanceName.substring(0, 1).toLowerCase() + parsedInstanceName.substring(1);
return parsedInstanceName;
}
public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}
public String getGenerationTargetPath() {
if (generationTargetPath.charAt(generationTargetPath.length() - 1) != '/') {
this.generationTargetPath = generationTargetPath + "/";
}
return generationTargetPath;
}
public void setGenerationTargetPath(String generationTargetPath) {
this.generationTargetPath = generationTargetPath;
}
public ConfigurationSymbol getConfigurationSymbol(Path modelsDirPath, String rootModelName) {
final ModelPath mp = new ModelPath(modelsDirPath);
GlobalScope scope = new GlobalScope(mp, new CNNTrainLanguage());
Optional<CNNTrainCompilationUnitSymbol> compilationUnit = scope.resolve(rootModelName, CNNTrainCompilationUnitSymbol.KIND);
if (!compilationUnit.isPresent()) {
Log.error("could not resolve training configuration " + rootModelName);
quitGeneration();
}
setInstanceName(compilationUnit.get().getFullName());
CNNTrainCocos.checkAll(compilationUnit.get());
supportCheck(compilationUnit.get().getConfiguration());
return compilationUnit.get().getConfiguration();
trainParamSupportChecker = new CNNArch2Caffe2TrainParamSupportChecker();
}
@Override
public void generate(Path modelsDirPath, String rootModelName) {
ConfigurationSymbol configuration = getConfigurationSymbol(modelsDirPath, rootModelName);
Map<String, String> fileContents = generateStrings(configuration);
......@@ -122,6 +39,7 @@ public class CNNTrain2Caffe2 implements CNNTrainGenerator {
}
}
@Override
public Map<String, String> generateStrings(ConfigurationSymbol configuration) {
ConfigurationData configData = new ConfigurationData(configuration, getInstanceName());
List<ConfigurationData> configDataList = new ArrayList<>();
......@@ -131,5 +49,4 @@ public class CNNTrain2Caffe2 implements CNNTrainGenerator {
String templateContent = TemplateConfiguration.processTemplate(ftlContext, "CNNTrainer.ftl");
return Collections.singletonMap("CNNTrainer_" + getInstanceName() + ".py", templateContent);
}
}
package de.monticore.lang.monticar.cnnarch.caffe2generator;
import de.monticore.lang.monticar.cnntrain._symboltable.*;
import static de.monticore.lang.monticar.cnntrain.helper.ConfigEntryNameConstants.*;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -60,11 +61,8 @@ public class ConfigurationData {
return getConfiguration().getEntry("context").getValue().toString();
}
public String getEvalMetric() {
if (!getConfiguration().getEntryMap().containsKey("eval_metric")) {
return null;
}
return getConfiguration().getEntry("eval_metric").getValue().toString();
public Map<String, Object> getEvalMetric() {
return getMultiParamEntry(EVAL_METRIC, "name");
}
public String getLoss() {
......@@ -103,4 +101,29 @@ public class ConfigurationData {
}
return mapToStrings;
}
protected Map<String, Object> getMultiParamEntry(final String key, final String valueName) {
if (!configurationContainsKey(key)) {
return null;
}
Map<String, Object> resultView = new HashMap<>();
ValueSymbol value = this.getConfiguration().getEntryMap().get(key).getValue();
if (value instanceof MultiParamValueSymbol) {
MultiParamValueSymbol multiParamValue = (MultiParamValueSymbol) value;
resultView.put(valueName, multiParamValue.getValue());
resultView.putAll(multiParamValue.getParameters());
}
else {
resultView.put(valueName, value.getValue());
}
return resultView;
}
protected Boolean configurationContainsKey(final String key) {
return this.getConfiguration().getEntryMap().containsKey(key);
}
}
......@@ -23,20 +23,20 @@ CAFFE2_DEFINE_string(predict_net_${tc.fileNameWithoutEnding}, "./model/${tc.comp
using namespace caffe2;
class ${tc.fileNameWithoutEnding}{
class ${tc.fileNameWithoutEnding}_0{
private:
TensorCPU input;
Workspace workSpace;
NetDef initNet, predictNet;
public:
const std::vector<TIndex> input_shapes = {<#list tc.architecture.inputs as input>{1,${tc.join(input.definition.type.dimensions, ",")}}<#if input?has_next>,</#if></#list>};
const std::vector<TIndex> input_shapes = {<#list tc.architecture.inputs as input>{1,${tc.join(input.ioDeclaration.type.dimensions, ",")}}<#if input?has_next>,</#if></#list>};
explicit ${tc.fileNameWithoutEnding}(){
explicit ${tc.fileNameWithoutEnding}_0(){
init(input_shapes);
}
~${tc.fileNameWithoutEnding}(){};
~${tc.fileNameWithoutEnding}_0(){};
void init(const std::vector<TIndex> &input_shapes){
int n = 0;
......@@ -105,9 +105,9 @@ class ${tc.fileNameWithoutEnding}{
// Get output blob
<#list tc.architectureOutputs as outputName>
#ifdef USE_GPU
auto ${outputName + "Blob"} = TensorCPU(workSpace.GetBlob("${outputName}")->Get<TensorCUDA>());
auto ${outputName + "Blob"} = TensorCPU(workSpace.GetBlob("${outputName?keep_before_last("_")}")->Get<TensorCUDA>());
#else
auto ${outputName + "Blob"} = workSpace.GetBlob("${outputName}")->Get<TensorCPU>();
auto ${outputName + "Blob"} = workSpace.GetBlob("${outputName?keep_before_last("_")}")->Get<TensorCPU>();
#endif
${outputName}.assign(${outputName + "Blob"}.data<float>(),${outputName + "Blob"}.data<float>() + ${outputName + "Blob"}.size());
......
......@@ -27,7 +27,7 @@ if __name__ == "__main__":
context='${config.context}',
</#if>
<#if (config.evalMetric)??>
eval_metric='${config.evalMetric}',
eval_metric='${config.evalMetric.name}',
</#if>
<#if (config.loss)??>
loss='${config.loss}',
......
<#list tc.architecture.outputs as output>
<#assign shape = output.definition.type.dimensions>
<#assign shape = output.ioDeclaration.type.dimensions>
vector<float> CNN_${tc.getName(output)}(<#list shape as dim>${dim?c}<#if dim?has_next>*</#if></#list>);
</#list>
_cnn_.predict(<#list tc.architecture.inputs as input>CNNTranslator::translate(${input.name}<#if input.arrayAccess.isPresent()>[${input.arrayAccess.get().intValue.get()?c}]</#if>),
_predictor_0_.predict(<#list tc.architecture.inputs as input>CNNTranslator::translate(${input.name}<#if input.arrayAccess.isPresent()>[${input.arrayAccess.get().intValue.get()?c}]</#if>),
</#list><#list tc.architecture.outputs as output>CNN_${tc.getName(output)}<#if output?has_next>,
</#if></#list>);
<#list tc.architecture.outputs as output>
<#assign shape = output.definition.type.dimensions>
<#assign shape = output.ioDeclaration.type.dimensions>
<#if shape?size == 1>
${output.name}<#if output.arrayAccess.isPresent()>[${output.arrayAccess.get().intValue.get()?c}]</#if> = CNNTranslator::translateToCol(CNN_${tc.getName(output)}, std::vector<size_t> {${shape[0]?c}});
</#if>
......
......@@ -67,16 +67,26 @@ public class GenerationTest extends AbstractSymtabTest{
public void testUnsupportedLayersCifar10Classifier() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/valid_tests", "-r", "CifarClassifierNetwork", "-o", "./target/generated-sources-cnnarch/"};
exit.expectSystemExit();
exit.checkAssertionAfterwards(new Assertion() {
public void checkAssertion() {
assertTrue(Log.getFindings().size() == 2);
}
});
CNNArch2Caffe2Cli.main(args);
assertTrue(Log.getFindings().size() == 1);
}
@Test
public void testUnsupportedLayersAlexnet() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/architectures", "-r", "Alexnet", "-o", "./target/generated-sources-cnnarch/"};
exit.expectSystemExit();
exit.checkAssertionAfterwards(new Assertion() {
public void checkAssertion() {
assertTrue(Log.getFindings().size() == 2);
}
});
CNNArch2Caffe2Cli.main(args);
assertTrue(Log.getFindings().size() == 1);
}
@Test
......@@ -100,24 +110,39 @@ public class GenerationTest extends AbstractSymtabTest{
public void testThreeInputCNNGeneration() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/architectures", "-r", "ThreeInputCNN_M14"};
exit.expectSystemExit();
exit.checkAssertionAfterwards(new Assertion() {
public void checkAssertion() {
assertTrue(Log.getFindings().size() == 2);
}
});
CNNArch2Caffe2Cli.main(args);
assertTrue(Log.getFindings().size() == 2);
}
@Test
public void testUnsupportedLayersResNeXt() throws IOException, TemplateException {
Log.getFindings().clear();;
String[] args = {"-m", "src/test/resources/architectures", "-r", "ResNeXt50"};
exit.expectSystemExit();
exit.checkAssertionAfterwards(new Assertion() {
public void checkAssertion() {
assertTrue(Log.getFindings().size() == 2);
}
});
CNNArch2Caffe2Cli.main(args);
assertTrue(Log.getFindings().size() == 1);
}
@Test
public void testMultipleOutputs() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/valid_tests", "-r", "MultipleOutputs"};
exit.expectSystemExit();
exit.checkAssertionAfterwards(new Assertion() {
public void checkAssertion() {
assertTrue(Log.getFindings().size() == 2);
}
});
CNNArch2Caffe2Cli.main(args);
assertTrue(Log.getFindings().size() == 4);
}
@Test
......
......@@ -72,9 +72,9 @@ class CNNCreator_LeNet:
def create_model(self, model, data, device_opts, is_test):
with core.DeviceScope(device_opts):
image = data
# image, output shape: {[1,28,28]}
conv1_ = brew.conv(model, image, 'conv1_', dim_in=1, dim_out=20, kernel=5, stride=1)
image_ = data
# image_, output shape: {[1,28,28]}
conv1_ = brew.conv(model, image_, 'conv1_', dim_in=1, dim_out=20, kernel=5, stride=1)
# conv1_, output shape: {[20,24,24]}
pool1_ = brew.max_pool(model, conv1_, 'pool1_', kernel=2, stride=2)
# pool1_, output shape: {[20,12,12]}
......@@ -87,9 +87,9 @@ class CNNCreator_LeNet:
relu2_ = brew.relu(model, fc2_, fc2_)
fc3_ = brew.fc(model, relu2_, 'fc3_', dim_in=500, dim_out=10)
# 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
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_LeNet:
# == Training model ==
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)
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)
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)
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):
brew.add_weight_decay(train_model, weight_decay)
......@@ -185,9 +185,9 @@ class CNNCreator_LeNet:
# == Testing model. ==
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)
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':
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.CreateNet(test_model.net, overwrite=True)
......
......@@ -72,9 +72,9 @@ class CNNCreator_VGG16:
def create_model(self, model, data, device_opts, is_test):
with core.DeviceScope(device_opts):
data = data
# data, output shape: {[3,224,224]}
conv1_ = brew.conv(model, data, 'conv1_', dim_in=3, dim_out=64, kernel=3, stride=1, pad=1)
data_ = data
# data_, output shape: {[3,224,224]}
conv1_ = brew.conv(model, data_, 'conv1_', dim_in=3, dim_out=64, kernel=3, stride=1, pad=1)
# conv1_, output shape: {[64,224,224]}
relu1_ = brew.relu(model, conv1_, conv1_)
conv2_ = brew.conv(model, relu1_, 'conv2_', dim_in=64, dim_out=64, kernel=3, stride=1, pad=1)
......@@ -133,9 +133,9 @@ class CNNCreator_VGG16:
dropout15_ = brew.dropout(model, relu15_, 'dropout15_', ratio=0.5, is_test=False)
fc15_ = brew.fc(model, dropout15_, 'fc15_', dim_in=4096, dim_out=1000)
# fc15_, output shape: {[1000,1,1]}
predictions = brew.softmax(model, fc15_, 'predictions')
predictions_ = brew.softmax(model, fc15_, 'predictions_')
return predictions
return predictions_
# 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) :
......@@ -196,10 +196,10 @@ class CNNCreator_VGG16:
# == Training model ==
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)
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)
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)
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):
brew.add_weight_decay(train_model, weight_decay)
......@@ -231,9 +231,9 @@ class CNNCreator_VGG16:
# == Testing model. ==
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)
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':
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.CreateNet(test_model.net, overwrite=True)
......@@ -314,4 +314,4 @@ class CNNCreator_VGG16:
net_def.ParseFromString(f.read())
net_def.device_option.CopyFrom(device_opts)
workspace.CreateNet(net_def.SerializeToString(), overwrite=True)
print("*** Loaded init_net and predict_net ***")
\ No newline at end of file
print("*** Loaded init_net and predict_net ***")
......@@ -23,7 +23,7 @@ CAFFE2_DEFINE_string(predict_net, "./model/Alexnet/predict_net.pb", "The given p
using namespace caffe2;
class CNNPredictor_Alexnet{
class CNNPredictor_Alexnet_0{
private:
TensorCPU input;
Workspace workSpace;
......@@ -32,11 +32,11 @@ class CNNPredictor_Alexnet{
public:
const std::vector<TIndex> input_shapes = {{1,3,224,224}};
explicit CNNPredictor_Alexnet(){
explicit CNNPredictor_Alexnet_0(){
init(input_shapes);
}
~CNNPredictor_Alexnet(){};
~CNNPredictor_Alexnet_0(){};
void init(const std::vector<TIndex> &input_shapes){
int n = 0;
......
......@@ -23,7 +23,7 @@ CAFFE2_DEFINE_string(predict_net, "./model/CifarClassifierNetwork/predict_net.pb
using namespace caffe2;
class CNNPredictor_CifarClassifierNetwork{
class CNNPredictor_CifarClassifierNetwork_0{
private:
TensorCPU input;
Workspace workSpace;
......@@ -32,11 +32,11 @@ class CNNPredictor_CifarClassifierNetwork{
public:
const std::vector<TIndex> input_shapes = {{1,3,32,32}};
explicit CNNPredictor_CifarClassifierNetwork(){
explicit CNNPredictor_CifarClassifierNetwork_0(){
init(input_shapes);
}
~CNNPredictor_CifarClassifierNetwork(){};
~CNNPredictor_CifarClassifierNetwork_0(){};
void init(const std::vector<TIndex> &input_shapes){
int n = 0;
......
......@@ -23,7 +23,7 @@ CAFFE2_DEFINE_string(predict_net_CNNPredictor_LeNet, "./model/LeNet/predict_net.
using namespace caffe2;
class CNNPredictor_LeNet{
class CNNPredictor_LeNet_0{
private:
TensorCPU input;
Workspace workSpace;
......@@ -32,11 +32,11 @@ class CNNPredictor_LeNet{
public:
const std::vector<TIndex> input_shapes = {{1,1,28,28}};
explicit CNNPredictor_LeNet(){
explicit CNNPredictor_LeNet_0(){
init(input_shapes);
}
~CNNPredictor_LeNet(){};
~CNNPredictor_LeNet_0(){};
void init(const std::vector<TIndex> &input_shapes){
int n = 0;
......@@ -85,9 +85,9 @@ class CNNPredictor_LeNet{
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.
input.ShareExternalPointer((float *) image.data());
input.ShareExternalPointer((float *) image_.data());
// Get input blob
#ifdef USE_GPU
......@@ -104,11 +104,11 @@ class CNNPredictor_LeNet{
// Get output blob
#ifdef USE_GPU
auto predictionsBlob = TensorCPU(workSpace.GetBlob("predictions")->Get<TensorCUDA>());
auto predictions_Blob = TensorCPU(workSpace.GetBlob("predictions")->Get<TensorCUDA>());
#else
auto predictionsBlob = workSpace.GetBlob("predictions")->Get<TensorCPU>();
auto predictions_Blob = workSpace.GetBlob("predictions")->Get<TensorCPU>();
#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();
}
......
......@@ -23,7 +23,7 @@ CAFFE2_DEFINE_string(predict_net_CNNPredictor_VGG16, "./model/VGG16/predict_net.
using namespace caffe2;
class CNNPredictor_VGG16{
class CNNPredictor_VGG16_0{
private:
TensorCPU input;
Workspace workSpace;
......@@ -32,11 +32,11 @@ class CNNPredictor_VGG16{
public:
const std::vector<TIndex> input_shapes = {{1,3,224,224}};
explicit CNNPredictor_VGG16(){
explicit CNNPredictor_VGG16_0(){
init(input_shapes);
}
~CNNPredictor_VGG16(){};
~CNNPredictor_VGG16_0(){};
void init(const std::vector<TIndex> &input_shapes){
int n = 0;
......@@ -85,9 +85,9 @@ class CNNPredictor_VGG16{
input.Resize(input_shapes);
}
void predict(const std::vector<float> &data, std::vector<float> &predictions){
void predict(const std::vector<float> &data_, std::vector<float> &predictions_){
//Note: ShareExternalPointer requires a float pointer.
input.ShareExternalPointer((float *) data.data());
input.ShareExternalPointer((float *) data_.data());
// Get input blob
#ifdef USE_GPU
......@@ -104,11 +104,11 @@ class CNNPredictor_VGG16{
// Get output blob
#ifdef USE_GPU
auto predictionsBlob = TensorCPU(workSpace.GetBlob("predictions")->Get<TensorCUDA>());
auto predictions_Blob = TensorCPU(workSpace.GetBlob("predictions")->Get<TensorCUDA>());
#else
auto predictionsBlob = workSpace.GetBlob("predictions")->Get<TensorCPU>();
auto predictions_Blob = workSpace.GetBlob("predictions")->Get<TensorCPU>();
#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();
}
......
......@@ -18,7 +18,6 @@ if __name__ == "__main__":
batch_size=100,
context='gpu',
eval_metric='mse',
loss='cross_entropy',
opt_type='rmsprop',
epsilon=1.0E-6,
weight_decay=0.01,
......
......@@ -17,5 +17,5 @@ if __name__ == "__main__":
num_epoch=5,
batch_size=100,
context='gpu',
opt_type='adam'
)
vector<float> CNN_predictions(10);
_cnn_.predict(CNNTranslator::translate(data),
_predictor_0_.predict(CNNTranslator::translate(data),
CNN_predictions);
predictions = CNNTranslator::translateToCol(CNN_predictions, std::vector<size_t> {10});
\ No newline at end of file
vector<float> CNN_softmax(10);
_cnn_.predict(CNNTranslator::translate(data),
_predictor_0_.predict(CNNTranslator::translate(data),
CNN_softmax);
softmax = CNNTranslator::translateToCol(CNN_softmax, std::vector<size_t> {10});
\ No newline at end of file
vector<float> CNN_predictions(10);
vector<float> CNN_predictions_(10);
_cnn_.predict(CNNTranslator::translate(image),
CNN_predictions);
_predictor_0_.predict(CNNTranslator::translate(image),
CNN_predictions_);
predictions = CNNTranslator::translateToCol(CNN_predictions, std::vector<size_t> {10});
predictions = CNNTranslator::translateToCol(CNN_predictions_, std::vector<size_t> {10});
vector<float> CNN_predictions(1000);
vector<float> CNN_predictions_(1000);
_cnn_.predict(CNNTranslator::translate(data),
CNN_predictions);
_predictor_0_.predict(CNNTranslator::translate(data),
CNN_predictions_);
predictions = CNNTranslator::translateToCol(CNN_predictions, std::vector<size_t> {1000});
\ No newline at end of file
predictions = CNNTranslator::translateToCol(CNN_predictions_, std::vector<size_t> {1000});