0

Disclaimer: I know very little Javascript

I'm trying to run a TensorflowJS in a Django instance, I can successfully load the JS to call the tf.loadLayersModel function, however this function requires a json file (and another .bin file later) to run and the function isnt able to find it.

Both the files (json and bin) are in the expected static directory (the same that loaded the JS file in the first place), but everytime I load the page Django returns Not Found: /myapp/test_model/model.json and the browser returns (through Inspect Element) Error: Request to model.json failed with status code 404. Please verify this URL points to the model JSON of the model to load.

I'm using in settings.py:

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) 
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

Directory:

myproject/
++ static/  #(created by django)
++ __init__.py
++ settings.py
++ ...
myapp/
static/ #(where I'm putting the files)
++ myapp/
++++ test_model/
++++++ model.json
++++++ otherfile.bin
manage.py

So the files are put in the static/myapp/test_model/ and after that i run python manage.py collectstatic the files get reconized, and copy to myproject/static/myapp/test_model/

The .html loaded at the view: (localhost/myapp/test_model/)

<html>
<head>
{% load static %}

...

<script src = "{% static "myapp/test_model/main.js" %}" > </script>

</head>
<body>
...
</body>
{% block javascript %}
<script>
  start()
</script>
{% endblock %}
</html>

Javascript:

async function start() {
    model = await tf.loadLayersModel('model.json')
    model.predict(tf.zeros([1, 28, 28, 1]))
    await loadClasses()
}   

However when I load the page the model doesn't load AND django console returns: Not Found: /myapp/test_model/model.json

Justcurious
  • 1,952
  • 4
  • 11
  • 17

1 Answers1

1

Edit:

The error is probably within your await tf.loadLayersModel('model.json').

You will have to use the {% static 'myapp/test_model/model.json' %} there,

e.g. await tf.loadLayersModel({% static 'myapp/test_model/model.json' %}) but you cannot do this in your JS-file.

Store it in a variable in your template,

eg. let modelfile = "{% static 'myapp/test_model/model.json' %}";

and you can use this variable in your js-file:

await tf.loadLayersModel(modelfile);

Original answer:

You need to correctly set the STATIC_ROOT constant, so Django will find your files after you have ran collectstatic.

STATIC_ROOT = os.path.join(BASE_DIR, 'static') # Absolute path to the static directory

I suggest you change your name of the folder from where you collect your staticfiles to a different name than where you are copying them.

Also, see here for clarification between the different static-constants in Django: Difference between static STATIC_URL and STATIC_ROOT on Django

ErikR
  • 560
  • 5
  • 13
  • I should have included STATIC_ROOT in my question, sorry. I edited the question to include it now. However I would like to point out that, as I said, Django does collect my CSS and JS files from my static directory with no problem, it seems to ignore my JSON file though. – Justcurious Sep 24 '19 at 00:56
  • 1
    When you run collectstatic, it does not ignore your JSON file. You probably mean it does not find it when you are running your start-function, right? Updated my answer regarding this issue. – ErikR Sep 24 '19 at 23:10
  • Thanks for your answer, but using the var as suggested still doesn't work and Django returns ```'Not Found: /myapp/{% static 'myapp/test_model/model.json' %}'``` Which is interesting, since it adds "/myapp/" before the static reference, not sure it is supposed to do that. – Justcurious Sep 30 '19 at 00:24
  • 1
    You need to have the line `let modelfile = "{% static 'myapp/test_model/model.json' %}";` in your templatefile, not in your js. – ErikR Sep 30 '19 at 21:44