jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _parse_eval_data(self, decoded_tensors):
    """Parses data for evaluation."""
    image_bytes = decoded_tensors['image/encoded']
    image_shape = tf.image.extract_jpeg_shape(image_bytes)

    if self._test_crop:
      image = preprocess_ops.center_crop_image_v2(image_bytes, image_shape)
    else:
      image = tf.image.decode_jpeg(image_bytes, channels=3)
    # This line convert the image to float 0.0 - 1.0
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)

    image = tf.image.resize(
        image, self._output_size, method=tf.image.ResizeMethod.BILINEAR)
    image = tf.reshape(image, [self._output_size[0], self._output_size[1], 3])

    image = tf.clip_by_value(image, 0., 1.)

    # Convert image to self._dtype.
    image = tf.image.convert_image_dtype(image, self._dtype)

    if self._parse_label:
      label = tf.to_int32(decoded_tensors['image/class/label'])
      return image, label

    return image
//  [python]
def _parse_eval_data(self, decoded_tensors):
    """Parses data for evaluation."""
    image_bytes = decoded_tensors['image/encoded']
    image_shape = tf.image.extract_jpeg_shape(image_bytes)

    if self._test_crop:
      image = preprocess_ops.center_crop_image_v2(image_bytes, image_shape)
    else:
      image = tf.image.decode_jpeg(image_bytes, channels=3)
    # This line convert the image to float 0.0 - 1.0
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)

    image = tf.image.resize(
        image, self._output_size, method=tf.image.ResizeMethod.BILINEAR)
    image = tf.reshape(image, [self._output_size[0], self._output_size[1], 3])

    image = tf.clip_by_value(image, 0., 1.)

    # Convert image to self._dtype.
    image = tf.image.convert_image_dtype(image, self._dtype)

    if self._parse_label:
      label = tf.cast(decoded_tensors['image/class/label'], dtype=tf.int32)
      return image, label

    return image
//  [python]
def record_to_xy(example_proto, labels):
  features = {
      'samples': tf.FixedLenSequenceFeature([1], tf.float32, allow_missing=True),
      'label': tf.FixedLenSequenceFeature([], tf.string, allow_missing=True)
  }
  example = tf.parse_single_example(example_proto, features)

  wav = example['samples'][:, 0]
  wav = wav[:16384]
  wav = tf.pad(wav, [[0, 16384 - tf.shape(wav)[0]]])
  wav.set_shape([16384])

  label_chars = example['label']
  # Truncate labels for TIMIT
  label_lens = [len(l) for l in labels]
  if len(set(label_lens)) == 1:
    label_chars = label_chars[:label_lens[0]]

  label = tf.reduce_join(label_chars, 0)
  label_id = tf.constant(0, dtype=tf.int32)
  nmatches = tf.constant(0)

  for i, label_candidate in enumerate(labels):
    match = tf.to_int32(tf.equal(label, label_candidate))
    label_id += i * match
    nmatches += match

  with tf.control_dependencies([tf.assert_equal(nmatches, 1)]):
    return wav, label_id
//  [python]
def record_to_xy(example_proto, labels):
  features = {
      'samples': tf.FixedLenSequenceFeature([1], tf.float32, allow_missing=True),
      'label': tf.FixedLenSequenceFeature([], tf.string, allow_missing=True)
  }
  example = tf.parse_single_example(example_proto, features)

  wav = example['samples'][:, 0]
  wav = wav[:16384]
  wav = tf.pad(wav, [[0, 16384 - tf.shape(wav)[0]]])
  wav.set_shape([16384])

  label_chars = example['label']
  # Truncate labels for TIMIT
  label_lens = [len(l) for l in labels]
  if len(set(label_lens)) == 1:
    label_chars = label_chars[:label_lens[0]]

  label = tf.reduce_join(label_chars, 0)
  label_id = tf.constant(0, dtype=tf.int32)
  nmatches = tf.constant(0)

  for i, label_candidate in enumerate(labels):
    match = tf.cast(tf.equal(label, label_candidate), tf.int32)
    label_id += i * match
    nmatches += match

  with tf.control_dependencies([tf.assert_equal(nmatches, 1)]):
    return wav, label_id
//  [python]
def _apply(self, grad, var, indices=None):
        """
        :param tf.Tensor grad:
        :param tf.Variable|resource_variable_ops.ResourceVariable var:
        :param tf.Tensor|None indices: if this is a sparse update, the indices of the grad values
        :return: update
        :rtype: tf.Tensor|tf.Operation
        """
        lr = t.cast(self._learning_rate_tensor, grad.dtype.base_dtype)
        return self._assign_sub(ref=var, updates=lr * grad, indices=indices).op
//  [python]
def _apply(self, grad, var, indices=None):
        """
        :param tf.Tensor grad:
        :param tf.Variable|resource_variable_ops.ResourceVariable var:
        :param tf.Tensor|None indices: if this is a sparse update, the indices of the grad values
        :return: update
        :rtype: tf.Tensor|tf.Operation
        """
        lr = tf.cast(self._learning_rate_tensor, grad.dtype.base_dtype)
        return self._assign_sub(ref=var, updates=lr * grad, indices=indices).op
//  [python]
def particle_mask(start, end):
  r = tf.range(0, num_particles)
  return t.cast(tf.logical_and(start <= r, r < end), tf.float32)[None, :]
//  [python]
def particle_mask(start, end):
  r = tf.range(0, num_particles)
  return tf.cast(tf.logical_and(start <= r, r < end), tf.float32)[None, :]
//  [python]
def _apply(self, grad, var, indices=None):
        lr = t.cast(self._learning_rate_tensor, var.dtype.base_dtype)
        m = self.get_slot(var, "m")
        # m_t = beta1 * m + (1 - beta1) * g_t
        beta1_t = t.cast(self._beta1_t, var.dtype.base_dtype)
        m_scaled_g_values = grad * (1 - beta1_t)
        m_t = tf_compat.v1.assign(m, m * beta1_t, use_locking=self._use_locking)
        with tf.control_dependencies([m_t]):
            m_t = self._assign_add(m, updates=m_scaled_g_values, indices=indices)
        # update = lr * grad * where(...)
        m_gathered = self._gather(m_t, indices=indices)
        ones = tf.ones_like(grad)
        update = lr * grad * tf.where(tf.equal(tf.sign(m_gathered), tf.sign(grad)), ones, ones * self._decrease_factor)
        var_update = self._assign_sub(ref=var, updates=update, indices=indices)
        return tf.group(*[var_update, m_t])
