【发布时间】:2020-01-23 23:39:09
【问题描述】:
免责声明:我对 JavaScript 知之甚少
我正在尝试在 Django 实例中运行 TensorflowJS,我可以成功加载 JS 以调用 tf.loadLayersModel 函数,但是该函数需要一个 json 文件(以及稍后的另一个 .bin 文件)才能运行并且该函数无法找到它。
这两个文件(json 和 bin)都在预期的静态目录中(与首先加载 JS 文件相同),但每次我加载页面时 Django 返回 Not Found: /myapp/test_model/model.json 并且浏览器返回(通过 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.
我在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'),)
目录:
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
所以文件被放入static/myapp/test_model/,然后我运行python manage.py collectstatic,文件被重新整理,并复制到myproject/static/myapp/test_model/
视图中加载的.html:(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()
}
但是,当我加载页面时,模型没有加载并且 django 控制台返回:
Not Found: /myapp/test_model/model.json
【问题讨论】: