Explorar el Código

Move test.js to promises

Lutz Roeder hace 6 años
padre
commit
7180705b95
Se han modificado 2 ficheros con 149 adiciones y 148 borrados
  1. 28 42
      test/models.json
  2. 121 106
      test/test.js

+ 28 - 42
test/models.json

@@ -1941,10 +1941,17 @@
     "script": [ "${root}/tools/keras", "sync install zoo" ],
     "status": "script"
   },
+  {
+    "type":   "keras",
+    "target": "keras_invalid_file.h5",
+    "source": "https://github.com/lutzroeder/netron/files/3364286/keras_invalid_file.zip[keras_invalid_file.h5]",
+    "error":  "Not a valid HDF5 file in 'keras_invalid_file.h5'.",
+    "link":   "https://github.com/lutzroeder/netron/issues/57"
+  },
   {
     "type":   "keras",
     "target": "lstm_seq2seq.h5",
-    "source": "https://github.com/lutzroeder/netron/files/2592328/lstm_seq2seq.zip[lstm_seq2seq.h5]  ",
+    "source": "https://github.com/lutzroeder/netron/files/2592328/lstm_seq2seq.zip[lstm_seq2seq.h5]",
     "format": "Keras v2.1.2",
     "link":   "https://github.com/lutzroeder/netron/issues/57"
   },
@@ -2428,10 +2435,10 @@
   },
   {
     "type":   "onnx",
-    "target": "conv_autopad.pb",
-    "source": "https://raw.githubusercontent.com/Microsoft/onnxruntime/master/onnxruntime/test/testdata/conv_autopad.pb",
+    "target": "conv_autopad.onnx",
+    "source": "https://raw.githubusercontent.com/microsoft/onnxruntime/master/onnxruntime/test/testdata/conv_autopad.onnx",
     "format": "ONNX v3",
-    "link":   "https://github.com/Microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
+    "link":   "https://github.com/microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
   },
   {
     "type":   "onnx",
@@ -2510,20 +2517,6 @@
     "source": "https://raw.githubusercontent.com/tkat0/chainer-nnvm-example/master/models/chainer-fast-neuralstyle/FastStyleNet.onnx",
     "format": "ONNX v1", "producer": "Chainer 3.2.0"
   },
-  {
-    "type":   "onnx",
-    "target": "fuse_add_1.pb",
-    "source": "https://raw.githubusercontent.com/Microsoft/onnxruntime/master/onnxruntime/test/testdata/fuse_add_1.pb",
-    "format": "ONNX v0",
-    "link":   "https://github.com/Microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
-  },
-  {
-    "type":   "onnx",
-    "target": "gru_1.pb",
-    "source": "https://raw.githubusercontent.com/Microsoft/onnxruntime/master/onnxruntime/test/testdata/gru_1.pb",
-    "format": "ONNX v0",
-    "link":   "https://github.com/Microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
-  },
   {
     "type":   "onnx",
     "target": "gzip_invalid_archive.tar.gz",
@@ -2581,15 +2574,8 @@
   },
   {
     "type":   "onnx",
-    "target": "LabelEncoder.pb",
-    "source": "https://raw.githubusercontent.com/Microsoft/onnxruntime/master/onnxruntime/test/testdata/LabelEncoder.pb",
-    "format": "ONNX v3",
-    "link":   "https://github.com/Microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
-  },
-  {
-    "type":   "onnx",
-    "target": "lstm_1.onnx",
-    "source": "https://raw.githubusercontent.com/Microsoft/onnxruntime/master/onnxruntime/test/testdata/lstm_1.onnx",
+    "target": "LabelEncoder.onnx",
+    "source": "https://raw.githubusercontent.com/microsoft/onnxruntime/master/onnxruntime/test/testdata/LabelEncoder.onnx",
     "format": "ONNX v3",
     "link":   "https://github.com/Microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
   },
