我正在使用 Imgaug 进行图像增强以及我的 CNN 模型的自定义数据生成器,用于分类 23 类 X 射线身体部位。我不确定如何将我的增强函数传递给我的 def getitem函数,以便查看我的训练集上的增强。代码如下:
train_augment = iaa.Sequential([iaa.Fliplr(1.0)])
training_generator = CustomGenerator(training_set augmentation = train_augment)
class CustomGenerator(Sequence):
def __init__(self, folder_path, class_names, images, label_index, batch_size, shuffle, augmentation):
images_path = []
for a_class in class_names:
image_folder = os.path.join(folder_path, a_class)
a_class_num = class_names.index(a_class)
for imgs in os.listdir(image_folder):
images.append(imgs)
images_path += [os.path.join(image_folder, imgs)]
label_index.append(a_class_num)
self.images_path = images_path
self.images = images
self.labels = label_index
self.num_classes = len(np.unique(self.labels))
self.batch_size = batch_size
self.shuffle = shuffle
self.augmentation = augmentation
self.on_epoch_end()
def on_epoch_end(self):
if self.shuffle == True:
rand_int = np.random.permutation(np.arange(0,len(self.images))).astype(int)
self.images_path = [self.images_path[i] for i in rand_int]
self.images = [self.images[i] for i in rand_int]
self.labels = [self.labels[i] for i in rand_int]
self.indexes = np.arange(len(self.images_path))
def __len__(self):
return int(np.ceil(len(self.images_path) / self.batch_size))
def __getitem__(self, index):
indexes = self.indexes[index * self.batch_size: (index + 1) * self.batch_size]
labels_batch = np.array([self.labels[k] for k in indexes])
image_batch = np.array([imread(self.images_path[f]) for f in indexes])
# images = self.augmentation(image_batch)
return image_batch[..., np.newaxis], tf.keras.utils.to_categorical(np.array(labels_batch), num_classes=self.num_classes)
我对此相当陌生,因此将不胜感激任何开放的建议。谢谢你