Python tensorflow.invert_permutation() Examples

The following are 10 code examples of tensorflow.invert_permutation(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: models.py    From Object_Detection_Tracking with Apache License 2.0 6 votes vote down vote up
def multilevel_roi_align(self, features, rcnn_boxes, output_shape):
    config = self.config
    assert len(features) == 4
    # Reassign rcnn_boxes to levels # based on box area size
    level_ids, level_boxes = self.fpn_map_rois_to_levels(rcnn_boxes)
    all_rois = []

    # Crop patches from corresponding levels
    for i, boxes, featuremap in zip(itertools.count(), level_boxes, features):
      with tf.name_scope("roi_level%s" % (i + 2)):
        boxes_on_featuremap = boxes * (1.0 / config.anchor_strides[i])
        all_rois.append(
            roi_align(featuremap, boxes_on_featuremap, output_shape))

    # this can fail if using TF<=1.8 with MKL build
    all_rois = tf.concat(all_rois, axis=0)  # NCHW
    # Unshuffle to the original order, to match the original samples
    level_id_perm = tf.concat(level_ids, axis=0)  # A permutation of 1~N
    level_id_invert_perm = tf.invert_permutation(level_id_perm)
    all_rois = tf.gather(all_rois, level_id_invert_perm)
    return all_rois 
Example #2
Source File: models.py    From Object_Detection_Tracking with Apache License 2.0 6 votes vote down vote up
def multilevel_roi_align(self, features, rcnn_boxes, output_shape):
    config = self.config
    assert len(features) == 4
    # Reassign rcnn_boxes to levels # based on box area size
    level_ids, level_boxes = self.fpn_map_rois_to_levels(rcnn_boxes)
    all_rois = []

    # Crop patches from corresponding levels
    for i, boxes, featuremap in zip(itertools.count(), level_boxes, features):
      with tf.name_scope("roi_level%s"%(i + 2)):
        boxes_on_featuremap = boxes * (1.0 / config.anchor_strides[i])
        all_rois.append(roi_align(featuremap, boxes_on_featuremap, output_shape))

    # this can fail if using TF<=1.8 with MKL build
    all_rois = tf.concat(all_rois, axis=0)  # NCHW
    # Unshuffle to the original order, to match the original samples
    level_id_perm = tf.concat(level_ids, axis=0)  # A permutation of 1~N
    level_id_invert_perm = tf.invert_permutation(level_id_perm)
    all_rois = tf.gather(all_rois, level_id_invert_perm)
    return all_rois 
Example #3
Source File: memory.py    From DNC with MIT License 6 votes vote down vote up
def calculate_allocation_weighting(self, usage_vector):
        """

        :param: usage vector: tensor of shape [batch_size, memory_size]
        :return: allocation tensor of shape [batch_size, memory_size]
        """
        usage_vector = Memory.epsilon + (1 - Memory.epsilon) * usage_vector

        # We're sorting the "-self.usage_vector" because top_k returns highest values and we need the lowest
        highest_usage, inverse_indices = tf.nn.top_k(-usage_vector, k=self.memory_size)
        lowest_usage = -highest_usage

        allocation_scrambled = (1 - lowest_usage) * tf.cumprod(lowest_usage, axis=1, exclusive=True)

        # allocation is not in the correct order. alloation[i] contains the sorted[i] value
        # reversing the already inversed indices for each batch
        indices = tf.stack([tf.invert_permutation(batch_indices) for batch_indices in tf.unstack(inverse_indices)])
        allocation = tf.stack([tf.gather(mem, ind)
                               for mem, ind in
                               zip(tf.unstack(allocation_scrambled), tf.unstack(indices))])

        return allocation 
Example #4
Source File: util.py    From dnc with Apache License 2.0 5 votes vote down vote up
def batch_invert_permutation(permutations):
  """Returns batched `tf.invert_permutation` for every row in `permutations`."""
  with tf.name_scope('batch_invert_permutation', values=[permutations]):
    perm = tf.cast(permutations, tf.float32)
    dim = int(perm.get_shape()[-1])
    size = tf.cast(tf.shape(perm)[0], tf.float32)
    delta = tf.cast(tf.shape(perm)[-1], tf.float32)
    rg = tf.range(0, size * delta, delta, dtype=tf.float32)
    rg = tf.expand_dims(rg, 1)
    rg = tf.tile(rg, [1, dim])
    perm = tf.add(perm, rg)
    flat = tf.reshape(perm, [-1])
    perm = tf.invert_permutation(tf.cast(flat, tf.int32))
    perm = tf.reshape(perm, [-1, dim])
    return tf.subtract(perm, tf.cast(rg, tf.int32)) 
Example #5
Source File: rec_input_fn_local.py    From rec-rl with Apache License 2.0 5 votes vote down vote up
def _invert_permutation(input, row_count):
    '''wrapper for matrix'''
    rows = []
    for i in range(row_count):
        row = input[i,:]
        rows.append(tf.invert_permutation(row))
    return tf.cast(tf.stack(rows, axis=0), tf.float32) 
Example #6
Source File: rec_env.py    From rec-rl with Apache License 2.0 5 votes vote down vote up
def _invert_permutation(tensor):
    '''wrapper for matrix'''
    return tf.cast(tf.map_fn(tf.invert_permutation, tensor), tf.float32) 
Example #7
Source File: tensor_ops.py    From hart with GNU General Public License v3.0 5 votes vote down vote up
def inverse_selection(y, presence, batch_size=1, name='inverse_selection'):
    with tf.variable_scope(name):
        idx = tf.reshape(tf.range(tf.size(y)), tf.shape(y))
        idx = tf.reshape(select_present(idx, presence, batch_size), (-1,))
        idx = tf.invert_permutation(idx)

        x = tf.gather(tf.reshape(y, (-1,)), idx)
    return tf.reshape(x, tf.shape(y)) 
Example #8
Source File: ops.py    From tfdeploy with MIT License 5 votes vote down vote up
def test_InvertPermutation(self):
        t = tf.invert_permutation(np.random.permutation(10))
        self.check(t)


    #
    # control flow ops
    # 
Example #9
Source File: efficientdet_wrapper.py    From Object_Detection_Tracking with Apache License 2.0 5 votes vote down vote up
def multilevel_roi_align(fpn_feats, boxes, level_indexes, output_shape,
                         eff_config):
  """
    Given [R, 4] boxes and [R] level_indexes indicating the FPN level
    # boxes are x1, y1, x2, y2
  """
  # gather boxes for each feature level
  all_rois = []
  level_ids = []
  # for debuging
  #boxes_on_fp = []
  #1920 -> [160, 80, 40, 20, 10]/{3, 4, 5, 6, 7}
  for level in range(eff_config.min_level, eff_config.max_level + 1):
    this_level_boxes_idxs = tf.where(tf.equal(level_indexes, level))
    # [K, 1] -> [K]
    this_level_boxes_idxs = tf.reshape(this_level_boxes_idxs, [-1])
    level_ids.append(this_level_boxes_idxs)
    this_level_boxes = tf.gather(boxes, this_level_boxes_idxs)
    boxes_on_featuremap = this_level_boxes * (1.0 / (2. ** level))
    featuremap = fpn_feats[level]  # [1, H, W, C]
    # [K, output_shape, output_shape, C]
    box_feats = roi_align(featuremap, boxes_on_featuremap, output_shape)
    box_feats = tf.reduce_mean(box_feats, axis=[1, 2])  #  [K, C]
    all_rois.append(box_feats)

    # for debugging
    #boxes_on_fp.append(boxes_on_featuremap)

  all_rois = tf.concat(all_rois, axis=0)
  # Unshuffle to the original order, to match the original samples
  level_id_perm = tf.concat(level_ids, axis=0)  # A permutation of 1~N
  level_id_invert_perm = tf.invert_permutation(level_id_perm)
  all_rois = tf.gather(all_rois, level_id_invert_perm)

  #boxes_on_fp = tf.concat(boxes_on_fp, axis=0)
  #boxes_on_fp = tf.gather(boxes_on_fp, level_id_invert_perm)
  return all_rois#, boxes_on_fp 
Example #10
Source File: model_fpn.py    From tensorpack with Apache License 2.0 5 votes vote down vote up
def multilevel_roi_align(features, rcnn_boxes, resolution):
    """
    Args:
        features ([tf.Tensor]): 4 FPN feature level 2-5
        rcnn_boxes (tf.Tensor): nx4 boxes
        resolution (int): output spatial resolution
    Returns:
        NxC x res x res
    """
    assert len(features) == 4, features
    # Reassign rcnn_boxes to levels
    level_ids, level_boxes = fpn_map_rois_to_levels(rcnn_boxes)
    all_rois = []

    # Crop patches from corresponding levels
    for i, boxes, featuremap in zip(itertools.count(), level_boxes, features):
        with tf.name_scope('roi_level{}'.format(i + 2)):
            boxes_on_featuremap = boxes * (1.0 / cfg.FPN.ANCHOR_STRIDES[i])
            all_rois.append(roi_align(featuremap, boxes_on_featuremap, resolution))

    # this can fail if using TF<=1.8 with MKL build
    all_rois = tf.concat(all_rois, axis=0)  # NCHW
    # Unshuffle to the original order, to match the original samples
    level_id_perm = tf.concat(level_ids, axis=0)  # A permutation of 1~N
    level_id_invert_perm = tf.invert_permutation(level_id_perm)
    all_rois = tf.gather(all_rois, level_id_invert_perm, name="output")
    return all_rois