code: # Copyright 2017 The TensorFlow Authors All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  
  r"""A simple demonstration of running VGGish in training mode.
  
  This is intended as a toy example that demonstrates how to use the VGGish model
  definition within a larger model that adds more layers on top, and then train
  the larger model. If you let VGGish train as well, then this allows you to
  fine-tune the VGGish model parameters for your application. If you don't let
  VGGish train, then you use VGGish as a feature extractor for the layers above
  it.
  
  For this toy task, we are training a classifier to distinguish between three
  classes: sine waves, constant signals, and white noise. We generate synthetic
  waveforms from each of these classes, convert into shuffled batches of log mel
  spectrogram examples with associated labels, and feed the batches into a model
  that includes VGGish at the bottom and a couple of additional layers on top. We
  also plumb in labels that are associated with the examples, which feed a label
  loss used for training.
  
  Usage:
    # Run training for 100 steps using a model checkpoint in the default
    # location (vggish_model.ckpt in the current directory). Allow VGGish
    # to get fine-tuned.
    $ python vggish_train_demo.py --num_batches 100
  
    # Same as before but run for fewer steps and don't change VGGish parameters
    # and use a checkpoint in a different location
    $ python vggish_train_demo.py --num_batches 50 \
                                  --train_vggish=False \
                                  --checkpoint /path/to/model/checkpoint
  """
  
  from __future__ import print_function
  
  from random import shuffle
  
  import numpy as np
  import tensorflow as tf
  import vggish_input
  import vggish_params
  import vggish_slim
  
  flags = tf.app.flags
  slim = tf.contrib.slim
  
  flags.DEFINE_integer(
      'num_batches', 30,
      'Number of batches of examples to feed into the model. Each batch is of '
      'variable size and contains shuffled examples of each class of audio.')
  
  flags.DEFINE_boolean(
      'train_vggish', True,
      'If True, allow VGGish parameters to change during training, thus '
      'fine-tuning VGGish. If False, VGGish parameters are fixed, thus using '
      'VGGish as a fixed feature extractor.')
  
  flags.DEFINE_string(
      'checkpoint', 'vggish_model.ckpt',
      'Path to the VGGish checkpoint file.')
  
  FLAGS = flags.FLAGS
  
  _NUM_CLASSES = 3
  
  
  def _get_examples_batch():
    """Returns a shuffled batch of examples of all audio classes.
  
    Note that this is just a toy function because this is a simple demo intended
    to illustrate how the training code might work.
  
    Returns:
      a tuple (features, labels) where features is a NumPy array of shape
      [batch_size, num_frames, num_bands] where the batch_size is variable and
      each row is a log mel spectrogram patch of shape [num_frames, num_bands]
      suitable for feeding VGGish, while labels is a NumPy array of shape
      [batch_size, num_classes] where each row is a multi-hot label vector that
      provides the labels for corresponding rows in features.
    """
    # Make a waveform for each class.
    num_seconds = 5
    sr = 44100  # Sampling rate.
    t = np.linspace(0, num_seconds, int(num_seconds * sr))  # Time axis.
    # Random sine wave.
    freq = np.random.uniform(100, 1000)
    sine = np.sin(2 * np.pi * freq * t)
    # Random constant signal.
    magnitude = np.random.uniform(-1, 1)
    const = magnitude * t
    # White noise.
    noise = np.random.normal(-1, 1, size=t.shape)
  
    # Make examples of each signal and corresponding labels.
    # Sine is class index 0, Const class index 1, Noise class index 2.
    sine_examples = vggish_input.waveform_to_examples(sine, sr)
    sine_labels = np.array([[1, 0, 0]] * sine_examples.shape[0])
    const_examples = vggish_input.waveform_to_examples(const, sr)
    const_labels = np.array([[0, 1, 0]] * const_examples.shape[0])
    noise_examples = vggish_input.waveform_to_examples(noise, sr)
    noise_labels = np.array([[0, 0, 1]] * noise_examples.shape[0])
  
    # Shuffle (example, label) pairs across all classes.
    all_examples = np.concatenate((sine_examples, const_examples, noise_examples))
    all_labels = np.concatenate((sine_labels, const_labels, noise_labels))
    labeled_examples = list(zip(all_examples, all_labels))
    shuffle(labeled_examples)
  
    # Separate and return the features and labels.
    features = [example for (example, _) in labeled_examples]
    labels = [label for (_, label) in labeled_examples]
    return (features, labels)
  
  
  def main(_):
    with tf.Graph().as_default(), tf.Session() as sess:
      # Define VGGish.
      embeddings = vggish_slim.define_vggish_slim(FLAGS.train_vggish)
  
      # Define a shallow classification model and associated training ops on top
      # of VGGish.
      with tf.variable_scope('mymodel'):
        # Add a fully connected layer with 100 units.
        num_units = 100
        fc = slim.fully_connected(embeddings, num_units)
  
        # Add a classifier layer at the end, consisting of parallel logistic
        # classifiers, one per class. This allows for multi-class tasks.
        logits = slim.fully_connected(
            fc, _NUM_CLASSES, activation_fn=None, scope='logits')
        tf.sigmoid(logits, name='prediction')
  
        # Add training ops.
        with tf.variable_scope('train'):
          global_step = tf.Variable(
              0, name='global_step', trainable=False,
              collections=[tf.GraphKeys.GLOBAL_VARIABLES,
                           tf.GraphKeys.GLOBAL_STEP])
  
          # Labels are assumed to be fed as a batch multi-hot vectors, with
          # a 1 in the position of each positive class label, and 0 elsewhere.
          labels = tf.placeholder(
              tf.float32, shape=(None, _NUM_CLASSES), name='labels')
  
          # Cross-entropy label loss.
          xent = tf.nn.sigmoid_cross_entropy_with_logits(
              logits=logits, labels=labels, name='xent')
          loss = tf.reduce_mean(xent, name='loss_op')
          tf.summary.scalar('loss', loss)
  
          # We use the same optimizer and hyperparameters as used to train VGGish.
          optimizer = tf.train.AdamOptimizer(
              learning_rate=vggish_params.LEARNING_RATE,
              epsilon=vggish_params.ADAM_EPSILON)
          optimizer.minimize(loss, global_step=global_step, name='train_op')
  
      # Initialize all variables in the model, and then load the pre-trained
      # VGGish checkpoint.
      sess.run(tf.global_variables_initializer())
      vggish_slim.load_vggish_slim_checkpoint(sess, FLAGS.checkpoint)
  
      # Locate all the tensors and ops we need for the training loop.
      features_tensor = sess.graph.get_tensor_by_name(
          vggish_params.INPUT_TENSOR_NAME)
      labels_tensor = sess.graph.get_tensor_by_name('mymodel/train/labels:0')
      global_step_tensor = sess.graph.get_tensor_by_name(
          'mymodel/train/global_step:0')
      loss_tensor = sess.graph.get_tensor_by_name('mymodel/train/loss_op:0')
      train_op = sess.graph.get_operation_by_name('mymodel/train/train_op')
  
      # The training loop.
      for _ in range(FLAGS.num_batches):
        (features, labels) = _get_examples_batch()
        [num_steps, loss, _] = sess.run(
            [global_step_tensor, loss_tensor, train_op],
            feed_dict={features_tensor: features, labels_tensor: labels})
        print('Step %d: loss %g' % (num_steps, loss))
  
  if __name__ == '__main__':
    tf.app.run()
   # Copyright 2018 The TensorFlow Authors. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import tensorflow as tf  # pylint: disable=g-bad-import-order
  import tensorflow.contrib.eager as tfe  # pylint: disable=g-bad-import-order
  from official.mnist import mnist
  from official.mnist import mnist_eager
  from official.utils.misc import keras_utils
  
  
  def device():
    return "/device:GPU:0" if tfe.num_gpus() else "/device:CPU:0"
  
  
  def data_format():
    return "channels_first" if tfe.num_gpus() else "channels_last"
  
  
  def random_dataset():
    batch_size = 64
    images = tf.random_normal([batch_size, 784])
    labels = tf.random_uniform([batch_size], minval=0, maxval=10, dtype=tf.int32)
    return tf.data.Dataset.from_tensors((images, labels))
  
  
  def train(defun=False):
    model = mnist.create_model(data_format())
    if defun:
      model.call = tfe.defun(model.call)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
    dataset = random_dataset()
    with tf.device(device()):
      mnist_eager.train(model, optimizer, dataset,
                        step_counter=tf.train.get_or_create_global_step())
  
  
  def evaluate(defun=False):
    model = mnist.create_model(data_format())
    dataset = random_dataset()
    if defun:
      model.call = tfe.defun(model.call)
    with tf.device(device()):
      mnist_eager.test(model, dataset)
  
  
  class MNISTTest(tf.test.TestCase):
    """Run tests for MNIST eager loop."""
  
    def setUp(self):
      if not keras_utils.is_v2_0():
        tf.compat.v1.enable_v2_behavior()
      super(MNISTTest, self).setUp()
  
    def test_train(self):
      train(defun=False)
  
    def test_evaluate(self):
      evaluate(defun=False)
  
    def test_train_with_defun(self):
      train(defun=True)
  
    def test_evaluate_with_defun(self):
      evaluate(defun=True)
  
  
  if __name__ == "__main__":
    tf.test.main()
   # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  
  r"""Binary for training a RNN-based classifier for the Quick, Draw! data.
  
  python train_model.py \
    --training_data train_data \
    --eval_data eval_data \
    --model_dir /tmp/quickdraw_model/ \
    --cell_type cudnn_lstm
  
  When running on GPUs using --cell_type cudnn_lstm is much faster.
  
  The expected performance is ~75% in 1.5M steps with the default configuration.
  """
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import argparse
  import ast
  import functools
  import sys
  
  import tensorflow as tf
  
  
  def get_num_classes():
    classes = []
    with tf.gfile.GFile(FLAGS.classes_file, "r") as f:
      classes = [x for x in f]
    num_classes = len(classes)
    return num_classes
  
  
  def get_input_fn(mode, tfrecord_pattern, batch_size):
    """Creates an input_fn that stores all the data in memory.
  
    Args:
     mode: one of tf.contrib.learn.ModeKeys.{TRAIN, INFER, EVAL}
     tfrecord_pattern: path to a TF record file created using create_dataset.py.
     batch_size: the batch size to output.
  
    Returns:
      A valid input_fn for the model estimator.
    """
  
    def _parse_tfexample_fn(example_proto, mode):
      """Parse a single record which is expected to be a tensorflow.Example."""
      feature_to_type = {
          "ink": tf.VarLenFeature(dtype=tf.float32),
          "shape": tf.FixedLenFeature([2], dtype=tf.int64)
      }
      if mode != tf.estimator.ModeKeys.PREDICT:
        # The labels won't be available at inference time, so don't add them
        # to the list of feature_columns to be read.
        feature_to_type["class_index"] = tf.FixedLenFeature([1], dtype=tf.int64)
  
      parsed_features = tf.parse_single_example(example_proto, feature_to_type)
      labels = None
      if mode != tf.estimator.ModeKeys.PREDICT:
        labels = parsed_features["class_index"]
      parsed_features["ink"] = tf.sparse_tensor_to_dense(parsed_features["ink"])
      return parsed_features, labels
  
    def _input_fn():
      """Estimator `input_fn`.
  
      Returns:
        A tuple of:
        - Dictionary of string feature name to `Tensor`.
        - `Tensor` of target labels.
      """
      dataset = tf.data.TFRecordDataset.list_files(tfrecord_pattern)
      if mode == tf.estimator.ModeKeys.TRAIN:
        dataset = dataset.shuffle(buffer_size=10)
      dataset = dataset.repeat()
      # Preprocesses 10 files concurrently and interleaves records from each file.
      dataset = dataset.interleave(
          tf.data.TFRecordDataset,
          cycle_length=10,
          block_length=1)
      dataset = dataset.map(
          functools.partial(_parse_tfexample_fn, mode=mode),
          num_parallel_calls=10)
      dataset = dataset.prefetch(10000)
      if mode == tf.estimator.ModeKeys.TRAIN:
        dataset = dataset.shuffle(buffer_size=1000000)
      # Our inputs are variable length, so pad them.
      dataset = dataset.padded_batch(
          batch_size, padded_shapes=dataset.output_shapes)
      features, labels = dataset.make_one_shot_iterator().get_next()
      return features, labels
  
    return _input_fn
  
  
  def model_fn(features, labels, mode, params):
    """Model function for RNN classifier.
  
    This function sets up a neural network which applies convolutional layers (as
    configured with params.num_conv and params.conv_len) to the input.
    The output of the convolutional layers is given to LSTM layers (as configured
    with params.num_layers and params.num_nodes).
    The final state of the all LSTM layers are concatenated and fed to a fully
    connected layer to obtain the final classification scores.
  
    Args:
      features: dictionary with keys: inks, lengths.
      labels: one hot encoded classes
      mode: one of tf.estimator.ModeKeys.{TRAIN, INFER, EVAL}
      params: a parameter dictionary with the following keys: num_layers,
        num_nodes, batch_size, num_conv, conv_len, num_classes, learning_rate.
  
    Returns:
      ModelFnOps for Estimator API.
    """
  
    def _get_input_tensors(features, labels):
      """Converts the input dict into inks, lengths, and labels tensors."""
      # features[ink] is a sparse tensor that is [8, batch_maxlen, 3]
      # inks will be a dense tensor of [8, maxlen, 3]
      # shapes is [batchsize, 2]
      shapes = features["shape"]
      # lengths will be [batch_size]
      lengths = tf.squeeze(
          tf.slice(shapes, begin=[0, 0], size=[params.batch_size, 1]))
      inks = tf.reshape(features["ink"], [params.batch_size, -1, 3])
      if labels is not None:
        labels = tf.squeeze(labels)
      return inks, lengths, labels
  
    def _add_conv_layers(inks, lengths):
      """Adds convolution layers."""
      convolved = inks
      for i in range(len(params.num_conv)):
        convolved_input = convolved
        if params.batch_norm:
          convolved_input = tf.layers.batch_normalization(
              convolved_input,
              training=(mode == tf.estimator.ModeKeys.TRAIN))
        # Add dropout layer if enabled and not first convolution layer.
        if i > 0 and params.dropout:
          convolved_input = tf.layers.dropout(
              convolved_input,
              rate=params.dropout,
              training=(mode == tf.estimator.ModeKeys.TRAIN))
        convolved = tf.layers.conv1d(
            convolved_input,
            filters=params.num_conv[i],
            kernel_size=params.conv_len[i],
            activation=None,
            strides=1,
            padding="same",
            name="conv1d_%d" % i)
      return convolved, lengths
  
    def _add_regular_rnn_layers(convolved, lengths):
      """Adds RNN layers."""
      if params.cell_type == "lstm":
        cell = tf.nn.rnn_cell.BasicLSTMCell
      elif params.cell_type == "block_lstm":
        cell = tf.contrib.rnn.LSTMBlockCell
      cells_fw = [cell(params.num_nodes) for _ in range(params.num_layers)]
      cells_bw = [cell(params.num_nodes) for _ in range(params.num_layers)]
      if params.dropout > 0.0:
        cells_fw = [tf.contrib.rnn.DropoutWrapper(cell) for cell in cells_fw]
        cells_bw = [tf.contrib.rnn.DropoutWrapper(cell) for cell in cells_bw]
      outputs, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn(
          cells_fw=cells_fw,
          cells_bw=cells_bw,
          inputs=convolved,
          sequence_length=lengths,
          dtype=tf.float32,
          scope="rnn_classification")
      return outputs
  
    def _add_cudnn_rnn_layers(convolved):
      """Adds CUDNN LSTM layers."""
      # Convolutions output [B, L, Ch], while CudnnLSTM is time-major.
      convolved = tf.transpose(convolved, [1, 0, 2])
      lstm = tf.contrib.cudnn_rnn.CudnnLSTM(
          num_layers=params.num_layers,
          num_units=params.num_nodes,
          dropout=params.dropout if mode == tf.estimator.ModeKeys.TRAIN else 0.0,
          direction="bidirectional")
      outputs, _ = lstm(convolved)
      # Convert back from time-major outputs to batch-major outputs.
      outputs = tf.transpose(outputs, [1, 0, 2])
      return outputs
  
    def _add_rnn_layers(convolved, lengths):
      """Adds recurrent neural network layers depending on the cell type."""
      if params.cell_type != "cudnn_lstm":
        outputs = _add_regular_rnn_layers(convolved, lengths)
      else:
        outputs = _add_cudnn_rnn_layers(convolved)
      # outputs is [batch_size, L, N] where L is the maximal sequence length and N
      # the number of nodes in the last layer.
      mask = tf.tile(
          tf.expand_dims(tf.sequence_mask(lengths, tf.shape(outputs)[1]), 2),
          [1, 1, tf.shape(outputs)[2]])
      zero_outside = tf.where(mask, outputs, tf.zeros_like(outputs))
      outputs = tf.reduce_sum(zero_outside, axis=1)
      return outputs
  
    def _add_fc_layers(final_state):
      """Adds a fully connected layer."""
      return tf.layers.dense(final_state, params.num_classes)
  
    # Build the model.
    inks, lengths, labels = _get_input_tensors(features, labels)
    convolved, lengths = _add_conv_layers(inks, lengths)
    final_state = _add_rnn_layers(convolved, lengths)
    logits = _add_fc_layers(final_state)
    # Add the loss.
    cross_entropy = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=labels, logits=logits))
    # Add the optimizer.
    train_op = tf.contrib.layers.optimize_loss(
        loss=cross_entropy,
        global_step=tf.train.get_global_step(),
        learning_rate=params.learning_rate,
        optimizer="Adam",
        # some gradient clipping stabilizes training in the beginning.
        clip_gradients=params.gradient_clipping_norm,
        summaries=["learning_rate", "loss", "gradients", "gradient_norm"])
    # Compute current predictions.
    predictions = tf.argmax(logits, axis=1)
    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions={"logits": logits, "predictions": predictions},
        loss=cross_entropy,
        train_op=train_op,
        eval_metric_ops={"accuracy": tf.metrics.accuracy(labels, predictions)})
  
  
  def create_estimator_and_specs(run_config):
    """Creates an Experiment configuration based on the estimator and input fn."""
    model_params = tf.contrib.training.HParams(
        num_layers=FLAGS.num_layers,
        num_nodes=FLAGS.num_nodes,
        batch_size=FLAGS.batch_size,
        num_conv=ast.literal_eval(FLAGS.num_conv),
        conv_len=ast.literal_eval(FLAGS.conv_len),
        num_classes=get_num_classes(),
        learning_rate=FLAGS.learning_rate,
        gradient_clipping_norm=FLAGS.gradient_clipping_norm,
        cell_type=FLAGS.cell_type,
        batch_norm=FLAGS.batch_norm,
        dropout=FLAGS.dropout)
  
    estimator = tf.estimator.Estimator(
        model_fn=model_fn,
        config=run_config,
        params=model_params)
  
    train_spec = tf.estimator.TrainSpec(input_fn=get_input_fn(
        mode=tf.estimator.ModeKeys.TRAIN,
        tfrecord_pattern=FLAGS.training_data,
        batch_size=FLAGS.batch_size), max_steps=FLAGS.steps)
  
    eval_spec = tf.estimator.EvalSpec(input_fn=get_input_fn(
        mode=tf.estimator.ModeKeys.EVAL,
        tfrecord_pattern=FLAGS.eval_data,
        batch_size=FLAGS.batch_size))
  
    return estimator, train_spec, eval_spec
  
  
  def main(unused_args):
    estimator, train_spec, eval_spec = create_estimator_and_specs(
        run_config=tf.estimator.RunConfig(
            model_dir=FLAGS.model_dir,
            save_checkpoints_secs=300,
            save_summary_steps=100))
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
  
  
  if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.register("type", "bool", lambda v: v.lower() == "true")
    parser.add_argument(
        "--training_data",
        type=str,
        default="",
        help="Path to training data (tf.Example in TFRecord format)")
    parser.add_argument(
        "--eval_data",
        type=str,
        default="",
        help="Path to evaluation data (tf.Example in TFRecord format)")
    parser.add_argument(
        "--classes_file",
        type=str,
        default="",
        help="Path to a file with the classes - one class per line")
    parser.add_argument(
        "--num_layers",
        type=int,
        default=3,
        help="Number of recurrent neural network layers.")
    parser.add_argument(
        "--num_nodes",
        type=int,
        default=128,
        help="Number of node per recurrent network layer.")
    parser.add_argument(
        "--num_conv",
        type=str,
        default="[48, 64, 96]",
        help="Number of conv layers along with number of filters per layer.")
    parser.add_argument(
        "--conv_len",
        type=str,
        default="[5, 5, 3]",
        help="Length of the convolution filters.")
    parser.add_argument(
        "--cell_type",
        type=str,
        default="lstm",
        help="Cell type used for rnn layers: cudnn_lstm, lstm or block_lstm.")
    parser.add_argument(
        "--batch_norm",
        type="bool",
        default="False",
        help="Whether to enable batch normalization or not.")
    parser.add_argument(
        "--learning_rate",
        type=float,
        default=0.0001,
        help="Learning rate used for training.")
    parser.add_argument(
        "--gradient_clipping_norm",
        type=float,
        default=9.0,
        help="Gradient clipping norm used during training.")
    parser.add_argument(
        "--dropout",
        type=float,
        default=0.3,
        help="Dropout used for convolutions and bidi lstm layers.")
    parser.add_argument(
        "--steps",
        type=int,
        default=100000,
        help="Number of training steps.")
    parser.add_argument(
        "--batch_size",
        type=int,
        default=8,
        help="Batch size to use for training/evaluation.")
    parser.add_argument(
        "--model_dir",
        type=str,
        default="",
        help="Path for storing the model checkpoints.")
    parser.add_argument(
        "--self_test",
        type="bool",
        default="False",
        help="Whether to enable batch normalization or not.")
  
    FLAGS, unparsed = parser.parse_known_args()
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
   # Copyright 2017 Google Inc. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  
  """Conversion script for CoNLL checkpoints to DRAGNN SavedModel format.
  
  This script loads and finishes a CoNLL checkpoint, then exports it as a
  SavedModel. It expects that the CoNLL RNN cells have been updated using the
  RNN update script.
  """
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import os
  
  import tensorflow as tf
  from absl import flags
  from dragnn.protos import spec_pb2
  from dragnn.python import dragnn_model_saver_lib as saver_lib
  from dragnn.python import spec_builder
  from google.protobuf import text_format
  
  FLAGS = flags.FLAGS
  
  flags.DEFINE_string('master_spec', None, 'Path to task context with '
                      'inputs and parameters for feature extractors.')
  flags.DEFINE_string('params_path', None, 'Path to trained model parameters.')
  flags.DEFINE_string('export_path', '', 'Output path for exported servo model.')
  flags.DEFINE_string('resource_path', '',
                      'Base directory for resources in the master spec.')
  flags.DEFINE_bool('export_moving_averages', True,
                    'Whether to export the moving average parameters.')
  
  
  def export(master_spec_path, params_path, resource_path, export_path,
             export_moving_averages):
    """Restores a model and exports it in SavedModel form.
  
    This method loads a graph specified by the spec at master_spec_path and the
    params in params_path. It then saves the model in SavedModel format to the
    location specified in export_path.
  
    Args:
      master_spec_path: Path to a proto-text master spec.
      params_path: Path to the parameters file to export.
      resource_path: Path to resources in the master spec.
      export_path: Path to export the SavedModel to.
      export_moving_averages: Whether to export the moving average parameters.
    """
    # Old CoNLL checkpoints did not need a known-word-map. Create a temporary if
    # that file is missing.
    if not tf.gfile.Exists(os.path.join(resource_path, 'known-word-map')):
      with tf.gfile.FastGFile(os.path.join(resource_path, 'known-word-map'),
                              'w') as out_file:
        out_file.write('This file intentionally left blank.')
  
    graph = tf.Graph()
    master_spec = spec_pb2.MasterSpec()
    with tf.gfile.FastGFile(master_spec_path) as fin:
      text_format.Parse(fin.read(), master_spec)
  
    # This is a workaround for an issue where the segmenter master-spec had a
    # spurious resource in it; this resource was not respected in the spec-builder
    # and ended up crashing the saver (since it didn't really exist).
    for component in master_spec.component:
      del component.resource[:]
  
    spec_builder.complete_master_spec(master_spec, None, resource_path)
  
    # Remove '/' if it exists at the end of the export path, ensuring that
    # path utils work correctly.
    stripped_path = export_path.rstrip('/')
    saver_lib.clean_output_paths(stripped_path)
  
    short_to_original = saver_lib.shorten_resource_paths(master_spec)
    saver_lib.export_master_spec(master_spec, graph)
    saver_lib.export_to_graph(master_spec, params_path, stripped_path, graph,
                              export_moving_averages)
    saver_lib.export_assets(master_spec, short_to_original, stripped_path)
  
  
  def main(unused_argv):
    # Run the exporter.
    export(FLAGS.master_spec, FLAGS.params_path, FLAGS.resource_path,
           FLAGS.export_path, FLAGS.export_moving_averages)
    tf.logging.info('Export complete.')
  
  
  if __name__ == '__main__':
    tf.app.run()
   # Copyright 2018 The TensorFlow Authors. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import os
  
  import data_provider
  import numpy as np
  import tensorflow as tf
  from absl import flags
  
  
  class DataProviderTest(tf.test.TestCase):
  
    def setUp(self):
      super(DataProviderTest, self).setUp()
      self.testdata_dir = os.path.join(
          flags.FLAGS.test_srcdir,
          'google3/third_party/tensorflow_models/gan/progressive_gan/testdata/')
  
    def test_normalize_image(self):
      image_np = np.asarray([0, 255, 210], dtype=np.uint8)
      normalized_image = data_provider.normalize_image(tf.constant(image_np))
      self.assertEqual(normalized_image.dtype, tf.float32)
      self.assertEqual(normalized_image.shape.as_list(), [3])
      with self.test_session(use_gpu=True) as sess:
        normalized_image_np = sess.run(normalized_image)
      self.assertNDArrayNear(normalized_image_np, [-1, 1, 0.6470588235], 1.0e-6)
  
    def test_sample_patch_large_patch_returns_upscaled_image(self):
      image_np = np.reshape(np.arange(2 * 2), [2, 2, 1])
      image = tf.constant(image_np, dtype=tf.float32)
      image_patch = data_provider.sample_patch(
          image, patch_height=3, patch_width=3, colors=1)
      with self.test_session(use_gpu=True) as sess:
        image_patch_np = sess.run(image_patch)
      expected_np = np.asarray([[[0.], [0.66666669], [1.]], [[1.33333337], [2.],
                                                             [2.33333349]],
                                [[2.], [2.66666675], [3.]]])
      self.assertNDArrayNear(image_patch_np, expected_np, 1.0e-6)
  
    def test_sample_patch_small_patch_returns_downscaled_image(self):
      image_np = np.reshape(np.arange(3 * 3), [3, 3, 1])
      image = tf.constant(image_np, dtype=tf.float32)
      image_patch = data_provider.sample_patch(
          image, patch_height=2, patch_width=2, colors=1)
      with self.test_session(use_gpu=True) as sess:
        image_patch_np = sess.run(image_patch)
      expected_np = np.asarray([[[0.], [1.5]], [[4.5], [6.]]])
      self.assertNDArrayNear(image_patch_np, expected_np, 1.0e-6)
  
    def test_batch_images(self):
      image_np = np.reshape(np.arange(3 * 3), [3, 3, 1])
      image = tf.constant(image_np, dtype=tf.float32)
      images = data_provider.batch_images(
          image,
          patch_height=2,
          patch_width=2,
          colors=1,
          batch_size=2,
          shuffle=False,
          num_threads=1)
      with self.test_session(use_gpu=True) as sess:
        with tf.contrib.slim.queues.QueueRunners(sess):
          images_np = sess.run(images)
      expected_np = np.asarray([[[[0.], [1.5]], [[4.5], [6.]]], [[[0.], [1.5]],
                                                                 [[4.5], [6.]]]])
      self.assertNDArrayNear(images_np, expected_np, 1.0e-6)
  
    def test_provide_data(self):
      images = data_provider.provide_data(
          'mnist',
          'train',
          dataset_dir=self.testdata_dir,
          batch_size=2,
          shuffle=False,
          patch_height=3,
          patch_width=3,
          colors=1)
      self.assertEqual(images.shape.as_list(), [2, 3, 3, 1])
      with self.test_session(use_gpu=True) as sess:
        with tf.contrib.slim.queues.QueueRunners(sess):
          images_np = sess.run(images)
      self.assertEqual(images_np.shape, (2, 3, 3, 1))
  
    def test_provide_data_from_image_files_a_single_pattern(self):
      file_pattern = os.path.join(self.testdata_dir, '*.jpg')
      images = data_provider.provide_data_from_image_files(
          file_pattern,
          batch_size=2,
          shuffle=False,
          patch_height=3,
          patch_width=3,
          colors=1)
      self.assertEqual(images.shape.as_list(), [2, 3, 3, 1])
      with self.test_session(use_gpu=True) as sess:
        sess.run(tf.local_variables_initializer())
        with tf.contrib.slim.queues.QueueRunners(sess):
          images_np = sess.run(images)
      self.assertEqual(images_np.shape, (2, 3, 3, 1))
  
    def test_provide_data_from_image_files_a_list_of_patterns(self):
      file_pattern = [os.path.join(self.testdata_dir, '*.jpg')]
      images = data_provider.provide_data_from_image_files(
          file_pattern,
          batch_size=2,
          shuffle=False,
          patch_height=3,
          patch_width=3,
          colors=1)
      self.assertEqual(images.shape.as_list(), [2, 3, 3, 1])
      with self.test_session(use_gpu=True) as sess:
        sess.run(tf.local_variables_initializer())
        with tf.contrib.slim.queues.QueueRunners(sess):
          images_np = sess.run(images)
      self.assertEqual(images_np.shape, (2, 3, 3, 1))
  
  
  if __name__ == '__main__':
    tf.test.main()
  