@@ -2635,10 +2621,10 @@
   },
   {
     "type":   "onnx",
-    "target": "optional_1.pb",
-    "source": "https://raw.githubusercontent.com/Microsoft/onnxruntime/master/onnxruntime/test/testdata/optional_1.pb",
+    "target": "optional_1.onnx",
+    "source": "https://raw.githubusercontent.com/microsoft/onnxruntime/master/onnxruntime/test/testdata/optional_1.onnx",
     "format": "ONNX v0",
-    "link":   "https://github.com/Microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
+    "link":   "https://github.com/microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
   },
   {
     "type":   "onnx",
@@ -2725,16 +2711,16 @@
   {
     "type":   "onnx",
     "target": "test_LSTM_tanh_bidirectional.onnx",
-    "source": "https://raw.githubusercontent.com/Microsoft/onnxruntime/master/onnxruntime/test/testdata/CNTK/test_LSTM.tanh.bidirectional//model.onnx",
+    "source": "https://raw.githubusercontent.com/microsoft/onnxruntime/master/onnxruntime/test/testdata/CNTK/test_LSTM.tanh.bidirectional//model.onnx",
     "format": "ONNX v3",
-    "link":   "https://github.com/Microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
+    "link":   "https://github.com/microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
   },
   {
     "type":   "onnx",
     "target": "test_RNN_bidirectional_one_layer_relu.onnx",
-    "source": "https://raw.githubusercontent.com/Microsoft/onnxruntime/master/onnxruntime/test/testdata/CNTK/test_RNN.bidirectional.one_layer.relu//model.onnx",
+    "source": "https://raw.githubusercontent.com/microsoft/onnxruntime/master/onnxruntime/test/testdata/CNTK/test_RNN.bidirectional.one_layer.relu//model.onnx",
     "format": "ONNX v3",
-    "link":   "https://github.com/Microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
+    "link":   "https://github.com/microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
   },
   {
     "type":   "onnx",
@@ -2804,10 +2790,10 @@
   },
   {
     "type":   "onnx",
-    "target": "zipmap_int64float.pb",
-    "source": "https://raw.githubusercontent.com/Microsoft/onnxruntime/master/onnxruntime/test/testdata/zipmap_int64float.pb",
+    "target": "zipmap_int64float.onnx",
+    "source": "https://raw.githubusercontent.com/microsoft/onnxruntime/master/onnxruntime/test/testdata/zipmap_int64float.onnx",
     "format": "ONNX v3",
-    "link":   "https://github.com/Microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
+    "link":   "https://github.com/microsoft/onnxruntime/tree/master/onnxruntime/test/testdata"
   },
   {
     "type":   "openvino",
@@ -4110,15 +4096,15 @@
   },
   {
     "type":   "tfjs",
-    "target": "mobilenet_v1_0.25_224/model.json",
-    "source": "https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json",
+    "target": "mnist_transfer_cnn_v1/model.json,mnist_transfer_cnn_v1/group1-shard1of1,mnist_transfer_cnn_v1/group2-shard1of1,mnist_transfer_cnn_v1/group3-shard1of1,mnist_transfer_cnn_v1/group4-shard1of1",
+    "source": "https://github.com/lutzroeder/netron/files/3361048/mnist_transfer_cnn_v1.zip[model.json,group1-shard1of1,group2-shard1of1,group3-shard1of1,group4-shard1of1]",
     "format": "TensorFlow.js Keras v2.1.4",
     "link":   "https://github.com/tensorflow/tfjs-examples"
   },
   {
     "type":   "tfjs",
-    "target": "mnist_transfer_cnn_v1/model.json,mnist_transfer_cnn_v1/group1-shard1of1,mnist_transfer_cnn_v1/group2-shard1of1,mnist_transfer_cnn_v1/group3-shard1of1,mnist_transfer_cnn_v1/group4-shard1of1",
-    "source": "https://storage.googleapis.com/tfjs-models/tfjs/mnist_transfer_cnn_v1/model.json,https://storage.googleapis.com/tfjs-models/tfjs/mnist_transfer_cnn_v1/group1-shard1of1,https://storage.googleapis.com/tfjs-models/tfjs/mnist_transfer_cnn_v1/group2-shard1of1,https://storage.googleapis.com/tfjs-models/tfjs/mnist_transfer_cnn_v1/group3-shard1of1,https://storage.googleapis.com/tfjs-models/tfjs/mnist_transfer_cnn_v1/group3-shard1of1,https://storage.googleapis.com/tfjs-models/tfjs/mnist_transfer_cnn_v1/group4-shard1of1",
+    "target": "mobilenet_v1_0.25_224/model.json",
+    "source": "https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json",
     "format": "TensorFlow.js Keras v2.1.4",
     "link":   "https://github.com/tensorflow/tfjs-examples"
   },
@@ -4132,7 +4118,7 @@
   {
     "type":   "tfjs",
     "target": "sentiment_cnn_v1/model.json",
-    "source": "https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/model.json",
+    "source": "https://github.com/lutzroeder/netron/files/3361046/sentiment_cnn_v1.zip[model.json]",
     "format": "TensorFlow.js Keras v2.1.4",
     "link":   "https://github.com/tensorflow/tfjs-examples"
   },

+ 121 - 106
test/test.js

@@ -54,7 +54,7 @@ global.TextDecoder = class {
 
 var type = process.argv.length > 2 ? process.argv[2] : null;
 
-var models = JSON.parse(fs.readFileSync(__dirname + '/models.json', 'utf-8'));
+var items = JSON.parse(fs.readFileSync(__dirname + '/models.json', 'utf-8'));
 var dataFolder = __dirname + '/data';
 
 class TestHost {
@@ -263,73 +263,82 @@ function decompress(buffer, identifier) {
     return archive;
 }
 
-function request(location, cookie, callback) {
-    var data = [];
-    var position = 0;
-    var protocol = url.parse(location).protocol;
-    var httpModules = { 'http:': http, 'https:': https };
-    var httpModule = httpModules[protocol];
-    var httpRequest = httpModule.request(location, {
-        rejectUnauthorized: false
-    });
-    if (cookie.length > 0) {
+function request(location, cookie) {
+    var options = { rejectUnauthorized: false };
+    var httpRequest = null;
+    switch (url.parse(location).protocol) {
+        case 'http:': 
+            httpRequest = http.request(location, options);
+            break;
+        case 'https:':
+            httpRequest = https.request(location, options);
+            break;
+    }
+    if (cookie && cookie.length > 0) {
         httpRequest.setHeader('Cookie', cookie);
     }
-    httpRequest.on('response', (response) => {
-        if (response.statusCode == 200 && url.parse(location).hostname == 'drive.google.com' && 
+    return new Promise((resolve, reject) => {
+        httpRequest.on('response', (response) => {
+            resolve(response);
+        });
+        httpRequest.on('error', (error) => {
+            reject(error);
+        });
+        httpRequest.end();
+    });
+}
+
+function downloadFile(location, cookie) {
+    var data = [];
+    var position = 0;
+    return request(location, cookie).then((response) => {
+        if (response.statusCode == 200 &&
+            url.parse(location).hostname == 'drive.google.com' && 
             response.headers['set-cookie'].some((cookie) => cookie.startsWith('download_warning_'))) {
             cookie = response.headers['set-cookie'];
             var download = cookie.filter((cookie) => cookie.startsWith('download_warning_')).shift();
             var confirm = download.split(';').shift().split('=').pop();
             location = location + '&confirm=' + confirm;
-            request(location, cookie, callback);
-            return;
+            return downloadFile(location, cookie);
         }
         if (response.statusCode == 301 || response.statusCode == 302) {
             location = url.parse(response.headers.location).hostname ?
                 response.headers.location : 
                 url.parse(location).protocol + '//' + url.parse(location).hostname + response.headers.location;
-            request(location, cookie, callback);
-            return;
+            return downloadFile(location, cookie);
         }
         if (response.statusCode != 200) {
-            callback(new Error(response.statusCode.toString() + ' ' + location), null);
-            return;
-        }
-        var length = response.headers['content-length'] ? Number(response.headers['content-length']) : -1;
-        response.on("data", (chunk) => {
-            position += chunk.length;
-            if (length >= 0) {
-                var label = location.length > 70 ? location.substring(0, 66) + '...' : location; 
-                process.stdout.write('  (' + ('  ' + Math.floor(100 * (position / length))).slice(-3) + '%) ' + label + '\r');
-            }
-            else {
-                process.stdout.write('  ' + position + ' bytes\r');
-            }
-            data.push(chunk);
-        });
-        response.on("end", () => {
-            callback(null, Buffer.concat(data));
-        });
-        response.on("error", (err) => {
-            callback(err, null);
+            throw new Error(response.statusCode.toString() + ' ' + location);
+        }
+        return new Promise((resolve, reject) => {
+            var length = response.headers['content-length'] ? Number(response.headers['content-length']) : -1;
+            response.on('data', (chunk) => {
+                position += chunk.length;
+                if (length >= 0) {
+                    var label = location.length > 70 ? location.substring(0, 66) + '...' : location; 
+                    process.stdout.write('  (' + ('  ' + Math.floor(100 * (position / length))).slice(-3) + '%) ' + label + '\r');
+                }
+                else {
+                    process.stdout.write('  ' + position + ' bytes\r');
+                }
+                data.push(chunk);
+            });
+            response.on('end', () => {
+                resolve(Buffer.concat(data));
+            });
+            response.on('error', (error) => {
+                reject(error);
+            });
         });
     });
-    httpRequest.on('error', (err) => {
-        callback(err, null);
-    });
-    httpRequest.end();
 }
 
-function download(folder, targets, sources, completed, callback) {
+function download(folder, targets, sources) {
     if (targets.every((file) => fs.existsSync(folder + '/' + file))) {
-        completed = completed.concat(targets);
-        callback(null, completed);
-        return;
+        return Promise.resolve();
     }
     if (!sources) {
-        callback(new Error('Download source not specified.'), null);
-        return;
+        return Promise.reject(new Error('Download source not specified.'));
     }
     var source = '';
     var sourceFiles = [];
@@ -358,18 +367,13 @@ function download(folder, targets, sources, completed, callback) {
     for (target of targets) {
         makeDir(path.dirname(folder + '/' + target));
     }
-    request(source, [], (err, data) => {
-        if (err) {
-            callback(err, null);
-            return;
-        }
+    return downloadFile(source).then((data) => {
         if (sourceFiles.length > 0) {
             if (process.stdout.clearLine) {
                 process.stdout.clearLine();
             }
             process.stdout.write('  decompress...\r');
             var archive = decompress(data, source.split('/').pop());
-            // console.log(archive);
             for (var file of sourceFiles) {
                 if (process.stdout.clearLine) {
                     process.stdout.clearLine();
@@ -377,11 +381,10 @@ function download(folder, targets, sources, completed, callback) {
                 process.stdout.write('  write ' + file + '\n');
                 var entry = archive.entries.filter((entry) => entry.name == file)[0];
                 if (!entry) {
-                    callback(new Error("Entry not found '" + file + '. Archive contains entries: ' + JSON.stringify(archive.entries.map((entry) => entry.name)) + " ."), null);
+                    throw new Error("Entry not found '" + file + '. Archive contains entries: ' + JSON.stringify(archive.entries.map((entry) => entry.name)) + " .");
                 }
                 var target = targets.shift();
                 fs.writeFileSync(folder + '/' + target, entry.data, null);
-                completed.push(target);
             }
         }
         else {
@@ -391,16 +394,30 @@ function download(folder, targets, sources, completed, callback) {
             }
             process.stdout.write('  write ' + target + '\r');
             fs.writeFileSync(folder + '/' + target, data, null);
-            completed.push(target);
         }
         if (process.stdout.clearLine) {
             process.stdout.clearLine();
         }
         if (sources.length > 0) {
-            download(folder, targets, sources, completed, callback);
-            return;
+            return download(folder, targets, sources);
+        }
+        return;
+    });
+}
+
+function script(folder, targets, command, args) {
+    if (targets.every((file) => fs.existsSync(folder + '/' + file))) {
+        return Promise.resolve();
+    }
+    return new Promise((resolve, reject) => {
+        try {
+            console.log('  ' + command + ' ' + args);
+            child_process.execSync(command + ' ' + args, { stdio: [ 0, 1 , 2] });
+            resolve();
+        }
+        catch (error) {
+            reject(error);
         }
-        callback(null, completed);
     });
 }
 
@@ -434,6 +451,10 @@ function loadModel(target, item) {
         if (item.runtime && model.runtime != item.runtime) {
             throw new Error("Invalid runtime '" + model.runtime + "'.");
         }
+        model.version;
+        model.description;
+        model.author;
+        model.license;
         for (var graph of model.graphs) {
             var input;
             var argument;
@@ -462,6 +483,7 @@ function loadModel(target, item) {
             for (var node of graph.nodes) {
                 node.name.toString();
                 node.name.length;
+                node.description;
                 node.documentation.toString();
                 node.category.toString();
                 for (var attribute of node.attributes) {
@@ -479,11 +501,13 @@ function loadModel(target, item) {
                     for (argument of input.arguments) {
                         argument.id.toString();
                         argument.id.length;
+                        argument.description;
                         if (argument.type) {
                             argument.type.toString();
                         }
                         if (argument.initializer) {
                             argument.initializer.toString();
+                            argument.initializer.type.toString();
                         }
                     }
                 }
@@ -531,10 +555,10 @@ function render(model) {
 }
 
 function next() {
-    if (models.length == 0) {
+    if (items.length == 0) {
         return;
     }
-    var item = models.shift();
+    var item = items.shift();
     if (!item.type) {
         console.error("Property 'type' is required for item '" + JSON.stringify(item) + "'.");
         return;
@@ -543,59 +567,50 @@ function next() {
         next();
         return;
     }
-    var targets = item.target.split(',');
     if (process.stdout.clearLine) {
         process.stdout.clearLine();
     }
+    var targets = item.target.split(',');
+    var target = targets[0];
     var folder = dataFolder + '/' + item.type;
-    process.stdout.write(item.type + '/' + targets[0] + '\n');
-    var sources = item.source;
-    download(folder, targets, sources, [], (err, completed) => {
-        if (err) {
-            if (item.script) {
-                try {
-                    var root = path.dirname(__dirname);
-                    var command = item.script[0].replace('${root}', root);
-                    var args = item.script[1].replace('${root}', root);
-                    console.log('  ' + command + ' ' + args);
-                    child_process.execSync(command + ' ' + args, { stdio: [ 0, 1 , 2] });
-                    completed = targets;
-                }
-                catch (err) {
-                    console.error(err);
-                    return;
-                }
-            }
-            else {
-                console.error(err);
-                return;
-            }
-        }
-        loadModel(folder + '/' + completed[0], item).then((model) => {
-            if (item.render != 'skip') {
-                render(model).then(() => {
-                    if (item.error) {
-                        console.error('Expected error.');
-                        return;
-                    }
-                    next();
-                }).catch((error) => {
-                    if (!item.error || item.error != error.message) {
-                        console.error(err);
-                    }
-                    next();
-                });
+    process.stdout.write(item.type + '/' + target + '\n');
+
+    var promise = null;
+    if (item.script) {
+        var root = path.dirname(__dirname);
+        var command = item.script[0].replace('${root}', root);
+        var args = item.script[1].replace('${root}', root);
+        promise = script(folder, targets, command, args);
+    }
+    else {
+        var sources = item.source;
+        promise = download(folder, targets, sources);
+    }
+    return promise.then(() => {
+        return loadModel(folder + '/' + target, item).then((model) => {
+            var promise = null;
+            if (item.render == 'skip') {
+                promise = Promise.resolve();
             }
             else {
-                next();
+                promise = render(model);
             }
-        }).catch((error) => {
-            if (!item.error || item.error != error.message) {
-                console.error(error);
-                return;
-            }
-            next();
+            return promise.then(() => {
+                if (item.error) {
+                    console.error('Expected error.');
+                }
+                else {
+                    return next();
+                }
+            });
         });
+    }).catch((error) => {
+        if (!item.error || item.error != error.message) {
+            console.error(error);
+        }
+        else {
+            return next();
+        }
     });
 }