微信小程序嵌入机器学习模型(一)

最近需要做一个微信小程序,具体就是将之前的数据建立的标准模型嵌入到小程序中,在此记录下自己的学习过程吧,这篇博文主要记录如何将python生成的神经网络模型打包成小程序支持的格式,下一篇博文将介绍如何在小程序中引用模型

 

我的电脑:windows10

                  python:3.7.0

                  tensorflow:2.2.0(使用pip在cmd终端安装)

                  tensorflowjs:1.7.4r1(使用pip在cmd终端安装)

1、确定标准模型,在这里我们就选择tensorflow官网所给出的用mnist手写数字数据集所建立的模型:

    网址:https://tensorflow.google.cn/overview

代码如下:

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)

2. a.我们在上面代码的最后加一行代码,即使用 Python API 直接导出为 TF.js 图层(Layer)格式,第一个参数是我们所建立的模型,第二个参数是贮存的位置信息

tfjs.converters.save_keras_model(model,'D://na.file')

 得到如图所示的两个文件。

 b.官网给出另外一种方式也可以将已有Keras模型转换成TF.js层(Layer)格式

tensorflowjs_converter --input_format keras \
                       path/to/my_model.h5 \
                       path/to/tfjs_target_dir

这个的运行需要我们将我们上面生成的model通过model.save命令保存在D盘,为h5格式,代码如下

import tensorflow as tf
import tensorflowjs as tfjs

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
model.save('D://mnist.h5')

这种方法我一直运行错误,一直都是tensorflowjs——converter既不是内部或外部命令,也不是可运行的程序,如果有知道原因的,还请指教!!!

 

3.利用python内置的simpleHTTPServer搭建一个简单的服务器

我的是python3,输入下面的命令:端口号任意选(这里用8800,我手机尾号)

python -m http.server 8800

接着浏览器输入http://localhost:8800/

如果需要在局域网内共享服务器,那在cmd中查询ip,键入ipconfig,将连接改为http://你的局域网ip:端口号。

详见:https://www.pianshen.com/article/5497488042/

4.在微信小程序项目中安装

npm安装第三方库
在项目目录下:

```
npm init
npm install @tensorflow/tfjs-core
npm install @tensorflow/tfjs-converter
npm install @tensorflow/tfjs-layers
npm install fetch-wechat
npm install regenerator-runtime
```

然后去微信小程序开发工具里面"构建npm"

,我是通过npm安装,需要安装到项目目录,例如你的微信小程序项目在D://weichat,你就需要安装在这里,

关于npm的安装这里也是一堆坑,具体遇到问题请参考我的专栏https://blog.csdn.net/weixin_44719615/category_10068153.html

可以直接安装nodejs,通过npm来安装,我这里出了问题,就是明明我已经卸载了nodejs,在电脑文件里也查不到该文件,我安装nodejs的时候却显示你已经安装过,然后就退出了,我没找到原因,就通过下面的步骤安装了 

基本上全是这里安装出现的坑,我是先安装的scoop,https://blog.csdn.net/weixin_44719615/article/details/106488507

然后通过scoop安装nodejs和yarn,npmhttps://blog.csdn.net/weixin_44719615/article/details/106490619

再通过npm或yarn来安装tfjs.layers库,https://blog.csdn.net/weixin_44719615/article/details/106490693

                                                            https://blog.csdn.net/weixin_44719615/article/details/106490619

 

最后需要在微信小程序中构建npm包,遇到错误请参考https://blog.csdn.net/weixin_44719615/article/details/106492594

 

推荐一个视频吧:TensorFlow.js遇到小程序

https://ke.qq.com/course/428263?taid=3607635090114791 

微信小程序入门:https://www.icourse163.org/learn/NCUT-1206419808?tid=1450238453#/learn/announce

 

 

 

剩下的步骤请参考我的下一篇博文

  • 11
    点赞
  • 113
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值