当微信小程序遇上TensorFlow - 官方文档
前一段时间为了在微信小程序中使用tensorflow.js,对tfjs-core代码做了一些修改,具体情况请参考我之前写的几篇文档:
后来,我厚着脸皮向tensorflow/tfjs-core项目提交了一个PR,不出意外,这个提交被拒了。当然谷歌的开发人员还是比较友好,给了我一个文档链接: https://mp.weixin.qq.com/wxopen/plugindevdoc?appid=wx6afed118d9e81df9 原来google有一份这样的开发指导文档,而且还是中文的,为啥没在tfjs的文档中列出??循着这份文档,我还找到了tensorflow项目组的tfjs微信小程序示例: https://github.com/tensorflow/tfjs-wechat/tree/master/demo/mobilenet 这个项目说明也是中文的,看来微信小程序虽然红红火火,但真的还是仅限于中文世界。 就如同我所预想的,按照这个文档,一定不会那么顺利,实际做下来,也确实印证了这一点,下面就谈谈几个要点:
尝试了很多次,也上网寻求帮助,始终无法解决。后来还是决定自己新建一个项目,然后在项目根目录执行: $ npm init 在后续的提示里,一直按回车键,使用默认值: This utility will walk you through creating a package.json file.
It only covers the most common items,and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (wechat-tfjs-examples)
version: (1.0.0)
description:
entry point: (app.js)
test command:
git repository: (https://github.com/mogoweb/wechat-tfjs-examples)
keywords:
author:
license: (ISC)
About write to D:workaiwechat-tfjs-examplespackage.json:
{
"name": "wechat-tfjs-examples","version": "1.0.0",0);">"description": "微信小程序中使用TensorFlow js的示例代码,pages下面每个目录一个示例。",0);">"main": "app.js",0);">"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},0);">"repository": {
"type": "git",0);">"url": "git+https://github.com/mogoweb/wechat-tfjs-examples.git"
},0);">"author": "",0);">"license": "ISC",0);">"bugs": {
"https://github.com/mogoweb/wechat-tfjs-examples/issues"
},0);">"homepage": "https://github.com/mogoweb/wechat-tfjs-examples#readme"
}
Is this OK? (yes)
在项目下多了package.json文件,往该文件中添加: "devDependencies": {
"miniprogram-api-typings": "^2.6.5-2"
},0);">"dependencies": {
"@tensorflow/tfjs-core": "1.2.2",0);">"@tensorflow/tfjs-converter": "@tensorflow/tfjs-layers": "fetch-wechat": "0.0.3"
}
接着执行: $ npm install
再点击开发工具中的?构建npm?,就可以继续下去。这时可能会弹出提示: 未找到npm包入口文件
忽略之。 3. TensorFlow.js有一个联合包 - @tensorflow/tfjs,包含了四个分npm包: tfjs-core: 基础包
tfjs-converter: GraphModel 导入和执行包
tfjs-layers: LayersModel 创建,导入和执行包
tfjs-data:数据流工具包 对于小程序而言,由于有2M的app大小限制,不建议直接使用联合包,而是按照需求加载分包。
在我的示例代码中,使用到tfjs-core和tfjs-layers包,导入代码如下: import * as tf from '@tensorflow/tfjs-layers';
as tfc '@tensorflow/tfjs-core';
小结使用tensorflow的官方方法,可以跟上tfjs的最新进展,可以在第一时间上使用最新特性,推荐使用这种方法。但我之前的尝试也不是毫无价值,tfjs官方没有增加对模型本地缓存的支持,每次都需要从网络加载模型,当然不排除以后tfjs官方也会添加这样的特性。 我把调试好的tensorflow示例放在github上,请访问: https://github.com/mogoweb/wechat-tfjs-examples 然后切换到?official?分支。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |