数据图片生成插件 java_数据增强图像数据生成器Keras语义分割

有关扩展ImageDataGenerator的工作对于这些类型的情况更加灵活(参见this issue on Github中的示例) .

此外,正如Mikael Rousson在评论中所提到的,您可以自己轻松创建自己的ImageDataGenerator版本,同时利用其许多内置函数使其更容易 . 下面是我用于图像去噪问题的示例代码,其中我使用随机作物加性噪声来动态生成干净且嘈杂的图像对 . 您可以轻松修改此选项以添加其他类型的扩充 . 之后,您可以使用Model.fit_generator来训练使用这些方法 .

from keras.preprocessing.image import load_img, img_to_array, list_pictures

def random_crop(image, crop_size):

height, width = image.shape[1:]

dy, dx = crop_size

if width < dx or height < dy:

return None

x = np.random.randint(0, width - dx + 1)

y = np.random.randint(0, height - dy + 1)

return image[:, y:(y+dy), x:(x+dx)]

def image_generator(list_of_files, crop_size, to_grayscale=True, scale=1, shift=0):

while True:

filename = np.random.choice(list_of_files)

try:

img = img_to_array(load_img(filename, to_grayscale))

except:

return

cropped_img = random_crop(img, crop_size)

if cropped_img is None:

continue

yield scale * cropped_img - shift

def corrupted_training_pair(images, sigma):

for img in images:

target = img

if sigma > 0:

source = img + np.random.normal(0, sigma, img.shape)/255.0

else:

source = img

yield (source, target)

def group_by_batch(dataset, batch_size):

while True:

try:

sources, targets = zip(*[next(dataset) for i in xrange(batch_size)])

batch = (np.stack(sources), np.stack(targets))

yield batch

except:

return

def load_dataset(directory, crop_size, sigma, batch_size):

files = list_pictures(directory)

generator = image_generator(files, crop_size, scale=1/255.0, shift=0.5)

generator = corrupted_training_pair(generator, sigma)

generator = group_by_batch(generator, batch_size)

return generator

然后您可以使用以上内容:

train_set = load_dataset('images/train', (patch_height, patch_width), noise_sigma, batch_size)

val_set = load_dataset('images/val', (patch_height, patch_width), noise_sigma, batch_size)

model.fit_generator(train_set, samples_per_epoch=batch_size * 1000, nb_epoch=nb_epoch, validation_data=val_set, nb_val_samples=1000)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值