Example #1
0
def run_inference_for_single_image(image, graph):
  with graph.as_default():
    with tf.Session() as sess:
      # Get handles to input and output tensors
      ops = tf.get_default_graph().get_operations()
      all_tensor_names = {output.name for op in ops for output in op.outputs}
      tensor_dict = {}
      for key in [
          'num_detections', 'detection_boxes', 'detection_scores',
          'detection_classes', 'detection_masks'
      ]:
        tensor_name = key + ':0'
        if tensor_name in all_tensor_names:
          tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
              tensor_name)
      if 'detection_masks' in tensor_dict:
        # The following processing is only for single image
        detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
        detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
        # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
        real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
        detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
        detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
        detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
            detection_masks, detection_boxes, image.shape[0], image.shape[1])
        detection_masks_reframed = tf.cast(
            tf.greater(detection_masks_reframed, 0.5), tf.uint8)
        # Follow the convention by adding back the batch dimension
        tensor_dict['detection_masks'] = tf.expand_dims(
            detection_masks_reframed, 0)
      image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')

      # Run inference
      output_dict = sess.run(tensor_dict,
                             feed_dict={image_tensor: np.expand_dims(image, 0)})

      # all outputs are float32 numpy arrays, so convert types as appropriate
      output_dict['num_detections'] = int(output_dict['num_detections'][0])
      output_dict['detection_classes'] = output_dict[
          'detection_classes'][0].astype(np.uint8)
      output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
      output_dict['detection_scores'] = output_dict['detection_scores'][0]
      if 'detection_masks' in output_dict:
        output_dict['detection_masks'] = output_dict['detection_masks'][0]
  return output_dict
Example #2
0
def select_fg_for_masks(class_targets, box_targets, boxes,
                        proposal_to_label_map, max_num_fg=128):
  """Selects the fore ground objects for mask branch during training.

  Args:
    class_targets: a tensor of shape [batch_size, num_boxes]  representing the
      class label for each box.
    box_targets: a tensor with a shape of [batch_size, num_boxes, 4]. The tensor
      contains the ground truth pixel coordinates of the scaled images for each
      roi.
    boxes: A 3-D Tensor of shape [batch_size, num_boxes, 4]. Each row
      represents a box with [y1, x1, y2, x2] in un-normalized coordinates.
    proposal_to_label_map: a tensor with a shape of [batch_size, num_boxes].
      This tensor keeps the mapping between proposal to labels.
      proposal_to_label_map[i] means the index of the ground truth instance for
      the i-th proposal.
    max_num_fg: an integer represents the number of masks per image.
  Returns:
    class_targets, boxes, proposal_to_label_map, box_targets that have
    foreground objects.
  """
  with tf.name_scope('select_fg_for_masks'):
    # Masks are for positive (fg) objects only. Reference: https://github.com/facebookresearch/Detectron/blob/master/detectron/roi_data/mask_rcnn.py  # pylint: disable=line-too-long
    batch_size = boxes.shape[0]
    _, fg_indices = tf.nn.top_k(
        tf.to_float(tf.greater(class_targets, 0)), k=max_num_fg)
    # Contructs indices for gather.
    indices = tf.reshape(
        fg_indices + tf.expand_dims(
            tf.range(batch_size) * tf.shape(class_targets)[1], 1), [-1])

    fg_class_targets = tf.reshape(
        tf.gather(tf.reshape(class_targets, [-1, 1]), indices),
        [batch_size, -1])
    fg_box_targets = tf.reshape(
        tf.gather(tf.reshape(box_targets, [-1, 4]), indices),
        [batch_size, -1, 4])
    fg_box_rois = tf.reshape(
        tf.gather(tf.reshape(boxes, [-1, 4]), indices), [batch_size, -1, 4])
    fg_proposal_to_label_map = tf.reshape(
        tf.gather(tf.reshape(proposal_to_label_map, [-1, 1]), indices),
        [batch_size, -1])

  return (fg_class_targets, fg_box_targets, fg_box_rois,
          fg_proposal_to_label_map)
Example #3
0
def _make_evaluation_metrics(labels, predictions, num_output_classes, hparams):
    """Construct various eval metrics.

  Args:
    labels: dict with ground truth data necessary for computing metrics.
    predictions: dict containing Tensors for predictions.
    num_output_classes: number of different labels.
    hparams: tf.contrib.HParams object.

  Returns:
    A dict where the values obey the tf.metrics API.
  """
    labels_op = labels[protein_dataset.LABEL_KEY]
    multi_hot_labels = _indices_to_multihot(labels_op, num_output_classes)
    predictions_as_floats = predictions[protein_dataset.LABEL_KEY]
    recall_threshold = hparams.decision_threshold
    predictions_as_bools = tf.greater(predictions_as_floats,
                                      tf.constant(recall_threshold))

    metrics = {
        'precision_at_threshold':
        tf.metrics.precision(labels=multi_hot_labels,
                             predictions=predictions_as_bools),
        'recall_at_threshold':
        tf.metrics.recall(labels=multi_hot_labels,
                          predictions=predictions_as_bools),
        'f1_at_threshold':
        _f1_score(labels=multi_hot_labels, predictions=predictions_as_bools),
        'mean_examplewise_f1_at_threshold':
        _mean_examplewise_f1_score(labels=multi_hot_labels,
                                   predictions=predictions_as_bools),
        'true_positives':
        tf.metrics.true_positives(labels=multi_hot_labels,
                                  predictions=predictions_as_bools),
        'false_positives':
        tf.metrics.false_positives(labels=multi_hot_labels,
                                   predictions=predictions_as_bools)
    }
    for k in _THRESHOLDS_FOR_RECALL_METRIC:
        metrics['recall@%d' % k] = _custom_recall_at_k(
            labels_as_multi_hot=multi_hot_labels,
            predictions=predictions_as_floats,
            k=k)

    return metrics
