Alvin Chan
Full Stack Developer
Data scientist
input_1
input_2
input_3
output
param_1
param_2
param_3
+
+
=
output
input_1 * param_1 input_2 * param_2 input_3 * param_3
length
breadth
brightness
perimeter
x0
x2
x2
length
breadth
input_1
input_2
input_3
output
Dense Layer 1
Dense Layer 2
input_1
input_2
input_3
output_1
input_4
input_5
step 1
input_1
input_2
input_3
output_1
input_4
input_5
output_2
Step 2
input_1
input_2
input_3
input_4
input_5
output_3
step 3
output_1
output_2
32 px
32 px
Source: http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture6.pdf
32 px
32 px
input_1
input_2
input_3
output_1
input_4
input_5
Source: http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture6.pdf
https://storage.googleapis.com/tfjs-examples/webcam-transfer-learning/dist/index.html
[224, 224, 3]
[7, 7, 256]
[4]
[12544]
flatten
dense
layer
mobilenet
trained model
import * as tf from '@tensorflow/tfjs';
async function loadMobilenet() {
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});
}
mobilenet = loadMobilenet();
import * as tf from '@tensorflow/tfjs';
model = tf.sequential({
layers: [
tf.layers.flatten({inputShape: [7, 7, 256]}),
// Layer 1
tf.layers.dense({
units: ui.getDenseUnits(),
activation: 'relu',
kernelInitializer: 'varianceScaling',
useBias: true
}),
// Layer 2. 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'
})
]
});
[N, 224, 224, 3]
[N, 7, 7, 256]
[N, 4]
[N, 12544]
flatten
dense
layer
mobilenet
trained model
const optimizer = tf.train.adam(ui.getLearningRate());
model.compile({optimizer: optimizer, loss: 'categoricalCrossentropy'});
const batchSize =
Math.floor(controllerDataset.xs.shape[0] * ui.getBatchSizeFraction());
// Train the model!
model.fit(controllerDataset.xs, controllerDataset.ys, {
batchSize,
epochs: ui.getEpochs(),
callbacks: {
onBatchEnd: async (batch, logs) => {
ui.trainStatus('Loss: ' + logs.loss.toFixed(5));
await tf.nextFrame();
}
}
});
[224, 224, 3]
[7, 7, 256]
[4]
mobilenet
trained model
argmax
LEFT
const predictedClass = tf.tidy(() => {
const img = webcam.capture();
const activation = mobilenet.predict(img);
const predictions = model.predict(activation);
return predictions.as1D().argMax();
});
const classId = (await predictedClass.data())[0];
https://github.com/cheeseprata/
twitter: @a1vinchan
Slides @
https://slides.com/alvinchan/tensorflow-js