keras如何实现批量数据增强(附代码)

本文详细介绍了如何利用Keras库进行批量数据增强,包括旋转、宽度和高度偏移、缩放、切变、随机缩放、水平翻转等操作,以提升模型的泛化能力。同时,提到了fill_mode参数在图像填充中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import skimage.io as io
import os,sys
from skimage import data_dir
import numpy as np

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

datagen = ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest')
path="/home/public/桌面/hongyi/trainA/";     
dirs=os.listdir(path)
x_all=[]
for file in dirs:
    img = load_img(path+file)
    x = img_to_array(img)  
    x = x.reshape((1,) + x.shape)  # 这是一个numpy数组,形状为 (1, 3, 150, 150)
    x_all.append(x)
    i = 0
    for batch in datagen.flow(x, batch_size=1,
                          save_to_dir='/home/public/桌面/hongyi/results1'):
       i += 1
       if i > 50:             # 数据扩充倍数,此处为数据扩充50倍
        break             #  否则生成器会退出循环

rotation_range: 旋转范围, 随机旋转(0-180)度;
width_shift and height_shift: 随机沿着水平或者垂直方

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值