def layer_op(self, random_source, image_size, conditioning, is_training):
    keep_prob_ph = 1
    add_noise = self.noise_channels_per_layer
    if (conditioning is not None):
        conditioning_channels = conditioning.shape.as_list()[(- 1)]
        conditioning_channels = (conditioning_channels + add_noise)
    else:
        conditioning_channels = add_noise
    ch = [512]
    sz = image_size[:(- 1)]
    for i in range(4):
        new_ch = (ch[(- 1)] + (conditioning_channels * self.with_conditionings[i]))
        new_ch = round((new_ch / 2))
        ch.append(new_ch)
        sz = [int(round((spatial_len / 2))) for spatial_len in sz]
    ch.append(1)
    spatial_rank = (len(image_size) - 1)
    if (spatial_rank == 3):

        def resize_func(x, sz):
            sz_x = x.shape.as_list()
            r1 = tf.image.resize_images(tf.reshape(x, (sz_x[:3] + [(- 1)])), sz[0:2])
            r2 = tf.image.resize_images(tf.reshape(r1, [sz_x[0], (sz[0] * sz[1]), sz_x[3], (- 1)]), [(sz[0] * sz[1]), sz[2]])
            resized_3d = tf.reshape(r2, (([sz_x[0]] + sz) + [sz_x[(- 1)]]))
            return resized_3d
    elif (spatial_rank == 2):
        resize_func = tf.image.resize_bilinear

    def concat_cond(x, with_conditioning):
        noise = []
        if add_noise:
            feature_shape = x.shape.as_list()[0:(- 1)]
            noise_shape = (feature_shape + [add_noise])
            noise = [tf.random_normal(noise_shape, 0.0, 0.1)]
        if (with_conditioning and (conditioning is not None)):
            with tf.name_scope('concat_conditioning'):
                spatial_shape = x.shape.as_list()[1:(- 1)]
                resized_cond = resize_func(conditioning, spatial_shape)
                return tf.concat(([x, resized_cond] + noise), axis=(- 1))
        return x

    def conv(ch, x):
        with tf.name_scope('conv'):
            conv_layer = ConvolutionalLayer(n_output_chns=ch, kernel_size=3, feature_normalization='batch', with_bias=False, acti_func='relu', w_initializer=self.initializers['w'])
            return conv_layer(x, is_training=is_training)

    def up(ch, x):
        with tf.name_scope('up'):
            deconv_layer = DeconvolutionalLayer(n_output_chns=ch, kernel_size=3, stride=2, feature_normalization='batch', with_bias=False, acti_func='relu', w_initializer=self.initializers['w'])
            return deconv_layer(x, is_training=is_training)

    def up_block(ch, x, with_conditioning):
        with tf.name_scope('up_block'):
            u = up(ch, x)
            cond = concat_cond(u, with_conditioning)
            return conv(cond.shape.as_list()[(- 1)], cond)

    def noise_to_image(sz, ch, rand_tensor, with_conditioning):
        batch_size = rand_tensor.shape.as_list()[0]
        output_shape = (([batch_size] + sz) + [ch])
        with tf.name_scope('noise_to_image'):
            g_no_0 = (np.prod(sz) * ch)
            fc_layer = FullyConnectedLayer(n_output_chns=g_no_0, feature_normalization=None, with_bias=True, w_initializer=self.initializers['w'], b_initializer=self.initializers['b'])
            g_h1p = fc_layer(rand_tensor, keep_prob=keep_prob_ph)
            g_h1p = tf.reshape(g_h1p, output_shape)
            g_h1p = concat_cond(g_h1p, with_conditioning)
            return conv((ch + conditioning_channels), g_h1p)

    def final_image(n_chns, x):
        with tf.name_scope('final_image'):
            if (add_noise > 0):
                feature_shape = x.shape.as_list()[0:(- 1)]
                noise_shape = (feature_shape + [add_noise])
                noise = tf.random_normal(noise_shape, 0, 0.1)
                x = tf.concat([x, noise], axis=3)
            conv_layer = ConvolutionalLayer(n_output_chns=n_chns, kernel_size=3, acti_func='tanh', feature_normalization=None, with_bias=True, w_initializer=self.initializers['w'], b_initializer=self.initializers['b'])
            x_sample = conv_layer(x, is_training=is_training, keep_prob=keep_prob_ph)
            return tf.image.resize_images(x_sample, image_size[:(- 1)])
    flow = random_source
    for (idx, chns) in enumerate(ch):
        if (idx == 0):
            flow = noise_to_image(sz, chns, flow, self.with_conditionings[idx])
        elif (idx == (len(ch) - 1)):
            return final_image(chns, flow)
        else:
            flow = up_block(chns, flow, self.with_conditionings[idx])