//  [python]
def _apply(self, grad, var, indices=None):
        lr = tf.cast(self._learning_rate_tensor, var.dtype.base_dtype)
        m = self.get_slot(var, "m")
        # m_t = beta1 * m + (1 - beta1) * g_t
        beta1_t = tf.cast(self._beta1_t, var.dtype.base_dtype)
        m_scaled_g_values = grad * (1 - beta1_t)
        m_t = tf_compat.v1.assign(m, m * beta1_t, use_locking=self._use_locking)
        with tf.control_dependencies([m_t]):
            m_t = self._assign_add(m, updates=m_scaled_g_values, indices=indices)
        # update = lr * grad * where(...)
        m_gathered = self._gather(m_t, indices=indices)
        ones = tf.ones_like(grad)
        update = lr * grad * tf.where(tf.equal(tf.sign(m_gathered), tf.sign(grad)), ones, ones * self._decrease_factor)
        var_update = self._assign_sub(ref=var, updates=update, indices=indices)
        return tf.group(*[var_update, m_t])
//  [python]
def _process_click_ranks(self, click_ranks):
        """
        Return reciprocal click ranks for MRR

        Parameters
        ----------
        click_ranks: Tensor object
            Tensor object containing the ranks of the clicked records for each query

        Returns
        -------
        Tensor object
            Reciprocal ranks tensor
        """
        return math_ops.reciprocal(t.cast(click_ranks, t.float32))
//  [python]
def _process_click_ranks(self, click_ranks):
        """
        Return reciprocal click ranks for MRR

        Parameters
        ----------
        click_ranks: Tensor object
            Tensor object containing the ranks of the clicked records for each query

        Returns
        -------
        Tensor object
            Reciprocal ranks tensor
        """
        return math_ops.reciprocal(tf.cast(click_ranks, tf.float32))
//  [python]
def _process_click_ranks(self, click_ranks):
        """
        Return click ranks for ACR

        Parameters
        ----------
        click_ranks: Tensor object
            Tensor object containing the ranks of the clicked records for each query

        Returns
        -------
        Tensor object
            Ranks tensor cast to float
        """
        return t.cast(click_ranks, t.float32)
//  [python]
def _process_click_ranks(self, click_ranks):
        """
        Return click ranks for ACR

        Parameters
        ----------
        click_ranks: Tensor object
            Tensor object containing the ranks of the clicked records for each query

        Returns
        -------
        Tensor object
            Ranks tensor cast to float
        """
        return tf.cast(click_ranks, tf.float32)
//  [python]
def get_current_step_learning_rate(self):
        """
        :rtype: tf.Tensor
        """
        lr = self.learning_rate_var
        if callable(self.config.typed_dict.get("dynamic_learning_rate")):
            import inspect

            learning_rate_function = self.config.typed_dict.get("dynamic_learning_rate")
            signature = inspect.signature(learning_rate_function)
            assert any(
                [arg.kind == inspect.Parameter.VAR_KEYWORD for arg in signature.parameters.values()]
            ), "please specify **kwargs in dynamic_learning_rate for future compatibility"
            if "epoch" in signature.parameters:
                raise NotImplementedError("TF updater: dynamic_learning_rate with epoch not supported currently")
            lr = learning_rate_function(
                network=self.network, global_train_step=self.global_train_step, learning_rate=lr
            )
        elif self.config.typed_dict.get("dynamic_learning_rate"):
            # To implement any kind of cyclic learning rate during the epoch. E.g.: https://arxiv.org/abs/1608.03983
            with tf.name_scope("dynamic_learning_rate"):
                from returnn.util.basic import CollectionReadCheckCovered

                opts = CollectionReadCheckCovered(self.config.typed_dict["dynamic_learning_rate"])
                # Currently all intervals of same step size.
                interval_steps = tf.constant(opts["interval"], name="interval", dtype=self.global_train_step.dtype)
                step_in_interval = tf_compat.v1.mod(self.global_train_step, interval_steps, name="step_in_interval")
                factor = tf.pow(
                    tf.constant(opts["decay"], name="decay", dtype=tf.float32),
                    tf.cast(step_in_interval, dtype=tf.float32, name="step_in_interval_float"),
                    name="factor",
                )
                lr = lr * factor
                opts.assert_all_read()
        if self.config.is_true("use_horovod") and self.config.is_true("horovod_scale_lr"):
            # noinspection PyPackageRequirements,PyUnresolvedReferences
            import horovod.tensorflow as hvd

            lr = lr * hvd.size()
        return lr
//  [python]
def _pad_mask(self, seqs):
        mask = tf.to_float(self._pad_bool(seqs))
        return mask[:, tf.newaxis, tf.newaxis, :]
//  [python]
def _pad_mask(self, seqs):
        mask = tf.cast(self._pad_bool(seqs), tf.float32)
        return mask[:, tf.newaxis, tf.newaxis, :]
//  [python]
def _process_click_ranks(self, click_ranks):
        """
        Process the click ranks.

        Parameters:
            click_ranks (tf.Tensor): The tensor containing the click ranks.

        Returns:
            tf.Tensor: The processed click ranks.
        """
        return t.cast(click_ranks, t.float32)
//  [python]
def _process_click_ranks(self, click_ranks):
        """
        Process the click ranks.

        Parameters:
            click_ranks (tf.Tensor): The tensor containing the click ranks.

        Returns:
            tf.Tensor: The processed click ranks.
        """
        return tf.cast(click_ranks, tf.float32)
