数据集准备及数据预处理_1.准备数据集
數(shù)據(jù)集準(zhǔn)備及數(shù)據(jù)預(yù)處理
In this post I will introduce an example of how to upgrade legacy tensorflow codes to new style. tensorflow 2.0 recommends using Keras which is a well known high level API. Their sequential style approach makes your code more simple. And the script would be worked line by line, so we can easily understand the working process.
在這篇文章中,我將介紹一個(gè)如何將舊版tensorflow代碼升級(jí)為新樣式的示例。 tensorflow 2.0建議使用Keras,這是眾所周知的高級(jí)API。 他們的順序樣式方法使您的代碼更簡(jiǎn)單。 而且腳本將逐行工作,因此我們可以輕松地了解工作過程。
Let me introduce a guide by my BEGAN repository
讓我介紹一下BEGAN存儲(chǔ)庫(kù)中的指南
1.準(zhǔn)備數(shù)據(jù)集 (1. Prepare a dataset)
For preparing the dataset, at the previous version, I made the ImageIterator class which provides preprocessing and iterator. Obviously, it is not quite hard, cos we can find a kind guide on the homepage for that. But it looks cumbersome. In tensorflow 2.0 you don’t have to make that kind of class, we just use ImageDataGenerator which is a predefined API for data generation. We can set parameters for preprocessing and data augmentation when we create ImageDataGenerator. I gave some parameters for normalization and horizontal flipping as data augmentation as shown in below.
為了準(zhǔn)備數(shù)據(jù)集,在以前的版本中,我制作了ImageIterator 類 ,其提供預(yù)處理和迭代器。 顯然,這并不難,因?yàn)槲覀兛梢栽谥黜撋险业揭环N類似的指南。 但這看起來很麻煩。 在tensorflow 2.0中,您不必創(chuàng)建此類,我們只使用ImageDataGenerator ,這是用于數(shù)據(jù)生成的預(yù)定義API。 創(chuàng)建ImageDataGenerator時(shí),可以設(shè)置用于預(yù)處理和數(shù)據(jù)擴(kuò)充的參數(shù)。 我提供了一些參數(shù)進(jìn)行標(biāo)準(zhǔn)化和水平翻轉(zhuǎn),作為數(shù)據(jù)增強(qiáng),如下所示。
2.設(shè)計(jì)一個(gè)網(wǎng)絡(luò)。 (2. Design a network.)
The began network consists of a decoder and an encoder. In the previous repository, to implement a Convolution Layer, I used 3 low level functions as shown in below.
初始網(wǎng)絡(luò)由解碼器和編碼器組成。 在先前的存儲(chǔ)庫(kù)中,為了實(shí)現(xiàn)卷積層,我使用了3個(gè)低級(jí)函數(shù),如下所示。
And I also predefined some parameters about layer weights.
我還預(yù)定義了一些有關(guān)圖層權(quán)重的參數(shù)。
So, someone used to define a custom function for reducing code lines and better readability.
因此,曾經(jīng)有人定義了一個(gè)自定義函數(shù)以減少代碼行并提高可讀性。
But, with Keras, I only needed just a line.
但是,有了Keras,我只需要一條線。
The high level API includes parameters on how to define activation and bias. So we don’t have to make any custom function. We just use predefined Keras APIs. I could design the decoder network using about 20 lines. Awesome!!
高級(jí)API包含有關(guān)如何定義激活和偏差的參數(shù)。 因此,我們不必進(jìn)行任何自定義功能。 我們只使用預(yù)定義的Keras API。 我可以使用大約20條線設(shè)計(jì)解碼器網(wǎng)絡(luò)。 太棒了!!
Before training the network we have to make a model which takes inputs and returns outputs. Keras provides an API for that. Look at the codes to feel how simple it is.
在訓(xùn)練網(wǎng)絡(luò)之前,我們必須先建立一個(gè)模型,該模型接受輸入并返回輸出。 Keras為此提供了一個(gè)API。 查看代碼以了解它有多簡(jiǎn)單。
It is interesting I didn’t write any lines for defining variable scope like previous codes. It works automatically on the Keras API.
有趣的是,我沒有寫任何行來定義變量范圍,如先前的代碼。 它可以在Keras API上自動(dòng)運(yùn)行。
3.訓(xùn)練和測(cè)試模型 (3. Train and Test a Model)
3.1 Train
3.1火車
What I really love about Keras is I don’t have to use sess.run anymore. I think that the old style process makes codes quite complex. Look at the flow, we can easily follow these lines how this is gonna work.
我真正喜歡Keras的地方是我不再需要使用sess.run 。 我認(rèn)為舊樣式的流程使代碼變得相當(dāng)復(fù)雜。 看一下流程,我們可以很容易地遵循以下原則。
The model only had been trained for 5 hours on a Nvidia 1080TI. But the generator could show quite impressive outputs.
該模型僅在Nvidia 1080TI上訓(xùn)練了5個(gè)小時(shí)。 但是生成器可能會(huì)顯示出令人印象深刻的輸出。
3.2 Test
3.2測(cè)試
Look at the super tiny test codes. There’s no excuse for not updating your legacy repository.
看一下超小的測(cè)試代碼。 沒有任何理由不更新舊版存儲(chǔ)庫(kù)。
I also updated my DCGAN repository. Visit the repository, switch branches and check updated codes. I derived help from a tutorial that introduced the tensorflow homepage. I would like to recommend you read the tutorial too.
我還更新了DCGAN存儲(chǔ)庫(kù) 。 訪問存儲(chǔ)庫(kù),切換分支并檢查更新的代碼。 我從介紹tensorflow主頁的教程中獲得了幫助。 我也建議您閱讀本教程。
翻譯自: https://medium.com/@fabulousjeong/updating-old-tensorflow-codes-to-new-tensorflow-2-0-style-ed4dca5f42f
數(shù)據(jù)集準(zhǔn)備及數(shù)據(jù)預(yù)處理
總結(jié)
以上是生活随笔為你收集整理的数据集准备及数据预处理_1.准备数据集的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米 pegasus_使用Google的
- 下一篇: 2023年旗舰手机方向预测 全方面内卷自