Machine Learning

 JS

@AidaraAnta

Machine Learning

Gives computers the ability to learn without being explicitly programmed, Arthur Samuel 1959

Labels

Features

Model

Machine learning dans le navigateur

Pourquoi ? 

  • Large distribution

  • Protection des données personnelles

  • Interactivité

  • Démocratisation

  • Pas d’installation

Démo

Step 1 : Chargement et conversion du modèle

from keras.applications.mobilenet import MobileNet

# Load Mobilenet model with imagenet weights
model = MobileNet(weights='imagenet')

# Save model on file system
model.save('mobilenet.h5')
$> tensorflowjs_converter --input_format keras \
path/to/mobilenet.h5 path/to/tfjs-target-dir
 {
    "modelTopology": {
      ...
    },

    "weightsManifest": {
      ...
    }
 }
// Load model in tensorflow.js
const model = await tf.loadModel('model.json');

Sauvegarde

Conversion

Chargement

Step 2 : Récupération des données d'entraînement

async (label, canvas) => {
    tf.tidy(() => {
      const sample = this.camera.takePicture(canvas);
      this.props.classifier.addTrainingData(sample, label);
    });
    await tf.nextFrame();
};

Step 3 : Création et compilation du modèle final

// Create model
this.model = tf.sequential({
      layers: [
        tf.layers.flatten({ inputShape: [7, 7, 256] }),
        tf.layers.dense({
          units: 100,
          useBias: true,
          activation: 'relu',
          kernelInitializer: 'varianceScaling',
        }),
        tf.layers.dense({
          useBias: true,
          activation: 'softmax',
          units: this.classes,
          kernelInitializer: 'varianceScaling',
        }),
      ],
});

// Compile model
this.model.compile({ optimizer: tf.train.adam(0.0001), loss: 'categoricalCrossentropy' });

Step 4 : Entraînement

// Train model
this.model.fit(dataset.features, dataset.labels, {
   batchSize,
   epochs: 20,
   callbacks: {
     onBatchEnd: async (batch, logs) => {
       console.log('Loss:' + logs.loss.toFixed(5));
       await tf.nextFrame();
     },
   },
});

Step 5 : Prédiction

async predict(callback) {
    while (this.isPredicting) {
      const predictions = tf.tidy(() => {
        const image = this.camera.takePicture();
        const activation = this.trainedModel.predict(image);
        const predictions = this.model.predict(activation);
        const tensor = predictions.as1D();

        return tensor.argMax();
      });
      const label = (await predictions.data())[0];
      console.log(`Predicted label: ${label}`);
      predictions.dispose();
      callback(label);
      await tf.nextFrame();
    }
}

Conclusion

Take away!

Introduction Machine Learning

https://developers.google.com/machine-learning/crash-course

Tensorflow.js tutoriels

https://js.tensorflow.org/tutorials/

Tensorflow playground

https://playground.tensorflow.org/​

Tensorflow.js server side

https://github.com/tensorflow/tfjs-node

Apprendre la data avec JS

http://learnjsdata.com/

Liste de frameworks de machine learning par langages

https://github.com/josephmisiti/awesome-machine-learning​


@AidaraAnta

Machine learning <3 JS : rendez votre navigateur web intelligent

By anta

Machine learning <3 JS : rendez votre navigateur web intelligent

  • 241
Loading comments...

More from anta