Tensorflow2.* 加载和预处理数据之用 tf.data 加载磁盘图片数据(4)

Tensorflow2.* 机器学习基础知识篇:

  1. 对服装图像进行分类
  2. 使用Tensorflow Hub对未处理的电影评论数据集IMDB进行分类
  3. Keras 机器学习基础知识之对预处理的电影评论文本分类
  4. Tensorflow2.*教程之使用Auto MPG数据集构建回归模型预测燃油率

Tensorflow2.* 加载和预处理数据篇:

  1. Tensorflow2.* 加载和预处理数据之用 tf.data 加载 CSV 数据
  2. Tensorflow2.* 加载和预处理数据之用 tf.data 加载 Numpy数据
  3. Tensorflow2.* 加载和预处理数据之用 tf.data 加载pandas dataframes类型数据
  4. Tensorflow2.* 加载和预处理数据之用 tf.data 加载磁盘图片数据

今天,我们主要分享的是如何使用tensorflow2.0API加载磁盘图像数据,并转换为tf.data.Dataset对象送入模型进行训练和评估。本次实验使用的数据集是花卉数据集,里面有5中类别的花,每种花类包含600多张图片。

使用库文件

import tensorflow as tf
import pathlib
import random
import os
import IPython.display as display
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

数据集下载

DATA_URL = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
path = tf.keras.utils.get_file(fname='flower_photos',origin=DATA_URL,untar=True)
# 创建Path对象
data_root = pathlib.Path(path)

for item in data_root.iterdir():
	print(item)

下载后文件目录:

---------------flower_photos/

----------------------------------daisy/

----------------------------------dandelion/

----------------------------------roses/

----------------------------------sunflowers/

----------------------------------tulips/

获取每个类别文件下的图片路径

import random
all_image_paths = list(data_root.glob('*/*'))
all_image_paths = [str(path) for path in all_image_paths]
random.shuffle(all_image_paths)

image_count = len(all_image_paths)

获取图像描述

attributions = (data_root/"LICENSE.txt").open(encoding="utf-8").readlines()[4:]
attributions = [line.split('CC-BY') for line in attributions]
attributions = dict(attributions)
print(attributions)

def caption_image(image_path):
	image_rel = pathlib.Path(image_path).relative_to(data_root)
	image_rel = str(image_rel).replace("\\","/")
	image_rel = image_rel + " "
	return "Image (CC BY 2.0) "+ " - ".join(attributions[image_rel].split(" - ")[:-1])

随机显示四张图片及描述,具体描述从文件LICENSE.txt中获取

plt.figure(figsize=(10,10))
for n in range(1,5):
    image_path = random.choice(all_image_paths)
    img_data = np.asarray(Image.open(image_path))
    # display.display(display.Image(image_path))
    plt.subplot(2,2,n)
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plt.xlabel(caption_image(image_path))
    plt.imshow(img_data)
plt.show()

Tensorflow2.* 加载和预处理数据之用 tf.data 加载磁盘图片数据(4)_第1张图片

 获取具体类别,并将其数值化处理

label_names = sorted(item.name for item in data_root.glob("*/") if item.is_dir())

label_to_index = dict((name,index) for index, name in enumerate(label_names))
print(label_to_index)

{'daisy': 0, 'dandelion': 1, 'roses': 2, 'sunflowers': 3, 'tulips': 4}

 获取所有图片的类别

all_image_labels = [label_to_index[pathlib.Path(impath).parent.name] for impath in all_image_paths]

print("First 10 labels indices: ", all_image_labels[:10])

First 10 labels indices:  [3, 2, 3, 4, 0, 4, 4, 3, 4, 3]

解析图像数据,并将其归一化处理

def process_image(image):
	image = tf.image.decode_jpeg(image,channels=3)
	image = tf.image.resize(image,(192,192))
	image /= 255.0
	return image

def load_and_process_image(path):
	image = tf.io.read_file(path)
	return process_image(image)

将图像数据转换为Dataset处理

数据dataset化,可以方便模型训练。方便数据的处理。

ds = tf.data.Dataset.from_tensor_slices((all_image_paths, all_image_labels))

# 元组被解压缩到映射函数的位置参数中
def load_and_preprocess_from_path_label(path, label):
	return load_and_process_image(path), label

image_label_ds = ds.map(load_and_preprocess_from_path_label)
print(image_label_ds)

 要使用此数据集训练模型,要对数据进行如下处理:

  • 充分打乱。
  • 分割为 batch。
  • 永远重复。
  • 尽快提供 batch。

这里有一些注意事项

  • 顺序很重要。

    • 在 .repeat 之后 .shuffle,会在 epoch 之间打乱数据(当有些数据出现两次的时候,其他数据还没有出现过)。

    • 在 .batch 之后 .shuffle,会打乱 batch 的顺序,但是不会在 batch 之间打乱数据。

  • 在完全打乱中使用和数据集大小一样的 buffer_size(缓冲区大小)。较大的缓冲区大小提供更好的随机化,但使用更多的内存,直到超过数据集大小。

  • 在从随机缓冲区中拉取任何元素前,要先填满它。所以当 Dataset(数据集)启动的时候一个大的 buffer_size(缓冲区大小)可能会引起延迟。

  • 在随机缓冲区完全为空之前,被打乱的数据集不会报告数据集的结尾。Dataset(数据集)由 .repeat 重新启动,导致需要再次等待随机缓冲区被填满。

最后一点可以通过使用 tf.data.Dataset.apply 方法和融合过的 tf.data.experimental.shuffle_and_repeat 函数来解决:

# 使用 tf.data.Dataset.cache 在 epoch 之间轻松缓存计算结果。这是非常高效的,特别是当内存能容纳全# 部数据时。

