小白带你去逛逛TensorFlow图像处理

TensorFlow ?

TensorFlow是一个异构分布式系统上的大规模机器学习框架,移植性好(小到移动设备如手机,大到大规模集群,都能支持),支持多种深度学习模型。根据Google的说法,TensorFlow是综合的、灵活的、可移植的、易用的,更为关键的是,它是开源的。

1.安装

$pip3 install tflite。

例如,如果您有运行Raspbian Buster(它有Python 3.7)的Raspberry Pi,请按如下方式安装Python控制盘:

1.1安装提示

 pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-macosx_10_14_x86_64.whl
 

Installing collected packages: tflite-runtime
Successfully installed tflite-runtime-2.1.0.post1

1.2 相关的依赖

Successfully installed TensorFlow-2.1.0 absl-py-0.9.0 astor-0.8.1 cachetools-4.1.0 chardet-3.0.4 gast-0.2.2 google-auth-1.14.1 google-auth-oauthlib-0.4.1 google-pasta-0.2.0 grpcio-1.28.1 h5py-2.10.0 idna-2.9 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.2.1 oauthlib-3.1.0 opt-einsum-3.2.1 protobuf-3.11.3 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.23.0 requests-oauthlib-1.3.0 rsa-4.0 scipy-1.4.1 tensorboard-2.1.1 tensorflow-estimator-2.1.0 termcolor-1.1.0 urllib3-1.25.9 werkzeug-1.0.1 wrapt-1.12.1
 

2.使用tflite运行时运行推断

2.1 下载文件beginner.ipynb

ipynb是jupyter notebook的文件

文件内容为:

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "rX8mhOLljYeM"
   },
   "source": [
    "##### Copyright 2019 The TensorFlow Authors."
   ]
  ......
 "nbformat": 4,
 "nbformat_minor": 0
}


2.2 用jupyter notebook运行 Run All

2.3 照片识别准确度达到 98%

3.给图像【衣服、裤子、鞋帽等】分类

3.1  图像标签

图像是28x28numpy数组,像素值从0到255不等。标签是一个整数数组,范围从0到9。这些对应于图像所代表的服装类别:

标签定义;标签列表如下为:

  • 0   T恤/上衣
  • 1    条裤子
  • 2   套套头衫
  • 3   件连衣裙
  • 4   层
  • 5   凉鞋
  • 6   件衬衫
  • 7   运动鞋
  • 8   袋
  • 9   踝靴

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

3.2 训练图片

在训练模型之前,让我们先研究一下数据集的格式,最关键的步骤训练数据;

数据分为:训练集合、测试集合,测试集合有10000份。

显示训练集中有60000个图像,每个图像表示为28 x 28像素:

测试集中有10000个图像。每个图像表示为28 x 28像素

3.2.1显示图片 5*5

plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()

3.3 创建一个Model 28*28 参数训练中学习的参数

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

像素展平后,网络由两个tf.keras.layers.Dense层组成。这些神经层紧密相连,或完全相连。第一致密层有128个节点(或神经元)。第二层(也是最后一层)返回长度为10的logits数组。每个节点都包含一个分数,表示当前图像属于10个类之一。

在本例中,训练数据位于train_images和train_labels数组中。
要求模型对测试集test_images数组进行预测。验证预测是否与测试标签数组中的标签匹配。

3.4 运行结果,精确度到87%

3.5 各个类型命中图

 

 

猜你喜欢

转载自blog.csdn.net/keny88888/article/details/105770457