-
Notifications
You must be signed in to change notification settings - Fork 40
Sequential NeuralNetwork #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mop9047
wants to merge
47
commits into
main
Choose a base branch
from
timeseries-base
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
0355d38
added_instantiation_files
mop9047 b8f031e
solved convergence issue with compile()
mop9047 bf67b69
instantiation code form TSDATA
mop9047 1228a59
added previous examples and refactored layers
mop9047 bedb655
added helper functions, simplified examples
mop9047 61c7d27
changes based on PR comments
mop9047 494ce16
convert linear/spatial to flag
mop9047 576d54c
Code Clean up and Simplified examples
mop9047 60d0eb4
Merged timeseries-as-task to timeseries-base and bug fixes
mop9047 1356185
merged timeseries as task to timeseries-base and bug fixes
mop9047 c5f78cf
Transferred files into NeuralNetwork Folder and code cleanup
mop9047 b9a1959
Fixed comment formatting
mop9047 a6afd6d
Changed Class names to Sequential
mop9047 174b1d8
changes based on PR comments
mop9047 3fc035a
chore: changed gestures to gesture
mop9047 e70ab0e
chore: replace Conv to withCNN
mop9047 8caffd3
Tweaks to neuralNetwork-sequence-weather-prediction example
gohai 03d3a18
Fix typo
gohai 790a162
Open-code sliding window in example
gohai f702700
Tweak variable placement and add comment
gohai 4c9594e
fix: sliding window bug
mop9047 e3d95f2
chore: slidingwindow to verb and rename samplewindow outputs
mop9047 002213a
chore: change padCoordinate name to setFixedLength
mop9047 b0c968c
fix: tslayers naming bug
mop9047 b5adac3
chore: clarify error when task is unknown
mop9047 c083beb
Rework the neuralNetwork-sequence-mouse-gesture-rdp example
gohai ec08ad9
Construct NN inside setup() in weather-prediction example
gohai e8f6958
Shave off two unneeded lines
gohai f580e64
fix: error capitalization from seqUtils
mop9047 0998b5b
chore: remove rdp visualizer
mop9047 0febf59
Rework the neuralNetwork-sequence-hand-gesture example
gohai 17a1cc2
Fix grammar in comment
gohai 21d3054
Use canvasDiv everywhere
gohai 155fde6
Use training/predicting in examples
gohai 9e53b97
Whitespace change
gohai d4a26a4
Flip for order
gohai 0e71849
Rework neuralNetwork-sequence-hand-gesture-load-model example
gohai 452ed2d
Tweaks to language
gohai 46924ba
Rename targetLength to sequenceLength and other tweaks
gohai 1308865
Revert part of last commit
gohai 024d91f
Rename "predicting" to "classifiying" in classification tasks
gohai 43b9198
feat: filter input/output by labels
mop9047 0d50690
chore: add new pre-trained models
mop9047 a7f8eea
chore: trainOptions, mm, barHeight
mop9047 8fd81c2
chore: friendlier error messages
mop9047 e84709f
fix: friendly error message normalize
mop9047 7776f28
chore: error for different lengths
mop9047 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,42 @@ | ||
<!-- | ||
👋 Hello! This is an ml5.js example made and shared with ❤️. | ||
Learn more about the ml5.js project: https://ml5js.org/ | ||
ml5.js license and Code of Conduct: https://github.com/ml5js/ml5-next-gen/blob/main/LICENSE.md | ||
|
||
This example demonstrates training a Sign Language classifier through ml5.neuralNetwork with sequeceClassification Task. | ||
--> | ||
|
||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>ml5.js Neural Network Hand Gesture Train and Save</title> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.4/p5.min.js"></script> | ||
<script src="../../dist/ml5.js"></script> | ||
</head> | ||
|
||
<body> | ||
<script src="sketch.js"></script> | ||
<div id="canvasDiv"></div> | ||
|
||
<p> | ||
Instructions: <br /> | ||
gohai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
1.) To add data, press the "Add Gesture #1 Data" button <br /> | ||
2.) Use one hand to do a gesture in front of the camera <br /> | ||
3.) Once done with the gesture, take your hand out of the frame<br /> | ||
4.) Repeat until there is a good amount of data<br /> | ||
5.) Do the same for gesture #2 by pressing the "Add Gesture #2 Data" | ||
button before doing the hand gesture in the frame<br /> | ||
6.) Press the "Train and Save Model" button to train the model and save it | ||
for future use.<br /> | ||
</p> | ||
</body> | ||
|
||
<style> | ||
button { | ||
font-family: Georgia, "Times New Roman", Times, serif; | ||
font-size: 20px; | ||
} | ||
</style> | ||
</html> |
180 changes: 180 additions & 0 deletions
180
examples/neuralNetwork-sequence-hand-gestures/sketch.js
This file contains hidden or 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,180 @@ | ||
/* | ||
* 👋 Hello! This is an ml5.js example made and shared with ❤️. | ||
* Learn more about the ml5.js project: https://ml5js.org/ | ||
* ml5.js license and Code of Conduct: https://github.com/ml5js/ml5-next-gen/blob/main/LICENSE.md | ||
* | ||
* This example demonstrates training a Hand Gesture classifier through ml5.neuralNetwork with sequeceClassification Task. | ||
*/ | ||
|
||
let video; | ||
let handPose; | ||
let hands = []; | ||
|
||
let sequence = []; | ||
let targetLength = 30; | ||
|
||
let gestures = ["Gesture #1", "Gesture #2"]; | ||
let counts = { "Gesture #1": 0, "Gesture #2": 0 }; | ||
|
||
let state = "collection"; | ||
let currGesture = gestures[0]; //set currGesture to gesture 1 by default | ||
gohai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
function preload() { | ||
// Load the handPose model | ||
handPose = ml5.handPose(); | ||
|
||
// setup the timeseries neural network | ||
let options = { | ||
outputs: ["label"], | ||
task: "sequenceClassificationConv", | ||
debug: "true", | ||
learningRate: 0.001, // the default learning rate of 0.01 didn't converge for this usecase, thus a learning rate of 0.001 is used (make smaller steps of parameters each update) | ||
}; | ||
model = ml5.neuralNetwork(options); | ||
} | ||
|
||
function setup() { | ||
let canvas = createCanvas(640, 480); | ||
canvas.parent("canvasDiv"); | ||
|
||
// setup video capture | ||
video = createCapture(VIDEO); | ||
gohai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
video.size(640, 480); | ||
video.hide(); | ||
|
||
// Setup the UI buttons for training | ||
UI(); | ||
|
||
// use handpose model on video | ||
handPose.detectStart(video, gotHands); | ||
} | ||
|
||
function draw() { | ||
// draw video on frame | ||
image(video, 0, 0, width, height); | ||
|
||
// helpful tooltip to include | ||
textSize(20); | ||
stroke(255); | ||
fill(0); | ||
text(state + " : " + currGesture, 50, 50); | ||
|
||
// if hand is detected in the frame, start recording gesture | ||
if (hands.length > 0) { | ||
handpoints = drawPoints(); | ||
sequence.push(handpoints); | ||
|
||
// add collected data to model once the hand is gone and state is collection | ||
} else if (hands.length <= 0 && sequence.length > 0) { | ||
if (state == "collection") { | ||
// pad the length of the coordinates to targetLength | ||
let inputData = model.padCoordinates(sequence, targetLength); | ||
let outputData = { label: currGesture }; | ||
|
||
// add data to the model | ||
model.addData(inputData, outputData); | ||
|
||
// Update the counts for the UI | ||
counts[currGesture]++; | ||
updateDataCountUI(); | ||
|
||
// pad the data and use for prediction if state is prediction | ||
} else if (state == "prediction") { | ||
let predictData = model.padCoordinates(sequence, targetLength); | ||
model.classify(predictData, gotResults); | ||
} | ||
|
||
// reset the sequence | ||
sequence = []; | ||
} | ||
} | ||
|
||
// Train the data when 'Train abd Save Model' button is pressed | ||
function train() { | ||
// The data should be normalized before training | ||
model.normalizeData(); | ||
|
||
currGesture = ""; | ||
|
||
// Train the model | ||
let trainingOptions = { | ||
epochs: 50, | ||
}; | ||
model.train(trainingOptions, finishedTraining); | ||
} | ||
|
||
// When the model is trained, save the model | ||
function finishedTraining() { | ||
state = "prediction"; | ||
model.save(); | ||
} | ||
|
||
// callback for predict | ||
function gotResults(results) { | ||
currGesture = results[0].label; | ||
} | ||
|
||
// Callback function for when handPose outputs data | ||
function gotHands(results) { | ||
hands = results; | ||
} | ||
|
||
// draw visuals for hand points and flatten values into an array | ||
function drawPoints() { | ||
let handpoints = []; | ||
// iterate through both hands | ||
for (let i = 0; i < hands.length; i++) { | ||
let hand = hands[i]; | ||
for (let j = 0; j < hand.keypoints.length; j++) { | ||
// access the keypoints in the hand | ||
let keypoint = hand.keypoints[j]; | ||
handpoints.push(keypoint.x, keypoint.y); | ||
|
||
fill(0, 255, 0); | ||
noStroke(); | ||
circle(keypoint.x, keypoint.y, 5); | ||
} | ||
} | ||
|
||
// assign to a different variable before clearing | ||
let output = handpoints; | ||
handpoints = []; | ||
|
||
return output; | ||
} | ||
|
||
////////////// UI Elements //////////// | ||
gohai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
function UI() { | ||
dataCountsP = createP( | ||
"Gesture 1 data: " + | ||
counts[gestures[0]] + | ||
"<br>Gesture 2 data: " + | ||
counts[gestures[0]] | ||
); | ||
rockButton = createButton("Add Gesture #1 Data"); | ||
rockButton.mousePressed(addGesture1); | ||
paperButton = createButton("Add Gesture #2 Data"); | ||
paperButton.mousePressed(addGesture2); | ||
trainButton = createButton("Train and Save Model"); | ||
trainButton.mousePressed(train); | ||
} | ||
|
||
// Set the current handPose data to the model as "Gesture #1" | ||
function addGesture1() { | ||
currGesture = gestures[0]; | ||
} | ||
|
||
// Set the current handPose data to the model as "Gesture #2" | ||
function addGesture2() { | ||
currGesture = gestures[1]; | ||
} | ||
|
||
// Update the HTML UI with the current data counts | ||
function updateDataCountUI() { | ||
dataCountsP.html( | ||
"Gesture 1 data: " + | ||
counts[gestures[0]] + | ||
"<br>Gesture 2 data: " + | ||
counts[gestures[1]] | ||
); | ||
} |
45 changes: 45 additions & 0 deletions
45
examples/neuralNetwork-sequence-load-model-hand-gestures/index.html
This file contains hidden or 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,45 @@ | ||
<!-- | ||
👋 Hello! This is an ml5.js example made and shared with ❤️. | ||
Learn more about the ml5.js project: https://ml5js.org/ | ||
ml5.js license and Code of Conduct: https://github.com/ml5js/ml5-next-gen/blob/main/LICENSE.md | ||
|
||
This example demonstrates loading a Sign Language classifier through ml5.neuralNetwork with sequeceClassification Task. | ||
--> | ||
|
||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>ml5.js Time Series Hand Gesture load model</title> | ||
mop9047 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.4/p5.min.js"></script> | ||
<script src="../../dist/ml5.js"></script> | ||
</head> | ||
|
||
<body> | ||
<script src="sketch.js"></script> | ||
<div id="canvasDiv"></div> | ||
<p> | ||
This example loads a model that is trained with ASL hand gestures for | ||
gohai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Hello and Goodbye. <br /> | ||
<br /> | ||
|
||
Instructions: <br /> | ||
1.) Use one hand to do a gesture in front of the camera <br /> | ||
2.) Wait for the points to disappear or the prediction appears on | ||
screen<br /> | ||
3.) To predict again, remove your hands in the frame and do the gesture | ||
again<br /><br /> | ||
|
||
How to do gestures for Hello and Goodbye in ASL: <br /> | ||
Hello: | ||
<a href="https://babysignlanguage.com/dictionary/hello/" | ||
>https://babysignlanguage.com/dictionary/hello/ </a | ||
><br /> | ||
Goodbye: | ||
<a href="https://babysignlanguage.com/dictionary/goodbye/" | ||
>https://babysignlanguage.com/dictionary/goodbye/ </a | ||
><br /> | ||
</p> | ||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.