Exemple #1
0
    def _build_sampler(self, **kwargs):
        d = dict()
        c_dim = self.X.shape[-1]
        output_dim_H, output_dim_W = (self.X.shape[1], self.X.shape[2])
        kernel_size = (5, 5)
        fc_channel = kwargs.pop('G_FC_layer_channel', 1024)
        G_channel = kwargs.pop('G_channel', 64)

        with tf.variable_scope("generator") as scope:
            scope.reuse_variables()
            z_input = self.z

            d['layer_1'] = fc_layer(z_input, 4 * 4 * fc_channel)
            d['reshape'] = tf.nn.relu(
                batchNormalization(
                    tf.reshape(d['layer_1'], [-1, 4, 4, fc_channel]),
                    self.is_train))
            d['layer_2'] = deconv_bn_relu(d['reshape'],
                                          G_channel * 4,
                                          kernel_size,
                                          self.is_train,
                                          strides=(2, 2))
            d['layer_3'] = deconv_bn_relu(d['layer_2'],
                                          G_channel * 2,
                                          kernel_size,
                                          self.is_train,
                                          strides=(2, 2))
            d['layer_4'] = deconv_bn_relu(d['layer_3'],
                                          G_channel,
                                          kernel_size,
                                          self.is_train,
                                          strides=(2, 2))
            d['layer_5'] = deconv_bn_relu(d['layer_4'],
                                          c_dim,
                                          kernel_size,
                                          self.is_train,
                                          strides=(2, 2),
                                          bn=False,
                                          relu=False)
            d['tanh'] = tf.nn.tanh(d['layer_5'])

        return d['tanh']
    def __init__(self,
                 image_size,
                 image_channels,
                 classes,
                 fc_layers=3,
                 fc_units=1000):

        # Configurations.
        super().__init__()
        self.classes = classes
        self.label = "Classifier"
        self.fc_layers = fc_layers

        # Attributes that need to be set before training.
        self.optimizer = None
        self.multi_head = None  #-->  <list> with for each task its class-IDs

        # Check whether there is at least 1 fc-layer.
        if fc_layers < 1:
            raise ValueError(
                "The classifier needs to have at least 1 fully-connected layer."
            )

        ######------SPECIFY MODEL------######

        # Flatten image to 2D-tensor.
        self.flatten = modules.Flatten()

        # Fully connected hidden layers.
        self.fcE = MLP(input_size=image_channels * image_size**2,
                       output_size=fc_units,
                       layers=fc_layers - 1,
                       hid_size=fc_units)
        self.mlp_output_size = fc_units if fc_layers > 1 else image_channels * image_size**2

        # Classifier.
        self.classifier = fc_layer(self.mlp_output_size,
                                   classes,
                                   nl='none',
                                   bias=True)
Exemple #3
0
    def _build_discriminator(self, fake_image=False, **kwargs):
        d = dict()
        kernel_size = (5, 5)
        if fake_image:
            input_image = self.G
        else:
            input_image = self.X
        batch_size = kwargs.pop('batch_size', 8)
        D_channel = kwargs.pop('D_channel', 64)

        with tf.variable_scope("discriminator") as scope:
            if fake_image:
                scope.reuse_variables()

            d['layer_1'] = conv_bn_lrelu(input_image,
                                         D_channel,
                                         kernel_size,
                                         self.is_train,
                                         strides=(2, 2),
                                         bn=False)
            d['layer_2'] = conv_bn_lrelu(d['layer_1'],
                                         D_channel * 2,
                                         kernel_size,
                                         self.is_train,
                                         strides=(2, 2))
            d['layer_3'] = conv_bn_lrelu(d['layer_2'],
                                         D_channel * 4,
                                         kernel_size,
                                         self.is_train,
                                         strides=(2, 2))
            d['layer_4'] = conv_bn_lrelu(d['layer_3'],
                                         D_channel * 8,
                                         kernel_size,
                                         self.is_train,
                                         strides=(2, 2))
            d['layer_5'] = fc_layer(tf.contrib.layers.flatten(d['layer_4']), 1)
            d['sigmoid'] = tf.nn.sigmoid(d['layer_5'])

        return d['sigmoid'], d['layer_5'], d['layer_1']
