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.
  # ==============================================================================
  
  """Train an entropy coder model."""
  
  import time
  
  import tensorflow as tf
  
  from research.compression.entropy_coder.core import code_loader
  from research.compression.entropy_coder.core import config_helper
  
  # pylint: enable=unused-import
  from research.compression.entropy_coder.model import model_factory
  
  FLAGS = tf.app.flags.FLAGS
  
  # Hardware resources configuration.
  tf.app.flags.DEFINE_string('master', '',
                             """Name of the TensorFlow master to use.""")
  tf.app.flags.DEFINE_string('train_dir', None,
                             """Directory where to write event logs.""")
  tf.app.flags.DEFINE_integer('task', None,
                              """Task id of the replica running the training.""")
  tf.app.flags.DEFINE_integer('ps_tasks', 0, """Number of tasks in the ps job.
                              If 0 no ps job is used.""")
  
  # Model selection and configuration.
  tf.app.flags.DEFINE_string('model', None, """Underlying encoder model.""")
  tf.app.flags.DEFINE_string('model_config', None,
                             """Model config protobuf given as text file.""")
  
  # Training data and parameters configuration.
  tf.app.flags.DEFINE_string('input_config', None,
                             """Path to the training input config file.""")
  tf.app.flags.DEFINE_string('train_config', None,
                             """Path to the training experiment config file.""")
  
  
  def train():
      if FLAGS.train_dir is None:
          raise ValueError('Parameter train_dir must be provided')
      if FLAGS.task is None:
          raise ValueError('Parameter task must be provided')
      if FLAGS.model is None:
          raise ValueError('Parameter model must be provided')
  
      input_config_string = config_helper.GetConfigString(FLAGS.input_config)
      input_config = config_helper.InputConfig(input_config_string)
  
      # Training parameters.
      train_config_string = config_helper.GetConfigString(FLAGS.train_config)
      train_config = config_helper.TrainConfig(train_config_string)
  
      batch_size = train_config.batch_size
      initial_learning_rate = train_config.learning_rate
      decay_rate = train_config.decay_rate
      samples_per_decay = train_config.samples_per_decay
  
      # Parameters for learning-rate decay.
      # The formula is decay_rate ** floor(steps / decay_steps).
      decay_steps = samples_per_decay / batch_size
      decay_steps = max(decay_steps, 1)
  
      first_code = code_loader.ReadFirstCode(input_config.data)
      first_code_height = (
          first_code.features.feature['code_shape'].int64_list.value[0])
      first_code_width = (
          first_code.features.feature['code_shape'].int64_list.value[1])
      max_bit_depth = (
          first_code.features.feature['code_shape'].int64_list.value[2])
      print('Maximum code depth: {}'.format(max_bit_depth))
  
      with tf.Graph().as_default():
          ps_ops = ["Variable", "VariableV2", "AutoReloadVariable", "VarHandleOp"]
          with tf.device(tf.train.replica_device_setter(FLAGS.ps_tasks,
                                                        ps_ops=ps_ops)):
              codes = code_loader.LoadBinaryCode(
                  input_config=input_config,
                  batch_size=batch_size)
              if input_config.unique_code_size:
                  print('Input code size: {} x {}'.format(first_code_height,
                                                          first_code_width))
                  codes.set_shape(
                      [batch_size, first_code_height, first_code_width, max_bit_depth])
              else:
                  codes.set_shape([batch_size, None, None, max_bit_depth])
              codes_effective_shape = tf.shape(codes)
  
              global_step = tf.contrib.framework.create_global_step()
  
              # Apply learning-rate decay.
              learning_rate = tf.train.exponential_decay(
                  learning_rate=initial_learning_rate,
                  global_step=global_step,
                  decay_steps=decay_steps,
                  decay_rate=decay_rate,
                  staircase=True)
              tf.summary.scalar('Learning Rate', learning_rate)
              optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate,
                                                 epsilon=1.0)
  
              # Create the entropy coder model.
              model = model_factory.GetModelRegistry().CreateModel(FLAGS.model)
              model_config_string = config_helper.GetConfigString(FLAGS.model_config)
              model.Initialize(global_step, optimizer, model_config_string)
              model.BuildGraph(codes)
  
              summary_op = tf.summary.merge_all()
  
              # Verify that the model can actually be trained.
              if model.train_op is None:
                  raise ValueError('Input model {} is not trainable'.format(FLAGS.model))
  
              # We disable the summary thread run by Supervisor class by passing
              # summary_op=None. We still pass save_summaries_secs because it is used by
              # the global step counter thread.
              is_chief = (FLAGS.task == 0)
              sv = tf.train.Supervisor(logdir=FLAGS.train_dir,
                                       is_chief=is_chief,
                                       global_step=global_step,
                                       # saver=model.saver,
                                       summary_op=None,
                                       save_summaries_secs=120,
                                       save_model_secs=600,
                                       recovery_wait_secs=30)
  
              sess = sv.PrepareSession(FLAGS.master)
              sv.StartQueueRunners(sess)
  
              step = sess.run(global_step)
              print('Trainer initial step: {}.'.format(step))
  
              # Once everything has been setup properly, save the configs.
              if is_chief:
                  config_helper.SaveConfig(FLAGS.train_dir, 'input_config.json',
                                           input_config_string)
                  config_helper.SaveConfig(FLAGS.train_dir, 'model_config.json',
                                           model_config_string)
                  config_helper.SaveConfig(FLAGS.train_dir, 'train_config.json',
                                           train_config_string)
  
              # Train the model.
              next_summary_time = time.time()
              while not sv.ShouldStop():
                  feed_dict = None
  
                  # Once in a while, update the summaries on the chief worker.
                  if is_chief and next_summary_time < time.time():
                      summary_str = sess.run(summary_op, feed_dict=feed_dict)
                      sv.SummaryComputed(sess, summary_str)
                      next_summary_time = time.time() + sv.save_summaries_secs
                  else:
                      tf_tensors = {
                          'train': model.train_op,
                          'code_length': model.average_code_length
                      }
                      np_tensors = sess.run(tf_tensors, feed_dict=feed_dict)
                      print(np_tensors['code_length'])
  
              sv.Stop()
  
  
  def main(argv=None):  # pylint: disable=unused-argument
      train()
  
  
  if __name__ == '__main__':
      tf.app.run()
   # 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.
  # ==============================================================================
  
  """Base anchor generator.
  
  The job of the anchor generator is to create (or load) a collection
  of bounding boxes to be used as anchors.
  
  Generated anchors are assumed to match some convolutional grid or list of grid
  shapes.  For example, we might want to generate anchors matching an 8x8
  feature map and a 4x4 feature map.  If we place 3 anchors per grid location
  on the first feature map and 6 anchors per grid location on the second feature
  map, then 3*8*8 + 6*4*4 = 288 anchors are generated in total.
  
  To support fully convolutional settings, feature map shapes are passed
  dynamically at generation time.  The number of anchors to place at each location
  is static --- implementations of AnchorGenerator must always be able return
  the number of anchors that it uses per location for each feature map.
  """
  from abc import ABCMeta
  from abc import abstractmethod
  
  import tensorflow as tf
  
  
  class AnchorGenerator(object):
      """Abstract base class for anchor generators."""
      __metaclass__ = ABCMeta
  
      @abstractmethod
      def name_scope(self):
          """Name scope.
  
      Must be defined by implementations.
  
      Returns:
        a string representing the name scope of the anchor generation operation.
      """
          pass
  
      @property
      def check_num_anchors(self):
          """Whether to dynamically check the number of anchors generated.
  
      Can be overridden by implementations that would like to disable this
      behavior.
  
      Returns:
        a boolean controlling whether the Generate function should dynamically
        check the number of anchors generated against the mathematically
        expected number of anchors.
      """
          return True
  
      @abstractmethod
      def num_anchors_per_location(self):
          """Returns the number of anchors per spatial location.
  
      Returns:
        a list of integers, one for each expected feature map to be passed to
        the `generate` function.
      """
          pass
  
      def generate(self, feature_map_shape_list, **params):
          """Generates a collection of bounding boxes to be used as anchors.
  
      TODO(rathodv): remove **params from argument list and make stride and
        offsets (for multiple_grid_anchor_generator) constructor arguments.
  
      Args:
        feature_map_shape_list: list of (height, width) pairs in the format
          [(height_0, width_0), (height_1, width_1), ...] that the generated
          anchors must align with.  Pairs can be provided as 1-dimensional
          integer tensors of length 2 or simply as tuples of integers.
        **params: parameters for anchor generation op
  
      Returns:
        boxes_list: a list of BoxLists each holding anchor boxes corresponding to
          the input feature map shapes.
  
      Raises:
        ValueError: if the number of feature map shapes does not match the length
          of NumAnchorsPerLocation.
      """
          if self.check_num_anchors and (
                  len(feature_map_shape_list) != len(self.num_anchors_per_location())):
              raise ValueError('Number of feature maps is expected to equal the length '
                               'of `num_anchors_per_location`.')
          with tf.name_scope(self.name_scope()):
              anchors_list = self._generate(feature_map_shape_list, **params)
              if self.check_num_anchors:
                  with tf.control_dependencies([
                      self._assert_correct_number_of_anchors(
                          anchors_list, feature_map_shape_list)]):
                      for item in anchors_list:
                          item.set(tf.identity(item.get()))
              return anchors_list
  
      @abstractmethod
      def _generate(self, feature_map_shape_list, **params):
          """To be overridden by implementations.
  
      Args:
        feature_map_shape_list: list of (height, width) pairs in the format
          [(height_0, width_0), (height_1, width_1), ...] that the generated
          anchors must align with.
        **params: parameters for anchor generation op
  
      Returns:
        boxes_list: a list of BoxList, each holding a collection of N anchor
          boxes.
      """
          pass
  
      def _assert_correct_number_of_anchors(self, anchors_list,
                                            feature_map_shape_list):
          """Assert that correct number of anchors was generated.
  
      Args:
        anchors_list: A list of box_list.BoxList object holding anchors generated.
        feature_map_shape_list: list of (height, width) pairs in the format
          [(height_0, width_0), (height_1, width_1), ...] that the generated
          anchors must align with.
      Returns:
        Op that raises InvalidArgumentError if the number of anchors does not
          match the number of expected anchors.
      """
          expected_num_anchors = 0
          actual_num_anchors = 0
          for num_anchors_per_location, feature_map_shape, anchors in zip(
                  self.num_anchors_per_location(), feature_map_shape_list, anchors_list):
              expected_num_anchors += (num_anchors_per_location
                                       * feature_map_shape[0]
                                       * feature_map_shape[1])
              actual_num_anchors += anchors.num_boxes()
          return tf.assert_equal(expected_num_anchors, actual_num_anchors)
   # 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.
  # ==============================================================================
  """Tests for framework.concat_and_slice_regularizers."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import tensorflow as tf
  
  from research.morph_net.framework import concat_and_slice_regularizers
  from research.morph_net.testing import op_regularizer_stub
  
  
  class ConcatAndSliceRegularizersTest(tf.test.TestCase):
  
      def setUp(self):
          self._reg_vec1 = [0.1, 0.3, 0.6, 0.2]
          self._alive_vec1 = [False, True, True, False]
          self._reg_vec2 = [0.2, 0.4, 0.5]
          self._alive_vec2 = [False, True, False]
          self._reg1 = op_regularizer_stub.OpRegularizerStub(self._reg_vec1,
                                                             self._alive_vec1)
          self._reg2 = op_regularizer_stub.OpRegularizerStub(self._reg_vec2,
                                                             self._alive_vec2)
  
      def testConcatRegularizer(self):
          concat_reg = concat_and_slice_regularizers.ConcatRegularizer(
              [self._reg1, self._reg2])
          with self.test_session():
              self.assertAllEqual(self._alive_vec1 + self._alive_vec2,
                                  concat_reg.alive_vector.eval())
              self.assertAllClose(self._reg_vec1 + self._reg_vec2,
                                  concat_reg.regularization_vector.eval(), 1e-5)
  
      def testSliceRegularizer(self):
          concat_reg = concat_and_slice_regularizers.SlicingReferenceRegularizer(
              lambda: self._reg1, 1, 2)
          with self.test_session():
              self.assertAllEqual(self._alive_vec1[1:3],
                                  concat_reg.alive_vector.eval())
              self.assertAllClose(self._reg_vec1[1:3],
                                  concat_reg.regularization_vector.eval(), 1e-5)
  
  
  if __name__ == '__main__':
      tf.test.main()
  
apis: tensorflow.summary.scalar tensorflow.shape tensorflow.app.flags.DEFINE_string tensorflow.app.run tensorflow.summary.merge_all tensorflow.train.AdamOptimizer tensorflow.Graph tensorflow.contrib.framework.create_global_step tensorflow.train.exponential_decay tensorflow.train.Supervisor tensorflow.train.replica_device_setter tensorflow.app.flags.DEFINE_integer
apis: tensorflow.assert_equal
apis: tensorflow.test.main