Пример #1
0
    def output(self) -> tf.Tensor:
        """Output vector of the CNN.

        If there are specified some fully connected layers, there are applied
        on top of the last convolutional map. Dropout is applied between all
        layers, default activation function is ReLU. There are only projection
        layers, no softmax is applied.

        If there is fully_connected layer specified, average-pooled last
        convolutional map is used as a vector output.
        """
        # pylint: disable=no-member
        last_height, last_width, last_n_channels = [
            s.value for s in self.spatial_states.get_shape()[1:]]
        # pylint: enable=no-member

        if self.fully_connected is None:
            # we average out by the image size -> shape is number
            # channels from the last convolution
            encoded = tf.reduce_mean(self.spatial_states, [1, 2])
            return encoded

        states_flat = tf.reshape(
            self.spatial_states,
            [-1, last_width * last_height * last_n_channels])
        return multilayer_projection(
            states_flat, self.fully_connected,
            activation=tf.nn.relu,
            dropout_keep_prob=self.dropout_keep_prob,
            train_mode=self.train_mode)
Пример #2
0
    def output(self) -> tf.Tensor:
        """Output vector of the CNN.

        If there are specified some fully connected layers, there are applied
        on top of the last convolutional map. Dropout is applied between all
        layers, default activation function is ReLU. There are only projection
        layers, no softmax is applied.

        If there is fully_connected layer specified, average-pooled last
        convolutional map is used as a vector output.
        """
        # pylint: disable=no-member
        last_height, last_width, last_n_channels = [
            s.value for s in self.spatial_states.get_shape()[1:]
        ]
        # pylint: enable=no-member

        if self.fully_connected is None:
            # we average out by the image size -> shape is number
            # channels from the last convolution
            encoded = tf.reduce_mean(self.spatial_states, [1, 2])
            return encoded

        states_flat = tf.reshape(
            self.spatial_states,
            [-1, last_width * last_height * last_n_channels])
        return multilayer_projection(states_flat,
                                     self.fully_connected,
                                     activation=tf.nn.relu,
                                     dropout_keep_prob=self.dropout_keep_prob,
                                     train_mode=self.train_mode)
Пример #3
0
    def _projection(prev_state, prev_output, ctx_tensors, train_mode):
        mlp_input = tf.concat([prev_state, prev_output] + ctx_tensors, 1)

        return multilayer_projection(mlp_input, layer_sizes,
                                     activation=activation,
                                     dropout_keep_prob=dropout_keep_prob,
                                     train_mode=train_mode,
                                     scope="deep_output_mlp")
Пример #4
0
    def _projection(prev_state, prev_output, ctx_tensors, train_mode):
        mlp_input = tf.concat([prev_state, prev_output] + ctx_tensors, 1)

        return multilayer_projection(mlp_input, layer_sizes,
                                     activation=activation,
                                     dropout_keep_prob=dropout_keep_prob,
                                     train_mode=train_mode,
                                     scope="deep_output_mlp")
Пример #5
0
    def _projection(prev_state, prev_output, ctx_tensors):
        mlp_input = tf.concat(1, [prev_state, prev_output] + ctx_tensors)

        return multilayer_projection(mlp_input,
                                     layer_sizes,
                                     activation=activation,
                                     dropout_plc=dropout_plc,
                                     scope="deep_output_mlp")
Пример #6
0
    def __init__(self,
                 mlp_input: tf.Tensor,
                 layer_configuration: List[int],
                 dropout_keep_prob: float,
                 output_size: int,
                 train_mode: tf.Tensor,
                 activation_fn: Callable[[tf.Tensor], tf.Tensor] = tf.nn.relu,
                 name: str = "multilayer_perceptron") -> None:

        with tf.variable_scope(name):
            last_layer = multilayer_projection(
                mlp_input, layer_configuration, activation=activation_fn,
                dropout_keep_prob=dropout_keep_prob, train_mode=train_mode,
                scope="deep_output_mlp")

            self.logits = tf.layers.dense(
                last_layer, output_size, name="classification_layer")
Пример #7
0
    def __init__(self,
                 mlp_input: tf.Tensor,
                 layer_configuration: List[int],
                 dropout_keep_prob: float,
                 output_size: int,
                 train_mode: tf.Tensor,
                 activation_fn: Callable[[tf.Tensor], tf.Tensor] = tf.nn.relu,
                 name: str = "multilayer_perceptron") -> None:

        with tf.variable_scope(name):
            last_layer = multilayer_projection(
                mlp_input, layer_configuration, activation=activation_fn,
                dropout_keep_prob=dropout_keep_prob, train_mode=train_mode,
                scope="deep_output_mlp")

            self.logits = tf.layers.dense(
                last_layer, output_size, name="classification_layer")
