具有数据增强功能的keras中3D体积的图像生成器

时间:2019-07-31 10:31:57

标签: python tensorflow keras conv-neural-network

由于keras的ImageDataGenerator不适合3D体积,因此我开始编写自己的keras生成器(语义分割,而不是分类!)。

1)如果外面有人适应了ImageDataGenerator代码以处理3D卷,请共享它! guy已完成了视频操作。

2)根据this tutorial,我写了一个自定义生成器。


import glob
import os

import keras
import numpy as np
import skimage
from imgaug import augmenters as iaa


class DataGenerator(keras.utils.Sequence):
    """Generates data for Keras"""
    """This structure guarantees that the network will only train once on each sample per epoch"""

    def __init__(self, list_IDs, im_path, label_path, batch_size=4, dim=(128, 128, 128),
                 n_classes=4, shuffle=True, augment=False):
        'Initialization'
        self.dim = dim
        self.batch_size = batch_size
        self.list_IDs = list_IDs
        self.im_path = im_path
        self.label_path = label_path
        self.n_classes = n_classes
        self.shuffle = shuffle
        self.augment = augment
        self.on_epoch_end()

    def __len__(self):
        'Denotes the number of batches per epoch'
        return int(np.floor(len(self.list_IDs) / self.batch_size))

    def __getitem__(self, index):
        'Generate one batch of data'
        # Generate indexes of the batch
        indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size]

        # Find list of IDs
        list_IDs_temp = [self.list_IDs[k] for k in indexes]

        # Generate data
        X, y = self.__data_generation(list_IDs_temp)

        return X, y

    def on_epoch_end(self):
        'Updates indexes after each epoch'
        self.indexes = np.arange(len(self.list_IDs))
        if self.shuffle == True:
            np.random.shuffle(self.indexes)

    def __data_generation(self, list_IDs_temp):

        if self.augment:
            pass

        if not self.augment:

            X = np.empty([self.batch_size, *self.dim])
            Y = np.empty([self.batch_size, *self.dim, self.n_classes])

            # Generate data
            for i, ID in enumerate(list_IDs_temp):
                img_X = skimage.io.imread(os.path.join(im_path, ID))
                X[i,] = img_X

                img_Y = skimage.io.imread(os.path.join(label_path, ID))
                Y[i,] = keras.utils.to_categorical(img_Y, num_classes=self.n_classes)

            X = X.reshape(self.batch_size, *self.dim, 1)
            return X, Y

params = {'dim': (128, 128, 128),
          'batch_size': 4,
          'im_path': "some/path/for/the/images/",
          'label_path': "some/path/for/the/label_images",
          'n_classes': 4,
          'shuffle': True,
          'augment': True}



partition = {}
im_path = "some/path/for/the/images/"
label_path = "some/path/for/the/label_images/"

images = glob.glob(os.path.join(im_path, "*.tif"))
images_IDs = [name.split("/")[-1] for name in images]

partition['train'] = images_IDs

training_generator = DataGenerator(partition['train'], **params)

我的图像的大小为(128, 128, 128),当我将其加载时,我得到一个大小为(batch_size, depth, heigt, width, number_of_channels)的5D张量,例如(4, 128, 128, 128, 1)。对于label_images(具有相同的尺寸,并且是单通道编码的(值1 =标签1,值2 =标签2,值3 =标签3,值0 =标签4或背景)),我得到了标签的二进制表示形式来自keras的to_categorical()函数,并以5D结尾,例如(4, 128, 128, 128, 4)。图像和label_images具有相同的名称,并且位于不同的文件夹中。

由于我只有很少的图像,所以我想通过图像增强来扩展图像的总数。我将如何使用此生成器?我已经成功测试了imgaug软件包,但是没有将图像添加到我的图像集中,而是仅转换现有图像(例如,水平翻转它们)

编辑:我对数据扩充有误解。参见this article about image augmentation。图像将通过随机转换(即时)传递。现在,我只需要收集足够的数据并使用imgaug设置参数。我将尽快更新。

0 个答案:

没有答案