4.Transfer Learning
Intro
這是深度學習第4課。
在本課程結束時,您將能夠使用遷移學習為您的自定義目標構建高度準確的計算機視覺模型,即使您的數據相對較少。
Lesson
[1]
from IPython.display import YouTubeVideo YouTubeVideo('mPFq5KMxKVw', width=800, height=450)?
Sample Code
Specify Model
[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 = FalseCompile Model
[3]
my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])Fit Model
[4]
from tensorflow.python.keras.applications.resnet50 import preprocess_input from tensorflow.python.keras.preprocessing.image import ImageDataGeneratorimage_size = 224 data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)train_generator = data_generator.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')validation_generator = data_generator.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,validation_data=validation_generator,validation_steps=1) Found 72 images belonging to 2 classes. Found 20 images belonging to 2 classes. Epoch 1/1 3/3 [==============================] - 29s 10s/step - loss: 0.5130 - acc: 0.8056 - val_loss: 0.3568 - val_acc: 0.9000<tensorflow.python.keras._impl.keras.callbacks.History at 0x7f9f5bc56a20>Note on Results:
在此階段,打印出的驗證準確度可能比訓練準確度更好。 這一開始可能令人費解。
之所以出現這種情況,是因為隨著網絡的改進,在多個點計算訓練精度(卷積中的數字正在更新以使模型更準確)。 當模型看到第一個訓練圖像時,網絡是不準確的,因為權重還沒有被訓練/改進。?
在模型完成所有數據后計算驗證損失和準確度度量。 因此,在計算這些分數時,網絡已經過全面訓練。
這在實踐中不是一個嚴重的問題,我們不會擔心它。
?
Your Turn
寫下您自己的內核來進行遷移學習。
Exercise:Using Transfer Learning
Exercise Introduction
拍攝我們深度學習視頻的攝像師提到了一個我們可以通過深度學習解決的令人沮喪的問題。
他提供掃描照片和幻燈片的服務,以數字方式存儲它們。他使用的機器能夠快速掃描許多照片。但是根據原始照片的方向,許多圖像都是橫向數字化的。他目前花了很多時間尋找哪些照片需要側向旋轉,所以他可以修復它們。
如果這個過程可以自動化,那將節省他很多時間。在本練習中,您將構建一個模型,用于區分哪些照片是橫向的,哪些照片是豎向的。
如果您打算在商業上銷售此服務,則可以使用大型數據集來訓練模型。但即使是一個小數據集,我們也會取得巨大成功。我們將使用一個小型關于狗的圖片數據集,其中一半是橫向旋轉。
指定和編譯模型看起來與您看到的示例相同。但是您需要進行一些更改以適應模型。
1)Specify the Model
由于這是您第一次,您將無法從頭開始創建。
我們已經填寫了您需要的大部分代碼,但是將一些關鍵部分留空了。
在下面的代碼中填寫(標有____)。 然后取消注釋這些行并運行單元格。
[1]
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 is the number of categories your model chooses between for each prediction # num_classes = ____ 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'))# The value below is either True or False. If you choose the wrong answer, your modeling results # won't be very good. Recall whether the first layer should be trained/changed or not. # my_new_model.layers[0].trainable = ____2)Compile the Model
我們再次提供了大部分代碼,并留下了一個非常重要的部分。 填寫空白(標有____)。 然后取消注釋該行代碼并運行單元格。
[2]
# We are calling the compile command for some python object. # Which python object is being compiled? Fill in the answer so the compile command works. # ____.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])3)Fit Model
您的訓練數據位于../input/dogs-gone-sideways/images/train目錄中。 驗證數據在../input/dogs-gone-sideways/images/val中。 設置train_generator和validation_generator時使用該信息。
您有220張訓練數據圖像和217張驗證數據。 對于訓練生成器,選擇批量大小為10。在fit_generator調用中找出steps_per_epoch的相應值? 它與示例中的不同。
填寫所有空白(再次標記為____)。 然后取消注釋每一行并運行代碼單元格。 觀察您的模型訓練權重并提高準確度。
【3】
from tensorflow.python.keras.applications.resnet50 import preprocess_input from tensorflow.python.keras.preprocessing.image import ImageDataGeneratorimage_size = 224 data_generator = ImageDataGenerator(preprocess_input)#train_generator = data_generator.flow_from_directory( # directory = ____, # target_size=(image_size, image_size), # batch_size=____, # class_mode='categorical')#validation_generator = data_generator.flow_from_directory( # directory = ____, # target_size=(image_size, image_size), # class_mode='categorical')#my_new_model.fit_generator( # train_generator, # steps_per_epoch=____, # validation_data=____, # validation_steps=1)您能從結果中判斷出您的模型在驗證數據中的正確時間嗎?
在下一步中,我們將看看我們是否可以改進。
Keep Going
繼續學習數據增強。 這是改進模型的一種巧妙而簡單的方法。 然后,您將數據增強應用于此自動圖像旋轉問題。
總結
以上是生活随笔為你收集整理的4.Transfer Learning的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java对象模型 指令_深入理解多线程(
- 下一篇: 使用OpenCV库快速求解相机内参