apis: numpy.random.uniform tensorflow.nn.sigmoid_cross_entropy_with_logits tensorflow.placeholder tensorflow.summary.scalar tensorflow.app.run tensorflow.global_variables_initializer tensorflow.sigmoid tensorflow.train.AdamOptimizer tensorflow.reduce_mean tensorflow.variable_scope tensorflow.Graph tensorflow.Session tensorflow.Variable numpy.random.normal numpy.sin numpy.concatenate numpy.array
apis: tensorflow.contrib.eager.defun tensorflow.random_uniform tensorflow.data.Dataset.from_tensors tensorflow.train.GradientDescentOptimizer tensorflow.train.get_or_create_global_step tensorflow.compat.v1.enable_v2_behavior tensorflow.contrib.eager.num_gpus tensorflow.random_normal tensorflow.test.main
apis: tensorflow.sparse_tensor_to_dense tensorflow.VarLenFeature tensorflow.reshape tensorflow.contrib.rnn.DropoutWrapper tensorflow.squeeze tensorflow.slice tensorflow.nn.sparse_softmax_cross_entropy_with_logits tensorflow.train.get_global_step tensorflow.reduce_sum tensorflow.contrib.rnn.stack_bidirectional_dynamic_rnn tensorflow.gfile.GFile tensorflow.estimator.RunConfig tensorflow.FixedLenFeature tensorflow.transpose tensorflow.layers.conv1d tensorflow.shape tensorflow.app.run tensorflow.parse_single_example tensorflow.layers.batch_normalization tensorflow.data.TFRecordDataset.list_files tensorflow.contrib.cudnn_rnn.CudnnLSTM tensorflow.zeros_like tensorflow.layers.dense tensorflow.layers.dropout tensorflow.estimator.Estimator tensorflow.metrics.accuracy tensorflow.argmax tensorflow.estimator.train_and_evaluate
apis: tensorflow.Graph tensorflow.gfile.FastGFile tensorflow.logging.info tensorflow.app.run
apis: tensorflow.contrib.slim.queues.QueueRunners numpy.asarray numpy.arange tensorflow.constant tensorflow.local_variables_initializer tensorflow.test.main