def chi2(exp, obs):
"""
Compute CHI^2 statistics of non-zero expected elements
"""
zero = tf.constant(0, dtype=tf.float32)
mask = tf.not_equal(exp, zero)
def masking(tensor, mask):
return tf.boolean_mask(tensor, mask)
stat = tf.reduce_sum(
tf.div(
tf.pow(
tf.subtract(masking(obs, mask), masking(exp, mask)),
2),
masking(exp, mask)),
name="chi2_statistics")
return stat
python类boolean_mask()的实例源码
grl_beam_decoder.py 文件源码
项目:Deep-Reinforcement-Learning-for-Dialogue-Generation-in-tensorflow
作者: liuyuemaicha
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def sparse_boolean_mask(tensor, mask):
"""
Creates a sparse tensor from masked elements of `tensor`
Inputs:
tensor: a 2-D tensor, [batch_size, T]
mask: a 2-D mask, [batch_size, T]
Output: a 2-D sparse tensor
"""
mask_lens = tf.reduce_sum(tf.cast(mask, tf.int32), -1, keep_dims=True)
mask_shape = tf.shape(mask)
left_shifted_mask = tf.tile(
tf.expand_dims(tf.range(mask_shape[1]), 0),
[mask_shape[0], 1]
) < mask_lens
return tf.SparseTensor(
indices=tf.where(left_shifted_mask),
values=tf.boolean_mask(tensor, mask),
shape=tf.cast(tf.pack([mask_shape[0], tf.reduce_max(mask_lens)]), tf.int64) # For 2D only
)
def add_loss_op(self, preds):
"""Adds Ops for the loss function to the computational graph.
TODO: Compute averaged cross entropy loss for the predictions.
Importantly, you must ignore the loss for any masked tokens.
Hint: You might find tf.boolean_mask useful to mask the losses on masked tokens.
Hint: You can use tf.nn.sparse_softmax_cross_entropy_with_logits to simplify your
implementation. You might find tf.reduce_mean useful.
Args:
pred: A tensor of shape (batch_size, max_length, n_classes) containing the output of the neural
network before the softmax layer.
Returns:
loss: A 0-d tensor (scalar)
"""
### YOUR CODE HERE (~2-4 lines)
loss = tf.reduce_mean(tf.boolean_mask(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = self.labels_placeholder,
logits = preds), self.mask_placeholder))
### END YOUR CODE
return loss
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
def bboxes_filter_center(labels, bboxes, margins=[0., 0., 0., 0.],
scope=None):
"""Filter out bounding boxes whose center are not in
the rectangle [0, 0, 1, 1] + margins. The margin Tensor
can be used to enforce or loosen this condition.
Return:
labels, bboxes: Filtered elements.
"""
with tf.name_scope(scope, 'bboxes_filter', [labels, bboxes]):
cy = (bboxes[:, 0] + bboxes[:, 2]) / 2.
cx = (bboxes[:, 1] + bboxes[:, 3]) / 2.
mask = tf.greater(cy, margins[0])
mask = tf.logical_and(mask, tf.greater(cx, margins[1]))
mask = tf.logical_and(mask, tf.less(cx, 1. + margins[2]))
mask = tf.logical_and(mask, tf.less(cx, 1. + margins[3]))
# Boolean masking...
labels = tf.boolean_mask(labels, mask)
bboxes = tf.boolean_mask(bboxes, mask)
return labels, bboxes
def bboxes_filter_labels(labels, bboxes,
out_labels=[], num_classes=np.inf,
scope=None):
"""Filter out labels from a collection. Typically used to get
of DontCare elements. Also remove elements based on the number of classes.
Return:
labels, bboxes: Filtered elements.
"""
with tf.name_scope(scope, 'bboxes_filter_labels', [labels, bboxes]):
mask = tf.greater_equal(labels, num_classes)
for l in labels:
mask = tf.logical_and(mask, tf.not_equal(labels, l))
labels = tf.boolean_mask(labels, mask)
bboxes = tf.boolean_mask(bboxes, mask)
return labels, bboxes
# =========================================================================== #
# Standard boxes computation.
# =========================================================================== #
def average_precision_voc07(precision, recall, name=None):
"""Compute (interpolated) average precision from precision and recall Tensors.
The implementation follows Pascal 2007 guidelines.
See also: https://sanchom.wordpress.com/tag/average-precision/
"""
with tf.name_scope(name, 'average_precision_voc07', [precision, recall]):
# Convert to float64 to decrease error on cumulated sums.
precision = tf.cast(precision, dtype=tf.float64)
recall = tf.cast(recall, dtype=tf.float64)
# Add zero-limit value to avoid any boundary problem...
precision = tf.concat([precision, [0.]], axis=0)
recall = tf.concat([recall, [np.inf]], axis=0)
# Split the integral into 10 bins.
l_aps = []
for t in np.arange(0., 1.1, 0.1):
mask = tf.greater_equal(recall, t)
v = tf.reduce_max(tf.boolean_mask(precision, mask))
l_aps.append(v / 11.)
ap = tf.add_n(l_aps)
return ap
image_reader_segment.py 文件源码
项目:dcsp_segmentation
作者: arslan-chaudhry
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def image_mirroring(img, label, seed):
"""
Randomly mirrors the images.
Args:
img: Training image to mirror.
label: Segmentation mask to mirror.
seed: Random seed.
"""
distort_left_right_random = tf.random_uniform([1], 0, 1.0, dtype=tf.float32, seed=seed)[0]
mirror = tf.less(tf.stack([1.0, distort_left_right_random, 1.0]), 0.5)
mirror = tf.boolean_mask([0, 1, 2], mirror)
img = tf.reverse(img, mirror)
label = tf.reverse(label, mirror)
return img, label
def sample_k_fids_for_pid(pid, all_fids, all_pids, batch_k):
""" Given a PID, select K FIDs of that specific PID. """
possible_fids = tf.boolean_mask(all_fids, tf.equal(all_pids, pid))
# The following simply uses a subset of K of the possible FIDs
# if more than, or exactly K are available. Otherwise, we first
# create a padded list of indices which contain a multiple of the
# original FID count such that all of them will be sampled equally likely.
count = tf.shape(possible_fids)[0]
padded_count = tf.cast(tf.ceil(batch_k / count), tf.int32) * count
full_range = tf.mod(tf.range(padded_count), count)
# Sampling is always performed by shuffling and taking the first k.
shuffled = tf.random_shuffle(full_range)
selected_fids = tf.gather(possible_fids, shuffled[:batch_k])
return selected_fids, tf.fill([batch_k], pid)
def separation_loss(tf_prediction_serial, tf_interactions_serial, **kwargs):
"""
This loss function models the explicit positive and negative interaction predictions as normal distributions and
returns the probability of overlap between the two distributions.
:param tf_prediction_serial:
:param tf_interactions_serial:
:return:
"""
tf_positive_mask = tf.greater(tf_interactions_serial, 0.0)
tf_negative_mask = tf.less_equal(tf_interactions_serial, 0.0)
tf_positive_predictions = tf.boolean_mask(tf_prediction_serial, tf_positive_mask)
tf_negative_predictions = tf.boolean_mask(tf_prediction_serial, tf_negative_mask)
tf_pos_mean, tf_pos_var = tf.nn.moments(tf_positive_predictions, axes=[0])
tf_neg_mean, tf_neg_var = tf.nn.moments(tf_negative_predictions, axes=[0])
tf_overlap_distribution = tf.contrib.distributions.Normal(loc=(tf_neg_mean - tf_pos_mean),
scale=tf.sqrt(tf_neg_var + tf_pos_var))
loss = 1.0 - tf_overlap_distribution.cdf(0.0)
return loss
def ctc_label_dense_to_sparse(labels, label_lengths, batch_size):
# The second dimension of labels must be equal to the longest label length in the batch
correct_shape_assert = tf.assert_equal(tf.shape(labels)[1], tf.reduce_max(label_lengths))
with tf.control_dependencies([correct_shape_assert]):
labels = tf.identity(labels)
label_shape = tf.shape(labels)
num_batches_tns = tf.stack([label_shape[0]])
max_num_labels_tns = tf.stack([label_shape[1]])
def range_less_than(previous_state, current_input):
return tf.expand_dims(tf.range(label_shape[1]), 0) < current_input
init = tf.cast(tf.fill(max_num_labels_tns, 0), tf.bool)
init = tf.expand_dims(init, 0)
dense_mask = tf.scan(range_less_than, label_lengths, initializer=init, parallel_iterations=1)
dense_mask = dense_mask[:, 0, :]
label_array = tf.reshape(tf.tile(tf.range(0, label_shape[1]), num_batches_tns),
label_shape)
label_ind = tf.boolean_mask(label_array, dense_mask)
batch_array = tf.transpose(tf.reshape(tf.tile(tf.range(0, label_shape[0]), max_num_labels_tns), tf.reverse(label_shape, [0])))
batch_ind = tf.boolean_mask(batch_array, dense_mask)
indices = tf.transpose(tf.reshape(tf.concat([batch_ind, label_ind], 0), [2, -1]))
shape = [batch_size, tf.reduce_max(label_lengths)]
vals_sparse = gather_nd(labels, indices, shape)
return tf.SparseTensor(tf.to_int64(indices), vals_sparse, tf.to_int64(label_shape))
# Validate and normalize transcriptions. Returns a cleaned version of the label
# or None if it's invalid.
def ctc_label_dense_to_sparse(labels, label_lengths, batch_size):
# The second dimension of labels must be equal to the longest label length in the batch
correct_shape_assert = tf.assert_equal(tf.shape(labels)[1], tf.reduce_max(label_lengths))
with tf.control_dependencies([correct_shape_assert]):
labels = tf.identity(labels)
label_shape = tf.shape(labels)
num_batches_tns = tf.stack([label_shape[0]])
max_num_labels_tns = tf.stack([label_shape[1]])
def range_less_than(previous_state, current_input):
return tf.expand_dims(tf.range(label_shape[1]), 0) < current_input
init = tf.cast(tf.fill(max_num_labels_tns, 0), tf.bool)
init = tf.expand_dims(init, 0)
dense_mask = tf.scan(range_less_than, label_lengths, initializer=init, parallel_iterations=1)
dense_mask = dense_mask[:, 0, :]
label_array = tf.reshape(tf.tile(tf.range(0, label_shape[1]), num_batches_tns),
label_shape)
label_ind = tf.boolean_mask(label_array, dense_mask)
batch_array = tf.transpose(tf.reshape(tf.tile(tf.range(0, label_shape[0]), max_num_labels_tns), tf.reverse(label_shape, [0])))
batch_ind = tf.boolean_mask(batch_array, dense_mask)
indices = tf.transpose(tf.reshape(tf.concat([batch_ind, label_ind], 0), [2, -1]))
shape = [batch_size, tf.reduce_max(label_lengths)]
vals_sparse = gather_nd(labels, indices, shape)
return tf.SparseTensor(tf.to_int64(indices), vals_sparse, tf.to_int64(label_shape))
# Validate and normalize transcriptions. Returns a cleaned version of the label
# or None if it's invalid.
def __init__(self, scope = 'policy_network', learning_rate = 0.001):
self.initializer = tf.contrib.layers.xavier_initializer()
with tf.variable_scope(scope):
self.state = tf.placeholder(tf.float32, [None, state_dim], name = 'state')
self.action = tf.placeholder(tf.int32, [None], name = 'action')
self.target = tf.placeholder(tf.float32, name = 'target')
self.action_prob = policy_nn(self.state, state_dim, action_space, self.initializer)
action_mask = tf.cast(tf.one_hot(self.action, depth = action_space), tf.bool)
self.picked_action_prob = tf.boolean_mask(self.action_prob, action_mask)
self.loss = tf.reduce_sum(-tf.log(self.picked_action_prob)*self.target) + sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES, scope=scope))
self.optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
self.train_op = self.optimizer.minimize(self.loss)
def __init__(self, learning_rate = 0.001):
self.initializer = tf.contrib.layers.xavier_initializer()
with tf.variable_scope('supervised_policy'):
self.state = tf.placeholder(tf.float32, [None, state_dim], name = 'state')
self.action = tf.placeholder(tf.int32, [None], name = 'action')
self.action_prob = policy_nn(self.state, state_dim, action_space, self.initializer)
action_mask = tf.cast(tf.one_hot(self.action, depth = action_space), tf.bool)
self.picked_action_prob = tf.boolean_mask(self.action_prob, action_mask)
self.loss = tf.reduce_sum(-tf.log(self.picked_action_prob)) + sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES, scope = 'supervised_policy'))
self.optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
self.train_op = self.optimizer.minimize(self.loss)
def _cls_mining(self, scores, status, hard_neg_ratio=3.0, scope=None):
"""
Positive classification loss and hard negative classificatin loss
ARGS
scores: [n, n_classes]
status: int [n] node or link matching status
RETURNS
pos_loss: []
n_pos: int []
hard_neg_loss: []
n_hard_neg: []
"""
with tf.variable_scope(scope or 'cls_mining'):
# positive classification loss
pos_mask = tf.equal(status, MATCH_STATUS_POS)
pos_scores = tf.boolean_mask(scores, pos_mask)
n_pos = tf.shape(pos_scores)[0]
pos_labels = tf.fill([n_pos], POS_LABEL)
pos_loss = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=pos_scores, labels=pos_labels))
# hard negative classification loss
neg_mask = tf.equal(status, MATCH_STATUS_NEG)
neg_scores = tf.boolean_mask(scores, neg_mask)
n_neg = tf.shape(neg_scores)[0]
n_hard_neg = tf.cast(n_pos, tf.float32) * hard_neg_ratio
n_hard_neg = tf.minimum(n_hard_neg, tf.cast(n_neg, tf.float32))
n_hard_neg = tf.cast(n_hard_neg, tf.int32)
neg_prob = tf.nn.softmax(neg_scores)[:, NEG_LABEL]
# find the k examples with the least negative probabilities
_, hard_neg_indices = tf.nn.top_k(-neg_prob, k=n_hard_neg)
hard_neg_scores = tf.gather(neg_scores, hard_neg_indices)
hard_neg_labels = tf.fill([n_hard_neg], NEG_LABEL)
hard_neg_loss = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=hard_neg_scores, labels=hard_neg_labels))
return pos_loss, n_pos, hard_neg_loss, n_hard_neg
def ctc_label_dense_to_sparse(labels, label_lengths):
# undocumented feature soon to be made public
from tensorflow.python.ops import functional_ops
label_shape = tf.shape(labels)
num_batches_tns = tf.pack([label_shape[0]])
max_num_labels_tns = tf.pack([label_shape[1]])
def range_less_than(previous_state, current_input):
return tf.expand_dims(tf.range(label_shape[1]), 0) < tf.fill(max_num_labels_tns, current_input)
init = tf.cast(tf.fill([1, label_shape[1]], 0), tf.bool)
dense_mask = functional_ops.scan(range_less_than, label_lengths,
initializer=init, parallel_iterations=1)
dense_mask = dense_mask[:, 0, :]
label_array = tf.reshape(tf.tile(tf.range(0, label_shape[1]), num_batches_tns),
label_shape)
label_ind = tf.boolean_mask(label_array, dense_mask)
batch_array = tf.transpose(tf.reshape(tf.tile(tf.range(0, label_shape[0]),
max_num_labels_tns), tf.reverse(label_shape, [True])))
batch_ind = tf.boolean_mask(batch_array, dense_mask)
indices = tf.transpose(tf.reshape(tf.concat(0, [batch_ind, label_ind]), [2, -1]))
vals_sparse = tf.gather_nd(labels, indices)
return tf.SparseTensor(tf.to_int64(indices), vals_sparse, tf.to_int64(label_shape))
def filter_small_gt(gt_bboxes, gt_cats, min_size):
mask = tf.logical_and(gt_bboxes[:, 2] >= min_size,
gt_bboxes[:, 3] >= min_size)
return tf.boolean_mask(gt_bboxes, mask), tf.boolean_mask(gt_cats, mask)
def nms(self, localization, confidence, tiling):
good_bboxes = decode_bboxes(localization, tiling)
not_crap_mask = tf.reduce_max(confidence[:, 1:], axis=-1) >= args.conf_thresh
good_bboxes = tf.boolean_mask(good_bboxes, not_crap_mask)
confidence = tf.boolean_mask(confidence, not_crap_mask)
self.detection_list = []
self.score_list = []
for i in range(1, self.loader.num_classes):
class_mask = tf.greater(confidence[:, i], args.conf_thresh)
class_scores = tf.boolean_mask(confidence[:, i], class_mask)
class_bboxes = tf.boolean_mask(good_bboxes, class_mask)
K = tf.minimum(tf.size(class_scores), args.top_k_nms)
_, top_k_inds = tf.nn.top_k(class_scores, K)
top_class_scores = tf.gather(class_scores, top_k_inds)
top_class_bboxes = tf.gather(class_bboxes, top_k_inds)
final_inds = tf.image.non_max_suppression(top_class_bboxes,
top_class_scores,
max_output_size=args.top_k_after_nms,
iou_threshold=args.nms_thresh)
final_class_bboxes = tf.gather(top_class_bboxes, final_inds)
final_scores = tf.gather(top_class_scores, final_inds)
self.detection_list.append(final_class_bboxes)
self.score_list.append(final_scores)
def build_detector(self):
img_size = self.config['image_size']
self.image_ph = tf.placeholder(shape=[None, None, 3],
dtype=tf.float32, name='img_ph')
self.seg_ph = tf.placeholder(shape=[None, None], dtype=tf.int32, name='seg_ph')
img = tf.image.resize_bilinear(tf.expand_dims(self.image_ph, 0),
(img_size, img_size))
self.net.create_trunk(img)
if args.detect:
self.net.create_multibox_head(self.loader.num_classes)
confidence = tf.nn.softmax(tf.squeeze(self.net.outputs['confidence']))
location = tf.squeeze(self.net.outputs['location'])
self.nms(location, confidence, self.bboxer.tiling)
if args.segment:
self.net.create_segmentation_head(self.loader.num_classes)
self.segmentation = self.net.outputs['segmentation']
seg_shape = tf.shape(self.image_ph)[:2]
self.segmentation = tf.image.resize_bilinear(self.segmentation, seg_shape)
self.segmentation = tf.cast(tf.argmax(tf.squeeze(self.segmentation), axis=-1), tf.int32)
self.segmentation = tf.reshape(self.segmentation, seg_shape)
self.segmentation.set_shape([None, None])
if not self.no_gt:
easy_mask = self.seg_ph <= self.loader.num_classes
predictions = tf.boolean_mask(self.segmentation, easy_mask)
labels = tf.boolean_mask(self.seg_ph, easy_mask)
self.mean_iou, self.iou_update = mean_iou(predictions, labels, self.loader.num_classes)
else:
self.mean_iou = tf.constant(0)
self.iou_update = tf.constant(0)
def roc_auc_score(y_pred, y_true):
""" ROC AUC Score.
Approximates the Area Under Curve score, using approximation based on
the Wilcoxon-Mann-Whitney U statistic.
Yan, L., Dodier, R., Mozer, M. C., & Wolniewicz, R. (2003).
Optimizing Classifier Performance via an Approximation to the Wilcoxon-Mann-Whitney Statistic.
Measures overall performance for a full range of threshold levels.
Arguments:
y_pred: `Tensor`. Predicted values.
y_true: `Tensor` . Targets (labels), a probability distribution.
"""
with tf.name_scope("RocAucScore"):
pos = tf.boolean_mask(y_pred, tf.cast(y_true, tf.bool))
neg = tf.boolean_mask(y_pred, ~tf.cast(y_true, tf.bool))
pos = tf.expand_dims(pos, 0)
neg = tf.expand_dims(neg, 1)
# original paper suggests performance is robust to exact parameter choice
gamma = 0.2
p = 3
difference = tf.zeros_like(pos * neg) + pos - neg - gamma
masked = tf.boolean_mask(difference, difference < 0.0)
return tf.reduce_sum(tf.pow(-masked, p))