日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

4.Transfer Learning

發(fā)布時間:2023/12/10 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4.Transfer Learning 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Intro

這是深度學(xué)習(xí)第4課。

在本課程結(jié)束時,您將能夠使用遷移學(xué)習(xí)為您的自定義目標構(gòu)建高度準確的計算機視覺模型,即使您的數(shù)據(jù)相對較少。

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 = False

Compile 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:

在此階段,打印出的驗證準確度可能比訓(xùn)練準確度更好。 這一開始可能令人費解。
之所以出現(xiàn)這種情況,是因為隨著網(wǎng)絡(luò)的改進,在多個點計算訓(xùn)練精度(卷積中的數(shù)字正在更新以使模型更準確)。 當模型看到第一個訓(xùn)練圖像時,網(wǎng)絡(luò)是不準確的,因為權(quán)重還沒有被訓(xùn)練/改進。?
在模型完成所有數(shù)據(jù)后計算驗證損失和準確度度量。 因此,在計算這些分數(shù)時,網(wǎng)絡(luò)已經(jīng)過全面訓(xùn)練。
這在實踐中不是一個嚴重的問題,我們不會擔心它。

?

Your Turn

寫下您自己的內(nèi)核來進行遷移學(xué)習(xí)。

Exercise:Using Transfer Learning

Exercise Introduction

拍攝我們深度學(xué)習(xí)視頻的攝像師提到了一個我們可以通過深度學(xué)習(xí)解決的令人沮喪的問題。

他提供掃描照片和幻燈片的服務(wù),以數(shù)字方式存儲它們。他使用的機器能夠快速掃描許多照片。但是根據(jù)原始照片的方向,許多圖像都是橫向數(shù)字化的。他目前花了很多時間尋找哪些照片需要側(cè)向旋轉(zhuǎn),所以他可以修復(fù)它們。

如果這個過程可以自動化,那將節(jié)省他很多時間。在本練習(xí)中,您將構(gòu)建一個模型,用于區(qū)分哪些照片是橫向的,哪些照片是豎向的。

如果您打算在商業(yè)上銷售此服務(wù),則可以使用大型數(shù)據(jù)集來訓(xùn)練模型。但即使是一個小數(shù)據(jù)集,我們也會取得巨大成功。我們將使用一個小型關(guān)于狗的圖片數(shù)據(jù)集,其中一半是橫向旋轉(zhuǎn)。

指定和編譯模型看起來與您看到的示例相同。但是您需要進行一些更改以適應(yīng)模型。

1)Specify the Model

由于這是您第一次,您將無法從頭開始創(chuàng)建。

我們已經(jīng)填寫了您需要的大部分代碼,但是將一些關(guān)鍵部分留空了。

在下面的代碼中填寫(標有____)。 然后取消注釋這些行并運行單元格。

[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

您的訓(xùn)練數(shù)據(jù)位于../input/dogs-gone-sideways/images/train目錄中。 驗證數(shù)據(jù)在../input/dogs-gone-sideways/images/val中。 設(shè)置train_generator和validation_generator時使用該信息。

您有220張訓(xùn)練數(shù)據(jù)圖像和217張驗證數(shù)據(jù)。 對于訓(xùn)練生成器,選擇批量大小為10。在fit_generator調(diào)用中找出steps_per_epoch的相應(yīng)值? 它與示例中的不同。

填寫所有空白(再次標記為____)。 然后取消注釋每一行并運行代碼單元格。 觀察您的模型訓(xùn)練權(quán)重并提高準確度。

【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)

您能從結(jié)果中判斷出您的模型在驗證數(shù)據(jù)中的正確時間嗎?

在下一步中,我們將看看我們是否可以改進。

Keep Going

繼續(xù)學(xué)習(xí)數(shù)據(jù)增強。 這是改進模型的一種巧妙而簡單的方法。 然后,您將數(shù)據(jù)增強應(yīng)用于此自動圖像旋轉(zhuǎn)問題。

總結(jié)

以上是生活随笔為你收集整理的4.Transfer Learning的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。