//  [python]
def eval(fps, args):
  import numpy as np

  eval_dir = os.path.join(args.train_dir, 'eval_{}'.format(args.eval_split))
  if not os.path.isdir(eval_dir):
    os.makedirs(eval_dir)

  with tf.name_scope('eval_loader'):
    dataset = tf.data.TFRecordDataset(fps)
    dataset = dataset.map(lambda x: record_to_xy(x, args.data_labels))
    dataset = dataset.apply(tf.contrib.data.batch_and_drop_remainder(args.eval_batch_size))
    iterator = dataset.make_one_shot_iterator()

    x, y = iterator.get_next()

  with tf.variable_scope('classifier'):
    logits = model(x, len(args.data_labels))

  xent = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)
  xent_mean = tf.reduce_mean(xent)

  preds = tf.argmax(logits, axis=1, output_type=tf.int32)
  acc = tf.reduce_mean(tf.to_float(tf.equal(preds, y)))
  step = tf.train.get_or_create_global_step()

  summaries = [
      tf.summary.scalar('acc', acc),
      tf.summary.scalar('xent', xent_mean)
  ]
  summaries = tf.summary.merge(summaries)
  summary_writer = tf.summary.FileWriter(eval_dir)

  saver = tf.train.Saver(max_to_keep=1)

  def eval_ckpt_fp(sess, ckpt_fp):
    saver.restore(sess, ckpt_fp)

    _xents = []
    _accs = []
    while True:
      try:
        _xent, _acc = sess.run([xent_mean, acc])
      except:
        break
      _xents.append(_xent)
      _accs.append(_acc)

    _step = sess.run(step)

    _summaries = sess.run(summaries, {acc: np.mean(_accs), xent_mean: np.mean(_xents)})
    summary_writer.add_summary(_summaries, _step)

    return _step, np.mean(_accs)

  if args.eval_ckpt_fp is not None:
    # Eval one
    with tf.Session() as sess:
      eval_ckpt_fp(sess, args.eval_ckpt_fp)
  else:
    # Loop, waiting for checkpoints
    ckpt_fp = None
    _best_acc = 0.
    while True:
      latest_ckpt_fp = tf.train.latest_checkpoint(args.train_dir)
      if latest_ckpt_fp != ckpt_fp:
        print('Preview: {}'.format(latest_ckpt_fp))

        with tf.Session() as sess:
          _step, _acc = eval_ckpt_fp(sess, latest_ckpt_fp)
          if _acc > _best_acc:
            saver.save(sess, os.path.join(eval_dir, 'best_acc'), _step)
            _best_acc = _acc

        print('Done')

        ckpt_fp = latest_ckpt_fp

      time.sleep(1)
//  [python]
def eval(fps, args):
  import numpy as np

  eval_dir = os.path.join(args.train_dir, 'eval_{}'.format(args.eval_split))
  if not os.path.isdir(eval_dir):
    os.makedirs(eval_dir)

  with tf.name_scope('eval_loader'):
    dataset = tf.data.TFRecordDataset(fps)
    dataset = dataset.map(lambda x: record_to_xy(x, args.data_labels))
    dataset = dataset.apply(tf.contrib.data.batch_and_drop_remainder(args.eval_batch_size))
    iterator = dataset.make_one_shot_iterator()

    x, y = iterator.get_next()

  with tf.variable_scope('classifier'):
    logits = model(x, len(args.data_labels))

  xent = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)
  xent_mean = tf.reduce_mean(xent)

  preds = tf.argmax(logits, axis=1, output_type=tf.int32)
  acc = tf.reduce_mean(tf.cast(tf.equal(preds, y), tf.float32))
  step = tf.train.get_or_create_global_step()

  summaries = [
      tf.summary.scalar('acc', acc),
      tf.summary.scalar('xent', xent_mean)
  ]
  summaries = tf.summary.merge(summaries)
  summary_writer = tf.summary.FileWriter(eval_dir)

  saver = tf.train.Saver(max_to_keep=1)

  def eval_ckpt_fp(sess, ckpt_fp):
    saver.restore(sess, ckpt_fp)

    _xents = []
    _accs = []
    while True:
      try:
        _xent, _acc = sess.run([xent_mean, acc])
      except:
        break
      _xents.append(_xent)
      _accs.append(_acc)

    _step = sess.run(step)

    _summaries = sess.run(summaries, {acc: np.mean(_accs), xent_mean: np.mean(_xents)})
    summary_writer.add_summary(_summaries, _step)

    return _step, np.mean(_accs)

  if args.eval_ckpt_fp is not None:
    # Eval one
    with tf.Session() as sess:
      eval_ckpt_fp(sess, args.eval_ckpt_fp)
  else:
    # Loop, waiting for checkpoints
    ckpt_fp = None
    _best_acc = 0.
    while True:
      latest_ckpt_fp = tf.train.latest_checkpoint(args.train_dir)
      if latest_ckpt_fp != ckpt_fp:
        print('Preview: {}'.format(latest_ckpt_fp))

        with tf.Session() as sess:
          _step, _acc = eval_ckpt_fp(sess, latest_ckpt_fp)
          if _acc > _best_acc:
            saver.save(sess, os.path.join(eval_dir, 'best_acc'), _step)
            _best_acc = _acc

        print('Done')

        ckpt_fp = latest_ckpt_fp

      time.sleep(1)
//  [python]
def update_state(self, y_true, y_pred, mask=None, sample_weight=None):
        """
        Accumulates metric statistics by computing the mean of the
        metric function

        Parameters
        ----------
        y_true : Tensor object
            The ground truth values. Shape : [batch_size, max_sequence_size]
        y_pred : Tensor object
            The predicted values. Shape : [batch_size, max_sequence_size]
        sample_weight : Tensor object
            Optional weighting of each example. Defaults to 1. Can be
            a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns
        -------
            Updated state of the metric

        Notes
        -----
        `y_true` and `y_pred` should have the same shape.
        """
        # Convert predicted ranking scores into ranks for each record per query
        y_pred_ranks = t.cast(
            t.add(
                t.argsort(
                    t.argsort(y_pred, axis=-1, direction="DESCENDING", stable=True), stable=True
                ),
                t.constant(1),
            ),
            t.float32
        )

        # Fetch highest relevance grade from the y_true labels
        y_true_clicks = t.cast(t.equal(y_true, t.math.reduce_max(y_true, axis=-1)[:, t.newaxis]), t.float32)

        # Compute rank of clicked record from predictions
        # Break ties in case of multiple target click labels using the min rank from y_pred_ranks
        click_ranks = t.reduce_min(t.divide(y_pred_ranks, y_true_clicks), axis=-1)

        # Post processing on click ranks before mean
        query_scores = self._process_click_ranks(click_ranks)

        return super().update_state(query_scores, sample_weight=sample_weight)
