本实例主要介绍
本文的应用场景是对于卫星图片数据的分类,图片总共1400张,分为airplane和lake两类,也就是一个二分类的问题,所有的图片已经分别放置在2_class文件夹下的两个子文件夹中。数据集–提取码:7qb0
在该过程主要分为两个部分,第一个步骤是读取文件所在的路径,第二个步骤是使用tesorflow提供的模块对图片进行读取和封装。
本程序中我的数据集路径为/content/gdrive/My Drive/Colab Notebooks/tensorflow/DS/2_class/
from google.colab import drive
drive.mount('/content/gdrive')
import os
os.chdir("/content/gdrive/My Drive/Colab Notebooks/tensorflow")
import tensorflow as tf
print('Tensorflow version: {}'.format(tf.__version__))
from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pathlib
Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount("/content/gdrive", force_remount=True).
Tensorflow version: 2.2.0-rc4
#配置数据集路径
path_root = os.path.join(os.path.realpath("."),"DS","2_class")
data_dir = pathlib.Path(path_root)
#目录的数量
image_count = len(list(data_dir.glob('*/*.jpg')))
#显示类别
CLASS_NAMES = np.array([item.name for item in data_dir.glob('*')])
print("数据集的数量:{}\n数据集的类别:{}".format(image_count,CLASS_NAMES))
#%%
# 打印该路径下的文件
for item in data_dir.iterdir():
print(item)
import random
all_image_path = list(data_dir.glob("*/*"))
all_image_path = [str(path) for path in all_image_path]
random.shuffle(all_image_path)
数据集的数量:1400
数据集的类别:['lake' 'airplane']
/content/gdrive/My Drive/Colab Notebooks/tensorflow/DS/2_class/lake
/content/gdrive/My Drive/Colab Notebooks/tensorflow/DS/2_class/airplane
#%%确定每个图像的标签
lable_names = sorted(item.name for item in data_dir.glob("*/"))
#为每个标签分配索引,构建字典
lable_to_index = dict((name,index) for index,name in enumerate(lable_names))
print(lable_to_index)
#创建一个列表,包含每个文件的标签索引
all_image_label = [lable_to_index[pathlib.Path(path).parent.name] for path in all_image_path]
#包装为函数,以备后用
def preprocess_image(image):
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [256, 256])
image /= 255.0 # normalize to [0,1] range
return image
#加载图片
def load_and_preprocess_image(path):
image = tf.io.read_file(path)
return preprocess_image(image)
{'airplane': 0, 'lake': 1}
下面我们举一个例子,调用方法,显示一个图片实例
image_path = all_image_path[0]
label = all_image_label[0]
plt.imshow(load_and_preprocess_image(image_path))
plt.grid(False)
##plt.xlabel(caption_image(image_path))
plt.title(lable_names[label].title())
plt.axis("off")
print()
在这一部分我们只要介绍采用from_tensor_slices
方法对图片数据集进行构建,这也是比较简单而且常用的方法。
#%%构建一个tf.data.Dataset
#一个图片数据集构建 tf.data.Dataset 最简单的方法就是使用 from_tensor_slices 方法。
#将字符串数组切片,得到一个字符串数据集:
path_ds = tf.data.Dataset.from_tensor_slices(all_image_path)
print(path_ds)
#现在创建一个新的数据集,通过在路径数据集上映射 preprocess_image 来动态加载和格式化图片。
AUTOTUNE = tf.data.experimental.AUTOTUNE
image_ds = path_ds.map(load_and_preprocess_image,num_parallel_calls=AUTOTUNE)
lable_ds = tf.data.Dataset.from_tensor_slices(tf.cast(all_image_label,tf.int64))
for label in lable_ds.take(5):
print(lable_names[label.numpy()])
#%%构建一个(图片,标签)对数据集
#因为这些数据集顺序相同,可以将他们打包起来
image_label_ds = tf.data.Dataset.zip((image_ds,lable_ds))
print(image_label_ds)
#注意:当你拥有形似 all_image_labels 和 all_image_paths 的数组,tf.data.dataset.Dataset.zip 的替代方法是将这对数组切片
# =================================im============================================
# ds = tf.data.Dataset.from_tensor_slices((all_image_path,all_image_label))
# def load_and_preprocess_from_path_label(path, label):
# return load_and_preprocess_image(path),label
# image_label_ds = ds.map(load_and_preprocess_from_path_label)
# =============================================================================
lake
lake
lake
lake
airplane
我们将数据集分为训练集和验证集,训练集占80%。
#%%设置训练数据和测试数据的大小
test_count = int(image_count*0.2)
train_count = image_count - test_count
print(test_count,train_count)
#跳过test_count个
train_dataset = image_label_ds.skip(test_count)
test_dataset = image_label_ds.take(test_count)
280 1120
在对数据进行训练前,我们一般会对数据进行一定的处理
batch_size = 32
# 设置一个和数据集大小一致的 shuffle buffer size(随机缓冲区大小)以保证数据被充分打乱。
train_ds = train_dataset.shuffle(buffer_size=image_count).repeat().batch(batch_size)
test_ds = test_dataset.batch(batch_size)
#%%数据标准化
model = tf.keras.Sequential() #顺序模型
model.add(tf.keras.layers.Conv2D(64, (3, 3), input_shape=(256, 256, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(128, (3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(128, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(256, (3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(256, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(512, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(512, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(1024, (3, 3), activation='relu'))
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(tf.keras.layers.Dense(1024, activation='relu'))
model.add(tf.keras.layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
#%%
model.summary()
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc']
)
结果:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 254, 254, 64) 1792
_________________________________________________________________
conv2d_1 (Conv2D) (None, 252, 252, 64) 36928
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 126, 126, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 124, 124, 128) 73856
_________________________________________________________________
conv2d_3 (Conv2D) (None, 122, 122, 128) 147584
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 61, 61, 128) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 59, 59, 256) 295168
_________________________________________________________________
conv2d_5 (Conv2D) (None, 57, 57, 256) 590080
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 28, 28, 256) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 26, 26, 512) 1180160
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 13, 13, 512) 0
_________________________________________________________________
conv2d_7 (Conv2D) (None, 11, 11, 512) 2359808
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 5, 5, 512) 0
_________________________________________________________________
conv2d_8 (Conv2D) (None, 3, 3, 1024) 4719616
_________________________________________________________________
global_average_pooling2d (Gl (None, 1024) 0
_________________________________________________________________
dense (Dense) (None, 1024) 1049600
_________________________________________________________________
dense_1 (Dense) (None, 256) 262400
_________________________________________________________________
dense_2 (Dense) (None, 1) 257
=================================================================
Total params: 10,717,249
Trainable params: 10,717,249
Non-trainable params: 0
_________________________________________________________________
steps_per_eooch = train_count//batch_size
validation_steps = test_count//batch_size
history = model.fit(train_ds,epochs=30,steps_per_epoch=steps_per_eooch,validation_data=test_ds,validation_steps=validation_steps)
Epoch 1/30
35/35 [==============================] - 20s 565ms/step - loss: 0.8902 - acc: 0.5688 - val_loss: 0.4821 - val_acc: 0.8672
Epoch 2/30
35/35 [==============================] - 19s 556ms/step - loss: 0.7571 - acc: 0.6170 - val_loss: 0.6877 - val_acc: 0.5078
Epoch 3/30
35/35 [==============================] - 19s 556ms/step - loss: 0.6371 - acc: 0.6232 - val_loss: 0.4861 - val_acc: 0.8008
Epoch 4/30
35/35 [==============================] - 19s 555ms/step - loss: 0.4127 - acc: 0.8554 - val_loss: 0.2898 - val_acc: 0.9062
Epoch 5/30
35/35 [==============================] - 19s 557ms/step - loss: 0.4168 - acc: 0.7688 - val_loss: 0.4776 - val_acc: 0.5000
Epoch 6/30
35/35 [==============================] - 19s 555ms/step - loss: 0.4127 - acc: 0.7080 - val_loss: 0.2026 - val_acc: 0.9297
Epoch 7/30
35/35 [==============================] - 19s 556ms/step - loss: 0.2303 - acc: 0.9384 - val_loss: 0.1515 - val_acc: 0.9453
Epoch 8/30
35/35 [==============================] - 19s 556ms/step - loss: 0.1769 - acc: 0.9491 - val_loss: 0.1918 - val_acc: 0.9531
Epoch 9/30
35/35 [==============================] - 19s 556ms/step - loss: 0.1526 - acc: 0.9518 - val_loss: 0.0907 - val_acc: 0.9727
Epoch 10/30
35/35 [==============================] - 19s 556ms/step - loss: 0.1172 - acc: 0.9625 - val_loss: 0.0790 - val_acc: 0.9766
Epoch 11/30
35/35 [==============================] - 19s 556ms/step - loss: 0.1337 - acc: 0.9482 - val_loss: 0.0888 - val_acc: 0.9805
Epoch 12/30
35/35 [==============================] - 19s 556ms/step - loss: 0.1312 - acc: 0.9536 - val_loss: 0.1095 - val_acc: 0.9805
Epoch 13/30
35/35 [==============================] - 19s 555ms/step - loss: 0.4718 - acc: 0.9027 - val_loss: 0.2007 - val_acc: 0.9141
Epoch 14/30
35/35 [==============================] - 19s 554ms/step - loss: 0.1906 - acc: 0.9321 - val_loss: 0.1523 - val_acc: 0.9609
Epoch 15/30
35/35 [==============================] - 19s 554ms/step - loss: 0.1567 - acc: 0.9554 - val_loss: 0.0998 - val_acc: 0.9727
Epoch 16/30
35/35 [==============================] - 19s 555ms/step - loss: 0.1333 - acc: 0.9589 - val_loss: 0.1101 - val_acc: 0.9805
Epoch 17/30
35/35 [==============================] - 19s 554ms/step - loss: 0.1245 - acc: 0.9679 - val_loss: 0.0773 - val_acc: 0.9844
Epoch 18/30
35/35 [==============================] - 19s 554ms/step - loss: 0.1157 - acc: 0.9652 - val_loss: 0.0978 - val_acc: 0.9805
Epoch 19/30
35/35 [==============================] - 19s 553ms/step - loss: 0.1237 - acc: 0.9688 - val_loss: 0.0766 - val_acc: 0.9766
Epoch 20/30
35/35 [==============================] - 19s 554ms/step - loss: 0.1069 - acc: 0.9670 - val_loss: 0.0850 - val_acc: 0.9805
Epoch 21/30
35/35 [==============================] - 19s 554ms/step - loss: 0.1234 - acc: 0.9696 - val_loss: 0.0670 - val_acc: 0.9805
Epoch 22/30
35/35 [==============================] - 19s 553ms/step - loss: 0.0945 - acc: 0.9741 - val_loss: 0.0665 - val_acc: 0.9805
Epoch 23/30
35/35 [==============================] - 19s 553ms/step - loss: 0.1293 - acc: 0.9679 - val_loss: 0.0733 - val_acc: 0.9805
Epoch 24/30
35/35 [==============================] - 19s 553ms/step - loss: 0.1314 - acc: 0.9607 - val_loss: 0.0785 - val_acc: 0.9805
Epoch 25/30
35/35 [==============================] - 19s 554ms/step - loss: 0.1082 - acc: 0.9661 - val_loss: 0.0637 - val_acc: 0.9844
Epoch 26/30
35/35 [==============================] - 19s 554ms/step - loss: 0.1139 - acc: 0.9714 - val_loss: 0.0671 - val_acc: 0.9805
Epoch 27/30
35/35 [==============================] - 19s 553ms/step - loss: 0.1266 - acc: 0.9652 - val_loss: 0.0688 - val_acc: 0.9766
Epoch 28/30
35/35 [==============================] - 19s 553ms/step - loss: 0.0986 - acc: 0.9696 - val_loss: 0.0668 - val_acc: 0.9844
Epoch 29/30
35/35 [==============================] - 19s 553ms/step - loss: 0.0882 - acc: 0.9723 - val_loss: 0.0513 - val_acc: 0.9805
Epoch 30/30
35/35 [==============================] - 19s 554ms/step - loss: 0.0832 - acc: 0.9777 - val_loss: 0.0423 - val_acc: 0.9883
这一部分我们将展示测试集合验证集的准确度和损失的变化趋势’
history.history.keys()
plt.plot(history.epoch, history.history.get('acc'), label='acc')
plt.plot(history.epoch, history.history.get('val_acc'), label='val_acc')
plt.legend()
plt.show()
plt.plot(history.epoch, history.history.get('loss'), label='loss')
plt.plot(history.epoch, history.history.get('val_loss'), label='val_loss')
plt.legend()