译者 | 婉清、姗姗

编辑 | 姗姗

出品 | 人工智能头条

【人工智能头条导读】TensorFlow 是一个开放源代码软件库,用于进行高性能数值计算。借助灵活的架构,用户可以轻松地将计算工作部署到多种平台(CPU、GPU、TPU)和设备(桌面设备、服务器集群、移动设备、边缘设备等)。最近在 JS 社区中,对 TF 中 Java API 相关项目与技术的高度需求是前所未有的。

TensorFlow v1.9 

近日,TensorFlow 发表推文正式发布 TensorFlow v1.9 ,大家可以更新各自的代码啦~~在 TF 的更新文档中更新了 keras,包括一个新的基于keras的入门,一个非常适合初学者的Jupyter 笔记本,还增加了更多的实例。



其中有两个案例受到了大家的广泛关注,这个项目是通过 Colab 在 tf.keras 中训练模型,并通过TensorFlow.js 在浏览器中运行;最近在 JS 社区中,对这些相关项目的高度需求是前所未有的。之前人工智能头条也为大家介绍了一个在浏览器中通过TensorFlow.js 进行多人人脸识别与特征检测的项目,也受到大家的广泛关注。此外 TensorFlow 还给那些想了解、学习相关项目技术的关注者们推出了系列官方教学视频。

今天人工智能头条会带领大家学习这个新项目,JS 爱好者和开发者们不要错过!



另一个项目是简单的RNN网络生成文本的实践,这次作者不仅在GitHub 上分享了源码,大家还可以利用这次 v1.9 中的笔记本新功能来进行端到端的直接运行。

在 Google Colab 中看到这个项目的第一眼,就觉得真的很适合初学者研究学习。左侧可以一目了然地看 “目录” 与 “代码段”。



已经分解的源码结构,从安装、导入需要的工具,到下载读取数据,创建训练模型,最后预测模型,只要跟着一步一步来,相信大家都会学有所成。

参考链接:

https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/generative_examples/text_generation.ipynb?linkId=54107458#scrollTo=UHjdCjDuSvX_

TensorFlow v1.9 实践

前言

在这个应用中我们将完成通过识别图画来输出当前图画的名称的实践。使用 Google Colab 来训练模型,使用 TensorFlow.js 在浏览器上进行部署,直接在浏览器上运行。需要注意的一点是,务必要在测试 Google Colab 的 notebook 工具:

https://colab.research.google.com/github/zaidalyafeai/zaidalyafeai.github.io/blob/master/sketcher/Sketcher.ipynb

数据集

我们将使用 CNN 来识别不同类型的图画。CNN 将在Quick Draw数据集上进行训练。这个数据集包含了大约5000万张、345类别的图画。



管道

我们将使用 Keras 在 Google Colab 上训练模型,然后通过 TensorFlow.js (tfjs) 在浏览器上直接运行。下图向大家展示了这个项目的管道图

参考教程:

https://js.tensorflow.org/

https://github.com/tensorflow/tfjs-examples



输入

我们将使用带有tensorflow后端的keras:

 

import os
import glob
import numpy as np
from tensorflow.keras import layers
from tensorflow import keras 
import tensorflow as tf

加载数据

由于内存有限,我们不会在所有的类别进行训练。我们只使用100个类别的数据集。在Google Cloud 的quickdraw_dataset 上每个类别的数据都可以用形状表示为 [N,784] 的numpy数组,其中 N 是该特定类的的图像数量。我们首先下载数据集:

 

import urllib.request
def download():

base = 'https://storage.googleapis.com/quickdraw_dataset/full/numpy_bitmap/'
  for c in classes:
    cls_url = c.replace('_', '%20')
    path = base+cls_url+'.npy'
    print(path)
    urllib.request.urlretrieve(path, 'data/'+c+'.npy')

由于内存有限,因此每个类只能加载5000个图像到内存。另外还保留了20%的用于测试的数据。



 

