keras语义分割--准备分割数据集--方式2

使用keras的ImageDataGenerator准备分割数据集

1.不要验证集

img_size = (160, 160)
batch_size = 4
seed = 1
#  图像数据生成器,可在该函数内实现数据集增强
datagen = keras.preprocessing.image.ImageDataGenerator(
            featurewise_center=True,
            featurewise_std_normalization=True
)
#  将上面的图像数据生成器应用于训练集图像及标签,得到训练集的生成器
traingen_imgs = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\imgs",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
)
traingen_labels = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\labels",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
                color_mode='grayscale'
)
traingen = zip(traingen_imgs, traingen_labels)

2.从训练集中分割验证集

img_size = (160, 160)
batch_size = 4
seed = 1
#  图像数据生成器,可在该函数内实现数据集增强,validation_split=0.2是分割验证集的比例,即验证集占20%,与下面的subset='training'和subset='validation'相对应
datagen = keras.preprocessing.image.ImageDataGenerator(
            featurewise_center=True, 
            featurewise_std_normalization=True, 
            validation_split=0.2
)
#  将上面的图像数据生成器应用于训练集图像及标签,得到训练集的生成器
traingen_imgs = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\imgs",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
                subset='training'
)
traingen_labels = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\labels",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
                subset='training',
                color_mode='grayscale'
)
traingen = zip(traingen_imgs, traingen_labels)
#  将上面的图像数据生成器应用于验证集图像及标签,得到验证集的生成器
validgen_imgs = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\imgs",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
                subset='validation'
)
validgen_labels = datagen.flow_from_directory(
                  directory=r"The Oxford-IIIT Pet Dataset\0train\labels",
                  target_size=img_size,
                  class_mode=None,
                  batch_size=batch_size,
                  seed=seed,
                  subset='validation',
                  color_mode='grayscale'
)
validgen = zip(validgen_imgs, validgen_labels)

3.另外准备验证集

img_size = (160, 160)
batch_size = 4
seed = 1
#  图像数据生成器,可在该函数内实现数据集增强
datagen = keras.preprocessing.image.ImageDataGenerator(
            featurewise_center=True,
            featurewise_std_normalization=True
)
#  将上面的图像数据生成器应用于训练集图像及标签,得到训练集的生成器
traingen_imgs = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\imgs",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
)
traingen_labels = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\labels",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
                color_mode='grayscale'
)
traingen = zip(traingen_imgs, traingen_labels)
#  将上面的图像数据生成器应用于验证集图像及标签,得到验证集的生成器
validgen_imgs = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\1valid\imgs",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed
)
validgen_labels = datagen.flow_from_directory(
                  directory=r"The Oxford-IIIT Pet Dataset\1valid\labels",
                  target_size=img_size,
                  class_mode=None,
                  batch_size=batch_size,
                  seed=seed,
                  color_mode='grayscale'
)
validgen = zip(validgen_imgs, validgen_labels)

整体代码(2.从训练集中分割验证集)

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers


def get_model(img_size, num_classes):
  inputs = keras.Input(shape=img_size + (3,))

  ###  [First half of the network: downsampling inputs] ###

  #  Entry block
  x = layers.Conv2D(32, 3, strides=2, padding="same")(inputs)
  x = layers.BatchNormalization()(x)
  x = layers.Activation("relu")(x)

  previous_block_activation = x  # Set aside residual

  #  Blocks 1, 2, 3 are identical apart from the feature depth.
  for filters in [64, 128, 256]:
    x = layers.Activation("relu")(x)
    x = layers.SeparableConv2D(filters, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)

    x = layers.Activation("relu")(x)
    x = layers.SeparableConv2D(filters, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)

    x = layers.MaxPooling2D(3, strides=2, padding="same")(x)

    # Project residual
    residual = layers.Conv2D(filters, 1, strides=2, padding="same")(
      previous_block_activation
    )
    x = layers.add([x, residual])  # Add back residual
    previous_block_activation = x  # Set aside next residual

  ###  [Second half of the network: upsampling inputs] ###

  for  filters in [256, 128, 64, 32]:
    x = layers.Activation("relu")(x)
    x = layers.Conv2DTranspose(filters, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)

    x = layers.Activation("relu")(x)
    x = layers.Conv2DTranspose(filters, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)

    x = layers.UpSampling2D(2)(x)

    # Project residual
    residual = layers.UpSampling2D(2)(previous_block_activation)
    residual = layers.Conv2D(filters, 1, padding="same")(residual)
    x = layers.add([x, residual])  # Add back residual
    previous_block_activation = x  # Set aside next residual

  #  Add a per-pixel classification layer
  outputs = layers.Conv2D(num_classes, 3, activation="softmax", padding="same")(x)

  #  Define the model
  model = keras.Model(inputs, outputs)
  return model


