def _instantiate_layers(self): """ 定义两个全连接层和后面的分类回归层 :return: 无 """ # We define layers as an array since they are simple fully connected # ones and it should be easy to tune it from the network config. # 定义两个全连接层, layer_size表示输出通道数 self._layers = [ snt.Linear( layer_size, name='fc_{}'.format(i), initializers={'w': self._rcnn_initializer}, regularizers={'w': self.regularizer}, ) for i, layer_size in enumerate(self._layer_sizes) ] # We define the classifier layer having a num_classes + 1 background # since we want to be able to predict if the proposal is background as # well. # 定义一个分类的全连接层(21类) self._classifier_layer = snt.Linear( self._num_classes + 1, name='fc_classifier', initializers={'w': self._cls_initializer}, regularizers={'w': self.regularizer}, ) # The bounding box adjustment layer has 4 times the number of classes # We choose which to use depending on the output of the classifier # layer # 输出通道数为类别数目x4, 也就是4x20, 每个类别对应4个值, 这个是独立于分类器的输出 self._bbox_layer = snt.Linear( self._num_classes * 4, name='fc_bbox', initializers={'w': self._bbox_initializer}, regularizers={'w': self.regularizer}) # ROIPoolingLayer is used to extract the feature from the feature map # using the proposals. # self._config.roi表示的就是选择crop, 7x7, valid的池化配置 self._roi_pool = ROIPoolingLayer(self._config.roi, debug=self._debug) # RCNNTarget is used to define a minibatch and the correct values for # each of the proposals. # 用来定义minibatch和提案的正确值 self._rcnn_target = RCNNTarget(self._num_classes, self._config.target, variances=self._variances, seed=self._seed) # RCNNProposal generates the final bounding boxes and tries to remove # duplicates. self._rcnn_proposal = RCNNProposal(self._num_classes, self._config.proposals, variances=self._variances)
def testLimits(self): """Tests that we're respecting the limits imposed by the config. """ limits_config = self._config.copy() limits_config['class_max_detections'] = 2 limits_config['total_max_detections'] = 3 limits_config = EasyDict(limits_config) limits_num_classes = 2 limits_model = RCNNProposal(limits_num_classes, limits_config) proposed_boxes = tf.constant([ (self._batch_number, 0, 0, 1, 1), # class 0 (self._batch_number, 5, 5, 10, 10), # class 1 (self._batch_number, 15, 15, 20, 20), # class 1 (self._batch_number, 25, 25, 30, 30), # class 0 (self._batch_number, 35, 35, 40, 40), (self._batch_number, 38, 40, 65, 65), (self._batch_number, 70, 50, 90, 90), # class 0 (self._batch_number, 95, 95, 100, 100), (self._batch_number, 105, 105, 110, 110), # class 1 ]) # All zeroes for our bbox_pred. bbox_pred = tf.constant([[0.] * limits_num_classes * 4] * 9) cls_prob = tf.constant([ (0., 1., 0.), (0., .2, .8), (0., .45, .55), (0., .55, .45), (1., 0., 0.), (1., 0., 0.), (0., .95, .05), (1., 0., 0.), (0., .495, .505), ]) proposal_prediction = self._run_rcnn_proposal( limits_model, proposed_boxes, bbox_pred, cls_prob, ) labels = proposal_prediction['proposal_label'] num_class0 = labels[labels == 0].shape[0] num_class1 = labels[labels == 1].shape[0] self.assertLessEqual(num_class0, limits_config.class_max_detections) self.assertLessEqual(num_class1, limits_config.class_max_detections) num_total = labels.shape[0] self.assertLessEqual(num_total, limits_config.total_max_detections)
def _instantiate_layers(self): # We define layers as an array since they are simple fully connected # ones and it should be easy to tune it from the network config. self._layers = [ snt.Linear( layer_size, name="fc_{}".format(i), initializers={"w": self._rcnn_initializer}, regularizers={"w": self.regularizer}, ) for i, layer_size in enumerate(self._layer_sizes) ] # We define the classifier layer having a num_classes + 1 background # since we want to be able to predict if the proposal is background as # well. self._classifier_layer = snt.Linear( self._num_classes + 1, name="fc_classifier", initializers={"w": self._cls_initializer}, regularizers={"w": self.regularizer}, ) # The bounding box adjustment layer has 4 times the number of classes # We choose which to use depending on the output of the classifier # layer self._bbox_layer = snt.Linear( self._num_classes * 4, name="fc_bbox", initializers={"w": self._bbox_initializer}, regularizers={"w": self.regularizer}, ) # ROIPoolingLayer is used to extract the feature from the feature map # using the proposals. self._roi_pool = ROIPoolingLayer(self._config.roi, debug=self._debug) # RCNNTarget is used to define a minibatch and the correct values for # each of the proposals. self._rcnn_target = RCNNTarget( self._num_classes, self._config.target, variances=self._variances, seed=self._seed, ) # RCNNProposal generates the final bounding boxes and tries to remove # duplicates. self._rcnn_proposal = RCNNProposal( self._num_classes, self._config.proposals, variances=self._variances )
def setUp(self): super(RCNNProposalTest, self).setUp() self._num_classes = 3 self._image_shape = (900, 1440) self._config = EasyDict({ 'class_max_detections': 100, 'class_nms_threshold': 0.6, 'total_max_detections': 300, 'min_prob_threshold': 0.0, }) self._equality_delta = 1e-03 self._shared_model = RCNNProposal(self._num_classes, self._config) tf.reset_default_graph()