I’m trying to load a tensorflow.js model into my PC scene. This is my directory structure -
My code is as follows -
/*jshint esversion: 6*/
let QuantimeDnn = pc.createScript('quantimeDnn');
const predict = async function() {
let data = tf.tensor([22, 18, 15, 9, 18, 5, 1, 1, 3]);
const model = await tf.loadLayersModel('model.json');
alert(model.predict(data));
}
predict();
// swap method called for script hot-reloading
// inherit your script state here
// QuantimeDnn.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/
I am getting a 404 error like this -
From the error, I understand that the path to the model.json file is incorrect, and therefore tf.js isn’t able to load the model, and by extension run the predict() call. How am I supposed to give the path to the model.json file? I simply specified the name of the file since it is in the same directory as the script, so how do I proceed now to eliminate the error?
@yaustar, I get this -
I tried doing - let modelAsset = this.app.assets.find("model", 'json');, what’s wrong with the code? I’ve also tried replacing model with model.json to get the same error, so I’m sure it isn’t that.
EDIT - I can’t seem to be able to access the this keyword. I tried declaring let app = this; globally, but even that doesn’t solve the issue. I can’t even alert a basic this.app.root.name. I have to use an async function for the tensorflow model to work correctly, and thus cannot use the regular init function, so what’s the best way to fix this?
You are in an anonymous function outside a pc.script class. As Leonidas has mentioned, you will have to get a reference to the pc.Application instance and then access the assets registry there.
You ca find by name or asset id (which is probably faster).
Alright, I’ve got it to return the URL, but now instead of a 404, I get a 500(Server Error). This is the dump.
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
io_utils.js:169 Uncaught (in promise) RangeError: byte length of Float32Array should be a multiple of 4
at new Float32Array (<anonymous>)
at yw (io_utils.js:169)
at oT (models.js:334)
at models.js:316
at u (runtime.js:45)
at Generator._invoke (runtime.js:274)
at Generator.forEach.t.<computed> [as next] (runtime.js:97)
at Wm (runtime.js:728)
at o (runtime.js:728)
@yaustar, just restored changes. This is odd. I’m getting a completely different error now despite just restoring a checkpoint. Shouldn’t normally happen right?
@yaustar I tried running the same model on my localhost, it works perfectly, so I don’t think this is a setup issue. I am following the tutorial given here - https://www.tensorflow.org/js/tutorials/conversion/import_keras. If you could try to repro the bug, it would be great, because I am certain it’s a PlayCanvas issue now that I’ve seen it work on localhost. If required, I can send the .h5 file for the model on DM, let me know if you need it.