"official/legacy/bert/run_squad.py" did not exist on "5981350ba33d6db9e7a41f20e73b6103a1a09827"
layers.py 12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Layers for a StarGAN model.

This module contains basic layers to build a StarGAN model.

See https://arxiv.org/abs/1711.09020 for details about the model.

See https://github.com/yunjey/StarGAN for the original pytorvh implementation.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function



import tensorflow as tf

import ops


def generator_down_sample(input_net, final_num_outputs=256):
  """Down-sampling module in Generator.

  Down sampling pathway of the Generator Architecture:

  PyTorch Version:
  https://github.com/yunjey/StarGAN/blob/fbdb6a6ce2a4a92e1dc034faec765e0dbe4b8164/model.py#L32

  Notes:
    We require dimension 1 and dimension 2 of the input_net to be fully defined
    for the correct down sampling.

  Args:
    input_net: Tensor of shape (batch_size, h, w, c + num_class).
    final_num_outputs: (int) Number of hidden unit for the final layer.

  Returns:
    Tensor of shape (batch_size, h / 4, w / 4, 256).

  Raises:
    ValueError: If final_num_outputs are not divisible by 4,
      or input_net does not have a rank of 4,
      or dimension 1 and dimension 2 of input_net are not defined at graph
      construction time,
      or dimension 1 and dimension 2 of input_net are not divisible by 4.
  """

  if final_num_outputs % 4 != 0:
    raise ValueError('Final number outputs need to be divisible by 4.')

  # Check the rank of input_net.
  input_net.shape.assert_has_rank(4)

  # Check dimension 1 and dimension 2 are defined and divisible by 4.
  if input_net.shape[1]:
    if input_net.shape[1] % 4 != 0:
      raise ValueError(
          'Dimension 1 of the input should be divisible by 4, but is {} '
          'instead.'.
          format(input_net.shape[1]))
  else:
    raise ValueError('Dimension 1 of the input should be explicitly defined.')

  # Check dimension 1 and dimension 2 are defined and divisible by 4.
  if input_net.shape[2]:
    if input_net.shape[2] % 4 != 0:
      raise ValueError(
          'Dimension 2 of the input should be divisible by 4, but is {} '
          'instead.'.
          format(input_net.shape[2]))
  else:
    raise ValueError('Dimension 2 of the input should be explicitly defined.')

  with tf.variable_scope('generator_down_sample'):

    with tf.contrib.framework.arg_scope(
        [tf.contrib.layers.conv2d],
        padding='VALID',
        biases_initializer=None,
        normalizer_fn=tf.contrib.layers.instance_norm,
        activation_fn=tf.nn.relu):

      down_sample = ops.pad(input_net, 3)
      down_sample = tf.contrib.layers.conv2d(
          inputs=down_sample,
          num_outputs=final_num_outputs / 4,
          kernel_size=7,
          stride=1,
          scope='conv_0')

      down_sample = ops.pad(down_sample, 1)
      down_sample = tf.contrib.layers.conv2d(
          inputs=down_sample,
          num_outputs=final_num_outputs / 2,
          kernel_size=4,
          stride=2,
          scope='conv_1')

      down_sample = ops.pad(down_sample, 1)
      output_net = tf.contrib.layers.conv2d(
          inputs=down_sample,
          num_outputs=final_num_outputs,
          kernel_size=4,
          stride=2,
          scope='conv_2')

    return output_net


