python类less_equal()的实例源码

Dense_Transformer_Network.py 文件源码 项目:3D_Dense_Transformer_Networks 作者: JohnYC1995 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _bilinear_interpolate(self,im, im_org, x, y):
        with tf.variable_scope('_interpolate'):
            # constants
            x = tf.cast(x, 'float32')
            y = tf.cast(y, 'float32')
            height_f = tf.cast(self.height, 'float32')
            width_f = tf.cast(self.width, 'float32')
            zero = tf.zeros([], dtype='int32')
            max_y = tf.cast(tf.shape(im)[1] - 1, 'int32')
            max_x = tf.cast(tf.shape(im)[2] - 1, 'int32')
            # scale indices from [-1, 1] to [0, width/height]
            x = (x + 1.0)*(width_f) / 2.0
            y = (y + 1.0)*(height_f) / 2.0
            # do sampling
            x0 = tf.cast(tf.floor(x), 'int32')
            x1 = x0 + 1
            y0 = tf.cast(tf.floor(y), 'int32')
            y1 = y0 + 1
            x0 = tf.clip_by_value(x0, zero, max_x)
            x1 = tf.clip_by_value(x1, zero, max_x)
            y0 = tf.clip_by_value(y0, zero, max_y)
            y1 = tf.clip_by_value(y1, zero, max_y)
            dim2 = self.width
            dim1 = self.width*self.height
            base = self._repeat(tf.range(self.num_batch)*dim1, self.out_height*self.out_width, 'int32')
            base_y0 = base + y0*dim2
            base_y1 = base + y1*dim2
            idx_a = tf.expand_dims(base_y0 + x0, 1)
            idx_b = tf.expand_dims(base_y1 + x0, 1)
            idx_c = tf.expand_dims(base_y0 + x1, 1)
            idx_d = tf.expand_dims(base_y1 + x1, 1)
            # use indices to lookup pixels in the flat image and restore
            # channels dim
            im_flat = tf.reshape(im, tf.stack([-1, self.num_channels]))
            im_flat = tf.cast(im_flat, 'float32')
            Ia = tf.scatter_nd(idx_a, im_flat, [self.num_batch*self.out_height*self.out_width, self.num_channels])
            Ib = tf.scatter_nd(idx_b, im_flat, [self.num_batch*self.out_height*self.out_width, self.num_channels])
            Ic = tf.scatter_nd(idx_c, im_flat, [self.num_batch*self.out_height*self.out_width, self.num_channels])
            Id = tf.scatter_nd(idx_d, im_flat, [self.num_batch*self.out_height*self.out_width, self.num_channels])

            x0_f = tf.cast(x0, 'float32')
            x1_f = tf.cast(x1, 'float32')
            y0_f = tf.cast(y0, 'float32')
            y1_f = tf.cast(y1, 'float32')
            wa = tf.scatter_nd(idx_a, tf.expand_dims(((x1_f-x) * (y1_f-y)), 1), [self.num_batch*self.out_height*self.out_width, 1])
            wb = tf.scatter_nd(idx_b, tf.expand_dims(((x1_f-x) * (y-y0_f)), 1), [self.num_batch*self.out_height*self.out_width, 1])
            wc = tf.scatter_nd(idx_c, tf.expand_dims(((x-x0_f) * (y1_f-y)), 1), [self.num_batch*self.out_height*self.out_width, 1])
            wd = tf.scatter_nd(idx_d, tf.expand_dims(((x-x0_f) * (y-y0_f)), 1), [self.num_batch*self.out_height*self.out_width, 1])

            value_all = tf.add_n([wa*Ia, wb*Ib, wc*Ic, wd*Id])
            weight_all = tf.clip_by_value(tf.add_n([wa, wb, wc, wd]),1e-5,1e+10)
            flag = tf.less_equal(weight_all, 1e-5* tf.ones_like(weight_all))
            flag = tf.cast(flag, tf.float32)
            im_org = tf.reshape(im_org, [-1,self.num_channels])
            output = tf.add(tf.div(value_all, weight_all), tf.multiply(im_org, flag))
            return output
