def atrous_conv2d(name, x, w=None, num_filters=16, kernel_size=(3, 3), padding='SAME', dilation_rate=1, initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, bias=0.0, activation=None, batchnorm_enabled=False, max_pool_enabled=False, dropout_keep_prob=-1, is_training=True): """ This block is responsible for a Dilated convolution 2D layer followed by optional (non-linearity, dropout, max-pooling). Note that: "is_training" should be passed by a correct value based on being in either training or testing. :param name: (string) The name scope provided by the upper tf.name_scope('name') as scope. :param x: (tf.tensor) The input to the layer (N, H, W, C). :param w: :param num_filters: (integer) No. of filters (This is the output depth) :param kernel_size: (integer tuple) The size of the convolving kernel. :param padding: (string) The amount of padding required. :param dilation_rate: (integer) The amount of dilation required. If equals 1, it means normal convolution. :param initializer: (tf.contrib initializer) The initialization scheme, He et al. normal or Xavier normal are recommended. :param l2_strength:(weight decay) (float) L2 regularization parameter. :param bias: (float) Amount of bias. :param activation: (tf.graph operator) The activation function applied after the convolution operation. If None, linear is applied. :param batchnorm_enabled: (boolean) for enabling batch normalization. :param max_pool_enabled: (boolean) for enabling max-pooling 2x2 to decrease width and height by a factor of 2. :param dropout_keep_prob: (float) for the probability of keeping neurons. If equals -1, it means no dropout :param is_training: (boolean) to diff. between training and testing (important for batch normalization and dropout) :return: The output tensor of the layer (N, H', W', C'). """ with tf.variable_scope(name) as scope: conv_o_b = __atrous_conv2d_p(scope, x=x, w=w, num_filters=num_filters, kernel_size=kernel_size, padding=padding, dilation_rate=dilation_rate, initializer=initializer, l2_strength=l2_strength, bias=bias) if batchnorm_enabled: conv_o_bn = tf.layers.batch_normalization(conv_o_b, training=is_training) if not activation: conv_a = conv_o_bn else: conv_a = activation(conv_o_bn) else: if not activation: conv_a = conv_o_b else: conv_a = activation(conv_o_b) def dropout_with_keep(): return tf.nn.dropout(conv_a, dropout_keep_prob) def dropout_no_keep(): return tf.nn.dropout(conv_a, 1.0) if dropout_keep_prob != -1: conv_o_dr = tf.cond(is_training, dropout_with_keep, dropout_no_keep) else: conv_o_dr = conv_a conv_o = conv_o_dr if max_pool_enabled: conv_o = max_pool_2d(conv_o_dr) return conv_o
def conv2d(name, x, w=None, num_filters=16, kernel_size=(3, 3), padding='SAME', stride=(1, 1), initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, bias=0.0, activation=None, batchnorm_enabled=False, max_pool_enabled=False, dropout_keep_prob=-1, is_training=True): """ This block is responsible for a convolution 2D layer followed by optional (non-linearity, dropout, max-pooling). Note that: "is_training" should be passed by a correct value based on being in either training or testing. :param name: (string) 由tf.name_scope('name') as scope提供. :param x: (tf.tensor)输入(N, H, W, C). :param num_filters: (integer) No. of filters (输出的深度) :param kernel_size: (integer tuple) 卷积核的大小. :param padding: (string) padding的大小. :param stride: (integer tuple) 步长. :param initializer: (tf.contrib initializer) 初始化, He et al. normal or Xavier normal are recommended. :param l2_strength:(weight decay) (float) L2 正则化参数. :param bias: (float) 偏置. :param activation: (tf.graph operator) 卷积运算后的激活函数. If None, linear is applied. :param batchnorm_enabled: (boolean) for enabling batch normalization. :param max_pool_enabled: (boolean) for enabling max-pooling 2x2 to decrease width and height by a factor of 2. :param dropout_keep_prob: (float) 保持多少个神经元的概率. If equals -1, it means no dropout :param is_training: (boolean) to diff. between training and testing (important for batch normalization and dropout) :return: The output tensor of the layer (N, H', W', C'). """ with tf.variable_scope(name) as scope: conv_o_b = __conv2d_p(scope, x=x, w=w, num_filters=num_filters, kernel_size=kernel_size, stride=stride, padding=padding, initializer=initializer, l2_strength=l2_strength, bias=bias) if batchnorm_enabled: conv_o_bn = tf.layers.batch_normalization(conv_o_b, training=is_training) if not activation: conv_a = conv_o_bn else: conv_a = activation(conv_o_bn) else: if not activation: conv_a = conv_o_b else: conv_a = activation(conv_o_b) def dropout_with_keep(): return tf.nn.dropout(conv_a, dropout_keep_prob) def dropout_no_keep(): return tf.nn.dropout(conv_a, 1.0) if dropout_keep_prob != -1: conv_o_dr = tf.cond(is_training, dropout_with_keep, dropout_no_keep) else: conv_o_dr = conv_a conv_o = conv_o_dr if max_pool_enabled: conv_o = max_pool_2d(conv_o_dr) return conv_o