def body(self, time, inp, state, finished, output_ta):
"""Body of the dynamic decoding phase."""
# invoke the decoder step.
output, next_inp, next_state, decoder_finished = self._decoder.step(time, inp, state)
# check the termination status and filter the output.
next_finished = tf.logical_or(finished, decoder_finished)
next_finished = tf.logical_or(next_finished, self._helper.finished(time, output))
output = self.output(output, finished)
output_ta = output_ta.write(time, output)
ntime = tf.add(time, 1)
return ntime, next_inp, next_state, next_finished, output_ta
# pylint: disable=W0613,I0011,R0913
# disable unused arguments (needed for the loop).
python类logical_or()的实例源码
def Uniform(name=None):
X = tf.placeholder(config.dtype, name=name)
Distribution.logp = tf.fill(tf.shape(X), config.dtype(0))
def integral(lower, upper):
return tf.cond(
tf.logical_or(
tf.is_inf(tf.cast(lower, config.dtype)),
tf.is_inf(tf.cast(upper, config.dtype))
),
lambda: tf.constant(1, dtype=config.dtype),
lambda: tf.cast(upper, config.dtype) - tf.cast(lower, config.dtype),
)
Distribution.integral = integral
return X
def UniformInt(name=None):
X = tf.placeholder(config.int_dtype, name=name)
Distribution.logp = tf.fill(tf.shape(X), config.dtype(0))
def integral(lower, upper):
val = tf.cond(
tf.logical_or(
tf.is_inf(tf.ceil(tf.cast(lower, config.dtype))),
tf.is_inf(tf.floor(tf.cast(upper, config.dtype)))
),
lambda: tf.constant(1, dtype=config.dtype),
lambda: tf.cast(upper, config.dtype) - tf.cast(lower, config.dtype),
)
return val
Distribution.integral = integral
return X
def tf_explore(self, episode, timestep, num_actions):
def true_fn():
# Know if first is not true second must be true from outer cond check.
return tf.cond(
pred=(timestep < self.start_timestep),
true_fn=(lambda: self.initial_epsilon),
false_fn=(lambda: self.final_epsilon)
)
def false_fn():
completed_ratio = (tf.cast(x=timestep, dtype=util.tf_dtype('float')) - self.start_timestep) / self.timesteps
return self.initial_epsilon + completed_ratio * (self.final_epsilon - self.initial_epsilon)
pred = tf.logical_or(x=(timestep < self.start_timestep), y=(timestep > self.start_timestep + self.timesteps))
return tf.cond(pred=pred, true_fn=true_fn, false_fn=false_fn)
def tf_explore(self, episode=0, timestep=0, num_actions=1):
def true_fn():
# Know if first is not true second must be true from outer cond check.
return tf.cond(
pred=(timestep < self.start_timestep),
true_fn=(lambda: self.initial_epsilon),
false_fn=(lambda: self.final_epsilon)
)
def false_fn():
half_life_ratio = (tf.cast(x=timestep, dtype=util.tf_dtype('float')) - self.start_timestep) / self.half_life
epsilon = self.final_epsilon + (2 ** (-half_life_ratio)) * (self.initial_epsilon - self.final_epsilon)
return epsilon
pred = tf.logical_or(x=(timestep < self.start_timestep), y=(timestep > self.start_timestep + self.timesteps))
return tf.cond(pred=pred, true_fn=true_fn, false_fn=false_fn)
def __or__(self, other):
return tf.logical_or(self, other)
def __ror__(self, other):
return tf.logical_or(other, self)
def logical_impl(x, y): # pylint: disable=I0011,C0103
"""Returns the truth value of x -> y element-wise.
*NOTE*: `logical_impl` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
Arguments:
x: a `Tensor` of type `bool`.
y: a `Tensor` of type `bool`.
Returns:
a `Tensor` of type bool.
"""
return tf.logical_or(tf.logical_not(x), y)
def finished(self, time, output):
"""Check which sentences are finished.
Arguments:
time: a `Tensor` of rank `0D` (i.e. a scalar) with the 0-based value of the
current step in the loop.
output: a `Tensor` of rank `2D` and shape `[batch_size, num_classes]` representing
the current output of the model, i.e. abatch of probability distribution estimations
over the output classes.
Returns:
a `Tensor` of shape `[batch_size]` of `tf.bool` elements, indicating for each
position if the corresponding sequence has terminated or not. A sequence is
has terminated if the current step is greater or equal the number of steps allowed
(defined in the `lengths` input argument) and if the `argmax` over the output
probability distribution ends up in the class that has id equal to the `EOS` symbol
(if provided).
"""
length = time + 1
finished = tf.greater_equal(length, self._lengths)
if finished.get_shape().ndims == 0:
batch = [utils.get_dimension(output, 0)]
finished = tf.tile([finished], batch)
if self._EOS is not None:
ids = tf.cast(tf.argmax(output, axis=-1), tf.int32)
eos = tf.equal(ids, self._EOS)
finished = tf.logical_or(finished, eos)
return finished
def cond(self, time, inp, state, finished, output_ta):
"""Logical contidion for termination."""
continuation = tf.logical_not(tf.reduce_all(finished))
if self._pad_to is None:
return continuation
padding = time < self._pad_to
return tf.logical_or(continuation, padding)
# pylint: disable=W0221,I0011
# disable the changed signature of the method.
def _lower_bound_grad(op, grad):
"""Gradient for `_lower_bound`.
Args:
op: the tensorflow op for which to calculate a gradient
grad: gradient with respect to the output of the op
Returns:
gradients with respect to the inputs of the op
"""
inputs = op.inputs[0]
bound = op.inputs[1]
pass_through_if = tf.logical_or(inputs >= bound, grad < 0)
return [tf.cast(pass_through_if, grad.dtype) * grad, None]
def setUp(self):
super(LogicalBinaryOpsTest, self).setUp()
self.ops = [
('logical_and', operator.and_, tf.logical_and, core.logical_and),
('logical_or', operator.or_, tf.logical_or, core.logical_or),
('logical_xor', operator.xor, tf.logical_xor, core.logical_xor),
]
self.test_lt_1 = self.original_lt < 10
self.test_lt_2 = self.original_lt < 5
self.test_lt_1_broadcast = self.test_lt_1.tensor
self.test_lt_2_broadcast = self.test_lt_2.tensor
self.broadcast_axes = self.test_lt_1.axes
def set_logp_to_neg_inf(X, logp, bounds):
"""Set `logp` to negative infinity when `X` is outside the allowed bounds.
# Arguments
X: tensorflow.Tensor
The variable to apply the bounds to
logp: tensorflow.Tensor
The log probability corrosponding to `X`
bounds: list of `Region` objects
The regions corrosponding to allowed regions of `X`
# Returns
logp: tensorflow.Tensor
The newly bounded log probability
"""
conditions = []
for l, u in bounds:
lower_is_neg_inf = not isinstance(l, tf.Tensor) and np.isneginf(l)
upper_is_pos_inf = not isinstance(u, tf.Tensor) and np.isposinf(u)
if not lower_is_neg_inf and upper_is_pos_inf:
conditions.append(tf.greater(X, l))
elif lower_is_neg_inf and not upper_is_pos_inf:
conditions.append(tf.less(X, u))
elif not (lower_is_neg_inf or upper_is_pos_inf):
conditions.append(tf.logical_and(tf.greater(X, l), tf.less(X, u)))
if len(conditions) > 0:
is_inside_bounds = conditions[0]
for condition in conditions[1:]:
is_inside_bounds = tf.logical_or(is_inside_bounds, condition)
logp = tf.select(
is_inside_bounds,
logp,
tf.fill(tf.shape(X), config.dtype(-np.inf))
)
return logp
def accuracy(self):
a = tf.equal(self.hard,self.targetPlaceholder)
for decoder in self.decoders:
if decoder.token != STOP:
vector = decoder.accuracyVector()
if vector != True:
a = tf.logical_and(a,
tf.logical_or(vector, tf.not_equal(self.hard,decoder.token)))
return tf.reduce_mean(tf.cast(a, tf.float32))
def evaluationTok_k(k=3):
prediction =tf.nn.in_top_k(model,tf.argmax(Y,1),k=k)
accuracy =tf.reduce_mean(tf.cast(prediction,'float32'))
is_top1 =tf.equal(tf.nn.top_k(model,k)[1][:,0],tf.cast(tf.argmax(Y,1),'int32'))
is_top2 = tf.equal(tf.nn.top_k(model, k)[1][:, 1], tf.cast(tf.argmax(Y, 1), 'int32'))
is_top3 = tf.equal(tf.nn.top_k(model, k)[1][:, 2], tf.cast(tf.argmax(Y, 1), 'int32'))
is_in_top1 =is_top1
is_in_top2 = tf.logical_or(is_in_top1, is_top2)
is_in_top3 = tf.logical_or(is_in_top2, is_top3)
accuracy11 = tf.reduce_mean(tf.cast(is_in_top1, "float32"))
accuracy22 = tf.reduce_mean(tf.cast(is_in_top2, "float32"))
accuracy33 = tf.reduce_mean(tf.cast(is_in_top3, "float32"))
return accuracy
def rnn_segment(features, targets, mode, params):
seq_feature = features['seq_feature']
seq_length = features['seq_length']
with tf.variable_scope("emb"):
embeddings = tf.get_variable("char_emb", shape=[params['num_char'], params['emb_size']])
seq_emb = tf.nn.embedding_lookup(embeddings, seq_feature)
batch_size = tf.shape(seq_feature)[0]
time_step = tf.shape(seq_feature)[1]
flat_seq_emb = tf.reshape(seq_emb, shape=[batch_size, time_step, (params['k'] + 1) * params['emb_size']])
cell = rnn.LSTMCell(params['rnn_units'])
if mode == ModeKeys.TRAIN:
cell = rnn.DropoutWrapper(cell, params['input_keep_prob'], params['output_keep_prob'])
projection_cell = rnn.OutputProjectionWrapper(cell, params['num_class'])
logits, _ = tf.nn.dynamic_rnn(projection_cell, flat_seq_emb, sequence_length=seq_length, dtype=tf.float32)
weight_mask = tf.to_float(tf.sequence_mask(seq_length))
loss = seq2seq.sequence_loss(logits, targets, weights=weight_mask)
train_op = layers.optimize_loss(
loss=loss,
global_step=tf.contrib.framework.get_global_step(),
learning_rate=params["learning_rate"],
optimizer=tf.train.AdamOptimizer,
clip_gradients=params['grad_clip'],
summaries=[
"learning_rate",
"loss",
"gradients",
"gradient_norm",
])
pred_classes = tf.to_int32(tf.argmax(input=logits, axis=2))
pred_words = tf.logical_or(tf.equal(pred_classes, 0), tf.equal(pred_classes, 3))
target_words = tf.logical_or(tf.equal(targets, 0), tf.equal(targets, 3))
precision = metrics.streaming_precision(pred_words, target_words, weights=weight_mask)
recall = metrics.streaming_recall(pred_words, target_words, weights=weight_mask)
predictions = {
"classes": pred_classes
}
eval_metric_ops = {
"precision": precision,
"recall": recall
}
return learn.ModelFnOps(mode, predictions, loss, train_op, eval_metric_ops=eval_metric_ops)
def _deepfoolx(model, x, epochs, eta, clip_min, clip_max, min_prob):
y0 = tf.stop_gradient(model(x))
y0 = tf.reshape(y0, [-1])
k0 = tf.argmax(y0)
ydim = y0.get_shape().as_list()[0]
xdim = x.get_shape().as_list()[1:]
xflat = _prod(xdim)
def _cond(i, z):
xadv = tf.clip_by_value(x + z*(1+eta), clip_min, clip_max)
y = tf.reshape(model(xadv), [-1])
p = tf.reduce_max(y)
k = tf.argmax(y)
return tf.logical_and(tf.less(i, epochs),
tf.logical_or(tf.equal(k0, k),
tf.less(p, min_prob)))
def _body(i, z):
xadv = tf.clip_by_value(x + z*(1+eta), clip_min, clip_max)
y = tf.reshape(model(xadv), [-1])
gs = [tf.reshape(tf.gradients(y[i], xadv)[0], [-1])
for i in range(ydim)]
g = tf.stack(gs, axis=0)
yk, yo = y[k0], tf.concat((y[:k0], y[(k0+1):]), axis=0)
gk, go = g[k0], tf.concat((g[:k0], g[(k0+1):]), axis=0)
yo.set_shape(ydim - 1)
go.set_shape([ydim - 1, xflat])
a = tf.abs(yo - yk)
b = go - gk
c = tf.norm(b, axis=1)
score = a / c
ind = tf.argmin(score)
si, bi = score[ind], b[ind]
dx = si * bi
dx = tf.reshape(dx, [-1] + xdim)
return i+1, z+dx
_, noise = tf.while_loop(_cond, _body, [0, tf.zeros_like(x)],
name='_deepfoolx_impl', back_prop=False)
return noise
def _jsma_impl(model, x, yind, epochs, eps, clip_min, clip_max, score_fn):
def _cond(i, xadv):
return tf.less(i, epochs)
def _body(i, xadv):
ybar = model(xadv)
dy_dx = tf.gradients(ybar, xadv)[0]
# gradients of target w.r.t input
yt = tf.gather_nd(ybar, yind)
dt_dx = tf.gradients(yt, xadv)[0]
# gradients of non-targets w.r.t input
do_dx = dy_dx - dt_dx
c0 = tf.logical_or(eps < 0, xadv < clip_max)
c1 = tf.logical_or(eps > 0, xadv > clip_min)
cond = tf.reduce_all([dt_dx >= 0, do_dx <= 0, c0, c1], axis=0)
cond = tf.to_float(cond)
# saliency score for each pixel
score = cond * score_fn(dt_dx, do_dx)
shape = score.get_shape().as_list()
dim = _prod(shape[1:])
score = tf.reshape(score, [-1, dim])
# find the pixel with the highest saliency score
ind = tf.argmax(score, axis=1)
dx = tf.one_hot(ind, dim, on_value=eps, off_value=0.0)
dx = tf.reshape(dx, [-1] + shape[1:])
xadv = tf.stop_gradient(xadv + dx)
xadv = tf.clip_by_value(xadv, clip_min, clip_max)
return i+1, xadv
_, xadv = tf.while_loop(_cond, _body, (0, tf.identity(x)),
back_prop=False, name='_jsma_batch')
return xadv
def _jsma2_impl(model, x, yind, epochs, eps, clip_min, clip_max, score_fn):
def _cond(k, xadv):
return tf.less(k, epochs)
def _body(k, xadv):
ybar = model(xadv)
dy_dx = tf.gradients(ybar, xadv)[0]
# gradients of target w.r.t input
yt = tf.gather_nd(ybar, yind)
dt_dx = tf.gradients(yt, xadv)[0]
# gradients of non-targets w.r.t input
do_dx = dy_dx - dt_dx
c0 = tf.logical_or(eps < 0, xadv < clip_max)
c1 = tf.logical_or(eps > 0, xadv > clip_min)
cond = tf.reduce_all([dt_dx >= 0, do_dx <= 0, c0, c1], axis=0)
cond = tf.to_float(cond)
# saliency score for each pixel
score = cond * score_fn(dt_dx, do_dx)
shape = score.get_shape().as_list()
dim = _prod(shape[1:])
score = tf.reshape(score, [-1, dim])
a = tf.expand_dims(score, axis=1)
b = tf.expand_dims(score, axis=2)
score2 = tf.reshape(a + b, [-1, dim*dim])
ij = tf.argmax(score2, axis=1)
i = tf.to_int32(ij / dim)
j = tf.to_int32(ij) % dim
dxi = tf.one_hot(i, dim, on_value=eps, off_value=0.0)
dxj = tf.one_hot(j, dim, on_value=eps, off_value=0.0)
dx = tf.reshape(dxi + dxj, [-1] + shape[1:])
xadv = tf.stop_gradient(xadv + dx)
xadv = tf.clip_by_value(xadv, clip_min, clip_max)
return k+1, xadv
_, xadv = tf.while_loop(_cond, _body, (0, tf.identity(x)),
back_prop=False, name='_jsma2_batch')
return xadv
def __compute_AP(self,c_scores,c_tp,c_fp,c_num_gbboxes):
aps_voc07 = {}
aps_voc12 = {}
for c in c_scores.keys():
num_gbboxes = c_num_gbboxes[c]
tp = c_tp[c]
fp = c_fp[c]
scores = c_scores[c]
#reshape data
num_gbboxes = math_ops.to_int64(num_gbboxes)
scores = math_ops.to_float(scores)
stype = tf.bool
tp = tf.cast(tp, stype)
fp = tf.cast(fp, stype)
# Reshape TP and FP tensors and clean away 0 class values.(difficult bboxes)
scores = tf.reshape(scores, [-1])
tp = tf.reshape(tp, [-1])
fp = tf.reshape(fp, [-1])
# Remove TP and FP both false.
mask = tf.logical_or(tp, fp)
rm_threshold = 1e-4
mask = tf.logical_and(mask, tf.greater(scores, rm_threshold))
scores = tf.boolean_mask(scores, mask)
tp = tf.boolean_mask(tp, mask)
fp = tf.boolean_mask(fp, mask)
num_gbboxes = tf.reduce_sum(num_gbboxes)
num_detections = tf.size(scores, out_type=tf.int32)
# Precison and recall values.
prec, rec = tfe.precision_recall(num_gbboxes, num_detections, tp, fp, scores)
v = tfe.average_precision_voc07(prec, rec)
aps_voc07[c] = v
# Average precision VOC12.
v = tfe.average_precision_voc12(prec, rec)
aps_voc12[c] = v
return aps_voc07, aps_voc12
def decode_image(contents, channels=None, name=None):
"""Convenience function for `decode_gif`, `decode_jpeg`, and `decode_png`.
Detects whether an image is a GIF, JPEG, or PNG, and performs the appropriate
operation to convert the input bytes `string` into a `Tensor` of type `uint8`.
Note: `decode_gif` returns a 4-D array `[num_frames, height, width, 3]`, as
opposed to `decode_jpeg` and `decode_png`, which return 3-D arrays
`[height, width, num_channels]`. Make sure to take this into account when
constructing your graph if you are intermixing GIF files with JPEG and/or PNG
files.
Args:
contents: 0-D `string`. The encoded image bytes.
channels: An optional `int`. Defaults to `0`. Number of color channels for
the decoded image.
name: A name for the operation (optional)
Returns:
`Tensor` with type `uint8` with shape `[height, width, num_channels]` for
JPEG and PNG images and shape `[num_frames, height, width, 3]` for GIF
images.
"""
with ops.name_scope(name, 'decode_image') as scope:
if channels not in (None, 0, 1, 3):
raise ValueError('channels must be in (None, 0, 1, 3)')
substr = tf.substr(contents, 0, 4)
def _gif():
# Create assert op to check that bytes are GIF decodable
is_gif = tf.equal(substr, b'\x47\x49\x46\x38', name='is_gif')
decode_msg = 'Unable to decode bytes as JPEG, PNG, or GIF'
assert_decode = control_flow_ops.Assert(is_gif, [decode_msg])
# Create assert to make sure that channels is not set to 1
# Already checked above that channels is in (None, 0, 1, 3)
gif_channels = 0 if channels is None else channels
good_channels = tf.not_equal(gif_channels, 1, name='check_channels')
channels_msg = 'Channels must be in (None, 0, 3) when decoding GIF images'
assert_channels = control_flow_ops.Assert(good_channels, [channels_msg])
with ops.control_dependencies([assert_decode, assert_channels]):
return gen_image_ops.decode_gif(contents)
def _png():
return gen_image_ops.decode_png(contents, channels)
def check_png():
is_png = tf.equal(substr, b'\211PNG', name='is_png')
return control_flow_ops.cond(is_png, _png, _gif, name='cond_png')
def _jpeg():
return gen_image_ops.decode_jpeg(contents, channels)
is_jpeg = tf.logical_or(tf.equal(substr, b'\xff\xd8\xff\xe0', name='is_jpeg0'),
tf.equal(substr, b'\xff\xd8\xff\xe1', name='is_jpeg0'))
return control_flow_ops.cond(is_jpeg, _jpeg, check_png, name='cond_jpeg')
def threshold_by_percent_max(t, threshold, use_active_set=False):
"""Creates tensorflow ops to perform a thresholding of a tensor by a
percentage of the maximum value. To be used when thresholding gradients.
Optionally maintains an active set.
Parameters
----------
t: tensor
The tensor to threshold by percent max.
threshold: float
A number between 0 and 1 that specifies the threshold.
use_active_set: bool
Specifies whether or not to use an active set.
Returns
-------
A tensor of the same shape as t that has had all values under the threshold
set to 0.
"""
with tf.name_scope("threshold_by_percent_max"):
# t = tf.convert_to_tensor(t, name="t")
# shape = tf.shape(t)
abs_t = tf.abs(t)
thresh_percentile = tf.constant(threshold, dtype=tf.float32)
zeros = tf.zeros(shape=tf.shape(t), dtype=tf.float32)
maximum = tf.reduce_max(abs_t, reduction_indices=[0])
# A tensor, the same shape as t, that has the threshold values to be
# compared against every value.
thresh_one_voxel = tf.expand_dims(tf.mul(thresh_percentile,
maximum), 0)
thresh_tensor = tf.tile(thresh_one_voxel,
tf.pack([tf.shape(t)[0], 1]))
above_thresh_values = tf.greater_equal(abs_t, thresh_tensor)
if use_active_set:
active_set = tf.Variable(tf.equal(tf.ones(tf.shape(t)),
tf.zeros(tf.shape(t))),
name="active_set", dtype=tf.bool)
active_set_inc = tf.assign(active_set,
tf.logical_or(active_set,
above_thresh_values),
name="incremented_active_set")
active_set_size = tf.reduce_sum(tf.cast(active_set, tf.float32),
name="size_of_active_set")
return (tf.select(active_set_inc, t, zeros), active_set_size)
else:
return tf.select(above_thresh_values, t, zeros)
def __process_rois(self, regions, class_scores):
""" get relevant regions, with non-maximum suppression and clipping """
region_shape = tf.shape(regions)
width = region_shape[2] * self.feat_stride
height = region_shape[1] * self.feat_stride
anchors = self.get_tiled_anchors_for_shape(width, height)
region_boxes = self.adjust_bbox(tf.reshape(regions,
[-1, 4]), anchors)
class_scores = tf.reshape(class_scores, [-1, 2])
if self.is_training:
# ignore boxes that cross the image boundary
self.outside_anchors = tf.logical_and(tf.logical_and(anchors[:, 0] - anchors[:, 2] / 2.0 >= -self.feat_stride, anchors[:, 1] - anchors[:, 3] / 2.0 >= -self.feat_stride), tf.logical_and(
anchors[:, 0] + anchors[:, 2] / 2.0 < tf.cast(height, tf.float32) + self.feat_stride, anchors[:, 1] + anchors[:, 3] / 2.0 < tf.cast(width, tf.float32) + self.feat_stride))
region_boxes = tf.boolean_mask(region_boxes, self.outside_anchors)
class_scores = tf.boolean_mask(class_scores, self.outside_anchors)
shape = tf.shape(region_boxes)
mask = tf.logical_or(region_boxes[:, 2] >
self.min_box_size,
region_boxes[:,
3] > self.min_box_size)
region_box = tf.boolean_mask(region_boxes, mask)
class_score = tf.boolean_mask(class_scores, mask)
region_box = self.clip_bboxes(region_box, width, height)
class_score = tf.nn.softmax(class_score)
class_score = tf.slice(class_score, [0, 1], [-1, 1])
class_score = tf.reshape(class_score, [-1])
_, idx = tf.nn.top_k(class_score, k=tf.minimum(self.num_proposals, tf.shape(class_score)[0]))
class_score = tf.gather(class_score, idx)
region_box = tf.gather(region_box, idx)
with tf.variable_scope('non_maximum_supression'):
bboxes2 = self.bboxes_to_yxyx(
region_box, tf.cast(height, tf.float32))
idx = tf.image.non_max_suppression(bboxes2, class_score,
self.num_proposals_after_nms,
0.7)
region_box = tf.gather(region_box, idx)
return region_box
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)
balanced_positive_negative_sampler.py 文件源码
项目:tensorflow
作者: luyishisi
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def subsample(self, indicator, batch_size, labels):
"""Returns subsampled minibatch.
Args:
indicator: boolean tensor of shape [N] whose True entries can be sampled.
batch_size: desired batch size.
labels: boolean tensor of shape [N] denoting positive(=True) and negative
(=False) examples.
Returns:
is_sampled: boolean tensor of shape [N], True for entries which are
sampled.
Raises:
ValueError: if labels and indicator are not 1D boolean tensors.
"""
if len(indicator.get_shape().as_list()) != 1:
raise ValueError('indicator must be 1 dimensional, got a tensor of '
'shape %s' % indicator.get_shape())
if len(labels.get_shape().as_list()) != 1:
raise ValueError('labels must be 1 dimensional, got a tensor of '
'shape %s' % labels.get_shape())
if labels.dtype != tf.bool:
raise ValueError('labels should be of type bool. Received: %s' %
labels.dtype)
if indicator.dtype != tf.bool:
raise ValueError('indicator should be of type bool. Received: %s' %
indicator.dtype)
# Only sample from indicated samples
negative_idx = tf.logical_not(labels)
positive_idx = tf.logical_and(labels, indicator)
negative_idx = tf.logical_and(negative_idx, indicator)
# Sample positive and negative samples separately
max_num_pos = int(self._positive_fraction * batch_size)
sampled_pos_idx = self.subsample_indicator(positive_idx, max_num_pos)
max_num_neg = batch_size - tf.reduce_sum(tf.cast(sampled_pos_idx, tf.int32))
sampled_neg_idx = self.subsample_indicator(negative_idx, max_num_neg)
sampled_idx = tf.logical_or(sampled_pos_idx, sampled_neg_idx)
return sampled_idx
def retain_boxes_above_threshold(
boxes, labels, label_scores, masks=None, keypoints=None, threshold=0.0):
"""Retains boxes whose label score is above a given threshold.
If the label score for a box is missing (represented by NaN), the box is
retained. The boxes that don't pass the threshold will not appear in the
returned tensor.
Args:
boxes: float32 tensor of shape [num_instance, 4] representing boxes
location in normalized coordinates.
labels: rank 1 int32 tensor of shape [num_instance] containing the object
classes.
label_scores: float32 tensor of shape [num_instance] representing the
score for each box.
masks: (optional) rank 3 float32 tensor with shape
[num_instances, height, width] containing instance masks. The masks are of
the same height, width as the input `image`.
keypoints: (optional) rank 3 float32 tensor with shape
[num_instances, num_keypoints, 2]. The keypoints are in y-x normalized
coordinates.
threshold: scalar python float.
Returns:
retained_boxes: [num_retained_instance, 4]
retianed_labels: [num_retained_instance]
retained_label_scores: [num_retained_instance]
If masks, or keypoints are not None, the function also returns:
retained_masks: [num_retained_instance, height, width]
retained_keypoints: [num_retained_instance, num_keypoints, 2]
"""
with tf.name_scope('RetainBoxesAboveThreshold',
values=[boxes, labels, label_scores]):
indices = tf.where(
tf.logical_or(label_scores > threshold, tf.is_nan(label_scores)))
indices = tf.squeeze(indices, axis=1)
retained_boxes = tf.gather(boxes, indices)
retained_labels = tf.gather(labels, indices)
retained_label_scores = tf.gather(label_scores, indices)
result = [retained_boxes, retained_labels, retained_label_scores]
if masks is not None:
retained_masks = tf.gather(masks, indices)
result.append(retained_masks)
if keypoints is not None:
retained_keypoints = tf.gather(keypoints, indices)
result.append(retained_keypoints)
return result