コード例 #1
0
    def layer_op(self, codes, is_training):
        """

        :param codes: tensor, input latent space
        :param is_training: boolean, True if network is in training mode
        :return: samples from posterior distribution, means and log variances of the posterior distribution
        """
        def clip(input):
            # This is for clipping logvars,
            # so that variances = exp(logvars) behaves well
            output = tf.maximum(input, self.logvars_lower_bound)
            output = tf.minimum(output, self.logvars_upper_bound)
            return output

        encoder_means = FullyConnectedLayer(
            n_output_chns=self.number_of_latent_variables,
            feature_normalization=None,
            acti_func=None,
            w_initializer=self.initializers['w'],
            w_regularizer=self.regularizers['w'],
            name='encoder_fc_means_{}'.format(self.number_of_latent_variables))
        print(encoder_means)

        encoder_logvars = FullyConnectedLayer(
            n_output_chns=self.number_of_latent_variables,
            feature_normalization=None,
            acti_func=None,
            w_initializer=self.initializers['w'],
            w_regularizer=self.regularizers['w'],
            name='encoder_fc_logvars_{}'.format(
                self.number_of_latent_variables))
        print(encoder_logvars)

        # Predict the posterior distribution's parameters
        posterior_means = encoder_means(codes, is_training)
        posterior_logvars = clip(encoder_logvars(codes, is_training))

        if self.number_of_samples == 1:
            noise_sample = tf.random_normal(tf.shape(posterior_means), 0.0,
                                            1.0)
        else:
            sample_shape = tf.concat([
                tf.constant(self.number_of_samples, shape=[
                    1,
                ]),
                tf.shape(posterior_means)
            ],
                                     axis=0)
            noise_sample = tf.reduce_mean(tf.random_normal(
                sample_shape, 0.0, 1.0),
                                          axis=0)

        return [
            posterior_means + tf.exp(0.5 * posterior_logvars) * noise_sample,
            posterior_means, posterior_logvars
        ]
コード例 #2
0
    def layer_op(self, images, is_training, bn_momentum=0.9, layer_id=-1):
        block1 = ConvPoolBlock((self.num_features[0], self.num_features[0]),
                               ((1, 3, 3), (1, 3, 3)),
                               w_initializer=self.initializers['w'],
                               w_regularizer=self.regularizers['w'],
                               acti_func=self.acti_func,
                               name='B1')
        block2 = ConvPoolBlock((self.num_features[1], self.num_features[1]),
                               ((1, 3, 3), (1, 3, 3)),
                               w_initializer=self.initializers['w'],
                               w_regularizer=self.regularizers['w'],
                               acti_func=self.acti_func,
                               name='B2')

        block3 = ConvPoolBlock((self.num_features[2], self.num_features[2]),
                               ((1, 3, 3), (1, 3, 3), (1, 3, 3)),
                               w_initializer=self.initializers['w'],
                               w_regularizer=self.regularizers['w'],
                               acti_func=self.acti_func,
                               name='B3')

        block4 = ConvPoolBlock((self.num_features[3], self.num_features[3]),
                               ((1, 3, 3), (1, 3, 3)),
                               w_initializer=self.initializers['w'],
                               w_regularizer=self.regularizers['w'],
                               acti_func=self.acti_func,
                               name='B4')

        block5 = ConvPoolBlock((self.num_features[4], self.num_features[4]),
                               ((1, 3, 3), (1, 3, 3)),
                               w_initializer=self.initializers['w'],
                               w_regularizer=self.regularizers['w'],
                               acti_func=self.acti_func,
                               name='B5')

        fc1 = FullyConnectedLayer(self.num_features[5],
                                  w_initializer=self.initializers['w'],
                                  w_regularizer=self.regularizers['w'],
                                  acti_func=self.acti_func,
                                  name='fc1')
        fc2 = FullyConnectedLayer(self.num_out,
                                  w_initializer=w_initializer_near_zero(),
                                  w_regularizer=self.regularizers['w'],
                                  with_bn=False,
                                  acti_func=self.acti_func,
                                  name='fc2')
        output = block1(images, is_training, bn_momentum)
        output = block2(output, is_training, bn_momentum)
        output = block3(output, is_training, bn_momentum)
        output = block4(output, is_training, bn_momentum)
        output = block5(output, is_training, bn_momentum)

        output = tf.reshape(output, [self.batch_size, -1])
        output = fc1(output, is_training, bn_momentum)
        output = fc2(output, is_training, bn_momentum)
        return output