Пример #8
0
    def __init__(self, mlp_input, layer_configuration, dropout_plc,
                 output_size, name: str = 'multilayer_perceptron',
                 activation_fn=tf.nn.relu) -> None:

        with tf.variable_scope(name):
            last_layer_size = mlp_input.get_shape()[-1].value

            last_layer = multilayer_projection(mlp_input,
                                               layer_configuration,
                                               activation=activation_fn,
                                               dropout_plc=dropout_plc,
                                               scope="deep_output_mlp")
            self.n_params = 0
            for size in layer_configuration:
                self.n_params += last_layer_size * size
                last_layer_size = size

            with tf.variable_scope("classification_layer"):
                self.n_params += last_layer_size * output_size
                self.logits = linear(last_layer, output_size)
 def _mlp_output(self):
     return multilayer_projection(self._mlp_input, self._layers,
                                  self.train_mode, self._activation_fn,
                                  self._dropout_keep_prob)
Пример #10
0
 def _mlp_output(self):
     return multilayer_projection(
         self._mlp_input, self._layers, self.train_mode,
         self._activation_fn, self._dropout_keep_prob)
Пример #11
0
    def __init__(self,
                 name: str,
                 data_id: str,
                 convolutions: List[Tuple[int, int, Optional[int]]],
                 image_height: int,
                 image_width: int,
                 pixel_dim: int,
                 fully_connected: Optional[List[int]] = None,
                 batch_normalization: bool = True,
                 local_response_normalization: bool = True,
                 dropout_keep_prob: float = 0.5,
                 attention_type: Type = Attention,
                 save_checkpoint: Optional[str] = None,
                 load_checkpoint: Optional[str] = None) -> None:
        """Initialize a convolutional network for image processing.

        Args:
            convolutions: Configuration of convolutional layers. It is a list
                of triplets of integers where the values are: size of the
                convolutional window, number of convolutional filters, and size
                of max-pooling window. If the max-pooling size is set to None,
                no pooling is performed.
            data_id: Identifier of the data series in the dataset.
            image_height: Height of the input image in pixels.
            image_width: Width of the image.
            pixel_dim: Number of color channels in the input images.
            batch_normalization: Flag whether the batch normalization
                should be used between the convolutional layers.
            local_response_normalization: Flag whether to use local
                response normalization between the convolutional layers.
            dropout_keep_prob: Probability of keeping neurons active in
                dropout. Dropout is done between all convolutional layers and
                fully connected layer.
        """
        ModelPart.__init__(self, name, save_checkpoint, load_checkpoint)
        Attentive.__init__(self, attention_type)

        self.data_id = data_id
        self.dropout_keep_prob = dropout_keep_prob

        with self.use_scope():
            self.dropout_placeholder = tf.placeholder(tf.float32,
                                                      name="dropout")
            self.train_mode = tf.placeholder(tf.bool,
                                             shape=[],
                                             name="mode_placeholder")
            self.input_op = tf.placeholder(tf.float32,
                                           shape=(None, image_height,
                                                  image_width, pixel_dim),
                                           name="input_images")

            self.padding_masks = tf.placeholder(tf.float32,
                                                shape=(None, image_height,
                                                       image_width, 1),
                                                name="padding_masks")

            last_layer = self.input_op
            last_padding_masks = self.padding_masks

            self.image_processing_layers = []  # type: List[tf.Tensor]

            with tf.variable_scope("convolutions"):
                for i, (filter_size, n_filters,
                        pool_size) in enumerate(convolutions):
                    with tf.variable_scope("cnn_layer_{}".format(i)):
                        last_layer = conv2d(last_layer, n_filters, filter_size)
                        self.image_processing_layers.append(last_layer)

                        if pool_size:
                            last_layer = max_pool2d(last_layer, pool_size)
                            self.image_processing_layers.append(last_layer)
                            last_padding_masks = max_pool2d(
                                last_padding_masks, pool_size)

                        if local_response_normalization:
                            last_layer = tf.nn.local_response_normalization(
                                last_layer)

                        if batch_normalization:
                            last_layer = batch_norm(
                                last_layer, is_training=self.train_mode)

                        last_layer = dropout(last_layer, dropout_keep_prob,
                                             self.train_mode)

                # last_layer shape is batch X height X width X channels
                last_layer = last_layer * last_padding_masks

            # pylint: disable=no-member
            last_height, last_width, last_n_channels = [
                s.value for s in last_layer.get_shape()[1:]
            ]
            # pylint: enable=no-member

            if fully_connected is None:
                # we average out by the image size -> shape is number
                # channels from the last convolution
                self.encoded = tf.reduce_mean(last_layer, [1, 2])
                assert_shape(self.encoded, [None, convolutions[-1][1]])
            else:
                last_layer_flat = tf.reshape(
                    last_layer,
                    [-1, last_width * last_height * last_n_channels])
                self.encoded = multilayer_projection(
                    last_layer_flat,
                    fully_connected,
                    activation=tf.nn.relu,
                    dropout_plc=self.dropout_placeholder)

            self.__attention_tensor = tf.reshape(
                last_layer, [-1, last_width * last_height, last_n_channels])

            self.__attention_mask = tf.reshape(last_padding_masks,
                                               [-1, last_width * last_height])