//  [python]
def update_state(self, y_true, y_pred, mask=None, sample_weight=None):
        """
        Accumulates metric statistics by computing the mean of the
        metric function

        Parameters
        ----------
        y_true : Tensor object
            The ground truth values. Shape : [batch_size, max_sequence_size]
        y_pred : Tensor object
            The predicted values. Shape : [batch_size, max_sequence_size]
        sample_weight : Tensor object
            Optional weighting of each example. Defaults to 1. Can be
            a `Tensor` whose rank is either 0, or the same rank as `y_true`,
            and must be broadcastable to `y_true`.

        Returns
        -------
            Updated state of the metric

        Notes
        -----
        `y_true` and `y_pred` should have the same shape.
        """
        # Convert predicted ranking scores into ranks for each record per query
        y_pred_ranks = tf.cast(
            tf.add(
                tf.argsort(
                    tf.argsort(y_pred, axis=-1, direction="DESCENDING", stable=True), stable=True
                ),
                tf.constant(1),
            ),
            tf.float32
        )

        # Fetch highest relevance grade from the y_true labels
        y_true_clicks = tf.cast(tf.equal(y_true, tf.math.reduce_max(y_true, axis=-1)[:, tf.newaxis]), tf.float32)

        # Compute rank of clicked record from predictions
        # Break ties in case of multiple target click labels using the min rank from y_pred_ranks
        click_ranks = tf.reduce_min(tf.divide(y_pred_ranks, y_true_clicks), axis=-1)

        # Post processing on click ranks before mean
        query_scores = self._process_click_ranks(click_ranks)

        return super().update_state(query_scores, sample_weight=sample_weight)
//  [python]
def scaled_dot_product_attention(self, q, k, v, mask=None):
        dk = tf.to_float(k.shape[-1])
        score = tf.matmul(q, k, transpose_b=True) / (tf.sqrt(dk) + 1e-8)  # [n, h_dim, q_step, step]
        if mask is not None:
            score += mask * -1e9
        self.attention = tf.nn.softmax(score, axis=-1)                               # [n, h, q_step, step]
        context = tf.matmul(self.attention, v)         # [n, h, q_step, step] @ [n, h, step, dv] = [n, h, q_step, dv]
        context = tf.transpose(context, perm=[0, 2, 1, 3])   # [n, q_step, h, dv]
        context = tf.reshape(context, (context.shape[0], context.shape[1], -1))     # [n, q_step, h*dv]
        return context
//  [python]
def scaled_dot_product_attention(self, q, k, v, mask=None):
        dk = tf.cast(k.shape[-1], dtype=tf.float32)
        score = tf.matmul(q, k, transpose_b=True) / (tf.math.sqrt(dk) + 1e-8)  # [n, h_dim, q_step, step]
        if mask is not None:
            score += mask * -1e9
        self.attention = tf.nn.softmax(score, axis=-1)                               # [n, h, q_step, step]
        context = tf.matmul(self.attention, v)         # [n, h, q_step, step] @ [n, h, step, dv] = [n, h, q_step, dv]
        context = tf.transpose(context, perm=[0, 2, 1, 3])   # [n, q_step, h, dv]
        context = tf.reshape(context, (context.shape[0], context.shape[1], -1))     # [n, q_step, h*dv]
        return context
//  [python]
def _observe(self) -> types.NestedTensor:
    context, lbl = eager_utils.get_next(self._data_iterator)
    self._previous_label.assign(self._current_label)
    self._current_label.assign(
        tf.reshape(
            tf.to_float(lbl), shape=[self._batch_size]
        )
    )
    return tf.reshape(
        context,
        shape=[self._batch_size] + self._time_step_spec.observation.shape,
    )
//  [python]
def _observe(self) -> types.NestedTensor:
    context, lbl = eager_utils.get_next(self._data_iterator)
    self._previous_label.assign(self._current_label)
    self._current_label.assign(
        tf.reshape(
            tf.cast(lbl, dtype=self._label_dtype), shape=[self._batch_size]
        )
    )
    return tf.reshape(
        context,
        shape=[self._batch_size] + self._time_step_spec.observation.shape,
    )
//  [python]
def _make_variables(self):
    """make variables."""
    # emb_size must equal to vocab_size,
    # otherwise exceed vocab will be encoded as zeros
    tf.logging.info('using variable dict for embedding')
    self.emb_dict = tf.Variable(
        tf.one_hot(
            list(range(self._params.vocab_size)), depth=self._params.emb_size))
    self.x_emb = tf.nn.embedding_lookup(
        self.emb_dict, tf.to_int64(self.x), name='embx')
//  [python]
def _make_variables(self):
    """make variables."""
    # emb_size must equal to vocab_size,
    # otherwise exceed vocab will be encoded as zeros
    tf.logging.info('using variable dict for embedding')
    self.emb_dict = tf.Variable(
        tf.one_hot(
            list(range(self._params.vocab_size)), depth=self._params.emb_size))
    self.x_emb = tf.nn.embedding_lookup(
        self.emb_dict, tf.cast(self.x, dtype=tf.int64), name='embx')
//  [python]
def _one_hot_encode_x(self):
    """Make embedding layer."""
    # input for encoder
    tf.logging.info('use one hot encoding')
    self.x_emb = tf.one_hot(
        tf.to_int64(self.x), depth=self._params.vocab_size)
    tf.logging.info('shape of x_emb=%s', self.x_emb.shape)
//  [python]
def _one_hot_encode_x(self):
    """Make embedding layer."""
    # input for encoder
    tf.logging.info('use one hot encoding')
    self.x_emb = tf.one_hot(
        tf.cast(self.x, dtype=tf.int64), depth=self._params.vocab_size)
    tf.logging.info('shape of x_emb=%s', self.x_emb.shape)
//  [python]
def undo_info(boxes: tf.Tensor,
              num_detections: int,
              info: tf.Tensor,
              expand: bool = True) -> tf.Tensor:
  """Clip and normalize boxes for serving."""

  mask = tf.sequence_mask(num_detections, maxlen=tf.shape(boxes)[1])
  boxes = t.cast(t.expand_dims(mask, axis=-1), boxes.dtype) * boxes

  if expand:
    info = t.cast(t.expand_dims(info, axis=0), boxes.dtype)
  inshape = t.expand_dims(info[:, 1, :], axis=1)
  ogshape = t.expand_dims(info[:, 0, :], axis=1)
  scale = t.expand_dims(info[:, 2, :], axis=1)
  offset = t.expand_dims(info[:, 3, :], axis=1)

  boxes = box_ops.denormalize_boxes(boxes, inshape)
  boxes += tf.tile(offset, [1, 1, 2])
  boxes /= tf.tile(scale, [1, 1, 2])
  boxes = box_ops.clip_boxes(boxes, ogshape)
  boxes = box_ops.normalize_boxes(boxes, ogshape)
  return boxes
