5, Data Augmentation
生活随笔
收集整理的這篇文章主要介紹了
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, 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
我們將返回您在上一個練習中處理的自動旋轉問題。
我們還提供了您已經使用過的大部分代碼。 復制這篇筆記并采取數據增加步驟(下面的步驟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, 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
填寫空白,并取消注釋這些代碼行。 在這樣做之后,您可以運行此單元格,您應該獲得一個達到約90%準確度的模型。 通過使用數據擴充,您可以將錯誤率降低一半。
【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
您已準備好深入了解深度學習。
總結
以上是生活随笔為你收集整理的5, Data Augmentation的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 动手学PaddlePaddle(2):房
- 下一篇: 吴恩达机器学习作业(五):支持向量机