Exemple #4
0
    def _build_model(self, **kwargs):
        """
        Build model.
        :param kwargs: dict, extra arguments for building AlexNet.
            - image_mean: np.ndarray, mean image for each input channel, shape: (C,).
            - dropout_prob: float, the probability of dropping out each unit in FC layer.
        :return d: dict, containing outputs on each layer.
        """
        d = dict()    # Dictionary to save intermediate values returned from each layer.
        X_mean = kwargs.pop('image_mean', 0.0)
        dropout_prob = kwargs.pop('dropout_prob', 0.0)
        num_classes = int(self.y.get_shape()[-1])

        # The probability of keeping each unit for dropout layers
        keep_prob = tf.cond(self.is_train,
                            lambda: 1. - dropout_prob,
                            lambda: 1.)

        # input
        X_input = self.X - X_mean    # perform mean subtraction

        # First Convolution Layer
        # conv1 - relu1 - pool1

        with tf.variable_scope('conv1'):
            # conv_layer(x, side_l, stride, out_depth, padding='SAME', **kwargs):
            d['conv1'] = conv_layer(X_input, 3, 1, 64, padding='SAME',
                                    weights_stddev=0.01, biases_value=1.0)
            print('conv1.shape', d['conv1'].get_shape().as_list())
        d['relu1'] = tf.nn.relu(d['conv1'])
        # max_pool(x, side_l, stride, padding='SAME'):
        d['pool1'] = max_pool(d['relu1'], 2, 1, padding='SAME')
        d['drop1'] = tf.nn.dropout(d['pool1'], keep_prob)
        print('pool1.shape', d['pool1'].get_shape().as_list())

        # Second Convolution Layer
        # conv2 - relu2 - pool2
        with tf.variable_scope('conv2'):
            d['conv2'] = conv_layer(d['pool1'], 3, 1, 128, padding='SAME',
                                    weights_stddev=0.01, biases_value=1.0)
            print('conv2.shape', d['conv2'].get_shape().as_list())
        d['relu2'] = tf.nn.relu(d['conv2'])
        d['pool2'] = max_pool(d['relu2'], 2, 1, padding='SAME')
        d['drop2'] = tf.nn.dropout(d['pool2'], keep_prob)
        print('pool2.shape', d['pool2'].get_shape().as_list())

        # Third Convolution Layer
        # conv3 - relu3
        with tf.variable_scope('conv3'):
            d['conv3'] = conv_layer(d['pool2'], 3, 1, 256, padding='SAME',
                                    weights_stddev=0.01, biases_value=1.0)
            print('conv3.shape', d['conv3'].get_shape().as_list())
        d['relu3'] = tf.nn.relu(d['conv3'])
        d['pool3'] = max_pool(d['relu3'], 2, 1, padding='SAME')
        d['drop3'] = tf.nn.dropout(d['pool3'], keep_prob)
        print('pool3.shape', d['pool3'].get_shape().as_list())


        # Flatten feature maps
        f_dim = int(np.prod(d['drop3'].get_shape()[1:]))
        f_emb = tf.reshape(d['drop3'], [-1, f_dim])

        # fc4
        with tf.variable_scope('fc4'):
            d['fc4'] = fc_layer(f_emb, 1024,
                                weights_stddev=0.005, biases_value=0.1)
        d['relu4'] = tf.nn.relu(d['fc4'])
        print('fc4.shape', d['relu4'].get_shape().as_list())

        # fc5
        with tf.variable_scope('fc5'):
            d['fc5'] = fc_layer(d['relu4'], 1024,
                                weights_stddev=0.005, biases_value=0.1)
        d['relu5'] = tf.nn.relu(d['fc5'])
        print('fc5.shape', d['relu5'].get_shape().as_list())
        d['logits'] = fc_layer(d['relu5'], num_classes,
                               weights_stddev=0.01, biases_value=0.0)
        print('logits.shape', d['logits'].get_shape().as_list())

        # softmax
        d['pred'] = tf.nn.softmax(d['logits'])

        return d
    def _build_model(self, **kwargs):
        """
        Build model.
        :param kwargs: dict, extra arguments for building AlexNet.
            - image_mean: np.ndarray, mean image for each input channel, shape: (C,).
            - dropout_prob: float, the probability of dropping out each unit in FC layer.
        :return d: dict, containing outputs on each layer.
        """
        d = dict(
        )  # Dictionary to save intermediate values returned from each layer.
        X_mean = kwargs.pop('image_mean', 0.0)
        dropout_prob = kwargs.pop('dropout_prob', 0.0)
        num_classes = int(self.y.get_shape()[-1])

        # The probability of keeping each unit for dropout layers
        keep_prob = tf.cond(self.is_train, lambda: 1. - dropout_prob,
                            lambda: 1.)

        # input
        X_input = self.X - X_mean  # perform mean subtraction

        # conv1 - relu1 - pool1
        with tf.variable_scope('conv1'):
            d['conv1'] = conv_layer(X_input,
                                    11,
                                    4,
                                    96,
                                    padding='VALID',
                                    weights_stddev=0.01,
                                    biases_value=0.0)
            print('conv1.shape', d['conv1'].get_shape().as_list())
        d['relu1'] = tf.nn.relu(d['conv1'])
        # (227, 227, 3) --> (55, 55, 96)
        d['pool1'] = max_pool(d['relu1'], 3, 2, padding='VALID')
        # (55, 55, 96) --> (27, 27, 96)
        print('pool1.shape', d['pool1'].get_shape().as_list())

        # conv2 - relu2 - pool2
        with tf.variable_scope('conv2'):
            d['conv2'] = conv_layer(d['pool1'],
                                    5,
                                    1,
                                    256,
                                    padding='SAME',
                                    weights_stddev=0.01,
                                    biases_value=0.1)
            print('conv2.shape', d['conv2'].get_shape().as_list())
        d['relu2'] = tf.nn.relu(d['conv2'])
        # (27, 27, 96) --> (27, 27, 256)
        d['pool2'] = max_pool(d['relu2'], 3, 2, padding='VALID')
        # (27, 27, 256) --> (13, 13, 256)
        print('pool2.shape', d['pool2'].get_shape().as_list())

        # conv3 - relu3
        with tf.variable_scope('conv3'):
            d['conv3'] = conv_layer(d['pool2'],
                                    3,
                                    1,
                                    384,
                                    padding='SAME',
                                    weights_stddev=0.01,
                                    biases_value=0.0)
            print('conv3.shape', d['conv3'].get_shape().as_list())
        d['relu3'] = tf.nn.relu(d['conv3'])
        # (13, 13, 256) --> (13, 13, 384)

        # conv4 - relu4
        with tf.variable_scope('conv4'):
            d['conv4'] = conv_layer(d['relu3'],
                                    3,
                                    1,
                                    384,
                                    padding='SAME',
                                    weights_stddev=0.01,
                                    biases_value=0.1)
            print('conv4.shape', d['conv4'].get_shape().as_list())
        d['relu4'] = tf.nn.relu(d['conv4'])
        # (13, 13, 384) --> (13, 13, 384)

        # conv5 - relu5 - pool5
        with tf.variable_scope('conv5'):
            d['conv5'] = conv_layer(d['relu4'],
                                    3,
                                    1,
                                    256,
                                    padding='SAME',
                                    weights_stddev=0.01,
                                    biases_value=0.1)
            print('conv5.shape', d['conv5'].get_shape().as_list())
        d['relu5'] = tf.nn.relu(d['conv5'])
        # (13, 13, 384) --> (13, 13, 256)
        d['pool5'] = max_pool(d['relu5'], 3, 2, padding='VALID')
        # (13, 13, 256) --> (6, 6, 256)
        print('pool5.shape', d['pool5'].get_shape().as_list())

        # Flatten feature maps
        f_dim = int(np.prod(d['pool5'].get_shape()[1:]))
        f_emb = tf.reshape(d['pool5'], [-1, f_dim])
        # (6, 6, 256) --> (9216)

        # fc6
        with tf.variable_scope('fc6'):
            d['fc6'] = fc_layer(f_emb,
                                4096,
                                weights_stddev=0.005,
                                biases_value=0.1)
        d['relu6'] = tf.nn.relu(d['fc6'])
        d['drop6'] = tf.nn.dropout(d['relu6'], keep_prob)
        # (9216) --> (4096)
        print('drop6.shape', d['drop6'].get_shape().as_list())

        # fc7
        with tf.variable_scope('fc7'):
            d['fc7'] = fc_layer(d['drop6'],
                                4096,
                                weights_stddev=0.005,
                                biases_value=0.1)
        d['relu7'] = tf.nn.relu(d['fc7'])
        d['drop7'] = tf.nn.dropout(d['relu7'], keep_prob)
        # (4096) --> (4096)
        print('drop7.shape', d['drop7'].get_shape().as_list())

        # fc8
        with tf.variable_scope('fc8'):
            d['logits'] = fc_layer(d['relu7'],
                                   num_classes,
                                   weights_stddev=0.01,
                                   biases_value=0.0)
        # (4096) --> (num_classes)

        # softmax
        d['pred'] = tf.nn.softmax(d['logits'])

        return d
