def Proposals(self): #threshold probability rpn_bbox_pred = self.prediction_dict['rpn_bbox_pred'] rpn_cls_prob = self.prediction_dict['rpn_cls_prob'] '''x_min, y_min, x_max, y_max = tf.unstack(self.all_anchors, axis=1) anchor_filter = tf.logical_and( tf.logical_and ( tf.greater_equal(x_min, 0), tf.greater_equal(y_min, 0) ), tf.logical_and ( tf.less_equal(x_max, self.im_shape[1]), tf.less_equal(y_max, self.im_shape[0]) ) )''' anchor_filter = Filter(self.all_anchors, self.im_shape) all_scores = rpn_cls_prob[:, 1] all_scores = tf.reshape(all_scores, [-1]) #Anchors, bbox, scores--->>> filtered anchors = tf.boolean_mask(self.all_anchors, anchor_filter, axis=0) #C bbox_pred = tf.boolean_mask(rpn_bbox_pred, anchor_filter, axis=0) scores = tf.boolean_mask(all_scores, anchor_filter, axis=0) #Decoding anchors and b-box predictions to give predicted points on image proposals = Decode(anchors, bbox_pred) #Filter with < than threshold probability ~ 0.0 min_prob_filter = tf.greater_equal(scores, 0.0) #Filter with -ve or zero area x1, y1, x2, y2 = tf.unstack(proposals, axis=1) width = x2 - x1 + 1 height = y2 - y1 + 1 area = width * height area_filter = tf.greater_equal(area, 0) #combining both filters and applying net_filter = tf.logical_and(min_prob_filter, area_filter) unsorted_proposals = tf.boolean_mask(proposals, net_filter) unsorted_scores = tf.boolean_mask(scores, net_filter) #Separating top-K proposals by score k = 2000 k = tf.minimum(k, tf.shape(unsorted_scores)[0]) top_k = tf.nn.top_k(unsorted_scores, k=k) top_k_proposals = tf.gather(unsorted_proposals, top_k.indices) top_k_scores = top_k.values #Clipping proposals to image size top_k_proposals = Clip_Boxes(top_k_proposals, self.im_shape) #Non-Maximum supression ordered_tf_proposal = Change_Order(top_k_proposals) selected_indices = tf.image.non_max_suppression(ordered_tf_proposal, tf.reshape( top_k_scores, [-1]), max_output_size=200, iou_threshold=0.5) nms_proposal_tf_order = tf.gather(ordered_tf_proposal, selected_indices) proposals = Change_Order(nms_proposal_tf_order) scores = tf.gather(top_k_scores, selected_indices) self.prediction_dict['proposals'] = proposals self.prediction_dict['scores'] = scores
def Target_Anchors(self): # Keep only the coordinates of gt_boxes gt_boxes = self.gt_boxes[:, :4] all_anchors = self.all_anchors[:, :4] # Only keep anchors inside the image '''(x_min_anchor, y_min_anchor, x_max_anchor, y_max_anchor) = tf.unstack(all_anchors, axis=1) anchor_filter = tf.logical_and( tf.logical_and( tf.greater_equal(x_min_anchor, 0), tf.greater_equal(y_min_anchor, 0) ), tf.logical_and( tf.less(x_max_anchor, im_shape[1]), tf.less(y_max_anchor, im_shape[0]) ) )''' anchor_filter = Filter(all_anchors, self.im_shape) # Filter anchors. anchors = tf.boolean_mask(all_anchors, anchor_filter, name='filter_anchors') # Generate array with the labels for all_anchors. labels = tf.fill((tf.gather(tf.shape(all_anchors), [0])), -1) labels = tf.boolean_mask(labels, anchor_filter, name='filter_labels') # Intersection over union (IoU) overlap overlaps = BBox_Overlap(tf.to_float(anchors), tf.to_float(gt_boxes)) # Generate array with the IoU value of the closest GT box for each anchor max_overlaps = tf.reduce_max(overlaps, axis=1) #Assigning background labels negative_overlap_nonzero = tf.less(max_overlaps, 0.3) labels = tf.where(condition=negative_overlap_nonzero, x=tf.zeros(tf.shape(labels)), y=tf.to_float(labels)) # Get the value of the max IoU for the closest anchor for each gt gt_max_overlaps = tf.reduce_max(overlaps, axis=0) # Find all the indices that match (at least one, but could be more) gt_argmax_overlaps = tf.squeeze(tf.equal(overlaps, gt_max_overlaps)) gt_argmax_overlaps = tf.where(gt_argmax_overlaps)[:, 0] # Eliminate duplicates indices. gt_argmax_overlaps, _ = tf.unique(gt_argmax_overlaps) # Order the indices for sparse_to_dense compatibility gt_argmax_overlaps, _ = tf.nn.top_k(gt_argmax_overlaps, k=tf.shape(gt_argmax_overlaps)[-1]) gt_argmax_overlaps = tf.reverse(gt_argmax_overlaps, [0]) # Foreground label: We set 1 at gt_argmax_overlaps_cond indices gt_argmax_overlaps_cond = tf.sparse_to_dense(gt_argmax_overlaps, tf.shape( labels, out_type=tf.int64), True, default_value=False) labels = tf.where(condition=gt_argmax_overlaps_cond, x=tf.ones(tf.shape(labels)), y=tf.to_float(labels)) # Foreground label: above threshold Intersection over Union (IoU) positive_overlap_inds = tf.greater_equal(max_overlaps, 0.7) labels = tf.where(condition=positive_overlap_inds, x=tf.ones(tf.shape(labels)), y=labels) # Subsample positive labels if we have too many def subsample_positive(): # Shuffle the foreground indices disable_fg_inds = tf.random_shuffle(fg_inds) disable_place = (tf.shape(fg_inds)[0] - num_fg) disable_fg_inds = disable_fg_inds[:disable_place] disable_fg_inds, _ = tf.nn.top_k(disable_fg_inds, k=tf.shape(disable_fg_inds)[-1]) disable_fg_inds = tf.reverse(disable_fg_inds, [0]) disable_fg_inds = tf.sparse_to_dense(disable_fg_inds, tf.shape(labels, out_type=tf.int64), True, default_value=False) # Put -1 to ignore the anchors in the selected indices return tf.where(condition=tf.squeeze(disable_fg_inds), x=tf.to_float(tf.fill(tf.shape(labels), -1)), y=labels) num_fg = 128 #128 +ve anchors # Get foreground indices, get True in the indices where we have a one fg_inds = tf.equal(labels, 1) # We get only the indices where we have True fg_inds = tf.squeeze(tf.where(fg_inds), axis=1) fg_inds_size = tf.size(fg_inds) # Condition for check if we have too many positive labels subsample_positive_cond = fg_inds_size > num_fg # Check the condition and subsample positive labels labels = tf.cond(subsample_positive_cond, true_fn=subsample_positive, false_fn=lambda: labels) # Subsample negative labels if we have too many def subsample_negative(): disable_bg_inds = tf.random_shuffle(bg_inds) disable_place = (tf.shape(bg_inds)[0] - num_bg) disable_bg_inds = disable_bg_inds[:disable_place] # Order the indices for sparse_to_dense compatibility disable_bg_inds, _ = tf.nn.top_k(disable_bg_inds, k=tf.shape(disable_bg_inds)[-1]) disable_bg_inds = tf.reverse(disable_bg_inds, [0]) disable_bg_inds = tf.sparse_to_dense(disable_bg_inds, tf.shape(labels, out_type=tf.int64), True, default_value=False) # Put -1 to ignore the anchors in the selected indices return tf.where(condition=tf.squeeze(disable_bg_inds), x=tf.to_float(tf.fill(tf.shape(labels), -1)), y=labels) # Recalculate the foreground indices after (maybe) disable some of them # Get foreground indices, get True in the indices where we have a one. fg_inds = tf.equal(labels, 1) # We get only the indices where we have True. fg_inds = tf.squeeze(tf.where(fg_inds), axis=1) fg_inds_size = tf.size(fg_inds) num_bg = tf.to_int32(256 - fg_inds_size) # Get background indices, get True in the indices where we have a zero. bg_inds = tf.equal(labels, 0) # We get only the indices where we have True. bg_inds = tf.squeeze(tf.where(bg_inds), axis=1) bg_inds_size = tf.size(bg_inds) # Condition for check if we have too many positive labels. subsample_negative_cond = bg_inds_size > num_bg # Check the condition and subsample positive labels. labels = tf.cond(subsample_negative_cond, true_fn=subsample_negative, false_fn=lambda: labels) # Return bbox targets with shape (anchors.shape[0], 4) # Find the closest gt box for each anchor argmax_overlaps = tf.argmax(overlaps, axis=1) argmax_overlaps_unique, _ = tf.unique(argmax_overlaps) # Filter the gt_boxes # We get only the indices where we have "inside anchors" anchor_filter_inds = tf.where(anchor_filter) gt_boxes = tf.gather(gt_boxes, argmax_overlaps) bbox_targets = Encode(anchors, gt_boxes) # For the anchors that arent foreground, we ignore the bbox_targets anchor_foreground_filter = tf.equal(labels, 1) bbox_targets = tf.where(condition=anchor_foreground_filter, x=bbox_targets, y=tf.zeros_like(bbox_targets)) # We unroll "inside anchors" value for all anchors (for shape compatibility) bbox_targets = tf.scatter_nd(indices=tf.to_int32(anchor_filter_inds), updates=bbox_targets, shape=tf.shape(all_anchors)) labels_scatter = tf.scatter_nd(indices=tf.to_int32(anchor_filter_inds), updates=labels, shape=[tf.shape(all_anchors)[0]]) # Put -1 to ignore the indices with 0 generated by scatter_nd labels = tf.where(condition=anchor_filter, x=labels_scatter, y=tf.to_float(tf.fill(tf.shape(labels_scatter), -1))) max_overlaps = tf.scatter_nd(indices=tf.to_int32(anchor_filter_inds), updates=max_overlaps, shape=[tf.shape(all_anchors)[0]]) ## return labels, bbox_targets, max_overlaps self.prediction_dict['rpn_bbox_target'] = bbox_targets self.prediction_dict['rpn_cls_target'] = labels self.prediction_dict['max_overlaps'] = max_overlaps
termination_criterion=StoppingByEvaluationsCustom(max_evaluations=max_evaluations, reference_point=REFERENCE_POINT, AlgorithmName='SMPSO') ), algorithm_tag='SMPSO', problem_tag=problem_tag, run=run, ) ) return jobs if __name__ == '__main__': # Configure the experiments jobs = configure_experiment(problems={'OTN': problemOTN}, n_run=TIMES_TO_RUN) # Run the study output_directory = 'data' experiment = Experiment(output_dir=output_directory, jobs=jobs) experiment.run() Filter.RemovePenalty(output_directory) # Reference fronts is the folder where is the reference to be compared with. generate_summary_from_experiment( input_dir=output_directory, reference_fronts='C:\\Users\\aryss\\Documents\\Repositories\\OTN_Mastering\\Output\\CT3\\8 services', quality_indicators=[InvertedGenerationalDistance(), EpsilonIndicator(), HyperVolume(REFERENCE_POINT)] #quality_indicators = [HyperVolume(REFERENCE_POINT)] )