tfjs入门之mobilenet
mobilenet.html
<!doctype html>
<html lang="en">
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core@1.2.7/dist/tf-core.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-converter@1.2.7/dist/tf-converter.min.js"></script>
</head>
<body>
<script>
//const modelUrl = 'http://localhost:1111/mobilenet_model.json'
const modelUrl ='https://storage.googleapis.com/tfjs-models/savedmodel/mobilenet_v2_1.0_224/model.json';
async function fun(){
const model = await tf.loadGraphModel(modelUrl);
const zeros = tf.zeros([1, 224, 224, 3]);
model.predict(zeros).print();
}
fun()
</script>
</body>
</html>
在浏览器打开,按F12
说一说遇到的坑:
(1)指定tfjs-core和tfjs-converter版本问题,此处为1.2.7(两者版本是否需一致尚不明确),否则可能会遇到找不到函数等各种bug。
具体tfjs-core版本等信息可以参见https://cdn.jsdelivr.net/
查询方法见 这里
(2)加载本地模型时的跨域问题,解决方案见 这里