Exemple #6
0
    def __init__(self,
                 input_size=1000,
                 output_size=10,
                 layers=2,
                 hid_size=1000,
                 hid_smooth=None,
                 size_per_layer=None,
                 drop=0,
                 batch_norm=False,
                 nl="relu",
                 bias=True,
                 gated=False,
                 output='normal'):
        '''sizes: 0th=[input], 1st=[hid_size], ..., 1st-to-last=[hid_smooth], last=[output].
        [input_size]       # of inputs
        [output_size]      # of units in final layer
        [layers]           # of layers
        [hid_size]         # of units in each hidden layer
        [hid_smooth]       if None, all hidden layers have [hid_size] units, else # of units linearly in-/decreases s.t.
                             final hidden layer has [hid_smooth] units (if only 1 hidden layer, it has [hid_size] units)
        [size_per_layer]   None or <list> with for each layer number of units (1st element = number of inputs)
                                --> overwrites [input_size], [output_size], [layers], [hid_size] and [hid_smooth]
        [drop]             % of each layer's inputs that is randomly set to zero during training
        [batch_norm]       <bool>; if True, batch-normalization is applied to each layer
        [nl]               <str>; type of non-linearity to be used (options: "relu", "leakyrelu", "none")
        [gated]            <bool>; if True, each linear layer has an additional learnable gate
        [output]           <str>; if - "normal", final layer is same as all others
                                     - "BCE", final layer has sigmoid non-linearity'''

        super().__init__()
        self.output = output

        # get sizes of all layers
        if size_per_layer is None:
            hidden_sizes = []
            if layers > 1:
                if (hid_smooth is not None):
                    hidden_sizes = [
                        int(x) for x in np.linspace(
                            hid_size, hid_smooth, num=layers - 1)
                    ]
                else:
                    hidden_sizes = [
                        int(x) for x in np.repeat(hid_size, layers - 1)
                    ]
            size_per_layer = [input_size] + hidden_sizes + [
                output_size
            ] if layers > 0 else [input_size]
        self.layers = len(size_per_layer) - 1

        # set label for this module
        # -determine "non-default options"-label
        nd_label = "{drop}{bias}{bn}{nl}{gate}".format(
            drop="" if drop == 0 else "d{}".format(drop),
            bias="" if bias else "n",
            bn="b" if batch_norm else "",
            nl="l" if nl == "leakyrelu" else "",
            gate="g" if gated else "",
        )
        nd_label = "{}{}".format(
            "" if nd_label == "" else "-{}".format(nd_label),
            "" if output == "normal" else "-{}".format(output))
        # -set label
        size_statement = ""
        for i in size_per_layer:
            size_statement += "{}{}".format(
                "-" if size_statement == "" else "x", i)
        self.label = "F{}{}".format(size_statement,
                                    nd_label) if self.layers > 0 else ""

        # set layers
        for lay_id in range(1, self.layers + 1):
            # number of units of this layer's input and output
            in_size = size_per_layer[lay_id - 1]
            out_size = size_per_layer[lay_id]
            # define and set the fully connected layer
            layer = fc_layer(
                in_size,
                out_size,
                bias=bias,
                drop=drop,
                batch_norm=False if
                (lay_id == self.layers
                 and not output == "normal") else batch_norm,
                gated=gated,
                nl=nn.Sigmoid() if
                (lay_id == self.layers and not output == "normal") else nl,
            )
            setattr(self, 'fcLayer{}'.format(lay_id), layer)

        # if no layers, add "identity"-module to indicate in this module's representation nothing happens
        if self.layers < 1:
            self.noLayers = modules.Identity()