ml5js / ml5-library

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

Loading JSON into neural network seems to be broken #1252

Open KokoDoko opened 2 years ago

KokoDoko commented 2 years ago

The examples for loading color data as JSON into a neural network is broken. This example json data is given:

[
  {"r":255, "g":0, "b":0, "color": "red-ish"},
  {"r":254, "g":0, "b":0, "color": "red-ish"},
  {"r":253, "g":0, "b":0, "color": "red-ish"},
  {"r":0,   "g":0, "b":255, "color": "blue-ish"},
  {"r":0,   "g":0, "b":254, "color": "blue-ish"},
  {"r":0,   "g":0, "b":253, "color": "blue-ish"}
];

When copy>pasting this example directly, the following error occurs:

your data must be contained in an array in a property called 'entries' or 'data' of your json object

Then, when putting the array in a "data" property, as follows:

{
    "data" : [
        {
            "r": 255, "g": 10, "b": 10, "color": "red-ish"
        },
        {
            "r": 254, "g": 10, "b": 10, "color": "red-ish"
        },
       {},{},{}
    ]
}

The following error occurs:

Error: Expected units to be a positive integer, but got 0.

Then I tried adding the "r","g","b" labels to the loading options as follows:

const options = {
    dataUrl: 'colors.json',
    inputs: ['r', 'g', 'b'],
    outputs: ['color'],
    task: 'classification',
    debug: true
}

const nn = ml5.neuralNetwork(options, dataLoaded)

Now when the model starts training, I get this warning:

WARN Infinite extent for field "loss": [Infinity, -Infinity]

The training also doesn't work at all.

nn.normalizeData()
const trainingOptions = {
        epochs: 32,
        batchSize: 12
}
nn.train(trainingOptions, finishedTraining)

I can leave out the nn.normalizeData(), then no error is shown but nothing is learned. The debug graph just remains a flat line.

The strange thing is, the following weather.json example DOES work. These numbers seem to be normalised already. Is this what causes the bug?

{
    "data": [
        {
            "avg_temperature": 20,
            "humidity": 0.2,
            "rained": "no"
        },
        {
            "avg_temperature": 30,
            "humidity": 0.9,
            "rained": "yes"
        }
    ]
}
timmaed commented 2 years ago

Sorry, I'm absolute noob in javascript. I tried to track this down and to me it looks like a typo. Sometimes it's createMetadata createMetaData I think createMetaData will be correct.

It also affects the csv loading feature, so the original is somehow correct.