renderer.py 文件源码 项目:tf.rasterizer 作者: vahidk 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def draw_fn(self, shader):
        indices = tf.placeholder(tf.int32, [None, 3], name="ph_indices")
        verts = [None, None, None]

        for i in range(3):
            verts[i] = shader.vertex(indices[:, i], i)
            verts[i] = tf.matmul(verts[i], self.viewport, transpose_b=True)
            verts[i] = utils.affine_to_cartesian(verts[i])

        bbmin, bbmax = bounds(verts, self.width, self.height)

        def _fn(i):
            bbmin_i = tf.gather(bbmin, i)
            bbmax_i = tf.gather(bbmax, i)
            verts_i = [tf.gather(verts[0], i),
                       tf.gather(verts[1], i),
                       tf.gather(verts[2], i)]

            x, y = tf.meshgrid(tf.range(bbmin_i[0], bbmax_i[0]),
                               tf.range(bbmin_i[1], bbmax_i[1]))

            num_frags = tf.reduce_prod(tf.shape(x))
            p = tf.stack([tf.reshape(x, [-1]),
                          tf.reshape(y, [-1]),
                          tf.zeros([num_frags], dtype=tf.float32)], axis=1)

            bc, valid = barycentric(verts_i, p)

            p = tf.boolean_mask(p, valid)
            bc = [tf.boolean_mask(bc[k], valid) for k in range(3)]
            z = utils.tri_dot([verts_i[k][2] for k in range(3)], bc)

            inds = tf.to_int32(tf.stack([p[:, 1], p[:, 0]], axis=1))
            cur_z = tf.gather_nd(self.depth, inds)
            visible = tf.less_equal(cur_z, z)

            inds = tf.boolean_mask(inds, visible)
            bc = [tf.boolean_mask(bc[k], visible) for k in range(3)]
            z = tf.boolean_mask(z, visible)

            c = utils.pack_colors(shader.fragment(bc, i), 1)

            updates = [
                tf.scatter_nd_update(self.color, inds, c, use_locking=False),
                tf.scatter_nd_update(self.depth, inds, z, use_locking=False)]
            return updates

        num_faces = tf.shape(indices)[0]
        updates = utils.sequential_for(_fn, 0, num_faces)
        self.commands.append(updates)

        def _draw(indices_val, **kwargs):
            self.args[indices] = indices_val
            for k, v in kwargs.items():
                self.args[getattr(shader, k)] = v

        return _draw
ops.py 文件源码 项目:LiTeFlow 作者: petrux 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def trim(tensor, width):
    """Trim the tensor on the -1 axis.

    Trim a given tensor of shape `[..., in_width]` to a smaller tensor
    of shape `[..., width]`, along the -1 axis. If the `width` argument
    is greater or equal than the actual width of the tensor, no operation
    is performed.

    Arguments:
      tensor: a 3D tf.Tensor of shape `[..., in_width]`.
      width: a `int` representing the target value of the 3rd
        dimension of the output tensor.

    Returns:
      a 3D tensor of shape `[..., width]` where the
        third dimension is the minimum between the input width
        and the value of the `width` argument.

    Example:
    ```python
    # t is a tensor like:
    # [[[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]],
        [7, 7, 7],
        [8, 8, 8],
        [9, 9, 9]]]

    q = trim(t, 2)

    # q is a tensor like:
    # [[[1, 1],
        [2, 2],
        [3, 3]],
        [7, 7],
        [8, 8],
        [9, 9]]]
"""
result = tf.cond(
    tf.less_equal(tf.shape(tensor)[-1], width),
    lambda: tensor,
    lambda: _trim(tensor, width))
result.set_shape(tensor.get_shape().as_list()[:-1] + [width])
return result