def random_horizontal_flip(image,
                           boxes=None,
                           masks=None,
                           keypoints=None,
                           keypoint_flip_permutation=None,
                           seed=None):
    def _flip_image(image):
        image_flipped = tf.image.flip_left_right(image)
        return image_flipped

    if keypoints is not None and keypoint_flip_permutation is None:
        raise ValueError(
            'keypoints are provided but keypoints_flip_permutation is not provided'
        )

    with tf.name_scope('RandomHorizontalFlip', values=[image, boxes]):
        result = []
        # random variable defining whether to do flip or not
        do_a_flip_random = tf.greater(tf.random_uniform([], seed=seed), 0.5)
        # flip image
        image = tf.cond(do_a_flip_random, lambda: _flip_image(image),
                        lambda: image)
        result.append(image)
        # flip boxes
        if boxes is not None:
            boxes = tf.cond(do_a_flip_random,
                            lambda: _flip_boxes_left_right(boxes),
                            lambda: boxes)
            result.append(boxes)
        # flip masks
        if masks is not None:
            masks = tf.cond(do_a_flip_random,
                            lambda: _flip_masks_left_right(masks),
                            lambda: masks)
            result.append(masks)

        # flip keypoints
        if keypoints is not None and keypoint_flip_permutation is not None:
            permutation = keypoint_flip_permutation
            keypoints = tf.cond(
                do_a_flip_random,
                lambda: keypoint_flip_horizontal(keypoints, 0.5, permutation),
                lambda: keypoints)
            result.append(keypoints)
        return tuple(result)
Example #5
0
    def body(self, features):
        self.has_actions = "input_action" in features
        self.has_rewards = "target_reward" in features
        self.has_policies = "target_policy" in features
        self.has_values = "target_value" in features
        hparams = self.hparams

        def merge(inputs, targets):
            """Split inputs and targets into lists."""
            inputs = tf.unstack(inputs, axis=1)
            targets = tf.unstack(targets, axis=1)
            assert len(inputs) == hparams.video_num_input_frames
            assert len(targets) == hparams.video_num_target_frames
            return inputs + targets

        frames = merge(features["inputs"], features["targets"])
        frames_raw = merge(features["inputs_raw"], features["targets_raw"])
        actions, rewards = None, None
        if self.has_actions:
            actions = merge(features["input_action"],
                            features["target_action"])
        if self.has_rewards:
            rewards = merge(features["input_reward"],
                            features["target_reward"])

        # Reset the internal states if the reset_internal_states has been
        # passed as a feature and has greater value than 0.
        if self.is_recurrent_model and self.internal_states is not None:

            def reset_func():
                reset_ops = flat_lists(self.reset_internal_states_ops())
                with tf.control_dependencies(reset_ops):
                    return tf.no_op()

            if self.is_predicting and "reset_internal_states" in features:
                reset = features["reset_internal_states"]
                reset = tf.greater(tf.reduce_sum(reset), 0.5)
                reset_ops = tf.cond(reset, reset_func, tf.no_op)
            else:
                reset_ops = tf.no_op()
            with tf.control_dependencies([reset_ops]):
                frames[0] = tf.identity(frames[0])

        with tf.control_dependencies([frames[0]]):
            return self.__process(frames, actions, rewards, frames_raw)
        def SingleImageDistortion(image):
            # Adjust brightness to a random level.
            if random_brightness:
                delta = tf.random_uniform([], -max_delta_brightness,
                                          max_delta_brightness)
                image = tf.image.adjust_brightness(image, delta)

            # Adjust saturation to a random level.
            if random_saturation:
                lower = lower_saturation
                upper = upper_saturation
                saturation_factor = tf.random_uniform([], lower, upper)
                image = tf.image.adjust_saturation(image, saturation_factor)

            # Randomly shift the hue.
            if random_hue:
                delta = tf.random_uniform([], -max_delta_hue, max_delta_hue)
                image = tf.image.adjust_hue(image, delta)

            # Adjust contrast to a random level.
            if random_contrast:
                lower = lower_contrast
                upper = upper_contrast
                contrast_factor = tf.random_uniform([], lower, upper)
                image = tf.image.adjust_contrast(image, contrast_factor)

            # Add random Gaussian noise.
            if random_noise_level:
                rnd_noise = tf.random_normal(tf.shape(image),
                                             stddev=random_noise_level)
                img_shape = tf.shape(image)

                def ImageClosure(value):
                    return lambda: value

                image = tf.cond(
                    tf.reduce_all(
                        tf.greater(tf.random.uniform([1]),
                                   random_noise_apply_probability)),
                    ImageClosure(image), ImageClosure(image + rnd_noise))
                image = tf.reshape(image, img_shape)

            # Clip to valid range.
            image = tf.clip_by_value(image, 0.0, 1.0)
            return image
