def _classifier_layers(self, input_value, num_inputs):
        """Create nodes to carry out classification into target number of classes.

        :param input_value: Input features
        :param num_inputs: Number of input channels
        :param keep_prob: Probability to keep values in dropout
        :return: Class logits
        """

        with tf.variable_scope('classifier_layer'):
            conv1 = conv2d(input_value, [1, 1, num_inputs, 1024], 'dim_inc', fn_activation=self._fn_activation,
                           use_bias=False, use_bn=True, is_training=self._is_training,
                           merge_op=self._merge_bn, merge_op_transit=self._merge_bn_transition,
                           add_summary=True, norm_kernel=self._norm_kernels)

            glob_pool = glob_max_pool(conv1, add_summary=True)

            conv2 = conv2d(glob_pool, [1, 1, 1024, 1280], 'internal', fn_activation=self._fn_activation,
                           use_bias=False, use_bn=True, is_training=self._is_training,
                           merge_op=self._merge_bn, merge_op_transit=self._merge_bn_transition,
                           add_summary=True, norm_kernel=self._norm_kernels)
            logits = conv2d(conv2, [1, 1, 1280, self._num_classes], 'logits', fn_activation=None,
                            use_bias=True, use_bn=False, is_training=self._is_training,
                            merge_op=self._merge_bn, merge_op_transit=self._merge_bn_transition,
                            add_summary=True, norm_kernel=self._norm_kernels)
            logits = tf.reshape(logits, [-1, self._num_classes], name='output')

            return logits
    def _add_head_shared(self, input_value, out_size, name):
        """Adds shared part of the action head.

        :param input_value: Input tensor
        :param out_size: Output number of channels
        :param name: Name of block
        :return: Output tensor
        """

        with tf.variable_scope(name):
            conv1 = conv2d(input_value,
                           [1, 1, input_value.get_shape()[3], out_size],
                           'conv1',
                           fn_activation=self._fn_activation,
                           norm_kernel=self._norm_kernels,
                           use_bias=False,
                           use_bn=True,
                           is_training=self._is_training,
                           merge_op=self._merge_bn,
                           merge_op_transit=self._merge_bn_transition)
            conv2 = conv2d(conv1, [3, 3, out_size, 1],
                           'conv2',
                           norm_kernel=self._norm_kernels,
                           depth_wise=True,
                           use_bias=False,
                           use_bn=True,
                           is_training=self._is_training,
                           merge_op=self._merge_bn,
                           merge_op_transit=self._merge_bn_transition)

        return conv2