//  [python]
def undo_info(boxes: tf.Tensor,
              num_detections: int,
              info: tf.Tensor,
              expand: bool = True) -> tf.Tensor:
  """Clip and normalize boxes for serving."""

  mask = tf.sequence_mask(num_detections, maxlen=tf.shape(boxes)[1])
  boxes = tf.cast(tf.expand_dims(mask, axis=-1), boxes.dtype) * boxes

  if expand:
    info = tf.cast(tf.expand_dims(info, axis=0), boxes.dtype)
  inshape = tf.expand_dims(info[:, 1, :], axis=1)
  ogshape = tf.expand_dims(info[:, 0, :], axis=1)
  scale = tf.expand_dims(info[:, 2, :], axis=1)
  offset = tf.expand_dims(info[:, 3, :], axis=1)

  boxes = box_ops.denormalize_boxes(boxes, inshape)
  boxes += tf.tile(offset, [1, 1, 2])
  boxes /= tf.tile(scale, [1, 1, 2])
  boxes = box_ops.clip_boxes(boxes, ogshape)
  boxes = box_ops.normalize_boxes(boxes, ogshape)
  return boxes
//  [python]
def train():
    x = tf.placeholder(tf.float32, [None, 784])
    y = tf.placeholder(tf.float32, [None, 10])
    y_outputs = inference(x)
    global_step = tf.Variable(0, trainable=False)

    entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y_outputs, labels=tf.argmax(y, 1))
    loss = tf.reduce_mean(entropy)
    train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=global_step)

    prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_outputs, 1))
    accuracy = tf.reduce_mean(tf.to_float(prediction))

    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(10000):
            x_b, y_b = mnist.train.next_batch(batch_size)
            train_op_, loss_, step = sess.run([train_op, loss, global_step], feed_dict={x: x_b, y: y_b})
            if i % 50 == 0:
                print("training step {0}, loss {1}".format(step, loss_))

                x_b, y_b = mnist.test.images[:500], mnist.test.labels[:500]
                result = sess.run(accuracy, feed_dict={x: x_b, y: y_b})
                print("training step {0},accuracy {1} ".format(step,result))

            saver.save(sess, model_save_path + 'my_model', global_step=global_step)
//  [python]
def train():
    x = tf.placeholder(tf.float32, [None, 784])
    y = tf.placeholder(tf.float32, [None, 10])
    y_outputs = inference(x)
    global_step = tf.Variable(0, trainable=False)

    entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y_outputs, labels=tf.argmax(y, 1))
    loss = tf.reduce_mean(entropy)
    train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=global_step)

    prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_outputs, 1))
    accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32))

    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(10000):
            x_b, y_b = mnist.train.next_batch(batch_size)
            train_op_, loss_, step = sess.run([train_op, loss, global_step], feed_dict={x: x_b, y: y_b})
            if i % 50 == 0:
                print("training step {0}, loss {1}".format(step, loss_))

                x_b, y_b = mnist.test.images[:500], mnist.test.labels[:500]
                result = sess.run(accuracy, feed_dict={x: x_b, y: y_b})
                print("training step {0},accuracy {1} ".format(step,result))

            saver.save(sess, model_save_path + 'my_model', global_step=global_step)
//  [python]
def letterbox(image: tf.Tensor,
              desired_size: List[int],
              letter_box: bool = True) -> Tuple[tf.Tensor, tf.Tensor]:
  """Letter box an image for image serving."""

  with tf.name_scope('letter_box'):
    image_size = t.cast(preprocessing_ops.get_image_shape(image), t.float32)

    scaled_size = t.cast(desired_size, image_size.dtype)
    if letter_box:
      scale = t.minimum(scaled_size[0] / image_size[0],
                         scaled_size[1] / image_size[1])
      scaled_size = t.round(image_size * scale)
    else:
      scale = 1.0

    # Computes 2D image_scale.
    image_scale = scaled_size / image_size
    image_offset = t.cast((desired_size - scaled_size) * 0.5, t.int32)
    offset = (scaled_size - desired_size) * 0.5
    scaled_image = tf.image.resize(
        image, t.cast(scaled_size, t.int32), method='nearest')

    output_image = tf.image.pad_to_bounding_box(scaled_image, image_offset[0],
                                                image_offset[1],
                                                desired_size[0],
                                                desired_size[1])

    image_info = t.stack([
        image_size,
        t.cast(desired_size, dtype=t.float32), image_scale,
        t.cast(offset, t.float32)
    ])
    return output_image, image_info
//  [python]
def letterbox(image: tf.Tensor,
              desired_size: List[int],
              letter_box: bool = True) -> Tuple[tf.Tensor, tf.Tensor]:
  """Letter box an image for image serving."""

  with tf.name_scope('letter_box'):
    image_size = tf.cast(preprocessing_ops.get_image_shape(image), tf.float32)

    scaled_size = tf.cast(desired_size, image_size.dtype)
    if letter_box:
      scale = tf.minimum(scaled_size[0] / image_size[0],
                         scaled_size[1] / image_size[1])
      scaled_size = tf.round(image_size * scale)
    else:
      scale = 1.0

    # Computes 2D image_scale.
    image_scale = scaled_size / image_size
    image_offset = tf.cast((desired_size - scaled_size) * 0.5, tf.int32)
    offset = (scaled_size - desired_size) * 0.5
    scaled_image = tf.image.resize(
        image, tf.cast(scaled_size, tf.int32), method='nearest')

    output_image = tf.image.pad_to_bounding_box(scaled_image, image_offset[0],
                                                image_offset[1],
                                                desired_size[0],
                                                desired_size[1])

    image_info = tf.stack([
        image_size,
        tf.cast(desired_size, dtype=tf.float32), image_scale,
        tf.cast(offset, tf.float32)
    ])
    return output_image, image_info
//  [python]
def NormalizedNx3(v):
    v = tf.reshape(v, (-1, 3))
    ss = tf.reduce_sum(tf.square(v), axis=1)
    # prevent zero division
    indices = tf.equal(ss, 0.)
    mask = tf.cast(indices, ss.dtype)  # a mask, 1 where norms==0, 0 otherwise
    norms = tf.add(ss, mask)
    s = tf.sqrt(norms)
    return tf.reshape(tf.divide(v, col(s)), [-1])