# 在被预处理之后(解码和调整大小),图片在此被缓存了:
ds = image_label_ds.cache()
# 设置一个和数据集大小一致的 shuffle buffer size(随机缓冲区大小)以保证数据
# 被充分打乱
ds = ds.apply(
  tf.data.experimental.shuffle_and_repeat(buffer_size=image_count))

ds = ds.batch(BATCH_SIZE)

# 当模型在训练的时候,`prefetch` 使数据集在后台取得 batch。
ds = ds.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

模型迁移方式进行模型构建及训练

mobile_net = tf.keras.applications.MobileNetV2(input_shape=(192, 192, 3), include_top=False)
mobile_net.trainable=False
help(tf.keras.applications.mobilenet_v2.preprocess_input)

def change_range(image,label):
	return 2*image-1, label

keras_ds = ds.map(change_range)

image_batch, label_batch = next(iter(keras_ds))
feature_map_batch = mobile_net(image_batch)
print(feature_map_batch.shape)

model = tf.keras.Sequential([
  mobile_net,
  tf.keras.layers.GlobalAveragePooling2D(),
  tf.keras.layers.Dense(len(label_names), activation = 'softmax')])

logit_batch = model(image_batch).numpy()

print("min logit:", logit_batch.min())
print("max logit:", logit_batch.max())
print()

print("Shape:", logit_batch.shape)

model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss='sparse_categorical_crossentropy',
              metrics=["accuracy"])

steps_per_epoch = int(tf.math.ceil(len(all_image_paths)/BATCH_SIZE).numpy())
model.fit(ds, epochs=3,steps_per_epoch=steps_per_epoch)