def load_data(root, vfold_ratio=0.2, max_items_per_class= 5000 ):
    all_files = glob.glob(os.path.join(root, '*.npy'))

#initialize variables 
    x = np.empty([0, 784])
    y = np.empty([0])
    class_names = []

#load a subset of the data to memory 
    for idx, file in enumerate(all_files):
        data = np.load(file)
        data = data[0: max_items_per_class, :]
        labels = np.full(data.shape[0], idx)

x = np.concatenate((x, data), axis=0)
        y = np.append(y, labels)

class_name, ext = os.path.splitext(os.path.basename(file))
        class_names.append(class_name)

data = None
    labels = None

#separate into training and testing 
    permutation = np.random.permutation(y.shape[0])
    x = x[permutation, :]
    y = y[permutation]

vfold_size = int(x.shape[0]/100*(vfold_ratio*100))

x_test = x[0:vfold_size, :]
    y_test = y[0:vfold_size]

x_train = x[vfold_size:x.shape[0], :]
    y_train = y[vfold_size:y.shape[0]]
    return x_train, y_train, x_test, y_test, class_names

预处理数据

我们对数据进行预处理,为训练做准备。该模型将采用  [N, 28, 28, 1] 批次 并输出为 [N, 100] 的概率。

 

# Reshape and normalize
x_train = x_train.reshape(x_train.shape[0], image_size, image_size, 1).astype('float32')
x_test = x_test.reshape(x_test.shape[0], image_size, image_size, 1).astype('float32')

x_train /= 255.0
x_test /= 255.0

# Convert class vectors to class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

创建模型

我们将创建一个简单的CNN。参数数量越少,模型就越简单越好。因为我们是在浏览器上进行转换后运行模型,并且希望模型能够快速运行以便进行预测。下面模型包含了3个conv层和2个dense层。

 

