def fpn_map_rois_to_levels(boxes): """ Assign boxes to level 2~5. Args: boxes (nx4): Returns: [tf.Tensor]: 4 tensors for level 2-5. Each tensor is a vector of indices of boxes in its level. [tf.Tensor]: 4 tensors, the gathered boxes in each level. Be careful that the returned tensor could be empty. """ sqrtarea = tf.sqrt(tf_area(boxes)) level = tf.to_int32(tf.floor( 4 + tf.log(sqrtarea * (1. / 224) + 1e-6) * (1.0 / np.log(2)))) # RoI levels range from 2~5 (not 6) level_ids = [ tf.where(level <= 2), tf.where(tf.equal(level, 3)), # == is not supported tf.where(tf.equal(level, 4)), tf.where(level >= 5)] level_ids = [tf.reshape(x, [-1], name='roi_level{}_id'.format(i + 2)) for i, x in enumerate(level_ids)] num_in_levels = [tf.size(x, name='num_roi_level{}'.format(i + 2)) for i, x in enumerate(level_ids)] add_moving_summary(*num_in_levels) level_boxes = [tf.gather(boxes, ids) for ids in level_ids] return level_ids, level_boxes
def fpn_map_rois_to_levels(boxes): """ Assign boxes to level 2~5. Args: boxes (nx4): Returns: [tf.Tensor]: 4 tensors for level 2-5. Each tensor is a vector of indices of boxes in its level. [tf.Tensor]: 4 tensors, the gathered boxes in each level. Be careful that the returned tensor could be empty. """ sqrtarea = tf.sqrt(tf_area(boxes)) level = tf.cast(tf.floor( 4 + tf.math.log(sqrtarea * (1. / 224) + 1e-6) * (1.0 / np.log(2))), tf.int32) # RoI levels range from 2~5 (not 6) level_ids = [ tf.where(level <= 2), tf.where(tf.equal(level, 3)), # == is not supported tf.where(tf.equal(level, 4)), tf.where(level >= 5)] level_ids = [tf.reshape(x, [-1], name='roi_level{}_id'.format(i + 2)) for i, x in enumerate(level_ids)] num_in_levels = [tf.size(x, name='num_roi_level{}'.format(i + 2)) for i, x in enumerate(level_ids)] add_moving_summary(*num_in_levels) level_boxes = [tf.gather(boxes, ids) for ids in level_ids] return level_ids, level_boxes
def run_head(self, proposals, stage): """ Args: proposals: BoxProposals stage: 0, 1, 2 Returns: FastRCNNHead Nx4, updated boxes """ reg_weights = tf.constant(cfg.CASCADE.BBOX_REG_WEIGHTS[stage], dtype=tf.float32) pooled_feature = self.roi_func(proposals.boxes) # N,C,S,S pooled_feature = self.scale_gradient(pooled_feature) head_feature = self.fastrcnn_head_func('head', pooled_feature) label_logits, box_logits = fastrcnn_outputs( 'outputs', head_feature, self.num_categories, class_agnostic_regression=True) head = FastRCNNHead(proposals, box_logits, label_logits, self.gt_boxes, reg_weights) refined_boxes = head.decoded_output_boxes_class_agnostic() refined_boxes = clip_boxes(refined_boxes, self.image_shape2d) if self.training: refined_boxes = tf.boolean_mask(refined_boxes, tf_area(refined_boxes) > 0) return head, tf.stop_gradient(refined_boxes, name='output_boxes')
def build_graph(self, *inputs): inputs = dict(zip(self.input_names, inputs)) if "gt_masks_packed" in inputs: gt_masks = tf.cast(unpackbits_masks(inputs.pop("gt_masks_packed")), tf.uint8, name="gt_masks") inputs["gt_masks"] = gt_masks image = self.preprocess(inputs['image']) # 1CHW features = self.backbone(image) anchor_inputs = {k: v for k, v in inputs.items() if k.startswith('anchor_')} proposals, rpn_losses = self.rpn(image, features, anchor_inputs) # inputs? targets = [inputs[k] for k in ['gt_boxes', 'gt_labels', 'gt_masks'] if k in inputs] gt_boxes_area = tf.reduce_mean(tf_area(inputs["gt_boxes"]), name='mean_gt_box_area') add_moving_summary(gt_boxes_area) head_losses = self.roi_heads(image, features, proposals, targets) if self.training: wd_cost = regularize_cost( '.*/W', l2_regularizer(cfg.TRAIN.WEIGHT_DECAY), name='wd_cost') total_cost = tf.add_n( rpn_losses + head_losses + [wd_cost], 'total_cost') add_moving_summary(total_cost, wd_cost) return total_cost else: # Check that the model defines the tensors it declares for inference # For existing models, they are defined in "fastrcnn_predictions(name_scope='output')" G = tf.get_default_graph() ns = G.get_name_scope() for name in self.get_inference_tensor_names()[1]: try: name = '/'.join([ns, name]) if ns else name G.get_tensor_by_name(name + ':0') except KeyError: raise KeyError("Your model does not define the tensor '{}' in inference context.".format(name))
def fpn_map_rois_to_levels(boxes): """ Assign boxes to level 2~5. Args: boxes: t x 5, t is the number of sampled boxes Returns: level_ids[tf.Tensor]: 4 tensors for level 2-5. Each tensor is a vector of indices of boxes in its level. level_boxes[tf.Tensor]: 4 tensors, the gathered boxes in each level. Be careful that the returned tensor could be empty. """ sqrtarea = tf.sqrt(tf_area(boxes[:,1:])) # Map equation from the FPN paper: https://arxiv.org/abs/1612.03144, page 4 # k = [k0 + log2(sqrt(wh)/224)] level = tf.cast(tf.floor( 4 + tf.log(sqrtarea * (1. / 224) + 1e-6) * (1.0 / np.log(2))), tf.int32) # RoI levels range from 2~5 (not 6) level_ids = [ tf.where(level <= 2), tf.where(tf.equal(level, 3)), # == is not supported tf.where(tf.equal(level, 4)), tf.where(level >= 5)] level_ids = [tf.reshape(x, [-1], name='roi_level{}_id'.format(i + 2)) for i, x in enumerate(level_ids)] num_in_levels = [tf.size(x, name='num_roi_level{}'.format(i + 2)) for i, x in enumerate(level_ids)] add_moving_summary(*num_in_levels) level_boxes = [tf.gather(boxes, ids) for ids in level_ids] return level_ids, level_boxes
def build_graph(self, *inputs): print(f"self.input_names : {self.input_names}") print(F"inputs before : {inputs}") inputs = dict(zip(self.input_names, inputs)) print(f"inputs : {inputs}") if "gt_masks_packed" in inputs: gt_masks = tf.cast(unpackbits_masks(inputs.pop("gt_masks_packed")), tf.uint8, name="gt_masks") inputs["gt_masks"] = gt_masks print("inputs[gt_masks] :", inputs["gt_masks"]) # inputs['image'] = tf.Print(inputs['image'], [tf.shape(inputs['image'])], message="image before preprocess : ", summarize=100) image = self.preprocess(inputs['image']) # 1CHW # image = tf.Print(image, [tf.shape(image)], message="image after preprocess : ", summarize=100) features = self.backbone(image) for i, feature in enumerate(features): feature = tf.Print(feature, [tf.shape(feature)], message=f"feature p{i+2} : ", summarize=100) # 여기까지 봄 anchor_inputs = {k: v for k, v in inputs.items() if k.startswith('anchor_')} # anchor_inputs = tf.Print(anchor_inputs, [anchor_inputs], message="anchor_inputs : ", summarize=100) proposals, rpn_losses = self.rpn(image, features, anchor_inputs) # inputs? # proposals = tf.Print(proposals, [tf.shape(proposals)], message="proposals : ", summarize=100) # rpn_losses = tf.Print(rpn_losses, [tf.shape(rpn_losses)], message="rpn_losses : ", summarize=100) targets = [inputs[k] for k in ['gt_boxes', 'gt_labels', 'gt_masks'] if k in inputs] gt_boxes_area = tf.reduce_mean(tf_area(inputs["gt_boxes"]), name='mean_gt_box_area') add_moving_summary(gt_boxes_area) head_losses = self.roi_heads(image, features, proposals, targets) if self.training: print("l2_regularizer") print(l2_regularizer) wd_cost = regularize_cost( '.*/W', l2_regularizer(cfg.TRAIN.WEIGHT_DECAY), name='wd_cost') total_cost = tf.add_n( rpn_losses + head_losses + [wd_cost], 'total_cost') add_moving_summary(total_cost, wd_cost) return total_cost else: # Check that the model defines the tensors it declares for inference # For existing models, they are defined in "fastrcnn_predictions(name_scope='output')" G = tf.get_default_graph() ns = G.get_name_scope() for name in self.get_inference_tensor_names()[1]: try: name = '/'.join([ns, name]) if ns else name G.get_tensor_by_name(name + ':0') except KeyError: raise KeyError("Your model does not define the tensor '{}' in inference context.".format(name))