作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有 100 个训练灰度图像和 100 个 RGB 训练掩码,每个大小为 512x512。我能够使用 to_categorical
对掩码进行单热编码在 Keras 中具有以下内容
numclasses=3
masks_one_hot=to_categorical(maskArr,numclasses)
maskArr
是 100x512x512x1,和
masks_one_hot
是 100x512x512x3。
ImageDataGenerator
和
flow_from_directory
使用
trainGenerator
来自
https://github.com/zhixuhao/unet/blob/master/data.py ,我尝试保存 one-hot 编码的训练图像,然后使用
trainGenerator
读取它们.但是,我在使用
imwrite
后注意到然后用
imread
阅读它们,他们从 one-hot 编码的 512x512x3 变成了 512x512x3 RGB 图像。也就是说,每个 channel 的值不是 0 或 1,而是现在的范围是 0-255
myGenerator = trainGeneratorOneHot(20,'data/membrane/train','image','label',data_gen_args,save_to_dir = "data/membrane/train/aug", flag_multi_class = True,
num_class = 3, target_size=(512,512,3))
num_batch=3
for i,batch in enumerate(myGenerator):
if(i >= num_batch):
break
trainGeneratorOneHot
在下面:
def trainGeneratorOneHot(batch_size,...class_mode=None, image_class_mode=None):
image_datagen = ImageDataGenerator(**aug_dict)
mask_datagen = ImageDataGenerator(**aug_dict)
image_generator = image_datagen.flow_from_directory(train_path,classes = [image_folder], class_mode = image_class_mode, color_mode = image_color_mode,target_size = target_size, ...)
mask_generator = mask_datagen.flow_from_directory(train_path, classes = [mask_folder], class_mode = class_mode, target_size = target_size,...)
train_generator = zip(image_generator, mask_generator)
for (img,mask) in train_generator:
img,mask = adjustDataOneHot(img,mask)
yield (img,mask)
def adjustDataOneHot(img,mask):
return (img,mask)
最佳答案
几天前正在处理同样的问题。我发现制作我自己的数据生成器类来处理从数据帧中获取数据、扩充它,然后在将其传递给我的模型之前对其进行单热编码是必不可少的。我永远无法获得 Keras ImageDataGenerator
用于处理多个类的语义分割问题。
下面是一个数据生成器类,以防它可能对您有所帮助:
def one_hot_encoder(mask, num_classes = 8):
hot_mask = np.zeros(shape = mask.shape, dtype = 'uint8')
for _ in range(8):
temp = np.zeros(shape = mask.shape[0:2], dtype = 'uint8')
temp[mask[:, :, _] != 0] = 1
hot_mask[:, :, _] = temp
return hot_mask
# Image data generator class
class DataGenerator(keras.utils.Sequence):
def __init__(self, dataframe, batch_size, n_classes = 8, augment = False):
self.dataframe = dataframe
self.batch_size = batch_size
self.n_classes = n_classes
self.augment = augment
# Steps per epoch
def __len__(self):
return len(self.dataframe) // self.batch_size
# Shuffles and resets the index at the end of training epoch
def on_epoch_end(self):
self.dataframe = self.dataframe.reset_index(drop = True)
# Generates data, feeds to training
def __getitem__(self, index):
processed_images = []
processed_masks = []
for _ in range(self.batch_size):
the_image = io.imread(self.dataframe['Images'][index])
the_mask = io.imread(self.dataframe['Masks'][index]).astype('uint8');
one_hot_mask = one_hot_encoder(the_mask, 8)
if(self.augment):
# Resizing followed by some augmentations
processed_image = augs_for_images(image = the_image) / 255.0
processed_mask = augs_for_masks(image = one_hot_mask)
else:
# Still resizing but no augmentations
processed_image = resize(image = the_image) / 255.0
processed_mask = resize(image = one_hot_mask)
processed_images.append(processed_image)
processed_masks.append(processed_mask)
batch_x = np.array( processed_images )
batch_y = np.array( processed_masks )
return (batch_x, batch_y)
关于python - 如何在 Keras 中使用 flow_from_directory 进行多类语义分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57486898/
我是一名优秀的程序员,十分优秀!