def _residual_block(input_net,
                    num_outputs,
                    kernel_size,
                    stride=1,
                    padding_size=0,
                    activation_fn=tf.nn.relu,
                    normalizer_fn=None,
                    name='residual_block'):
  """Residual Block.

  Input Tensor X - > Conv1 -> IN -> ReLU -> Conv2 -> IN + X

  PyTorch Version:
  https://github.com/yunjey/StarGAN/blob/fbdb6a6ce2a4a92e1dc034faec765e0dbe4b8164/model.py#L7

  Args:
    input_net: Tensor as input.
    num_outputs: (int) number of output channels for Convolution.
    kernel_size: (int) size of the square kernel for Convolution.
    stride: (int) stride for Convolution. Default to 1.
    padding_size: (int) padding size for Convolution. Default to 0.
    activation_fn: Activation function.
    normalizer_fn: Normalization function.
    name: Name scope

  Returns:
    Residual Tensor with the same shape as the input tensor.
  """

  with tf.variable_scope(name):

    with tf.contrib.framework.arg_scope(
        [tf.contrib.layers.conv2d],
        num_outputs=num_outputs,
        kernel_size=kernel_size,
        stride=stride,
        padding='VALID',
        normalizer_fn=normalizer_fn,
        activation_fn=None):

      res_block = ops.pad(input_net, padding_size)
      res_block = tf.contrib.layers.conv2d(inputs=res_block, scope='conv_0')

      res_block = activation_fn(res_block, name='activation_0')

      res_block = ops.pad(res_block, padding_size)
      res_block = tf.contrib.layers.conv2d(inputs=res_block, scope='conv_1')

      output_net = res_block + input_net

  return output_net


def generator_bottleneck(input_net, residual_block_num=6, num_outputs=256):
  """Bottleneck module in Generator.

  Residual bottleneck pathway in Generator.

  PyTorch Version:
  https://github.com/yunjey/StarGAN/blob/fbdb6a6ce2a4a92e1dc034faec765e0dbe4b8164/model.py#L40

  Args:
    input_net: Tensor of shape (batch_size, h / 4, w / 4, 256).
    residual_block_num: (int) Number of residual_block_num. Default to 6 per the
      original implementation.
    num_outputs: (int) Number of hidden unit in the residual bottleneck. Default
      to 256 per the original implementation.

  Returns:
    Tensor of shape (batch_size, h / 4, w / 4, 256).

  Raises:
    ValueError: If the rank of the input tensor is not 4,
      or the last channel of the input_tensor is not explicitly defined,
      or the last channel of the input_tensor is not the same as num_outputs.
  """

  # Check the rank of input_net.
  input_net.shape.assert_has_rank(4)

  # Check dimension 4 of the input_net.
  if input_net.shape[-1]:
    if input_net.shape[-1] != num_outputs:
      raise ValueError(
          'The last dimension of the input_net should be the same as '
          'num_outputs: but {} vs. {} instead.'.format(input_net.shape[-1],
                                                       num_outputs))
  else:
    raise ValueError(
        'The last dimension of the input_net should be explicitly defined.')

  with tf.variable_scope('generator_bottleneck'):

    bottleneck = input_net

    for i in range(residual_block_num):

      bottleneck = _residual_block(
          input_net=bottleneck,
          num_outputs=num_outputs,
          kernel_size=3,
          stride=1,
          padding_size=1,
          activation_fn=tf.nn.relu,
          normalizer_fn=tf.contrib.layers.instance_norm,
          name='residual_block_{}'.format(i))

  return bottleneck


def generator_up_sample(input_net, num_outputs):
  """Up-sampling module in Generator.

  Up sampling path for image generation in the Generator.

  PyTorch Version:
  https://github.com/yunjey/StarGAN/blob/fbdb6a6ce2a4a92e1dc034faec765e0dbe4b8164/model.py#L44

  Args:
    input_net: Tensor of shape (batch_size, h / 4, w / 4, 256).
    num_outputs: (int) Number of channel for the output tensor.

  Returns:
    Tensor of shape (batch_size, h, w, num_outputs).
  """

  with tf.variable_scope('generator_up_sample'):

    with tf.contrib.framework.arg_scope(
        [tf.contrib.layers.conv2d_transpose],
        kernel_size=4,
        stride=2,
        padding='VALID',
        normalizer_fn=tf.contrib.layers.instance_norm,
        activation_fn=tf.nn.relu):

      up_sample = tf.contrib.layers.conv2d_transpose(
          inputs=input_net, num_outputs=128, scope='deconv_0')
      up_sample = up_sample[:, 1:-1, 1:-1, :]

      up_sample = tf.contrib.layers.conv2d_transpose(
          inputs=up_sample, num_outputs=64, scope='deconv_1')
      up_sample = up_sample[:, 1:-1, 1:-1, :]

    output_net = ops.pad(up_sample, 3)
    output_net = tf.contrib.layers.conv2d(
        inputs=output_net,
        num_outputs=num_outputs,
        kernel_size=7,
        stride=1,
        padding='VALID',
        activation_fn=tf.nn.tanh,
        normalizer_fn=None,
        biases_initializer=None,
        scope='conv_0')

  return output_net


