This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function loadModel() { | |
google.script.run.withSuccessHandler(async modelFiles => { | |
const modelJson = new File([modelFiles[0]], "model.json", { type: "application/json" }) | |
const modelWeights = new File([Uint8Array.from(modelFiles[1])], "group1-shard1of1.bin") | |
model = await tf.loadLayersModel(tf.io.browserFiles([modelJson, modelWeights])) | |
model.summary(); | |
}).withFailureHandler(onFailure).loadModelFromDrive(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadModelFromDrive() { | |
const f = DriveApp.getFileById("1iO0Lz3022AHgTOR4Xd_WLuR1FVXNUIdU") | |
const modeljson = f.getAs("application/json").getDataAsString(); | |
const f2 = DriveApp.getFileById("1bDZaBUjweWy-KwGpQP68eoJLMulUfXYZ") | |
const weights = f2.getBlob().getBytes() | |
return [modeljson, weights] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const c = document.getElementById("myCanvas"); | |
c.addEventListener("mousedown", setLastCoords); // fires before mouse left btn is released | |
c.addEventListener("mousemove", freeForm); | |
const ctx = c.getContext("2d"); | |
function setLastCoords(e) { | |
const {x, y} = c.getBoundingClientRect(); | |
lastX = e.clientX - x; | |
lastY = e.clientY - y; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VisionModel { | |
// ... other methods | |
async run({ batchSize = 1024, epochs = 1, trainExisting = true } = {}) { | |
// ... same as before | |
await this.visionModelWorker.create(true); | |
const modelInstance = await tf.loadLayersModel(Constants.MODEL_DISK_PATH()); | |
tfvis.show.modelSummary(this.tensorboardDiv, modelInstance); | |
// ... training callbacks like before |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In the main thread | |
class VisionModel { | |
/** | |
* Creates an instance of a wrapper of Vision Model that resides in the worker thread | |
* This has access to the DOM and can show visualizations, unlike the worker | |
* @param {HTMLElement} tensorboardDiv Instance of a Div tag where to show live model training | |
*/ | |
constructor(tensorboardDiv) { | |
this.tensorboardDiv = tensorboardDiv; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VisionModelWorker { | |
... // create and getData functions | |
async train(epochs, batchSize) { | |
this.model.compile({ optimizer: tf.train.adam(), loss: 'categoricalCrossentropy', metrics: ['accuracy'] }); | |
const historyObj = await this.model.fit(this.dataBunch.trainX, this.dataBunch.trainY, { | |
batchSize: batchSize, | |
validationData: [this.dataBunch.testX, this.dataBunch.testY], | |
epochs: epochs, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
importScripts("https://cdnjs.cloudflare.com/ajax/libs/tensorflow/1.3.2/tf.min.js", "data.js"); | |
class VisionModelWorker { | |
constructor() { | |
this.model = null; // holds tfjs model | |
this.dataBunch = null; // holds the X and y datasets | |
} | |
create() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static parseBuffer(buffer, offset = 16, isBigEndianProcessor = false) { | |
let allData = null; | |
if (isBigEndianProcessor) { | |
// Let native JavaScript decode the bytes as MNIST dataset is encoded in Big Endian format | |
allData = new Uint8Array(buffer, offset) | |
} | |
else { | |
const dataView = new DataView(buffer); | |
const numBytes = dataView.byteLength - offset; | |
allData = new Uint8Array(numBytes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function main() { | |
console.log(`Start: ${performance.now()}`); | |
await sleep(2000); | |
console.log(`End: ${performance.now()}`); | |
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
NewerOlder