5, Data Augmentation
Intro
這是深度學(xué)習(xí)第5課
在本課程結(jié)束時(shí),您將能夠使用數(shù)據(jù)增強(qiáng)。 這個(gè)技巧讓你看起來(lái)?yè)碛械臄?shù)據(jù)遠(yuǎn)遠(yuǎn)超過(guò)實(shí)際擁有的數(shù)據(jù),從而產(chǎn)生更好的模型。
?
Lesson
[1]
from IPython.display import YouTubeVideo YouTubeVideo('ypt_BAotCLo', width=800, height=450)?
Sample Code
我們有一些你以前見(jiàn)過(guò)的模型設(shè)置代碼。 它暫時(shí)不是我們關(guān)注的焦點(diǎn).
[2]
from tensorflow.python.keras.applications import ResNet50 from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2Dnum_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 = Falsemy_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 ImageDataGeneratorimage_size = 224data_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
我們將返回您在上一個(gè)練習(xí)中處理的自動(dòng)旋轉(zhuǎn)問(wèn)題。
我們還提供了您已經(jīng)使用過(guò)的大部分代碼。 復(fù)制這篇筆記并采取數(shù)據(jù)增加步驟(下面的步驟2)。
?
1) Specify and Compile the Model
這與您之前使用的代碼的工作方式相同。 所以你在這里收到了它的完整版本。 運(yùn)行此單元格以指定和編譯模型
【4】
from tensorflow.python.keras.applications import ResNet50 from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2Dnum_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 = Falsemy_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])2) Fit the Model Using Data Augmentation
填寫(xiě)空白,并取消注釋這些代碼行。 在這樣做之后,您可以運(yùn)行此單元格,您應(yīng)該獲得一個(gè)達(dá)到約90%準(zhǔn)確度的模型。 通過(guò)使用數(shù)據(jù)擴(kuò)充,您可以將錯(cuò)誤率降低一半。
【5】
from tensorflow.python.keras.applications.resnet50 import preprocess_input from tensorflow.python.keras.preprocessing.image import ImageDataGeneratorimage_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
您已準(zhǔn)備好深入了解深度學(xué)習(xí)。
總結(jié)
以上是生活随笔為你收集整理的5, Data Augmentation的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 动手学PaddlePaddle(2):房
- 下一篇: pop3pack.exe是什么进程 po