def discriminator_input_hidden(input_net, hidden_layer=6, init_num_outputs=64):
  """Input Layer + Hidden Layer in the Discriminator.

  Feature extraction pathway in the Discriminator.

  PyTorch Version:
  https://github.com/yunjey/StarGAN/blob/fbdb6a6ce2a4a92e1dc034faec765e0dbe4b8164/model.py#L68

  Args:
    input_net: Tensor of shape (batch_size, h, w, 3) as batch of images.
    hidden_layer: (int) Number of hidden layers. Default to 6 per the original
      implementation.
    init_num_outputs: (int) Number of hidden unit in the first hidden layer. The
      number of hidden unit double after each layer. Default to 64 per the
      original implementation.

  Returns:
    Tensor of shape (batch_size, h / 64, w / 64, 2048) as features.
  """

  num_outputs = init_num_outputs

  with tf.variable_scope('discriminator_input_hidden'):

    hidden = input_net

    for i in range(hidden_layer):

      hidden = ops.pad(hidden, 1)
      hidden = tf.contrib.layers.conv2d(
          inputs=hidden,
          num_outputs=num_outputs,
          kernel_size=4,
          stride=2,
          padding='VALID',
          activation_fn=None,
          normalizer_fn=None,
          scope='conv_{}'.format(i))
      hidden = tf.nn.leaky_relu(hidden, alpha=0.01)

      num_outputs = 2 * num_outputs

  return hidden


def discriminator_output_source(input_net):
  """Output Layer for Source in the Discriminator.

  Determine if the image is real/fake based on the feature extracted. We follow
  the original paper design where the output is not a simple (batch_size) shape
  Tensor but rather a (batch_size, 2, 2, 2048) shape Tensor. We will get the
  correct shape later when we piece things together.

  PyTorch Version:
  https://github.com/yunjey/StarGAN/blob/fbdb6a6ce2a4a92e1dc034faec765e0dbe4b8164/model.py#L79

  Args:
    input_net: Tensor of shape (batch_size, h / 64, w / 64, 2048) as features.

  Returns:
    Tensor of shape (batch_size, h / 64, w / 64, 1) as the score.
  """

  with tf.variable_scope('discriminator_output_source'):

    output_src = ops.pad(input_net, 1)
    output_src = tf.contrib.layers.conv2d(
        inputs=output_src,
        num_outputs=1,
        kernel_size=3,
        stride=1,
        padding='VALID',
        activation_fn=None,
        normalizer_fn=None,
        biases_initializer=None,
        scope='conv')

  return output_src


def discriminator_output_class(input_net, class_num):
  """Output Layer for Domain Classification in the Discriminator.

  The original paper use convolution layer where the kernel size is the height
  and width of the Tensor. We use an equivalent operation here where we first
  flatten the Tensor to shape (batch_size, K) and a fully connected layer.

  PyTorch Version:
  https://github.com/yunjey/StarGAN/blob/fbdb6a6ce2a4a92e1dc034faec765e0dbe4b8164/model.py#L80https

  Args:
    input_net: Tensor of shape (batch_size, h / 64, w / 64, 2028).
    class_num: Number of output classes to be predicted.

  Returns:
    Tensor of shape (batch_size, class_num).
  """

  with tf.variable_scope('discriminator_output_class'):

    output_cls = tf.contrib.layers.flatten(input_net, scope='flatten')
    output_cls = tf.contrib.layers.fully_connected(
        inputs=output_cls,
        num_outputs=class_num,
        activation_fn=None,
        normalizer_fn=None,
        biases_initializer=None,
        scope='fully_connected')

  return output_cls