# Define model
model = keras.Sequential()
model.add(layers.Convolution2D(16, (3, 3),
                        padding='same',
                        input_shape=x_train.shape[1:], activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Convolution2D(32, (3, 3), padding='same', activation= 'relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Convolution2D(64, (3, 3), padding='same', activation= 'relu'))
model.add(layers.MaxPooling2D(pool_size =(2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(100, activation='softmax')) 
# Train model
adam = tf.train.AdamOptimizer()
model.compile(loss='categorical_crossentropy',
              optimizer=adam,
              metrics=['top_k_categorical_accuracy'])
print(model.summary())

适配、验证和测试

在此之后,我们对模型进行了5个轮数和256个批次的训练,并进行了10%的验证划分:



 

#fit the model 
model.fit(x = x_train, y = y_train, validation_split=0.1, batch_size = 256, verbose=2, epochs=5)

#evaluate on unseen data
score = model.evaluate(x_test, y_test, verbose=0)
print('Test accuarcy: {:0.2f}%'.format(score[1] * 100))
view raw

训练的结果如下图所示:



测试准确率中第5个准确率为92.20%。

为Web格式准备模型

在我们对模型的准确率感到满意之后,我们将其保存并准备进行转换:

 

model.save('keras.h5')

安装tfjs包进行转换:

 

!pip install tensorflowjs

然后转换模型:

 

!mkdir model
!tensorflowjs_converter --input_format keras keras.h5 model/

创建了一些权重文件和包含模型体系结构的json文件。

压缩模型,准备将其下载到我们的本地计算机中:

 

!zip -r model.zip model

最后下载模型:



 

from google.colab import files
files.download('model.zip')

下面将展示如何加载模型并进行推理。假设我们有一个 300x300 大小的画布。

加载模型

要使用TensorFlow.js,请首先运行以下脚本:

 

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>

注意:需要在本地计算机上运行服务器,

然后,使用浏览器加载模型(await 关键字用于等待浏览器加载模型)

 

model = await tf.loadModel('model/model.json')
view raw

预处理

在做预测之前,我们需要先对数据进行预处理。首先从画布中获取图像数据变量 dpi 用于根据屏幕像素的密度对画布进行拉伸。

 

//the minimum boudning box around the current drawing
const mbb = getMinBox()
//cacluate the dpi of the current window 
const dpi = window.devicePixelRatio
//extract the image data 
const imgData = canvas.contextContainer.getImageData(mbb.min.x * dpi, mbb.min.y * dpi,
                               (mbb.max.x - mbb.min.x) * dpi, (mbb.max.y - mbb.min.y) * dpi);

我们将画布当前的图像数据转换为一个张量,调整大小并进行规范化。

 

function preprocess(imgData)
{
return tf.tidy(()=>{
    //convert the image data to a tensor 
    let tensor = tf.fromPixels(imgData, numChannels= 1)
    //resize to 28 x 28 
    const resized = tf.image.resizeBilinear(tensor, [28, 28]).toFloat()
    // Normalize the image 
    const offset = tf.scalar(255.0);
    const normalized = tf.scalar(1.0).sub(resized.div(offset));
    //We add a dimension to get a batch shape 
    const batched = normalized.expandDims(0)
    return batched
})
}

对于预测,我们使用 model.predict ,这将返回形状的概率 [N, 100]:

 

const pred = model.predict(preprocess(imgData)).dataSync()

然后我们可以用简单的函数来求前5个概率。

提高准确率

记住,我们的模型接受形状 [N, 28, 28,1] 的张量,绘图画布大小为 300x300 ,对于绘图来说,可能需要两个大的用于绘图,或者可能需要用户绘制小一些的图。最好只裁剪包含当前图画的框。为了做到这一点,我们通过查找左上角和右下角来提取绘图周围的最小边界框:

 

//record the current drawing coordinates       
function recordCoor(event)
{
  //get current mouse coordinate 
  var pointer = canvas.getPointer(event.e);
  var posX = pointer.x;
  var posY = pointer.y;

//record the point if withing the canvas and the mouse is pressed 
  if(posX >=0 && posY >= 0 && mousePressed)  
  {      
    coords.push(pointer) 
  } 
}

//get the best bounding box by finding the top left and bottom right cornders    
function getMinBox(){

var coorX = coords.map(function(p) {return p.x});
   var coorY = coords.map(function(p) {return p.y});
   //find top left corner 
   var min_coords = {
    x : Math.min.apply(null, coorX),
    y : Math.min.apply(null, coorY)
   }
   //find right bottom corner 
   var max_coords = {
    x : Math.max.apply(null, coorX),
    y : Math.max.apply(null, coorY)
   }
   return {
    min : min_coords,
    max : max_coords
   }
}

测试绘图

下图展示了一些第一次绘图和最高百分比级别。所有的图画都是我用鼠标绘制的。如果用一支电脑绘图笔可以获得更好的准确率:



更多参考内容可参考原文

原文链接:

https://medium.com/@alyafey22/train-on-google-colab-and-run-on-the-browser-a-case-study-8a45f9b1474e

*本文由人工智能头条整理编译,转载请联系编辑(微信1092722531)

TensorFlow 发布新版本v1.9(附应用实践教程)相关推荐

  1. 简单易用NLP框架Flair发布新版本!(附教程)

    机器之心编辑,参与:路. Flair 是 Zalando Research 开发的一款简单易用的 Python NLP 库,近日,Flair 0.4 版发布! Flair 具备以下特征: 强大的 NL ...

  2. TensorFlow发布语音识别入门教程,附1GB数据集代码

    原标题:TensorFlow发布语音识别入门教程,附1GB数据集&代码 机械鸡的鸡友经常问:如何开始入门深度学习语音和其他音频识别,例如关键字检测或语音命令. 虽然有一些伟大的开源语音识别系统 ...

  3. 【干货】百度联合清华大学发布国内首个基于AI实践的《产业智能化白皮书》(附报告全文)...

    来源:百度AI 在4月9日举行的"百度大学 Alpha 学院首期学员毕业典礼"上,百度联合清华大学发布国内首个基于 AI 实践的行业重磅报告,<产业智能化白皮书--人工智能产 ...

  4. DNN和IBatis.Net几乎同时发布新版本

    DotNetNuke发布了最新的版本4.5.0,确实让人期待了很久,据说这个版本在性能上有很大的提升. IBatis.NET几乎在同一时间也发布了新版本DataMapper 1.6.1,也有不少的改进 ...

  5. Maltego发布新版本4.2.18

    Maltego发布新版本4.2.18 Maltego是著名的信息收集工具.在新版本4.2.18中,Maltego修复了多个bug,并增加以下三个新功能. (1)导出为PDF时,增加更多选项,如支持显示 ...

  6. [图解tensorflow源码] 入门准备工作附常用的矩阵计算工具[转]

    [图解tensorflow源码] 入门准备工作 附常用的矩阵计算工具[转] Link: https://www.cnblogs.com/yao62995/p/5773142.html tensorfl ...

  7. 狗狗币协议发布新版本Dogecoin Core 1.14.3

    3月1日消息,狗狗币(DOGE)开发者发布新版本Dogecoin Core 1.14.3.新版本可提高节点同步速度,并减少默认内存池的到期时间.每当一个区块被发送至另一个节点时,就需要执行一次价格昂贵 ...

  8. Cesium矢量地图插件CesiumVectorTile 发布新版本

    Cesium矢量地图插件CesiumVectorTile 发布新版本 一周前CesiumVectorTile 1.2.1版本就已经更新到npm里了,本次更新主要解决Cesium最新版本适配问题. Ce ...

  9. doodoo.js发布1.1.0 -- 中文最佳实践Node.js Web快速开发框架,支持Koa.js, Express.js中间件。包含多项功能改进,及Bug修复。...

    2019独角兽企业重金招聘Python工程师标准>>> doodoo.js发布1.1.0 -- 中文最佳实践Node.js Web快速开发框架,支持Koa.js, Express.j ...

最新文章

  1. 汤家凤高等数学基础手写笔记-不定积分
  2. 【BZOJ4916】神犇和蒟蒻(杜教筛)
  3. opencv python教程简书_OpenCV-Python系列二:常用的图像属性
  4. Linux以下基于TCP多线程聊天室(client)
  5. mysql启动命令指定data目录_CentOS 7下MySQL的data目录更改后,使用mysqld服务启动失败...
  6. xinetd出马拯救Ftp服务器
  7. matlab中如何创建使用构架数组?
  8. 开启Hive的本地模式
  9. SpringBoot 启动过程,你不知道的秘密!
  10. hencoder学习自定义view(1)
  11. 将微信表情包保存为图片
  12. 【信号检测】基于matlab双稳随机共振微弱信号检测【含Matlab源码 1701期】
  13. 程序员的可迁移技能和经验
  14. web浮动框架 简易灯箱画廊设计
  15. 基于单片机的车速控制语音报警系统
  16. 网络工程师常用的命令整理-windows版,还不快收藏起来
  17. 浙江杭州烟花爆炸事故中受伤最重者属二级烧伤-杭州-烟花爆炸-烧伤
  18. 走进就职演说幕后:大有深意的用词
  19. 设计模式|理解单一职责原则
  20. python读取多个文件g_Python多行正则表达式+多个条目在一个g中读取一个文件

热门文章

  1. backtrack X server 启动不了
  2. 文本分类step by step(二)
  3. 计算机电磁兼容性设计方法,某型號加固计算机电磁兼容性设计.doc
  4. 自动驾驶软件工程之局部规划
  5. PyCharm使用笔记
  6. for循环 lamda python_Python中if-else判断语句、while循环语句以及for循环语句的使用...
  7. maven生命周期理解
  8. tomcat在服务器上改了8080的端口之后所带来的问题
  9. mac终端命令-----常规操作
  10. 数据事务四种隔离机制和七种传播行为