Browse Source

Run Caffe2 script on Python 2.7

Lutz Roeder 7 years ago
parent
commit
53b65b5fb1
5 changed files with 72 additions and 16 deletions
  1. 1 1
      tools/caffe-update
  2. 6 5
      tools/caffe2-update
  3. 1 1
      tools/coreml-update
  4. 2 2
      tools/onnx-update
  5. 62 7
      tools/update_pbjs.js

+ 1 - 1
tools/caffe-update

@@ -23,4 +23,4 @@ fi
 
 echo "Generate '../src/caffe.js'"
 ${node_modules}/protobufjs/bin/pbjs -t static-module -w closure -r caffe -o ${src}/caffe.js ${third_party}/${identifier}/src/caffe/proto/caffe.proto
-node ${tools}/update_pbjs_float.js ${src}/caffe.js data float 1
+node ${tools}/update_pbjs.js array ${src}/caffe.js data float 1

+ 6 - 5
tools/caffe2-update

@@ -9,12 +9,13 @@ src=${root}/src
 tools=${root}/tools
 third_party=${root}/third_party
 
-python=${python:-python}
-pip=${pip:-pip}
+python="python"
+pip="pip"
 
 identifier=pytorch
 repository=https://github.com/pytorch/pytorch.git
 
+: '
 mkdir -p ${third_party}
 
 if [ -d "${third_party}/${identifier}" ]; then
@@ -40,14 +41,14 @@ rm -rf ${PYTHONUSERBASE}
 ${pip} install --user future leveldb numpy protobuf pydot python-gflags pyyaml scikit-image setuptools six hypothesis typing
 pushd "${third_party}/pytorch" > /dev/null
 git submodule update --init
-FULL_CAFFE2=1 ${python} setup.py develop --user
+FULL_CAFFE2=1 ${python} setup.py install
 popd > /dev/null
 
 echo "Generate '../src/caffe2-metadata.json'"
 pushd ${tools} > /dev/null
 ${python} caffe2-metadata.py
 popd > /dev/null
-
+'
 echo "Generate '../src/caffe2.js'"
 ${node_modules}/protobufjs/bin/pbjs -t static-module -w closure -r caffe2 -o ${src}/caffe2.js ${third_party}/pytorch/caffe2/proto/caffe2.proto
-
+node ${tools}/update_pbjs.js enumeration ${src}/caffe2.js floats float 1

+ 1 - 1
tools/coreml-update

@@ -23,4 +23,4 @@ fi
 
 echo "Generate '../src/coreml.js'"
 ${node_modules}/protobufjs/bin/pbjs -t static-module -w closure -r coreml -o ${src}/coreml.js ${third_party}/${identifier}/mlmodel/format/Model.proto
-node ${tools}/update_pbjs_float.js ${src}/coreml.js floatValue float 2
+node ${tools}/update_pbjs.js array ${src}/coreml.js floatValue float 2

+ 2 - 2
tools/onnx-update

@@ -41,5 +41,5 @@ popd > /dev/null
 
 echo "Generate '../src/onnx.js'"
 ${node_modules}/protobufjs/bin/pbjs -t static-module -w closure -r onnx -o ${src}/onnx.js ${third_party}/${identifier}/onnx/onnx-ml.proto ${third_party}/${identifier}/onnx/onnx-operators-ml.proto
-node ${tools}/update_pbjs_float.js ${src}/onnx.js floatData float 1
-node ${tools}/update_pbjs_float.js ${src}/onnx.js doubleData double 1
+node ${tools}/update_pbjs.js array ${src}/onnx.js floatData float 1
+node ${tools}/update_pbjs.js array ${src}/onnx.js doubleData double 1

+ 62 - 7
tools/update_pbjs_float.js → tools/update_pbjs.js

@@ -3,10 +3,11 @@
 const fs = require('fs');
 const process = require('process');
 
-var file = process.argv[2];
-var variable = process.argv[3];
-var type = process.argv[4];
-var count = parseInt(process.argv[5]);
+var pattern = process.argv[2];
+var file = process.argv[3];
+var variable = process.argv[4];
+var type = process.argv[5];
+var count = parseInt(process.argv[6]);
 
 var arrayType = '';
 var dataViewMethod = '';
@@ -31,13 +32,17 @@ switch (type) {
 
 var source = fs.readFileSync(file, 'utf-8');
 
-var search = `if ((tag & 7) === 2) {
+var search = '';
+var replace = '';
+
+switch (pattern) {
+    case 'array':
+search = `if ((tag & 7) === 2) {
     var end2 = reader.uint32() + reader.pos;
     while (reader.pos < end2)
         message.$(variable).push(reader.$(type)());
 } else`;
-
-var replace = `if ((tag & 7) === 2) {
+replace = `if ((tag & 7) === 2) {
     var end2 = reader.uint32() + reader.pos;
     if (message.$(variable).length == 0 && (end2 - reader.pos) > 1048576) {
         var $(variable)Length = end2 - reader.pos;
@@ -55,6 +60,56 @@ var replace = `if ((tag & 7) === 2) {
             message.$(variable).push(reader.$(type)());
     }
 } else`;
+        break;
+
+    case 'enumeration':
+search = `if (!(message.$(variable) && message.$(variable).length))
+    message.$(variable) = [];
+if ((tag & 7) === 2) {
+    var end2 = reader.uint32() + reader.pos;
+    while (reader.pos < end2)
+        message.$(variable).push(reader.$(type)());
+} else
+    message.$(variable).push(reader.$(type)());
+break;`;
+
+replace = `if (!(message.$(variable) && message.$(variable).length)) {
+    if (message.$(variable) != -1) {
+        message.$(variable) = [];
+        message.$(variable)Count = 0;
+    }
+}
+if (message.$(variable)Count < 1000000) {
+    if ((tag & 7) === 2) {
+        var end2 = reader.uint32() + reader.pos;
+        while (reader.pos < end2) {
+            message.$(variable).push(reader.$(type)());
+            message.$(variable)Count++;
+        }
+    }
+    else {
+        message.$(variable).push(reader.$(type)());
+        message.$(variable)Count++;
+    }
+}
+else {
+    message.$(variable) = -1;
+    if ((tag & 7) === 2) {
+        var endx = reader.uint32() + reader.pos;
+        while (reader.pos < endx)
+            reader.$(type)();
+    }
+    else {
+        reader.$(type)();
+    }
+}
+break;`;
+        break;
+
+    default:
+        console.log('ERROR: Unknown pattern.')
+        process.exit(1);
+}
 
 search = search.split('$(variable)').join(variable);
 search = search.split('$(type)').join(type);