```

deeplab_model.py 文件源码 项目:TF-deeplab 作者: chenxi116 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _build_train_op(self):
    """Build training specific ops for the graph."""
    labels_coarse = tf.image.resize_nearest_neighbor(self.labels, 
      [tf.shape(self.pred)[1], tf.shape(self.pred)[2]])
    labels_coarse = tf.squeeze(labels_coarse, squeeze_dims=[3])
    self.labels_coarse = tf.to_int32(labels_coarse)

    # ignore illegal labels
    raw_pred = tf.reshape(self.logits, [-1, self.num_classes])
    raw_gt = tf.reshape(self.labels_coarse, [-1,])
    indices = tf.squeeze(tf.where(tf.less_equal(raw_gt, self.num_classes - 1)), 1)
    remain_pred = tf.gather(raw_pred, indices)
    remain_gt = tf.gather(raw_gt, indices)

    xent = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=remain_pred, 
      labels=remain_gt)
    self.cls_loss = tf.reduce_mean(xent, name='xent')
    self.cost = self.cls_loss + self._decay()
    # tf.summary.scalar('cost', self.cost)

    self.global_step = tf.Variable(0, name='global_step', trainable=False)
    self.learning_rate = tf.train.polynomial_decay(self.lrn_rate, 
      self.global_step, self.lr_decay_step, power=0.9)
    # tf.summary.scalar('learning rate', self.learning_rate)

    tvars = tf.trainable_variables()

    if self.optimizer == 'sgd':
      optimizer = tf.train.GradientDescentOptimizer(self.learning_rate)
    elif self.optimizer == 'mom':
      optimizer = tf.train.MomentumOptimizer(self.learning_rate, 0.9)
    else:
      raise NameError("Unknown optimizer type %s!" % self.optimizer)

    grads_and_vars = optimizer.compute_gradients(self.cost, var_list=tvars)
    var_lr_mult = {}
    for var in tvars:
      if var.op.name.find(r'fc1_voc12') > 0 and var.op.name.find(r'biases') > 0:
        var_lr_mult[var] = 20.
      elif var.op.name.find(r'fc1_voc12') > 0:
        var_lr_mult[var] = 10.
      else:
        var_lr_mult[var] = 1.
    grads_and_vars = [((g if var_lr_mult[v] == 1 else tf.multiply(var_lr_mult[v], g)), v) 
        for g, v in grads_and_vars]

    apply_op = optimizer.apply_gradients(grads_and_vars,
        global_step=self.global_step, name='train_step')

    train_ops = [apply_op] + self._extra_train_ops
    self.train_step = tf.group(*train_ops)

  # TODO(xpan): Consider batch_norm in contrib/layers/python/layers/layers.py
textdataflow.py 文件源码 项目:tefla 作者: openAGI 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def bucket_by_sequence_length(self, dataset, example_length_fn, bucket_boundaries,
                                  bucket_batch_sizes, window_size):
        """Bucket entries in dataset by length.

        Args:
          dataset: Dataset of dict<feature name, Tensor>.
          example_length_fn: function from example to int, determines the length of
            the example, which will determine the bucket it goes into.
          bucket_boundaries: list<int>, boundaries of the buckets.
          bucket_batch_sizes: list<int>, batch size per bucket.
          window_size: an integer divisible by all elements of bucket_batch_sizes

        Returns:
          Dataset of padded and batched examples.
        """
        with tf.name_scope("bucket_by_seq_length"):

            def example_to_bucket_id(example):
                """Return int64 id of the length bucket for this example."""
                seq_length = example_length_fn(example)

                boundaries = list(bucket_boundaries)
                buckets_min = [np.iinfo(np.int32).min] + boundaries
                buckets_max = boundaries + [np.iinfo(np.int32).max]
                conditions_c = tf.logical_and(
                    tf.less_equal(buckets_min, seq_length),
                    tf.less(seq_length, buckets_max))
                bucket_id = tf.reduce_min(tf.where(conditions_c))

                return bucket_id

            def batching_fn(bucket_id, grouped_dataset):
                batch_sizes = tf.constant(bucket_batch_sizes, dtype=tf.int64)
                batch_size = batch_sizes[bucket_id]

                # Pad each dimension of each feature so that they match.
                padded_shapes = dict(
                    [(name, [None] * len(shape))
                     for name, shape in grouped_dataset.output_shapes.items()])
                return grouped_dataset.padded_batch(batch_size, padded_shapes)

            dataset = dataset.group_by_window(example_to_bucket_id, batching_fn,
                                              window_size)
            return dataset
siamese.py 文件源码 项目:jon-siamese 作者: maigimenez 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __init__(self, sequence_length, vocab_size, embedding_size,
                 filter_sizes, num_filters, margin):
        with tf.name_scope("embeddings") as embeddings_scope:
            self.filter_sizes = filter_sizes
            self.embedding_size = embedding_size
            self.num_filters = num_filters
            self.W_embedding = tf.Variable(tf.random_uniform([vocab_size, self.embedding_size], -1.0, 1.0),
                                           trainable=True, name="W_embedding")
            self.is_training = tf.placeholder(tf.bool, [], name='is_training')

        with tf.variable_scope("siamese") as siam_scope:
            # 1ST LAYER: Embedding layer
            with tf.variable_scope("embeddings-siamese") as input_scope:
                self.left_input = tf.placeholder(tf.int32, [None, sequence_length], name='left')
                left_embedded_words = tf.nn.embedding_lookup(self.W_embedding, self.left_input)
                self.left_embedded = tf.expand_dims(left_embedded_words, -1, name='left_embeddings')
                print('  ---> EMBEDDING LEFT: ', self.left_embedded)

                self.right_input = tf.placeholder(tf.int32, [None, sequence_length], name='right')
                right_embedded_words = tf.nn.embedding_lookup(self.W_embedding, self.right_input)
                self.right_embedded = tf.expand_dims(right_embedded_words, -1, name='right_embeddings')
                print('  ---> EMBEDDING RIGHT: ', self.right_embedded)

            self.left_siamese = self.subnet(self.left_embedded, 'left', False)
            print("---> SIAMESE TENSOR: ", self.left_siamese)
            siam_scope.reuse_variables()
            self.right_siamese = self.subnet(self.right_embedded, 'right', True)
            print("---> SIAMESE TENSOR: ", self.right_siamese)

        with tf.name_scope("similarity"):
            print('\n ----------------------- JOIN SIAMESE ----------------------------')
            self.labels = tf.placeholder(tf.int32, [None, 1], name='labels')
            self.labels = tf.to_float(self.labels)
            print('---> LABELS: ', self.labels)

            with tf.variable_scope("loss"):
                self.margin = tf.get_variable('margin', dtype=tf.float32,
                                              initializer=tf.constant(margin, shape=[1]),
                                              trainable=False)
                self.loss, self.attr, \
                self.rep, self.distance, self.maxpart = contrastive_loss(self.labels,
                                                           self.left_siamese,
                                                           self.right_siamese,
                                                           self.margin)

        with tf.name_scope("prediction"):
            # TODO Este es un parámetro de configuración
            self.threshold = tf.get_variable('threshold', dtype=tf.float32,
                                             initializer=tf.constant(1.0, shape=[1]))
            self.predictions = tf.less_equal(self.distance, self.threshold)
            self.predictions = tf.cast(self.predictions, 'float32')
            self.correct_predictions = tf.equal(self.predictions, self.labels)
            self.accuracy = tf.reduce_mean(tf.cast(self.correct_predictions, tf.float32))
data_reader.py 文件源码 项目:tensor2tensor 作者: tensorflow 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def bucket_by_sequence_length(dataset,
                              example_length_fn,
                              bucket_boundaries,
                              bucket_batch_sizes,
                              padded_shapes=None):
  """Bucket entries in dataset by length.

  Args:
    dataset: Dataset of dict<feature name, Tensor>.
    example_length_fn: function from example to int, determines the length of
      the example, which will determine the bucket it goes into.
    bucket_boundaries: list<int>, boundaries of the buckets.
    bucket_batch_sizes: list<int>, batch size per bucket.
    padded_shapes: dict<feature name, list<int>>, optional, shapes of the
      features with None where feature should be padded to max in that dim.

  Returns:
    Dataset of padded and batched examples.
  """
  with tf.name_scope("bucket_by_seq_length"):

    def example_to_bucket_id(example):
      """Return int64 id of the length bucket for this example."""
      seq_length = example_length_fn(example)

      boundaries = list(bucket_boundaries)
      buckets_min = [np.iinfo(np.int32).min] + boundaries
      buckets_max = boundaries + [np.iinfo(np.int32).max]
      conditions_c = tf.logical_and(
          tf.less_equal(buckets_min, seq_length),
          tf.less(seq_length, buckets_max))
      bucket_id = tf.reduce_min(tf.where(conditions_c))

      return bucket_id

    def window_size_fn(bucket_id):
      # window size = batch size
      batch_sizes = tf.constant(bucket_batch_sizes, dtype=tf.int64)
      window_size = batch_sizes[bucket_id]
      return window_size

    def batching_fn(bucket_id, grouped_dataset):
      batch_sizes = tf.constant(bucket_batch_sizes, dtype=tf.int64)
      batch_size = batch_sizes[bucket_id]
      return padded_batch(grouped_dataset, batch_size, padded_shapes)

    dataset = dataset.apply(
        tf.contrib.data.group_by_window(example_to_bucket_id, batching_fn, None,
                                        window_size_fn))
    return dataset
losses.py 文件源码 项目:tensorflow 作者: luyishisi 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _subsample_selection_to_desired_neg_pos_ratio(self,
                                                    indices,
                                                    match,
                                                    max_negatives_per_positive,
                                                    min_negatives_per_image=0):
    """Subsample a collection of selected indices to a desired neg:pos ratio.

    This function takes a subset of M indices (indexing into a large anchor
    collection of N anchors where M<N) which are labeled as positive/negative
    via a Match object (matched indices are positive, unmatched indices
    are negative).  It returns a subset of the provided indices retaining all
    positives as well as up to the first K negatives, where:
      K=floor(num_negative_per_positive * num_positives).

    For example, if indices=[2, 4, 5, 7, 9, 10] (indexing into 12 anchors),
    with positives=[2, 5] and negatives=[4, 7, 9, 10] and
    num_negatives_per_positive=1, then the returned subset of indices
    is [2, 4, 5, 7].

    Args:
      indices: An integer tensor of shape [M] representing a collection
        of selected anchor indices
      match: A matcher.Match object encoding the match between anchors and
        groundtruth boxes for a given image, with rows of the Match objects
        corresponding to groundtruth boxes and columns corresponding to anchors.
      max_negatives_per_positive: (float) maximum number of negatives for
        each positive anchor.
      min_negatives_per_image: minimum number of negative anchors for a given
        image. Allow sampling negatives in image without any positive anchors.

    Returns:
      selected_indices: An integer tensor of shape [M'] representing a
        collection of selected anchor indices with M' <= M.
      num_positives: An integer tensor representing the number of positive
        examples in selected set of indices.
      num_negatives: An integer tensor representing the number of negative
        examples in selected set of indices.
    """
    positives_indicator = tf.gather(match.matched_column_indicator(), indices)
    negatives_indicator = tf.gather(match.unmatched_column_indicator(), indices)
    num_positives = tf.reduce_sum(tf.to_int32(positives_indicator))
    max_negatives = tf.maximum(min_negatives_per_image,
                               tf.to_int32(max_negatives_per_positive *
                                           tf.to_float(num_positives)))
    topk_negatives_indicator = tf.less_equal(
        tf.cumsum(tf.to_int32(negatives_indicator)), max_negatives)
    subsampled_selection_indices = tf.where(
        tf.logical_or(positives_indicator, topk_negatives_indicator))
    num_negatives = tf.size(subsampled_selection_indices) - num_positives
    return (tf.reshape(tf.gather(indices, subsampled_selection_indices), [-1]),
            num_positives, num_negatives)


问题


面经


文章

微信
公众号

扫码关注公众号