Example #7
0
    def _build_losses(self, json_data):
        actor_weight_decay = 0 if (
            self.ACTOR_WEIGHT_DECAY_KEY
            not in json_data) else json_data[self.ACTOR_WEIGHT_DECAY_KEY]
        critic_weight_decay = 0 if (
            self.CRITIC_WEIGHT_DECAY_KEY
            not in json_data) else json_data[self.CRITIC_WEIGHT_DECAY_KEY]

        norm_val_diff = self.val_norm.normalize_tf(
            self.tar_val_tf) - self.val_norm.normalize_tf(self.critic_tf)
        self.critic_loss_tf = 0.5 * tf.reduce_mean(tf.square(norm_val_diff))

        if (critic_weight_decay != 0):
            self.critic_loss_tf += critic_weight_decay * self._weight_decay_loss(
                'main/critic')

        norm_tar_a_tf = self.a_norm.normalize_tf(self.a_tf)
        self._norm_a_mean_tf = self.a_norm.normalize_tf(self.a_mean_tf)

        self.logp_tf = TFUtil.calc_logp_gaussian(norm_tar_a_tf,
                                                 self._norm_a_mean_tf,
                                                 self.norm_a_std_tf)
        ratio_tf = tf.exp(self.logp_tf - self.old_logp_tf)
        actor_loss0 = self.adv_tf * ratio_tf
        actor_loss1 = self.adv_tf * tf.clip_by_value(
            ratio_tf, 1.0 - self.ratio_clip, 1 + self.ratio_clip)
        self.actor_loss_tf = -tf.reduce_mean(
            tf.minimum(actor_loss0, actor_loss1))

        norm_a_bound_min = self.a_norm.normalize(self.a_bound_min)
        norm_a_bound_max = self.a_norm.normalize(self.a_bound_max)
        a_bound_loss = TFUtil.calc_bound_loss(self._norm_a_mean_tf,
                                              norm_a_bound_min,
                                              norm_a_bound_max)
        self.actor_loss_tf += a_bound_loss

        if (actor_weight_decay != 0):
            self.actor_loss_tf += actor_weight_decay * self._weight_decay_loss(
                'main/actor')

        # for debugging
        self.clip_frac_tf = tf.reduce_mean(
            tf.to_float(tf.greater(tf.abs(ratio_tf - 1.0), self.ratio_clip)))

        return
Example #8
0
 def body_fn(step, selected_indices):
     cluster_mask = tf.equal(clusters, unique_clusters[step])
     cluster_indices = tf.where(cluster_mask)[:, 0]
     cluster_scores = tf.gather(scores, cluster_indices, axis=0)
     selected_idx = tf.cond(
         pred=tf.greater(size_per_cluster[step], 0),
         true_fn=lambda: Sampler.select_indices(
             size=size_per_cluster[step],
             scores=cluster_scores,
             indices=cluster_indices,
             soft=soft,
         ),
         false_fn=lambda: tf.constant([], dtype=tf.int32),
     )
     return [
         tf.add(step, 1),
         selected_indices.write(step, selected_idx)
     ]
 def nucleus_sampling(self, logits):
     """Nucleus sampling."""
     p = self.hparams.nucleus_sampling
     tf.logging.info("Nucleus sampling top_p = {}".format(p))
     sort_indices = tf.argsort(logits, axis=-1, direction="DESCENDING")
     probs = tf.gather(tf.nn.softmax(logits), sort_indices, batch_dims=1)
     cumprobs = tf.cumsum(probs, axis=-1, exclusive=True)
     # The top 1 candidate always will not be masked.
     # This way ensures at least 1 indices will be selected.
     sort_mask = tf.cast(tf.greater(cumprobs, p), logits.dtype)
     batch_indices = tf.tile(
         tf.expand_dims(tf.range(logits.shape[0]), axis=-1),
         [1, logits.shape[1]])
     top_p_mask = tf.scatter_nd(
         tf.stack([batch_indices, sort_indices], axis=-1), sort_mask,
         logits.shape)
     logits -= top_p_mask * logits.dtype.max
     return logits