コード例 #3
0
    def layer_op(self, features):
        batch_size = features.shape.as_list()[0]
        conv_1 = ConvolutionalLayer(
            20, 3, with_bn=False, with_bias=True, acti_func='relu')
        fc_1 = FullyConnectedLayer(
            20, with_bn=False, with_bias=True, acti_func='relu')
        fc_2 = FullyConnectedLayer(
            2, with_bn=False, with_bias=True)

        hidden_feature = conv_1(features, is_training=True)
        hidden_feature = tf.reshape(hidden_feature, [batch_size, -1])
        hidden_feature = fc_1(hidden_feature, is_training=True)
        logits = fc_2(hidden_feature, is_training=True)
        return logits
コード例 #4
0
 def fully_connected(ch, features):
     with tf.name_scope('fully_connected'):
         # with bn?
         fc_layer = FullyConnectedLayer(n_output_chns=ch,
                                        with_bn=False,
                                        with_bias=True)
         return fc_layer(features, is_training=is_training)
コード例 #5
0
    def layer_op(self, codes, is_training):
        """

        :param codes: tensor, input latent codes
        :param is_training: boolean, True if network is in training mode
        :return: tensor, output of series of FC layers
        """

        # Define the decoding fully-connected layers
        decoders_fc = []
        for i in range(0, len(self.layer_sizes_decoder)):
            decoders_fc.append(
                FullyConnectedLayer(n_output_chns=self.layer_sizes_decoder[i],
                                    with_bias=True,
                                    feature_normalization='batch',
                                    acti_func=self.acti_func_decoder[i],
                                    w_initializer=self.initializers['w'],
                                    w_regularizer=self.regularizers['w'],
                                    name='FCDecoder_fc_{}'.format(
                                        self.layer_sizes_decoder[i])))
            print(decoders_fc[-1])

        # Fully-connected decoder layers
        flow = codes
        for i in range(0, len(self.layer_sizes_decoder)):
            flow = decoders_fc[i](flow, is_training)

        return flow
コード例 #6
0
    def layer_op(self, input_tensor):
        # spatial squeeze
        input_rank = len(input_tensor.shape)
        reduce_indices = list(range(input_rank))[1:-1]
        if self.func == 'AVG':
            squeeze_tensor = tf.reduce_mean(input_tensor, axis=reduce_indices)
        elif self.func == 'MAX':
            squeeze_tensor = tf.reduce_max(input_tensor, axis=reduce_indices)
        else:
            raise NotImplementedError("pooling function not supported")

        # channel excitation
        num_channels = int(squeeze_tensor.shape[-1])
        reduction_ratio = self.reduction_ratio
        if num_channels % reduction_ratio != 0:
            raise ValueError(
                "reduction ratio incompatible with "
                "number of input tensor channels")

        num_channels_reduced = num_channels / reduction_ratio
        fc1 = FullyConnectedLayer(num_channels_reduced,
                                  with_bias=False,
                                  with_bn=False,
                                  acti_func='relu',
                                  name='se_fc_1')
        fc2 = FullyConnectedLayer(num_channels,
                                  with_bias=False,
                                  with_bn=False,
                                  acti_func='sigmoid',
                                  name='se_fc_2')

        fc_out_1 = fc1(squeeze_tensor)
        fc_out_2 = fc2(fc_out_1)

        while len(fc_out_2.shape) < input_rank:
            fc_out_2 = tf.expand_dims(fc_out_2, axis=1)

        output_tensor = tf.multiply(input_tensor, fc_out_2)

        return output_tensor
