keras日渐火热,了解其高效建立输入通道的方法有重要意义。
以图像分割为例,你必须跟踪图像和它们的膜。通常情况下,,你将选择 PASCAL VOC, 或者 MS COCO, 或者 Cityscapes。
如果选择这些数据集,你大部分的工作已经完成,因为这些数据集已经有了各自的train、val和test集合。你仅仅需要下载并将它们分成相关目录。
如果你的工作是图像分割,那么你的数据集结构应该如图
对于使用公共数据集的人来说,你仅仅需要将目录结构与上述保持一致。
对于其他的使用自己数据集的人,你需要写一个适合自己的脚本,参考代码:
为了训练模型,你需要生成批量图片并输入模型。当你做这一步时候你可能想要执行一些基本操作,增加数据集,例如缩放、旋转、裁剪和移位等。这成为数据扩增。事实上,一个非常常见的操作是将所有图像调整到统一的shape,这使得训练过程统一。数据扩增并不改变你的图像,仅仅创建图像的另一种表达。
为了实现这个不分,你需要使用Keras's ImageDataGenerator.
官方文档解释:
Generate batches of tensor image data with real-time data augmentation. The data will be looped over (in batches).
1.利用官方函数
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
val_datagen = ImageDataGenerator(rescale=1./255)
train_image_generator = train_datagen.flow_from_directory(
'data/train_frames/train',
batch_size=4#NORMALLY 4/8/16/32
)
train_mask_generator = train_datagen.flow_from_directory(
'data/train_frames/train',
batch_size=4#NORMALY 4/8/16/32
)
val_image_generator = val_datagen.flow_from_directory(
'data/val_frames/val',
batch_size=4
)
val_mask_generator = val_datagen.flow_from_directory(
'data/val_masks/val',
batch_szie=4
)
train_generator = zip(train_image_generator, train_mask_generator)
val_generator = zip(val_image_generator, val_mask_generator)
2.利用自定义函数
import cv2
import os
import numpy as np
import random
def data_gen(img_folder, mask_folder, batch_size):
c = 0
n = os.listdir(img_folder)
random.shuffle(n)
while(True):
img = np.zeros((batch_size, 512, 512, 3)).astype('float')
mask = np.zeros((batch_size, 512, 512, 1)).astype('float')
for i in range(c, c+batch_size):
train_img = cv2.imread(img_folder+'/'+n[i])/255
train_img = cv2.resize(train_img, (512, 512))
img[i-c] = train_img
train_mask = cv2.imread(mask_folder+'/'+n[i], cv2.IMREAD_GRAYSCALE)/255
train_mask = cv2.resize(train_mask, (512, 512))
train_mask = train_mask.reshape(512, 512, 1)
mask[i-c] = train_mask
c+=batch_size
if(c+batch_size>=len(os.listdir(img_folder))):
c = 0
random.shuffle(n)
yield img,mask
train_frame_path = 'train_frames'
train_mask_path = 'train_masks'
val_frames_path = 'val_frames'
val_mask_path = 'val_masks'
#Train the model
train_gen = data_gen(train_frame_path, train_mask_path, batch_size=4)
val_gen = data_gen(val_frames_path, val_mask_path, batch_size=4)
当你准备好数据,并创建针对于训练的图像生成器后,你就完成了数据准备阶段。接下来,你将开始准备训练。
在你准备训练之前,你需要选择模型结构。幸运的是大部分流行的模型已经被训练过并且被免费提供使用。例如
在初期,你无需翻新这些结构的差异以及在哪里使用他们。本例中,你可以直接将上面keras提供的模型model.py导入你自己的train.py中。 示例:
from keras.callbacks import ModelCheckpoint
from keras.callbacks import CSVLogger
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
import model
NO_OF_TRAINING_IMAGES = len(os.listdir('/your_data/train_frames/train/'))
NO_OF_VAL_IMAGES = len(os.listdir('/your_data/val_frames/val/'))
NO_OF_EPOCHS = 'ANYTHING FROM 30-100 FOR SMALL-MEDIUM SIZED DATASETS IS OKAY'
BATCH_SIZE = 'BATCH SIZE PREVIOUSLY INITIALISED'
weights_path = 'path/where/resulting_weights_will_be_saved'
m = model.FCN_Vgg16_32s()
opt = Adam(lr=1E-5, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
m.compile(loss='The loss to optimise [eg: dice_loss],
optimizer=opt,
metrics='YOUR_METRIC [could be 'accuracy' or mIOU, dice_coeff etc]')
checkpoint = ModelCheckpoint(weights_path, monitor='METRIC_TO_MONITOR',
verbose=1, save_best_only=True, mode='max')
csv_logger = CSVLogger('./log.out', append=True, separator=';')
earlystopping = EarlyStopping(monitor = 'METRIC_TO_MONITOR', verbose = 1,
min_delta = 0.01, patience = 3, mode = 'max')
callbacks_list = [checkpoint, csv_logger, earlystopping]
results = m.fit_generator(train_gen, epochs=NO_OF_EPOCHS,
steps_per_epoch = (NO_OF_TRAINING_IMAGES//BATCH_SIZE),
validation_data=val_gen,
validation_steps=(NO_OF_VAL_IMAGES//BATCH_SIZE),
callbacks=callbacks_list)
m.save('Model.h5')
参考https://towardsdatascience.com/a-keras-pipeline-for-image-segmentation-part-1-6515a421157d