Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
adds simple classification example
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyklee committed Oct 2, 2019
1 parent af12536 commit 387b470
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
19 changes: 19 additions & 0 deletions p5js/NeuralNetwork/NeuralNetwork_Simple-Classification/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>

<head>
<meta charset="UTF-8">
<title>Neural Network</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/addons/p5.dom.min.js"></script>
<script src="http://localhost:8080/ml5.js" type="text/javascript"></script>
</head>

<body>
<h1>Neural Network Classification</h1>
<label for="avatar">Load Model:</label>
<input type="file" id="load" multiple />
<script src="sketch.js"></script>
</body>

</html>
56 changes: 56 additions & 0 deletions p5js/NeuralNetwork/NeuralNetwork_Simple-Classification/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2018 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

/* ===
ml5 Example
Image classification using MobileNet and p5.js
This example uses a callback pattern to create the classifier
=== */
let nn;

const options = {
inputs: 1,
outputs: 2,
task: 'classification',
debug: true
}

function setup(){
createCanvas(400, 400);
nn = ml5.neuralNetwork(options);


console.log(nn)
createTrainingData();
nn.normalize();

const trainingOptions={
batchSize: 24,
epochs: 32
}

nn.train(trainingOptions,finishedTraining); // if you want to change the training options
// nn.train(finishedTraining); // use the default training options
}

function finishedTraining(){

nn.classify([300], function(err, result){
console.log(result.output);
})

}

function createTrainingData(){
for(let i = 0; i < 400; i++){
if(i%2 === 0){
const x = random(0, width/2);
nn.addData( [x], ['left'])
} else {
const x = random(width/2, width);
nn.addData( [x], ['right'])
}
}
}

1 comment on commit 387b470

@shiffman
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!! 😻

Please sign in to comment.