Example #10
0
    def _decode_masks(self, parsed_tensors):
        """Decode a set of PNG masks to the tf.float32 tensors."""
        def _decode_png_mask(png_bytes):
            mask = tf.squeeze(tf.io.decode_png(png_bytes,
                                               channels=1,
                                               dtype=tf.uint8),
                              axis=-1)
            mask = tf.cast(mask, dtype=tf.float32)
            mask.set_shape([None, None])
            return mask

        height = parsed_tensors['image/height']
        width = parsed_tensors['image/width']
        masks = parsed_tensors['image/object/mask']
        return tf.cond(
            tf.greater(tf.size(masks), 0),
            lambda: tf.map_fn(_decode_png_mask, masks, dtype=tf.float32),
            lambda: tf.zeros([0, height, width], dtype=tf.float32))
Example #11
0
    def visit(tuple_path, oneof):
      """Visit a OneOf node in `structure`."""
      string_path = '/'.join(map(str, tuple_path))
      num_choices = len(oneof.choices)

      logits = tf.get_variable(
          name='logits/' + string_path,
          initializer=tf.initializers.zeros(),
          shape=[num_choices],
          dtype=tf.float32)
      logits = logits / temperature

      tag_name = '{:s}_{:d}'.format(oneof.tag, tag_counters[oneof.tag])
      tag_counters[oneof.tag] += 1

      dist_info['logits_by_path'][string_path] = logits
      dist_info['logits_by_tag'][tag_name] = logits

      dist = tfp.distributions.OneHotCategorical(
          logits=logits, dtype=tf.float32)
      entropies[tuple_path] = dist.entropy()

      sample_mask = dist.sample()
      sample_log_prob = dist.log_prob(sample_mask)
      if oneof.tag == basic_specs.OP_TAG:
        sample_mask, sample_log_prob = _replace_sample_with_probability(
            sample_mask, sample_log_prob, increase_ops_probability,
            tf.constant([1.0/num_choices]*num_choices, tf.float32))
      elif oneof.tag == basic_specs.FILTERS_TAG:
        # NOTE: While np.argmax() was originally designed to work with integer
        # filter sizes, it will also work with any object type that supports
        # "less than" and "greater than" operations.
        sample_mask, sample_log_prob = _replace_sample_with_probability(
            sample_mask, sample_log_prob, increase_filters_probability,
            tf.one_hot(np.argmax(oneof.choices), len(oneof.choices)))

      log_probs[tuple_path] = sample_log_prob
      for i in range(len(oneof.choices)):
        tuple_subpath = tuple_path + ('choices', i)
        is_active[tuple_subpath] = tf.greater(tf.abs(sample_mask[i]), 1e-6)

      return schema.OneOf(choices=oneof.choices,
                          tag=oneof.tag,
                          mask=sample_mask)
def pano_forwards_condition(trip):
    """Checks if a pano is in a forward condition."""
    ref_pose = trip.pose[1, :, :]
    pano_pose = trip.pose[3, :, :]
    ref_twds = -1.0 * ref_pose[:, 2]

    # make sure max_depth>forward motion>median_depth
    t_vec = pano_pose[:, 3] - ref_pose[:, 3]
    ref_depth = trip.depth[1, :, :, 0]
    ref_depth = tf.where(tf.equal(ref_depth, 0.0),
                         tf.reduce_max(ref_depth) * tf.ones_like(ref_depth),
                         ref_depth)
    max_depth = tf.reduce_max(ref_depth)
    median_depth = tf.contrib.distributions.percentile(ref_depth, 0.5)

    min_depth_cond = tf.greater(tf.reduce_sum(ref_twds * t_vec), median_depth)
    max_depth_cond = tf.less(tf.reduce_sum(ref_twds * t_vec), max_depth)

    return tf.logical_and(min_depth_cond, max_depth_cond)
Example #13
0
def _fast_rcnn_box_loss(box_outputs, box_targets, class_targets, normalizer=1.0,
                        delta=1.):
  """Computes box regression loss."""
  # delta is typically around the mean value of regression target.
  # for instances, the regression targets of 512x512 input with 6 anchors on
  # P2-P6 pyramid is about [0.1, 0.1, 0.2, 0.2].
  with tf.name_scope('fast_rcnn_box_loss'):
    mask = tf.tile(tf.expand_dims(tf.greater(class_targets, 0), axis=2),
                   [1, 1, 4])
    # The loss is normalized by the sum of non-zero weights before additional
    # normalizer provided by the function caller.
    box_loss = tf.losses.huber_loss(
        box_targets,
        box_outputs,
        weights=mask,
        delta=delta,
        reduction=tf.losses.Reduction.SUM_BY_NONZERO_WEIGHTS)
    box_loss /= normalizer
    return box_loss
Example #14
0
def detectMinVal(input_mat, var, threshold=1e-6, name='', debug=False):
    eigen_min = tf.reduce_min(input_mat)
    eigen_max = tf.reduce_max(input_mat)
    eigen_ratio = eigen_max / eigen_min
    input_mat_clipped = clipoutNeg(input_mat, threshold)

    if debug:
        input_mat_clipped = tf.cond(
            tf.logical_or(tf.greater(eigen_ratio, 0.),
                          tf.less(eigen_ratio,
                                  -500)), lambda: input_mat_clipped,
            lambda: tf.Print(input_mat_clipped, [
                tf.convert_to_tensor('screwed ratio ' + name +
                                     ' eigen values!!!'),
                tf.convert_to_tensor(var.name), eigen_min, eigen_max,
                eigen_ratio
            ]))

    return input_mat_clipped