Train for 115 steps
Epoch 1/3

  1/115 [..............................] - ETA: 26:49 - loss: 2.0788 - accuracy: 0.1250
  2/115 [..............................] - ETA: 13:57 - loss: 1.7999 - accuracy: 0.2656
  3/115 [..............................] - ETA: 9:41 - loss: 1.7991 - accuracy: 0.2604 
  4/115 [>.............................] - ETA: 7:31 - loss: 1.7441 - accuracy: 0.2969
  5/115 [>.............................] - ETA: 6:13 - loss: 1.6859 - accuracy: 0.3187
  6/115 [>.............................] - ETA: 5:21 - loss: 1.6070 - accuracy: 0.3594
  7/115 [>.............................] - ETA: 4:44 - loss: 1.6025 - accuracy: 0.3705
  8/115 [=>............................] - ETA: 4:15 - loss: 1.5744 - accuracy: 0.3828
  9/115 [=>............................] - ETA: 3:53 - loss: 1.5504 - accuracy: 0.3785
 10/115 [=>............................] - ETA: 3:35 - loss: 1.5142 - accuracy: 0.4062
 11/115 [=>............................] - ETA: 3:20 - loss: 1.5098 - accuracy: 0.4006
 12/115 [==>...........................] - ETA: 3:08 - loss: 1.4877 - accuracy: 0.4010
 13/115 [==>...........................] - ETA: 2:57 - loss: 1.4707 - accuracy: 0.4062
 14/115 [==>...........................] - ETA: 2:48 - loss: 1.4458 - accuracy: 0.4129
 15/115 [==>...........................] - ETA: 2:40 - loss: 1.4298 - accuracy: 0.4229
 16/115 [===>..........................] - ETA: 2:33 - loss: 1.3922 - accuracy: 0.4492
 17/115 [===>..........................] - ETA: 2:27 - loss: 1.3677 - accuracy: 0.4632
 18/115 [===>..........................] - ETA: 2:21 - loss: 1.3575 - accuracy: 0.4688
 19/115 [===>..........................] - ETA: 2:16 - loss: 1.3508 - accuracy: 0.4753
 20/115 [====>.........................] - ETA: 2:12 - loss: 1.3325 - accuracy: 0.4812
 21/115 [====>.........................] - ETA: 2:07 - loss: 1.3177 - accuracy: 0.4881
 22/115 [====>.........................] - ETA: 2:03 - loss: 1.2966 - accuracy: 0.4986
 23/115 [=====>........................] - ETA: 1:59 - loss: 1.2768 - accuracy: 0.5068
 24/115 [=====>........................] - ETA: 1:56 - loss: 1.2643 - accuracy: 0.5130
 25/115 [=====>........................] - ETA: 1:52 - loss: 1.2485 - accuracy: 0.5188
 26/115 [=====>........................] - ETA: 1:49 - loss: 1.2370 - accuracy: 0.5252
 27/115 [======>.......................] - ETA: 1:46 - loss: 1.2190 - accuracy: 0.5359
 28/115 [======>.......................] - ETA: 1:43 - loss: 1.2038 - accuracy: 0.5424
 29/115 [======>.......................] - ETA: 1:40 - loss: 1.1938 - accuracy: 0.5453
 30/115 [======>.......................] - ETA: 1:38 - loss: 1.1853 - accuracy: 0.5479
 31/115 [=======>......................] - ETA: 1:35 - loss: 1.1780 - accuracy: 0.5524
 32/115 [=======>......................] - ETA: 1:33 - loss: 1.1622 - accuracy: 0.5596
 33/115 [=======>......................] - ETA: 1:31 - loss: 1.1502 - accuracy: 0.5653
 34/115 [=======>......................] - ETA: 1:29 - loss: 1.1373 - accuracy: 0.5708
 35/115 [========>.....................] - ETA: 1:27 - loss: 1.1217 - accuracy: 0.5786
 36/115 [========>.....................] - ETA: 1:25 - loss: 1.1144 - accuracy: 0.5816
 37/115 [========>.....................] - ETA: 1:23 - loss: 1.0987 - accuracy: 0.5904
 38/115 [========>.....................] - ETA: 1:21 - loss: 1.0879 - accuracy: 0.5946
 39/115 [=========>....................] - ETA: 1:19 - loss: 1.0798 - accuracy: 0.5986
 40/115 [=========>....................] - ETA: 1:18 - loss: 1.0688 - accuracy: 0.6039
 41/115 [=========>....................] - ETA: 1:16 - loss: 1.0553 - accuracy: 0.6090
 42/115 [=========>....................] - ETA: 1:14 - loss: 1.0514 - accuracy: 0.6101
 43/115 [==========>...................] - ETA: 1:13 - loss: 1.0385 - accuracy: 0.6156
 44/115 [==========>...................] - ETA: 1:11 - loss: 1.0260 - accuracy: 0.6200
 45/115 [==========>...................] - ETA: 1:10 - loss: 1.0162 - accuracy: 0.6243
 46/115 [===========>..................] - ETA: 1:08 - loss: 1.0080 - accuracy: 0.6284
 47/115 [===========>..................] - ETA: 1:07 - loss: 1.0080 - accuracy: 0.6283
 48/115 [===========>..................] - ETA: 1:05 - loss: 1.0011 - accuracy: 0.6296
 49/115 [===========>..................] - ETA: 1:04 - loss: 0.9953 - accuracy: 0.6307
 50/115 [============>.................] - ETA: 1:03 - loss: 0.9883 - accuracy: 0.6350
 51/115 [============>.................] - ETA: 1:02 - loss: 0.9856 - accuracy: 0.6366
 52/115 [============>.................] - ETA: 1:00 - loss: 0.9771 - accuracy: 0.6412
 53/115 [============>.................] - ETA: 59s - loss: 0.9714 - accuracy: 0.6445 
 54/115 [=============>................] - ETA: 58s - loss: 0.9644 - accuracy: 0.6481
 55/115 [=============>................] - ETA: 57s - loss: 0.9570 - accuracy: 0.6506
 56/115 [=============>................] - ETA: 55s - loss: 0.9523 - accuracy: 0.6512
 57/115 [=============>................] - ETA: 54s - loss: 0.9501 - accuracy: 0.6524
 58/115 [==============>...............] - ETA: 53s - loss: 0.9454 - accuracy: 0.6546
 59/115 [==============>...............] - ETA: 52s - loss: 0.9390 - accuracy: 0.6568
 60/115 [==============>...............] - ETA: 51s - loss: 0.9345 - accuracy: 0.6578
 61/115 [==============>...............] - ETA: 50s - loss: 0.9284 - accuracy: 0.6598
 62/115 [===============>..............] - ETA: 48s - loss: 0.9245 - accuracy: 0.6623
 63/115 [===============>..............] - ETA: 47s - loss: 0.9213 - accuracy: 0.6637
 64/115 [===============>..............] - ETA: 46s - loss: 0.9198 - accuracy: 0.6646
 65/115 [===============>..............] - ETA: 45s - loss: 0.9170 - accuracy: 0.6644
 66/115 [================>.............] - ETA: 44s - loss: 0.9111 - accuracy: 0.6667
 67/115 [================>.............] - ETA: 43s - loss: 0.9061 - accuracy: 0.6684
 68/115 [================>.............] - ETA: 42s - loss: 0.9048 - accuracy: 0.6677
 69/115 [=================>............] - ETA: 41s - loss: 0.8975 - accuracy: 0.6712
 70/115 [=================>............] - ETA: 40s - loss: 0.8947 - accuracy: 0.6714
 71/115 [=================>............] - ETA: 39s - loss: 0.8889 - accuracy: 0.6743
 72/115 [=================>............] - ETA: 38s - loss: 0.8858 - accuracy: 0.6753
 73/115 [==================>...........] - ETA: 37s - loss: 0.8840 - accuracy: 0.6751
 74/115 [==================>...........] - ETA: 36s - loss: 0.8783 - accuracy: 0.6778
 75/115 [==================>...........] - ETA: 35s - loss: 0.8721 - accuracy: 0.6808
 76/115 [==================>...........] - ETA: 34s - loss: 0.8677 - accuracy: 0.6822
 77/115 [===================>..........] - ETA: 33s - loss: 0.8674 - accuracy: 0.6822
 78/115 [===================>..........] - ETA: 32s - loss: 0.8632 - accuracy: 0.6835
 79/115 [===================>..........] - ETA: 31s - loss: 0.8609 - accuracy: 0.6843
 80/115 [===================>..........] - ETA: 30s - loss: 0.8600 - accuracy: 0.6840
 81/115 [====================>.........] - ETA: 29s - loss: 0.8584 - accuracy: 0.6848
 82/115 [====================>.........] - ETA: 28s - loss: 0.8548 - accuracy: 0.6867
 83/115 [====================>.........] - ETA: 27s - loss: 0.8491 - accuracy: 0.6898
 84/115 [====================>.........] - ETA: 26s - loss: 0.8450 - accuracy: 0.6916
 85/115 [=====================>........] - ETA: 25s - loss: 0.8409 - accuracy: 0.6926
 86/115 [=====================>........] - ETA: 24s - loss: 0.8379 - accuracy: 0.6933
 87/115 [=====================>........] - ETA: 23s - loss: 0.8355 - accuracy: 0.6940
 88/115 [=====================>........] - ETA: 22s - loss: 0.8325 - accuracy: 0.6957
 89/115 [======================>.......] - ETA: 22s - loss: 0.8283 - accuracy: 0.6973
 90/115 [======================>.......] - ETA: 21s - loss: 0.8277 - accuracy: 0.6972
 91/115 [======================>.......] - ETA: 20s - loss: 0.8222 - accuracy: 0.6995
 92/115 [=======================>......] - ETA: 19s - loss: 0.8191 - accuracy: 0.7007
 93/115 [=======================>......] - ETA: 18s - loss: 0.8186 - accuracy: 0.7013
 94/115 [=======================>......] - ETA: 17s - loss: 0.8147 - accuracy: 0.7028
 95/115 [=======================>......] - ETA: 16s - loss: 0.8112 - accuracy: 0.7036
 96/115 [========================>.....] - ETA: 15s - loss: 0.8093 - accuracy: 0.7038
 97/115 [========================>.....] - ETA: 15s - loss: 0.8071 - accuracy: 0.7046
 98/115 [========================>.....] - ETA: 14s - loss: 0.8030 - accuracy: 0.7070
 99/115 [========================>.....] - ETA: 13s - loss: 0.8042 - accuracy: 0.7071
