|
1 | 1 | """Batch Normalization for TensorFlow. |
2 | | -Parag K. Mital, Jan 2016.""" |
| 2 | +Parag K. Mital, Jan 2016. |
| 3 | +""" |
3 | 4 |
|
4 | 5 | import tensorflow as tf |
| 6 | +from tensorflow.python import control_flow_ops |
5 | 7 |
|
6 | 8 |
|
7 | | -class batch_norm(object): |
8 | | - """Basic usage from: http://stackoverflow.com/a/33950177 |
9 | | -
|
10 | | - Parag K. Mital, Jan 2016 |
11 | | -
|
12 | | - Attributes |
13 | | - ---------- |
14 | | - batch_size : int |
15 | | - Size of the batch. Set to -1 to fit to current net. |
16 | | - beta : Tensor |
17 | | - A 1D beta Tensor with size matching the last dimension of t. |
18 | | - An offset to be added to the normalized tensor. |
19 | | - ema : tf.train.ExponentialMovingAverage |
20 | | - For computing the moving average. |
21 | | - epsilon : float |
22 | | - A small float number to avoid dividing by 0. |
23 | | - gamma : Tensor |
24 | | - If "scale_after_normalization" is true, this tensor will be multiplied |
25 | | - with the normalized tensor. |
26 | | - momentum : float |
27 | | - The decay to use for the moving average. |
28 | | - name : str |
29 | | - The variable scope for all variables under batch normalization. |
| 9 | +def batch_norm(x, phase_train, scope='bn', affine=True): |
30 | 10 | """ |
| 11 | + Batch normalization on convolutional maps. |
31 | 12 |
|
32 | | - def __init__(self, batch_size, epsilon=1e-5, |
33 | | - momentum=0.1, name="batch_norm"): |
34 | | - """Summary |
35 | | -
|
36 | | - Parameters |
37 | | - ---------- |
38 | | - batch_size : int |
39 | | - Size of the batch, or -1 for size to fit. |
40 | | - epsilon : float, optional |
41 | | - A small float number to avoid dividing by 0. |
42 | | - momentum : float, optional |
43 | | - Decay to use for the moving average. |
44 | | - name : str, optional |
45 | | - Variable scope will be under this prefix. |
46 | | - """ |
47 | | - with tf.variable_scope(name) as scope: |
48 | | - self.epsilon = epsilon |
49 | | - self.momentum = momentum |
50 | | - self.batch_size = batch_size |
51 | | - self.ema = tf.train.ExponentialMovingAverage(decay=self.momentum) |
52 | | - self.name = name |
53 | | - |
54 | | - def __call__(self, x, train=True): |
55 | | - """Applies/updates the BN to the input Tensor. |
| 13 | + from: https://stackoverflow.com/questions/33949786/how-could-i- |
| 14 | + use-batch-normalization-in-tensorflow |
56 | 15 |
|
57 | | - Parameters |
58 | | - ---------- |
59 | | - x : Tensor |
60 | | - The input tensor to normalize. |
61 | | - train : bool, optional |
62 | | - Whether or not to train parameters. |
| 16 | + Only modified to infer shape from input tensor x. |
63 | 17 |
|
64 | | - Returns |
65 | | - ------- |
66 | | - x_normed : Tensor |
67 | | - The normalized Tensor. |
68 | | - """ |
| 18 | + Parameters |
| 19 | + ---------- |
| 20 | + x |
| 21 | + Tensor, 4D BHWD input maps |
| 22 | + phase_train |
| 23 | + boolean tf.Variable, true indicates training phase |
| 24 | + scope |
| 25 | + string, variable scope |
| 26 | + affine |
| 27 | + whether to affine-transform outputs |
| 28 | +
|
| 29 | + Return |
| 30 | + ------ |
| 31 | + normed |
| 32 | + batch-normalized maps |
| 33 | + """ |
| 34 | + with tf.variable_scope(scope): |
69 | 35 | shape = x.get_shape().as_list() |
70 | 36 |
|
71 | | - # Using a variable scope means any new variables |
72 | | - # will be prefixed with "variable_scope/", e.g.: |
73 | | - # "variable_scope/new_variable". Also, using |
74 | | - # TensorBoard, this will make everything very |
75 | | - # nicely grouped. |
76 | | - with tf.variable_scope(self.name) as scope: |
77 | | - self.gamma = tf.get_variable( |
78 | | - "gamma", [shape[-1]], |
79 | | - initializer=tf.random_normal_initializer(1., 0.02)) |
80 | | - self.beta = tf.get_variable( |
81 | | - "beta", [shape[-1]], |
82 | | - initializer=tf.constant_initializer(0.)) |
83 | | - |
84 | | - mean, variance = tf.nn.moments(x, [0, 1, 2]) |
85 | | - |
86 | | - return tf.nn.batch_norm_with_global_normalization( |
87 | | - x, mean, variance, self.beta, self.gamma, self.epsilon, |
88 | | - scale_after_normalization=True) |
| 37 | + beta = tf.Variable(tf.constant(0.0, shape=[shape[-1]]), |
| 38 | + name='beta', trainable=True) |
| 39 | + gamma = tf.Variable(tf.constant(1.0, shape=[shape[-1]]), |
| 40 | + name='gamma', trainable=affine) |
| 41 | + |
| 42 | + batch_mean, batch_var = tf.nn.moments(x, [0, 1, 2], name='moments') |
| 43 | + ema = tf.train.ExponentialMovingAverage(decay=0.9) |
| 44 | + ema_apply_op = ema.apply([batch_mean, batch_var]) |
| 45 | + ema_mean, ema_var = ema.average(batch_mean), ema.average(batch_var) |
| 46 | + |
| 47 | + def mean_var_with_update(): |
| 48 | + """Summary |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + name : TYPE |
| 53 | + Description |
| 54 | + """ |
| 55 | + with tf.control_dependencies([ema_apply_op]): |
| 56 | + return tf.identity(batch_mean), tf.identity(batch_var) |
| 57 | + mean, var = control_flow_ops.cond(phase_train, |
| 58 | + mean_var_with_update, |
| 59 | + lambda: (ema_mean, ema_var)) |
| 60 | + |
| 61 | + normed = tf.nn.batch_norm_with_global_normalization( |
| 62 | + x, mean, var, beta, gamma, 1e-3, affine) |
| 63 | + return normed |
0 commit comments