【深度学习】利⽤tensorflow2.0卷积神经⽹络进⾏卫星图⽚分
类实例操作详解
本⽂的应⽤场景是对于卫星图⽚数据的分类,图⽚总共1400张,分为airplane和lake两类,也就是⼀个⼆分类的问题,所有的图⽚已经分别放置在2_class⽂件夹下的两个⼦⽂件夹中。下⾯将从这个实例的详细拆解中,理解tensorflow2.0对于数据的处理过程。⽂章的案例数据和代码附在最后,通过点解链接可以直接在gothub上获取。
1 导⼊库
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
import pathlib
print(tf.__version__,tf.test.is_gpu_available())#tensorflow版本,gpu加速
('2.0.0-alpha0', True)
2 数据处理
2.1 加载数据⽂件⽬录
data_dir = "D:/Program Files (x86)/tensorflow-data/dataset/2_class"
2.2 设置数据路径对象
data_root = pathlib.Path(data_dir)
print(data_root)#⽣成⼀个路径对象
WindowsPath('D:/Program Files (x86)/tensorflow-data/dataset/2_class')
2.3 路径对象迭代输出
for item in data_root.iterdir():#对⾥⾯所有的⽂件进⾏迭代
print(item)
D:\Program Files (x86)\tensorflow-data\dataset\2_class\airplane
D:\Program Files (x86)\tensorflow-data\dataset\2_class\lake
2.4 加载所有图⽚数据路径,转变路径格式
all_image_paths = list(data_root.glob('*/*'))
image_count = len(all_image_paths)
print(image_count)
1400
print(all_image_paths[:3])
[WindowsPath('D:/Program Files (x86)/tensorflow-data/dataset/2_class/airplane/airplane_001.jpg'), WindowsPath('D:/Program Files (x86)/tensorflow-data/dataset/2_class/airplane/airplane_002.jpg'), WindowsPath('D:/Program Files (x86)/tensorflow-data/dataset/2_class/airplane/airplane_003.jpg')]
all_image_paths = [str(path) for path in all_image_paths]
print(all_image_paths[:3])
['D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\airplane\\airplane_001.jpg',
'D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\airplane\\airplane_002.jpg',
'D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\airplane\\airplane_003.jpg']
2.5 查看图⽚数据路径并进⾏乱序排列
import random
random.shuffle(all_image_paths)
print(all_image_path[:3])
tensorflow版本选择['D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\airplane\\airplane_437.jpg',
'D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\lake\\lake_553.jpg',
'D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\airplane\\airplane_293.jpg']
2.6 标签预处理
label_names = sorted(item.name for item in data_root.glob('*/') if item.is_dir())
print(label_names)
['airplane', 'lake']
2.7 确定标签
label_to_index = dict((name, index) for index,name in enumerate(label_names))
print(label_to_index)
{'airplane': 0, 'lake': 1}#为了模型输出,便于分类
2.8 贴标签
#根据图⽚所在路径的⽂件夹进⾏贴标签
all_image_labels = [label_to_index[pathlib.Path(path).parent.name] for path in all_image_paths]
print(all_image_labels[:5])
[1, 0, 1, 0, 0]
print(all_image_paths[:5]) #进⾏对⽐
['D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\lake\\lake_622.jpg',
'D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\airplane\\airplane_602.jpg',
'D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\lake\\lake_459.jpg',
'D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\airplane\\airplane_243.jpg',
'D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\airplane\\airplane_640.jpg']
2.9 检查标签是否贴正确
#使⽤IPython
import IPython.display as display
def caption_image(label):
return {0: 'airplane', 1: 'lake'}.get(label)
#这⾥需要将字典⾥⾯的元素进⾏对调,为了可视化
#和上⾯的进⾏对⽐这⾥输出的是原来字典的键,上⾯输出的是值
for i in range(3):
image_index = random.choice(range(len(all_image_paths)))
display.display(display.Image(all_image_paths[image_index]))
print(caption_image(all_image_labels[image_index]))
plane lake lake 2.10 单张图⽚处理
#步骤:取⼀张图⽚,然后使⽤ad_file⽅法读取,解码图⽚,转换图⽚数据格式,最后是标准化
img_path = all_image_paths[0] #取⼀张图⽚
print(img_path )
'D:\\Program Files (x86)\\tensorflow-data\\dataset\\2_class\\lake\\lake_622.jpg'
img_raw = ad_file(img_path) #读取⽂件
print(repr(img_raw)[:100]+"...")
<tf.Tensor: id=13525, shape=(), dtype=string, numpy=b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\'
img_tensor = tf.image.decode_image(img_raw) #解码图⽚
print(img_tensor.shape)
print(img_tensor.dtype)
(256, 256, 3)
<dtype: 'uint8'>
img_tensor = tf.cast(img_tensor, tf.float32) #转换图⽚数据格式
img_final = img_tensor/255.0 #数据标准化
print(img_final.shape)
print(img_final.numpy().min())
print(img_final.numpy().max())#查看数据
(256, 256, 3)
0.0
1.0
2.11 函数封装
#将单个图⽚处理的过程封装成函数,⽅便调⽤
def load_and_preprocess_image(path):
image = ad_file(path)
image = tf.image.decode_jpeg(image, channels=3)
image = size(image, [256, 256])
image = tf.cast(image, tf.float32)
image = image/255.0 # normalize to [0,1] range
return image
2.12 检验函数能否正常运⾏
image_path = all_image_paths[0]
label = all_image_labels[0]
plt.imshow(load_and_preprocess_image(img_path))
plt.xlabel(caption_image(label))
plt.show()
3 卷积神经⽹络构建
3.1 创建图像路径的dataset,调⽤定义函数
path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)
AUTOTUNE = perimental.AUTOTUNE
image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE) label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(all_image_labels, tf.int64))
for label in label_ds.take(10): #查看数据处理结果
print(label_names[label.numpy()],end=" ")
lake airplane lake airplane airplane lake lake lake airplane airplane
3.2 合并图像和标签,显⽰合并后数据的格式
image_label_ds = tf.data.Dataset.zip((image_ds, label_ds))
print(image_label_ds)
<ZipDataset shapes: ((256, 256, 3), ()), types: (tf.float32, tf.int64)>
3.3 划分测试集和训练集
#数据容量的确定和挑选
test_count = int(image_count*0.2)
train_count = image_count - test_count
print(test_count,train_count) #数据容量的确定
(280, 1120)
train_data = image_label_ds.skip(test_count) #数据挑选
test_data = image_label_ds.take(test_count)
3.4 训练数据和测试数据的预处理
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论