Example #15
0
 def model_fn(features, labels, mode):
   """The model function for creating an Estimtator."""
   del labels
   input_count = tf.reduce_sum(
       tf.to_int32(tf.greater(features["input_refs"][:, :, 1],
                              features["input_refs"][:, :, 0])))
   tf.summary.scalar("input_count", input_count)
   loss_dict, pred_dict, areas = seq2act_model.core_graph(
       features, hparams, mode, compute_additional_loss_fn)
   if mode == tf_estimator.ModeKeys.PREDICT:
     pred_dict["sequences"] = decode_sequence(
         features, areas, hparams, decode_length,
         post_processing=FLAGS.post_processing)
     return tf_estimator.EstimatorSpec(mode, predictions=pred_dict)
   elif mode == tf_estimator.ModeKeys.EVAL:
     metrics = {}
     _eval(metrics, pred_dict, loss_dict, features,
           areas, compute_seq_accuracy,
           hparams,
           metric_types=FLAGS.metric_types.split(","),
           decode_length=decode_length)
     if compute_additional_metric_fn:
       compute_additional_metric_fn(metrics, pred_dict, features)
     return tf_estimator.EstimatorSpec(
         mode, loss=loss_dict["total_loss"], eval_metric_ops=metrics)
   else:
     assert mode == tf_estimator.ModeKeys.TRAIN
     loss = loss_dict["total_loss"]
     for loss_name in loss_dict:
       if loss_name == "total_loss":
         continue
       if loss_name.endswith("losses"):
         continue
       tf.summary.scalar(loss_name, loss_dict[loss_name])
     step_num = tf.to_float(tf.train.get_global_step())
     schedule_string = hparams.learning_rate_schedule
     names = schedule_string.split("*")
     names = [name.strip() for name in names if name.strip()]
     ret = tf.constant(1.0)
     for name in names:
       ret *= learning_rate.learning_rate_factor(name, step_num, hparams)
     train_op = optimize.optimize(loss, ret, hparams)
     return tf_estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
Example #16
0
 def body_fn(size_left, cluster_counts_left, cluster_sizes):
     # Determine available clusters.
     cluster_mask = tf.greater(cluster_counts_left, 0)
     available_clusters = tf.where(cluster_mask)[:, 0]
     # Uniformly select clusters from available.
     indices = tf.random.uniform(
         dtype=tf.int32, shape=(size_left,), maxval=tf.size(available_clusters)
     )
     cluster_indices = tf.gather(available_clusters, indices, axis=0)
     cluster_sizes = tf.tensor_scatter_nd_add(
         cluster_sizes,
         indices=tf.expand_dims(cluster_indices, -1),
         updates=tf.ones_like(cluster_indices, dtype=tf.int32),
     )
     # Truncate cluster sizes as necessary.
     cluster_sizes = tf.minimum(cluster_sizes, cluster_counts)
     cluster_counts_left = cluster_counts - cluster_sizes
     size_left = size - tf.reduce_sum(cluster_sizes)
     return size_left, cluster_counts_left, cluster_sizes
Example #17
0
def select_action_tf(belief, vector_set):
    """
    Compute optimal action given a belief distribution
    :param belief: dim(belief) == dim(AlphaVector)
    :param vector_set
    :return: optimal action, V(b)
    """
    assert not len(vector_set) == 0

    max_v = tf.constant([-np.inf], tf.float32)
    best_action = tf.constant([-1])
    for av in vector_set:
        with tf.name_scope('V_b'):
            v = tf.reduce_sum(tf.multiply(av.v, belief))
            best_action = tf.cond(tf.greater(v, max_v)[0], lambda: tf.constant([av.action]),
                                  lambda: best_action)
            max_v = tf.maximum(v, max_v)

    return best_action, max_v
Example #18
0
def compute_normalization_binary_search(activations, t, num_iters=10):
    """Returns the normalization value for each example (t < 1.0).

  Args:
    activations: A multi-dimensional tensor with last dimension `num_classes`.
    t: Temperature 2 (< 1.0 for finite support).
    num_iters: Number of iterations to run the method.
  Return: A tensor of same rank as activation with the last dimension being 1.
  """
    mu = tf.reduce_max(activations, -1, keep_dims=True)
    normalized_activations = activations - mu
    shape_activations = tf.shape(activations)
    effective_dim = tf.cast(
        tf.reduce_sum(tf.cast(
            tf.greater(normalized_activations, -1.0 / (1.0 - t)), tf.int32),
                      -1,
                      keep_dims=True), tf.float32)
    shape_partition = tf.concat([shape_activations[:-1], [1]], 0)
    lower = tf.zeros(shape_partition)
    upper = -log_t(1.0 / effective_dim, t) * tf.ones(shape_partition)

    def iter_condition(i, unused_lower, unused_upper):
        return i < num_iters

    def iter_body(i, lower, upper):
        logt_partition = (upper + lower) / 2.0
        sum_probs = tf.reduce_sum(exp_t(
            normalized_activations - logt_partition, t),
                                  -1,
                                  keep_dims=True)
        update = tf.cast(tf.less(sum_probs, 1.0), tf.float32)
        lower = tf.reshape(lower * update + (1.0 - update) * logt_partition,
                           shape_partition)
        upper = tf.reshape(upper * (1.0 - update) + update * logt_partition,
                           shape_partition)
        return [i + 1, lower, upper]

    _, lower, upper = tf.while_loop(iter_condition,
                                    iter_body, [0, lower, upper],
                                    maximum_iterations=num_iters)
    logt_partition = (upper + lower) / 2.0
    return logt_partition + mu
