5, Data Augmentation

Intro

这是深度学习第5课

在本课程结束时,您将能够使用数据增强。 这个技巧让你看起来拥有的数据远远超过实际拥有的数据,从而产生更好的模型。

Lesson

[1]

from IPython.display import YouTubeVideo
YouTubeVideo('ypt_BAotCLo', width=800, height=450)

Sample Code

我们有一些你以前见过的模型设置代码。 它暂时不是我们关注的焦点.

[2]

from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D

num_classes = 2
resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'

my_new_model = Sequential()
my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(num_classes, activation='softmax'))

# Say not to train first layer (ResNet) model. It is already trained
my_new_model.layers[0].trainable = False

my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])

Fitting a Model With Data Augmentation

[3]

from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator

image_size = 224

data_generator_with_aug = ImageDataGenerator(preprocessing_function=preprocess_input,
                                   horizontal_flip=True,
                                   width_shift_range = 0.2,
                                   height_shift_range = 0.2)

train_generator = data_generator_with_aug.flow_from_directory(
        '../input/urban-and-rural-photos/rural_and_urban_photos/train',
        target_size=(image_size, image_size),
        batch_size=24,
        class_mode='categorical')

data_generator_no_aug = ImageDataGenerator(preprocessing_function=preprocess_input)
validation_generator = data_generator_no_aug.flow_from_directory(
        '../input/urban-and-rural-photos/rural_and_urban_photos/val',
        target_size=(image_size, image_size),
        class_mode='categorical')

my_new_model.fit_generator(
        train_generator,
        steps_per_epoch=3,
        epochs=2,
        validation_data=validation_generator,
        validation_steps=1)
Found 72 images belonging to 2 classes.
Found 20 images belonging to 2 classes.
Epoch 1/2
3/3 [==============================] - 32s 11s/step - loss: 0.7974 - acc: 0.5556 - val_loss: 0.9505 - val_acc: 0.7000
Epoch 2/2
3/3 [==============================] - 28s 9s/step - loss: 0.5379 - acc: 0.7639 - val_loss: 0.4675 - val_acc: 0.8000

<tensorflow.python.keras._impl.keras.callbacks.History at 0x7f8ce4375f60>

Exercise:Data Augmentation

Exercise Introduction

我们将返回您在上一个练习中处理的自动旋转问题。
我们还提供了您已经使用过的大部分代码。 复制这篇笔记并采取数据增加步骤(下面的步骤2)。

1) Specify and Compile the Model

这与您之前使用的代码的工作方式相同。 所以你在这里收到了它的完整版本。 运行此单元格以指定和编译模型

【4】

from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D

num_classes = 2
resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'

my_new_model = Sequential()
my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(num_classes, activation='softmax'))

my_new_model.layers[0].trainable = False

my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])

2) Fit the Model Using Data Augmentation

填写空白,并取消注释这些代码行。 在这样做之后,您可以运行此单元格,您应该获得一个达到约90%准确度的模型。 通过使用数据扩充,您可以将错误率降低一半。

【5】

from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator

image_size = 224

# Specify the values for all arguments to data_generator_with_aug. Then uncomment those lines
#data_generator_with_aug = ImageDataGenerator(preprocessing_function=preprocess_input
#                                              horizontal_flip = _____,
#                                              width_shift_range = ____,
#                                              height_shift_range = ____)
            
# data_generator_no_aug = ImageDataGenerator(preprocessing_function=preprocess_input)


# Specify which type of ImageDataGenerator above is to load in training data
#train_generator = ____.flow_from_directory(
#        directory = '../input/dogs-gone-sideways/images/train',
#        target_size=(image_size, image_size),
#        batch_size=12,
#        class_mode='categorical')

# Specify which type of ImageDataGenerator above is to load in validation data
#validation_generator = ____.flow_from_directory(
#        directory = '../input/dogs-gone-sideways/images/val',
#        target_size=(image_size, image_size),
#        class_mode='categorical')

#my_new_model.fit_generator(
#        ____, # specify where model gets training data
#        epochs = 3,
#        steps_per_epoch=19,
#        validation_data=____) # specify where model gets validation data

Keep Going

您已准备好深入了解深度学习。

猜你喜欢

转载自blog.csdn.net/cg129054036/article/details/82883419