def SampleIds(self):
     p = self.params
     if p.cur_iter_in_seed:
         random_seed = p.random_seed * 2000 * self._cur_iter
     else:
         random_seed = p.random_seed * 2000
     return tf.as_string(
         tf.random_uniform(p.target_shape[:1], seed=random_seed))
Пример #2
0
 def _BuildSave(self):
     """Builds save ops."""
     self._save_global_step = py_utils.GetGlobalStep()
     self._save_prefix = tf.strings.join([
         self._logdir_ph, "/ckpt-",
         tf.as_string(self._save_global_step, width=8, fill="0")
     ])
     self._save_op = self._AddShardedSaveOps(self._vars, self._save_prefix,
                                             _VarKey)
    def _runBeamSearchV2OpHelper(self,
                                 b_size,
                                 num_beams,
                                 seq_len,
                                 init_best_score,
                                 probs,
                                 init_atten_probs,
                                 atten_probs,
                                 beam_size=3.0,
                                 ensure_full_beam=False,
                                 force_eos_in_last_step=False,
                                 local_eos_threshold=-100.0):
        eos_id = 2
        num_hyps_per_beam = b_size / num_beams

        best_scores = tf.zeros([num_beams])
        cumulative_scores = tf.zeros([b_size])
        scores = tf.zeros([seq_len, b_size])
        hyps = tf.zeros([seq_len, b_size], dtype=tf.int32)
        prev_hyps = tf.zeros([seq_len, b_size], dtype=tf.int32)
        done_hyps = tf.as_string(tf.zeros([seq_len, b_size], dtype=tf.int32))
        best_scores += init_best_score
        beam_done = tf.zeros([num_beams], dtype=tf.bool)

        for i, prob in enumerate(probs):
            (best_scores, cumulative_scores, scores, hyps, prev_hyps,
             done_hyps, atten_probs, beam_done,
             done) = ops.beam_search_step_v2(
                 prob,
                 init_atten_probs,
                 best_scores,
                 cumulative_scores,
                 scores,
                 hyps,
                 prev_hyps,
                 done_hyps,
                 atten_probs,
                 beam_done, [],
                 i,
                 eos_id=eos_id,
                 beam_size=beam_size,
                 ensure_full_beam=ensure_full_beam,
                 num_hyps_per_beam=num_hyps_per_beam,
                 valid_eos_max_logit_delta=0.1,
                 force_eos_in_last_step=force_eos_in_last_step,
                 local_eos_threshold=local_eos_threshold,
                 beam_independence=True)

        with self.session(use_gpu=False):
            (best_scores, cumulative_scores, scores, hyps, prev_hyps,
             done_hyps, atten_probs, done, beam_done) = self.evaluate([
                 best_scores, cumulative_scores, scores, hyps, prev_hyps,
                 done_hyps, atten_probs, done, beam_done
             ])

        return (best_scores, cumulative_scores, scores, hyps, prev_hyps,
                done_hyps, atten_probs, done, beam_done)
Пример #4
0
 def _BuildSave(self):
   """Builds save ops."""
   self._save_global_step = py_utils.GetGlobalStep()
   self._save_prefix = tf.strings.join([
       self._logdir_ph, "/ckpt-",
       tf.as_string(self._save_global_step, width=8, fill="0")
   ])
   self._save_op = io_ops.save_v2(
       prefix=self._save_prefix,
       tensor_names=[_VarKey(v) for v in self._vars],
       tensors=[v.read_value() for v in self._vars],
       shape_and_slices=[""] * len(self._vars))
Пример #5
0
  def ProcessOutputs(self, input_batch, model_outputs):
    """Produce additional decoder outputs for WaymoOpenDataset.

    Args:
      input_batch: A .NestedMap of the inputs to the model.
      model_outputs: A .NestedMap of the outputs of the model, including::
        - per_class_predicted_bboxes: [batch, num_classes, num_boxes, 7] float
          Tensor with per class 3D (7 DOF) bounding boxes.
        - per_class_predicted_bbox_scores: [batch, num_classes, num_boxes] float
          Tensor with per class, per box scores.
        - per_class_valid_mask: [batch, num_classes, num_boxes] masking Tensor
          indicating which boxes were still kept after NMS for each class.

    Returns:
      A NestedMap of additional decoder outputs needed for
      PostProcessDecodeOut.
    """
    del model_outputs
    p = self.params
    input_labels = input_batch.labels
    input_metadata = input_batch.metadata
    source_ids = tf.strings.join([
        input_metadata.run_segment,
        tf.as_string(input_metadata.run_start_offset)
    ],
                                 separator='_')
    ret = py_utils.NestedMap({
        'num_points_in_bboxes': input_batch.labels.bboxes_3d_num_points,
        # Ground truth.
        'bboxes_3d': input_labels.bboxes_3d,
        'bboxes_3d_mask': input_labels.bboxes_3d_mask,
        'labels': input_labels.labels,
        'label_ids': input_labels.label_ids,
        'speed': input_labels.speed,
        'acceleration': input_labels.acceleration,
        # Fill the following in.
        'source_ids': source_ids,
        'difficulties': input_labels.single_frame_detection_difficulties,
        'unfiltered_bboxes_3d_mask': input_labels.unfiltered_bboxes_3d_mask,
        'run_segment': input_metadata.run_segment,
        'run_start_offset': input_metadata.run_start_offset,
        'pose': input_metadata.pose,
    })
    if p.draw_visualizations:
      laser_sample = self._SampleLaserForVisualization(
          input_batch.lasers.points_xyz, input_batch.lasers.points_padding)
      ret.update(laser_sample)
    return ret
