验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

如何在TFLearn中实现数据增强

阅读:835 来源:乙速云 作者:代码code

如何在TFLearn中实现数据增强

TFLearn提供了ImageDataGenerator类来实现数据增强。下面是一个简单的示例代码,演示了如何在TFLearn中实现数据增强:

from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation

# Load path/class_id image file:
dataset_file = 'path/to/dataset_file.txt'

# Build the preloader array, resize images to 227x227
from tflearn.data_utils import build_image_dataset_from_dir
build_image_dataset_from_dir('path/to/data/', dataset_file, resize=(227, 227), convert_gray=False, filetypes=['.jpg', '.png'], categorical_Y=True)

# Image transformations
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)

# Define the network
network = tflearn.input_data(shape=[None, 227, 227, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
network = tflearn.conv_2d(network, 64, 3, activation='relu')
network = tflearn.max_pool_2d(network, 2)
network = tflearn.local_response_normalization(network)
network = tflearn.conv_2d(network, 128, 3, activation='relu')
network = tflearn.max_pool_2d(network, 2)
network = tflearn.local_response_normalization(network)
network = tflearn.fully_connected(network, 512, activation='relu')
network = tflearn.dropout(network, 0.5)
network = tflearn.fully_connected(network, 2, activation='softmax')

# Training
network = tflearn.regression(network, optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.001)

# Train using classifier
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=50, validation_set=0.1, shuffle=True, show_metric=True, batch_size=64, snapshot_step=200, snapshot_epoch=False, run_id='convnet_mnist')

在上面的示例中,我们首先定义了ImageDataGenerator类的实例img_aug,然后将其作为参数传递给input_data函数。接下来,我们定义了一个简单的神经网络,并使用fit方法对数据进行训练。

通过使用ImageDataGenerator类,我们可以很容易地实现数据增强,从而提升模型的泛化能力。

分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>