Example #19
0
def preprocessing_fn(inputs):
    """tf.transform's callback function for preprocessing inputs.

  Args:
    inputs: map from feature keys to raw not-yet-transformed features.

  Returns:
    Map from string feature key to transformed feature operations.
  """
    outputs = {}
    for key in _DENSE_FLOAT_FEATURE_KEYS:
        # Preserve this feature as a dense float, setting nan's to the mean.
        outputs[_transformed_name(key)] = tft.scale_to_z_score(
            _fill_in_missing(inputs[key]))

    for key in _VOCAB_FEATURE_KEYS:
        # Build a vocabulary for this feature.
        outputs[_transformed_name(key)] = tft.compute_and_apply_vocabulary(
            _fill_in_missing(inputs[key]),
            top_k=_VOCAB_SIZE,
            num_oov_buckets=_OOV_SIZE)

    for key in _BUCKET_FEATURE_KEYS:
        outputs[_transformed_name(key)] = tft.bucketize(
            _fill_in_missing(inputs[key]),
            _FEATURE_BUCKET_COUNT,
            always_return_num_quantiles=False)

    for key in _CATEGORICAL_FEATURE_KEYS:
        outputs[_transformed_name(key)] = _fill_in_missing(inputs[key])

    # Was this passenger a big tipper?
    taxi_fare = _fill_in_missing(inputs[_FARE_KEY])
    tips = _fill_in_missing(inputs[_LABEL_KEY])
    outputs[_transformed_name(_LABEL_KEY)] = tf.compat.v1.where(
        tf.math.is_nan(taxi_fare),
        tf.cast(tf.zeros_like(taxi_fare), tf.int64),
        # Test if the tip was > 20% of the fare.
        tf.cast(tf.greater(tips, tf.multiply(taxi_fare, tf.constant(0.2))),
                tf.int64))

    return outputs
def padded_one_hot_encoding(indices, depth, left_pad):
  """Returns a zero padded one-hot tensor.

  This function converts a sparse representation of indices (e.g., [4]) to a
  zero padded one-hot representation (e.g., [0, 0, 0, 0, 1] with depth = 4 and
  left_pad = 1). If `indices` is empty, the result will simply be a tensor of
  shape (0, depth + left_pad). If depth = 0, then this function just returns
  `None`.

  Args:
    indices: an integer tensor of shape [num_indices].
    depth: depth for the one-hot tensor (integer).
    left_pad: number of zeros to left pad the one-hot tensor with (integer).

  Returns:
    padded_onehot: a tensor with shape (num_indices, depth + left_pad). Returns
      `None` if the depth is zero.

  Raises:
    ValueError: if `indices` does not have rank 1 or if `left_pad` or `depth are
      either negative or non-integers.

  TODO(rathodv): add runtime checks for depth and indices.
  """
  if depth < 0 or not isinstance(depth, six.integer_types):
    raise ValueError('`depth` must be a non-negative integer.')
  if left_pad < 0 or not isinstance(left_pad, six.integer_types):
    raise ValueError('`left_pad` must be a non-negative integer.')
  if depth == 0:
    return None

  rank = len(indices.get_shape().as_list())
  if rank != 1:
    raise ValueError('`indices` must have rank 1, but has rank=%s' % rank)

  def one_hot_and_pad():
    one_hot = tf.cast(tf.one_hot(tf.cast(indices, tf.int64), depth,
                                 on_value=1, off_value=0), tf.float32)
    return tf.pad(one_hot, [[0, 0], [left_pad, 0]], mode='CONSTANT')
  result = tf.cond(tf.greater(tf.size(indices), 0), one_hot_and_pad,
                   lambda: tf.zeros((tf.size(indices), depth + left_pad)))
  return tf.reshape(result, [-1, depth + left_pad])
  def __call__(self, attribute_outputs, attribute_targets, select_class_targets):
    with tf.name_scope('attributes_loss'):
      batch_size, num_instances, num_attributes = attribute_outputs.get_shape().as_list()
      weights = tf.tile(
          tf.reshape(tf.greater(select_class_targets, 0), [batch_size, num_instances, 1]),
          [1, 1, num_attributes])

      if self._loss_type == 'sigmoid_cross_entropy':
        loss = tf.losses.sigmoid_cross_entropy(attribute_targets, attribute_outputs, weights=weights,
                                               reduction=tf.losses.Reduction.SUM_BY_NONZERO_WEIGHTS)
      elif self._loss_type == 'focal':
        losses = focal_loss_v2(attribute_outputs, attribute_targets, self._focal_loss_alpha, self._focal_loss_gamma,
                               1.0)
        loss = tf.losses.compute_weighted_loss(losses, weights=weights,
                                               reduction=tf.losses.Reduction.SUM_BY_NONZERO_WEIGHTS)
        loss *= self._focal_loss_weight
      else:
        raise ValueError('Loss type "%s" is not implemented' % self._loss_type)

      return loss
