NCNet
NCNet copied to clipboard
Run the model by tensorflow.js
Do anyone know how to do this?
My steps
. Convert the model to tensorflow.js
import os
from model.basenet import basenet
from model import resolve
import tensorflow as tf
import numpy as np
import tensorflowjs as tfjs
model_name = 'basenet'
downgrade = 'bicubic'
scale = 3
image_folder = "data/"
output_folder = "original_output"
if __name__ == '__main__':
os.makedirs(output_folder, exist_ok=True)
model_dir = f"ckpt/{model_name}/model"
model = tf.keras.models.load_model(model_dir)
model.summary()
tfjs.converters.save_keras_model(model, "tfjsmodel")
. Create a http server by python(server_tf.py)
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
def test_server_with_threads():
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_my_headers()
http.server.SimpleHTTPRequestHandler.end_headers(self)
def send_my_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
http.server.test(HandlerClass=MyHTTPRequestHandler)
test_server_with_threads()
. Create a index.html to load the model
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TensorFlow.js Tutorial</title>
<!-- Import TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
<!-- Import tfjs-vis -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tfjs-vis.umd.min.js"></script>
<!-- Import the data file -->
<!-- Import the main script file -->
</head>
<body>
<script type='text/javascript'>
//wait tf.loadFrozenModel('http://localhost:8080/model.pb', 'http://localhost:8080/weights.json')
//const model = tf.loadGraphModel('http://localhost/./model.pb/model.json');
model = tf.loadLayersModel('http://localhost:8000/tfjsmodel/model.json');
</script>
</body>
</html>
. Turn on the server by python server_tf.py . Open http://0.0.0.0:8000 by firefox . Got error messages
Uncaught (in promise) Error: Unknown layer: Functional. This may be due to one of the following reasons:
1. The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().
Xm runtime.js:728
r runtime.js:728
e errors.js:47
jN generic_utils.js:242
GI serialization.js:31
t models.js:295
u runtime.js:45
_invoke runtime.js:274
e runtime.js:97
Wm runtime.js:728
o runtime.js:728
promise callback*Wm runtime.js:728
o runtime.js:728
Vm runtime.js:728
Vm runtime.js:728
iT models.js:328
aT models.js:269
t models.js:256
u runtime.js:45
_invoke runtime.js:274
e runtime.js:97
Wm runtime.js:728
o runtime.js:728
Vm runtime.js:728
Vm runtime.js:728
rT models.js:256
loadLayersModel exports.js:235
loadLayersModel exports.js:237
<anonymous> (index):26
runtime.js:728:42