@tensorflow-models/coco-ssd
Version:
Object detection model (coco-ssd) in TensorFlow.js
139 lines • 5.05 kB
JavaScript
/**
* @license
* Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
Object.defineProperty(exports, "__esModule", { value: true });
var environment_1 = require("./environment");
var backend_cpu_1 = require("./kernels/backend_cpu");
var backend_webgl_1 = require("./kernels/backend_webgl");
Error.stackTraceLimit = Infinity;
// Tests whether the current environment satisfies the set of constraints.
function envSatisfiesConstraints(constraints) {
for (var key in constraints) {
var value = constraints[key];
if (environment_1.ENV.get(key) !== value) {
return false;
}
}
return true;
}
exports.envSatisfiesConstraints = envSatisfiesConstraints;
function parseKarmaFlags(args) {
var features;
var backend;
var name = '';
args.forEach(function (arg, i) {
if (arg === '--features') {
features = JSON.parse(args[i + 1]);
}
else if (arg === '--backend') {
var type = args[i + 1];
name = type;
if (type.toLowerCase() === 'cpu') {
backend = function () { return new backend_cpu_1.MathBackendCPU(); };
features = features || {};
features['HAS_WEBGL'] = false;
}
else if (type.toLowerCase() === 'webgl') {
backend = function () { return new backend_webgl_1.MathBackendWebGL(); };
}
else {
throw new Error("Unknown value " + type + " for flag --backend. " +
"Allowed values are 'cpu' or 'webgl'.");
}
}
});
if (features == null && backend == null) {
return null;
}
if (features != null && backend == null) {
throw new Error('--backend flag is required when --features is present. ' +
'Available values are "webgl" or "cpu".');
}
return { features: features || {}, factory: backend, name: name };
}
exports.parseKarmaFlags = parseKarmaFlags;
function describeWithFlags(name, constraints, tests) {
exports.TEST_ENVS.forEach(function (testEnv) {
environment_1.ENV.setFeatures(testEnv.features);
if (envSatisfiesConstraints(constraints)) {
var testName = name + ' ' + testEnv.name + ' ' + JSON.stringify(testEnv.features);
executeTests(testName, tests, testEnv);
}
});
}
exports.describeWithFlags = describeWithFlags;
exports.TEST_ENVS = [
{
name: 'webgl1',
factory: function () { return new backend_webgl_1.MathBackendWebGL(); },
features: {
'WEBGL_VERSION': 1,
'WEBGL_CPU_FORWARD': false,
'WEBGL_SIZE_UPLOAD_UNIFORM': 0
}
},
{
name: 'webgl2',
factory: function () { return new backend_webgl_1.MathBackendWebGL(); },
features: {
'WEBGL_VERSION': 2,
'WEBGL_CPU_FORWARD': false,
'WEBGL_SIZE_UPLOAD_UNIFORM': 0
}
},
{
name: 'cpu',
factory: function () { return new backend_cpu_1.MathBackendCPU(); },
features: { 'HAS_WEBGL': false }
}
];
exports.CPU_FACTORY = function () { return new backend_cpu_1.MathBackendCPU(); };
if (typeof __karma__ !== 'undefined') {
var testEnv = parseKarmaFlags(__karma__.config.args);
if (testEnv) {
setTestEnvs([testEnv]);
}
}
function setTestEnvs(testEnvs) {
exports.TEST_ENVS = testEnvs;
}
exports.setTestEnvs = setTestEnvs;
function executeTests(testName, tests, testEnv) {
describe(testName, function () {
var backendName = 'test-' + testEnv.name;
beforeAll(function () {
environment_1.ENV.reset();
environment_1.ENV.setFeatures(testEnv.features);
environment_1.ENV.set('IS_TEST', true);
environment_1.ENV.registerBackend(backendName, testEnv.factory, 1000);
environment_1.Environment.setBackend(backendName);
});
beforeEach(function () {
environment_1.ENV.engine.startScope();
});
afterEach(function () {
environment_1.ENV.engine.endScope();
environment_1.Environment.disposeVariables();
});
afterAll(function () {
environment_1.ENV.removeBackend(backendName);
environment_1.ENV.reset();
});
tests(testEnv);
});
}
//# sourceMappingURL=jasmine_util.js.map
;