num_classes = 2
img_size = (160, 160)
batch_size = 4
seed = 1
#  图像数据生成器,可在该函数内实现数据集增强,validation_split=0.2是分割验证集的比例,即验证集占20%,与下面的subset='training'和subset='validation'相对应
datagen = keras.preprocessing.image.ImageDataGenerator(
            featurewise_center=True,
            featurewise_std_normalization=True,
            validation_split=0.2
)
#  将上面的图像数据生成器应用于训练集图像及标签,得到训练集的生成器
traingen_imgs = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\imgs",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
                subset='training'
)
traingen_labels = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\labels",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
                subset='training',
                color_mode='grayscale'
)
traingen = zip(traingen_imgs, traingen_labels)
#  将上面的图像数据生成器应用于验证集图像及标签,得到验证集的生成器
validgen_imgs = datagen.flow_from_directory(
                directory=r"The Oxford-IIIT Pet Dataset\0train\imgs",
                target_size=img_size,
                class_mode=None,
                batch_size=batch_size,
                seed=seed,
                subset='validation'
)
validgen_labels = datagen.flow_from_directory(
                  directory=r"The Oxford-IIIT Pet Dataset\0train\labels",
                  target_size=img_size,
                  class_mode=None,
                  batch_size=batch_size,
                  seed=seed,
                  subset='validation',
                  color_mode='grayscale'
)
validgen = zip(validgen_imgs, validgen_labels)
#  获取模型,打印模型结构,保存模型结构图片,dpi要高一点,不然图片很模糊1/2/3/4倍,其他可能有问题
model = get_model(img_size=img_size, num_classes=num_classes)
model.summary()
keras.utils.plot_model(model=model, show_shapes=True, dpi=96*4)
#  模型训练超参数设置
model.compile(optimizer=keras.optimizers.RMSprop(), loss=keras.losses.CategoricalCrossentropy(), metrics=['accuracy'])
#  训练模型
model.fit_generator(generator=traingen, steps_per_epoch=200/batch_size, epochs=5, validation_data=validgen, validation_steps=200*0.2/batch_size)

注:
keras.utils.plot_model()需要安装pydot和graphviz,可参考:
https://blog.csdn.net/cz19911223/article/details/107020723/

数据集路径格式:

├── The Oxford-IIIT Pet Dataset
├──├── 0train
├─────├── imgs
├────────├──1
├───────────├── 原图1
├───────────├── …
├───────────└── 原图n
├─────├── labels
├────────├──1
├───────────├── 标签1
├───────────├── …
├───────────└── 标签n
├──├── 1valid
├─────├── imgs
├────────├──1
├───────────├── 原图1
├───────────├── …
├───────────└── 原图m
├─────├── labels
├────────├──1
├───────────├── 标签1
├───────────├── …
├───────────└── 标签m
├──├── 2test
├─────├── imgs
├────────├──1
├───────────├── 原图1
├───────────├── …
├───────────└── 原图k
├─────├── labels
├────────├──1
├───────────├── 标签1
├───────────├── …
├───────────└── 标签k
├─────├── preds
├────────├──1
├───────────├── 预测1
├───────────├── …
├───────────└── 预测k

你可能感兴趣的:(tensorflow,深度学习,tensorflow)