コード例 #7
0
 def fully_connected(ch, features):
     """
     Final discriminator processing block
     :param ch: int, number of output channels
     :param features: tensor, input features for final classification
     :return: tensor, output logits of discriminator classification
     """
     with tf.name_scope('fully_connected'):
         # with bn?
         fc_layer = FullyConnectedLayer(n_output_chns=ch,
                                        feature_normalization=None,
                                        with_bias=True)
         return fc_layer(features, is_training=is_training)
コード例 #8
0
 def noise_to_image(sz, ch, rand_tensor, with_conditioning):
     batch_size = rand_tensor.get_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,
             with_bn=False,
             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)
コード例 #9
0
    def _test_fc_layer_output_shape(self,
                                    rank,
                                    param_dict,
                                    output_shape,
                                    is_training=None,
                                    dropout_prob=None):
        if rank == 2:
            input_data = self.get_2d_input()
        elif rank == 3:
            input_data = self.get_3d_input()

        fc_layer = FullyConnectedLayer(**param_dict)
        output_data = fc_layer(input_data,
                               is_training=is_training,
                               keep_prob=dropout_prob)
        print(fc_layer)
        with self.cached_session() as sess:
            sess.run(tf.global_variables_initializer())
            output_value = sess.run(output_data)
            self.assertAllClose(output_shape, output_value.shape)
コード例 #10
0
    def layer_op(self, codes, is_training):

        # Define the decoding fully-connected layers
        decoders_fc = []
        for i in range(0, len(self.layer_sizes_decoder)):
            decoders_fc.append(FullyConnectedLayer(
                n_output_chns=self.layer_sizes_decoder[i],
                with_bias=True,
                with_bn=True,
                acti_func=self.acti_func_decoder[i],
                w_initializer=self.initializers['w'],
                w_regularizer=self.regularizers['w'],
                name='FCDecoder_fc_{}'.format(self.layer_sizes_decoder[i])))
            print(decoders_fc[-1])

        # Fully-connected decoder layers
        flow = codes
        for i in range(0, len(self.layer_sizes_decoder)):
            flow = decoders_fc[i](flow, is_training)

        return flow
コード例 #11
0
 def noise_to_image(sz, ch, rand_tensor, with_conditioning):
     """
     Processes random noise with fully connected layer and then convolutional layer
     :param sz: image size
     :param ch: int, number of output channels
     :param rand_tensor: tensor, input random noise to generate image
     :param with_conditioning: boolean, True if conditioning is to be used
     :return: tensor, output of convolutional layer
     """
     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)
