栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

cnn数据增强_数据集增强的方法?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cnn数据增强_数据集增强的方法?

在深度学习过程中经常会因为数据量少而发生过拟合现象,或者模型的泛化能力比较低。基于此,本文讲一下图像的数据增强,就是通过对图像简答你的形变,用来应对因拍照的角度不同而使得图谱变形。tensorflow2给出了数据 增强的函数如下所示:

image_gen_train = rd.keras.preprocessing.image.ImageDataGenerator(
     rescale = 所有数据将乘以该数值
     rotation_range = 随机旋转角度数范围
     width_shift_range = 随机宽度偏移量
     height_shift_range = 随机高度偏移量
     水平反转:horizontal_flip = 是否随机水平翻转
     随机缩放:zoom_range = 随机缩放的范围[1-n,1+n]
)

image_gen_train.fit(x_train)

由于上面的fit()函数需要输入一个四维的数据,因此需要进行一个reshape()

例如对于手写数据集,是一个三维的(60000, 28, 28)60000张28行和28列的数据集需要转化位(60000, 28, 28, 1)转为60000张28行28列的单通道数据。即为:

x_train = x_train.reshape(x_train.shape[0],28,28,1)

因此这时候model.fit()也需要进行改进:

原来为:

model.fit(x_train, y_train, batch_size=32, .....)

需要改为:

model.fit(image_gen_train(x_train,y_train,batch_size =32), ....)

经过数据增强后对手数据进行识别程序和效果如下:

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)  # 给数据增加一个维度,使数据和网络结构匹配

image_gen_train = ImageDataGenerator(
    rescale=1. / 1.,   # 如为图像,分母为255时,可归至0-1
    rotation_range=45,
    width_shift_range=.15,
    height_shift_range=.15,
    horizontal_flip=True,
    zoom_range=0.5
)

image_gen_train.fit(x_train)

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

model.fit(image_gen_train.flow(x_train, y_train, batch_size=32), epochs=5, validation_data=(x_test, y_test), validation_freq=1)

model.summary()

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783616.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号