import tensorflow as tf


        out += aux

    out = activation(out)

    if dropout > 0:
        out = tf.layers.dropout(out, rate=dropout, training=training)

    if sum(dim[2]) > 2:
        out = deconv2d(out, [2*dim[0], dim[1], dim[2]], scope="%s_conv_out"%scope, training=training, ema=ema, init=init)
    else:
        out = conv2d(out, [2*dim[0], dim[1], dim[2]], scope="%s_conv_out"%scope, training=training, ema=ema, init=init)

    h_stack1, h_stack2 = tf.split(out, 2, 3)
    sigmoid_out = tf.sigmoid(h_stack2)
    out = (h_stack1 * sigmoid_out)

    out_shp = out.get_shape().as_list()
    if out_shp[1:-1] < in_shp[1:-1]:
        x = tf.nn.avg_pool(x, [1, dim[2][0], dim[2][1], 1], strides=[1, dim[2][0], dim[2][1], 1], padding='SAME')
    elif out_shp[1:-1] > in_shp[1:-1]:
        warnings.warn(
            "The height and width of the output are larger than the input. There will be no residual connection.")
        residual = False