コード例 #12
0
    def layer_op(self, codes, is_training):

        # Define the decoding fully-connected layers
        decoders_fc = []
        for i in range(0, len(self.layer_sizes_decoder)):
            decoders_fc.append(FullyConnectedLayer(
                n_output_chns=self.layer_sizes_decoder[i],
                with_bias=True,
                with_bn=True,
                acti_func=self.acti_func_decoder[i],
                w_initializer=self.initializers['w'],
                w_regularizer=self.regularizers['w'],
                name='decoder_fc_{}'.format(self.layer_sizes_decoder[i])))
            print(decoders_fc[-1])

        # Define the decoding convolutional layers
        decoders_cnn = []
        decoders_upsamplers = []
        for i in range(0, len(self.trans_conv_output_channels)):
            if self.upsampling_mode == 'DECONV':
                decoders_upsamplers.append(DeconvolutionalLayer(
                    n_output_chns=self.trans_conv_output_channels[i],
                    kernel_size=self.trans_conv_unpooling_factors[i],
                    stride=self.trans_conv_unpooling_factors[i],
                    padding='SAME',
                    with_bias=True,
                    with_bn=True,
                    w_initializer=self.initializers['w'],
                    w_regularizer=None,
                    acti_func=None,
                    name='decoder_upsampler_{}_{}'.format(
                        self.trans_conv_unpooling_factors[i],
                        self.trans_conv_unpooling_factors[i])))
                print(decoders_upsamplers[-1])

            decoders_cnn.append(DeconvolutionalLayer(
                n_output_chns=self.trans_conv_output_channels[i],
                kernel_size=self.trans_conv_kernel_sizes[i],
                stride=1,
                padding='SAME',
                with_bias=True,
                with_bn=True,
                #with_bn=not (i == len(self.trans_conv_output_channels) - 1),
                # No BN on output
                w_initializer=self.initializers['w'],
                w_regularizer=None,
                acti_func=self.acti_func_trans_conv[i],
                name='decoder_trans_conv_{}_{}'.format(
                    self.trans_conv_kernel_sizes[i],
                    self.trans_conv_output_channels[i])))
            print(decoders_cnn[-1])

        # Fully-connected decoder layers
        flow = codes
        for i in range(0, len(self.layer_sizes_decoder)):
            flow = decoders_fc[i](flow, is_training)

        # Reconstitute the feature maps
        flow = tf.reshape(flow, [-1] + self.downsampled_shape)

        # Convolutional decoder layers
        for i in range(0, len(self.trans_conv_output_channels)):
            if self.upsampling_mode == 'DECONV':
                flow = decoders_upsamplers[i](flow, is_training)
            elif self.upsampling_mode == 'CHANNELWISE_DECONV':
                flow = UpSampleLayer(
                    'CHANNELWISE_DECONV',
                    kernel_size=self.trans_conv_unpooling_factors[i],
                    stride=self.trans_conv_unpooling_factors[i])(flow)
            elif self.upsampling_mode == 'REPLICATE':
                flow = UpSampleLayer(
                    'REPLICATE',
                    kernel_size=self.trans_conv_unpooling_factors[i],
                    stride=self.trans_conv_unpooling_factors[i])(flow)
            flow = decoders_cnn[i](flow, is_training)

        return flow
コード例 #13
0
    def layer_op(self, images, is_training):

        # Define the encoding convolutional layers
        encoders_cnn = []
        encoders_downsamplers = []
        for i in range(0, len(self.conv_output_channels)):
            encoders_cnn.append(ConvolutionalLayer(
                n_output_chns=self.conv_output_channels[i],
                kernel_size=self.conv_kernel_sizes[i],
                padding='SAME',
                with_bias=True,
                with_bn=True,
                w_initializer=self.initializers['w'],
                w_regularizer=None,
                acti_func=self.acti_func_conv[i],
                name='encoder_conv_{}_{}'.format(
                    self.conv_kernel_sizes[i],
                    self.conv_output_channels[i])))
            print(encoders_cnn[-1])

            encoders_downsamplers.append(ConvolutionalLayer(
                n_output_chns=self.conv_output_channels[i],
                kernel_size=self.conv_pooling_factors[i],
                stride=self.conv_pooling_factors[i],
                padding='SAME',
                with_bias=False,
                with_bn=True,
                w_initializer=self.initializers['w'],
                w_regularizer=None,
                acti_func=self.acti_func_conv[i],
                name='encoder_downsampler_{}_{}'.format(
                    self.conv_pooling_factors[i],
                    self.conv_pooling_factors[i])))

            print(encoders_downsamplers[-1])

        # Define the encoding fully-connected layers
        encoders_fc = []
        for i in range(0, len(self.layer_sizes_encoder)):
            encoders_fc.append(FullyConnectedLayer(
                n_output_chns=self.layer_sizes_encoder[i],
                with_bias=True,
                with_bn=True,
                acti_func=self.acti_func_encoder[i],
                w_initializer=self.initializers['w'],
                w_regularizer=self.regularizers['w'],
                name='encoder_fc_{}'.format(self.layer_sizes_encoder[i])))
            print(encoders_fc[-1])

        # Add Gaussian noise to the input
        if self.denoising_variance > 0 and is_training:
            flow = images + tf.random_normal(
                    tf.shape(images), 0.0, self.denoising_variance)
        else:
            flow = images

        # Convolutional encoder layers
        for i in range(0, len(self.conv_output_channels)):
            flow = encoders_downsamplers[i](
                encoders_cnn[i](flow, is_training), is_training)

        # Flatten the feature maps
        flow = tf.reshape(flow, [-1, self.serialised_shape])

        # Fully-connected encoder layers
        for i in range(0, len(self.layer_sizes_encoder)):
            flow = encoders_fc[i](flow, is_training)

        return flow
