Skip to content

Instantly share code, notes, and snippets.

View Nithanaroy's full-sized avatar
🎯
Focusing

Nitin Pasumarthy Nithanaroy

🎯
Focusing
View GitHub Profile
@Nithanaroy
Nithanaroy / tfjs-drive-deploy-load-model.js
Last active August 28, 2021 17:51
Load Tensorflow JS model from Google Drive
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();
}
@Nithanaroy
Nithanaroy / tfjs-drive-deploy-app-script.js
Created August 16, 2020 03:45
App script file which loads a trained model from drive for serving
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]
}
@Nithanaroy
Nithanaroy / tfjs-blog-user-input-canvas.js
Created February 26, 2020 01:22
Lets users draw on the browser using their pointer device
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;
@Nithanaroy
Nithanaroy / tfjs-blog-viz-model-summary.js
Created February 21, 2020 17:56
Visualize Model Summary in DOM by restoring the model from IndexedDB
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
@Nithanaroy
Nithanaroy / tfjs-blog-viz-loss-acc-curves.js
Created February 21, 2020 07:16
Visualize Loss and Accuracy curves using Comlink
// 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;
}
@Nithanaroy
Nithanaroy / tfjs-blog-train-model.js
Created February 21, 2020 06:30
Train the CNN Model in TFJS
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,
@Nithanaroy
Nithanaroy / tfjs-blog-create-model.js
Last active February 21, 2020 06:11
Create a CNN Model in TFJS
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() {
@Nithanaroy
Nithanaroy / tfjs-blog-parse-mnist-binary.js
Last active February 20, 2020 08:02
Code to parse MNIST Binary Buffer for Training
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);
@Nithanaroy
Nithanaroy / main.js
Created January 5, 2020 16:45
Snippet that achieves Python's sleep
async function main() {
console.log(`Start: ${performance.now()}`);
await sleep(2000);
console.log(`End: ${performance.now()}`);
}
@Nithanaroy
Nithanaroy / balanced-clusters.ipynb
Last active December 27, 2019 12:36
Desktop/blog/clustering/balanced-clusters.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.