import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pathlib
数据读取及预处理
data_dir = "./2_class"# 文件路径
data_root = pathlib.Path(data_dir)# 构建路径对象
for item in data_root.iterdir(): # 对目录进行迭代查看文件路径及对象
print(item)
all_image_path = list(data_root.glob("*/*"))#使用glob方法及正则表达式提取目录里面所有文件
len(all_image_path) # 1400个数据
all_image_path[:3]# 通过切片查看前3个文件
all_image_path = [str(path) for path in all_image_path]# 使用str把路径变成一个实际的路径
all_image_path[10:12]
import random
random.shuffle(all_image_path)# 把内容乱序
all_image_path[10:12]
image_count = len(all_image_path)
image_count # 记录图片的张数
label_names = sorted (item.name for item in data_root.glob("*/")) # 提取分类名字
label_names
# 编码airplane对应0, lake对应1
label_to_index = dict((name,index) for index,name in enumerate(label_names))
label_to_index
all_image_path[:3]
pathlib.Path("2_class\lake\lake_405.jpg").parent.name
all_image_label = [label_to_index[pathlib.Path(p).parent.name]for p in all_image_path]
all_image_label[:5]
all_image_path[:5]
import IPython.display as display
index_to_label = dict((v,k) for k,v in label_to_index.items())
index_to_label
读取和解码图片
for n in range(3):
image_index = random.choice(range(len(all_image_path)))
display.display(display.Image(all_image_path[image_index]))
print(index_to_label[all_image_label[image_index]])
print()
# 对单张图片进行处理过程
# 使用tf读取图片
img_path = all_image_path[0]
img_path
img_raw = tf.io.read_file(img_path)
img_raw # 二进制的图片
# 解码图片
img_tensor = tf.image.decode_image(img_raw)
img_tensor.shape
img_tensor
img_tensor = tf.cast(img_tensor,tf.float32)# 转换数据类型为float32
img_tensor
# 标准化
img_tensor = img_tensor/255
定义函数对图片进行处理
# 定义函数对图片进行处理
def load_preprosess_image(img_paht):
img_raw = tf.io.read_file(img_path) # 读取图片的路径
img_tensor = tf.image.decode_jpeg(img_raw,channels=3)# 解码图片channels=3代表彩色图片
img_tensor = tf.image.resize(img_tensor,[256,256]) #定义图片大小
img_tensor = tf.cast(img_tensor,tf.float32) # 转化图片类型
img = img_tensor/255 # 标准化
return img
使用tf.data 构建图片输入管道
# 构造tf.data对所有图片进行处理
path_ds = tf.data.Dataset.from_tensor_slices(all_image_path)
image_dataset = path_ds.map(load_preprosess_image)# 使用上面定义好的图片处理函数处理all_image_path中所有的图片
label_dataset = tf.data.Dataset.from_tensor_slices(all_image_label)
# 合并
dataset = tf.data.Dataset.zip((image_dataset,label_dataset))
# 划分测试集与训练集
test_count = int(image_count*0.2)
train_count = image_count-test_count
train_dataset = dataset.skip(test_count) # skip 跳过测试集的张数
test_dataset = dataset.take(test_count)
BATCH_SIZE = 32# 每次训练32张
train_dataset = train_dataset.shuffle(buffer_size=train_count).batch(BATCH_SIZE)
test_dataset = test_dataset.batch(BATCH_SIZE)
建立模型
# 增加BN层
#建立模型
model = tf.keras.Sequential() # 顺序模型
model.add(tf.keras.layers.Conv2D(64,(3,3),input_shape=(256,256,3)))# 添加一个卷积层
model.add(tf.keras.layers.BatchNormalization()) # 批标准化
model.add(tf.keras.layers.Activation("relu")) # 激活层
model.add(tf.keras.layers.Conv2D(64,(3,3)))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(128,(3,3)))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(256,(3,3)))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(512,(3,3)))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(1024,(3,3)))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation("relu"))
model.add(tf.keras.layers.GlobalAveragePooling2D()) # 全局池化
model.add(tf.keras.layers.Dense(1024))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation("relu"))
model.add(tf.keras.layers.Dense(256))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation("relu"))
model.add(tf.keras.layers.Dense(1,activation="sigmoid"))#二分类使用sigmoid激活
model.summary()
# 编译模型
model.compile(optimizer="adam",
loss="binary_crossentropy",
metrics=["acc"])
steps_per_epoch = train_count//BATCH_SIZE
validation_steps = test_count//BATCH_SIZE # 步数
# 训练模型
history = model.fit(train_dataset,epochs=30,
steps_per_epoch=steps_per_epoch,
validation_data=test_dataset,
validation_steps=validation_steps)