Пример #3
0
    def _bottleneck(self, input_value, num_channels, name, keep_prob=None, rate=None, factor=4., k=1):
        """

        :param input_value:
        :param num_channels: Number of input and output channels
        :param name: Name of node
        :param keep_prob: Probability to keep value in dropout
        :param rate: Rate value for dilated convolutions
        :param factor: Factor to reduce number of channels in bottleneck
        :param k: Stride of node
        :return: Tensor
        """

        with tf.variable_scope(name):
            internal_num_channels = int(num_channels[1] // factor)

            conv1 = conv2d(input_value, [1, 1, num_channels[0], internal_num_channels], 'dim_red',
                           stride=[1, 1, 1, 1], is_training=self._is_training, init=self._ort_init,
                           use_bias=False, use_bn=True, fn_activation=self._fn_activation,
                           merge_op=self._merge_bn, merge_op_transit=self._merge_bn_transition,
                           norm_kernel=self._norm_kernels)

            conv2 = conv2d(conv1, [3, 3, internal_num_channels, 1], 'inner_conv', init_scale=0.1,
                           stride=[1, k, k, 1], depth_wise=True, rate=rate, is_training=self._is_training,
                           use_bias=False, use_bn=True, fn_activation=self._fn_activation,
                           merge_op=self._merge_bn, merge_op_transit=self._merge_bn_transition,
                           norm_kernel=self._norm_kernels)

            conv3 = conv2d(conv2, [1, 1, internal_num_channels, num_channels[1]], 'dim_inc', init_scale=0.1,
                           is_training=self._is_training, use_bias=False, use_bn=True, fn_activation=None,
                           merge_op=self._merge_bn, merge_op_transit=self._merge_bn_transition,
                           norm_kernel=self._norm_kernels)

            if keep_prob is not None:
                conv3 = dropout(conv3, keep_prob, is_training=self._is_training)

            skip_branch = input_value if k == 1 else max_pool(input_value, k=k)
            if num_channels[0] != num_channels[1]:
                skip_branch = conv2d(skip_branch, [1, 1, num_channels[0], num_channels[1]], 'skip_conv',
                                     stride=[1, 1, 1, 1], is_training=self._is_training, init_scale=0.1,
                                     use_bias=False, use_bn=True, fn_activation=None,
                                     merge_op=self._merge_bn, merge_op_transit=self._merge_bn_transition,
                                     norm_kernel=self._norm_kernels)

            out = tf.add(conv3, skip_branch)

            out = self._fn_activation(out)

        return out
    def _add_action_classifier_body(self,
                                    input_value,
                                    input_size,
                                    out_size,
                                    reuse=False):
        """Adds action classifier operations.

        :param input_value: Input tensor
        :param input_size: Input number of channels
        :param out_size: Embedding size
        :param reuse: Whether to reuse variables
        :return: Action logits
        """

        with tf.variable_scope('action_classifier'):
            conv1 = conv2d(input_value, [1, 1, input_size, input_size],
                           'conv1',
                           use_bias=False,
                           use_bn=True,
                           is_training=self._is_training,
                           fn_activation=self._fn_activation,
                           merge_op=self._merge_bn,
                           merge_op_transit=self._merge_bn_transition,
                           norm_kernel=self._norm_kernels,
                           reuse_var=reuse)
            conv2 = conv2d(conv1, [1, 1, input_size, out_size],
                           'conv2',
                           use_bias=False,
                           use_bn=True,
                           is_training=self._is_training,
                           merge_op=self._merge_bn,
                           merge_op_transit=self._merge_bn_transition,
                           norm_kernel=self._norm_kernels,
                           reuse_var=reuse)

            action_embeddings = tf.nn.l2_normalize(conv2, axis=-1)

        return action_embeddings
Пример #5
0
    def _init_block(self, input_value, num_channels, name):
        """Converts network input in some internal representation.

        :param input_value: Network input
        :param num_channels: Number of input and output channels
        :param name: Name of node
        :return: Tensor
        """

        with tf.variable_scope(name):
            out = conv2d(input_value, [3, 3, num_channels[0], num_channels[1]], 'dim_inc_conv',
                         stride=[1, 2, 2, 1], is_training=self._is_training, init=self._ort_init,
                         use_bias=False, use_bn=True, fn_activation=self._fn_activation,
                         merge_op=self._merge_bn, merge_op_transit=self._merge_bn_transition,
                         add_summary=True, init_scale=0.1, norm_kernel=self._norm_kernels)

        return out
    def _bridge(self, source, target, num_channels, name):
        """Constructs bridge between two input streams.

        :param source: Source stream input
        :param target: Target strean input
        :param num_channels: Number of input and output channels
        :param name: Name of block
        :return: Output of merged streams
        """

        with tf.variable_scope(name):
            out = conv2d(source, [1, 1, num_channels[0], num_channels[1]],
                         'mix',
                         is_training=self._is_training,
                         use_bias=False,
                         use_bn=True,
                         merge_op=self._merge_bn,
                         merge_op_transit=self._merge_bn_transition,
                         norm_kernel=self._norm_kernels)
            out = tf.add(out, target)
            out = self._fn_activation(out)
        return out
    def _add_anchor_specific_head(self, input_value, input_size, out_size,
                                  name):
        """Adds anchor-specific part of the action head.

        :param input_value: Input tensor
        :param input_size: Input number of channels
        :param out_size: Output number of channels
        :param name: Name of block
        :return: Output tensor
        """

        with tf.variable_scope(name):
            conv1 = conv2d(input_value, [1, 1, input_size, out_size],
                           'conv1',
                           use_bias=False,
                           use_bn=True,
                           is_training=self._is_training,
                           merge_op=self._merge_bn,
                           merge_op_transit=self._merge_bn_transition,
                           add_summary=True,
                           norm_kernel=self._norm_kernels)
            action_branch = tf.nn.l2_normalize(conv1, axis=-1)

        return action_branch
Пример #8
0
    def _add_detection_head(self, input_value, input_size, internal_size,
                            num_classes, num_anchors, name):
        """Adds single SSD head on top of the specified input features

        :param input_value: Input features
        :param input_size: Number of input channels
        :param internal_size: Number of channels for the internal representation
        :param num_classes: Number of taget classes
        :param num_anchors: Number of anchor boxes
        :param name: Name of block
        :return: Location and confidence tensors
        """

        with tf.variable_scope(name):
            loc_conv1 = conv2d(input_value, [1, 1, input_size, internal_size],
                               'loc_conv1',
                               use_bias=False,
                               use_bn=True,
                               is_training=self._is_training,
                               fn_activation=self._fn_activation,
                               norm_kernel=self._norm_kernels,
                               merge_op=self._merge_bn,
                               merge_op_transit=self._merge_bn_transition)
            loc_conv2 = conv2d(loc_conv1, [3, 3, internal_size, 1],
                               'loc_conv2',
                               depth_wise=True,
                               use_bias=False,
                               use_bn=True,
                               is_training=self._is_training,
                               merge_op=self._merge_bn,
                               merge_op_transit=self._merge_bn_transition,
                               norm_kernel=self._norm_kernels)
            locations = conv2d(loc_conv2,
                               [1, 1, internal_size, num_anchors * 4],
                               'loc',
                               use_bias=False,
                               use_bn=True,
                               is_training=self._is_training,
                               merge_op=self._merge_bn,
                               merge_op_transit=self._merge_bn_transition,
                               pr_product=True,
                               add_summary=True,
                               norm_kernel=self._norm_kernels)

            conf_conv1 = conv2d(input_value, [1, 1, input_size, internal_size],
                                'conf_conv1',
                                use_bias=False,
                                use_bn=True,
                                is_training=self._is_training,
                                fn_activation=self._fn_activation,
                                norm_kernel=self._norm_kernels,
                                merge_op=self._merge_bn,
                                merge_op_transit=self._merge_bn_transition)
            conf_conv2 = conv2d(conf_conv1, [3, 3, internal_size, 1],
                                'conf_conv2',
                                depth_wise=True,
                                use_bias=False,
                                use_bn=True,
                                is_training=self._is_training,
                                merge_op=self._merge_bn,
                                merge_op_transit=self._merge_bn_transition,
                                norm_kernel=self._norm_kernels)
            confidences = conv2d(
                conf_conv2, [1, 1, internal_size, num_anchors * num_classes],
                'conf',
                use_bias=False,
                use_bn=True,
                is_training=self._is_training,
                merge_op=self._merge_bn,
                merge_op_transit=self._merge_bn_transition,
                pr_product=True,
                add_summary=True,
                norm_kernel=self._norm_kernels)

        return locations, confidences