More code that reproduces the paper can be found at https://github.com/ppwwyyxx/GroupNorm-reproduce/.
(x, group=32, gamma_initializer=tf.constant_initializer(1.))
| 15 | |
| 16 | @layer_register(log_shape=True) |
| 17 | def GroupNorm(x, group=32, gamma_initializer=tf.constant_initializer(1.)): |
| 18 | """ |
| 19 | More code that reproduces the paper can be found at https://github.com/ppwwyyxx/GroupNorm-reproduce/. |
| 20 | """ |
| 21 | shape = x.get_shape().as_list() |
| 22 | ndims = len(shape) |
| 23 | assert ndims == 4, shape |
| 24 | chan = shape[1] |
| 25 | assert chan % group == 0, chan |
| 26 | group_size = chan // group |
| 27 | |
| 28 | orig_shape = tf.shape(x) |
| 29 | h, w = orig_shape[2], orig_shape[3] |
| 30 | |
| 31 | x = tf.reshape(x, tf.stack([-1, group, group_size, h, w])) |
| 32 | |
| 33 | mean, var = tf.nn.moments(x, [2, 3, 4], keep_dims=True) |
| 34 | |
| 35 | new_shape = [1, group, group_size, 1, 1] |
| 36 | |
| 37 | beta = tf.get_variable('beta', [chan], initializer=tf.constant_initializer()) |
| 38 | beta = tf.reshape(beta, new_shape) |
| 39 | |
| 40 | gamma = tf.get_variable('gamma', [chan], initializer=gamma_initializer) |
| 41 | gamma = tf.reshape(gamma, new_shape) |
| 42 | |
| 43 | out = tf.nn.batch_normalization(x, mean, var, beta, gamma, 1e-5, name='output') |
| 44 | return tf.reshape(out, orig_shape, name='output') |
| 45 | |
| 46 | |
| 47 | def freeze_affine_getter(getter, *args, **kwargs): |
no test coverage detected
searching dependent graphs…