TensorFlow2.0 教程-图像分类

Tensorflow 2.0 教程持续更新: https://blog.csdn.net/qq_31456593/article/details/88606284

TensorFlow 2.0 教程- Keras 快速入门
TensorFlow 2.0 教程-keras 函数api
TensorFlow 2.0 教程-使用keras训练模型
TensorFlow 2.0 教程-用keras构建自己的网络层
TensorFlow 2.0 教程-keras模型保存和序列化
TensorFlow 2.0 教程-eager模式
TensorFlow 2.0 教程-Variables
TensorFlow 2.0 教程–AutoGraph

TensorFlow 2.0 深度学习实践

TensorFlow2.0 教程-图像分类
TensorFlow2.0 教程-文本分类
TensorFlow2.0 教程-过拟合和欠拟合
TensorFlow2.0教程-结构化数据分类
TensorFlow2.0教程-回归
TensorFlow2.0教程-保持和读取模型

TensorFlow 2.0 基础网络结构

TensorFlow2教程-基础MLP网络
TensorFlow2教程-mlp及深度学习常见技巧合
TensorFlow2教程-基础CNN网络
TensorFlow2教程-CNN变体网络
TensorFlow2教程-文本卷积
TensorFlow2教程-使用预训练CNN模型

完整tensorflow2.0教程代码请看tensorflow2.0:中文教程tensorflow2_tutorials_chinese(欢迎star)

1.获取Fashion MNIST数据集

本指南使用Fashion MNIST数据集,该数据集包含10个类别中的70,000个灰度图像。 图像显示了低分辨率(28 x 28像素)的单件服装,如下所示:

Fashion MNIST旨在替代经典的MNIST数据集,通常用作计算机视觉机器学习计划的“Hello,World”。

我们将使用60,000张图像来训练网络和10,000张图像,以评估网络学习图像分类的准确程度。

(train_images, train_labels), (test_images, test_labels) = keras.datasets.fashion_mnist.load_data()

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

Label Class
0 T-shirt/top
1 Trouser
2 Pullover
3 Dress
4 Coat
5 Sandal
6 Shirt
7 Sneaker
8 Bag
9 Ankle boot

每个图像都映射到一个标签。 由于类名不包含在数据集中,因此将它们存储在此处以便在绘制图像时使用:

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

2.探索数据

让我们在训练模型之前探索数据集的格式。 以下显示训练集中有60,000个图像,每个图像表示为28 x 28像素:

print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)
(60000, 28, 28)
(60000,)
(10000, 28, 28)
(10000,)

3.处理数据

图片展示

plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

train_images = train_images / 255.0test_images = test_images / 255.0
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()

4.构造网络

model = keras.Sequential(
[layers.Flatten(input_shape=[28, 28]),layers.Dense(128, activation='relu'),layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])

5.训练与验证

model.fit(train_images, train_labels, epochs=5)
Epoch 1/5
60000/60000 [==============================] - 3s 58us/sample - loss: 0.4970 - accuracy: 0.8264
Epoch 2/5
60000/60000 [==============================] - 3s 43us/sample - loss: 0.3766 - accuracy: 0.8651
Epoch 3/5
60000/60000 [==============================] - 3s 42us/sample - loss: 0.3370 - accuracy: 0.8777
Epoch 4/5
60000/60000 [==============================] - 3s 42us/sample - loss: 0.3122 - accuracy: 0.8859
Epoch 5/5
60000/60000 [==============================] - 3s 42us/sample - loss: 0.2949 - accuracy: 0.8921
model.evaluate(test_images, test_labels)
[0.3623474566936493, 0.8737]

6.预测

predictions = model.predict(test_images)
print(predictions[0])
print(np.argmax(predictions[0]))
print(test_labels[0])
[2.1831402e-05 1.0357383e-06 1.0550731e-06 1.3231372e-06 8.0873624e-062.6805745e-02 1.2466960e-05 1.6174167e-01 1.4259206e-04 8.1126428e-01]
9
9
def plot_image(i, predictions_array, true_label, img):predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]plt.grid(False)plt.xticks([])plt.yticks([])plt.imshow(img, cmap=plt.cm.binary)predicted_label = np.argmax(predictions_array)if predicted_label == true_label:color = 'blue'else:color = 'red'plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],100*np.max(predictions_array),class_names[true_label]),color=color)def plot_value_array(i, predictions_array, true_label):predictions_array, true_label = predictions_array[i], true_label[i]plt.grid(False)plt.xticks([])plt.yticks([])thisplot = plt.bar(range(10), predictions_array, color="#777777")plt.ylim([0, 1]) predicted_label = np.argmax(predictions_array)thisplot[predicted_label].set_color('red')thisplot[true_label].set_color('blue')
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
plt.show()

# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):plt.subplot(num_rows, 2*num_cols, 2*i+1)plot_image(i, predictions, test_labels, test_images)plt.subplot(num_rows, 2*num_cols, 2*i+2)plot_value_array(i, predictions, test_labels)
plt.show()

img = test_images[0]img = (np.expand_dims(img,0))print(img.shape)
predictions_single = model.predict(img)print(predictions_single)
plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
(1, 28, 28)
[[2.1831380e-05 1.0357381e-06 1.0550700e-06 1.3231397e-06 8.0873460e-062.6805779e-02 1.2466959e-05 1.6174166e-01 1.4259205e-04 8.1126422e-01]]

TensorFlow2.0 教程-图像分类相关推荐

  1. TensorFlow2.0教程-使用keras训练模型

    TensorFlow2.0教程-使用keras训练模型 Tensorflow 2.0 教程持续更新: https://blog.csdn.net/qq_31456593/article/details ...

  2. tensorflow2.0教程- Keras 快速入门

    tensorflow2.0教程-tensorflow.keras 快速入门 Tensorflow 2.0 教程持续更新: https://blog.csdn.net/qq_31456593/artic ...

  3. TensorFlow2.0教程-keras 函数api

    TensorFlow2.0教程-keras 函数api Tensorflow 2.0 教程持续更新: https://blog.csdn.net/qq_31456593/article/details ...

  4. TensorFlow2.0教程-使用RNN实现文本分类

    TensorFlow2.0教程-使用RNN实现文本分类 原文地址:https://blog.csdn.net/qq_31456593/article/details/89923645 Tensorfl ...

  5. 神经网络与深度学习理论,tensorflow2.0教程,cnn

    *免责声明: 1\此方法仅提供参考 2\搬了其他博主的操作方法,以贴上路径. 3* 场景一:神经网络与深度学习理论 场景二:tensorflow的安装 场景三:numpy包介绍 场景四:机器学习基础 ...

  6. pip更新失败_最全Tensorflow2.0 入门教程持续更新

    最全Tensorflow 2.0 入门教程持续更新: Doit:最全Tensorflow 2.0 入门教程持续更新​zhuanlan.zhihu.com 完整tensorflow2.0教程代码请看ht ...

  7. TensorFlow 2.0 教程

    最全 TensorFlow2.0 教程-持续更新 原文地址:https://blog.csdn.net/qq_31456593/article/details/88606284 最新tensorflo ...

  8. tensorflow2.0 GPU 版本安装测试教程及新特性初探

    安装与测试 TensorFlow2.0安装: pip install tensorflow-gpu==2.2.0 -i https://pypi.douban.com/simple/ conda in ...

  9. 001-TensorFlow 2.0 教程-Transformer

    TensorFlow 2.0 教程-Transformer 原文地址:https://blog.csdn.net/qq_31456593/article/details/89923913 Tensor ...

最新文章

  1. python 排名函数_一个危险的Python函数,不推荐使用
  2. POI 使用替换字符方式进行模板生成word
  3. 简单的Postman,硬是玩出花!我能咋办
  4. 使用.NET Core进行Linux编程3:简介和第2章
  5. android Service Binder交互通信实例
  6. 使用模板实现asp代码和页面分离_asp技巧
  7. 为什么div设置其border无效?
  8. 1个鼠标和1个键盘控制2台电脑(windows和linux系统)
  9. TD式创新:中国标准横空出世 回归主流的TDD LTE
  10. excel柱状图自定x轴y轴_《excel柱状图自定x轴y轴》 如何更改EXCEL 图表中X轴的名称...
  11. 2017京东实习生笔试题之异或
  12. 电商运营如何打造爆品?
  13. ABAP CDS(Core Data Service)的创建和使用
  14. 电脑装双系统有什么坏处?可不只是速度变慢!
  15. [激光原理与应用-39]:《光电检测技术-6》- 光干涉的原理与基础
  16. 求1!+2!+3!+…+n!(2种方式)
  17. PT与PX,em(%)区别
  18. 面试官:说说微信小程序的登录流程?
  19. mvc中js是如何调用HTML的,MVC中javascript直接调用Model
  20. 【高等代数】第一章:多项式部分【1】

热门文章

  1. CVE-2020-1938漏洞复现(文末附EXP代码)
  2. MIT开源协议,多端适用的租房小程序,带完整的管理员后台
  3. 应用计算机软件matlab使用迭代法仿真激光谐振腔模式_【军工央企,津贴10万,包工作餐】中国航天科工集团光量子技术及应用总体部人才招聘...
  4. 农村创业最新项目有哪些?适合青年农民创业!
  5. 中国联通物联网连接服务能力介绍
  6. SprngBoot Admin (应用监控)
  7. STM32单片机(六)TIM定时器 -> 第一节:TIM定时中断
  8. 光电二极管(Photo-Diode)工作原理 与输出电压计算
  9. 热红外地表发射率遥感反演研究
  10. Matlab之矩阵分析基本应用(一)