iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 24
0
AI & Machine Learning

探索 Microsoft CNTK 機器學習工具系列 第 24

深度捲積生成對抗網路

  • 分享至 

  • xImage
  •  

Introduction

深度捲積生成對抗網路(Deep Convolutional Generative Adversarial Network, DCGAN)是生成對抗網路的改良,使得生成對抗網路更加穩定,有效減少訓練的回合數。

  • 生成器及鑑別器中使用梯度歸一化(Batch Normalization)
  • 移除全連接層(fully connected layer)
  • 生成器使用 Tanh、ReLU 激活函式(activation function)
  • 鑑別器使用 LeakyReLU 激活函式(activation function)

Tasks

3.建立模型(Model creation):

設定參數。

img_h, img_w = 28, 28
kernel_h, kernel_w = 5, 5 
stride_h, stride_w = 2, 2

g_input_dim = 100
g_output_dim = d_input_dim = img_h * img_w

if kernel_h == kernel_w:
    gkernel = dkernel = kernel_h
else:
    raise ValueError('This tutorial needs square shaped kernel') 
            
if stride_h == stride_w:
    gstride = dstride = stride_h
else:
    raise ValueError('This tutorial needs same stride in all dims')

宣告函式:bn_with_relu,梯度歸一化(Batch Normalization)及 ReLU 激活函式(activation function)

def bn_with_relu(x, activation=C.relu):
    h = C.layers.BatchNormalization(map_rank=1)(x)
    return C.relu(h)

宣告函式:bn_with_relu,梯度歸一化(Batch Normalization)及 LeakyReLU 激活函式(activation function)

def bn_with_leaky_relu(x, leak=0.2):
    h = C.layers.BatchNormalization(map_rank=1)(x)
    r = C.param_relu(C.constant((np.ones(h.shape)*leak).astype(np.float32)), h)
    return r

宣告函式:convolutional_generator,生成器使用 Tanh、ReLU 激活函式。

def convolutional_generator(z):
    with C.layers.default_options(init=C.normal(scale=0.02)):
        print('Generator input shape: ', z.shape)

        # MNIST 像素逐步下降
        s_h2, s_w2 = img_h//2, img_w//2
        s_h4, s_w4 = img_h//4, img_w//4
        gfc_dim = 1024
        gf_dim = 64

        h0 = C.layers.Dense(gfc_dim, activation=None)(z)
        h0 = bn_with_relu(h0)
        print('h0 shape', h0.shape)

        h1 = C.layers.Dense([gf_dim * 2, s_h4,  s_w4], activation=None)(h0)
        h1 = bn_with_relu(h1)
        print('h1 shape', h1.shape)

        h2 = C.layers.ConvolutionTranspose2D(gkernel,
                                  num_filters=gf_dim*2,
                                  strides=gstride,
                                  pad=True,
                                  output_shape=(s_h2, s_w2),
                                  activation=None)(h1)
        h2 = bn_with_relu(h2)
        print('h2 shape', h2.shape)

        h3 = C.layers.ConvolutionTranspose2D(gkernel,
                                  num_filters=1,
                                  strides=gstride,
                                  pad=True,
                                  output_shape=(img_h, img_w),
                                  activation=C.sigmoid)(h2)
        print('h3 shape :', h3.shape)

        return C.reshape(h3, img_h * img_w)

宣告函式:convolutional_discriminator,鑑別器使用 LeakyReLU 激活函式。

def convolutional_discriminator(x):
    with C.layers.default_options(init=C.normal(scale=0.02)):

        dfc_dim = 1024
        df_dim = 64

        print('Discriminator convolution input shape', x.shape)
        x = C.reshape(x, (1, img_h, img_w))

        h0 = C.layers.Convolution2D(dkernel, 1, strides=dstride)(x)
        h0 = bn_with_leaky_relu(h0, leak=0.2)
        print('h0 shape :', h0.shape)

        h1 = C.layers.Convolution2D(dkernel, df_dim, strides=dstride)(h0)
        h1 = bn_with_leaky_relu(h1, leak=0.2)
        print('h1 shape :', h1.shape)

        h2 = C.layers.Dense(dfc_dim, activation=None)(h1)
        h2 = bn_with_leaky_relu(h2, leak=0.2)
        print('h2 shape :', h2.shape)

        h3 = C.layers.Dense(1, activation=C.sigmoid)(h2)
        print('h3 shape :', h3.shape)

        return h3

上一篇
生成對抗網路
下一篇
重要性採樣
系列文
探索 Microsoft CNTK 機器學習工具30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言