def layer_op(self, random_source, image_size, conditioning, is_training):
    keep_prob_ph = 1
    add_noise = self.noise_channels_per_layer
    if (conditioning is not None):
        conditioning_channels = conditioning.shape.as_list()[(- 1)]
        conditioning_channels = (conditioning_channels + add_noise)
    else:
        conditioning_channels = add_noise
    ch = [512]
    sz = image_size[:(- 1)]
    for i in range(4):
        new_ch = (ch[(- 1)] + (conditioning_channels * self.with_conditionings[i]))
        new_ch = round((new_ch / 2))
        ch.append(new_ch)
        sz = [int(round((spatial_len / 2))) for spatial_len in sz]
    ch.append(1)
    spatial_rank = (len(image_size) - 1)
    if (spatial_rank == 3):

        def resize_func(x, sz):
            sz_x = x.shape.as_list()
            r1 = tf.image.resize_images(tf.reshape(x, (sz_x[:3] + [(- 1)])), sz[0:2])
            r2 = tf.image.resize_images(tf.reshape(r1, [sz_x[0], (sz[0] * sz[1]), sz_x[3], (- 1)]), [(sz[0] * sz[1]), sz[2]])
            resized_3d = tf.reshape(r2, (([sz_x[0]] + sz) + [sz_x[(- 1)]]))
            return resized_3d
    elif (spatial_rank == 2):
        resize_func = tf.image.resize_bilinear

    def concat_cond(x, with_conditioning):
        noise = []
        if add_noise:
            feature_shape = x.shape.as_list()[0:(- 1)]
            noise_shape = (feature_shape + [add_noise])
            noise = [tf.random_normal(noise_shape, 0.0, 0.1)]
        if (with_conditioning and (conditioning is not None)):
            with tf.name_scope('concat_conditioning'):
                spatial_shape = x.shape.as_list()[1:(- 1)]
                resized_cond = resize_func(conditioning, spatial_shape)
                return tf.concat(([x, resized_cond] + noise), axis=(- 1))
        return x

    def conv(ch, x):
        with tf.name_scope('conv'):
            conv_layer = ConvolutionalLayer(n_output_chns=ch, kernel_size=3, feature_normalization='batch', with_bias=False, acti_func='relu', w_initializer=self.initializers['w'])
            return conv_layer(x, is_training=is_training)

    def up(ch, x):
        with tf.name_scope('up'):
            deconv_layer = DeconvolutionalLayer(n_output_chns=ch, kernel_size=3, stride=2, feature_normalization='batch', with_bias=False, acti_func='relu', w_initializer=self.initializers['w'])
            return deconv_layer(x, is_training=is_training)

    def up_block(ch, x, with_conditioning):
        with tf.name_scope('up_block'):
            u = up(ch, x)
            cond = concat_cond(u, with_conditioning)
            return conv(cond.shape.as_list()[(- 1)], cond)

    def noise_to_image(sz, ch, rand_tensor, with_conditioning):
        batch_size = rand_tensor.shape.as_list()[0]
        output_shape = (([batch_size] + sz) + [ch])
        with tf.name_scope('noise_to_image'):
            g_no_0 = (np.prod(sz) * ch)
            fc_layer = FullyConnectedLayer(n_output_chns=g_no_0, feature_normalization=None, with_bias=True, w_initializer=self.initializers['w'], b_initializer=self.initializers['b'])
            g_h1p = fc_layer(rand_tensor, keep_prob=keep_prob_ph)
            g_h1p = tf.reshape(g_h1p, output_shape)
            g_h1p = concat_cond(g_h1p, with_conditioning)
            return conv((ch + conditioning_channels), g_h1p)

    def final_image(n_chns, x):
        with tf.name_scope('final_image'):
            if (add_noise > 0):
                feature_shape = x.shape.as_list()[0:(- 1)]
                noise_shape = (feature_shape + [add_noise])
                noise = tf.random_normal(noise_shape, 0, 0.1)
                x = tf.concat([x, noise], axis=3)
            conv_layer = ConvolutionalLayer(n_output_chns=n_chns, kernel_size=3, acti_func='tanh', feature_normalization=None, with_bias=True, w_initializer=self.initializers['w'], b_initializer=self.initializers['b'])
            x_sample = conv_layer(x, is_training=is_training, keep_prob=keep_prob_ph)
            return tf.image.resize_images(x_sample, image_size[:(- 1)])
    flow = random_source
    for (idx, chns) in enumerate(ch):
        if (idx == 0):
            flow = noise_to_image(sz, chns, flow, self.with_conditionings[idx])
        elif (idx == (len(ch) - 1)):
            return final_image(chns, flow)
        else:
            flow = up_block(chns, flow, self.with_conditionings[idx])