ml5js / ml5-library

Friendly machine learning for the web! 🤖
https://ml5js.org
Other
6.46k stars 901 forks source link

[neural network] neuralNetwork.classify() is returning an error: Cannot convert undefined or null to object #801

Closed lieberscott closed 3 years ago

lieberscott commented 4 years ago

Dear ml5 community,

I am running an ML5 classification using ml5@0.4.3 in a React app, and getting to the .classify() step, and when I enter my input into the function, I get an error: Cannot convert undefined or null to object. I've tried entering the data as both an object { } and an array [{ }]. I can't figure it out.

Here is my code


people_arr = json.voters_arr;
keys = ["male", "female", "dob"];

let model_options = {
  inputs: keys,
  outputs: 1,
  task: "classification"
};

let model = ml5.neuralNetwork(model_options);

for (let person of people_arr) {
/*
  inputs will look like this
  let inputs = {
    male: 1,
    female: 0,
    dob: 641710800000,
    // have more, but keeping it simple for this example...
  }
*/
  let inputs = {
    male: person.male,
    female: person.female,
    dob: person.dob
  };

  let output = person.job; // array of either [0, 1] or [1, 0] depending if they have a job or not
  model.addData(inputs, output);
}

model.normalizeData();

let train_options = { epochs: 100 }
model.train(train_options, whileTraining);
.then(() => {
  let new_obj = { male: 0, female: 1, dob: 463726800000 }
  console.log("pre classify");
  return model.classify(new_obj); // <-- error happening here
})
.then((err, results) => {
  if (err) { console.log("error") }

  else {
    console.log("results : ", results);
    setValues({...values, results: new_arr })
  }
})
.catch((err) => { console.log("err : ", err) });

Full error: TypeError: Cannot convert undefined or null to object at entries (<anonymous>) at index.js:676 at Array.map (<anonymous>) at t.<anonymous> (index.js:668) at x (runtime.js:62) at Generator._invoke (runtime.js:296) at Generator.t.<computed> [as next] (runtime.js:114) at i (asyncToGenerator.js:17) at asyncToGenerator.js:28

lieberscott commented 4 years ago

I have tried to recreate the issue in a p5 web editor dropping in my code to replace Daniel Shiffman's example, and now I'm getting a new error: tf-core.esm.js:17 Uncaught Error: Cannot infer the missing size in [-1,0] when there are 0 elements Here's a p5 sketch: https://editor.p5js.org/WSox1235/sketches/aVCCDp7m

Any help greatly appreciated.

VishnuRaghavan commented 4 years ago

I believe the error is coming from the negative and positive values of dob in each object. When I looked at the code in the p5.js web editor. I found that the error is in normalizeData() function. This is what I did to resolve the issue, but yet I couldn't understand what you are trying to achieve with those data, to predict the job of the person using the three properties named male, female and dob??

function mousePressed() {

  if (state == 'collection') {
    console.log("collecting...");
    let total = 0;
    for (let person of people_arr) {
      total++;

      let mappedDob = map(person.dob, -999999999999999,9999999999999999,0,1); // mapping to range 0 and 1

      let inputs = {
        male: person.male,
        female: person.female,
        dob: round(abs(mappedDob))               // rounding and converting the values to positive number
      }

      let target = { label: person.job };
      model.addData(inputs, target);
    }

    console.log("finished collecting total : ", total);
  }
  else if (state == 'prediction') {
    model.classify({ male: 1, female: 0, dob: 0 }, gotResults);
  }
}
joeyklee commented 4 years ago

Hi @lieberscott !

const input = {
 male: person.male,
 female: person.female,
 dob: person.dob
}
const output = {job: 1}

nn.addData(input, output);
const input = {
 sex: person.sex, // 0 or 1
 dob: person.dob
}
const output = {job: 1}

nn.addData(input, output);

Hope this helps!

bomanimc commented 3 years ago

Thanks @joeyklee for the suggested fixes here! Going to go ahead and close this issue for now since it's been quiet for a while, but please feel free to reopen for further discussion.

komal1502 commented 3 years ago

I am also facing the same issue. Would you please help in solving the issue? image image image

joeyklee commented 3 years ago

Hi @komal1502 I hope you're well. Based on the screenshots you've provided, it is difficult to diagnose the issue. If it is the case that you're trying to use a model trained in the Teachable Machine, that is unfortunately not supported in the ml5.neuralNetwork function last I checked. If if is the case that you're trying to load a model trained from Teachable Machine, then that might be the issue. Otherwise, without more context, it may be hard to assist you with your error. Thanks!

komal1502 commented 3 years ago

Sir, Here is my code : https://gitlab.com/k.parulekar152/ai-personaltrainer.git . Could you please look into my problem ? As I am facing an issue in loading custom models such as model.json,model_meta.json and model.weights.bin. Its throwing me an error saying server responded with status 404.