in electron/image_classifier.js [85:123]
async ensureModelLoaded(loadingCallback) {
if (this.model == null) {
console.log('Loading image classifier model...');
if (loadingCallback != null) {
loadingCallback();
}
let cachedModelJsonUrl;
if (isNode()) {
// Attempt to find and load model cached on file system if running
// in Node.js.
const fs = require('fs');
const path = require('path');
const cachedModelJsonPath = path.join(
this.getFileSystemCacheDirectory_(), 'model.json');
if (fs.existsSync(cachedModelJsonPath)) {
cachedModelJsonUrl = `file://${cachedModelJsonPath}`;
console.log(`Found cached model at ${cachedModelJsonUrl}`);
}
}
console.time('Model loading');
this.model = await tf.loadLayersModel(
cachedModelJsonUrl == null ?
MOBILENET_MODEL_URL : cachedModelJsonUrl);
console.timeEnd('Model loading');
if (isNode() && cachedModelJsonUrl == null) {
// Cache model on file system if running in Node.js.
const cacheDir = this.getFileSystemCacheDirectory_();
try {
await this.model.save(`file://${cacheDir}`);
console.log(`Cached model at ${cacheDir}`);
} catch (err) {
console.warn(`Failed to save model at cache directory: ${cacheDir}`);
}
}
}
}