Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
console.log(`Boolean flag log = ${log}`);
const taskType = 'model';
let environmentInfo: EnvironmentInfo;
if (isNodeJS) {
environmentInfo = common.getNodeEnvironmentInfo(tfn);
} else {
environmentInfo = common.getBrowserEnvironmentInfo();
}
const versionSet: VersionSet = isNodeJS ? {versions: tfn.version} : {
versions: {
'tfjs-converter': tfconverter.version_converter,
'tfjs-core': tfc.version_core,
'tfjs-data': tfd.version_data,
'tfjs-layers': tfl.version_layers
}
};
let suiteLog: common.SuiteLog;
if (isNodeJS) {
// tslint:disable-next-line:no-require-imports
const fs = require('fs');
suiteLog = JSON.parse(fs.readFileSync(BENCHMARKS_JSON_PATH, 'utf-8'));
} else {
suiteLog =
await (await fetch(BENCHMARKS_JSON_URL)).json() as common.SuiteLog;
}
const pyEnvironmentInfo = suiteLog.environmentInfo;
const pyEnvironmentId =
log ? await addEnvironmentInfoToFirestore(pyEnvironmentInfo) : null;
environmentInfo.systemInfo = pyEnvironmentInfo.systemInfo;
async function runBenchmark(artifactsDir, modelName, config) {
const modelPath = artifactsDir + modelName + '/';
console.log('Loading model "' + modelName + '" and benchmark data...');
// Note: currently we load only the topology. The weight values don't matter
// for the benchmarks and are initialized according to the initializer.
const modelJSON = await (await fetch(modelPath + 'model.json')).json();
const model = await tfl.models.modelFromJSON(modelJSON['modelTopology']);
console.log('Done loading model "' + modelName + '" and benchmark data.');
const benchmarkData = await (await fetch(modelPath + 'data.json')).json();
const lossMap = {
mean_squared_error: 'meanSquaredError',
categorical_crossentropy: 'categoricalCrossentropy',
};
// TODO(cais): Maybe TF.js Layers should tolerate these Python-style names
// for losses.
const [xs, ys] = getRandomInputsAndOutputs(model, benchmarkData.batch_size);
if (benchmarkData.train_epochs > 0) {
const optimizer =
optimizerMap[benchmarkData.optimizer] || benchmarkData.optimizer;
async function exportDepthwiseCNNModel(exportPath) {
const model = tfl.sequential();
// Cover depthwise 2D convoluational layer.
model.add(tfl.layers.depthwiseConv2d({
depthMultiplier: 2,
kernelSize: [3, 3],
strides: [2, 2],
inputShape: [40, 40, 3],
padding: 'valid',
}));
model.add(tfl.layers.batchNormalization({}));
model.add(tfl.layers.activation({activation: 'relu'}));
model.add(tfl.layers.dropout({rate: 0.5}));
model.add(tfl.layers.maxPooling2d({poolSize: 2}));
model.add(tfl.layers.flatten({}));
model.add(tfl.layers.dense({units: 100, activation: 'softmax'}));
async function exportCNNModel(exportPath) {
const model = tfl.sequential();
// Cover separable and non-separable convoluational layers.
const inputShape = [40, 40, 3];
model.add(tfl.layers.conv2d({
filters: 32,
kernelSize: [3, 3],
strides: [2, 2],
inputShape,
padding: 'valid',
}));
model.add(tfl.layers.batchNormalization({}));
model.add(tfl.layers.activation({activation: 'relu'}));
model.add(tfl.layers.dropout({rate: 0.5}));
model.add(tfl.layers.maxPooling2d({poolSize: 2}));
model.add(tfl.layers.separableConv2d({
filters: 32,
async function exportCNNModel(exportPath) {
const model = tfl.sequential();
// Cover separable and non-separable convoluational layers.
const inputShape = [40, 40, 3];
model.add(tfl.layers.conv2d({
filters: 32,
kernelSize: [3, 3],
strides: [2, 2],
inputShape,
padding: 'valid',
}));
model.add(tfl.layers.batchNormalization({}));
model.add(tfl.layers.activation({activation: 'relu'}));
model.add(tfl.layers.dropout({rate: 0.5}));
model.add(tfl.layers.maxPooling2d({poolSize: 2}));
model.add(tfl.layers.separableConv2d({
filters: 32,
async function exportMLPModel(exportPath) {
const model = tfl.sequential();
// Test both activations encapsulated in other layers and as standalone
// layers.
model.add(
tfl.layers.dense({units: 100, inputShape: [200], activation: 'relu'}));
model.add(tfl.layers.dense({units: 50, activation: 'elu'}));
model.add(tfl.layers.dense({units: 24}));
model.add(tfl.layers.activation({activation: 'elu'}));
model.add(tfl.layers.dense({units: 8, activation: 'softmax'}));
await saveModelAndRandomInputsAndOutputs(model, exportPath);
}
async function exportBidirectionalLSTMModel(exportPath) {
const model = tfl.sequential();
const inputDim = 100;
model.add(tfl.layers.embedding({inputDim, outputDim: 20, inputShape: [10]}));
// TODO(cais): Investigate why the `tfl.layers.RNN` typing doesn't work.
const lstm = tfl.layers.lstm({units: 4, goBackwards: true});
model.add(tfl.layers.bidirectional({layer: lstm, mergeMode: 'concat'}));
await saveModelAndRandomInputsAndOutputs(model, exportPath, inputDim);
}
async function exportDepthwiseCNNModel(exportPath) {
const model = tfl.sequential();
// Cover depthwise 2D convoluational layer.
model.add(tfl.layers.depthwiseConv2d({
depthMultiplier: 2,
kernelSize: [3, 3],
strides: [2, 2],
inputShape: [40, 40, 3],
padding: 'valid',
}));
model.add(tfl.layers.batchNormalization({}));
model.add(tfl.layers.activation({activation: 'relu'}));
model.add(tfl.layers.dropout({rate: 0.5}));
model.add(tfl.layers.maxPooling2d({poolSize: 2}));
model.add(tfl.layers.flatten({}));
model.add(tfl.layers.dense({units: 100, activation: 'softmax'}));
async function exportMLPModel(exportPath) {
const model = tfl.sequential();
// Test both activations encapsulated in other layers and as standalone
// layers.
model.add(
tfl.layers.dense({units: 100, inputShape: [200], activation: 'relu'}));
model.add(tfl.layers.dense({units: 50, activation: 'elu'}));
model.add(tfl.layers.dense({units: 24}));
model.add(tfl.layers.activation({activation: 'elu'}));
model.add(tfl.layers.dense({units: 8, activation: 'softmax'}));
await saveModelAndRandomInputsAndOutputs(model, exportPath);
}
async function exportMLPModel(exportPath) {
const model = tfl.sequential();
// Test both activations encapsulated in other layers and as standalone
// layers.
model.add(
tfl.layers.dense({units: 100, inputShape: [200], activation: 'relu'}));
model.add(tfl.layers.dense({units: 50, activation: 'elu'}));
model.add(tfl.layers.dense({units: 24}));
model.add(tfl.layers.activation({activation: 'elu'}));
model.add(tfl.layers.dense({units: 8, activation: 'softmax'}));
await saveModelAndRandomInputsAndOutputs(model, exportPath);
}