Example #22
0
def maybe_add_noise(image, noise_shape, scale0, scale1,
                    image_noise_probability, image_noise_ratio):
    """Add noise at two scales."""

    if image_noise_probability < 0.000001 or (image_noise_ratio < 0.000001):
        return image

    noise_list = []
    for scale in [scale0, scale1]:
        rand_image_noise_ratio = tf.random.uniform(shape=[],
                                                   minval=0.0,
                                                   maxval=image_noise_ratio)
        noise_list.append(
            _rand_noise(0.0, rand_image_noise_ratio, scale, noise_shape))

    skip_noise = tf.greater(tf.random.uniform([]), image_noise_probability)
    image = tf.cond(skip_noise, lambda: image, lambda: image + noise_list[0])
    image = tf.cond(skip_noise, lambda: image, lambda: image + noise_list[1])

    return image
Example #23
0
def compute_weight_by_frequency(labels):
    """Computes weights to keep positive/negative labels in balance.

   For instance if labels contains 1 positive out of 9 negative, the positive
   one will have weight 0.9, while negative will have weight 0.1

  Args:
    labels: +1/-1 tensor of shape [..., num_channels]

  Returns:
    tensor of the same shape as labels.
  """
    p = tf.greater(labels, 0)
    pf = tf.to_float(p)
    positives = tf.reduce_sum(pf, axis=-1, keepdims=True) + tf.zeros_like(pf)
    negatives = tf.reduce_sum(1 - pf, axis=-1,
                              keepdims=True) + tf.zeros_like(pf)
    total = positives + negatives
    weights = tf.where(p, negatives / total, positives / total)
    return weights
Example #24
0
 def process_obj_mask(obj_id):
     """Create a mask for obj_id, skipping the background mask."""
     mask = tf.logical_and(
         tf.equal(current_seg, obj_id),  # pylint: disable=cell-var-from-loop
         tf.not_equal(tf.cast(0, tf.uint8), obj_id))
     # Leave out vert small masks, that are most often errors.
     size = tf.reduce_sum(tf.to_int32(mask))
     mask = tf.logical_and(mask,
                           tf.greater(size, MIN_OBJECT_AREA))
     if not self.boxify:
         return mask
     # Complete the mask to its bounding box.
     binary_obj_masks_y = tf.reduce_any(mask,
                                        axis=1,
                                        keepdims=True)
     binary_obj_masks_x = tf.reduce_any(mask,
                                        axis=0,
                                        keepdims=True)
     return tf.logical_and(binary_obj_masks_y,
                           binary_obj_masks_x)
    def per_instance_loss(i, current_ta_term_0, current_ta_term_1,
                          current_ta_term_2):
        # TODO: simplify condition, use instance_axis somehow
        if data_format == 'channels_first':
            target_instances_mask = tf.expand_dims(tf.equal(
                instances[i, ...], 1),
                                                   axis=instance_axis)
            other_instances_mask = tf.expand_dims(tf.equal(
                instances[i, ...], 2),
                                                  axis=instance_axis)
        else:
            target_instances_mask = tf.expand_dims(tf.equal(
                instances[..., i], 1),
                                                   axis=instance_axis)
            other_instances_mask = tf.expand_dims(tf.equal(
                instances[..., i], 2),
                                                  axis=instance_axis)

        current_terms = tf.cond(
            tf.greater(tf.count_nonzero(target_instances_mask),
                       0), lambda: cosine_embedding_single_instance_loss(
                           embeddings,
                           target_instances_mask,
                           other_instances_mask,
                           normalized_embeddings,
                           term_1_2_normalization=term_1_2_normalization,
                           term_0_squared=term_0_squared,
                           term_1_squared=term_1_squared,
                           return_h_norm=return_h_norm,
                           is_background=is_background,
                           data_format=data_format), lambda:
            (tf.zeros([0], dtype=embeddings.dtype),
             tf.zeros([0], dtype=embeddings.dtype)) if not return_h_norm else
            (tf.zeros([0], dtype=embeddings.dtype),
             tf.zeros([0], dtype=embeddings.dtype),
             tf.zeros([embeddings.shape[0]], dtype=embeddings.dtype)))
        current_ta_term_0 = current_ta_term_0.write(i, current_terms[0])
        current_ta_term_1 = current_ta_term_1.write(i, current_terms[1])
        if return_h_norm:
            current_ta_term_2 = current_ta_term_2.write(i, current_terms[2])
        return i + 1, current_ta_term_0, current_ta_term_1, current_ta_term_2
