TensorFlow-数据增强(扩充数据集)

图像增强就是对图像的简单变形,解决因拍照角度不同,引起的图片变形。

image_gen_train.fit(x_train)这里的fit需要输入一个思维的数据,所以要对x_train进行reshape,把60000张28行28列的数据,转换为60000张28行28列的单通道数据(即为1),单通道为灰度值。

 

import tensorflow as tf
from matplotlib import pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np

mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0],28,28,1)

image_gen_train = ImageDataGenerator(
    rescale = 1. / 255,   
    rotation_range = 45,
    width_shift_range = .15,
    height_shift_range = .15,
    horizontal_flip = False,
    zoom_range = 0.5
    )

image_gen_train.fit(x_train)
print("x_train",x_train.shape)
x_train_subset1 = np.squeeze(x_train[:12])
print("x_train_subset1",x_train_subset1.shape)
print("x_train",x_train.shape)
x_train_subset2 = x_train[:12]
print("x_train_subset2",x_train_subset2.shape)

fig = plt.figure(figsize=(20,2))
plt.set_cmap('gray')

for i in range(0,len(x_train_subset1)):
    ax = fig.add_subplot(1,12,i + 1)
    ax.imshow(x_train_subset1[i])
fig.suptitle('Subset of Original Traaining Images',fontsize = 20)
plt.show()

fig = plt.figure(figsize = (20,2))
for x_batch in image_gen_train.flow(x_train_subset2,batch_size=12,shuffle=False):
    for i in range(0,12):
        ax = fig.add_subplot(1,12,i + 1)
        ax.imshow(np.squeeze(x_batch[i]))
    fig.suptitle('Augmented Image',fontsize = 20)
    plt.show()
    break;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值