Skip to content
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

Fix nerural network load model topology mismatch #84

Merged
merged 6 commits into from
Mar 6, 2024

Conversation

ziyuan-linn
Copy link
Member

This PR removes the JSON.stringify call on modelJsonResult.data and modelMetadata.data since Axios already returns those as strings.

This addresses issue #78.

@ziyuan-linn ziyuan-linn linked an issue Mar 4, 2024 that may be closed by this pull request
@@ -251,7 +251,7 @@ class NeuralNetwork {
const modelJsonResult = await axios.get(filesOrPath.model, {
responseType: "text",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response .data is already a string specifically because of the responseType: "text" here. If you don't specify a responseType then axios will default to parsing it as JSON.

Comment on lines 839 to 842
let modelMetadata = await axios.get(filesOrPath.metadata, {
responseType: "text",
});
modelMetadata = JSON.stringify(modelMetadata.data);
modelMetadata = JSON.parse(modelMetadata);
modelMetadata = JSON.parse(modelMetadata.data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want it as JSON then I think you could just remove the responseType: "text" and let axios parse it. Try this:

const metadataRes = await axios.get(filesOrPath.metadata);
this.meta = metadataRes.data;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @lindapaiste! @ziyuan-linn take a look at this feedback and after you make any changes as you see fit I can merge!

@@ -251,7 +251,7 @@ class NeuralNetwork {
const modelJsonResult = await axios.get(filesOrPath.model, {
responseType: "text",
});
const modelJson = JSON.stringify(modelJsonResult.data);
const modelJson = modelJsonResult.data;
// TODO: browser File() API won't be available in node env
const modelJsonFile = new File([modelJson], "model.json", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH this entire section of the code feels a bit odd and unnecessary. We are fetching the files, creating File objects, and then passing them to the TF model with tf.io.browserFiles. But TF is definitely capable of fetching files on its own so it seems like we shouldn't have to do that on our end. It seems like we should just be able to pass the URLs. That's a discussion for another time though, because maybe there's a reason that this is necessary and I'm just not seeing it.

Copy link
Member

@shiffman shiffman Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is necessary due to the way that the p5.js web editor stores the model files, but there may be a way to simplify it yes!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense. I know the p5.js web editor well and the way that it handles uploaded files is complicated and messy because there is a URL that the user sees and uses in their code, "/model/model.json", and then there is the actual URL of the file which is some long string file name on an S3 server. So when TF is looking for other files with specific names in the same directory it's not gonna find them. We might be able to define a custom TF loader, but it's not as simple as I was thinking.

@shiffman
Copy link
Member

shiffman commented Mar 6, 2024

Merging! If there are further refinements we can make to simplify the file loading but retain compatibility with the web editor, we can do so in a new branches!

@shiffman shiffman merged commit 7d3e053 into main Mar 6, 2024
@ziyuan-linn ziyuan-linn deleted the fix-neruralNetwork-load-model-topology-mismatch branch July 3, 2024 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Topology mismatch with 0.20.0.alpha releases
3 participants