问题描述
我使用 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)
我对此相当陌生,因此将不胜感激任何开放的建议。谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)