100/115 [=========================>....] - ETA: 12s - loss: 0.8012 - accuracy: 0.7081
101/115 [=========================>....] - ETA: 11s - loss: 0.7971 - accuracy: 0.7095
102/115 [=========================>....] - ETA: 10s - loss: 0.7949 - accuracy: 0.7102
103/115 [=========================>....] - ETA: 9s - loss: 0.7930 - accuracy: 0.7112 
104/115 [==========================>...] - ETA: 9s - loss: 0.7907 - accuracy: 0.7118
105/115 [==========================>...] - ETA: 8s - loss: 0.7909 - accuracy: 0.7122
106/115 [==========================>...] - ETA: 7s - loss: 0.7890 - accuracy: 0.7129
107/115 [==========================>...] - ETA: 6s - loss: 0.7854 - accuracy: 0.7141
108/115 [===========================>..] - ETA: 5s - loss: 0.7824 - accuracy: 0.7153
109/115 [===========================>..] - ETA: 4s - loss: 0.7793 - accuracy: 0.7162
110/115 [===========================>..] - ETA: 4s - loss: 0.7770 - accuracy: 0.7165
111/115 [===========================>..] - ETA: 3s - loss: 0.7741 - accuracy: 0.7176
112/115 [============================>.] - ETA: 2s - loss: 0.7706 - accuracy: 0.7190
113/115 [============================>.] - ETA: 1s - loss: 0.7699 - accuracy: 0.7188
114/115 [============================>.] - ETA: 0s - loss: 0.7665 - accuracy: 0.7201
115/115 [==============================] - 93s 812ms/step - loss: 0.7631 - accuracy: 0.7217
Epoch 2/3

  1/115 [..............................] - ETA: 1:14 - loss: 0.7156 - accuracy: 0.7188
  2/115 [..............................] - ETA: 1:13 - loss: 0.6408 - accuracy: 0.7344
  3/115 [..............................] - ETA: 1:13 - loss: 0.5986 - accuracy: 0.7708
  4/115 [>.............................] - ETA: 1:13 - loss: 0.5587 - accuracy: 0.7969
  5/115 [>.............................] - ETA: 1:12 - loss: 0.5195 - accuracy: 0.8188
  6/115 [>.............................] - ETA: 1:11 - loss: 0.5092 - accuracy: 0.8281
  7/115 [>.............................] - ETA: 1:10 - loss: 0.4859 - accuracy: 0.8438
  8/115 [=>............................] - ETA: 1:10 - loss: 0.4875 - accuracy: 0.8477
  9/115 [=>............................] - ETA: 1:09 - loss: 0.4721 - accuracy: 0.8472
 10/115 [=>............................] - ETA: 1:08 - loss: 0.4958 - accuracy: 0.8281
 11/115 [=>............................] - ETA: 1:08 - loss: 0.4966 - accuracy: 0.8295
 12/115 [==>...........................] - ETA: 1:07 - loss: 0.4940 - accuracy: 0.8281
 13/115 [==>...........................] - ETA: 1:06 - loss: 0.4768 - accuracy: 0.8365
 14/115 [==>...........................] - ETA: 1:05 - loss: 0.4617 - accuracy: 0.8393
 15/115 [==>...........................] - ETA: 1:05 - loss: 0.4642 - accuracy: 0.8396
 16/115 [===>..........................] - ETA: 1:04 - loss: 0.4633 - accuracy: 0.8379
 17/115 [===>..........................] - ETA: 1:04 - loss: 0.4598 - accuracy: 0.8419
 18/115 [===>..........................] - ETA: 1:03 - loss: 0.4794 - accuracy: 0.8316
 19/115 [===>..........................] - ETA: 1:03 - loss: 0.4863 - accuracy: 0.8273
 20/115 [====>.........................] - ETA: 1:03 - loss: 0.4778 - accuracy: 0.8328
 21/115 [====>.........................] - ETA: 1:02 - loss: 0.4758 - accuracy: 0.8333
 22/115 [====>.........................] - ETA: 1:02 - loss: 0.4760 - accuracy: 0.8324
 23/115 [=====>........................] - ETA: 1:01 - loss: 0.4797 - accuracy: 0.8329
 24/115 [=====>........................] - ETA: 1:01 - loss: 0.4790 - accuracy: 0.8346
 25/115 [=====>........................] - ETA: 1:00 - loss: 0.4819 - accuracy: 0.8338
 26/115 [=====>........................] - ETA: 1:00 - loss: 0.4735 - accuracy: 0.8377
 27/115 [======>.......................] - ETA: 1:00 - loss: 0.4719 - accuracy: 0.8380
 28/115 [======>.......................] - ETA: 59s - loss: 0.4742 - accuracy: 0.8371 
 29/115 [======>.......................] - ETA: 59s - loss: 0.4777 - accuracy: 0.8341
 30/115 [======>.......................] - ETA: 58s - loss: 0.4774 - accuracy: 0.8333
 31/115 [=======>......................] - ETA: 58s - loss: 0.4735 - accuracy: 0.8367
 32/115 [=======>......................] - ETA: 57s - loss: 0.4732 - accuracy: 0.8369
 33/115 [=======>......................] - ETA: 57s - loss: 0.4798 - accuracy: 0.8324
 34/115 [=======>......................] - ETA: 56s - loss: 0.4816 - accuracy: 0.8327
 35/115 [========>.....................] - ETA: 56s - loss: 0.4792 - accuracy: 0.8339
 36/115 [========>.....................] - ETA: 55s - loss: 0.4795 - accuracy: 0.8333
 37/115 [========>.....................] - ETA: 54s - loss: 0.4780 - accuracy: 0.8345
 38/115 [========>.....................] - ETA: 54s - loss: 0.4719 - accuracy: 0.8363
 39/115 [=========>....................] - ETA: 53s - loss: 0.4698 - accuracy: 0.8365
 40/115 [=========>....................] - ETA: 52s - loss: 0.4708 - accuracy: 0.8367
 41/115 [=========>....................] - ETA: 52s - loss: 0.4673 - accuracy: 0.8384
 42/115 [=========>....................] - ETA: 51s - loss: 0.4674 - accuracy: 0.8378
 43/115 [==========>...................] - ETA: 51s - loss: 0.4698 - accuracy: 0.8372
 44/115 [==========>...................] - ETA: 50s - loss: 0.4717 - accuracy: 0.8359
 45/115 [==========>...................] - ETA: 49s - loss: 0.4667 - accuracy: 0.8375
 46/115 [===========>..................] - ETA: 49s - loss: 0.4665 - accuracy: 0.8383
 47/115 [===========>..................] - ETA: 48s - loss: 0.4656 - accuracy: 0.8378
 48/115 [===========>..................] - ETA: 48s - loss: 0.4620 - accuracy: 0.8379
 49/115 [===========>..................] - ETA: 47s - loss: 0.4607 - accuracy: 0.8374
 50/115 [============>.................] - ETA: 46s - loss: 0.4622 - accuracy: 0.8369
 51/115 [============>.................] - ETA: 46s - loss: 0.4630 - accuracy: 0.8364
 52/115 [============>.................] - ETA: 45s - loss: 0.4586 - accuracy: 0.8383
 53/115 [============>.................] - ETA: 44s - loss: 0.4621 - accuracy: 0.8373
 54/115 [=============>................] - ETA: 44s - loss: 0.4607 - accuracy: 0.8380
 55/115 [=============>................] - ETA: 43s - loss: 0.4583 - accuracy: 0.8386
 56/115 [=============>................] - ETA: 42s - loss: 0.4567 - accuracy: 0.8404
 57/115 [=============>................] - ETA: 42s - loss: 0.4580 - accuracy: 0.8394
 58/115 [==============>...............] - ETA: 41s - loss: 0.4576 - accuracy: 0.8389
 59/115 [==============>...............] - ETA: 40s - loss: 0.4607 - accuracy: 0.8374
 60/115 [==============>...............] - ETA: 39s - loss: 0.4627 - accuracy: 0.8365
 61/115 [==============>...............] - ETA: 39s - loss: 0.4591 - accuracy: 0.8386
 62/115 [===============>..............] - ETA: 38s - loss: 0.4573 - accuracy: 0.8397
 63/115 [===============>..............] - ETA: 37s - loss: 0.4568 - accuracy: 0.8393
 64/115 [===============>..............] - ETA: 37s - loss: 0.4566 - accuracy: 0.8403
 65/115 [===============>..............] - ETA: 36s - loss: 0.4557 - accuracy: 0.8409
 66/115 [================>.............] - ETA: 35s - loss: 0.4598 - accuracy: 0.8390
 67/115 [================>.............] - ETA: 34s - loss: 0.4577 - accuracy: 0.8400
 68/115 [================>.............] - ETA: 34s - loss: 0.4555 - accuracy: 0.8410
 69/115 [=================>............] - ETA: 33s - loss: 0.4556 - accuracy: 0.8410
 70/115 [=================>............] - ETA: 32s - loss: 0.4546 - accuracy: 0.8415
 71/115 [=================>............] - ETA: 32s - loss: 0.4548 - accuracy: 0.8420
 72/115 [=================>............] - ETA: 31s - loss: 0.4553 - accuracy: 0.8416
 73/115 [==================>...........] - ETA: 30s - loss: 0.4534 - accuracy: 0.8420
 74/115 [==================>...........] - ETA: 30s - loss: 0.4549 - accuracy: 0.8408
 75/115 [==================>...........] - ETA: 29s - loss: 0.4561 - accuracy: 0.8404
 76/115 [==================>...........] - ETA: 28s - loss: 0.4553 - accuracy: 0.8409
 77/115 [===================>..........] - ETA: 27s - loss: 0.4561 - accuracy: 0.8409
 78/115 [===================>..........] - ETA: 27s - loss: 0.4530 - accuracy: 0.8425
 79/115 [===================>..........] - ETA: 26s - loss: 0.4520 - accuracy: 0.8430
 80/115 [===================>..........] - ETA: 25s - loss: 0.4517 - accuracy: 0.8438
 81/115 [====================>.........] - ETA: 25s - loss: 0.4515 - accuracy: 0.8441
 82/115 [====================>.........] - ETA: 24s - loss: 0.4483 - accuracy: 0.8453
 83/115 [====================>.........] - ETA: 23s - loss: 0.4484 - accuracy: 0.8456
 84/115 [====================>.........] - ETA: 23s - loss: 0.4479 - accuracy: 0.8452
 85/115 [=====================>........] - ETA: 22s - loss: 0.4458 - accuracy: 0.8460
 86/115 [=====================>........] - ETA: 21s - loss: 0.4466 - accuracy: 0.8452
 87/115 [=====================>........] - ETA: 20s - loss: 0.4478 - accuracy: 0.8445
 88/115 [=====================>........] - ETA: 20s - loss: 0.4475 - accuracy: 0.8448
 89/115 [======================>.......] - ETA: 19s - loss: 0.4477 - accuracy: 0.8452
 90/115 [======================>.......] - ETA: 18s - loss: 0.4477 - accuracy: 0.8451
 91/115 [======================>.......] - ETA: 17s - loss: 0.4466 - accuracy: 0.8455
 92/115 [=======================>......] - ETA: 17s - loss: 0.4470 - accuracy: 0.8448
 93/115 [=======================>......] - ETA: 16s - loss: 0.4486 - accuracy: 0.8438
 94/115 [=======================>......] - ETA: 15s - loss: 0.4496 - accuracy: 0.8434
 95/115 [=======================>......] - ETA: 14s - loss: 0.4489 - accuracy: 0.8434
 96/115 [========================>.....] - ETA: 14s - loss: 0.4489 - accuracy: 0.8434
 97/115 [========================>.....] - ETA: 13s - loss: 0.4487 - accuracy: 0.8438
 98/115 [========================>.....] - ETA: 12s - loss: 0.4469 - accuracy: 0.8444
 99/115 [========================>.....] - ETA: 11s - loss: 0.4475 - accuracy: 0.8434
