深度學習一行一行敲pix2pix網路-keras版(3.4,data_utils.py其它函數)

源碼地址:pix2pix源碼地址

論文地址:Image-to-Image Translation with Conditional Adversarial Network

文章目錄:深度學習一行一行敲pix2pix網路-keras版(目錄)

視頻目錄:深度學習一行一行敲pix2pix網路-keras版(視頻目錄)

1,gen_batch函數

該函數是為了得到一個batch的無限迭代器

def gen_batch(X1, X2, batch_size):nn while True:n idx = np.random.choice(X1.shape[0], batch_size, replace=False)n yield X1[idx], X2[idx]n

①np.random.choice:

  1. 從什麼範圍內取
  2. 取出多少個
  3. 不夠是是否重複取

print(np.random.choice(5,10,replace=True))nprint(np.random.choice(5,10,replace=False))n輸出:nTraceback (most recent call last):n[3 2 2 3 2 0 4 0 2 2]n File "D:/huang/pix2pix_keras/t.py", line 47, in <module>n print(np.random.choice(5,10,replace=False))n File "mtrand.pyx", line 1161, in mtrand.RandomState.choicenValueError: Cannot take a larger sample than population when replace=Falsen

②while True與yield一起用,以製造一個無限的迭代器(經常用)

③numpy取值的討論

1,僅給定一個維度

a = np.random.uniform(1,255,(400, 256, 256, 3))nprint(a[1].shape)n輸出:n(256, 256, 3)n

2,給定多個維度

a = np.random.uniform(1,255,(400, 256, 256, 3))nprint(a[1,2].shape)n輸出:n(256, 3)n

總結:當numpy的數組,給定一個或多個維度時其會將預設的維度用:補齊

3,給定的是一個list

a = np.random.uniform(1,255,(400, 256, 256, 3))nprint(a[[1,2]].shape)nprint(type([1,2]))n輸出:n(2, 256, 256, 3)n<class list>n

當給定list時,其會原封不動拿出相應的數據


2,get_disc_batch函數

該函數是為了得到D網路訓練用的數據

def get_disc_batch(X_full_batch, X_sketch_batch, generator_model, batch_counter, patch_size,n image_data_format, label_smoothing=False, label_flipping=0):nn # Create X_disc: alternatively only generated or real imagesn if batch_counter % 2 == 0:n # Produce an outputn X_disc = generator_model.predict(X_sketch_batch)n y_disc = np.zeros((X_disc.shape[0], 2), dtype=np.uint8)n y_disc[:, 0] = 1#這是一個onehot標籤,G網路產生的屬於第一類nn if label_flipping > 0:n p = np.random.binomial(1, label_flipping)n if p > 0:n y_disc[:, [0, 1]] = y_disc[:, [1, 0]]nn else:n X_disc = X_full_batchn y_disc = np.zeros((X_disc.shape[0], 2), dtype=np.uint8)n if label_smoothing:n y_disc[:, 1] = np.random.uniform(low=0.9, high=1, size=y_disc.shape[0])n#將標籤1,換成0.9到1的隨機數。我倒是見過這樣的操作,可以防止GAN模型的崩塌n else:n y_disc[:, 1] = 1nn if label_flipping > 0:n p = np.random.binomial(1, label_flipping)n if p > 0:n y_disc[:, [0, 1]] = y_disc[:, [1, 0]]nn # Now extract patches form X_discn X_disc = extract_patches(X_disc, image_data_format, patch_size)n#D網路nn return X_disc, y_discn

①np.random.binomial【numpy.random.binomial - NumPy v1.13.dev0 Manual】這是一個進行二項分布實驗的函數

p = np.random.binomial(10, 0.5,[1,2,3])nprint(p)n輸出:n[[[4 3 4]n [5 4 7]]]n

  1. 進行實驗的次數
  2. 每次實驗成功的概率
  3. 輸出的形狀

