petals=
MobileNet loading...
|
The script generates 300 pairs of randomly rotated procedural tulips with 5 and 6 petals (corresponding predictions 0 or 1). MobileNet (~15 MB of weights) is used to get a 1024 features map (embeddings) from a 224x224 image. The script collects embeddings in a dataset and trains one perceptron (just 1024 weights) for 30 epochs. It all takes a few minutes (HP laptop with Ryzen 5 3500U).
This simple demo counts petals surprisingly well (much faster than me)!
It is based on TensorFlow.js Example: Transfer Learning to play Pacman via the Webcam. It uses a small neural network on the top of MobileNet_v1_1.0_224
gl.drawElements(gl.TRIANGLES, di - di1, gl.UNSIGNED_INT, 4*di1) const embedding = await mobilenet.infer(cnv, true); const predictions = model.predict(embedding);with 1 dense layer
async function train() { model = tf.sequential({ layers: [ tf.layers.dense({ inputShape: [1024], units: 1, activation: 'sigmoid', kernelInitializer: 'varianceScaling', useBias: true }) ] }); const optimizer = tf.train.adam(.01); model.compile({optimizer: optimizer, loss: 'binaryCrossentropy'}); const batchSize = 32; model.fit(xs, ys, { batchSize, epochs: 40, // shuffle: true, callbacks: { onEpochEnd: async (epoch, logs) => { console.log(epoch + ' Loss: ' + logs.loss.toFixed(5)); } } }); }Embeddings from MobileNet (x below) are collected in Dataset for training.
async function data(x){ const dx = await x.data(); dataset.set(dx, dataPos); dataPos += 1024; x.dispose(); }