-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π New Release: ml5.js v0.3.0 π (#386)
* add ImageData as valid image type * add CVAE * add latent dim * add random generate * fix p5Image support * fix CVAE parameter * Added a parameter to the save function so that it is possible to add a custom filename to the model that is saved. * Unet fix (#357) Add uNet model and additional fixes - adds uNet model from @zaidalyafeai β¨ - adds preload() for uNet - uses loadImage on window.loadImage vs. window.p5.loadImage * Added sentiment analysis (#339) * Added sentiment analysis * delete files * fixed issues for pull request * add p5 utils (#358) * fix charRNN tests (#349) * add tests to CharRNN * test(CharRNN): add tests to CharRNN added descriptive tests to ensure CharRNN behaves like its example * remove dist * Add tests to CharRNN (#307) * add tests to CharRNN * test(CharRNN): add tests to CharRNN added descriptive tests to ensure CharRNN behaves like its example * remove dist * check preload support for other nets and classifiers (#313) Adds specified nets to support preload // TODO: add examples showing appropriate use of preload * change CharRNN specs to meet time limit, add initial code for videoClassifier * videoClassifier functioning * charRNN functional * fix out of date file * add preload support for cvae (#360) * Update TensorFlow.js to 1.0.2 (#336) * upgrade to tfjs1.0.0 * fix loadModel * fix buffer * fix getLayer * Adds fixes to PR #332 for tfjs 1.0.2 updates (#366) * upgrade to tfjs1.0.0 * fix loadModel * fix buffer * fix getLayer * updated package lock * added @tensorflow/tfjs-core as dependency * add graphmodel for infer (#365) * Add DCGAN Model into ml5 (#351) * Create index.js * updated index.js and DCGAN/index.js * DCGAN updates and fixes (#362) * Create index.js * fixed DCGAN errors * updates p5Utils destructuring, fixes linting issues, and updates tfjs to 1.0.2 to match dcgan reqs * fixed cvae * use this.model instead of using model as param to this.compute() * Makes UNET compatible with tfjs 1.0.2 (#367) * added package-lock * updated UNET for use with tfjs 1.0.2 * Makes Sentiment compatible with tfjs 1.0.2 (#368) * added package-lock * rm sentiment-node * changed loadModel to loadLayersModel * Makes CVAE compatible with tfjs 1.0.2 (#369) * added package-lock * updates cvae to tfjs 1.0.2 api * update tfjs to 1.1.2 (#373) * featureExtractor: accept HTML canvas or p5 canvas when addImage(), classify() or predict() * fix: KNNClassifier accepts a number as class index when addExample(features, number) * added check for moz browser ref:https://stackoverflow.com/questions/48623376/typeerror-capturestream-is-not-a-function (#375) This addresses the video capture breaking in YOLO and potentially other video based functions that require the use of .captureStream(). As the .captureStream() function is still experimental, this adds the moz prefix and a browser check to see if we are using firefox or not. * rm todo * updated package-lock.json * Adds label number option to featureExtractor.classification() (#376) * changed numClasses to numLabels * added num label as option to classification() * updated FeatureExtractor Test with numLabels * adds object as param to .classificaiton() * moved options into this.config * fix feature extractor test - add .config * added pose:poseWithParts into .singlePose() (#381) * Adds jsdoc inline-documentation - work in progress (#378) * added jsdoc documentation for imageClassifier * adds dcgan documentation - needs checking * Add jsdoc (#382) * Add jsdocs for CharRNN * Add jsdocs for CVAE * Add jsdocs for FeatureExtractor * Add jsdocs for KNN * Add jsdocs for PitchDetection * Add jsdocs for Pix2pix * Add jsdocs for posenet * Add jsdocs for Sentiment * Add jsdocs for styletransfer * add linebreaks to long lines * added basic docs to sketchRnn * added basic docs to unet * added basic docs to word2vec * added basic yolo docs * Adds V0.3.0 to package.json and Readme for new release (#385) * changed package.json to v0.3.0 * added latest version reference in readme * added lib min - will remove after this release
- Loading branch information
Showing
30 changed files
with
1,248 additions
and
9,568 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,15 +19,20 @@ ml5.js is heavily inspired by [Processing](https://processing.org/) and [p5.js]( | |
|
||
There are several ways you can use the ml5.js library: | ||
|
||
* You can use the latest version (0.2.3) by adding it to the head section of your HTML document: | ||
* You can use the latest version (0.3.0) by adding it to the head section of your HTML document: | ||
|
||
**v0.2.3** | ||
**v0.3.0** | ||
```javascript | ||
<script src="https://unpkg.com/ml5@0.2.3/dist/ml5.min.js" type="text/javascript"></script> | ||
<script src="https://unpkg.com/ml5@0.3.0/dist/ml5.min.js" type="text/javascript"></script> | ||
``` | ||
|
||
* If you need to use an earlier version for any reason, you can change the version number. | ||
|
||
**v0.2.3** | ||
```javascript | ||
<script src="https://unpkg.com/[email protected]/dist/ml5.min.js" type="text/javascript"></script> | ||
``` | ||
|
||
**v0.1.3** | ||
|
||
```javascript | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright (c) 2018 ml5 | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: false}}] */ | ||
/* eslint no-await-in-loop: "off" */ | ||
/* | ||
* CVAE: Run conditional auto-encoder for pro-trained model | ||
*/ | ||
|
||
import * as tf from '@tensorflow/tfjs'; | ||
import callCallback from '../utils/callcallback'; | ||
|
||
class Cvae { | ||
/** | ||
* Create a Conditional Variational Autoencoder (CVAE). | ||
* @param {String} modelPath - Required. The url path to your model. | ||
* @param {function} callback - Required. A function to run once the model has been loaded. | ||
*/ | ||
constructor(modelPath, callback) { | ||
/** | ||
* Boolean value that specifies if the model has loaded. | ||
* @type {boolean} | ||
* @public | ||
*/ | ||
this.ready = false; | ||
this.model = {}; | ||
this.latentDim = tf.randomUniform([1, 16]); | ||
this.modelPath = modelPath; | ||
this.modelPathPrefix = ''; | ||
|
||
this.jsonLoader().then(val => { | ||
this.modelPathPrefix = this.modelPath.split('manifest.json')[0] | ||
this.ready = callCallback(this.loadCVAEModel(this.modelPathPrefix+val.model), callback); | ||
this.labels = val.labels; | ||
// get an array full of zero with the length of labels [0, 0, 0 ...] | ||
this.labelVector = Array(this.labels.length+1).fill(0); | ||
}); | ||
} | ||
|
||
// load tfjs model that is converted by tensorflowjs with graph and weights | ||
async loadCVAEModel(modelPath) { | ||
this.model = await tf.loadLayersModel(modelPath); | ||
return this; | ||
} | ||
|
||
/** | ||
* Generate a random result. | ||
* @param {String} label - A label of the feature your want to generate | ||
* @param {function} callback - A function to handle the results of ".generate()". Likely a function to do something with the generated image data. | ||
* @return {raw: ImageData, src: Blob, image: p5.Image} | ||
*/ | ||
async generate(label, callback) { | ||
return callCallback(this.generateInternal(label), callback); | ||
} | ||
|
||
loadAsync(url){ | ||
return new Promise((resolve, reject) => { | ||
if(!this.ready) reject(); | ||
loadImage(url, (img) => { | ||
resolve(img); | ||
}); | ||
}); | ||
}; | ||
|
||
getBlob(inputCanvas) { | ||
return new Promise((resolve, reject) => { | ||
if (!this.ready) reject(); | ||
|
||
inputCanvas.toBlob((blob) => { | ||
resolve(blob); | ||
}); | ||
}); | ||
} | ||
|
||
checkP5() { | ||
if (typeof window !== 'undefined' && window.p5 && this | ||
&& window.p5.Image && typeof window.p5.Image === 'function') return true; | ||
return false; | ||
} | ||
|
||
async generateInternal(label) { | ||
const res = tf.tidy(() => { | ||
this.latentDim = tf.randomUniform([1, 16]); | ||
const cursor = this.labels.indexOf(label); | ||
if (cursor < 0) { | ||
console.log('Wrong input of the label!'); | ||
return [undefined, undefined]; // invalid input just return; | ||
} | ||
|
||
this.labelVector = this.labelVector.map(() => 0); // clear vector | ||
this.labelVector[cursor+1] = 1; | ||
|
||
const input = tf.tensor([this.labelVector]); | ||
|
||
const temp = this.model.predict([this.latentDim, input]); | ||
return temp.reshape([temp.shape[1], temp.shape[2], temp.shape[3]]); | ||
}); | ||
|
||
const raws = await tf.browser.toPixels(res); | ||
|
||
const canvas = document.createElement('canvas'); // consider using offScreneCanvas | ||
const ctx = canvas.getContext('2d'); | ||
const [x, y] = res.shape; | ||
canvas.width = x; | ||
canvas.height = y; | ||
const imgData = ctx.createImageData(x, y); | ||
const data = imgData.data; | ||
for (let i = 0; i < x * y * 4; i += 1) data[i] = raws[i]; | ||
ctx.putImageData(imgData, 0, 0); | ||
|
||
const src = URL.createObjectURL(await this.getBlob(canvas)); | ||
let image; | ||
/* global loadImage */ | ||
if (this.checkP5()) image = await this.loadAsync(src); | ||
return { src, raws, image }; | ||
} | ||
|
||
async jsonLoader() { | ||
return new Promise((resolve, reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open('GET', this.modelPath); | ||
|
||
xhr.onload = () => { | ||
const json = JSON.parse(xhr.responseText); | ||
resolve(json); | ||
}; | ||
xhr.onerror = (error) => { | ||
reject(error); | ||
}; | ||
xhr.send(); | ||
}); | ||
} | ||
} | ||
|
||
const CVAE = (model, callback) => new Cvae(model, callback); | ||
|
||
|
||
export default CVAE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.