Пример #6
0
    def testTopKTerminatedHypsOp(self):
        with self.session(use_gpu=False):
            b_size = 8
            num_beams = 2
            num_hyps_per_beam = b_size / num_beams
            seq_len = 6
            scores = tf.random.uniform([b_size, 5], seed=12345)
            atten_probs = tf.random.uniform([b_size, 3], seed=12345)
            src_seq_lengths = [3, 3]
            best_scores = tf.zeros([num_beams])
            cumulative_scores = tf.zeros([b_size])
            in_scores = tf.zeros([seq_len, b_size])
            in_hyps = tf.zeros([seq_len, b_size], dtype=tf.int32)
            in_prev_hyps = tf.zeros([seq_len, b_size], dtype=tf.int32)
            in_done_hyps = tf.as_string(
                tf.zeros([seq_len, b_size], dtype=tf.int32))
            in_atten_probs = tf.zeros([seq_len, b_size, 3])

            (out_best_scores_0, out_cumulative_scores_0, out_scores_0,
             out_hyps_0, out_prev_hyps_0, out_done_hyps_0, out_atten_probs_0,
             _) = ops.beam_search_step(scores,
                                       atten_probs,
                                       best_scores,
                                       cumulative_scores,
                                       in_scores,
                                       in_hyps,
                                       in_prev_hyps,
                                       in_done_hyps,
                                       in_atten_probs, [],
                                       0,
                                       eos_id=2,
                                       beam_size=3.0,
                                       num_hyps_per_beam=num_hyps_per_beam)

            outputs = ops.beam_search_step(scores,
                                           atten_probs,
                                           out_best_scores_0,
                                           out_cumulative_scores_0,
                                           out_scores_0,
                                           out_hyps_0,
                                           out_prev_hyps_0,
                                           out_done_hyps_0,
                                           out_atten_probs_0, [],
                                           1,
                                           eos_id=2,
                                           beam_size=3.0,
                                           num_hyps_per_beam=num_hyps_per_beam)

            # Get the topk terminated hyps.
            in_done_hyps = outputs[5]
            topk_hyps = ops.top_k_terminated_hyps(
                in_done_hyps,
                src_seq_lengths,
                k=2,
                num_hyps_per_beam=num_hyps_per_beam,
                length_normalization=0.2,
                coverage_penalty=0.2,
                target_seq_length_ratio=1.0)
            seq_ids, seq_lens, seq_scores = ops.unpack_hyp(tf.reshape(
                topk_hyps, [-1]),
                                                           max_seq_length=5)

            k1, k2, k3, k4 = self.evaluate(
                [topk_hyps, seq_ids, seq_lens, seq_scores])
            print(np.array_repr(k1))
            assert k1.size == 4

            expected_top1_for_beam_0 = """
      beam_id: 0
      ids: 1
      ids: 2
      scores: 0.86230338
      scores: 0.65504861
      atten_vecs {
        prob: 0.45372832
        prob: 0.86230338
        prob: 0.65504861
      }
      atten_vecs {
        prob: 0.45372832
        prob: 0.86230338
        prob: 0.65504861
      }
      normalized_score: 1.002714
      """
            expected_top2_for_beam_1 = """
      beam_id: 1
      ids: 3
      ids: 2
      scores: 0.38127339
      scores: 0.57700801
      atten_vecs {
        prob: 0.38612545
        prob: 0.42067075
        prob: 0.84442794
      }
      atten_vecs {
        prob: 0.18693292
        prob: 0.17821217
        prob: 0.66380036
      }
      normalized_score: 0.480028
      """
            self._SameHyp(expected_top1_for_beam_0, k1[0, 0])
            self._SameHyp(expected_top2_for_beam_1, k1[1, 1])

            self.assertAllClose(k2, [[1, 2, 0, 0, 0], [4, 2, 0, 0, 0],
                                     [4, 2, 0, 0, 0], [3, 2, 0, 0, 0]])
            self.assertAllClose(k3, [2, 2, 2, 2])
            self.assertAllClose(k4, [1.002714, 0.684296, 0.522484, 0.480028])