100/115 [=========================>....] - ETA: 11s - loss: 0.4497 - accuracy: 0.8428
101/115 [=========================>....] - ETA: 10s - loss: 0.4493 - accuracy: 0.8434
102/115 [=========================>....] - ETA: 9s - loss: 0.4485 - accuracy: 0.8434 
103/115 [=========================>....] - ETA: 8s - loss: 0.4477 - accuracy: 0.8441
104/115 [==========================>...] - ETA: 8s - loss: 0.4486 - accuracy: 0.8438
105/115 [==========================>...] - ETA: 7s - loss: 0.4486 - accuracy: 0.8432
106/115 [==========================>...] - ETA: 6s - loss: 0.4500 - accuracy: 0.8429
107/115 [==========================>...] - ETA: 6s - loss: 0.4481 - accuracy: 0.8440
108/115 [===========================>..] - ETA: 5s - loss: 0.4479 - accuracy: 0.8440
109/115 [===========================>..] - ETA: 4s - loss: 0.4487 - accuracy: 0.8438
110/115 [===========================>..] - ETA: 3s - loss: 0.4477 - accuracy: 0.8443
111/115 [===========================>..] - ETA: 3s - loss: 0.4473 - accuracy: 0.8446
112/115 [============================>.] - ETA: 2s - loss: 0.4484 - accuracy: 0.8443
113/115 [============================>.] - ETA: 1s - loss: 0.4492 - accuracy: 0.8438
114/115 [============================>.] - ETA: 0s - loss: 0.4483 - accuracy: 0.8443
115/115 [==============================] - 87s 752ms/step - loss: 0.4473 - accuracy: 0.8440
Epoch 3/3

  1/115 [..............................] - ETA: 1:49 - loss: 0.3273 - accuracy: 0.8438
  2/115 [..............................] - ETA: 1:36 - loss: 0.4477 - accuracy: 0.8438
  3/115 [..............................] - ETA: 1:31 - loss: 0.5078 - accuracy: 0.7812
  4/115 [>.............................] - ETA: 1:27 - loss: 0.4972 - accuracy: 0.8125
  5/115 [>.............................] - ETA: 1:26 - loss: 0.4543 - accuracy: 0.8313
  6/115 [>.............................] - ETA: 1:24 - loss: 0.4668 - accuracy: 0.8281
  7/115 [>.............................] - ETA: 1:23 - loss: 0.4491 - accuracy: 0.8348
  8/115 [=>............................] - ETA: 1:22 - loss: 0.4434 - accuracy: 0.8359
  9/115 [=>............................] - ETA: 1:21 - loss: 0.4547 - accuracy: 0.8333
 10/115 [=>............................] - ETA: 1:20 - loss: 0.4455 - accuracy: 0.8375
 11/115 [=>............................] - ETA: 1:19 - loss: 0.4258 - accuracy: 0.8494
 12/115 [==>...........................] - ETA: 1:18 - loss: 0.4083 - accuracy: 0.8594
 13/115 [==>...........................] - ETA: 1:17 - loss: 0.3880 - accuracy: 0.8678
 14/115 [==>...........................] - ETA: 1:16 - loss: 0.3824 - accuracy: 0.8728
 15/115 [==>...........................] - ETA: 1:16 - loss: 0.3725 - accuracy: 0.8771
 16/115 [===>..........................] - ETA: 1:15 - loss: 0.3688 - accuracy: 0.8789
 17/115 [===>..........................] - ETA: 1:14 - loss: 0.3619 - accuracy: 0.8824
 18/115 [===>..........................] - ETA: 1:13 - loss: 0.3553 - accuracy: 0.8872
 19/115 [===>..........................] - ETA: 1:13 - loss: 0.3503 - accuracy: 0.8898
 20/115 [====>.........................] - ETA: 1:13 - loss: 0.3574 - accuracy: 0.8859
 21/115 [====>.........................] - ETA: 1:12 - loss: 0.3720 - accuracy: 0.8780
 22/115 [====>.........................] - ETA: 1:11 - loss: 0.3709 - accuracy: 0.8778
 23/115 [=====>........................] - ETA: 1:10 - loss: 0.3648 - accuracy: 0.8818
 24/115 [=====>........................] - ETA: 1:09 - loss: 0.3620 - accuracy: 0.8841
 25/115 [=====>........................] - ETA: 1:09 - loss: 0.3650 - accuracy: 0.8813
 26/115 [=====>........................] - ETA: 1:08 - loss: 0.3629 - accuracy: 0.8810
 27/115 [======>.......................] - ETA: 1:07 - loss: 0.3610 - accuracy: 0.8819
 28/115 [======>.......................] - ETA: 1:06 - loss: 0.3581 - accuracy: 0.8817
 29/115 [======>.......................] - ETA: 1:05 - loss: 0.3580 - accuracy: 0.8804
 30/115 [======>.......................] - ETA: 1:04 - loss: 0.3559 - accuracy: 0.8802
 31/115 [=======>......................] - ETA: 1:03 - loss: 0.3508 - accuracy: 0.8831
 32/115 [=======>......................] - ETA: 1:03 - loss: 0.3473 - accuracy: 0.8848
 33/115 [=======>......................] - ETA: 1:02 - loss: 0.3482 - accuracy: 0.8826
 34/115 [=======>......................] - ETA: 1:01 - loss: 0.3441 - accuracy: 0.8842
 35/115 [========>.....................] - ETA: 1:00 - loss: 0.3412 - accuracy: 0.8857
 36/115 [========>.....................] - ETA: 59s - loss: 0.3442 - accuracy: 0.8837 
 37/115 [========>.....................] - ETA: 59s - loss: 0.3422 - accuracy: 0.8834
 38/115 [========>.....................] - ETA: 58s - loss: 0.3430 - accuracy: 0.8824
 39/115 [=========>....................] - ETA: 57s - loss: 0.3459 - accuracy: 0.8814
 40/115 [=========>....................] - ETA: 57s - loss: 0.3464 - accuracy: 0.8797
 41/115 [=========>....................] - ETA: 56s - loss: 0.3507 - accuracy: 0.8765
 42/115 [=========>....................] - ETA: 55s - loss: 0.3474 - accuracy: 0.8780
 43/115 [==========>...................] - ETA: 55s - loss: 0.3473 - accuracy: 0.8772
 44/115 [==========>...................] - ETA: 54s - loss: 0.3490 - accuracy: 0.8764
 45/115 [==========>...................] - ETA: 53s - loss: 0.3499 - accuracy: 0.8757
 46/115 [===========>..................] - ETA: 52s - loss: 0.3534 - accuracy: 0.8743
 47/115 [===========>..................] - ETA: 51s - loss: 0.3542 - accuracy: 0.8757
 48/115 [===========>..................] - ETA: 50s - loss: 0.3550 - accuracy: 0.8737
 49/115 [===========>..................] - ETA: 49s - loss: 0.3573 - accuracy: 0.8744
 50/115 [============>.................] - ETA: 48s - loss: 0.3562 - accuracy: 0.8744
 51/115 [============>.................] - ETA: 47s - loss: 0.3516 - accuracy: 0.8768
 52/115 [============>.................] - ETA: 47s - loss: 0.3491 - accuracy: 0.8780
 53/115 [============>.................] - ETA: 46s - loss: 0.3497 - accuracy: 0.8774
 54/115 [=============>................] - ETA: 45s - loss: 0.3488 - accuracy: 0.8779
 55/115 [=============>................] - ETA: 44s - loss: 0.3494 - accuracy: 0.8784
 56/115 [=============>................] - ETA: 43s - loss: 0.3480 - accuracy: 0.8795
 57/115 [=============>................] - ETA: 43s - loss: 0.3465 - accuracy: 0.8794
 58/115 [==============>...............] - ETA: 42s - loss: 0.3477 - accuracy: 0.8788
 59/115 [==============>...............] - ETA: 41s - loss: 0.3487 - accuracy: 0.8787
 60/115 [==============>...............] - ETA: 40s - loss: 0.3475 - accuracy: 0.8786
 61/115 [==============>...............] - ETA: 39s - loss: 0.3480 - accuracy: 0.8786
 62/115 [===============>..............] - ETA: 39s - loss: 0.3498 - accuracy: 0.8775
 63/115 [===============>..............] - ETA: 38s - loss: 0.3513 - accuracy: 0.8770
 64/115 [===============>..............] - ETA: 37s - loss: 0.3487 - accuracy: 0.8779
 65/115 [===============>..............] - ETA: 36s - loss: 0.3477 - accuracy: 0.8784
 66/115 [================>.............] - ETA: 36s - loss: 0.3490 - accuracy: 0.8778
 67/115 [================>.............] - ETA: 35s - loss: 0.3483 - accuracy: 0.8773
 68/115 [================>.............] - ETA: 34s - loss: 0.3495 - accuracy: 0.8773
 69/115 [=================>............] - ETA: 33s - loss: 0.3494 - accuracy: 0.8764
 70/115 [=================>............] - ETA: 32s - loss: 0.3536 - accuracy: 0.8732
 71/115 [=================>............] - ETA: 32s - loss: 0.3559 - accuracy: 0.8728
 72/115 [=================>............] - ETA: 31s - loss: 0.3579 - accuracy: 0.8728
 73/115 [==================>...........] - ETA: 30s - loss: 0.3566 - accuracy: 0.8729
 74/115 [==================>...........] - ETA: 29s - loss: 0.3551 - accuracy: 0.8737
 75/115 [==================>...........] - ETA: 29s - loss: 0.3552 - accuracy: 0.8742
 76/115 [==================>...........] - ETA: 28s - loss: 0.3535 - accuracy: 0.8750
 77/115 [===================>..........] - ETA: 27s - loss: 0.3523 - accuracy: 0.8762
 78/115 [===================>..........] - ETA: 26s - loss: 0.3545 - accuracy: 0.8754
 79/115 [===================>..........] - ETA: 26s - loss: 0.3554 - accuracy: 0.8754
 80/115 [===================>..........] - ETA: 25s - loss: 0.3542 - accuracy: 0.8766
 81/115 [====================>.........] - ETA: 24s - loss: 0.3547 - accuracy: 0.8762
 82/115 [====================>.........] - ETA: 23s - loss: 0.3538 - accuracy: 0.8769
 83/115 [====================>.........] - ETA: 23s - loss: 0.3541 - accuracy: 0.8765
 84/115 [====================>.........] - ETA: 22s - loss: 0.3532 - accuracy: 0.8769
 85/115 [=====================>........] - ETA: 21s - loss: 0.3541 - accuracy: 0.8765
 86/115 [=====================>........] - ETA: 21s - loss: 0.3562 - accuracy: 0.8750
 87/115 [=====================>........] - ETA: 20s - loss: 0.3579 - accuracy: 0.8746
 88/115 [=====================>........] - ETA: 19s - loss: 0.3566 - accuracy: 0.8754
 89/115 [======================>.......] - ETA: 18s - loss: 0.3557 - accuracy: 0.8757
 90/115 [======================>.......] - ETA: 18s - loss: 0.3585 - accuracy: 0.8743
 91/115 [======================>.......] - ETA: 17s - loss: 0.3615 - accuracy: 0.8740
 92/115 [=======================>......] - ETA: 16s - loss: 0.3610 - accuracy: 0.8740
 93/115 [=======================>......] - ETA: 15s - loss: 0.3609 - accuracy: 0.8740
 94/115 [=======================>......] - ETA: 15s - loss: 0.3609 - accuracy: 0.8737
 95/115 [=======================>......] - ETA: 14s - loss: 0.3626 - accuracy: 0.8730
 96/115 [========================>.....] - ETA: 13s - loss: 0.3618 - accuracy: 0.8734
 97/115 [========================>.....] - ETA: 13s - loss: 0.3611 - accuracy: 0.8734
 98/115 [========================>.....] - ETA: 12s - loss: 0.3612 - accuracy: 0.8728
 99/115 [========================>.....] - ETA: 11s - loss: 0.3605 - accuracy: 0.8731
