<commit_before>import collections
import numpy as np
import sonnet as snt
import tensorflow as tf
import tensorflow.contrib.slim as slim


from sonnet.python.modules.conv import Conv2D

from .anchor_target import AnchorTarget
from .rpn_proposal import RPNProposal
from .utils.generate_anchors import generate_anchors
from .utils.losses import smooth_l1_loss
from .utils.ops import spatial_softmax, spatial_reshape_layer
from .utils.vars import variable_summaries


class RPN(snt.AbstractModule):

    def __init__(self, num_anchors, num_channels=512, kernel_shape=[3, 3], name='rpn'):
        """RPN - Region Proposal Network

        This module works almost independently from the Faster RCNN module.
        It instantiates its own submodules and calculates its own loss,
        and can be used on its own

        """
        super(RPN, self).__init__(name=name)
        self._num_anchors = num_anchors
        self._num_channels = num_channels
        self._kernel_shape = kernel_shape

        # According to Faster RCNN paper we need to initialize layers with
        # "from a zero-mean Gaussian distribution with standard deviation 0.01"
        self._initializer = tf.random_normal_initializer(mean=0.0, stddev=0.01)

        # We could use normal relu without any problems.
        self._rpn_activation = tf.nn.relu6

        self._instantiate_layers()

    def _instantiate_layers(self):
        """Instantiates all convolutional modules used in the RPN."""
        with self._enter_variable_scope():
            self._rpn = Conv2D(
                output_channels=self._num_channels,
                kernel_shape=self._kernel_shape,
                initializers={'w': self._initializer}, name='conv'
            )

            self._rpn_cls = Conv2D(
                output_channels=self._num_anchors * 2, kernel_shape=[1, 1],
                initializers={'w': self._initializer}, padding='VALID',
                name='cls_conv'
            )

            # BBox prediction is 4 values * number of anchors.
            self._rpn_bbox = Conv2D(
                output_channels=self._num_anchors * 4, kernel_shape=[1, 1],
                initializers={'w': self._initializer}, padding='VALID',
                name='bbox_conv'
            )


            self._proposal = RPNProposal(self._num_anchors)
            if is_training:
                self._anchor_target = AnchorTarget(self._num_anchors)

    def _build(self, pretrained_feature_map, gt_boxes, image_shape, all_anchors,
               is_training=True):
        # We start with a common conv layer applied to the feature map.
        rpn_feature = self._rpn_activation(self._rpn(pretrained_feature_map))

        # Then we apply separate conv layers for classification and regression.
        rpn_cls_score_original = self._rpn_cls(rpn_feature)
        rpn_bbox_pred_original = self._rpn_bbox(rpn_feature)
        # rpn_cls_score_original has shape (1, H, W, num_anchors * 2)
        # rpn_bbox_pred_original has shape (1, H, W, num_anchors * 4)
        # where H, W are height and width of the pretrained feature map.

        # Convert `rpn_cls_score` which has two scalars per anchor per location<commit_msg>Remove is_training from RPN's creation<commit_after>import collections
import numpy as np
import sonnet as snt
import tensorflow as tf
import tensorflow.contrib.slim as slim


from sonnet.python.modules.conv import Conv2D

from .anchor_target import AnchorTarget
from .rpn_proposal import RPNProposal
from .utils.generate_anchors import generate_anchors
from .utils.losses import smooth_l1_loss
from .utils.ops import spatial_softmax, spatial_reshape_layer
from .utils.vars import variable_summaries


class RPN(snt.AbstractModule):

    def __init__(self, num_anchors, num_channels=512, kernel_shape=[3, 3], name='rpn'):
        """RPN - Region Proposal Network

        This module works almost independently from the Faster RCNN module.
        It instantiates its own submodules and calculates its own loss,
        and can be used on its own

        """
        super(RPN, self).__init__(name=name)
        self._num_anchors = num_anchors
        self._num_channels = num_channels
        self._kernel_shape = kernel_shape

        # According to Faster RCNN paper we need to initialize layers with
        # "from a zero-mean Gaussian distribution with standard deviation 0.01"
        self._initializer = tf.random_normal_initializer(mean=0.0, stddev=0.01)

        # We could use normal relu without any problems.
        self._rpn_activation = tf.nn.relu6

        self._instantiate_layers()

    def _instantiate_layers(self):
        """Instantiates all convolutional modules used in the RPN."""
        with self._enter_variable_scope():
            self._rpn = Conv2D(
                output_channels=self._num_channels,
                kernel_shape=self._kernel_shape,
                initializers={'w': self._initializer}, name='conv'
            )

            self._rpn_cls = Conv2D(
                output_channels=self._num_anchors * 2, kernel_shape=[1, 1],
                initializers={'w': self._initializer}, padding='VALID',
                name='cls_conv'
            )

            # BBox prediction is 4 values * number of anchors.
            self._rpn_bbox = Conv2D(
                output_channels=self._num_anchors * 4, kernel_shape=[1, 1],
                initializers={'w': self._initializer}, padding='VALID',
                name='bbox_conv'
            )


            self._proposal = RPNProposal(self._num_anchors)
            self._anchor_target = AnchorTarget(self._num_anchors)

    def _build(self, pretrained_feature_map, gt_boxes, image_shape, all_anchors,
               is_training=True):
        # We start with a common conv layer applied to the feature map.
        rpn_feature = self._rpn_activation(self._rpn(pretrained_feature_map))

        # Then we apply separate conv layers for classification and regression.
        rpn_cls_score_original = self._rpn_cls(rpn_feature)
        rpn_bbox_pred_original = self._rpn_bbox(rpn_feature)
        # rpn_cls_score_original has shape (1, H, W, num_anchors * 2)
        # rpn_bbox_pred_original has shape (1, H, W, num_anchors * 4)
        # where H, W are height and width of the pretrained feature map.

        # Convert `rpn_cls_score` which has two scalars per anchor per location
{'fraction_non_alphanumeric': 0.07473700737949443, 'fraction_numerical': 0.009420631182289214, 'mean_word_length': 2.7056428155904597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4171791', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1803, 'n_words': 511}