コード例 #14
0
ファイル: vae.py プロジェクト: blackPacha/MedNeurIPS_2019
    def layer_op(self, images, is_training, keep_prob=None, num_units_max=None):

        # Define the encoding convolutional layers
        encoders_cnn = []
        encoders_downsamplers = []
        for i in range(0, len(self.conv_output_channels)):
            encoders_cnn.append(ConvolutionalLayer(
                n_output_chns=self.conv_output_channels[i],
                kernel_size=self.conv_kernel_sizes[i],
                stride=self.conv_kernel_stride[i],
                padding='SAME',
                with_bias=True,
                feature_normalization='batch',
                w_initializer=self.initializers['w'],
                w_regularizer=None,
                acti_func=self.acti_func_conv[i],
                name='encoder_conv_{}_{}'.format(
                    self.conv_kernel_sizes[i],
                    self.conv_output_channels[i])))
            print(encoders_cnn[-1])

            encoders_downsamplers.append(ConvolutionalLayer(
                n_output_chns=self.conv_output_channels[i],
                kernel_size=self.conv_pooling_factors[i],
                stride=self.conv_pooling_factors[i],
                padding='SAME',
                with_bias=False,
                feature_normalization='batch',
                w_initializer=self.initializers['w'],
                w_regularizer=None,
                acti_func=self.acti_func_conv[i],
                name='encoder_downsampler_{}_{}'.format(
                    self.conv_pooling_factors[i],
                    self.conv_pooling_factors[i])))

            print(encoders_downsamplers[-1])

        # Define the encoding fully-connected layers
        encoders_fc = []
        for i in range(0, len(self.layer_sizes_encoder)):
            encoders_fc.append(FullyConnectedLayer(
                n_output_chns=self.layer_sizes_encoder[i],
                with_bias=True,
                feature_normalization='batch',
                acti_func=self.acti_func_encoder[i],
                w_initializer=self.initializers['w'],
                w_regularizer=self.regularizers['w'],
                name='encoder_fc_{}'.format(self.layer_sizes_encoder[i])))
            print(encoders_fc[-1])

        # Add Gaussian noise to the input
        if self.denoising_variance > 0 and is_training:
            flow = images + tf.random_normal(
                    tf.shape(images), 0.0, self.denoising_variance)
        else:
            flow = images

        # Convolutional encoder layers
        print("num_units_max: ", num_units_max)
        for i in range(0, len(self.conv_output_channels)):
            if keep_prob is not None:
                if type(keep_prob) == float: # if the same probability of dropout on all the layers
                    flow = encoders_downsamplers[i](
                        encoders_cnn[i](flow, is_training, keep_prob=keep_prob), is_training)
                else: # if keep_prob is a list
                    flow = encoders_downsamplers[i](
                        encoders_cnn[i](flow, is_training, keep_prob=keep_prob[i]), is_training)
            elif num_units_max is not None:
                if (type(num_units_max) == float) | (type(num_units_max) == int): # if the same probability of dropout on all the layers
                    flow = encoders_downsamplers[i](
                        encoders_cnn[i](flow, is_training, num_units_max=num_units_max), is_training)
                else: # if keep_prob is a list
                    flow = encoders_downsamplers[i](
                        encoders_cnn[i](flow, is_training, num_units_max=num_units_max[i]), is_training)
            else: # if no dropout nor maxout
                flow = encoders_downsamplers[i](
                    encoders_cnn[i](flow, is_training), is_training)
            print("Conv block ", i, ": ", flow.shape)

        # Flatten the feature maps
        #if len(self.layer_sizes_encoder) > 0:
        #    flow = tf.reshape(flow, [-1, self.serialised_shape])
        print("After convolution, flow shape: ", flow.shape)
        # Fully-connected encoder layers
        for i in range(0, len(self.layer_sizes_encoder)):
            flow = encoders_fc[i](flow, is_training)
            print("FC ", i, ": ", flow.shape)

        return flow