100/115 [=========================>....] - ETA: 10s - loss: 0.3599 - accuracy: 0.8734
101/115 [=========================>....] - ETA: 10s - loss: 0.3613 - accuracy: 0.8728
102/115 [=========================>....] - ETA: 9s - loss: 0.3619 - accuracy: 0.8722 
103/115 [=========================>....] - ETA: 8s - loss: 0.3616 - accuracy: 0.8723
104/115 [==========================>...] - ETA: 7s - loss: 0.3620 - accuracy: 0.8726
105/115 [==========================>...] - ETA: 7s - loss: 0.3608 - accuracy: 0.8729
106/115 [==========================>...] - ETA: 6s - loss: 0.3639 - accuracy: 0.8718
107/115 [==========================>...] - ETA: 5s - loss: 0.3634 - accuracy: 0.8724
108/115 [===========================>..] - ETA: 5s - loss: 0.3638 - accuracy: 0.8721
109/115 [===========================>..] - ETA: 4s - loss: 0.3639 - accuracy: 0.8721
110/115 [===========================>..] - ETA: 3s - loss: 0.3641 - accuracy: 0.8716
111/115 [===========================>..] - ETA: 2s - loss: 0.3638 - accuracy: 0.8713
112/115 [============================>.] - ETA: 2s - loss: 0.3654 - accuracy: 0.8705
113/115 [============================>.] - ETA: 1s - loss: 0.3653 - accuracy: 0.8706
114/115 [============================>.] - ETA: 0s - loss: 0.3664 - accuracy: 0.8703
115/115 [==============================] - 82s 713ms/step - loss: 0.3656 - accuracy: 0.8709

