本文整理汇总了Python中tensorflow.boolean_mask方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.boolean_mask方法的具体用法?Python tensorflow.boolean_mask怎么用?Python tensorflow.boolean_mask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.boolean_mask方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: yolo_filter_boxes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6):
# Compute box scores
box_scores = box_confidence * box_class_probs
# Find the box_classes thanks to the max box_scores, keep track of the corresponding score
box_classes = K.argmax(box_scores, axis=-1)
box_class_scores = K.max(box_scores, axis=-1, keepdims=False)
# Create a filtering mask based on "box_class_scores" by using "threshold". The mask should have the
# same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold)
filtering_mask = box_class_scores >= threshold
# Apply the mask to scores, boxes and classes
scores = tf.boolean_mask(box_class_scores, filtering_mask)
boxes = tf.boolean_mask(boxes, filtering_mask)
classes = tf.boolean_mask(box_classes, filtering_mask)
return scores, boxes, classes
示例2: char_predictions
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def char_predictions(self, chars_logit):
"""Returns confidence scores (softmax values) for predicted characters.
Args:
chars_logit: chars logits, a tensor with shape
[batch_size x seq_length x num_char_classes]
Returns:
A tuple (ids, log_prob, scores), where:
ids - predicted characters, a int32 tensor with shape
[batch_size x seq_length];
log_prob - a log probability of all characters, a float tensor with
shape [batch_size, seq_length, num_char_classes];
scores - corresponding confidence scores for characters, a float
tensor
with shape [batch_size x seq_length].
"""
log_prob = utils.logits_to_log_prob(chars_logit)
ids = tf.to_int32(tf.argmax(log_prob, dimension=2), name='predicted_chars')
mask = tf.cast(
slim.one_hot_encoding(ids, self._params.num_char_classes), tf.bool)
all_scores = tf.nn.softmax(chars_logit)
selected_scores = tf.boolean_mask(all_scores, mask, name='char_scores')
scores = tf.reshape(selected_scores, shape=(-1, self._params.seq_length))
return ids, log_prob, scores
示例3: yolo_nms
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def yolo_nms(outputs, anchors, masks, num_classes, iou_threshold=0.6, score_threshold=0.15):
boxes, confs, classes = [], [], []
for o in outputs:
boxes.append(tf.reshape(o[0], (tf.shape(o[0])[0], -1, tf.shape(o[0])[-1])))
confs.append(tf.reshape(o[1], (tf.shape(o[0])[0], -1, tf.shape(o[1])[-1])))
classes.append(tf.reshape(o[2], (tf.shape(o[0])[0], -1, tf.shape(o[2])[-1])))
boxes = tf.concat(boxes, axis=1)
confs = tf.concat(confs, axis=1)
class_probs = tf.concat(classes, axis=1)
box_scores = confs * class_probs
mask = box_scores >= score_threshold
mask = tf.reduce_any(mask, axis=-1)
class_boxes = tf.boolean_mask(boxes, mask)
class_boxes = tf.reshape(class_boxes, (tf.shape(boxes)[0], -1, 4))
class_box_scores = tf.boolean_mask(box_scores, mask)
class_box_scores = tf.reshape(class_box_scores, (tf.shape(boxes)[0], -1, num_classes))
class_boxes, class_box_scores = tf.py_function(func=batched_nms,
inp=[class_boxes, class_box_scores, num_classes, iou_threshold],
Tout=[tf.float32, tf.float32])
classes = tf.argmax(class_box_scores, axis=-1)
return class_boxes, class_box_scores, classes
示例4: _get_refined_encodings_for_postitive_class
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def _get_refined_encodings_for_postitive_class(
self, refined_box_encodings, flat_cls_targets_with_background,
batch_size):
# We only predict refined location encodings for the non background
# classes, but we now pad it to make it compatible with the class
# predictions
refined_box_encodings_with_background = tf.pad(refined_box_encodings,
[[0, 0], [1, 0], [0, 0]])
refined_box_encodings_masked_by_class_targets = (
box_list_ops.boolean_mask(
box_list.BoxList(
tf.reshape(refined_box_encodings_with_background,
[-1, self._box_coder.code_size])),
tf.reshape(tf.greater(flat_cls_targets_with_background, 0), [-1]),
use_static_shapes=self._use_static_shapes,
indicator_sum=batch_size * self.max_num_proposals
if self._use_static_shapes else None).get())
return tf.reshape(
refined_box_encodings_masked_by_class_targets, [
batch_size, self.max_num_proposals,
self._box_coder.code_size
])
示例5: filter_out_of_bound_boxes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def filter_out_of_bound_boxes(boxes, feature_shape, stride):
"""
过滤图像边框外的anchor
:param boxes: [n,y1,x1,y2,x2]
:param feature_shape: 特征图的长宽 [h,w]
:param stride: 网络步长
:return:
"""
# 图像原始长宽为特征图长宽*步长
h, w = feature_shape[0], feature_shape[1]
h = tf.cast(h * stride, tf.float32)
w = tf.cast(w * stride, tf.float32)
valid_boxes_tag = tf.logical_and(tf.logical_and(tf.logical_and(boxes[:, 0] >= 0,
boxes[:, 1] >= 0),
boxes[:, 2] <= h),
boxes[:, 3] <= w)
boxes = tf.boolean_mask(boxes, valid_boxes_tag)
valid_boxes_indices = tf.where(valid_boxes_tag)[:, 0]
return boxes, valid_boxes_indices
示例6: reflection
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def reflection(data, decision):
"""Conditionally reflects the data in XYZ.
Args:
data: input tensor, shape: [..], z, y, x, c
decision: boolean tensor, shape 3, indicating on which spatial dimensions
to apply the reflection (x, y, z)
Returns:
TF op to conditionally apply reflection.
"""
with tf.name_scope('augment_reflection'):
rank = data.get_shape().ndims
spatial_dims = tf.constant([rank - 2, rank - 3, rank - 4])
selected_dims = tf.boolean_mask(spatial_dims, decision)
return tf.reverse(data, selected_dims)
示例7: bboxes_filter_overlap
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def bboxes_filter_overlap(labels, bboxes,
threshold=0.5, assign_negative=False,
scope=None):
"""Filter out bounding boxes based on (relative )overlap with reference
box [0, 0, 1, 1]. Remove completely bounding boxes, or assign negative
labels to the one outside (useful for latter processing...).
Return:
labels, bboxes: Filtered (or newly assigned) elements.
"""
with tf.name_scope(scope, 'bboxes_filter', [labels, bboxes]):
scores = bboxes_intersection(tf.constant([0, 0, 1, 1], bboxes.dtype),
bboxes)
mask = scores > threshold
mask.set_shape([None])
if assign_negative:
labels = tf.where(mask, labels, -labels)
# bboxes = tf.where(mask, bboxes, bboxes)
else:
labels = tf.boolean_mask(labels, mask)
bboxes = tf.boolean_mask(bboxes, mask)
return labels, bboxes
示例8: _last_relevant
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def _last_relevant(outputs, sequence_length):
"""Deprecated"""
batch_size = tf.shape(outputs)[0]
max_length = outputs.get_shape()[1]
output_size = outputs.get_shape()[2]
index = tf.range(0, batch_size) * max_length + (sequence_length - 1)
flat = tf.reshape(outputs, [-1, output_size])
last_timesteps = tf.gather(flat, index) # very slow
# mask = tf.sign(index)
# last_timesteps = tf.boolean_mask(flat, mask)
# # Creating a vector of 0s and 1s that will specify what timesteps to choose.
# partitions = tf.reduce_sum(tf.one_hot(index, tf.shape(flat)[0], dtype='int32'), 0)
# # Selecting the elements we want to choose.
# _, last_timesteps = tf.dynamic_partition(flat, partitions, 2) # (batch_size, n_dim)
# https://stackoverflow.com/questions/35892412/tensorflow-dense-gradient-explanation
return last_timesteps
示例9: bboxes_filter_overlap
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def bboxes_filter_overlap(labels, bboxes,xs, ys, threshold, scope=None, assign_negative = False):
"""Filter out bounding boxes based on (relative )overlap with reference
box [0, 0, 1, 1]. Remove completely bounding boxes, or assign negative
labels to the one outside (useful for latter processing...).
Return:
labels, bboxes: Filtered (or newly assigned) elements.
"""
with tf.name_scope(scope, 'bboxes_filter', [labels, bboxes]):
scores = bboxes_intersection(tf.constant([0, 0, 1, 1], bboxes.dtype),bboxes)
mask = scores > threshold
if assign_negative:
labels = tf.where(mask, labels, -labels)
else:
labels = tf.boolean_mask(labels, mask)
bboxes = tf.boolean_mask(bboxes, mask)
scores = bboxes_intersection(tf.constant([0, 0, 1, 1], bboxes.dtype),bboxes)
xs = tf.boolean_mask(xs, mask);
ys = tf.boolean_mask(ys, mask);
return labels, bboxes, xs, ys
示例10: repeat
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def repeat(x, repeats):
"""
Repeats elements of a Tensor (equivalent to np.repeat, but only for 1D
tensors).
:param x: rank 1 Tensor;
:param repeats: rank 1 Tensor with same shape as x, the number of
repetitions for each element;
:return: rank 1 Tensor, of shape `(sum(repeats), )`.
"""
x = tf.expand_dims(x, 1)
max_repeats = tf.reduce_max(repeats)
tile_repeats = [1, max_repeats]
arr_tiled = tf.tile(x, tile_repeats)
mask = tf.less(tf.range(max_repeats), tf.expand_dims(repeats, 1))
result = tf.reshape(tf.boolean_mask(arr_tiled, mask), [-1])
return result
示例11: corrupt_single_relationship
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def corrupt_single_relationship(triple: tf.Tensor,
all_triples: tf.Tensor,
max_range: int,
name=None):
""" Corrupt the relationship by __sampling from [0, max_range]
:param triple:
:param all_triples:
:param max_range:
:param name:
:return: corrupted 1-d [h,r,t] triple
"""
with tf.name_scope(name, 'corrupt_single_relation', [triple, all_triples]):
h, r, t = tf.unstack(triple, name='unstack_triple')
head_mask = tf.equal(all_triples[:, 0], h, name='head_mask')
head_matched_triples = tf.boolean_mask(all_triples[:, 1:], head_mask, name='head_matched_triples')
tail_mask = tf.equal(head_matched_triples[:, 1], t, name='tail_mask')
true_rels = tf.boolean_mask(head_matched_triples[:, 0], tail_mask)
corrupted_rel = tf.reshape(single_negative_sampling(true_rels, max_range), ())
return tf.stack([h, corrupted_rel, t], name='rel_corrupted_triple')
示例12: _common
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def _common(cls, node, **kwargs):
attrs = copy.deepcopy(node.attrs)
tensor_dict = kwargs["tensor_dict"]
x = tensor_dict[node.inputs[0]]
condition = tensor_dict[node.inputs[1]]
x = tf.reshape(x, [-1]) if node.attrs.get("axis") is None else x
if condition.shape.is_fully_defined():
condition_shape = condition.shape[0]
indices = tf.constant(list(range(condition_shape)), dtype=tf.int64)
else:
condition_shape = tf.shape(condition, out_type=tf.int64)[0]
indices = tf.range(condition_shape, dtype=tf.int64)
not_zero = tf.not_equal(condition, tf.zeros_like(condition))
attrs['indices'] = tf.boolean_mask(indices, not_zero)
return [
cls.make_tensor_from_onnx_node(node, inputs=[x], attrs=attrs, **kwargs)
]
示例13: add_loss_op
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def add_loss_op(self):
"""Defines the loss"""
if self.config.use_crf:
log_likelihood, trans_params = tf.contrib.crf.crf_log_likelihood(
self.logits, self.labels, self.sequence_lengths)
self.trans_params = trans_params # need to evaluate it for decoding
self.loss = tf.reduce_mean(-log_likelihood)
else:
losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=self.logits, labels=self.labels)
mask = tf.sequence_mask(self.sequence_lengths)
losses = tf.boolean_mask(losses, mask)
self.loss = tf.reduce_mean(losses)
# for tensorboard
tf.summary.scalar("loss", self.loss)
示例14: flatten_binary_scores
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def flatten_binary_scores(scores, labels, ignore=None):
"""
Flattens predictions in the batch (binary case)
Remove labels equal to 'ignore'
"""
scores = tf.reshape(scores, (-1,))
labels = tf.reshape(labels, (-1,))
if ignore is None:
return scores, labels
valid = tf.not_equal(labels, ignore)
vscores = tf.boolean_mask(scores, valid, name='valid_scores')
vlabels = tf.boolean_mask(labels, valid, name='valid_labels')
return vscores, vlabels
# --------------------------- MULTICLASS LOSSES ---------------------------
示例15: flatten_probas
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import boolean_mask [as 别名]
def flatten_probas(probas, labels, ignore=None, order='BHWC'):
"""
Flattens predictions in the batch
"""
if len(probas.shape) == 3:
probas, order = tf.expand_dims(probas, 3), 'BHWC'
if order == 'BCHW':
probas = tf.transpose(probas, (0, 2, 3, 1), name="BCHW_to_BHWC")
order = 'BHWC'
if order != 'BHWC':
raise NotImplementedError('Order {} unknown'.format(order))
C = probas.shape[3]
probas = tf.reshape(probas, (-1, C))
labels = tf.reshape(labels, (-1,))
if ignore is None:
return probas, labels
valid = tf.not_equal(labels, ignore)
vprobas = tf.boolean_mask(probas, valid, name='valid_probas')
vlabels = tf.boolean_mask(labels, valid, name='valid_labels')
return vprobas, vlabels