def row_index(shape):
"""
Generate an X index for the given tensor.
.. code-block:: python
[
[ 0, 1, 2, ... width-1 ],
[ 0, 1, 2, ... width-1 ],
... (x height)
]
:param list[int] shape:
:return: Tensor
"""
height = shape[0]
width = shape[1]
row_identity = tf.cumsum(tf.ones([width], dtype=tf.int32), exclusive=True)
row_identity = tf.reshape(tf.tile(row_identity, [height]), [height, width])
return row_identity
python类cumsum()的实例源码
def segment_indices(segment_ids, name=None):
"""Returns a `Tensor` of indices within each segment.
segment_ids should be a sequence of non-decreasing non-negative integers that
define a set of segments, e.g. [0, 0, 1, 2, 2, 2] defines 3 segments of length
2, 1 and 3. The return value is a `Tensor` containing the indices within each
segment.
Example input: [0, 0, 1, 2, 2, 2]
Example output: [0, 1, 0, 0, 1, 2]
Args:
segment_ids: A 1-d `Tensor` containing an non-decreasing sequence of
non-negative integers with type `tf.int32` or `tf.int64`.
name: (Optional) A name for this operation.
Returns:
A `Tensor` containing the indices within each segment.
"""
with tf.name_scope(name, 'segment_indices'):
segment_lengths = tf.segment_sum(tf.ones_like(segment_ids), segment_ids)
segment_starts = tf.gather(tf.concat([[0], tf.cumsum(segment_lengths)], 0),
segment_ids)
return (tf.range(tf.size(segment_ids, out_type=segment_ids.dtype)) -
segment_starts)
def _precision_recall(n_gbboxes, n_detections, scores, tp, fp, scope=None):
"""Compute precision and recall from scores, true positives and false
positives booleans arrays
"""
# Sort by score.
with tf.name_scope(scope, 'prec_rec', [n_gbboxes, scores, tp, fp]):
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=n_detections, sorted=True)
tp = tf.gather(tp, idxes)
fp = tf.gather(fp, idxes)
# Computer recall and precision.
dtype = tf.float64
tp = tf.cumsum(tf.cast(tp, dtype), axis=0)
fp = tf.cumsum(tf.cast(fp, dtype), axis=0)
recall = _safe_div(tp, tf.cast(n_gbboxes, dtype), 'recall')
precision = _safe_div(tp, tp + fp, 'precision')
return tf.tuple([precision, recall])
def _precision_recall(n_gbboxes, n_detections, scores, tp, fp, scope=None):
"""Compute precision and recall from scores, true positives and false
positives booleans arrays
"""
# Sort by score.
with tf.name_scope(scope, 'prec_rec', [n_gbboxes, scores, tp, fp]):
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=n_detections, sorted=True)
tp = tf.gather(tp, idxes)
fp = tf.gather(fp, idxes)
# Computer recall and precision.
dtype = tf.float64
tp = tf.cumsum(tf.cast(tp, dtype), axis=0)
fp = tf.cumsum(tf.cast(fp, dtype), axis=0)
recall = _safe_div(tp, tf.cast(n_gbboxes, dtype), 'recall')
precision = _safe_div(tp, tp + fp, 'precision')
return tf.tuple([precision, recall])
def _precision_recall(n_gbboxes, n_detections, scores, tp, fp, scope=None):
"""Compute precision and recall from scores, true positives and false
positives booleans arrays
"""
# Sort by score.
with tf.name_scope(scope, 'prec_rec', [n_gbboxes, scores, tp, fp]):
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=n_detections, sorted=True)
tp = tf.gather(tp, idxes)
fp = tf.gather(fp, idxes)
# Computer recall and precision.
dtype = tf.float64
tp = tf.cumsum(tf.cast(tp, dtype), axis=0)
fp = tf.cumsum(tf.cast(fp, dtype), axis=0)
recall = _safe_div(tp, tf.cast(n_gbboxes, dtype), 'recall')
precision = _safe_div(tp, tp + fp, 'precision')
return tf.tuple([precision, recall])
def preturn_network(rewards, discounts, values):
# First reward must be zero, first discount must be one
first_reward = tf.Assert(
tf.reduce_all(tf.equal(rewards[:, 0, :], 0.0)), [rewards[:, 0, :]])
first_discount = tf.Assert(
tf.reduce_all(tf.equal(discounts[:, 0, :], 1.0)), [discounts[:, 0, :]])
with tf.control_dependencies([first_reward, first_discount]):
with tf.variable_scope('preturn'):
accum_value_discounts = tf.cumprod(discounts, axis=1, exclusive=False)
accum_reward_discounts = tf.cumprod(discounts, axis=1, exclusive=True)
discounted_values = values * accum_value_discounts
discounted_rewards = rewards * accum_reward_discounts
cumulative_rewards = tf.cumsum(discounted_rewards, axis=1)
preturns = cumulative_rewards + discounted_values
util.activation_summary(preturns)
return preturns
def preturn_network(rewards, discounts, values):
# First reward must be zero, first discount must be one
first_reward = tf.Assert(
tf.reduce_all(tf.equal(rewards[:, 0, :], 0.0)), [rewards[:, 0, :]])
first_discount = tf.Assert(
tf.reduce_all(tf.equal(discounts[:, 0, :], 1.0)), [discounts[:, 0, :]])
with tf.control_dependencies([first_reward, first_discount]):
with tf.variable_scope('preturn'):
accum_value_discounts = tf.cumprod(discounts, axis=1, exclusive=False)
accum_reward_discounts = tf.cumprod(discounts, axis=1, exclusive=True)
discounted_values = values * accum_value_discounts
discounted_rewards = rewards * accum_reward_discounts
cumulative_rewards = tf.cumsum(discounted_rewards, axis=1)
preturns = cumulative_rewards + discounted_values
util.activation_summary(preturns)
return preturns
def _precision_recall(n_gbboxes, n_detections, scores, tp, fp, scope=None):
"""Compute precision and recall from scores, true positives and false
positives booleans arrays
"""
# Sort by score.
with tf.name_scope(scope, 'prec_rec', [n_gbboxes, scores, tp, fp]):
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=n_detections, sorted=True)
tp = tf.gather(tp, idxes)
fp = tf.gather(fp, idxes)
# Computer recall and precision.
dtype = tf.float64
tp = tf.cumsum(tf.cast(tp, dtype), axis=0)
fp = tf.cumsum(tf.cast(fp, dtype), axis=0)
recall = _safe_div(tp, tf.cast(n_gbboxes, dtype), 'recall')
precision = _safe_div(tp, tp + fp, 'precision')
return tf.tuple([precision, recall])
def get_S_loss_hao(mean_x, logcov_x, qv_alpha, qv_beta, qeta_mu, qeta_sigma, epsilon = 1e-8):
sigma_px = 1.0
S1 = tf.digamma(qv_alpha) - tf.digamma(qv_alpha + qv_beta)
S2 = tf.cumsum(tf.digamma(qv_beta) - tf.digamma(qv_alpha + qv_beta))
mean_x_expand = tf.expand_dims(mean_x, 1)
logcov_x_expand = tf.expand_dims(logcov_x, 1)
qeta_mu_expand = tf.expand_dims(tf.transpose(qeta_mu), 0)
qeta_sigma_expand = tf.expand_dims(tf.transpose(qeta_sigma), 0)
S3 = 0.5 * tf.reduce_sum(1 + logcov_x_expand - 2 * tf.log(sigma_px) \
- (tf.exp(logcov_x_expand) + tf.square(qeta_sigma_expand) \
+ tf.square(mean_x_expand - qeta_mu_expand)) / tf.square(sigma_px), 2)
S = S3 + tf.concat(0, [S1, [0.0]]) + tf.concat(0, [[0.0], S2])
# get the variational distribution q(z)
S_max = tf.reduce_max(S, reduction_indices=1)
S_whiten = S - tf.expand_dims(S_max, 1)
qz = tf.exp(S_whiten) / tf.expand_dims(tf.reduce_sum(tf.exp(S_whiten), 1), 1)
# Summarize the S loss
# S_loss = -tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S), 1)))
S_loss = -tf.reduce_sum(S_max) - tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S - tf.expand_dims(S_max, 1)), 1) + epsilon))
return S_loss, qz, S
def get_S_loss_hao(mean_x, logcov_x, qv_alpha, qv_beta, qeta_mu, qeta_sigma, sigma_px, epsilon = 1e-8):
S1 = tf.digamma(qv_alpha) - tf.digamma(qv_alpha + qv_beta)
S2 = tf.cumsum(tf.digamma(qv_beta) - tf.digamma(qv_alpha + qv_beta))
mean_x_expand = tf.expand_dims(mean_x, 1)
logcov_x_expand = tf.expand_dims(logcov_x, 1)
qeta_mu_expand = tf.expand_dims(tf.transpose(qeta_mu), 0)
qeta_sigma_expand = tf.expand_dims(tf.transpose(qeta_sigma), 0)
sigma_px_expand = tf.expand_dims(tf.transpose(sigma_px), 0)
S3 = 0.5 * tf.reduce_sum(1 + logcov_x_expand - 2 * tf.log(sigma_px_expand) \
- (tf.exp(logcov_x_expand) + tf.square(qeta_sigma_expand) \
+ tf.square(mean_x_expand - qeta_mu_expand)) / tf.square(sigma_px_expand), 2)
S = S3 + tf.concat(0, [S1, [0.0]]) + tf.concat(0, [[0.0], S2])
# get the variational distribution q(z)
S_max = tf.reduce_max(S, reduction_indices=1)
S_whiten = S - tf.expand_dims(S_max, 1)
qz = tf.exp(S_whiten) / tf.expand_dims(tf.reduce_sum(tf.exp(S_whiten), 1), 1)
# Summarize the S loss
# S_loss = -tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S), 1)))
S_loss = -tf.reduce_sum(S_max) - tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S - tf.expand_dims(S_max, 1)), 1) + epsilon))
return S_loss, qz, S
def get_S_loss_hao(mean_x, logcov_x, qv_alpha, qv_beta, qeta_mu, qeta_sigma, epsilon = 1e-8):
sigma_px = 1.0
S1 = tf.digamma(qv_alpha) - tf.digamma(qv_alpha + qv_beta)
S2 = tf.cumsum(tf.digamma(qv_beta) - tf.digamma(qv_alpha + qv_beta))
mean_x_expand = tf.expand_dims(mean_x, 1)
logcov_x_expand = tf.expand_dims(logcov_x, 1)
qeta_mu_expand = tf.expand_dims(tf.transpose(qeta_mu), 0)
qeta_sigma_expand = tf.expand_dims(tf.transpose(qeta_sigma), 0)
S3 = 0.5 * tf.reduce_sum(1 + logcov_x_expand - 2 * tf.log(sigma_px) \
- (tf.exp(logcov_x_expand) + tf.square(qeta_sigma_expand) \
+ tf.square(mean_x_expand - qeta_mu_expand)) / tf.square(sigma_px), 2)
S = S3 + tf.concat(0, [S1, [0.0]]) + tf.concat(0, [[0.0], S2])
# get the variational distribution q(z)
S_max = tf.reduce_max(S, reduction_indices=1)
S_whiten = S - tf.expand_dims(S_max, 1)
qz = tf.exp(S_whiten) / tf.expand_dims(tf.reduce_sum(tf.exp(S_whiten), 1), 1)
# Summarize the S loss
# S_loss = -tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S), 1)))
S_loss = -tf.reduce_sum(S_max) - tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S - tf.expand_dims(S_max, 1)), 1) + epsilon))
return S_loss, qz, S
def get_S_loss_hao(mean_x, logcov_x, qv_alpha, qv_beta, qeta_mu, qeta_sigma, sigma_px, epsilon = 1e-8):
S1 = tf.digamma(qv_alpha) - tf.digamma(qv_alpha + qv_beta)
S2 = tf.cumsum(tf.digamma(qv_beta) - tf.digamma(qv_alpha + qv_beta))
mean_x_expand = tf.expand_dims(mean_x, 1)
logcov_x_expand = tf.expand_dims(logcov_x, 1)
qeta_mu_expand = tf.expand_dims(tf.transpose(qeta_mu), 0)
qeta_sigma_expand = tf.expand_dims(tf.transpose(qeta_sigma), 0)
sigma_px_expand = tf.expand_dims(tf.transpose(sigma_px), 0)
S3 = 0.5 * tf.reduce_sum(1 + logcov_x_expand - 2 * tf.log(sigma_px_expand) \
- (tf.exp(logcov_x_expand) + tf.square(qeta_sigma_expand) \
+ tf.square(mean_x_expand - qeta_mu_expand)) / tf.square(sigma_px_expand), 2)
S = S3 + tf.concat(0, [S1, [0.0]]) + tf.concat(0, [[0.0], S2])
# get the variational distribution q(z)
S_max = tf.reduce_max(S, reduction_indices=1)
S_whiten = S - tf.expand_dims(S_max, 1)
qz = tf.exp(S_whiten) / tf.expand_dims(tf.reduce_sum(tf.exp(S_whiten), 1), 1)
# Summarize the S loss
# S_loss = -tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S), 1)))
S_loss = -tf.reduce_sum(S_max) - tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S - tf.expand_dims(S_max, 1)), 1) + epsilon))
return S_loss, qz, S
def get_S_loss(alpha, beta, mean_x, logcov_x, mean_eta, logcov_eta, sigma2, epsilon=1e-8):
mean_x_pad = tf.expand_dims(mean_x, 1)
logcov_x_pad = tf.expand_dims(logcov_x, 1)
mean_eta_pad = tf.expand_dims(mean_eta, 0)
logcov_eta_pad = tf.expand_dims(logcov_eta, 0)
S1 = tf.digamma(alpha) - tf.digamma(alpha + beta)
S2 = tf.cumsum(tf.digamma(beta) - tf.digamma(alpha + beta))
S = 0.5 * tf.reduce_sum( \
1 + logcov_x_pad - math.log(sigma2) \
- (tf.exp(logcov_x_pad) + tf.exp(logcov_eta_pad) + tf.square(mean_x_pad - mean_eta_pad)) / sigma2 , 2 \
) \
+ tf.concat(0, [S1, tf.constant([0.0])]) + tf.concat(0, [tf.constant([0.0]), S2])
assignments = tf.argmax(S, dimension=1)
S_max = tf.reduce_max(S, reduction_indices=1)
S_loss = -tf.reduce_sum(S_max) - tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S - tf.expand_dims(S_max, 1)), reduction_indices = 1) + epsilon))
return assignments, S_loss
def get_words_from_chars(characters_list: List[str], sequence_lengths: List[int], name='chars_conversion'):
with tf.name_scope(name=name):
def join_charcaters_fn(coords):
return tf.reduce_join(characters_list[coords[0]:coords[1]])
def coords_several_sequences():
end_coords = tf.cumsum(sequence_lengths)
start_coords = tf.concat([[0], end_coords[:-1]], axis=0)
coords = tf.stack([start_coords, end_coords], axis=1)
coords = tf.cast(coords, dtype=tf.int32)
return tf.map_fn(join_charcaters_fn, coords, dtype=tf.string)
def coords_single_sequence():
return tf.reduce_join(characters_list, keep_dims=True)
words = tf.cond(tf.shape(sequence_lengths)[0] > 1,
true_fn=lambda: coords_several_sequences(),
false_fn=lambda: coords_single_sequence())
return words
def get_weights(sequence, eos_id, include_first_eos=True):
cumsum = tf.cumsum(tf.to_float(tf.not_equal(sequence, eos_id)), axis=1)
range_ = tf.range(start=1, limit=tf.shape(sequence)[1] + 1)
range_ = tf.tile(tf.expand_dims(range_, axis=0), [tf.shape(sequence)[0], 1])
weights = tf.to_float(tf.equal(cumsum, tf.to_float(range_)))
if include_first_eos:
weights = weights[:,:-1]
shape = [tf.shape(weights)[0], 1]
weights = tf.concat([tf.ones(tf.stack(shape)), weights], axis=1)
return tf.stop_gradient(weights)
def get_weights(sequence, eos_id, include_first_eos=True):
cumsum = tf.cumsum(tf.to_float(tf.not_equal(sequence, eos_id)), axis=1)
range_ = tf.range(start=1, limit=tf.shape(sequence)[1] + 1)
range_ = tf.tile(tf.expand_dims(range_, axis=0), [tf.shape(sequence)[0], 1])
weights = tf.to_float(tf.equal(cumsum, tf.to_float(range_)))
if include_first_eos:
weights = weights[:,:-1]
shape = [tf.shape(weights)[0], 1]
weights = tf.concat([tf.ones(tf.stack(shape)), weights], axis=1)
return tf.stop_gradient(weights)
def column_index(shape):
"""
Generate a Y index for the given tensor.
.. code-block:: python
[
[ 0, 0, 0, ... ],
[ 1, 1, 1, ... ],
[ n, n, n, ... ],
...
[ height-1, height-1, height-1, ... ]
]
:param list[int] shape:
:return: Tensor
"""
height = shape[0]
width = shape[1]
column_identity = tf.ones([width], dtype=tf.int32)
column_identity = tf.tile(column_identity, [height])
column_identity = tf.reshape(column_identity, [height, width])
column_identity = tf.cumsum(column_identity, exclusive=True)
return column_identity
def precision_recall(num_gbboxes, num_detections, tp, fp, scores,
dtype=tf.float64, scope=None):
"""Compute precision and recall from scores, true positives and false
positives booleans arrays
"""
# Input dictionaries: dict outputs as streaming metrics.
if isinstance(scores, dict):
d_precision = {}
d_recall = {}
for c in num_gbboxes.keys():
scope = 'precision_recall_%s' % c
p, r = precision_recall(num_gbboxes[c], num_detections[c],
tp[c], fp[c], scores[c],
dtype, scope)
d_precision[c] = p
d_recall[c] = r
return d_precision, d_recall
# Sort by score.
with tf.name_scope(scope, 'precision_recall',
[num_gbboxes, num_detections, tp, fp, scores]):
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=num_detections, sorted=True)
tp = tf.gather(tp, idxes)
fp = tf.gather(fp, idxes)
# Computer recall and precision.
tp = tf.cumsum(tf.cast(tp, dtype), axis=0)
fp = tf.cumsum(tf.cast(fp, dtype), axis=0)
recall = _safe_div(tp, tf.cast(num_gbboxes, dtype), 'recall')
precision = _safe_div(tp, tp + fp, 'precision')
return tf.tuple([precision, recall])
def certainty(self):
certainty = self.seg_prediction * tf.log(self.seg_prediction)
certainty = -tf.reduce_sum(certainty,reduction_indices=2)
s1 = tf.ones(tf.shape(certainty))
csum = tf.cumsum(s1,axis=1)
mask = tf.less_equal(csum,tf.cast(tf.tile(tf.expand_dims(self._length,1),[1,tf.shape(certainty)[1]]),tf.float32))
mask = tf.select(mask, tf.ones(tf.shape(certainty)),
tf.zeros(tf.shape(certainty)))
certainty *= mask
certainty = tf.reduce_sum(certainty, reduction_indices=1)
return certainty
def precision_recall(num_gbboxes, num_detections, tp, fp, scores,
dtype=tf.float64, scope=None):
"""Compute precision and recall from scores, true positives and false
positives booleans arrays
"""
# Input dictionaries: dict outputs as streaming metrics.
if isinstance(scores, dict):
d_precision = {}
d_recall = {}
for c in num_gbboxes.keys():
scope = 'precision_recall_%s' % c
p, r = precision_recall(num_gbboxes[c], num_detections[c],
tp[c], fp[c], scores[c],
dtype, scope)
d_precision[c] = p
d_recall[c] = r
return d_precision, d_recall
# Sort by score.
with tf.name_scope(scope, 'precision_recall',
[num_gbboxes, num_detections, tp, fp, scores]):
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=num_detections, sorted=True)
tp = tf.gather(tp, idxes)
fp = tf.gather(fp, idxes)
# Computer recall and precision.
tp = tf.cumsum(tf.cast(tp, dtype), axis=0)
fp = tf.cumsum(tf.cast(fp, dtype), axis=0)
recall = _safe_div(tp, tf.cast(num_gbboxes, dtype), 'recall')
precision = _safe_div(tp, tp + fp, 'precision')
return tf.tuple([precision, recall])
VisualAttentionSingle.py 文件源码
项目:cs234_final_project
作者: nipunagarwala
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def __init__(self, features_shape, num_classes, seq_len, cell_type='lstm', reuse=False, add_bn=False,
add_reg=False, scope="VA"):
self.config = VisualAttentionConfig()
self.config.features_shape = features_shape
self.config.num_classes = num_classes
self.reuse = reuse
self.config.seq_len = seq_len
self.inputs_placeholder = tf.placeholder(tf.float32, shape=tuple((None,)+ self.config.features_shape ))
self.targets_placeholder = tf.placeholder(tf.float32, shape=tuple((None,) + self.config.targets_shape))
self.seq_len_placeholder = tf.placeholder(tf.int32, shape=tuple((None,) ))
self.emission_num_layers = 1
self.loss_type = 'negative_l1_dist'
self.cumsum = False
self.scope = scope
if add_bn:
self.norm_fn = tf.contrib.layers.batch_norm
else:
self.norm_fn = None
if add_reg:
self.reg_fn = tf.nn.l2_loss
else:
self.reg_fn = None
if cell_type == 'rnn':
self.cell = tf.contrib.rnn.RNNCell
elif cell_type == 'gru':
self.cell = tf.contrib.rnn.GRUCell
elif cell_type == 'lstm':
self.cell = tf.contrib.rnn.LSTMCell
else:
raise ValueError('Input correct cell type')
def __init__(self, features_shape, num_classes, cell_type='lstm', seq_len=8, reuse=False, add_bn=False,
add_reg=False, deeper = False, loss_type = 'negative_l1_dist', cum_sum=False,
init_loc_size=(4,), num_objects=1, scope='RCNN'):
self.config = MOTRecurrentCNNConfig()
self.config.features_shape = features_shape
self.config.num_classes = num_classes
self.config.init_loc_size = init_loc_size
self.config.seq_len = seq_len
self.reuse = reuse
self.deeper = deeper
self.loss_type = loss_type
self.cumsum = cum_sum
self.scope = scope
self.num_objects = num_objects
self.inputs_placeholder = tf.placeholder(tf.float32, shape=tuple((None,None,)+ self.config.features_shape ))
self.init_loc = tf.placeholder(tf.float32, shape=tuple((None,self.num_objects)+ self.config.init_loc_size))
self.targets_placeholder = tf.placeholder(tf.float32, shape=tuple((None,self.num_objects, None) + self.config.targets_shape))
self.seq_len_placeholder = tf.placeholder(tf.int32, shape=tuple((None,) ))
if add_bn:
self.norm_fn = tf.contrib.layers.batch_norm
else:
self.norm_fn = None
if add_reg:
self.reg_fn = tf.nn.l2_loss
else:
self.reg_fn = None
if cell_type == 'rnn':
self.cell = tf.contrib.rnn.RNNCell
elif cell_type == 'gru':
self.cell = tf.contrib.rnn.GRUCell
elif cell_type == 'lstm':
self.cell = tf.contrib.rnn.LSTMCell
else:
raise ValueError('Input correct cell type')
def __init__(self, features_shape, num_classes, seq_len, cell_type='lstm', reuse=False, add_bn=False,
add_reg=False, scope="VA"):
self.config = VisualAttentionConfig()
self.config.features_shape = features_shape
self.config.num_classes = num_classes
self.reuse = reuse
self.inputs_placeholder = tf.placeholder(tf.float32, shape=tuple((None,None,)+ self.config.features_shape ))
self.init_loc = tf.placeholder(tf.float32, shape=tuple((None,)+ self.config.init_loc_size))
self.targets_placeholder = tf.placeholder(tf.float32, shape=tuple((None,None,) + self.config.targets_shape))
self.config.seq_len = seq_len
self.seq_len_placeholder = tf.placeholder(tf.int32, shape=tuple((None,) ))
self.emission_num_layers = 1
self.loss_type = 'negative_l1_dist'
self.cumsum = False
self.scope = scope
if add_bn:
self.norm_fn = tf.contrib.layers.batch_norm
else:
self.norm_fn = None
if add_reg:
self.reg_fn = tf.nn.l2_loss
else:
self.reg_fn = None
if cell_type == 'rnn':
self.cell = tf.contrib.rnn.RNNCell
elif cell_type == 'gru':
self.cell = tf.contrib.rnn.GRUCell
elif cell_type == 'lstm':
self.cell = tf.contrib.rnn.LSTMCell
else:
raise ValueError('Input correct cell type')
def __init__(self, features_shape, num_classes, cell_type='lstm', seq_len=8, reuse=False, add_bn=False,
add_reg=False, deeper = False, loss_type = 'negative_l1_dist', cum_sum=False, scope='RCNN'):
self.config = RecurrentCNNConfig()
self.config.features_shape = features_shape
self.config.num_classes = num_classes
self.reuse = reuse
self.inputs_placeholder = tf.placeholder(tf.float32, shape=tuple((None,None,)+ self.config.features_shape ))
self.init_loc = tf.placeholder(tf.float32, shape=tuple((None,)+ self.config.init_loc_size))
self.targets_placeholder = tf.placeholder(tf.float32, shape=tuple((None,None,) + self.config.targets_shape))
self.config.seq_len = seq_len
self.seq_len_placeholder = tf.placeholder(tf.int32, shape=tuple((None,) ))
self.deeper = deeper
self.loss_type = loss_type
self.cumsum = cum_sum
self.scope = scope
if add_bn:
self.norm_fn = tf.contrib.layers.batch_norm
else:
self.norm_fn = None
if add_reg:
self.reg_fn = tf.nn.l2_loss
else:
self.reg_fn = None
if cell_type == 'rnn':
self.cell = tf.contrib.rnn.RNNCell
elif cell_type == 'gru':
self.cell = tf.contrib.rnn.GRUCell
elif cell_type == 'lstm':
self.cell = tf.contrib.rnn.LSTMCell
else:
raise ValueError('Input correct cell type')
def get_mc_target(rewards_t, discount):
discounts = discount ** tf.cast(tf.range(tf.shape(rewards_t)[0]), dtype=tf.float32)
epsilon = 1e-7
return tf.cumsum(rewards_t * discounts, reverse=True) / (discounts + epsilon)
def tf_percentile(images):
min = tf.reduce_min(tf.log(1.0 + images))
max = tf.reduce_max(tf.log(1.0 + images))
histogram = tf.histogram_fixed_width(tf.reshape(images, [-1]), [min, max])
values = tf.linspace(min, max, 100)
csum = tf.cumsum(histogram)
csum_float = tf.cast(csum, tf.float32) / tf.cast(tf.size(csum), tf.float32)
argmin_index = tf.cast(tf.argmin((csum_float - 0.95) ** 2.0, axis = 0), tf.int32)
return tf.exp(values[argmin_index]) - 1.0
def precision_recall(num_gbboxes, num_detections, tp, fp, scores,
dtype=tf.float64, scope=None):
"""Compute precision and recall from scores, true positives and false
positives booleans arrays
"""
# Input dictionaries: dict outputs as streaming metrics.
if isinstance(scores, dict):
d_precision = {}
d_recall = {}
for c in num_gbboxes.keys():
scope = 'precision_recall_%s' % c
p, r = precision_recall(num_gbboxes[c], num_detections[c],
tp[c], fp[c], scores[c],
dtype, scope)
d_precision[c] = p
d_recall[c] = r
return d_precision, d_recall
# Sort by score.
with tf.name_scope(scope, 'precision_recall',
[num_gbboxes, num_detections, tp, fp, scores]):
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=num_detections, sorted=True)
tp = tf.gather(tp, idxes)
fp = tf.gather(fp, idxes)
# Computer recall and precision.
tp = tf.cumsum(tf.cast(tp, dtype), axis=0)
fp = tf.cumsum(tf.cast(fp, dtype), axis=0)
recall = _safe_div(tp, tf.cast(num_gbboxes, dtype), 'recall')
precision = _safe_div(tp, tp + fp, 'precision')
return tf.tuple([precision, recall])
def nonseq2seq(tensor, seq_length, length, name=None):
'''
Convert non sequential data to sequential data
Args:
tensor: non sequential data, which is a TxF tensor where T is the sum of
all sequence lengths
seq_length: a vector containing the sequence lengths
length: the constant length of the output sequences
name: [optional] the name of the operation
Returns:
sequential data, wich is a list containing an N x F
tensor for each time step where N is the batch size and F is the
input dimension
'''
with tf.name_scope(name or'nonseq2seq'):
#get the cumulated sequence lengths to specify the positions in tensor
cum_seq_length = tf.concat(0, [tf.constant([0]), tf.cumsum(seq_length)])
#get the indices in the tensor for each sequence
indices = [tf.range(cum_seq_length[l], cum_seq_length[l+1])
for l in range(int(seq_length.get_shape()[0]))]
#create the non-padded sequences
sequences = [tf.gather(tensor, i) for i in indices]
#pad the sequences with zeros
sequences = [tf.pad(sequences[s], [[0, length-seq_length[s]], [0, 0]])
for s in range(len(sequences))]
#specify that the sequences have been padded to the constant length
for seq in sequences:
seq.set_shape([length, int(tensor.get_shape()[1])])
#convert the list for eqch sequence to a list for eqch time step
tensorlist = tf.unpack(tf.pack(sequences), axis=1)
return tensorlist
def cumsum(x, axis=0):
"""Cumulative sum of the values in a tensor, alongside the specified axis.
# Arguments
x: A tensor or variable.
axis: An integer, the axis to compute the sum.
# Returns
A tensor of the cumulative sum of values of `x` along `axis`.
"""
axis = _normalize_axis(axis, ndim(x))
return tf.cumsum(x, axis=axis)
def weights_prepend_inputs_to_targets(labels):
"""Assign weight 1.0 to only the "targets" portion of the labels.
Weight 1.0 is assigned to all nonzero labels past the first zero.
See prepend_mode in common_hparams.py
Args:
labels: A Tensor of int32s.
Returns:
A Tensor of floats.
"""
past_first_zero = tf.cumsum(tf.to_float(tf.equal(labels, 0)), axis=1)
nonzero = tf.to_float(labels)
return tf.to_float(tf.not_equal(past_first_zero * nonzero, 0))