至此,我们完成了从磁盘读取图片数据,并将其和对应的label通过元组的方式转换为dataset对象,来完成送入模型,模型训练及评估。下面,我们介绍另一种读取图片文件的格式TFRecord。

TFRecord 文件 

1. TFRecord 文件是一种用来存储一串二进制 blob 的简单格式。通过将多个示例打包进同一个文件内,TensorFlow 能够一次性读取多个示例。可以加快图片的读取效率,从而提高网络训练速度。

首先,从原始图片数据中构建出一个 TFRecord 文件:

image_ds = tf.data.Dataset.from_tensor_slices(all_image_paths).map(tf.io.read_file)
tfrec = tf.data.experimental.TFRecordWriter('images.tfrec')
tfrec.write(image_ds)

接着,构建一个从 TFRecord 文件读取的数据集,并使用你之前定义的 preprocess_image 函数对图像进行解码/重新格式化:

image_ds = tf.data.TFRecordDataset('images.tfrec').map(preprocess_image)

压缩该数据集和之前定义的标签数据集以得到期望的 (图片,标签) 对:

ds = tf.data.Dataset.zip((image_ds, label_ds))
ds = ds.apply(
  tf.data.experimental.shuffle_and_repeat(buffer_size=image_count))
ds=ds.batch(BATCH_SIZE).prefetch(AUTOTUNE)
ds

2. 为了进一步加快图片处理速度,我们可以为 TFRecord 文件省去一些预处理过程,首先像之前一样制作一个处理过的图片数据集:

paths_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)
image_ds = paths_ds.map(load_and_preprocess_image)
image_ds

将此序列化至一个 TFRecord 文件你首先将该 tensor(张量)数据集转化为一个字符串数据集

ds = image_ds.map(tf.io.serialize_tensor)
tfrec = tf.data.experimental.TFRecordWriter('images.tfrec')
tfrec.write(ds)

 有了被缓存的预处理,就能从 TFrecord 文件高效地加载数据——只需记得在使用它之前反序列化:

ds = tf.data.TFRecordDataset('images.tfrec')

def parse(x):
  result = tf.io.parse_tensor(x, out_type=tf.float32)
  result = tf.reshape(result, [192, 192, 3])
  return result

ds = ds.map(parse, num_parallel_calls=AUTOTUNE)

 这就是TFRecord文件使用。

你可能感兴趣的:(加载和预处理数据,Tensorflow2.0)