//  [python]
def create_input_data(source_data_file, target_data_file,
                      source_vocab_file, target_vocab_file,
                      batch_size, sos, eos, unk_id,
                      source_max_length, target_max_length):
  source_dataset = tf.data.TextLineDataset(tf.gfile.Glob(source_data_file))
  target_dataset = tf.data.TextLineDataset(tf.gfile.Glob(target_data_file))
  source_vocab = lookup_ops.index_table_from_file(
    source_vocab_file, default_value=unk_id)
  target_vocab = lookup_ops.index_table_from_file(
    target_vocab_file, default_value=unk_id)

  output_buffer_size = batch_size * 1000

  source_eos_id = tf.to_int32(source_vocab.lookup(tf.constant(eos)))
  target_sos_id = tf.to_int32(target_vocab.lookup(tf.constant(sos)))
  target_eos_id = tf.to_int32(target_vocab.lookup(tf.constant(eos)))

  dataset = tf.data.Dataset.zip((source_dataset, target_dataset))
  dataset = dataset.map(
    lambda src, tgt: (tf.string_split([src]).values,
                      tf.string_split([tgt]).values)).prefetch(output_buffer_size)
  dataset = dataset.filter(
    lambda src, tgt: tf.logical_and(tf.size(src) > 0, tf.size(tgt) > 0))
  dataset = dataset.map(
    lambda src, tgt: (src[:source_max_length], tgt[:target_max_length]))
  dataset = dataset.prefetch(output_buffer_size)

  dataset = dataset.map(
    lambda src, tgt: (tf.to_int32(source_vocab.lookup(src)),
                      tf.to_int32(target_vocab.lookup(tgt))))
  dataset = dataset.prefetch(output_buffer_size)

  dataset = dataset.map(
    lambda src, tgt: (tf.reverse(src, axis=[0]),
                      tf.concat(([target_sos_id], tgt), 0),
                      tf.concat((tgt, [target_eos_id]), 0))).prefetch(output_buffer_size)

  dataset = dataset.map(
    lambda src, tgt_in, tgt_out: (
      src, tgt_in, tgt_out, tf.size(src), tf.size(tgt_in))).prefetch(output_buffer_size)

  dataset = dataset.shuffle(100).repeat().padded_batch(
    batch_size,
    padded_shapes=(tf.TensorShape([None]),
                   tf.TensorShape([None]),
                   tf.TensorShape([None]),
                   tf.TensorShape([]),
                   tf.TensorShape([])),
    padding_values=(source_eos_id,
                    target_eos_id,
                    target_eos_id,
                    0,
                    0))

  iterator = dataset.make_initializable_iterator()

  return iterator.get_next(), iterator.initializer, source_vocab, target_vocab
//  [python]
def create_input_data(source_data_file, target_data_file,
                      source_vocab_file, target_vocab_file,
                      batch_size, sos, eos, unk_id,
                      source_max_length, target_max_length):
  source_dataset = tf.data.TextLineDataset(tf.gfile.Glob(source_data_file))
  target_dataset = tf.data.TextLineDataset(tf.gfile.Glob(target_data_file))
  source_vocab = lookup_ops.index_table_from_file(
    source_vocab_file, default_value=unk_id)
  target_vocab = lookup_ops.index_table_from_file(
    target_vocab_file, default_value=unk_id)

  output_buffer_size = batch_size * 1000

  source_eos_id = tf.cast(source_vocab.lookup(tf.constant(eos)), tf.int32)
  target_sos_id = tf.cast(target_vocab.lookup(tf.constant(sos)), tf.int32)
  target_eos_id = tf.cast(target_vocab.lookup(tf.constant(eos)), tf.int32)

  dataset = tf.data.Dataset.zip((source_dataset, target_dataset))
  dataset = dataset.map(
    lambda src, tgt: (tf.string_split([src]).values,
                      tf.string_split([tgt]).values)).prefetch(output_buffer_size)
  dataset = dataset.filter(
    lambda src, tgt: tf.logical_and(tf.size(src) > 0, tf.size(tgt) > 0))
  dataset = dataset.map(
    lambda src, tgt: (src[:source_max_length], tgt[:target_max_length]))
  dataset = dataset.prefetch(output_buffer_size)

  dataset = dataset.map(
    lambda src, tgt: (tf.cast(source_vocab.lookup(src), tf.int32),
                      tf.cast(target_vocab.lookup(tgt), tf.int32)))
  dataset = dataset.prefetch(output_buffer_size)

  dataset = dataset.map(
    lambda src, tgt: (tf.reverse(src, axis=[0]),
                      tf.concat(([target_sos_id], tgt), 0),
                      tf.concat((tgt, [target_eos_id]), 0))).prefetch(output_buffer_size)

  dataset = dataset.map(
    lambda src, tgt_in, tgt_out: (
      src, tgt_in, tgt_out, tf.size(src), tf.size(tgt_in))).prefetch(output_buffer_size)

  dataset = dataset.shuffle(100).repeat().padded_batch(
    batch_size,
    padded_shapes=(tf.TensorShape([None]),
                   tf.TensorShape([None]),
                   tf.TensorShape([None]),
                   tf.TensorShape([]),
                   tf.TensorShape([])),
    padding_values=(source_eos_id,
                    target_eos_id,
                    target_eos_id,
                    0,
                    0))

  iterator = dataset.make_initializable_iterator()

  return iterator.get_next(), iterator.initializer, source_vocab, target_vocab
//  [python]
def _make_summary_stats(self):
    """make summary stats."""
    probs = tf.nn.softmax(self.logits)
    pred_words = tf.argmax(probs, axis=2)
    self.acc_i_t = tf.equal(pred_words, tf.to_int64(self.y))
    self.acc_i = tf.reduce_mean(tf.to_float(self.acc_i_t), axis=1)
    self.acc = tf.reduce_mean(self.acc_i)

    self.summary = tf.summary.merge([
        tf.summary.scalar('loss', self.loss),
        tf.summary.scalar('acc', self.acc),
        tf.summary.scalar('loss_total', self.loss_total),
        tf.summary.scalar('loss_reg', self.loss_reg)
    ])
