Published
Edited
Mar 20, 2019
1 fork
14 stars
Insert cell
Insert cell
mobilenet_truncated = {
const mobilenet = await tf.loadModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json');
// Return a model that outputs an internal activation.
const layer = mobilenet.getLayer('conv_pw_13_relu');
return tf.model({inputs: mobilenet.inputs, outputs: layer.output});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
capture = () => {
return tf.tidy(() => {
const webcamImage = tf.fromPixels(input).toFloat();
const croppedImage = cropImage(webcamImage);
const batchedImage = croppedImage.expandDims(0);
const offset = tf.scalar(127);
return batchedImage.sub(offset).div(offset);
});
}
Insert cell
addExample = (example, label) => {
// One-hot encode the label.
const y = tf.tidy(() => tf.oneHot(tf.tensor1d([label]), NUM_CLASSES));

if (xs == null) {
mutable xs = tf.keep(example);
mutable ys = tf.keep(y);
} else {
const oldX = xs;
mutable xs = tf.keep(oldX.concat(example, 0));

const oldY = ys;
mutable ys = tf.keep(oldY.concat(y, 0));

oldX.dispose();
oldY.dispose();
y.dispose();
}
}
Insert cell
Insert cell
Insert cell
Insert cell
model = {
const m = tf.sequential({
layers: [
// Flattens the input to a vector so we can use it in a dense layer. While
// technically a layer, this only performs a reshape (and has no training
// parameters).
tf.layers.flatten({inputShape: [7, 7, 256]}),
tf.layers.dense({
units: 100,
activation: 'relu',
kernelInitializer: 'varianceScaling',
useBias: true
}),
// The number of units of the last layer should correspond
// to the number of classes we want to predict.
tf.layers.dense({
units: NUM_CLASSES,
kernelInitializer: 'varianceScaling',
useBias: false,
activation: 'softmax'
})
]
});
const optimizer = tf.train.adam();
m.compile({optimizer: optimizer, loss: 'categoricalCrossentropy'});
return m;
}
Insert cell
Insert cell
Insert cell
trainingInfo = {
let i = 0;
while (isTraining) {
const history = await model.fit(xs, ys, {
batchSize: 8,
epochs: 5
});
const loss = history.history.loss[0];
console.log('doing this');
yield {i: i++, loss};
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more