y_disc = np.zeros((5, 2), dtype=np.uint8)ny_disc[:, 0] = 1 nprint(y_disc)ny_disc[:, [0, 1]] = y_disc[:, [1, 0]]nprint(y_disc)n輸出:n[[1 0]n [1 0]n [1 0]n [1 0]n [1 0]]n[[0 1]n [0 1]n [0 1]n [0 1]n [0 1]]n

?這段操作,我也不知道為什麼。可能是為了防止GAN模型的崩塌

但雖然有這段函數,但作者的默認是不使用該操作的


3,extract_patches函數

該函數是將原圖片切割成一個一個的patch

def extract_patches(X, image_data_format, patch_size):nn # Now extract patches form X_discn if image_data_format == "channels_first":n X = X.transpose(0,2,3,1)nn list_X = []n list_row_idx = [(i * patch_size[0], (i + 1) * patch_size[0]) for i in range(X.shape[1] // patch_size[0])]n list_col_idx = [(i * patch_size[1], (i + 1) * patch_size[1]) for i in range(X.shape[2] // patch_size[1])]nn for row_idx in list_row_idx:n for col_idx in list_col_idx:n list_X.append(X[:, row_idx[0]:row_idx[1], col_idx[0]:col_idx[1], :])nn if image_data_format == "channels_first":n for i in range(len(list_X)):n list_X[i] = list_X[i].transpose(0,3,1,2)nn return list_Xn

需要說明的是:D網路會對傳入的數據做處理的,它會一次性讀入nb_patch個patch 【深度學習一行一行敲pix2pix網路-keras版(3.1,DCGAN_discriminator)】


4,plot_generated_batch函數

def plot_generated_batch(X_full, X_sketch, generator_model, batch_size, image_data_format, suffix):nn # Generate imagesn X_gen = generator_model.predict(X_sketch)nn X_sketch = inverse_normalization(X_sketch)n X_full = inverse_normalization(X_full)n X_gen = inverse_normalization(X_gen)#反標準化nn Xs = X_sketch[:8]n Xg = X_gen[:8]n Xr = X_full[:8]#從batch中取出8張圖片nn if image_data_format == "channels_last":n X = np.concatenate((Xs, Xg, Xr), axis=0)#將圖片按0維度拼接在一起n list_rows = []n for i in range(int(X.shape[0] // 4)):n Xr = np.concatenate([X[k] for k in range(4 * i, 4 * (i + 1))], axis=1)n list_rows.append(Xr)#每4個為一批按1維度拼接在一起,注意現在已經沒有batch維度了nn Xr = np.concatenate(list_rows, axis=0)#再次拼接,按高拼接在一起nn if image_data_format == "channels_first":n X = np.concatenate((Xs, Xg, Xr), axis=0)n list_rows = []n for i in range(int(X.shape[0] // 4)):n Xr = np.concatenate([X[k] for k in range(4 * i, 4 * (i + 1))], axis=2)n list_rows.append(Xr)nn Xr = np.concatenate(list_rows, axis=1)n Xr = Xr.transpose(1,2,0)nn if Xr.shape[-1] == 1:n plt.imshow(Xr[:, :, 0], cmap="gray")n else:n plt.imshow(Xr)n plt.axis("off")n plt.savefig("../figures/current_batch_%s.png" % suffix)n plt.clf()n plt.close()#顯示與保存n

關於plt的討論見【python與numpy使用的一些小tips(5)】


5, normalization與inverse_normalization函數

def normalization(X):nn return X / 127.5 - 1nnndef inverse_normalization(X):nn return (X + 1.) / 2.n

圖片標準化與反標準化。需要說明的是:反標準化,是將像素點的值映射到[0,1]。這並不影響顯示最後附上我的結果:

歡迎關注公眾號:huangxiaobai880

https://www.zhihu.com/video/927165166473342976
推薦閱讀:

李宏毅機器學習2016 第十七講 遷移學習
理解《Deep Forest: Towards An Alternative to Deep Neural Network》
如何理解在二維空間內線性不可分的數據,可以在五維空間內線性可分?
當我們在談論K-means:數據概述

TAG:深度学习DeepLearning | 机器学习 | 源码阅读 |