const benchmarks = {
'mobilenet_v2': {
type: 'GraphModel',
load: async () => {
const url =
'https://storage.googleapis.com/learnjs-data/mobilenet_v2_100_fused/model.json';
return tf.loadGraphModel(url);
},
predictFunc: () => {
const input = tf.randomNormal([1, 224, 224, 3]);
return model => model.predict(input);
}
},
'blazeface': {
type: 'GraphModel',
supportedInput: [128],
load: async () => {
const url =
'https://tfhub.dev/tensorflow/tfjs-model/blazeface/1/default/1';
return tf.loadGraphModel(url, {fromTFHub: true});
},
predictFunc: (inputResolution = 128) => {
const input = tf.randomNormal([1, inputResolution, inputResolution, 3]);
return model => {
return model.predict(input);
};
},
},
}
function isInputSizeSupported(model) {
const benchmark = benchmarks[model];
if (typeof benchmark['supportedInput'] !== 'undefined') {
return true;
}
// Model doesn't support input size.
return false;
}
function isInputSizeSupportedNull(model) {
const benchmark = benchmarks[model];
if (benchmark['supportedInput'] != null) {
return true;
}
// Model doesn't support input size.
return false;
}
function isInputSizeSupportedNull2(model) {
const benchmark = benchmarks[model];
if (benchmark['supportedInput'] !== null) {
return true;
}
// Model doesn't support input size.
return false;
}
function isInputSizeSupportedTypeofNull(model) {
const benchmark = benchmarks[model];
if (typeof benchmark['supportedInput'] != null) {
return true;
}
// Model doesn't support input size.
return false;
}
console.log("use undefined="+ isInputSizeSupported('mobilenet_v2'));
console.log("use undefined="+ isInputSizeSupported('blazeface'));
console.log("use null="+ isInputSizeSupportedNull('mobilenet_v2'));
console.log("use null="+ isInputSizeSupportedNull('blazeface'));
console.log("use null=="+ isInputSizeSupportedNull2('mobilenet_v2'));
console.log("use null=="+ isInputSizeSupportedNull2('blazeface'));
console.log("use typeof null="+ isInputSizeSupportedTypeofNull('mobilenet_v2'));
console.log("use typeof null="+ isInputSizeSupportedTypeofNull('blazeface'));
答案:
use undefined=false
use undefined=true
use null=false
use null=true
use null==true
use null==true
use typeof null=true
use typeof null=true
interface ModelConfig {
architecture: string;
inputResolution: number;
modelUrl?: string;
}
// The default configuration for loading MobileNetV1 based PoseNet.
//
// (And for references, the default configuration for loading ResNet
// based PoseNet is also included).
//
// ```
// const RESNET_CONFIG = {
// architecture: 'ResNet50',
// outputStride: 32,
// quantBytes: 2,
// } as ModelConfig;
// ```
const MOBILENET_V1_CONFIG: ModelConfig = {
architecture: 'MobileNetV1',
inputResolution: 257,
} as ModelConfig;
let MOBILENET_V2_CONFIG: ModelConfig = {
inputResolution: 157,
} as ModelConfig;
function validateModelConfig(config: ModelConfig) {
config = config || MOBILENET_V1_CONFIG;
return config;
}
function validateModelConfig2(config: ModelConfig) {
config = {...MOBILENET_V1_CONFIG, ...config};
return config;
}
console.log(validateModelConfig(MOBILENET_V2_CONFIG));
console.log(validateModelConfig2(MOBILENET_V2_CONFIG));
答案是:
[LOG]: {
"inputResolution": 157
}
[LOG]: {
"architecture": "MobileNetV1",
"inputResolution": 157
}