ml5js
  • Reference
  • Examples
  • Data
  • Training
  • Experiments
  • Learn
  • Code

›Examples

Documentation

  • Getting Started
  • Promises and Callbacks
  • charRNN()
  • featureExtractor()
  • imageClassifier()
  • KNNClassifier()
  • pitchDetection()
  • pix2pix()
  • poseNet()
  • SketchRNN()
  • styleTransfer()
  • word2vec()
  • YOLO()

Examples

  • Quick Start
  • Image Classification
  • Video Classification
  • Classifier with Feature Extractor
  • Regression with Feature Extractor
  • KNN Classifier with Feature Extractor
  • KNN Classifier with PoseNet
  • Text Generation with LSTM
  • Interactive Text Generation LSTM
  • SketchRNN
  • Style Transfer
  • Style Transfer with Webcam
  • Pix2Pix
  • PoseNet with Webcam
  • YOLO with Webcam
  • Word2Vec

Data

  • Overview
  • Data Collection
  • Types of Data
  • Preparing your own data
  • Creating your data

Training

  • Introduction
  • Setting up
  • Training a LSTM
  • Training Style Transfer
  • Training Pix2Pix

Style Transfer

Style Transfer is a machine learning technique that allows to transfer the style of one image into another one. This is a two step process, first you need to train a model on one particular style and then you can apply this style to another image. In this example we are using two pre-trained models.

You can train your own images following this tutorial.

This example is using p5.js.

Demo

Loading Models...

Input Image:

input img

Style A: The Great Wave off Kanagawa, 1829 - Katsushika Hokusai

style one

Style B: Udnie (Young American Girl, The Dance), 1913 - Francis Picabia

style two

Code

// Copyright (c) 2018 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

/* ===
ml5 Example
Style Transfer Image Example using p5.js
This uses a pre-trained model of The Great Wave off Kanagawa and Udnie (Young American Girl, The Dance)
=== */

let inputImg;
let statusMsg;
let transferBtn;
let style1;
let style2;

function setup() {
  noCanvas();
  // Status Msg
  statusMsg = select('#statusMsg');

  // Get the input image
  inputImg = select('#inputImg');

  // Transfer Button
  transferBtn = select('#transferBtn')
  transferBtn.mousePressed(transferImages);

  // Create two Style methods with different pre-trained models
  style1 = ml5.styleTransfer('models/wave', modelLoaded);
  style2 = ml5.styleTransfer('models/udnie', modelLoaded);
}

// A function to be called when the models have loaded
function modelLoaded() {
  // Check if both models are loaded
  if(style1.ready && style2.ready){
    statusMsg.html('Ready!')
  }
}

// Apply the transfer to both images!
function transferImages() {
  statusMsg.html('Applying Style Transfer...!');

  style1.transfer(inputImg, function(err, result) {
    createImg(result.src).parent('styleA');
  });

  style2.transfer(inputImg, function(err, result) {
    createImg(result.src).parent('styleB');
  });

  statusMsg.html('Done!');
}

Source

← SketchRNNStyle Transfer with Webcam →
  • Demo
  • Code
  • Source
Docs
Getting StartedAPI ReferenceTraining Models
Learning
TutorialsGlossaryResources
Contribute
ExperimentsContributingStar
Facebook Open Source
This project is currently being maintained at NYU ITP by a community of teachers, residents and students.