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

[WIP] DIY NN - examples for loading model #210

Merged
merged 3 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions p5js/NeuralNetwork/NeuralNetwork_load_model/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>

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

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

<style>

</style>
</head>

<body>
<h1>Color Classifier - Neural Network</h1>
<script src="sketch.js"></script>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"modelTopology":{"class_name":"Sequential","config":[{"class_name":"Dense","config":{"units":16,"activation":"sigmoid","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense1","trainable":true,"batch_input_shape":[null,3],"dtype":"float32"}},{"class_name":"Dense","config":{"units":9,"activation":"softmax","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense2","trainable":true}}],"keras_version":"tfjs-layers 1.2.2","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["./model.weights.bin"],"weights":[{"name":"dense_Dense1/kernel","shape":[3,16],"dtype":"float32"},{"name":"dense_Dense1/bias","shape":[16],"dtype":"float32"},{"name":"dense_Dense2/kernel","shape":[16,9],"dtype":"float32"},{"name":"dense_Dense2/bias","shape":[9],"dtype":"float32"}]}]}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"inputMin":[0,0,0],"inputMax":[255,255,255],"outputMin":0,"outputMax":1},"meta":{"inputUnits":3,"outputUnits":9,"inputs":{"b":{"dtype":"number"},"g":{"dtype":"number"},"r":{"dtype":"number"}},"outputs":{"label":{"dtype":"string","uniqueValues":["green-ish","pink-ish","orange-ish","blue-ish","brown-ish","red-ish","yellow-ish","purple-ish","grey-ish"],"legend":{"green-ish":[1,0,0,0,0,0,0,0,0],"pink-ish":[0,1,0,0,0,0,0,0,0],"orange-ish":[0,0,1,0,0,0,0,0,0],"blue-ish":[0,0,0,1,0,0,0,0,0],"brown-ish":[0,0,0,0,1,0,0,0,0],"red-ish":[0,0,0,0,0,1,0,0,0],"yellow-ish":[0,0,0,0,0,0,1,0,0],"purple-ish":[0,0,0,0,0,0,0,1,0],"grey-ish":[0,0,0,0,0,0,0,0,1]}}},"isNormalized":true}}
56 changes: 56 additions & 0 deletions p5js/NeuralNetwork/NeuralNetwork_load_model/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
let neuralNetwork;
let submitButton;

let rSlider, gSlider, bSlider;
let labelP;
let lossP;

function setup() {
// Crude interface
lossP = createP('loss');

createCanvas(100, 100);

labelP = createP('label');

rSlider = createSlider(0, 255, 255);
gSlider = createSlider(0, 255, 0);
bSlider = createSlider(0, 255, 255);

let nnOptions = {
inputs: ['r', 'g', 'b'],
outputs: ['label'],
task: 'classification',
debug: true
};
neuralNetwork = ml5.neuralNetwork(nnOptions);
neuralNetwork.load('model/model.json', modelReady);
}

function modelReady() {
console.log('model loaded!')
classify();
};


function classify() {
let inputs = {
r: rSlider.value(),
g: gSlider.value(),
b: bSlider.value()
}
neuralNetwork.classify(inputs, gotResults);
}

function gotResults(error, results) {
if (error) {
console.error(error);
} else {
labelP.html(`label:${results[0].label}, confidence: ${results[0].confidence.toFixed(2)}`);
classify();
}
}

function draw() {
background(rSlider.value(), gSlider.value(), bSlider.value());
}