Example #26
0
def _atan2(y, x):
    """ My implementation of atan2 in tensorflow.  Returns in -pi .. pi."""
    tan = tf.atan(y / (x + 1e-8))  # this returns in -pi/2 .. pi/2

    one_map = tf.ones_like(tan)

    # correct quadrant error
    correction = tf.where(tf.less(x + 1e-8, 0.0), 3.141592653589793 * one_map,
                          0.0 * one_map)
    tan_c = tan + correction  # this returns in -pi/2 .. 3pi/2

    # bring to positive values
    correction = tf.where(tf.less(tan_c, 0.0), 2 * 3.141592653589793 * one_map,
                          0.0 * one_map)
    tan_zero_2pi = tan_c + correction  # this returns in 0 .. 2pi

    # make symmetric
    correction = tf.where(tf.greater(tan_zero_2pi, 3.141592653589793),
                          -2 * 3.141592653589793 * one_map, 0.0 * one_map)
    tan_final = tan_zero_2pi + correction  # this returns in -pi .. pi
    return tan_final
Example #27
0
def intensity_shift(image, label, per_class_intensity_scale,
                    per_class_intensity_shift):
    """Perturb intensity in lesion and non-lesion regions."""

    if per_class_intensity_scale < 0.000001 and (per_class_intensity_shift <
                                                 0.000001):
        return image

    # Randomly change (mostly increase) intensity of non-lesion region.
    per_class_noise = _truncated_normal(per_class_intensity_shift,
                                        per_class_intensity_scale)
    image = image + per_class_noise * (
        image * tf.cast(tf.greater(label, 1.5), tf.float32))

    # Randomly change (mostly decrease) intensity of lesion region.
    per_class_noise = _truncated_normal(-per_class_intensity_shift,
                                        per_class_intensity_scale)
    image = image + per_class_noise * (
        image * tf.cast(tf.less(label, 1.5), tf.float32))

    return image
Example #28
0
def zero_out_clipped_grads(grad, x, clip_min, clip_max):
    """
  Helper function to erase entries in the gradient where the update would be
  clipped.
  :param grad: The gradient
  :param x: The current input
  :param clip_min: Minimum input component value
  :param clip_max: Maximum input component value
  """
    signed_grad = tf.sign(grad)

    # Find input components that lie at the boundary of the input range, and
    # where the gradient points in the wrong direction.
    clip_low = tf.logical_and(tf.less_equal(x, tf.cast(clip_min, x.dtype)),
                              tf.less(signed_grad, 0))
    clip_high = tf.logical_and(tf.greater_equal(x, tf.cast(clip_max, x.dtype)),
                               tf.greater(signed_grad, 0))
    clip = tf.logical_or(clip_low, clip_high)
    grad = tf.where(clip, mul(grad, 0), grad)

    return grad
Example #29
0
def _to_bfloat16_unbiased(x, noise):
    """Convert a float32 to a bfloat16 using randomized roundoff.

  Args:
    x: A float32 Tensor.
    noise: a float32 Tensor with values in [0, 1), broadcastable to tf.shape(x)
  Returns:
    A float32 Tensor.
  """
    x_sign = tf.sign(x)
    # Make sure x is positive.  If it is zero, the two candidates are identical.
    x = x * x_sign + 1e-30
    cand1 = tf.to_bfloat16(x)
    cand1_f = tf.to_float(cand1)
    # This relies on the fact that for a positive bfloat16 b,
    # b * 1.005 gives you the next higher bfloat16 and b*0.995 gives you the
    # next lower one. Both 1.005 and 0.995 are ballpark estimation.
    cand2 = tf.to_bfloat16(
        tf.where(tf.greater(x, cand1_f), cand1_f * 1.005, cand1_f * 0.995))
    ret = _randomized_roundoff_to_bfloat16(x, noise, cand1, cand2)
    return ret * tf.to_bfloat16(x_sign)
Example #30
0
def _weights_for_nonzero_refs(source_waveforms, consider_as_zero=None):
    """Return shape (batch, source) weights for signals that are nonzero.

  Args:
    source_waveforms: A tensor (batch, source, samples), dtype=tf.float32.
    consider_as_zero: An optional tensor (batch, source), dtype=tf.bool which
      indicates some entries as being zero even if they are not exactly zero.
      This can be used to indicate some source types (e.g. noise) as being
      zero regardless of their norm.
  Returns:
    consider_nonzero: A tensor (batch, source), dtype=tf.bool of sources
      that will be considered as non-zero based on their norm being greater than
      1e-8 or they are not in consider_as_zero array if given.
  """
    source_norms = tf.sqrt(tf.reduce_mean(tf.square(source_waveforms),
                                          axis=-1))
    consider_nonzero = tf.greater(source_norms, 1e-8)
    if consider_as_zero is not None:
        consider_nonzero = tf.logical_and(consider_nonzero,
                                          tf.logical_not(consider_as_zero))
    return consider_nonzero