soumya gupta
soumya gupta

Reputation: 1

Uncaught (in promise) Error: Error: Error in oneHot: depth must be >=2, but it is 1

I’ve collected now 4 poses clearly I’ve put more than 2 poses in the Jason file “data.json” can anyone help me out with this error? Why is this getting stuck at brain.normalizeData(); it’d be really helpful if anyone can help me figure this out (see error image). error image 2

ml5.min.js:528 Uncaught (in promise) Error: Error: Error in oneHot: depth must be >=2, but it is 1
at t.<anonymous> (ml5.min.js:528:39116)
at l (ml5.min.js:579:35905)
at Generator._invoke (ml5.min.js:579:35693)
at Generator.next (ml5.min.js:579:36330)
at n (ml5.min.js:66:2122)
at s (ml5.min.js:66:2332)

Sketch.js

let video;
let poseNet;
let pose;
let skeleton;
let brain;
let state = 'waiting';
let targetLabel;
function keyPressed(){
    if(key == 's'){
        brain.saveData();
    } else {
    targetLabel = key;
    console.log(targetLabel);
    setTimeout(function(){
        console.log('collecting');
        state = 'collecting';
   
        setTimeout(function(){
            console.log('not collecting');
            state = 'waiting';        
   
        }, 10000);
    }, 10000);
}
}
function setup(){
         createCanvas(640, 480);
         video = createCapture(VIDEO);
         video.hide();
       poseNet = ml5.poseNet(video, modelLoaded);
         poseNet.on('pose', gotPoses)
         let options = {
            inputs: 34,
            outputs: 4,
            task: 'classification',
            debug: true
         }
         brain = ml5.neuralNetwork(options);
         brain.loadData('data.json', dataReady);
          console.log('data loaded');
}
 
 function dataReady(){
//  console.log('data normaliz call');
//     brain.normalizeData();
//      console.log('data normalized');
    brain.train({epochs: 50}, finished);
//     console.log('data trained');
 }
 function finished(){
    console.log('model trained');
     brain.save();
     console.log('data saved');
 }
function gotPoses(poses){
        if(poses.length > 0){
           pose = poses[0].pose;
          skeleton = poses[0].skeleton;
          if(state == 'collecting'){
          let inputs = [];
          for(let i = 0; i < pose.keypoints.length; i++){
            let x = pose.keypoints[i].position.x;
            let y = pose.keypoints[i].position.y;
            inputs.push(x);
            inputs.push(y);
            }
            let target = [targetLabel];
          brain.addData(inputs, target);
        }
        }
    }
   
  function modelLoaded(){
    console.log('poseNet ready');
  }
  function draw(){
    translate(video.width, 0);
    scale(-1, 1);
    image(video, 0, 0, video.width, video.height);
    if(pose){
        for(let i = 0; i < skeleton.length; i++){
       let a = skeleton[i][0];
       let b = skeleton[i][1];
        strokeWeight(2);
        stroke(0);
        line(a.position.x, a.position.y, b.position.x, b.position.y);
        }
   
    for(let i = 0; i < pose.keypoints.length; i++){
        let x = pose.keypoints[i].position.x;
        let y = pose.keypoints[i].position.y;
        fill(0);
        stroke(255);
        ellipse(x, y, 16, 16);
        }
  }
}

Here is the link to my sketch.js and data.json

Upvotes: 0

Views: 140

Answers (1)

Linda Paiste
Linda Paiste

Reputation: 42188

The error happens when the neural network is preparing for training because your input data is, essentially, not valid.

You are using a classification task, so the ys of your data should be the classification for that pose. Like "sitting", "standing", etc. However you have "ys": { "0": "y" } for every single data point. This means that every single pose gets the label "y". The neural network cannot make any associations because the only information that it has is that all inputs should get the label "y".

Specifically, it encounters an error when it tries to apply one-hot encoding to your labels. This is a method for transforming categorical strings into arrays of numbers that the neural network can process. If your labels were "sitting", "standing" and "running", it would map them like "sitting" => [0, 0, 1], "standing" => [0, 1, 0], "running" => [1, 0, 0]. But you have only one label "y", so you get an error:

Error in oneHot: depth must be >=2, but it is 1

That error comes from the underlying TensorFlow tfjs-core package, here.


The ml5 package could do a much better job of telling you what the actual error is. This was actually really hard to debug.

The error gets thrown from this line in the .loadData() function, so I initially assumed that it was encountering an error while loading your data. But it's actually happening in your dataReady callback, which is called inside of the same try/catch block. The way that ml5 catches the error only to re-throw it is dumb as it causes the stack trace to get lost.

There's a PR to give a better error in this particular scenario where all inputs have the same category.

Upvotes: 1

Related Questions