Use keras to augment dataset(20 images are generated after a picture is rotated, translated, etc.)

本文介绍了一个使用Keras的ImageDataGenerator进行图片数据增强的例子,通过旋转、平移、缩放等操作,从一张原始图片生成了20张新的训练图片。

Download cat_and_dog Dataset(kaggle dataset)

from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
import numpy as np

datagen = ImageDataGenerator(rotation_range=40,# Random rotation degree
                            width_shift_range=0.2, # Random horizontal translation
                            height_shift_range=0.2, # Random vertical translation
                            rescale=1/255, # numerical normalization
                            shear_range=0.2, # Random cutting
                            zoom_range=0.2, # Random expanding
                            horizontal_flip=True,# Flip horizontal
                            fill_mode='nearest', # Fillstyle
                             )
# load image
img = load_img('/home/haku/Documents/cat_dog_dataset/train/cat.1.jpg')
x = img_to_array(img)
print(x.shape)

x = np.expand_dims(x,0)
print(x.shape)

import os
dir = os.path.exists('./temp/')
if not dir:
    os.mkdir('./temp/')
# Generate 20 pictures
i = 0
for batch in datagen.flow(x, batch_size=1, save_to_dir='temp', save_prefix='new_cat', save_format='jpeg'):
    i += 1
    if i==20:
        break
print("finished")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值