//  [python]
def _make_summary_stats(self):
    """make summary stats."""
    probs = tf.nn.softmax(self.logits)
    pred_words = tf.argmax(probs, axis=2)
    self.acc_i_t = tf.equal(pred_words, tf.cast(self.y, dtype=tf.int64))
    self.acc_i = tf.reduce_mean(tf.cast(self.acc_i_t, dtype=tf.float32), axis=1)
    self.acc = tf.reduce_mean(self.acc_i)

    self.summary = tf.summary.merge([
        tf.summary.scalar('loss', self.loss),
        tf.summary.scalar('acc', self.acc),
        tf.summary.scalar('loss_total', self.loss_total),
        tf.summary.scalar('loss_reg', self.loss_reg)
    ])
//  [python]
def _make_rnn_model(self):
    """Make rnn model."""
    self.y = tf.to_int64(self.x[:, 1:])
    self.y_emb = tf.one_hot(self.y, depth=self._params.emb_size)
    tf.logging.info('y.shape=%s', self.y.shape)

    lstm_fw_cell_g = contrib_rnn.LayerNormBasicLSTMCell(
        self._params.hidden_lstm_size,
        layer_norm=self._params.norm_lstm,
        dropout_keep_prob=1 - self.dropout_rate)
    lstm_hidden, _ = tf.nn.dynamic_rnn(
        lstm_fw_cell_g, self.x_emb, dtype=tf.float32)
    # stagger two directional vectors so that the backward RNN does not reveal
    # medium.com/@plusepsilon/the-bidirectional-language-model-1f3961d1fb27
    self.logits = tf.layers.dense(
        lstm_hidden[:, :-1, :],
        units=self._params.vocab_size,
        activation=None,
        name='logits')
    tf.logging.info('shape of logits=%s', self.logits.shape)

    # cross entropy
    self.loss_i_t = tf.nn.softmax_cross_entropy_with_logits(
        labels=self.y_emb, logits=self.logits)
    self.loss_i = tf.reduce_mean(self.loss_i_t, axis=1)
//  [python]
def _make_rnn_model(self):
    """Make rnn model."""
    self.y = tf.cast(self.x[:, 1:], dtype=tf.int64)
    self.y_emb = tf.one_hot(self.y, depth=self._params.emb_size)
    tf.logging.info('y.shape=%s', self.y.shape)

    lstm_fw_cell_g = contrib_rnn.LayerNormBasicLSTMCell(
        self._params.hidden_lstm_size,
        layer_norm=self._params.norm_lstm,
        dropout_keep_prob=1 - self.dropout_rate)
    lstm_hidden, _ = tf.nn.dynamic_rnn(
        lstm_fw_cell_g, self.x_emb, dtype=tf.float32)
    # stagger two directional vectors so that the backward RNN does not reveal
    # medium.com/@plusepsilon/the-bidirectional-language-model-1f3961d1fb27
    self.logits = tf.layers.dense(
        lstm_hidden[:, :-1, :],
        units=self._params.vocab_size,
        activation=None,
        name='logits')
    tf.logging.info('shape of logits=%s', self.logits.shape)

    # cross entropy
    self.loss_i_t = tf.nn.softmax_cross_entropy_with_logits(
        labels=self.y_emb, logits=self.logits)
    self.loss_i = tf.reduce_mean(self.loss_i_t, axis=1)
//  [python]
def __call__(self, step):
    with tf.name_scope(self.name or "WarmUp") as name:
      global_step_float = tf.to_float(step)
      warmup_steps_float = tf.to_float(self.warmup_steps)
      warmup_percent_done = global_step_float / warmup_steps_float
      warmup_learning_rate = self.initial_learning_rate * tf.math.pow(
        warmup_percent_done, self.power
      )
      return tf.cond(
        global_step_float < warmup_steps_float,
        lambda: warmup_learning_rate,
        lambda: self.decay_schedule_fn(step - self.warmup_steps),
        name=name,
      )
//  [python]
def __call__(self, step):
    with tf.name_scope(self.name or "WarmUp") as name:
      global_step_float = tf.cast(step, tf.float32)
      warmup_steps_float = tf.cast(self.warmup_steps, tf.float32)
      warmup_percent_done = global_step_float / warmup_steps_float
      warmup_learning_rate = self.initial_learning_rate * tf.math.pow(
        warmup_percent_done, self.power
      )
      return tf.cond(
        global_step_float < warmup_steps_float,
        lambda: warmup_learning_rate,
        lambda: self.decay_schedule_fn(step - self.warmup_steps),
        name=name,
      )
//  [python]
def _project_perturbation(
    perturbation, epsilon, input_image, clip_min=None, clip_max=None
):
    """Project `perturbation` onto L-infinity ball of radius `epsilon`.
    Also project into hypercube such that the resulting adversarial example
    is between clip_min and clip_max, if applicable.
    """

    if clip_min is None or clip_max is None:
        raise NotImplementedError(
            "_project_perturbation currently has clipping " "hard-coded in."
        )

    # Ensure inputs are in the correct range
    with tf.control_dependencies(
        [
            utils_tf.assert_less_equal(
                input_image, t.cast(clip_max, input_image.dtype)
            ),
            utils_tf.assert_greater_equal(
                input_image, t.cast(clip_min, input_image.dtype)
            ),
        ]
    ):
        clipped_perturbation = utils_tf.clip_by_value(perturbation, -epsilon, epsilon)
        new_image = utils_tf.clip_by_value(
            input_image + clipped_perturbation, clip_min, clip_max
        )
        return new_image - input_image
//  [python]
def _project_perturbation(
    perturbation, epsilon, input_image, clip_min=None, clip_max=None
):
    """Project `perturbation` onto L-infinity ball of radius `epsilon`.
    Also project into hypercube such that the resulting adversarial example
    is between clip_min and clip_max, if applicable.
    """

    if clip_min is None or clip_max is None:
        raise NotImplementedError(
            "_project_perturbation currently has clipping " "hard-coded in."
        )

    # Ensure inputs are in the correct range
    with tf.control_dependencies(
        [
            utils_tf.assert_less_equal(
                input_image, tf.cast(clip_max, input_image.dtype)
            ),
            utils_tf.assert_greater_equal(
                input_image, tf.cast(clip_min, input_image.dtype)
            ),
        ]
    ):
        clipped_perturbation = utils_tf.clip_by_value(perturbation, -epsilon, epsilon)
        new_image = utils_tf.clip_by_value(
            input_image + clipped_perturbation, clip_min, clip_max
        )
        return new_image - input_image
