Skip to content

Commit e6daeee

Browse files
authored
fix json parse bug (#207)
1 parent 9aff062 commit e6daeee

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

src/NeuralNetwork/NeuralNetwork.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ class NeuralNetwork {
206206
if (filesOrPath instanceof FileList) {
207207
const files = Array.from(filesOrPath);
208208
// find the correct files
209-
const model = files.find((file) => file.name.includes(".json") && !file.name.includes("_meta"));
209+
const model = files.find(
210+
(file) => file.name.includes(".json") && !file.name.includes("_meta")
211+
);
210212
const weights = files.find((file) => file.name.includes(".bin"));
211213
// load the model
212214
this.model = await tf.loadLayersModel(
@@ -218,7 +220,7 @@ class NeuralNetwork {
218220
// Override the weights path from the JSON weightsManifest
219221
weightUrlConverter: (weightFileName) => {
220222
return filesOrPath.weights || weightFileName;
221-
}
223+
},
222224
})
223225
);
224226
} else {

src/NeuralNetwork/NeuralNetworkData.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as tf from "@tensorflow/tfjs";
22
import axios from "axios";
33
import { saveBlob } from "../utils/io";
4-
import modelLoader from '../utils/modelLoader';
4+
import modelLoader from "../utils/modelLoader";
55
import nnUtils from "./NeuralNetworkUtils";
66

77
class NeuralNetworkData {
@@ -532,7 +532,6 @@ class NeuralNetworkData {
532532
*/
533533
async loadDataFromUrl(dataUrl, inputs, outputs) {
534534
try {
535-
536535
if (dataUrl.endsWith(".csv")) {
537536
await this.loadCSV(dataUrl, inputs, outputs);
538537
} else if (dataUrl.endsWith(".json")) {
@@ -643,23 +642,21 @@ class NeuralNetworkData {
643642
);
644643
}
645644
} else {
646-
loadedData = await axios.get(filesOrPath, { responseType: "text" });
647-
const text = JSON.stringify(loadedData.data);
648-
if (nnUtils.isJsonOrString(text)) {
649-
loadedData = JSON.parse(text);
645+
let response = await axios.get(filesOrPath, { responseType: "text" });
646+
647+
if (nnUtils.isJsonOrString(response.data)) {
648+
loadedData = JSON.parse(response.data);
650649
} else {
651-
console.log(
652-
"Whoops! something went wrong. Either this kind of data is not supported yet or there is an issue with .loadData"
650+
console.error(
651+
"🟪 ml5.js error: `NeuralNetwork.loadData` only accepts JSON data."
653652
);
654653
}
655654
}
656-
657655
this.data.raw = this.findEntries(loadedData);
658-
659656
// check if a data or entries property exists
660657
if (!this.data.raw.length > 0) {
661-
console.log(
662-
'data must be a json object containing an array called "data" '
658+
console.error(
659+
"🟪 ml5.js error: `NeuralNetwork.loadData` only accepts JSON objects with a 'data' property."
663660
);
664661
}
665662
} catch (error) {
@@ -716,7 +713,7 @@ class NeuralNetworkData {
716713
file.name.includes("_meta.json")
717714
);
718715
if (!file) {
719-
console.warn('no model_meta.json file found in FileList');
716+
console.warn("no model_meta.json file found in FileList");
720717
return;
721718
}
722719
const text = await file.text();

src/NeuralNetwork/index.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import * as tf from "@tensorflow/tfjs";
22
import callCallback from "../utils/callcallback";
33
import handleArguments from "../utils/handleArguments";
4-
import { imgToPixelArray, isInstanceOfSupportedElement, } from "../utils/imageUtilities";
4+
import {
5+
imgToPixelArray,
6+
isInstanceOfSupportedElement,
7+
} from "../utils/imageUtilities";
58
import NeuralNetwork from "./NeuralNetwork";
69
import NeuralNetworkData from "./NeuralNetworkData";
710

@@ -22,7 +25,6 @@ const DEFAULTS = {
2225
};
2326
class DiyNeuralNetwork {
2427
constructor(options, callback) {
25-
2628
// Is there a better way to handle a different
2729
// default learning rate for image classification tasks?
2830
if (options.task === "imageClassification") {
@@ -227,11 +229,7 @@ class DiyNeuralNetwork {
227229
async loadDataFromUrl() {
228230
const { dataUrl, inputs, outputs } = this.options;
229231

230-
await this.neuralNetworkData.loadDataFromUrl(
231-
dataUrl,
232-
inputs,
233-
outputs
234-
);
232+
await this.neuralNetworkData.loadDataFromUrl(dataUrl, inputs, outputs);
235233

236234
// once the data are loaded, create the metadata
237235
// and prep the data for training
@@ -512,7 +510,10 @@ class DiyNeuralNetwork {
512510
finishedTrainingCb = optionsOrCallback;
513511
}
514512

515-
return callCallback(this.trainInternal(options, whileTrainingCb), finishedTrainingCb);
513+
return callCallback(
514+
this.trainInternal(options, whileTrainingCb),
515+
finishedTrainingCb
516+
);
516517
}
517518

518519
/**
@@ -579,9 +580,7 @@ class DiyNeuralNetwork {
579580
// then use those to create your architecture
580581
if (!this.neuralNetwork.isLayered) {
581582
// TODO: don't update this.options.layers - Linda
582-
this.options.layers = this.createNetworkLayers(
583-
this.options.layers
584-
);
583+
this.options.layers = this.createNetworkLayers(this.options.layers);
585584
}
586585

587586
// if the model does not have any layers defined yet
@@ -1150,7 +1149,10 @@ class DiyNeuralNetwork {
11501149
*/
11511150
saveData(name, callback) {
11521151
const args = handleArguments(name, callback);
1153-
return callCallback(this.neuralNetworkData.saveData(args.name), args.callback);
1152+
return callCallback(
1153+
this.neuralNetworkData.saveData(args.name),
1154+
args.callback
1155+
);
11541156
}
11551157

11561158
/**
@@ -1182,13 +1184,16 @@ class DiyNeuralNetwork {
11821184
*/
11831185
async save(name, callback) {
11841186
const args = handleArguments(name, callback);
1185-
const modelName = args.string || 'model';
1187+
const modelName = args.string || "model";
11861188

11871189
// save the model
1188-
return callCallback(Promise.all([
1189-
this.neuralNetwork.save(modelName),
1190-
this.neuralNetworkData.saveMeta(modelName)
1191-
]), args.callback);
1190+
return callCallback(
1191+
Promise.all([
1192+
this.neuralNetwork.save(modelName),
1193+
this.neuralNetworkData.saveMeta(modelName),
1194+
]),
1195+
args.callback
1196+
);
11921197
}
11931198

11941199
/**
@@ -1200,10 +1205,13 @@ class DiyNeuralNetwork {
12001205
* @return {Promise<void[]>}
12011206
*/
12021207
async load(filesOrPath, callback) {
1203-
return callCallback(Promise.all([
1204-
this.neuralNetwork.load(filesOrPath),
1205-
this.neuralNetworkData.loadMeta(filesOrPath)
1206-
]), callback);
1208+
return callCallback(
1209+
Promise.all([
1210+
this.neuralNetwork.load(filesOrPath),
1211+
this.neuralNetworkData.loadMeta(filesOrPath),
1212+
]),
1213+
callback
1214+
);
12071215
}
12081216

12091217
/**

0 commit comments

Comments
 (0)