def _anchor_target_layer(self, rpn_cls_score, name):
with tf.variable_scope(name) as scope:
rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = tf.py_func(
anchor_target_layer,
[rpn_cls_score, self._gt_boxes, self._im_info, self._feat_stride, self._anchors, self._num_anchors],
[tf.float32, tf.float32, tf.float32, tf.float32],
name="anchor_target")
rpn_labels.set_shape([1, 1, None, None])
rpn_bbox_targets.set_shape([1, None, None, self._num_anchors * 4])
rpn_bbox_inside_weights.set_shape([1, None, None, self._num_anchors * 4])
rpn_bbox_outside_weights.set_shape([1, None, None, self._num_anchors * 4])
rpn_labels = tf.to_int32(rpn_labels, name="to_int32")
self._anchor_targets['rpn_labels'] = rpn_labels
self._anchor_targets['rpn_bbox_targets'] = rpn_bbox_targets
self._anchor_targets['rpn_bbox_inside_weights'] = rpn_bbox_inside_weights
self._anchor_targets['rpn_bbox_outside_weights'] = rpn_bbox_outside_weights
self._score_summaries.update(self._anchor_targets)
return rpn_labels
python类py_func()的实例源码
def _proposal_target_layer(self, rois, roi_scores, name):
with tf.variable_scope(name) as scope:
rois, roi_scores, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights = tf.py_func(
proposal_target_layer,
[rois, roi_scores, self._gt_boxes, self._num_classes],
[tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32],
name="proposal_target")
rois.set_shape([cfg.TRAIN.BATCH_SIZE, 5])
roi_scores.set_shape([cfg.TRAIN.BATCH_SIZE])
labels.set_shape([cfg.TRAIN.BATCH_SIZE, 1])
bbox_targets.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])
bbox_inside_weights.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])
bbox_outside_weights.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])
self._proposal_targets['rois'] = rois
self._proposal_targets['labels'] = tf.to_int32(labels, name="to_int32")
self._proposal_targets['bbox_targets'] = bbox_targets
self._proposal_targets['bbox_inside_weights'] = bbox_inside_weights
self._proposal_targets['bbox_outside_weights'] = bbox_outside_weights
self._score_summaries.update(self._proposal_targets)
return rois, roi_scores
def anchor_target_layer(self, input, _feat_stride, anchor_scales, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
rpn_labels,rpn_bbox_targets,rpn_bbox_inside_weights,rpn_bbox_outside_weights, rpn_orientation_targets = tf.py_func(anchor_target_layer_py,[input[0],input[1],input[2],input[3], input[4], _feat_stride, anchor_scales],[tf.float32, tf.float32,tf.float32,tf.float32,tf.float32])
rpn_labels = tf.convert_to_tensor(tf.cast(rpn_labels,tf.int32), name = 'rpn_labels')
rpn_bbox_targets = tf.convert_to_tensor(rpn_bbox_targets, name = 'rpn_bbox_targets')
rpn_bbox_inside_weights = tf.convert_to_tensor(rpn_bbox_inside_weights , name = 'rpn_bbox_inside_weights')
rpn_bbox_outside_weights = tf.convert_to_tensor(rpn_bbox_outside_weights , name = 'rpn_bbox_outside_weights')
rpn_orientation_targets = tf.convert_to_tensor(rpn_orientation_targets, name='rpn_orientations')
return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights, rpn_orientation_targets
def proposal_target_layer(self, input, classes, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
input_0a = input[0][:,0:5] # rpn_rois as original
input_0b = input[0][:,5:6] # orientation that corresponds to the rpn_rois
#print(input[3])
rois,labels,bbox_targets,bbox_inside_weights,bbox_outside_weights, gt_orientations_new, orientations_pred_new, \
orientations_targets_new = tf.py_func(proposal_target_layer_py,[input_0a,input[1],classes, input[2], input_0b],
[tf.float32,tf.float32,tf.float32,tf.float32,tf.float32, tf.float32, tf.float32, tf.float32])
rois_wo = tf.reshape(rois,[-1,6] , name = 'rois') # does the name 'rois' matter?
rois = rois_wo[:, 0:5]
labels = tf.convert_to_tensor(tf.cast(labels,tf.int32), name = 'labels')
bbox_targets = tf.convert_to_tensor(bbox_targets, name = 'bbox_targets')
bbox_inside_weights = tf.convert_to_tensor(bbox_inside_weights, name = 'bbox_inside_weights')
bbox_outside_weights = tf.convert_to_tensor(bbox_outside_weights, name = 'bbox_outside_weights')
gt_orientations_new = tf.reshape(gt_orientations_new, [-1,1], name = 'gt_orientations_new') # these are the rotation angles for the predicted boxes
orientations_pred_new = tf.reshape(orientations_pred_new, [-1,1], name = 'orientations_pred_new')
orientations_targets_new = tf.reshape(orientations_targets_new, [-1, 1], name='orientations_targets_new')
return rois_wo, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights, gt_orientations_new, orientations_pred_new, orientations_targets_new
def flows_to_img(flows):
"""Pyfunc wrapper to transorm flow vectors in color coding"""
def _flow_transform(flows):
""" Tensorflow Pyfunc to transorm flow to color coding"""
flow_imgs = []
for flow in flows:
img = computeColor.computeImg(flow)
# cv2 returns bgr images
b, g, r = cv2.split(img)
img = cv2.merge((r, g, b))
flow_imgs.append(img)
return [flow_imgs]
flow_imgs = tf.py_func(_flow_transform, [flows],
[tf.uint8], stateful=False, name='flow_transform')
flow_imgs = tf.squeeze(tf.stack(flow_imgs))
flow_imgs.set_shape([FLAGS.batchsize] + FLAGS.d_shape_img)
return flow_imgs
def anchor_target_layer(self, input, _feat_stride, anchor_scales, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
# 'rpn_cls_score', 'gt_boxes', 'gt_ishard', 'dontcare_areas', 'im_info'
rpn_labels,rpn_bbox_targets,rpn_bbox_inside_weights,rpn_bbox_outside_weights = \
tf.py_func(anchor_target_layer_py,
[input[0],input[1],input[2],input[3],input[4], _feat_stride, anchor_scales],
[tf.float32,tf.float32,tf.float32,tf.float32])
rpn_labels = tf.convert_to_tensor(tf.cast(rpn_labels,tf.int32), name = 'rpn_labels') # shape is (1 x H x W x A, 2)
rpn_bbox_targets = tf.convert_to_tensor(rpn_bbox_targets, name = 'rpn_bbox_targets') # shape is (1 x H x W x A, 4)
rpn_bbox_inside_weights = tf.convert_to_tensor(rpn_bbox_inside_weights , name = 'rpn_bbox_inside_weights') # shape is (1 x H x W x A, 4)
rpn_bbox_outside_weights = tf.convert_to_tensor(rpn_bbox_outside_weights , name = 'rpn_bbox_outside_weights') # shape is (1 x H x W x A, 4)
return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights
def proposal_target_layer(self, input, classes, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
#inputs: 'rpn_rois','gt_boxes', 'gt_ishard', 'dontcare_areas'
rois,labels,bbox_targets,bbox_inside_weights,bbox_outside_weights \
= tf.py_func(proposal_target_layer_py,
[input[0],input[1],input[2],input[3],classes],
[tf.float32,tf.float32,tf.float32,tf.float32,tf.float32])
# rois <- (1 x H x W x A, 5) e.g. [0, x1, y1, x2, y2]
# rois = tf.convert_to_tensor(rois, name='rois')
rois = tf.reshape(rois, [-1, 5], name='rois') # goes to roi_pooling
labels = tf.convert_to_tensor(tf.cast(labels,tf.int32), name = 'labels') # goes to FRCNN loss
bbox_targets = tf.convert_to_tensor(bbox_targets, name = 'bbox_targets') # goes to FRCNN loss
bbox_inside_weights = tf.convert_to_tensor(bbox_inside_weights, name = 'bbox_inside_weights')
bbox_outside_weights = tf.convert_to_tensor(bbox_outside_weights, name = 'bbox_outside_weights')
self.layers['rois'] = rois
return rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights
def _build_metric(self, model: 'code.model.abstract.Model') -> tf.Tensor:
# predownload datasets
with NLTKEnv() as nltk_env:
nltk_env.download('perluniprops')
nltk_env.download('nonbreaking_prefixes')
with tf.name_scope(None, self.metric_name,
values=[self.dataset.source,
self.dataset.target, self.dataset.length]):
predicted = model.inference_model(
self.dataset.source, self.dataset.length, reuse=True
)
bleu = tf.py_func(self._py_implementaton,
[predicted, self.dataset.target],
tf.float32,
stateful=False,
name='nltk-corpus-bleu')
return bleu
def sample_with_gt_wrapper(boxes, scores, gt_boxes, is_training=True, scope='SampleBoxesWithGT'):
with tf.name_scope(scope) as sc:
boxes, scores, batch_inds, mask_boxes, mask_scores, mask_batch_inds = \
tf.py_func(sample.sample_rpn_outputs_wrt_gt_boxes,
[boxes, scores, gt_boxes, is_training],
[tf.float32, tf.float32, tf.int32, tf.float32, tf.float32, tf.int32])
boxes = tf.convert_to_tensor(boxes, name='Boxes')
scores = tf.convert_to_tensor(scores, name='Scores')
batch_inds = tf.convert_to_tensor(batch_inds, name='BatchInds')
mask_boxes = tf.convert_to_tensor(mask_boxes, name='MaskBoxes')
mask_scores = tf.convert_to_tensor(mask_scores, name='MaskScores')
mask_batch_inds = tf.convert_to_tensor(mask_batch_inds, name='MaskBatchInds')
return boxes, scores, batch_inds, mask_boxes, mask_scores, mask_batch_inds
def assign_boxes(gt_boxes, tensors, layers, scope='AssignGTBoxes'):
with tf.name_scope(scope) as sc:
min_k = layers[0]
max_k = layers[-1]
assigned_layers = \
tf.py_func(assign.assign_boxes,
[ gt_boxes, min_k, max_k ],
tf.int32)
assigned_layers = tf.reshape(assigned_layers, [-1])
assigned_tensors = []
for t in tensors:
split_tensors = []
for l in layers:
tf.cast(l, tf.int32)
inds = tf.where(tf.equal(assigned_layers, l))
inds = tf.reshape(inds, [-1])
split_tensors.append(tf.gather(t, inds))
assigned_tensors.append(split_tensors)
return assigned_tensors + [assigned_layers]
def tf_mod(x, y, name=None):
"""Differentiable mod based in numpy
Args
x: first argument
y: second argument
Returns
mod between x and y
"""
def np_mod(x, y):
return np.mod(x, y, dtype=np.float32)
def modgrad(op, grad):
x = op.inputs[0] # the first argument (normally you need those to calculate the gradient, like the gradient of x^2 is 2x. )
y = op.inputs[1] # the second argument
return grad * 1, grad * 0 #the propagated gradient with respect to the first and second argument respectively
def py_func(func, inp, Tout, stateful=True, name=None, grad=None):
# Need to generate a unique name to avoid duplicates:
rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))
tf.RegisterGradient(rnd_name)(grad) # see _MySquareGrad for grad example
g = tf.get_default_graph()
with g.gradient_override_map({"PyFunc": rnd_name}):
return tf.py_func(func, inp, Tout, stateful=stateful, name=name)
with ops.name_scope(name, "mod", [x,y]) as name:
z = py_func(np_mod,
[x,y],
[tf.float32],
name=name,
grad=modgrad) # <-- here's the call to the gradient
return tf.reshape(z[0], tf.shape(x))
def make_input_fn_from_generator(gen):
"""Use py_func to yield elements from the given generator."""
first_ex = six.next(gen)
flattened = tf.contrib.framework.nest.flatten(first_ex)
types = [t.dtype for t in flattened]
shapes = [[None] * len(t.shape) for t in flattened]
first_ex_list = [first_ex]
def py_func():
if first_ex_list:
example = first_ex_list.pop()
else:
example = six.next(gen)
return tf.contrib.framework.nest.flatten(example)
def input_fn():
flat_example = tf.py_func(py_func, [], types)
_ = [t.set_shape(shape) for t, shape in zip(flat_example, shapes)]
example = tf.contrib.framework.nest.pack_sequence_as(first_ex, flat_example)
return example
return input_fn
def rouge_l_fscore(predictions, labels, **unused_kwargs):
"""ROUGE scores computation between labels and predictions.
This is an approximate ROUGE scoring method since we do not glue word pieces
or decode the ids and tokenize the output.
Args:
predictions: tensor, model predicitons
labels: tensor, gold output.
Returns:
rouge_l_fscore: approx rouge-l f1 score.
"""
outputs = tf.to_int32(tf.argmax(predictions, axis=-1))
# Convert the outputs and labels to a [batch_size, input_length] tensor.
outputs = tf.squeeze(outputs, axis=[-1, -2])
labels = tf.squeeze(labels, axis=[-1, -2])
rouge_l_f_score = tf.py_func(rouge_l_sentence_level, (labels, outputs),
tf.float32)
return rouge_l_f_score, tf.constant(1.0)
def rouge_2_fscore(predictions, labels, **unused_kwargs):
"""ROUGE-2 F1 score computation between labels and predictions.
This is an approximate ROUGE scoring method since we do not glue word pieces
or decode the ids and tokenize the output.
Args:
predictions: tensor, model predicitons
labels: tensor, gold output.
Returns:
rouge2_fscore: approx rouge-2 f1 score.
"""
outputs = tf.to_int32(tf.argmax(predictions, axis=-1))
# Convert the outputs and labels to a [batch_size, input_length] tensor.
outputs = tf.squeeze(outputs, axis=[-1, -2])
labels = tf.squeeze(labels, axis=[-1, -2])
rouge_2_f_score = tf.py_func(rouge_n, (labels, outputs), tf.float32)
return rouge_2_f_score, tf.constant(1.0)
def read_and_augment_data(image_list, label_list, image_size, batch_size, max_nrof_epochs,
random_crop, random_flip, random_rotate, nrof_preprocess_threads, shuffle=True):
images = ops.convert_to_tensor(image_list, dtype=tf.string)
labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
# Makes an input queue
input_queue = tf.train.slice_input_producer([images, labels],
num_epochs=max_nrof_epochs, shuffle=shuffle)
images_and_labels = []
for _ in range(nrof_preprocess_threads):
image, label = read_images_from_disk(input_queue)
if random_rotate:
image = tf.py_func(random_rotate_image, [image], tf.uint8)
if random_crop:
image = tf.random_crop(image, [image_size, image_size, 3])
else:
image = tf.image.resize_image_with_crop_or_pad(image, image_size, image_size)
if random_flip:
image = tf.image.random_flip_left_right(image)
#pylint: disable=no-member
image.set_shape((image_size, image_size, 3))
image = tf.image.per_image_standardization(image)
images_and_labels.append([image, label])
image_batch, label_batch = tf.train.batch_join(
images_and_labels, batch_size=batch_size,
capacity=4 * nrof_preprocess_threads * batch_size,
allow_smaller_final_batch=True)
return image_batch, label_batch
def gather_tree(values, parents):
"""Tensor version of gather_tree_py"""
res = tf.py_func(
func=gather_tree_py, inp=[values, parents], Tout=values.dtype)
res.set_shape(values.get_shape().as_list())
return res
def create_metric_ops(self, _inputs, labels, predictions):
"""Creates (value, update_op) tensors
"""
with tf.variable_scope(self._name):
# Join tokens into single strings
predictions_flat = tf.reduce_join(
predictions["predicted_tokens"], 1, separator=self._separator)
labels_flat = tf.reduce_join(
labels["target_tokens"], 1, separator=self._separator)
sources_value, sources_update = accumulate_strings(
values=predictions_flat, name="sources")
targets_value, targets_update = accumulate_strings(
values=labels_flat, name="targets")
metric_value = tf.py_func(
func=self._py_func,
inp=[sources_value, targets_value],
Tout=tf.float32,
name="value")
with tf.control_dependencies([sources_update, targets_update]):
update_op = tf.identity(metric_value, name="update_op")
return metric_value, update_op
def _add_gt_image_summary(self):
# use a customized visualization function to visualize the boxes
if self._gt_image is None:
self._add_gt_image()
image = tf.py_func(draw_bounding_boxes,
[self._gt_image, self._gt_boxes, self._im_info],
tf.float32, name="gt_boxes")
return tf.summary.image('GROUND_TRUTH', image)
def _proposal_top_layer(self, rpn_cls_prob, rpn_bbox_pred, name):
with tf.variable_scope(name) as scope:
rois, rpn_scores = tf.py_func(proposal_top_layer,
[rpn_cls_prob, rpn_bbox_pred, self._im_info,
self._feat_stride, self._anchors, self._num_anchors],
[tf.float32, tf.float32], name="proposal_top")
rois.set_shape([cfg.TEST.RPN_TOP_N, 5])
rpn_scores.set_shape([cfg.TEST.RPN_TOP_N, 1])
return rois, rpn_scores
def _proposal_layer(self, rpn_cls_prob, rpn_bbox_pred, name):
with tf.variable_scope(name) as scope:
rois, rpn_scores = tf.py_func(proposal_layer,
[rpn_cls_prob, rpn_bbox_pred, self._im_info, self._mode,
self._feat_stride, self._anchors, self._num_anchors],
[tf.float32, tf.float32], name="proposal")
rois.set_shape([None, 5])
rpn_scores.set_shape([None, 1])
return rois, rpn_scores
# Only use it if you have roi_pooling op written in tf.image