张春成
2022/12/14阅读:28主题:默认主题
便携的深度网络
便携的深度网络
最近 ChatGPT 从实验室走到了千家万户,这说明虽然深度网络是极其依赖于计算资源的服务方式,但只要通过合适的技术途径,是可以实现轻计算终端接入的。在神经网络计算功能日益强大的今天,这是复杂计算落地的必由之路。因此,本文提供一个 DEMO,将预训练好的轻量图像计算网络直接放在前端进行图像识别计算。
本文开源代码可见我的前端代码库
The TensorFlow in Javascript[1]
轻量模型
本文使用的前端模型是托管于 TFHub 的 mobilenet-ssd 模型
mobilenet-ssd - OpenVINO™ documentation[2]
它的准确性不怎么样,但胜在重量轻
The accuracy results were obtained on test data from VOC2007 dataset.
Metric Value mAP 67.00%
比它准确性高的是 mobilenet-yolo-v4-syg 模型,但我没有找到它的 JS 版本。
mobilenet-yolo-v4-syg - OpenVINO™ documentation[3]
前端实现
为了在前端运行 mobilenet-ssd 模型,我使用 TensorFlow 的 JS 版本
@tensorflow/tfjs[4]
在前端可以用简单的代码对 TF 和 NET 进行整合
// The detections are the detections
{
var img = '[target img]';
var tf, ssdMobileNet, imageTensor, detections;
tf = require("@tensorflow/tfjs@4.0.0/dist/tf.min.js");
ssdMobileNet = {
const ssdMobileNetPath =
"https://tfhub.dev/tensorflow/tfjs-model/ssd_mobilenet_v2/1/default/1";
return tf.loadGraphModel(ssdMobileNetPath, { fromTFHub: true });
}
imageTensor = tf.browser.fromPixels(img);
detections = ssdMobileNet.executeAsync(imageTensor.expandDims());
return detections
}

Example
当然,由于模型不靠谱,因此它可能会产生一些意外的错误。

Example2

Example3
参考资料
The TensorFlow in Javascript: https://observablehq.com/@listenzcc/the-tensorflow-in-javascript
[2]mobilenet-ssd - OpenVINO™ documentation: https://docs.openvino.ai/latest/omz_models_model_mobilenet_ssd.html
[3]mobilenet-yolo-v4-syg - OpenVINO™ documentation: https://docs.openvino.ai/latest/omz_models_model_mobilenet_yolo_v4_syg.html
[4]@tensorflow/tfjs: https://www.npmjs.com/package/@tensorflow/tfjs
作者介绍