//  [python]
def _generate_image_mask(
      self, inputs: tf.Tensor, target_shape: tf.Tensor
  ) -> tf.Tensor:
    """Generates image mask from input image."""
    mask = tf.expand_dims(
        tf.to_float(
            tf.not_equal(tf.reduce_sum(inputs, axis=-1), 0.3)
        ),
        axis=-1,
    )
    mask = tf.image.resize(
        mask, target_shape, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR
    )
    return mask
//  [python]
def _generate_image_mask(
      self, inputs: tf.Tensor, target_shape: tf.Tensor
  ) -> tf.Tensor:
    """Generates image mask from input image."""
    mask = tf.expand_dims(
        tf.cast(
            tf.not_equal(tf.reduce_sum(inputs, axis=-1), 0.3), inputs.dtype
        ),
        axis=-1,
    )
    mask = tf.image.resize(
        mask, target_shape, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR
    )
    return mask
//  [python]
def load_data():
    # Load cats vs dogs dataset, and split into train and validation sets.
    train_dataset, val_dataset = tfds.load(
        "cats_vs_dogs", split=["train[:90%]", "train[90%:]"], as_supervised=True
    )

    resizing = keras.layers.Resizing(
        IMAGE_SIZE[0], IMAGE_SIZE[1], crop_to_aspect_ratio=True
    )

    def preprocess_inputs(image, label):
        image = tf.cast(image, "float32")
        return resizing(image), label

    train_dataset = (
        train_dataset.map(
            preprocess_inputs, num_parallel_calls=tf.data.AUTOTUNE
        )
        .batch(FLAGS.batch_size)
        .prefetch(tf.data.AUTOTUNE)
    )
    val_dataset = (
        val_dataset.map(preprocess_inputs, num_parallel_calls=tf.data.AUTOTUNE)
        .batch(FLAGS.batch_size)
        .cache()
        .prefetch(tf.data.AUTOTUNE)
    )
    return train_dataset, val_dataset
//  [python]
def _smallest_size_at_least(height, width, resize_min):
  """Computes new shape with the smallest side equal to `smallest_side`.

  Computes new shape with the smallest side equal to `smallest_side` while
  preserving the original aspect ratio.

  Args:
    height: an int32 scalar tensor indicating the current height.
    width: an int32 scalar tensor indicating the current width.
    resize_min: A python integer or scalar `Tensor` indicating the size of the
      smallest side after resize.

  Returns:
    new_height: an int32 scalar tensor indicating the new height.
    new_width: an int32 scalar tensor indicating the new width.
  """
  resize_min = t.cast(resize_min, t.float32)

  # Convert to floats to make subsequent calculations go smoothly.
  height, width = t.cast(height, t.float32), t.cast(width, t.float32)

  smaller_dim = t.minimum(height, width)
  scale_ratio = resize_min / smaller_dim

  # Convert back to ints to make heights and widths that TF ops will accept.
  new_height = t.cast(height * scale_ratio, t.int32)
  new_width = t.cast(width * scale_ratio, t.int32)

  return new_height, new_width
//  [python]
def _smallest_size_at_least(height, width, resize_min):
  """Computes new shape with the smallest side equal to `smallest_side`.

  Computes new shape with the smallest side equal to `smallest_side` while
  preserving the original aspect ratio.

  Args:
    height: an int32 scalar tensor indicating the current height.
    width: an int32 scalar tensor indicating the current width.
    resize_min: A python integer or scalar `Tensor` indicating the size of the
      smallest side after resize.

  Returns:
    new_height: an int32 scalar tensor indicating the new height.
    new_width: an int32 scalar tensor indicating the new width.
  """
  resize_min = tf.cast(resize_min, tf.float32)

  # Convert to floats to make subsequent calculations go smoothly.
  height, width = tf.cast(height, tf.float32), tf.cast(width, tf.float32)

  smaller_dim = tf.minimum(height, width)
  scale_ratio = resize_min / smaller_dim

  # Convert back to ints to make heights and widths that TF ops will accept.
  new_height = tf.cast(height * scale_ratio, tf.int32)
  new_width = tf.cast(width * scale_ratio, tf.int32)

  return new_height, new_width
//  [python]
def parse_record(raw_record, is_training, dtype):
  """Parses a record containing a training example of an image.

  The input record is parsed into a label and image, and the image is passed
  through preprocessing steps (cropping, flipping, and so on).

  Args:
    raw_record: scalar Tensor tf.string containing a serialized Example protocol
      buffer.
    is_training: A boolean denoting whether the input is for training.
    dtype: data type to use for images/features.

  Returns:
    Tuple with processed image tensor in a channel-last format and
    one-hot-encoded label tensor.
  """
  image_buffer, label, bbox = parse_example_proto(raw_record)

  image = preprocess_image(
      image_buffer=image_buffer,
      bbox=bbox,
      output_height=DEFAULT_IMAGE_SIZE,
      output_width=DEFAULT_IMAGE_SIZE,
      num_channels=NUM_CHANNELS,
      is_training=is_training)
  image = t.cast(image, dtype)

  # Subtract one so that labels are in [0, 1000), and cast to float32 for
  # Keras model.
  label = t.cast(
      t.cast(t.reshape(label, shape=[1]), dtype=t.int32) - 1,
      dtype=t.float32)
  return image, label
//  [python]
def parse_record(raw_record, is_training, dtype):
  """Parses a record containing a training example of an image.

  The input record is parsed into a label and image, and the image is passed
  through preprocessing steps (cropping, flipping, and so on).

  Args:
    raw_record: scalar Tensor tf.string containing a serialized Example protocol
      buffer.
    is_training: A boolean denoting whether the input is for training.
    dtype: data type to use for images/features.

  Returns:
    Tuple with processed image tensor in a channel-last format and
    one-hot-encoded label tensor.
  """
  image_buffer, label, bbox = parse_example_proto(raw_record)

  image = preprocess_image(
      image_buffer=image_buffer,
      bbox=bbox,
      output_height=DEFAULT_IMAGE_SIZE,
      output_width=DEFAULT_IMAGE_SIZE,
      num_channels=NUM_CHANNELS,
      is_training=is_training)
  image = tf.cast(image, dtype)

  # Subtract one so that labels are in [0, 1000), and cast to float32 for
  # Keras model.
  label = tf.cast(
      tf.cast(tf.reshape(label, shape=[1]), dtype=tf.int32) - 1,
      dtype=tf.float32)
  return image, label