Example #1
0
    def __model_gradients(variable_scope: tf.VariableScope,
                          transformation_variable_scope: tf.VariableScope,
                          output: tf.Tensor, output_gradient: tf.Tensor):
        trainable_variables = tf.get_collection(
            tf.GraphKeys.TRAINABLE_VARIABLES,
            transformation_variable_scope.name)

        gradients = tf.gradients(output, trainable_variables, output_gradient)

        for gradient in gradients:
            gradient_accumulator = tf.Variable(tf.zeros(
                gradient.get_shape(), gradient.dtype), name="gradient_accumulator")

            tf.add_to_collection(
                '{}/model_gradients'.format(variable_scope.name),
                gradient)
            tf.add_to_collection(
                '{}/model_gradient_accumulators'.format(variable_scope.name),
                gradient_accumulator)
            tf.add_to_collection(
                '{}/update_model_gradient_accumulators'.format(
                    variable_scope.name),
                tf.assign_add(gradient_accumulator, gradient).op)

        with tf.control_dependencies(tf.get_collection(
                "{}/update_model_gradient_accumulators".format(variable_scope.name))):
            # there is no noop
            tf.add(1, 1, "update_model_gradient_accumulators")

        tf.variables_initializer(
            tf.get_collection(
                '{}/model_gradient_accumulators'.format(variable_scope.name)),
            'zero_model_gradient_accumulators')
Example #2
0
    def __init__(self, model_architecture, policy_architecture, batch_size, n_particles, n_timesteps,
                        model_path_to_load_variables, model_path_to_save_variables,
                        policy_path_to_load_variables, policy_path_to_save_variables,
                        tb_path):


        self.batch_size = batch_size

        #Build Graph
            # - define all the vars
            # - start session
            # - initialize vars or load them
            # - later: save vars

        #Define model 
        print ('Defining model..')
        self.model = model_(model_architecture, batch_size=batch_size, n_particles=n_particles)

        #Define policy
        print ('Defining policy..')
        self.policy = policy_(policy_architecture, model=self.model, batch_size=batch_size, n_particles=n_particles, n_timesteps=n_timesteps)

        #Start session
        self.sess = tf.Session()

        
        #For tensorboard
        # train_writer = tf.summary.FileWriter(tb_path, self.sess.graph)
        writer = tf.summary.FileWriter(tb_path, graph=tf.get_default_graph())



        #Init the optimizer params, Im not if this resets all the other params. need to check by loading params
        self.sess.run(tf.global_variables_initializer())

        #Initialize vars or load them
        #Model
        print ('Initializing model..')
        saver = tf.train.Saver(self.model.params_dict)
        if model_path_to_load_variables == '':
            self.sess.run(tf.variables_initializer(self.model.params_list))
        else:
            saver.restore(self.sess, model_path_to_load_variables)
            print ('loaded model variables ' + model_path_to_load_variables)

        #Policy
        print( 'Initializing policy..')
        saver = tf.train.Saver(self.policy.params_dict)
        if policy_path_to_load_variables == '':
            self.sess.run(tf.variables_initializer(self.policy.params_list))
        else:
            saver.restore(self.sess, policy_path_to_load_variables)
            print ('loaded policy variables ' + policy_path_to_load_variables)



        self.model_path_to_save_variables = model_path_to_save_variables
        self.policy_path_to_save_variables = policy_path_to_save_variables

        print ('Init Complete')
Example #3
0
 def test_variable(self):
   with self.test_session() as sess:
     x = tf.Variable(2.0, name="CustomName")
     y = tf.constant(3.0)
     z = x * y
     z_new = ed.copy(z)
     tf.variables_initializer([x]).run()
     self.assertEqual(z_new.eval(), 6.0)
Example #4
0
 def test_swap_tensor_variable(self):
   with self.test_session() as sess:
     x = tf.constant(2.0)
     y = tf.constant(3.0)
     z = x * y
     qx = tf.Variable(4.0, name="CustomName")
     z_new = ed.copy(z, {x: qx})
     tf.variables_initializer([qx]).run()
     self.assertEqual(z_new.eval(), 12.0)
Example #5
0
  def test_scan_gradients(self):
    with self.test_session() as sess:
      a = tf.Variable([1.0, 2.0, 3.0])
      op = tf.scan(lambda a, x: a + x, a)
      copy_op = ed.copy(op)
      gradient = tf.gradients(op, [a])[0]
      copy_gradient = tf.gradients(copy_op, [a])[0]

      tf.variables_initializer([a]).run()
      result_copy, result = sess.run([copy_gradient, gradient])
      self.assertAllClose(result, [3.0, 2.0, 1.0])
      self.assertAllClose(result_copy, [3.0, 2.0, 1.0])
Example #6
0
 def test_local_variable(self):
   with self.test_session() as sess:
     self.assertEquals([], tf.local_variables())
     value0 = 42
     tf.contrib.framework.local_variable(value0)
     value1 = 43
     tf.contrib.framework.local_variable(value1)
     variables = tf.local_variables()
     self.assertEquals(2, len(variables))
     self.assertRaises(tf.OpError, sess.run, variables)
     tf.variables_initializer(variables).run()
     self.assertAllEqual(set([value0, value1]), set(sess.run(variables)))
Example #7
0
 def run_session(self, *args):
     (sess_device,
      model_params,) = args
     
     graphTf = tf.Graph()
     
     with graphTf.as_default():
         with graphTf.device(sess_device): # Throws an error if GPU is specified but not available.
             self._log.print3("=========== Making the CNN graph... ===============")
             cnn3d = Cnn3d()
             with tf.variable_scope("net"):
                 cnn3d.make_cnn_model( *model_params.get_args_for_arch() ) # Creates the network's graph (without optimizer).
                 
         self._log.print3("=========== Compiling the Testing Function ============")
         self._log.print3("=======================================================\n")
         
         cnn3d.setup_ops_n_feeds_to_test( self._log,
                                          self._params.indices_fms_per_pathtype_per_layer_to_save )
         # Create the saver
         saver_all = tf.train.Saver() # saver_net would suffice
         
     with tf.Session( graph=graphTf, config=tf.ConfigProto(log_device_placement=False, device_count={'CPU':999, 'GPU':99}) ) as sessionTf:
         file_to_load_params_from = self._params.get_path_to_load_model_from()
         if file_to_load_params_from is not None: # Load params
             self._log.print3("=========== Loading parameters from specified saved model ===============")
             chkpt_fname = tf.train.latest_checkpoint( file_to_load_params_from ) if os.path.isdir( file_to_load_params_from ) else file_to_load_params_from
             self._log.print3("Loading parameters from:" + str(chkpt_fname))
             try:
                 saver_all.restore(sessionTf, chkpt_fname)
                 self._log.print3("Parameters were loaded.")
             except Exception as e: handle_exception_tf_restore(self._log, e)
             
         else:
             self._ask_user_if_test_with_random() # Asks user whether to continue with randomly initialized model. It exits if no is given.
             self._log.print3("")
             self._log.print3("=========== Initializing network variables  ===============")
             tf.variables_initializer( var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="net") ).run()
             self._log.print3("Model variables were initialized.")
             
             
         self._log.print3("")
         self._log.print3("======================================================")
         self._log.print3("=========== Testing with the CNN model ===============")
         self._log.print3("======================================================\n")
         
         res_code = inferenceWholeVolumes( *( [sessionTf, cnn3d] + self._params.get_args_for_testing() ) )
     
     self._log.print3("")
     self._log.print3("======================================================")
     self._log.print3("=========== Testing session finished =================")
     self._log.print3("======================================================")
Example #8
0
def load_prior(config, sess, saver):
     logging.info('Loading prior model parameters from file ' + os.path.abspath(config.prior_model))
     saver.restore(sess, os.path.abspath(config.prior_model))

     # fill prior variables with the loaded values
     prior_variables = tf.get_collection_ref('prior_variables')
     prior_variables_dict = dict([(v.name, v) for v in prior_variables])
     assign_tensors = []
     with tf.variable_scope('prior'):
         for v in tf.trainable_variables():
             prior_name = 'loss/prior/'+v.name
             prior_variable = prior_variables_dict[prior_name]
             assign_tensors.append(prior_variable.assign(v))
     tf.variables_initializer(prior_variables)
     sess.run(assign_tensors)
Example #9
0
def yolo_eval(yolo_outputs,
              image_shape,
              max_boxes=10,
              score_threshold=.6,
              iou_threshold=.5):
    """Evaluate YOLO model on given input batch and return filtered boxes."""
    box_xy, box_wh, box_confidence, box_class_probs = yolo_outputs
    boxes = yolo_boxes_to_corners(box_xy, box_wh)
    boxes, scores, classes = yolo_filter_boxes(
        boxes, box_confidence, box_class_probs, threshold=score_threshold)

    # Scale boxes back to original image shape.
    height = image_shape[0]
    width = image_shape[1]
    image_dims = K.stack([height, width, height, width])
    image_dims = K.reshape(image_dims, [1, 4])
    boxes = boxes * image_dims

    # TODO: Something must be done about this ugly hack!
    max_boxes_tensor = K.variable(max_boxes, dtype='int32')
    K.get_session().run(tf.variables_initializer([max_boxes_tensor]))
    nms_index = tf.image.non_max_suppression(
        boxes, scores, max_boxes_tensor, iou_threshold=iou_threshold)
    boxes = K.gather(boxes, nms_index)
    scores = K.gather(scores, nms_index)
    classes = K.gather(classes, nms_index)
    return boxes, scores, classes
 def _get_ece(self, ece_op, update_op):
   """Return scalar expected calibration error."""
   with self.test_session() as sess:
     metrics_vars = tf.get_collection(tf.GraphKeys.METRIC_VARIABLES)
     sess.run(tf.variables_initializer(var_list=metrics_vars))
     _ = sess.run(update_op)
   return sess.run(ece_op)
    def initialize_uninitialized(self, sess):
        global_vars          = tf.global_variables()
        is_not_initialized   = sess.run([tf.is_variable_initialized(var) for var in global_vars])
        not_initialized_vars = [v for (v, f) in zip(global_vars, is_not_initialized) if not f]

        if len(not_initialized_vars):
            sess.run(tf.variables_initializer(not_initialized_vars))
  def initialize(self, sess):
    # Initial file lists are empty
    np_paths = []
    ss_paths = []
    # Fresh train directly from ImageNet weights
    print('Loading initial model weights from {:s}'.format(self.pretrained_model))
    variables = tf.global_variables()
    # Initialize all variables first
    sess.run(tf.variables_initializer(variables, name='init'))
    var_keep_dic = self.get_variables_in_checkpoint_file(self.pretrained_model)
    # Get the variables to restore, ignoring the variables to fix
    variables_to_restore = self.net.get_variables_to_restore(variables, var_keep_dic)

    restorer = tf.train.Saver(variables_to_restore)
    restorer.restore(sess, self.pretrained_model)
    print('Loaded.')
    # Need to fix the variables before loading, so that the RGB weights are changed to BGR
    # For VGG16 it also changes the convolutional weights fc6 and fc7 to
    # fully connected weights
    self.net.fix_variables(sess, self.pretrained_model)
    print('Fixed.')
    last_snapshot_iter = 0
    rate = cfg.TRAIN.LEARNING_RATE
    stepsizes = list(cfg.TRAIN.STEPSIZE)

    return rate, last_snapshot_iter, stepsizes, np_paths, ss_paths
Example #13
0
  def run(self, variables=None, use_coordinator=True, *args, **kwargs):
    """A simple wrapper to run inference.

    1. Initialize algorithm via `initialize`.
    2. (Optional) Build a TensorFlow summary writer for TensorBoard.
    3. (Optional) Initialize TensorFlow variables.
    4. (Optional) Start queue runners.
    5. Run `update` for `self.n_iter` iterations.
    6. While running, `print_progress`.
    7. Finalize algorithm via `finalize`.
    8. (Optional) Stop queue runners.

    To customize the way inference is run, run these steps
    individually.

    Args:
      variables: list, optional.
        A list of TensorFlow variables to initialize during inference.
        Default is to initialize all variables (this includes
        reinitializing variables that were already initialized). To
        avoid initializing any variables, pass in an empty list.
      use_coordinator: bool, optional.
        Whether to start and stop queue runners during inference using a
        TensorFlow coordinator. For example, queue runners are necessary
        for batch training with file readers.
      *args:
        Passed into `initialize`.
      **kwargs:
        Passed into `initialize`.
    """
    self.initialize(*args, **kwargs)

    if variables is None:
      init = tf.global_variables_initializer()
    else:
      init = tf.variables_initializer(variables)

    # Feed placeholders in case initialization depends on them.
    feed_dict = {}
    for key, value in six.iteritems(self.data):
      if isinstance(key, tf.Tensor) and "Placeholder" in key.op.type:
        feed_dict[key] = value

    init.run(feed_dict)

    if use_coordinator:
      # Start input enqueue threads.
      self.coord = tf.train.Coordinator()
      self.threads = tf.train.start_queue_runners(coord=self.coord)

    for _ in range(self.n_iter):
      info_dict = self.update()
      self.print_progress(info_dict)

    self.finalize()

    if use_coordinator:
      # Ask threads to stop.
      self.coord.request_stop()
      self.coord.join(self.threads)
Example #14
0
    def initializeOrRestore(self):

        self.ckptDir = os.path.join(self.checkpoint_dir, self.dataset.name)
        self.ckptPrefix = os.path.join(self.ckptDir, self.name, self.name)
        vgg_ckpt_file = os.path.join(self.ckptDir, 'vgg_16', 'vgg_16.ckpt')
        mt_ckpt_file = layers.latest_checkpoint(os.path.join(self.ckptDir, 'mt'))
        # ckpt_file = layers.latest_checkpoint(os.path.join(self.ckptDir, 'vgg_16', 'vgg_16.ckpt'))
        globalVars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)

        if vgg_ckpt_file is not None and tf.train.checkpoint_exists(vgg_ckpt_file):
            varsInCkpt, varsNotInCkpt = layers.scan_checkpoint_for_vars(vgg_ckpt_file, globalVars)
            if len(varsInCkpt) != 0:
                restorationSaver = tf.train.Saver(varsInCkpt)
                self.sess.run(tf.report_uninitialized_variables(var_list=varsInCkpt))
                restorationSaver.restore(self.sess, vgg_ckpt_file)
        else:
            varsNotInCkpt = globalVars

        if mt_ckpt_file is not None and tf.train.checkpoint_exists(mt_ckpt_file):
            varsInCkpt, varsNotInCkpt = layers.scan_checkpoint_for_vars(mt_ckpt_file, varsNotInCkpt)
            varsInCkpt, varsNotInCkpt = layers.replaceVarInListsByName(varsInCkpt, varsNotInCkpt, 'fc6')
            if len(varsInCkpt) != 0:
                restorationSaver = tf.train.Saver(varsInCkpt)
                self.sess.run(tf.report_uninitialized_variables(var_list=varsInCkpt))
                restorationSaver.restore(self.sess, mt_ckpt_file)
        else:
            varsNotInCkpt = globalVars

        self.saver = tf.train.Saver()
        self.sess.run(tf.group(tf.variables_initializer(varsNotInCkpt), tf.local_variables_initializer()))
 def test_expected_calibration_error_with_multiple_data_streams(self):
   """Test expected calibration error when multiple data batches provided."""
   y_true, y_pred = self._get_calibration_placeholders()
   expected_ece_op, update_op = calibration_metrics.expected_calibration_error(
       y_true, y_pred, nbins=2)
   with self.test_session() as sess:
     metrics_vars = tf.get_collection(tf.GraphKeys.METRIC_VARIABLES)
     sess.run(tf.variables_initializer(var_list=metrics_vars))
     # Identical data to test_expected_calibration_error_all_bins_filled,
     # except split over three batches.
     sess.run(
         update_op,
         feed_dict={
             y_pred: np.array([0., 0.2]),
             y_true: np.array([0, 0])
         })
     sess.run(
         update_op,
         feed_dict={
             y_pred: np.array([0.4, 0.5]),
             y_true: np.array([1, 0])
         })
     sess.run(
         update_op, feed_dict={
             y_pred: np.array([1.0]),
             y_true: np.array([1])
         })
   actual_ece = 0.08 + 0.1
   expected_ece = sess.run(expected_ece_op)
   self.assertAlmostEqual(actual_ece, expected_ece)
Example #16
0
def test_op_constant(dtype, diff, sess):
    ops = (SimNeurons(LIF(tau_rc=1), Signal(np.zeros(10)), None),
           SimNeurons(LIF(tau_rc=2 if diff else 1), Signal(np.zeros(10)),
                      None))

    signals = SignalDict(tf.float32, 1)
    const = signals.op_constant(
        [op.neurons for op in ops], [op.J.shape[0] for op in ops],
        "tau_rc", dtype)
    const1 = signals.op_constant(
        [op.neurons for op in ops], [op.J.shape[0] for op in ops],
        "tau_rc", dtype, ndims=1)
    const3 = signals.op_constant(
        [op.neurons for op in ops], [op.J.shape[0] for op in ops],
        "tau_rc", dtype, ndims=3)

    assert const.dtype.base_dtype == dtype

    sess.run(tf.variables_initializer(tf.get_collection("constants")),
             feed_dict=signals.constant_phs)
    x, x1, x3 = sess.run([const, const1, const3])

    if diff:
        assert np.array_equal(x, [[1]] * 10 + [[2]] * 10)
        assert np.array_equal(x[:, 0], x1)
        assert np.array_equal(x, x3[..., 0])
    else:
        assert np.array_equal(x, 1.0)
        assert np.array_equal(x, x1)
        assert np.array_equal(x, x3)
Example #17
0
  def _build_eval_specific_graph(self, iterator, model_fn, params,
                                 record_files_placeholder, num_eval_steps):
    """Builds the part of the model that is specific to evaluation."""

    def build():
      features = iterator.get_next()
      estimator_spec = model_fn(
          features, None, tf.estimator.ModeKeys.EVAL, params)
      run_model_op = tf.group(*(update_op for _, update_op in
                                estimator_spec.eval_metric_ops.values()))
      eval_metric_tensors = {k: tensor for (k, (tensor, _))
                             in estimator_spec.eval_metric_ops.items()}
      return run_model_op, estimator_spec.loss, eval_metric_tensors

    if self._use_while_loop:
      def body(i):
        run_model_op_single_step, _, _ = build()
        with tf.control_dependencies([run_model_op_single_step]):
          return i + 1

      run_model_op = tf.while_loop(lambda i: i < num_eval_steps, body, [0],
                                   parallel_iterations=1)
      loss = None
      eval_metric_tensors = {
          "HR": self._compute_metric_mean(rconst.HR_METRIC_NAME),
          "NDCG": self._compute_metric_mean(rconst.NDCG_METRIC_NAME),
      }
    else:
      run_model_op, loss, eval_metric_tensors = build()

    metric_initializer = tf.variables_initializer(
        tf.get_collection(tf.GraphKeys.METRIC_VARIABLES))
    return self._EvalModelProperties(
        record_files_placeholder, iterator, loss, params["eval_batch_size"],
        run_model_op, eval_metric_tensors, metric_initializer)
Example #18
0
    def _run_initsync(self):
        # tparams = [list(chain(*tp)) for tp in self._tower_params]
        tparams = self._tower_params

        # Check to prevent from unnecessarily re-initializing and
        # synchronizing, i.e. when the model loads the weights.
        for v in chain.from_iterable(tparams):
            if getattr(v, '_keras_initialized', False):
                return

        KB.manual_variable_initialization(True)
        sess = KB.get_session()
        KB.manual_variable_initialization(False)

        # glob_variables = tf.global_variables()
        # sess.run(tf.variables_initializer(glob_variables))

        # Initialize on GPU0 and sync to other GPUs
        init_op = tf.variables_initializer(tparams[0])
        # init_op = tf.variables_initializer(self._tower_params[0])
        # init_op = tf.variables_initializer(self.trainable_weights)
        sess.run(init_op)

        # Important if using model_creator. Not necessary of model instance is
        # reused in which case the model layers are shared between slices
        # and are automatically sync'd.
        sync_op = all_sync_params(tparams, self._gdev_list,
                                  usenccl=self._usenccl)
        sess.run(sync_op)

        for v in chain.from_iterable(tparams):
            v._keras_initialized = True
def style_transfer_train(loss, img_var, initial_lr=3.0, decayed_lr=0.1, decay_lr_at=180, max_iter=200, print_every=50):
    # Create and initialize the Adam optimizer
    lr_var = tf.Variable(initial_lr, name="lr")
    # Create train_op that updates the generated image when run
    with tf.variable_scope("optimizer") as opt_scope:
        train_op = tf.train.AdamOptimizer(lr_var).minimize(loss, var_list=[img_var])
    # Initialize the generated image and optimization variables
    opt_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=opt_scope.name)
    sess.run(tf.variables_initializer([lr_var, img_var] + opt_vars))
    # Create an op that will clamp the image values when run
    clamp_image_op = tf.assign(img_var, tf.clip_by_value(img_var, -1.5, 1.5))

    imgs_in_process = []

    # Hardcoded handcrafted 
    for t in range(max_iter):
        # Take an optimization step to update img_var
        sess.run(train_op)
        if t < decay_lr_at:
            sess.run(clamp_image_op)
        if t == decay_lr_at:
            sess.run(tf.assign(lr_var, decayed_lr))
        if t % print_every == 0:
            print("train step: %d" % t)
            img = sess.run(img_var)
            imgs_in_process.append(img[0])
    print("train step: %d" % t)
    final_img = sess.run(img_var)[0]
    return imgs_in_process, final_img
def yolo_non_max_suppression(scores, boxes, classes, max_boxes = 10, iou_threshold = 0.5):
    """
    Applies Non-max suppression (NMS) to set of boxes

    Arguments:
    scores -- tensor of shape (None,), output of yolo_filter_boxes()
    boxes -- tensor of shape (None, 4), output of yolo_filter_boxes() that have been scaled to the image size (see later)
    classes -- tensor of shape (None,), output of yolo_filter_boxes()
    max_boxes -- integer, maximum number of predicted boxes you'd like
    iou_threshold -- real value, "intersection over union" threshold used for NMS filtering

    Returns:
    scores -- tensor of shape (, None), predicted score for each box
    boxes -- tensor of shape (4, None), predicted box coordinates
    classes -- tensor of shape (, None), predicted class for each box

    """

    max_boxes_tensor = K.variable(max_boxes, dtype='int32')     # tensor to be used in tf.image.non_max_suppression()
    K.get_session().run(tf.variables_initializer([max_boxes_tensor])) # initialize variable max_boxes_tensor

    # Use tf.image.non_max_suppression() to get the list of indices corresponding to boxes you keep
    nms_indices = tf.image.non_max_suppression(boxes,scores,max_boxes_tensor,iou_threshold=iou_threshold)


    # Use K.gather() to select only nms_indices from scores, boxes and classes
    scores = K.gather(scores,nms_indices)
    boxes = K.gather(boxes,nms_indices)
    classes = K.gather(classes,nms_indices)


    return scores, boxes, classes
Example #21
0
    def restore(self, sess, do_restore, at_step=-1):
        saver = self.saver
        name = self.net_name
        out_file = saver['out_file'].replace('\\', '/')
        latest_ckpt = tf.train.get_checkpoint_state(
            self.conf.cachedir, saver['ckpt_file'])
        if not latest_ckpt or not do_restore:
            start_at = 0
            sess.run(tf.variables_initializer(
                PoseTools.get_vars(name)),
                feed_dict=self.fd)
            print("Not loading {:s} variables. Initializing them".format(name))
        else:
            if at_step < 0:
                saver['saver'].restore(sess, latest_ckpt.model_checkpoint_path)
                match_obj = re.match(out_file + '-(\d*)', latest_ckpt.model_checkpoint_path)
                start_at = int(match_obj.group(1)) + 1
            else:
                aa = latest_ckpt.all_model_checkpoint_paths
                model_file = ''
                for a in aa:
                    match_obj = re.match(out_file + '-(\d*)', a)
                    step = int(match_obj.group(1))
                    if step >= at_step:
                        model_file = a
                        break
                saver['saver'].restore(sess, model_file)
                match_obj = re.match(out_file + '-(\d*)', model_file)
                start_at = int(match_obj.group(1)) + 1

        if self.dep_nets:
            self.dep_nets.restore_joint(sess, self.name, self.joint, do_restore)

        return start_at
Example #22
0
    def initialize(self, sess):
        # Initial file lists are empty
        np_paths = []
        ss_paths = []

        variables = tf.global_variables()
        # Initialize all variables first
        sess.run(tf.variables_initializer(variables, name='init'))

        if self.pretrained_model is not None:
            if self.pretrained_model.endswith('.ckpt'):
                # Fresh train directly from ImageNet weights
                print('Loading initial model weights from {:s}'.format(self.pretrained_model))

                var_keep_dic = self.get_variables_in_checkpoint_file(self.pretrained_model)
                # Get the variables to restore, ignoring the variables to fix
                variables_to_restore = self.net.get_variables_to_restore(variables, var_keep_dic)

                restorer = tf.train.Saver(variables_to_restore)
                restorer.restore(sess, self.pretrained_model)
                print('Loaded.')
            else:
                # Restore from checkpoint and meta file
                self.restore_ckpt_from_dir(sess, self.net, self.pretrained_model)
                print('Loaded.')

        last_snapshot_iter = 0
        rate = cfg.TRAIN.LEARNING_RATE
        stepsizes = list(cfg.TRAIN.STEPSIZE)

        return rate, last_snapshot_iter, stepsizes, np_paths, ss_paths
Example #23
0
 def make_init_fn(parameters):
   with tf.device(device):
     init_op = tf.variables_initializer(parameters)
   def init_fn(sess):
     tf.logging.info("Initializing model parameters.")
     sess.run(init_op)
   return init_fn
Example #24
0
 def _run_init_test_vars_op(self):
   test_vars = tf.get_collection(bookkeeper.GraphKeys.TEST_VARIABLES)
   if test_vars:
     if test_vars != self._test_vars:
       self._test_vars = list(test_vars)
       self._test_var_init_op = tf.variables_initializer(test_vars)
     return self._test_var_init_op.run()
Example #25
0
 def adam_variables_initializer(opt, var_list):
     adam_vars = [opt.get_slot(var, name)
                  for name in opt.get_slot_names()
                  for var in var_list]
     if isinstance(opt, tf.train.AdamOptimizer):
         adam_vars.extend(list(opt._get_beta_accumulators()))
     return tf.variables_initializer(adam_vars)
  def __init__(self, session,
                     optimizer,
                     policy_network,
                     state_dim,
                     num_actions,
                     init_exp=0.5,         # initial exploration prob
                     final_exp=0.0,        # final exploration prob
                     anneal_steps=10000,   # N steps for annealing exploration
                     discount_factor=0.99, # discount future rewards
                     reg_param=0.001,      # regularization constants
                     max_gradient=5,       # max gradient norms
                     summary_writer=None,
                     summary_every=100):

    # tensorflow machinery
    self.session        = session
    self.optimizer      = optimizer
    self.summary_writer = summary_writer

    # model components
    self.policy_network = policy_network

    # training parameters
    self.state_dim       = state_dim
    self.num_actions     = num_actions
    self.discount_factor = discount_factor
    self.max_gradient    = max_gradient
    self.reg_param       = reg_param

    # exploration parameters
    self.exploration  = init_exp
    self.init_exp     = init_exp
    self.final_exp    = final_exp
    self.anneal_steps = anneal_steps

    # counters
    self.train_iteration = 0

    # rollout buffer
    self.state_buffer  = []
    self.reward_buffer = []
    self.action_buffer = []

    # record reward history for normalization
    self.all_rewards = []
    self.max_reward_length = 1000000

    # create and initialize variables
    self.create_variables()
    var_lists = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    self.session.run(tf.variables_initializer(var_lists))

    # make sure all variables are initialized
    self.session.run(tf.assert_variables_initialized())

    if self.summary_writer is not None:
      # graph was not available when journalist was created
      self.summary_writer.add_graph(self.session.graph)
      self.summary_every = summary_every
Example #27
0
  def __init__(self, session,
                     optimizer,
                     actor_network,
                     critic_network,
                     state_dim,
                     action_dim,
                     batch_size=32,
                     replay_buffer_size=1000000, # size of replay buffer
                     store_replay_every=1,       # how frequent to store experience
                     discount_factor=0.99,       # discount future rewards
                     target_update_rate=0.01,
                     reg_param=0.01,             # regularization constants
                     max_gradient=5,             # max gradient norms
                     noise_sigma=0.20,
                     noise_theta=0.15,
                     summary_writer=None,
                     summary_every=100):

    # tensorflow machinery
    self.session        = session
    self.optimizer      = optimizer
    self.summary_writer = summary_writer

    # model components
    self.actor_network  = actor_network
    self.critic_network = critic_network
    self.replay_buffer  = ReplayBuffer(buffer_size=replay_buffer_size)

    # training parameters
    self.batch_size         = batch_size
    self.state_dim          = state_dim
    self.action_dim         = action_dim
    self.discount_factor    = discount_factor
    self.target_update_rate = target_update_rate
    self.max_gradient       = max_gradient
    self.reg_param          = reg_param

    # Ornstein-Uhlenbeck noise for exploration
    self.noise_var = tf.Variable(tf.zeros([1, action_dim]))
    noise_random = tf.random_normal([1, action_dim], stddev=noise_sigma)
    self.noise = self.noise_var.assign_sub((noise_theta) * self.noise_var - noise_random)

    # counters
    self.store_replay_every   = store_replay_every
    self.store_experience_cnt = 0
    self.train_iteration      = 0

    # create and initialize variables
    self.create_variables()
    var_lists = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    self.session.run(tf.variables_initializer(var_lists))

    # make sure all variables are initialized
    self.session.run(tf.assert_variables_initialized())

    if self.summary_writer is not None:
      # graph was not available when journalist was created
      self.summary_writer.add_graph(self.session.graph)
      self.summary_every = summary_every
 def initialize_op(self):
   """Returns an op for initializing tensorflow variables."""
   all_vars = self._row_factors + self._col_factors
   all_vars.extend([self._row_gramian, self._col_gramian])
   if self._row_weights is not None:
     assert self._col_weights is not None
     all_vars.extend(self._row_weights + self._col_weights)
   return tf.variables_initializer(all_vars)
Example #29
0
 def set_up_init_ops(variables):
   init_op_list = []
   for variable in list(variables):
     if "train_input" in variable.name:
       init_op_list.append(tf.assign(variable, 1))
       variables.remove(variable)
   init_op_list.append(tf.variables_initializer(variables))
   return init_op_list
Example #30
0
def initialize_uninitialized(sess):
    global_vars          = tf.global_variables()
    is_not_initialized   = sess.run([tf.is_variable_initialized(var) for var in global_vars])
    not_initialized_vars = [v for (v, f) in zip(global_vars, is_not_initialized) if not f]

    print([str(i.name) for i in not_initialized_vars]) # only for testing
    if len(not_initialized_vars):
        sess.run(tf.variables_initializer(not_initialized_vars))
Example #31
0
def run(args, server):
    env = create_env(args.env_id,
                     client_id=str(args.task),
                     remotes=args.remotes)
    trainer = A3C(env, args.task, args.visualise)

    # Variable names that start with "local" are not saved in checkpoints.
    if use_tf12_api:
        variables_to_save = [
            v for v in tf.global_variables() if not v.name.startswith("local")
        ]
        init_op = tf.variables_initializer(variables_to_save)
        init_all_op = tf.global_variables_initializer()
    else:
        variables_to_save = [
            v for v in tf.all_variables() if not v.name.startswith("local")
        ]
        init_op = tf.initialize_variables(variables_to_save)
        init_all_op = tf.initialize_all_variables()
    saver = FastSaver(variables_to_save)

    var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                 tf.get_variable_scope().name)
    logger.info('Trainable vars:')
    for v in var_list:
        logger.info('  %s %s', v.name, v.get_shape())

    def init_fn(ses):
        logger.info("Initializing all parameters.")
        ses.run(init_all_op)

    config = tf.ConfigProto(device_filters=[
        "/job:ps", "/job:worker/task:{}/cpu:0".format(args.task)
    ])
    logdir = os.path.join(args.log_dir, 'train')

    if use_tf12_api:
        summary_writer = tf.summary.FileWriter(logdir + "_%d" % args.task)
    else:
        summary_writer = tf.train.SummaryWriter(logdir + "_%d" % args.task)

    logger.info("Events directory: %s_%s", logdir, args.task)
    sv = tf.train.Supervisor(
        is_chief=(args.task == 0),
        logdir=logdir,
        saver=saver,
        summary_op=None,
        init_op=init_op,
        init_fn=init_fn,
        summary_writer=summary_writer,
        ready_op=tf.report_uninitialized_variables(variables_to_save),
        global_step=trainer.global_step,
        save_model_secs=30,
        save_summaries_secs=30)

    num_global_steps = 100000000

    logger.info(
        "Starting session. If this hangs, we're mostly likely waiting to connect to the parameter server. "
        +
        "One common cause is that the parameter server DNS name isn't resolving yet, or is misspecified."
    )
    with sv.managed_session(server.target,
                            config=config) as sess, sess.as_default():
        trainer.start(sess, summary_writer)
        global_step = sess.run(trainer.global_step)
        logger.info("Starting training at step=%d", global_step)
        while not sv.should_stop() and (not num_global_steps
                                        or global_step < num_global_steps):
            trainer.process(sess)
            global_step = sess.run(trainer.global_step)

    # Ask for all the services to stop.
    sv.stop()
    logger.info('reached %s steps. worker stopped.', global_step)
Example #32
0
    def __init__(self, sess, model, beta, decision_rule, batch_size,
                 confidence, targeted, learning_rate, binary_search_steps,
                 max_iterations, abort_early, initial_const, clip_min,
                 clip_max, num_labels, shape):
        """
    EAD Attack

    Return a tensor that constructs adversarial examples for the given
    input. Generate uses tf.py_func in order to operate over tensors.

    :param sess: a TF session.
    :param model: a cleverhans.model.Model object.
    :param beta: Trades off L2 distortion with L1 distortion: higher
                 produces examples with lower L1 distortion, at the
                 cost of higher L2 (and typically Linf) distortion
    :param decision_rule: EN or L1. Select final adversarial example from
                          all successful examples based on the least
                          elastic-net or L1 distortion criterion.
    :param batch_size: Number of attacks to run simultaneously.
    :param confidence: Confidence of adversarial examples: higher produces
                       examples with larger l2 distortion, but more
                       strongly classified as adversarial.
    :param targeted: boolean controlling the behavior of the adversarial
                     examples produced. If set to False, they will be
                     misclassified in any wrong class. If set to True,
                     they will be misclassified in a chosen target class.
    :param learning_rate: The learning rate for the attack algorithm.
                          Smaller values produce better results but are
                          slower to converge.
    :param binary_search_steps: The number of times we perform binary
                                search to find the optimal tradeoff-
                                constant between norm of the perturbation
                                and confidence of the classification. Set
                                'initial_const' to a large value and fix
                                this param to 1 for speed.
    :param max_iterations: The maximum number of iterations. Setting this
                           to a larger value will produce lower distortion
                           results. Using only a few iterations requires
                           a larger learning rate, and will produce larger
                           distortion results.
    :param abort_early: If true, allows early abort when the total
                        loss starts to increase (greatly speeds up attack,
                        but hurts performance, particularly on ImageNet)
    :param initial_const: The initial tradeoff-constant to use to tune the
                          relative importance of size of the perturbation
                          and confidence of classification.
                          If binary_search_steps is large, the initial
                          constant is not important. A smaller value of
                          this constant gives lower distortion results.
                          For computational efficiency, fix
                          binary_search_steps to 1 and set this param
                          to a large value.
    :param clip_min: (optional float) Minimum input component value.
    :param clip_max: (optional float) Maximum input component value.
    :param num_labels: the number of classes in the model's output.
    :param shape: the shape of the model's input tensor.
    """

        self.sess = sess
        self.TARGETED = targeted
        self.LEARNING_RATE = learning_rate
        self.MAX_ITERATIONS = max_iterations
        self.BINARY_SEARCH_STEPS = binary_search_steps
        self.ABORT_EARLY = abort_early
        self.CONFIDENCE = confidence
        self.initial_const = initial_const
        self.batch_size = batch_size
        self.clip_min = clip_min
        self.clip_max = clip_max
        self.model = model
        self.decision_rule = decision_rule

        self.beta = beta
        self.beta_t = tf.cast(self.beta, tf_dtype)

        self.repeat = binary_search_steps >= 10

        self.shape = shape = tuple([batch_size] + list(shape))

        # these are variables to be more efficient in sending data to tf
        self.timg = tf.Variable(np.zeros(shape), dtype=tf_dtype, name='timg')
        self.newimg = tf.Variable(np.zeros(shape),
                                  dtype=tf_dtype,
                                  name='newimg')
        self.slack = tf.Variable(np.zeros(shape), dtype=tf_dtype, name='slack')
        self.tlab = tf.Variable(np.zeros((batch_size, num_labels)),
                                dtype=tf_dtype,
                                name='tlab')
        self.const = tf.Variable(np.zeros(batch_size),
                                 dtype=tf_dtype,
                                 name='const')

        # and here's what we use to assign them
        self.assign_timg = tf.placeholder(tf_dtype, shape, name='assign_timg')
        self.assign_newimg = tf.placeholder(tf_dtype,
                                            shape,
                                            name='assign_newimg')
        self.assign_slack = tf.placeholder(tf_dtype,
                                           shape,
                                           name='assign_slack')
        self.assign_tlab = tf.placeholder(tf_dtype, (batch_size, num_labels),
                                          name='assign_tlab')
        self.assign_const = tf.placeholder(tf_dtype, [batch_size],
                                           name='assign_const')

        self.global_step = tf.Variable(0, trainable=False)
        self.global_step_t = tf.cast(self.global_step, tf_dtype)

        # Fast Iterative Shrinkage Thresholding
        self.zt = tf.divide(self.global_step_t,
                            self.global_step_t + tf.cast(3, tf_dtype))
        cond1 = tf.cast(
            tf.greater(tf.subtract(self.slack, self.timg), self.beta_t),
            tf_dtype)
        cond2 = tf.cast(
            tf.less_equal(tf.abs(tf.subtract(self.slack, self.timg)),
                          self.beta_t), tf_dtype)
        cond3 = tf.cast(
            tf.less(tf.subtract(self.slack, self.timg),
                    tf.negative(self.beta_t)), tf_dtype)

        upper = tf.minimum(tf.subtract(self.slack, self.beta_t),
                           tf.cast(self.clip_max, tf_dtype))
        lower = tf.maximum(tf.add(self.slack, self.beta_t),
                           tf.cast(self.clip_min, tf_dtype))

        self.assign_newimg = tf.multiply(cond1, upper)
        self.assign_newimg += tf.multiply(cond2, self.timg)
        self.assign_newimg += tf.multiply(cond3, lower)

        self.assign_slack = self.assign_newimg
        self.assign_slack += tf.multiply(self.zt,
                                         self.assign_newimg - self.newimg)

        # --------------------------------
        self.setter = tf.assign(self.newimg, self.assign_newimg)
        self.setter_y = tf.assign(self.slack, self.assign_slack)

        # prediction BEFORE-SOFTMAX of the model
        self.output = model.get_logits(self.newimg)
        self.output_y = model.get_logits(self.slack)

        # distance to the input data
        self.l2dist = reduce_sum(tf.square(self.newimg - self.timg),
                                 list(range(1, len(shape))))
        self.l2dist_y = reduce_sum(tf.square(self.slack - self.timg),
                                   list(range(1, len(shape))))
        self.l1dist = reduce_sum(tf.abs(self.newimg - self.timg),
                                 list(range(1, len(shape))))
        self.l1dist_y = reduce_sum(tf.abs(self.slack - self.timg),
                                   list(range(1, len(shape))))
        self.elasticdist = self.l2dist + tf.multiply(self.l1dist, self.beta_t)
        self.elasticdist_y = self.l2dist_y + tf.multiply(
            self.l1dist_y, self.beta_t)
        if self.decision_rule == 'EN':
            self.crit = self.elasticdist
            self.crit_p = 'Elastic'
        else:
            self.crit = self.l1dist
            self.crit_p = 'L1'

        # compute the probability of the label class versus the maximum other
        real = reduce_sum((self.tlab) * self.output, 1)
        real_y = reduce_sum((self.tlab) * self.output_y, 1)
        other = reduce_max((1 - self.tlab) * self.output - (self.tlab * 10000),
                           1)
        other_y = reduce_max(
            (1 - self.tlab) * self.output_y - (self.tlab * 10000), 1)

        if self.TARGETED:
            # if targeted, optimize for making the other class most likely
            loss1 = tf.maximum(ZERO(), other - real + self.CONFIDENCE)
            loss1_y = tf.maximum(ZERO(), other_y - real_y + self.CONFIDENCE)
        else:
            # if untargeted, optimize for making this class least likely.
            loss1 = tf.maximum(ZERO(), real - other + self.CONFIDENCE)
            loss1_y = tf.maximum(ZERO(), real_y - other_y + self.CONFIDENCE)

        # sum up the losses
        self.loss21 = reduce_sum(self.l1dist)
        self.loss21_y = reduce_sum(self.l1dist_y)
        self.loss2 = reduce_sum(self.l2dist)
        self.loss2_y = reduce_sum(self.l2dist_y)
        self.loss1 = reduce_sum(self.const * loss1)
        self.loss1_y = reduce_sum(self.const * loss1_y)
        self.loss_opt = self.loss1_y + self.loss2_y
        self.loss = self.loss1 + self.loss2 + tf.multiply(
            self.beta_t, self.loss21)

        self.learning_rate = tf.train.polynomial_decay(self.LEARNING_RATE,
                                                       self.global_step,
                                                       self.MAX_ITERATIONS,
                                                       0,
                                                       power=0.5)

        # Setup the optimizer and keep track of variables we're creating
        start_vars = set(x.name for x in tf.global_variables())
        optimizer = tf.train.GradientDescentOptimizer(self.learning_rate)
        self.train = optimizer.minimize(self.loss_opt,
                                        var_list=[self.slack],
                                        global_step=self.global_step)
        end_vars = tf.global_variables()
        new_vars = [x for x in end_vars if x.name not in start_vars]

        # these are the variables to initialize when we run
        self.setup = []
        self.setup.append(self.timg.assign(self.assign_timg))
        self.setup.append(self.tlab.assign(self.assign_tlab))
        self.setup.append(self.const.assign(self.assign_const))

        var_list = [self.global_step] + [self.slack] + [self.newimg] + new_vars
        self.init = tf.variables_initializer(var_list=var_list)
Example #33
0
    def __init__(self, graph_path, target_size=(320, 240), tf_config=None, trt_bool=False):
        self.target_size = target_size

        # load graph
        logger.info('loading graph from %s(default size=%dx%d)' % (graph_path, target_size[0], target_size[1]))
        graph_def = tf.GraphDef()


        if trt_bool is True:
            rt_graph_path = os.path.splitext(graph_path)[0] #import os first
            rt_graph_path = rt_graph_path + "_rt.pb"
            with tf.gfile.GFile(rt_graph_path, 'rb') as f:
                graph_def.ParseFromString(f.read())

        else:
            with tf.gfile.GFile(graph_path, 'rb') as f:
                graph_def.ParseFromString(f.read())

        self.graph = tf.get_default_graph()
        tf.import_graph_def(graph_def, name='TfPoseEstimator')
        if tf_config is None:
            if(trt_bool is True):
                tf_config = tf.ConfigProto(gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.50))
            else:    
                tf_config = tf.ConfigProto()
            tf_config.gpu_options.allow_growth = True
            #sess = tf.Session(config=tf_config)

        self.persistent_sess = tf.Session(graph=self.graph, config=tf_config)

        for ts in [n.name for n in tf.get_default_graph().as_graph_def().node]:
            print(ts)

        self.tensor_image = self.graph.get_tensor_by_name('TfPoseEstimator/image:0')
        self.tensor_output = self.graph.get_tensor_by_name('TfPoseEstimator/Openpose/concat_stage7:0')
        self.tensor_heatMat = self.tensor_output[:, :, :, :19]
        self.tensor_pafMat = self.tensor_output[:, :, :, 19:]
        self.upsample_size = tf.placeholder(dtype=tf.int32, shape=(2,), name='upsample_size')
        self.tensor_heatMat_up = tf.image.resize_area(self.tensor_output[:, :, :, :19], self.upsample_size,
                                                      align_corners=False, name='upsample_heatmat')
        self.tensor_pafMat_up = tf.image.resize_area(self.tensor_output[:, :, :, 19:], self.upsample_size,
                                                     align_corners=False, name='upsample_pafmat')
        if trt_bool is True:
            smoother = Smoother({'data': self.tensor_heatMat_up}, 25, 3.0, 19)
        else:
            smoother = Smoother({'data': self.tensor_heatMat_up}, 25, 3.0)
        gaussian_heatMat = smoother.get_output()

        max_pooled_in_tensor = tf.nn.pool(gaussian_heatMat, window_shape=(3, 3), pooling_type='MAX', padding='SAME')
        self.tensor_peaks = tf.where(tf.equal(gaussian_heatMat, max_pooled_in_tensor), gaussian_heatMat,
                                     tf.zeros_like(gaussian_heatMat))

        self.heatMat = self.pafMat = None

        # warm-up
        self.persistent_sess.run(tf.variables_initializer(
            [v for v in tf.global_variables() if
             v.name.split(':')[0] in [x.decode('utf-8') for x in
                                      self.persistent_sess.run(tf.report_uninitialized_variables())]
             ])
        )
        self.persistent_sess.run(
            [self.tensor_peaks, self.tensor_heatMat_up, self.tensor_pafMat_up],
            feed_dict={
                self.tensor_image: [np.ndarray(shape=(target_size[1], target_size[0], 3), dtype=np.float32)],
                self.upsample_size: [target_size[1], target_size[0]]
            }
        )
        """
        self.persistent_sess.run(
            [self.tensor_peaks, self.tensor_heatMat_up, self.tensor_pafMat_up],
            feed_dict={
                self.tensor_image: [np.ndarray(shape=(target_size[1], target_size[0], 3), dtype=np.float32)],
                self.upsample_size: [target_size[1] // 2, target_size[0] // 2]
            }
        )
        self.persistent_sess.run(
            [self.tensor_peaks, self.tensor_heatMat_up, self.tensor_pafMat_up],
            feed_dict={
                self.tensor_image: [np.ndarray(shape=(target_size[1], target_size[0], 3), dtype=np.float32)],
                self.upsample_size: [target_size[1] // 4, target_size[0] // 4]
            }
        )
        """


        # logs
        if self.tensor_image.dtype == tf.quint8:
            logger.info('quantization mode enabled.')
Example #34
0
 def reinitialize_weights(self, scope_name):
     """Reinitializes the weights of a given layer"""
     variables = tf.contrib.framework.get_variables(scope_name)
     init = tf.variables_initializer(variables)
     self.sess.run(init)
Example #35
0
    def finalize(self, optimizer, optimizer_args=None, *args, **kwargs):
        """Finalizes the network.

        Arguments:
            optimizer: (tf.train.Optimizer) An optimizer class from those available at tf.train.Optimizer.
            optimizer_args: (dict) A dictionary of arguments for the __init__ method of the chosen optimizer.

        Returns: None
        """
        if len(self.layers) == 0:
            raise RuntimeError("Cannot finalize an empty network.")
        if self.finalized:
            raise RuntimeError("Can only finalize a network once.")

        optimizer_args = {} if optimizer_args is None else optimizer_args
        self.optimizer = optimizer(**optimizer_args)

        # Add variance output.
        self.layers[-1].set_output_dim(2 * self.layers[-1].get_output_dim())

        # Remove last activation to isolate variance from activation function.
        self.end_act = self.layers[-1].get_activation()
        self.end_act_name = self.layers[-1].get_activation(as_func=False)
        self.layers[-1].unset_activation()

        # Construct all variables.
        with self.sess.as_default():
            with tf.variable_scope(self.name):
                self.scaler = TensorStandardScaler(
                    self.layers[0].get_input_dim())
                self.max_logvar = tf.Variable(
                    np.ones([1, self.layers[-1].get_output_dim() // 2]) / 2.,
                    dtype=tf.float32,
                    name="max_log_var")
                self.min_logvar = tf.Variable(
                    -np.ones([1, self.layers[-1].get_output_dim() // 2]) * 10.,
                    dtype=tf.float32,
                    name="min_log_var")
                for i, layer in enumerate(self.layers):
                    with tf.variable_scope("Layer%i" % i):
                        layer.construct_vars()
                        self.decays.extend(layer.get_decays())
                        self.optvars.extend(layer.get_vars())
        self.optvars.extend([self.max_logvar, self.min_logvar])
        self.nonoptvars.extend(self.scaler.get_vars())

        # Set up training
        with tf.variable_scope(self.name):
            self.optimizer = optimizer(**optimizer_args)
            self.sy_train_in = tf.placeholder(
                dtype=tf.float32,
                shape=[self.num_nets, None, self.layers[0].get_input_dim()],
                name="training_inputs")
            self.sy_train_targ = tf.placeholder(
                dtype=tf.float32,
                shape=[
                    self.num_nets, None, self.layers[-1].get_output_dim() // 2
                ],
                name="training_targets")
            train_loss = tf.reduce_sum(
                self._compile_losses(self.sy_train_in,
                                     self.sy_train_targ,
                                     inc_var_loss=True))
            train_loss += tf.add_n(self.decays)
            train_loss += 0.01 * tf.reduce_sum(
                self.max_logvar) - 0.01 * tf.reduce_sum(self.min_logvar)
            self.mse_loss = self._compile_losses(self.sy_train_in,
                                                 self.sy_train_targ,
                                                 inc_var_loss=False)

            self.train_op = self.optimizer.minimize(train_loss,
                                                    var_list=self.optvars)

        # Initialize all variables
        self.sess.run(
            tf.variables_initializer(self.optvars + self.nonoptvars +
                                     self.optimizer.variables()))

        # Set up prediction
        with tf.variable_scope(self.name):
            self.sy_pred_in2d = tf.placeholder(
                dtype=tf.float32,
                shape=[None, self.layers[0].get_input_dim()],
                name="2D_training_inputs")
            self.sy_pred_mean2d_fac, self.sy_pred_var2d_fac = \
                self.create_prediction_tensors(self.sy_pred_in2d, factored=True)
            self.sy_pred_mean2d = tf.reduce_mean(self.sy_pred_mean2d_fac,
                                                 axis=0)
            self.sy_pred_var2d = tf.reduce_mean(self.sy_pred_var2d_fac, axis=0) + \
                tf.reduce_mean(tf.square(self.sy_pred_mean2d_fac - self.sy_pred_mean2d), axis=0)

            self.sy_pred_in3d = tf.placeholder(
                dtype=tf.float32,
                shape=[self.num_nets, None, self.layers[0].get_input_dim()],
                name="3D_training_inputs")
            self.sy_pred_mean3d_fac, self.sy_pred_var3d_fac = \
                self.create_prediction_tensors(self.sy_pred_in3d, factored=True)

        # Load model if needed
        if self.model_loaded:
            with self.sess.as_default():
                params_dict = loadmat(
                    os.path.join(self.model_dir, "%s.mat" % self.name))
                all_vars = self.nonoptvars + self.optvars
                for i, var in enumerate(all_vars):
                    var.load(params_dict[str(i)])
        self.finalized = True
def style_transfer(content_image, style_image, image_size, style_size, content_layer, content_weight,
                   style_layers, style_weights, tv_weight, model, sess, out_path, init_random = False, 
                   max_iter = 100):
    """Run style transfer!
    
    Inputs:
    - content_image: filename of content image
    - style_image: filename of style image
    - image_size: size of smallest image dimension (used for content loss and generated image)
    - style_size: size of smallest style image dimension
    - content_layer: layer to use for content loss
    - content_weight: weighting on content loss
    - style_layers: list of layers to use for style loss
    - style_weights: list of weights to use for each layer in style_layers
    - tv_weight: weight of total variation regularization term
    - init_random: initialize the starting image to uniform random noise
    """
    # Extract features from the content image
    content_img = preprocess_image(load_image(content_image, size=image_size))
    feats = model.extract_features(model.image)
    content_target = sess.run(feats[content_layer],
                              {model.image: content_img[None]})

    # Extract features from the style image
    style_img = preprocess_image(load_image(style_image, size=style_size))
    style_feat_vars = [feats[idx] for idx in style_layers]
    style_target_vars = []
    # Compute list of TensorFlow Gram matrices
    for style_feat_var in style_feat_vars:
        style_target_vars.append(gram_matrix(style_feat_var))
    # Compute list of NumPy Gram matrices by evaluating the TensorFlow graph on the style image
    style_targets = sess.run(style_target_vars, {model.image: style_img[None]})

    # Initialize generated image to content image
    
    if init_random:
        img_var = tf.Variable(tf.random_uniform(content_img[None].shape, 0, 1), name="image")
    else:
        img_var = tf.Variable(content_img[None], name="image")

    # Extract features on generated image
    feats = model.extract_features(img_var)
    # Compute loss
    c_loss = content_loss(content_weight, feats[content_layer], content_target)
    s_loss = style_loss(feats, style_layers, style_targets, style_weights)
    t_loss = tv_loss(img_var, tv_weight)
    loss = c_loss + s_loss + t_loss
    
    # Set up optimization hyperparameters
    initial_lr = 3.0
    decayed_lr = 0.1
    decay_lr_at = 180
    

    # Create and initialize the Adam optimizer
    lr_var = tf.Variable(initial_lr, name="lr")
    # Create train_op that updates the generated image when run
    with tf.variable_scope("optimizer") as opt_scope:
        train_op = tf.train.AdamOptimizer(lr_var).minimize(loss, var_list=[img_var])
    # Initialize the generated image and optimization variables
    opt_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=opt_scope.name)
    sess.run(tf.variables_initializer([lr_var, img_var] + opt_vars))
    # Create an op that will clamp the image values when run
    clamp_image_op = tf.assign(img_var, tf.clip_by_value(img_var, -1.5, 1.5))
    


    # Hardcoded handcrafted 
    for t in range(max_iter):
        # Take an optimization step to update img_var
        sess.run(train_op)
        if t < decay_lr_at:
            sess.run(clamp_image_op)
        if t == decay_lr_at:
            sess.run(tf.assign(lr_var, decayed_lr))

    img = sess.run(img_var)        
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    plt.axis('off')
    plt.imshow(deprocess_image(img[0], rescale=True))
    extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
    if out_path is not None:
        plt.savefig(out_path, bbox_inches=extent, pad_inches=0)
    else:
        out_path = re.split("/|\.", content_image)[-2]
        out_path = "output/" + out_path + "_out.jpg"
        plt.savefig(out_path, bbox_inches=extent, pad_inches=0)
Example #37
0
File: net.py Project: czgdp1807/wql
    def _build(self, convnet_pars):
        with tf.variable_scope(None, default_name=self._name):
            self._scope_name = tf.get_default_graph().get_name_scope() + '/'

            with tf.variable_scope('State'):
                self._x = tf.placeholder(tf.float32,
                                         shape=[None] + list(
                                             convnet_pars['input_shape']),
                                         name='input')

            with tf.variable_scope('Action'):
                self._action = tf.placeholder('uint8', [None], name='action')

                action_one_hot = tf.one_hot(self._action,
                                            convnet_pars['output_shape'][0],
                                            name='action_one_hot')

            with tf.variable_scope('Mask'):
                self._mask = tf.placeholder(
                    tf.float32, shape=[None, convnet_pars['n_approximators']])

            with tf.variable_scope('Convolutions'):
                hidden_1 = tf.layers.conv2d(
                    self._x / 255., 32, 8, 4, activation=tf.nn.relu,
                    kernel_initializer=tf.glorot_uniform_initializer(),
                    name='hidden_1'
                )
                hidden_2 = tf.layers.conv2d(
                    hidden_1, 64, 4, 2, activation=tf.nn.relu,
                    kernel_initializer=tf.glorot_uniform_initializer(),
                    name='hidden_2'
                )
                hidden_3 = tf.layers.conv2d(
                    hidden_2, 64, 3, 1, activation=tf.nn.relu,
                    kernel_initializer=tf.glorot_uniform_initializer(),
                    name='hidden_3'
                )
                flatten = tf.reshape(hidden_3, [-1, 7 * 7 * 64], name='flatten')

                '''def scale_gradient():
                    with flatten.graph.gradient_override_map(
                            {'Identity': 'scaled_gradient_' + self._name}):
                        return tf.identity(flatten, name='identity')

                @tf.RegisterGradient('scaled_gradient_' + self._name)
                def scaled_gradient(op, grad):
                    return grad / float(convnet_pars['n_approximators'])'''

                identity = flatten

            self._features = list()
            self._q = list()
            self._q_acted = list()
            self.n_approximators = convnet_pars['n_approximators']
            self.q_min = convnet_pars['q_min']
            self.q_max = convnet_pars['q_max']
            self.init_type = convnet_pars['init_type']

            if self.init_type == 'boot':
                kernel_initializer = lambda _: tf.glorot_uniform_initializer()
                bias_initializer = lambda _: tf.zeros_initializer()
            else:
                initial_values = np.linspace(self.q_min, self.q_max, self.n_approximators)
                kernel_initializer = lambda _: tf.glorot_uniform_initializer()
                bias_initializer = lambda i: tf.constant_initializer(initial_values[i])

            for i in range(self.n_approximators):
                with tf.variable_scope('head_' + str(i)):
                    self._features.append(tf.layers.dense(
                        identity, 512, activation=tf.nn.relu,
                        kernel_initializer=tf.glorot_uniform_initializer(),
                        name='_features_' + str(i)
                    ))
                    self._q.append(tf.layers.dense(
                        self._features[i],
                        convnet_pars['output_shape'][0],
                        kernel_initializer=kernel_initializer(i),
                        bias_initializer=bias_initializer(i),
                        name='q_' + str(i)
                    ))
                    self._q_acted.append(
                        tf.reduce_sum(self._q[i] * action_one_hot,
                                      axis=1,
                                      name='q_acted_' + str(i))
                    )

            self._q_acted = tf.transpose(self._q_acted)

            self._target_q = tf.placeholder(
                'float32',
                [None, convnet_pars['n_approximators']],
                name='target_q'
            )
            self._margin = tf.placeholder('float32', (),
                                                    name='margin')
            self._q_acted_sorted = tf.contrib.framework.sort(self._q_acted, axis=1)
            self._target_q_sorted = tf.contrib.framework.sort(self._target_q, axis=1)

            loss = 0.
            if convnet_pars["loss"] == "huber_loss":
                self.loss_fuction = tf.losses.huber_loss
            else:
                self.loss_fuction = tf.losses.mean_squared_error
            k = convnet_pars['n_approximators']
            if convnet_pars["loss"] == "triple_loss":

                loss = ConvNet.triple_loss(self._q_acted_sorted, self._target_q_sorted, k , self._margin)
            else:
                for i in range(convnet_pars['n_approximators']):

                    loss += self.loss_fuction(
                        self._target_q_sorted[:, i],
                        self._q_acted_sorted[:, i]
                    )
                loss = loss / k
            self._prob_exploration = tf.placeholder('float32', (),
                                                    name='prob_exploration')
            tf.summary.scalar(convnet_pars["loss"], loss)
            tf.summary.scalar('average_q', tf.reduce_mean(self._q))
            # tf.summary.scalar('average_std', tf.reduce_mean(tf.sqrt(tf.nn.moments(self._q, axes=[0])[1])))
            tf.summary.scalar('prob_exploration', self._prob_exploration)
            tf.summary.histogram('qs', self._q)
            self._merged = tf.summary.merge(
                tf.get_collection(tf.GraphKeys.SUMMARIES,
                                  scope=self._scope_name)
            )

            optimizer = convnet_pars['optimizer']
            if optimizer['name'] == 'rmspropcentered':
                opt = tf.train.RMSPropOptimizer(learning_rate=optimizer['lr'],
                                                decay=optimizer['decay'],
                                                epsilon=optimizer['epsilon'],
                                                centered=True)
            elif optimizer['name'] == 'rmsprop':
                opt = tf.train.RMSPropOptimizer(learning_rate=optimizer['lr'],
                                                decay=optimizer['decay'],
                                                epsilon=optimizer['epsilon'])
            elif optimizer['name'] == 'adam':
                opt = tf.train.AdamOptimizer(learning_rate=optimizer['lr'])
            elif optimizer['name'] == 'adadelta':
                opt = tf.train.AdadeltaOptimizer(learning_rate=optimizer['lr'])
            else:
                raise ValueError('Unavailable optimizer selected.')

            self._train_step = opt.minimize(loss=loss)

            initializer = tf.variables_initializer(
                tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                  scope=self._scope_name))

        self._session.run(initializer)

        if self._folder_name is not None:
            self._train_writer = tf.summary.FileWriter(
                self._folder_name + '/' + self._scope_name[:-1],
                graph=tf.get_default_graph()
            )

        self._train_count = 0

        self._add_collection()
Example #38
0
    def attack(self, audio, lengths, target, finetune=None):
        sess = self.sess

        # Initialize all of the variables
        # TODO: each of these assign ops creates a new TF graph
        # object, and they should be all created only once in the
        # constructor. It works fine as long as you don't call
        # attack() a bunch of times.
        sess.run(tf.variables_initializer([self.delta]))
        sess.run(self.original.assign(np.array(audio)))
        sess.run(self.lengths.assign((np.array(lengths)-1)//320))
        sess.run(self.mask.assign(np.array([[1 if i < l else 0 for i in range(self.max_audio_len)] for l in lengths])))
        sess.run(self.cwmask.assign(np.array([[1 if i < l else 0 for i in range(self.phrase_length)] for l in (np.array(lengths)-1)//320])))
        sess.run(self.target_phrase_lengths.assign(np.array([len(x) for x in target])))
        sess.run(self.target_phrase.assign(np.array([list(t)+[0]*(self.phrase_length-len(t)) for t in target])))
        c = np.ones((self.batch_size, self.phrase_length))
        sess.run(self.importance.assign(c))
        sess.run(self.rescale.assign(np.ones((self.batch_size,1))))

        # Here we'll keep track of the best solution we've found so far
        final_deltas = [None]*self.batch_size

        if finetune is not None and len(finetune) > 0:
            sess.run(self.delta.assign(finetune-audio))
        
        # We'll make a bunch of iterations of gradient descent here
        now = time.time()
        MAX = self.num_iterations
        for i in range(MAX):
            iteration = i
            now = time.time()

            # Print out some debug information every 10 iterations.
            if i%10 == 0:
                new, delta, r_out, r_logits = sess.run((self.new_input, self.delta, self.decoded, self.logits))
                lst = [(r_out, r_logits)]
                if self.mp3:
                    mp3ed = convert_mp3(new, lengths)
                    
                    mp3_out, mp3_logits = sess.run((self.decoded, self.logits),
                                                   {self.new_input: mp3ed})
                    lst.append((mp3_out, mp3_logits))

                for out, logits in lst:
                    chars = out[0].values

                    res = np.zeros(out[0].dense_shape)+len(toks)-1
                
                    for ii in range(len(out[0].values)):
                        x,y = out[0].indices[ii]
                        res[x,y] = out[0].values[ii]

                    # Here we print the strings that are recognized.
                    res = ["".join(toks[int(x)] for x in y).replace("-","") for y in res]
                    print("\n".join(res))
                    
                    # And here we print the argmax of the alignment.
                    res2 = np.argmax(logits,axis=2).T
                    res2 = ["".join(toks[int(x)] for x in y[:(l-1)//320]) for y,l in zip(res2,lengths)]
                    print("\n".join(res2))


            if self.mp3:
                new = sess.run(self.new_input)
                mp3ed = convert_mp3(new, lengths)
                feed_dict = {self.new_input: mp3ed}
            else:
                feed_dict = {}
                
            # Actually do the optimization ste
            d, el, cl, l, logits, new_input, _ = sess.run((self.delta, self.expanded_loss,
                                                           self.ctcloss, self.loss,
                                                           self.logits, self.new_input,
                                                           self.train),
                                                          feed_dict)
                    
            # Report progress
            print("%.3f"%np.mean(cl), "\t", "\t".join("%.3f"%x for x in cl))

            logits = np.argmax(logits,axis=2).T
            for ii in range(self.batch_size):
                # Every 100 iterations, check if we've succeeded
                # if we have (or if it's the final epoch) then we
                # should record our progress and decrease the
                # rescale constant.
                if (self.loss_fn == "CTC" and i%10 == 0 and res[ii] == "".join([toks[x] for x in target[ii]])) \
                   or (i == MAX-1 and final_deltas[ii] is None):
                    # Get the current constant
                    rescale = sess.run(self.rescale)
                    if rescale[ii]*2000 > np.max(np.abs(d)):
                        # If we're already below the threshold, then
                        # just reduce the threshold to the current
                        # point and save some time.
                        print("It's way over", np.max(np.abs(d[ii]))/2000.0)
                        rescale[ii] = np.max(np.abs(d[ii]))/2000.0

                    # Otherwise reduce it by some constant. The closer
                    # this number is to 1, the better quality the result
                    # will be. The smaller, the quicker we'll converge
                    # on a result but it will be lower quality.
                    rescale[ii] *= .8

                    # Adjust the best solution found so far
                    final_deltas[ii] = new_input[ii]

                    print("Worked i=%d ctcloss=%f bound=%f"%(ii,cl[ii], 2000*rescale[ii][0]))
                    #print('delta',np.max(np.abs(new_input[ii]-audio[ii])))
                    sess.run(self.rescale.assign(rescale))

                    # Just for debugging, save the adversarial example
                    # to /tmp so we can see it if we want
                    wav.write("/tmp/adv.wav", 16000,
                              np.array(np.clip(np.round(new_input[ii]),
                                               -2**15, 2**15-1),dtype=np.int16))

        return final_deltas
 def initialize(self, sess=None):
     if sess is None:
         sess = self.sess
     sess.run(tf.variables_initializer(self.vars_tf))
     sess.run(tf.variables_initializer(self.optimizer_instance.variables()))
     self.initialized = True
Example #40
0
    def __init__(self, sess, loss_fn, phrase_length, max_audio_len,
                 learning_rate=10, num_iterations=5000, batch_size=1,
                 mp3=False, l2penalty=float('inf'), restore_path=None):
        """
        Set up the attack procedure.

        Here we create the TF graph that we're going to use to
        actually generate the adversarial examples.
        """
        
        self.sess = sess
        self.learning_rate = learning_rate
        self.num_iterations = num_iterations
        self.batch_size = batch_size
        self.phrase_length = phrase_length
        self.max_audio_len = max_audio_len
        self.mp3 = mp3

        # Create all the variables necessary
        # they are prefixed with qq_ just so that we know which
        # ones are ours so when we restore the session we don't
        # clobber them.
        self.delta = delta = tf.Variable(np.zeros((batch_size, max_audio_len), dtype=np.float32), name='qq_delta')
        self.mask = mask = tf.Variable(np.zeros((batch_size, max_audio_len), dtype=np.float32), name='qq_mask')
        self.cwmask = cwmask = tf.Variable(np.zeros((batch_size, phrase_length), dtype=np.float32), name='qq_cwmask')
        self.original = original = tf.Variable(np.zeros((batch_size, max_audio_len), dtype=np.float32), name='qq_original')
        self.lengths = lengths = tf.Variable(np.zeros(batch_size, dtype=np.int32), name='qq_lengths')
        self.importance = tf.Variable(np.zeros((batch_size, phrase_length), dtype=np.float32), name='qq_importance')
        self.target_phrase = tf.Variable(np.zeros((batch_size, phrase_length), dtype=np.int32), name='qq_phrase')
        self.target_phrase_lengths = tf.Variable(np.zeros((batch_size), dtype=np.int32), name='qq_phrase_lengths')
        self.rescale = tf.Variable(np.zeros((batch_size,1), dtype=np.float32), name='qq_phrase_lengths')

        # Initially we bound the l_infty norm by 2000, increase this
        # constant if it's not big enough of a distortion for your dataset.
        self.apply_delta = tf.clip_by_value(delta, -2000, 2000)*self.rescale

        # We set the new input to the model to be the abve delta
        # plus a mask, which allows us to enforce that certain
        # values remain constant 0 for length padding sequences.
        self.new_input = new_input = self.apply_delta*mask + original

        # We add a tiny bit of noise to help make sure that we can
        # clip our values to 16-bit integers and not break things.
        noise = tf.random_normal(new_input.shape,
                                 stddev=2)
        pass_in = tf.clip_by_value(new_input+noise, -2**15, 2**15-1)

        # Feed this final value to get the logits.
        self.logits = logits = get_logits(pass_in, lengths)

        # And finally restore the graph to make the classifier
        # actually do something interesting.
        saver = tf.train.Saver([x for x in tf.global_variables() if 'qq' not in x.name])
        saver.restore(sess, restore_path)

        # Choose the loss function we want -- either CTC or CW
        self.loss_fn = loss_fn
        if loss_fn == "CTC":
            target = ctc_label_dense_to_sparse(self.target_phrase, self.target_phrase_lengths)
            
            ctcloss = tf.nn.ctc_loss(labels=tf.cast(target, tf.int32),
                                     inputs=logits, sequence_length=lengths)

            # Slight hack: an infinite l2 penalty means that we don't penalize l2 distortion
            # The code runs faster at a slight cost of distortion, and also leaves one less
            # paramaeter that requires tuning.
            if not np.isinf(l2penalty):
                loss = tf.reduce_mean((self.new_input-self.original)**2,axis=1) + l2penalty*ctcloss
            else:
                loss = ctcloss
            self.expanded_loss = tf.constant(0)
            
        elif loss_fn == "CW":
            raise NotImplemented("The current version of this project does not include the CW loss function implementation.")
        else:
            raise

        self.loss = loss
        self.ctcloss = ctcloss
        
        # Set up the Adam optimizer to perform gradient descent for us
        start_vars = set(x.name for x in tf.global_variables())
        optimizer = tf.train.AdamOptimizer(learning_rate)

        grad,var = optimizer.compute_gradients(self.loss, [delta])[0]
        self.train = optimizer.apply_gradients([(tf.sign(grad),var)])
        
        end_vars = tf.global_variables()
        new_vars = [x for x in end_vars if x.name not in start_vars]
        
        sess.run(tf.variables_initializer(new_vars+[delta]))

        # Decoder from the logits, to see how we're doing
        self.decoded, _ = tf.nn.ctc_beam_search_decoder(logits, lengths, merge_repeated=False, beam_width=100)
    traininputpredictions3 = tf.nn.softmax(
        tf.matmul(traininputlevel1, W3) + b3)
    traininputpredictions4 = tf.nn.softmax(
        tf.matmul(traininputlevel1, W4) + b4)
    traininputpredictions5 = tf.nn.softmax(
        tf.matmul(traininputlevel1, W5) + b5)

    level2traininput = tf.concat([
        traininputpredictions1, traininputpredictions2, traininputpredictions3,
        traininputpredictions4, traininputpredictions5
    ], 1)
    # print(level2traininput)

    level2W = tf.Variable(tf.truncated_normal([10, 100], stddev=0.1), name='W')
    level2b = tf.Variable(tf.truncated_normal([100]), name='b')
    sess.run(tf.variables_initializer([level2W, level2b]))

    # hidden layer2
    hidden2traininput = tf.nn.relu(
        tf.matmul(level2traininput, level2W) + level2b)
    hidden2W = tf.Variable(tf.truncated_normal([100, 10], stddev=0.1),
                           name='W')
    hidden2b = tf.Variable(tf.truncated_normal([10]), name='b')

    level3traininput = tf.nn.relu(
        tf.matmul(hidden2traininput, hidden2W) + hidden2b)

    # Dropout
    keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(level3traininput, keep_prob)
    def local_model(class1, class2):
        train_images_class1 = []
        train_images_class2 = []
        train_labels_originals_class1 = []
        train_labels_originals_class2 = []
        j = 0
        for i in train_labels:
            if (i[class1] == 1):
                train_images_class1.append(train_images[j])
                train_labels_originals_class1.append(i)
            if (i[class2] == 1):
                train_images_class2.append(train_images[j])
                train_labels_originals_class2.append(i)
            j = j + 1

        class1input = tf.Variable(numpy.array(train_images_class1),
                                  name='class1input',
                                  dtype="float32")
        class2input = tf.Variable(numpy.array(train_images_class2),
                                  name='class2input',
                                  dtype="float32")
        #print(class1input)
        class1label_1 = tf.Variable(tf.ones([class1input.get_shape()[0], 1]),
                                    name='class1label')
        class1label_2 = tf.Variable(tf.zeros([class1input.get_shape()[0], 1]),
                                    name='class1label')
        sess.run(tf.variables_initializer([class1label_1, class1label_2]))
        class1label = tf.concat([class1label_1, class1label_2], 1)

        class2label_1 = tf.Variable(tf.zeros([class2input.get_shape()[0], 1]),
                                    name='class2label')
        class2label_2 = tf.Variable(tf.ones([class2input.get_shape()[0], 1]),
                                    name='class2label')
        sess.run(tf.variables_initializer([class2label_1, class2label_2]))
        class2label = tf.concat([class2label_1, class2label_2], 1)

        W_temp = tf.Variable(tf.truncated_normal([784, 2], stddev=0.1),
                             name='W')
        b_temp = tf.Variable(tf.truncated_normal([2]), name='b')

        y = tf.nn.softmax(
            tf.matmul(tf.concat([class1input, class2input], 0), W_temp) +
            b_temp,
            name='y')
        sess.run(
            tf.variables_initializer(
                [class1input, class2input, W_temp, b_temp]))
        cross_entropy = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(
                logits=y, labels=tf.concat([class1label, class2label], 0)))
        # inverse time decay for learning rate
        global_step = tf.Variable(0, trainable=False)
        sess.run(tf.variables_initializer([global_step]))
        starter_learning_rate = 0.1
        k = 0.5
        learning_rate = tf.train.inverse_time_decay(starter_learning_rate,
                                                    global_step,
                                                    decay_rate=k,
                                                    decay_steps=20)

        train_step = tf.train.RMSPropOptimizer(learning_rate).minimize(
            cross_entropy, global_step=global_step)
        sess.run(
            tf.group(tf.global_variables_initializer(),
                     tf.local_variables_initializer()))
        for i in range(200):
            global_step = i
            sess.run(train_step)

        # Evaluate individual model
        # test images
        test_images_class1 = []
        test_images_class2 = []

        j = 0
        for i in test_labels:
            if (i[class1] == 1):
                test_images_class1.append(test_images[j])
            if (i[class2] == 1):
                test_images_class2.append(test_images[j])
            j = j + 1

        class1test = tf.constant(numpy.array(test_images_class1),
                                 name='class1test',
                                 dtype="float32")
        class2test = tf.constant(numpy.array(test_images_class2),
                                 name='class2test',
                                 dtype="float32")
        #print(class1test)
        class1testlabel_1 = tf.Variable(tf.ones([class1test.get_shape()[0],
                                                 1]),
                                        name='class1testlabel')
        class1testlabel_2 = tf.Variable(tf.zeros(
            [class1test.get_shape()[0], 1]),
                                        name='class1testlabel')
        sess.run(
            tf.variables_initializer([class1testlabel_1, class1testlabel_2]))
        class1testlabel = tf.concat([class1testlabel_1, class1testlabel_2], 1)

        class2testlabel_1 = tf.Variable(tf.zeros(
            [class2test.get_shape()[0], 1]),
                                        name='class2testlabel')
        class2testlabel_2 = tf.Variable(tf.ones([class2test.get_shape()[0],
                                                 1]),
                                        name='class2testlabel')
        sess.run(
            tf.variables_initializer([class2testlabel_1, class2testlabel_2]))
        class2testlabel = tf.concat([class2testlabel_1, class2testlabel_2], 1)
        # compare predicted label and actual label
        test_predicted = tf.nn.softmax(
            tf.matmul(tf.concat([class1test, class2test], 0), W_temp) + b_temp,
            name='test_predicted')

        correct_prediction = tf.equal(
            tf.argmax(test_predicted, 1),
            tf.argmax(tf.concat([class1testlabel, class2testlabel], 0), 1))

        # accuracy op
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        accu = sess.run(accuracy)
        print(accu)

        y_actual = tf.concat([
            tf.constant(numpy.array(train_labels_originals_class1)),
            tf.constant(numpy.array(train_labels_originals_class2))
        ], 0)
        #return W_temp, b_temp, class1input, class2input, class1test, class2test

        # return Weight array, biases array, input image vector, predictions of individual model, input train label
        return W_temp, b_temp, tf.concat([class1input, class2input],
                                         0), y, y_actual
Example #43
0
    def construct(self, args, source_chars, target_chars, bow, eow):
        with self.session.graph.as_default():
            if args.recodex:
                tf.get_variable_scope().set_initializer(tf.glorot_uniform_initializer(seed=42))

            # Inputs
            self.sentence_lens = tf.placeholder(tf.int32, [None], name="sentence_lens")
            self.source_ids = tf.placeholder(tf.int32, [None, None], name="source_ids")
            self.source_seqs = tf.placeholder(tf.int32, [None, None], name="source_seqs")
            self.source_seq_lens = tf.placeholder(tf.int32, [None], name="source_seq_lens")
            self.target_ids = tf.placeholder(tf.int32, [None, None], name="target_ids")
            self.target_seqs = tf.placeholder(tf.int32, [None, None], name="target_seqs")
            self.target_seq_lens = tf.placeholder(tf.int32, [None], name="target_seq_lens")

            # Append EOW after target_seqs
            target_seqs = tf.reverse_sequence(self.target_seqs, self.target_seq_lens, 1)
            target_seqs = tf.pad(target_seqs, [[0, 0], [1, 0]], constant_values=eow)
            target_seq_lens = self.target_seq_lens + 1
            target_seqs = tf.reverse_sequence(target_seqs, target_seq_lens, 1)

            # Encoder
            # TODO: Generate source embeddings for source chars, of shape [source_chars, args.char_dim].

            # TODO: Embed the self.source_seqs using the source embeddings.

            # TODO: Using a GRU with dimension args.rnn_dim, process the embedded self.source_seqs
            # using forward RNN and store the resulting states into `source_states`.

            # Index the unique words using self.source_ids and self.target_ids.
            sentence_mask = tf.sequence_mask(self.sentence_lens)
            source_states = tf.boolean_mask(tf.nn.embedding_lookup(source_states, self.source_ids), sentence_mask)
            source_lens = tf.boolean_mask(tf.nn.embedding_lookup(self.source_seq_lens, self.source_ids), sentence_mask)

            target_seqs = tf.boolean_mask(tf.nn.embedding_lookup(target_seqs, self.target_ids), sentence_mask)
            target_lens = tf.boolean_mask(tf.nn.embedding_lookup(target_seq_lens, self.target_ids), sentence_mask)

            # Decoder
            # TODO: Generate target embeddings for target chars, of shape [target_chars, args.char_dim].

            # TODO: Embed the target_seqs using the target embeddings.

            # TODO: Generate a decoder GRU with wimension args.rnn_dim.

            # TODO: Create a `decoder_layer` -- a fully connected layer with
            # target_chars neurons used in the decoder to classify into target characters.

            # The DecoderTraining will be used during training. It will output logits for each
            # target character.
            class DecoderTraining(tf.contrib.seq2seq.Decoder):
                @property
                def batch_size(self): return # TODO: Return size of the batch, using for example source_states size
                @property
                def output_dtype(self): return tf.float32 # Type for logits of target characters
                @property
                def output_size(self): return target_chars # Length of logits for every output

                def initialize(self, name=None):
                    finished = # TODO: False if target_lens > 0, True otherwise
                    states = # TODO: Initial decoder state to use
                    inputs = # TODO: embedded BOW characters of shape [self.batch_size] using target embeddings.
                             # You can use tf.fill to generate BOWs of appropriate size.
                    return finished, inputs, states

                def step(self, time, inputs, states, name=None):
                    outputs, states = # TODO: Run the decoder GRU cell using inputs and states.
                    outputs = # TODO: Apply the decoder_layer on outputs.
                    next_input = # TODO: Next input are character embeddings with index `time` in target_embedded.
                    finished = # TODO: False if target_lens > time + 1, True otherwise.
                    return outputs, states, next_input, finished
            output_layer, _, _ = tf.contrib.seq2seq.dynamic_decode(DecoderTraining())
            self.predictions_training = tf.argmax(output_layer, axis=2, output_type=tf.int32)

            # The DecoderPrediction will be used during prediction. It will
            # directly output the predicted target characters.
            class DecoderPrediction(tf.contrib.seq2seq.Decoder):
                @property
                def batch_size(self): return # TODO: Return size of the batch, using for example source_states size
                @property
                def output_dtype(self): return tf.int32 # Type for predicted target characters
                @property
                def output_size(self): return 1 # Will return just one output

                def initialize(self, name=None):
                    finished = # TODO: False of shape [self.batch_size].
                    states = # TODO: Initial decoder state to use.
                    inputs = # TODO: embedded BOW characters of shape [self.batch_size] using target embeddings.
                             # You can use tf.fill to generate BOWs of appropriate size.
                    return finished, inputs, states

                def step(self, time, inputs, states, name=None):
                    outputs, states = # TODO: Run the decoder GRU cell using inputs and states.
                    outputs = # TODO: Apply the decoder_layer on outputs.
                    outputs = # TODO: Use tf.argmax to choose most probable class (supply parameter `output_type=tf.int32`).
                    next_input = # TODO: Embed `outputs` using target_embeddings
                    finished = # TODO: True where outputs==eow, False otherwise
                               # Use tf.equal for the comparison, Python's '==' is not overloaded
                    return outputs, states, next_input, finished
            self.predictions, _, self.prediction_lens = tf.contrib.seq2seq.dynamic_decode(
                DecoderPrediction(), maximum_iterations=tf.reduce_max(source_lens) + 10)

            # Training
            weights = tf.sequence_mask(target_lens, dtype=tf.float32)
            loss = tf.losses.sparse_softmax_cross_entropy(target_seqs, output_layer, weights=weights)
            global_step = tf.train.create_global_step()
            self.training = tf.train.AdamOptimizer().minimize(loss, global_step=global_step, name="training")

            # Summaries
            accuracy_training = tf.reduce_all(tf.logical_or(
                tf.equal(self.predictions_training, target_seqs),
                tf.logical_not(tf.sequence_mask(target_lens))), axis=1)
            self.current_accuracy_training, self.update_accuracy_training = tf.metrics.mean(accuracy_training)

            minimum_length = tf.minimum(tf.shape(self.predictions)[1], tf.shape(target_seqs)[1])
            accuracy = tf.logical_and(
                tf.equal(self.prediction_lens, target_lens),
                tf.reduce_all(tf.logical_or(
                    tf.equal(self.predictions[:, :minimum_length], target_seqs[:, :minimum_length]),
                    tf.logical_not(tf.sequence_mask(target_lens, maxlen=minimum_length))), axis=1))
            self.current_accuracy, self.update_accuracy = tf.metrics.mean(accuracy)

            self.current_loss, self.update_loss = tf.metrics.mean(loss, weights=tf.reduce_sum(weights))
            self.reset_metrics = tf.variables_initializer(tf.get_collection(tf.GraphKeys.METRIC_VARIABLES))

            summary_writer = tf.contrib.summary.create_file_writer(args.logdir, flush_millis=10 * 1000)
            self.summaries = {}
            with summary_writer.as_default(), tf.contrib.summary.record_summaries_every_n_global_steps(10):
                self.summaries["train"] = [tf.contrib.summary.scalar("train/loss", self.update_loss),
                                           tf.contrib.summary.scalar("train/accuracy", self.update_accuracy_training)]
            with summary_writer.as_default(), tf.contrib.summary.always_record_summaries():
                for dataset in ["dev", "test"]:
                    self.summaries[dataset] = [tf.contrib.summary.scalar(dataset + "/loss", self.current_loss),
                                               tf.contrib.summary.scalar(dataset + "/accuracy", self.current_accuracy)]

            # Initialize variables
            self.session.run(tf.global_variables_initializer())
            with summary_writer.as_default():
                tf.contrib.summary.initialize(session=self.session, graph=self.session.graph)
Example #44
0
def training_phase(bert_config, processor, tokenizer, label_list, predict_label_list):
    """
    :param processor:
    :param tokenizer:
    :param label_list:
    :param predict_label_list:
    :return:
    """
    """
     建training_data和dev_data的联合pipeline,返回train_init_op和dev_init_op来初始化
     """
    (input_ids, input_mask, segment_ids, label_ids, train_init_op, dev_init_op,
     num_train_steps, handle) = build_train_dev_data_pipeline(processor, tokenizer, label_list, predict_label_list)
    num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion)
    tf.logging.info("=========== Train and evaluate set are loaded ============")
    """
    建模型
    """
    (total_loss, logits, trans, pred_ids) = create_model(bert_config=bert_config, is_training=True,
                                                         input_ids=input_ids,
                                                         input_mask=input_mask, segment_ids=segment_ids,
                                                         labels=label_ids,
                                                         num_labels=len(label_list) + 1, use_one_hot_embeddings=False)
    tf.summary.scalar("total_loss", total_loss)
    tf.logging.info("================= Model is built ====================")
    """
    以下开始加载BERT,即用BERT的参数去初始化我新模型的权重,
    init_from_checkpoint即按assignment_map对应的变量来加载 
    """
    init_checkpoint = FLAGS.init_checkpoint
    tvars = tf.trainable_variables()
    # 加载BERT模型, 用Bert的参数来初始化模型
    if init_checkpoint:
        (assignment_map, initialized_variable_names) = modeling.get_assignment_map_from_checkpoint(tvars,
                                                                                                  init_checkpoint)
        tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

    train_op = optimization.create_optimizer(
        total_loss, FLAGS.learning_rate, num_train_steps, num_warmup_steps, use_tpu=False)
    tf.logging.info("================BERT are loaded to initiate and train_op is built===========")
    """
    设置三种评价指标, 每个指标包含两个element(scalar float Tensor, update_op)
    """
    (precision, recall, f1) = eval_phase(label_ids, pred_ids,len(label_list) + 1)
    running_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES)
    running_vars_initializer = tf.variables_initializer(var_list=running_vars) #初始化precision、recall、f1这些计算节点
    prec_scalar, prec_op = precision
    recall_scalar, recall_op = recall
    f1_scalar, f1_op = f1
    tf.logging.info("=================eval metrics are loaded=========================")
    """
    设置Savar为最多保存五个model
    """
    saver = tf.train.Saver(max_to_keep=5)

    merged = tf.summary.merge_all()

    tf.logging.info("==================Entering Session Running=========================")
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer()) #除了CRF层,其它层都被initialized了
        train_writer = tf.summary.FileWriter(FLAGS.output_dir , sess.graph)
        dev_writer = tf.summary.FileWriter(FLAGS.output_dir + '/eval')
        train_iterator_handle = sess.run(train_init_op.string_handle())
        dev_iterator_handle = sess.run(dev_init_op.string_handle())
        for step in range(num_train_steps):
            if step % 100 == 0:
                tf.logging.info("===============evaluate at %d step=============="%step)
                sess.run(running_vars_initializer)
                sess.run(dev_init_op.initializer)
                # while True:
                while True:
                    try:
                        # print(sess.run([label_ids, pred_ids], feed_dict={handle: dev_iterator_handle}))
                        summary,_,_,_ = sess.run([merged, prec_op, recall_op, f1_op], feed_dict={handle: dev_iterator_handle})
                    except tf.errors.OutOfRangeError:
                        break
                dev_writer.add_summary(summary, step)
                _precision, _recall, _f1 = sess.run([prec_scalar, recall_scalar, f1_scalar])
                print("At step {}, the precision is {:.2f}%,the recall is {:.2f}%,the f1 is {:.2f}%".format(step, _precision*100, _recall*100, _f1*100))
            else:
                if step % 1000 == 999:
                    tf.logging.info("===============save model at %d step==============" % step)
                    run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
                    run_metadata = tf.RunMetadata()
                    summary, _ , _total_loss= sess.run([merged, train_op, total_loss],
                                          feed_dict={handle: train_iterator_handle},
                                          options=run_options,
                                          run_metadata=run_metadata)
                    train_writer.add_run_metadata(run_metadata, 'step%03d' % step)
                    train_writer.add_summary(summary, step)
                    tf.logging.info("========== the total loss is %.5f ===============" %(_total_loss))
                    print('Adding run metadata for', step)
                    save_path = saver.save(sess, os.path.join(FLAGS.output_dir, "model.ckpt"), global_step=step)
                    print("Model saved in path: %s" % save_path)
                else:
                    # print(sess.run([pred_ids, label_ids], feed_dict={handle: train_iterator_handle}))
                    summary, _ = sess.run([merged, train_op], feed_dict={handle: train_iterator_handle})
                    train_writer.add_summary(summary, step)
        train_writer.close()
        dev_writer.close()
Example #45
0
def model_fn(mode, inputs, params, reuse=False):
    """Model function defining the graph operations.

    Args:
        mode: (string) can be 'train' or 'eval'
        inputs: (dict) contains the inputs of the graph (features, labels...)
                this can be `tf.placeholder` or outputs of `tf.data`
        params: (Params) contains hyperparameters of the model (ex: `params.learning_rate`)
        reuse: (bool) whether to reuse the weights

    Returns:
        model_spec: (dict) contains the graph operations or nodes needed for training / evaluation
    """
    is_training = (mode == 'train')
    labels = inputs['labels']
    labels = tf.cast(labels, tf.int64)

    # -----------------------------------------------------------
    # MODEL: define the layers of the model
    with tf.variable_scope('model', reuse=reuse):
        # Compute the output distribution of the model and the predictions
        logits = build_model(is_training, inputs, params)
        predictions = tf.argmax(logits, 1)

    # Define loss and accuracy
    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(labels, predictions), tf.float32))

    # Define training step that minimizes the loss with the Adam optimizer
    if is_training:
        optimizer = tf.train.AdamOptimizer(params.learning_rate)
        global_step = tf.train.get_or_create_global_step()
        if params.use_batch_norm:
            # Add a dependency to update the moving mean and variance for batch normalization
            with tf.control_dependencies(
                    tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
                train_op = optimizer.minimize(loss, global_step=global_step)
        else:
            train_op = optimizer.minimize(loss, global_step=global_step)

    # -----------------------------------------------------------
    # METRICS AND SUMMARIES
    # Metrics for evaluation using tf.metrics (average over whole dataset)
    with tf.variable_scope("metrics"):
        metrics = {
            'accuracy':
            tf.metrics.accuracy(labels=labels,
                                predictions=tf.argmax(logits, 1)),
            'loss':
            tf.metrics.mean(loss)
        }

    # Group the update ops for the tf.metrics
    update_metrics_op = tf.group(*[op for _, op in metrics.values()])

    # Get the op to reset the local variables used in tf.metrics
    metric_variables = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES,
                                         scope="metrics")
    metrics_init_op = tf.variables_initializer(metric_variables)

    # Summaries for training
    tf.summary.scalar('loss', loss)
    tf.summary.scalar('accuracy', accuracy)
    tf.summary.image('train_image', inputs['images'])

    # Add incorrectly labeled images
    mask = tf.not_equal(labels, predictions)

    # Add a different summary to know how they were misclassified
    for label in range(0, params.num_labels):
        mask_label = tf.logical_and(mask, tf.equal(predictions, label))
        incorrect_image_label = tf.boolean_mask(inputs['images'], mask_label)
        tf.summary.image('incorrectly_labeled_{}'.format(label),
                         incorrect_image_label)

    # -----------------------------------------------------------
    # MODEL SPECIFICATION
    # Create the model specification and return it
    # It contains nodes or operations in the graph that will be used for training and evaluation
    model_spec = inputs
    model_spec['variable_init_op'] = tf.global_variables_initializer()
    model_spec["predictions"] = predictions
    model_spec['loss'] = loss
    model_spec['accuracy'] = accuracy
    model_spec['metrics_init_op'] = metrics_init_op
    model_spec['metrics'] = metrics
    model_spec['update_metrics'] = update_metrics_op
    model_spec['summary_op'] = tf.summary.merge_all()

    if is_training:
        model_spec['train_op'] = train_op

    return model_spec
Example #46
0
    def __init__(self, states_spec, actions_spec, config, **kwargs):

        # States and actions specifications
        self.states_spec = states_spec
        self.actions_spec = actions_spec

        # Discount factor
        self.discount = config.discount

        # Reward normalization
        assert isinstance(config.normalize_rewards, bool)
        self.normalize_rewards = config.normalize_rewards

        # Variable noise
        assert config.variable_noise is None or config.variable_noise > 0.0
        self.variable_noise = config.variable_noise

        self.scope = config.scope

        # Saver/summary/distributed specifications
        saver_spec = config.saver_spec
        summary_spec = config.summary_spec
        distributed_spec = config.distributed_spec

        # TensorFlow summaries
        if summary_spec is None:
            self.summary_labels = set()
        else:
            self.summary_labels = set(summary_spec.get('labels', ()))

        # Variables and summaries
        self.variables = dict()
        self.all_variables = dict()
        self.registered_variables = set()
        self.summaries = list()

        if distributed_spec is None:
            self.device = config.device
            self.global_model = None
            self.graph = tf.Graph()
            default_graph = self.graph.as_default()
            default_graph.__enter__()

        elif distributed_spec.get('parameter_server'):
            if distributed_spec.get('replica_model'):
                raise TensorForceError("Invalid config value for distributed mode.")
            self.device = config.device
            self.global_model = None
            self.graph = tf.Graph()
            default_graph = self.graph.as_default()
            default_graph.__enter__()

        elif distributed_spec.get('replica_model'):
            self.device = tf.train.replica_device_setter(
                worker_device=config.device,
                cluster=distributed_spec['cluster_spec']
            )
            self.global_model = None
            # Replica model is part of its parent model's graph, hence no new graph here.
            self.graph = tf.get_default_graph()

        else:
            self.device = config.device
            self.graph = tf.Graph()
            default_graph = self.graph.as_default()
            default_graph.__enter__()

            # Global model.
            global_config = config.copy()
            global_config.distributed_spec['replica_model'] = True
            self.global_model = self.__class__(
                states_spec=states_spec,
                actions_spec=actions_spec,
                config=global_config,
                **kwargs
            )

        with tf.device(device_name_or_function=self.device):
            # Episode
            collection = self.graph.get_collection(name='episode')
            if len(collection) == 0:
                self.episode = tf.get_variable(name='episode', dtype=tf.int32, initializer=0, trainable=False)
                self.graph.add_to_collection(name='episode', value=self.episode)
            else:
                assert len(collection) == 1
                self.episode = collection[0]

            # Timestep
            collection = self.graph.get_collection(name='timestep')
            if len(collection) == 0:
                self.timestep = tf.get_variable(name='timestep', dtype=tf.int32, initializer=0, trainable=False)
                self.graph.add_to_collection(name='timestep', value=self.timestep)
                self.graph.add_to_collection(name=tf.GraphKeys.GLOBAL_STEP, value=self.timestep)
            else:
                assert len(collection) == 1
                self.timestep = collection[0]

            def custom_getter(getter, name, registered=False, second=False, **kwargs):
                if registered:
                    self.registered_variables.add(name)
                elif name in self.registered_variables:
                    registered = True
                variable = getter(name=name, **kwargs)  # Top-level, hence no 'registered'
                if not registered:
                    self.all_variables[name] = variable
                    if kwargs.get('trainable', True) and not name.startswith('optimization'):
                        self.variables[name] = variable
                        if 'variables' in self.summary_labels:
                            summary = tf.summary.histogram(name=name, values=variable)
                            self.summaries.append(summary)
                return variable

            # Create placeholders, tf functions, internals, etc
            self.initialize(custom_getter=custom_getter)

            # Input tensors
            states = self.get_states(states=self.state_inputs)
            internals = [tf.identity(input=internal) for internal in self.internal_inputs]
            actions = self.get_actions(actions=self.action_inputs)
            terminal = tf.identity(input=self.terminal_input)
            reward = self.get_reward(states=states, internals=internals, terminal=terminal, reward=self.reward_input)

            # Stop gradients for input preprocessing
            states = {name: tf.stop_gradient(input=state) for name, state in states.items()}
            actions = {name: tf.stop_gradient(input=action) for name, action in actions.items()}
            reward = tf.stop_gradient(input=reward)

            if 'inputs' in self.summary_labels:
                for name, state in states.items():
                    summary = tf.summary.histogram(name=('/input/states/' + name), values=state)
                    self.summaries.append(summary)
                for name, action in actions.items():
                    summary = tf.summary.histogram(name=('/input/actions/' + name), values=action)
                    self.summaries.append(summary)
                summary = tf.summary.histogram(name='/input/reward', values=reward)
                self.summaries.append(summary)

            # Optimizer
            if config.optimizer is None:
                self.optimizer = None
            elif distributed_spec is not None and \
                    not distributed_spec.get('parameter_server') and \
                    not distributed_spec.get('replica_model'):
                # If not internal global model
                self.optimizer = GlobalOptimizer(optimizer=config.optimizer)
            else:
                self.optimizer = Optimizer.from_spec(spec=config.optimizer)

            # Create output fetch operations
            self.create_output_operations(
                states=states,
                internals=internals,
                actions=actions,
                terminal=terminal,
                reward=reward,
                update=self.update_input,
                deterministic=self.deterministic_input
            )

        if distributed_spec is not None:
            if distributed_spec.get('replica_model'):
                # If internal global model
                return

            elif distributed_spec.get('parameter_server'):
                server = tf.train.Server(
                    server_or_cluster_def=distributed_spec['cluster_spec'],
                    job_name='ps',
                    task_index=distributed_spec['task_index'],
                    protocol=distributed_spec.get('protocol'),
                    config=None,
                    start=True
                )
                # Param server does nothing actively
                server.join()
                return

        # Global and local variables initialize operations
        if distributed_spec is None:
            global_variables = self.get_variables(include_non_trainable=True)
            init_op = tf.variables_initializer(var_list=global_variables)
            ready_op = tf.report_uninitialized_variables(var_list=global_variables)
            ready_for_local_init_op = None
            local_init_op = None
        else:
            global_variables = self.global_model.get_variables(include_non_trainable=True)
            local_variables = self.get_variables(include_non_trainable=True)
            init_op = tf.variables_initializer(var_list=global_variables)
            ready_op = tf.report_uninitialized_variables(var_list=(global_variables + local_variables))
            ready_for_local_init_op = tf.report_uninitialized_variables(var_list=global_variables)
            local_init_op = tf.group(*(local_var.assign(value=global_var) for local_var, global_var in zip(local_variables, global_variables)))

        def init_fn(scaffold, session):
            if saver_spec is not None and saver_spec.get('load', True):
                directory = saver_spec['directory']
                file = saver_spec.get('file')
                if file is None:
                    file = tf.train.latest_checkpoint(
                        checkpoint_dir=directory,
                        latest_filename=None  # Corresponds to argument of saver.save() in Model.save().
                    )
                elif not os.path.isfile(file):
                    file = os.path.join(directory, file)
                if file is not None:
                    scaffold.saver.restore(sess=session, save_path=file)

        # Summary operation
        summaries = self.get_summaries()
        if len(summaries) > 0:
            summary_op = tf.summary.merge(inputs=summaries)
        else:
            summary_op = None

        # TensorFlow saver object
        saver = tf.train.Saver(
            var_list=global_variables,  # should be given?
            reshape=False,
            sharded=False,  # should be true?
            max_to_keep=5,
            keep_checkpoint_every_n_hours=10000.0,
            name=None,
            restore_sequentially=False,
            saver_def=None,
            builder=None,
            defer_build=False,
            allow_empty=True,
            write_version=tf.train.SaverDef.V2,
            pad_step_number=False,
            save_relative_paths=True,
            filename=None
        )

        # TensorFlow scaffold object
        self.scaffold = tf.train.Scaffold(
            init_op=init_op,
            init_feed_dict=None,
            init_fn=init_fn,
            ready_op=ready_op,
            ready_for_local_init_op=ready_for_local_init_op,
            local_init_op=local_init_op,
            summary_op=summary_op,
            saver=saver,
            copy_from_scaffold=None
        )

        hooks = list()

        # Checkpoint saver hook
        if saver_spec is not None and (distributed_spec is None or distributed_spec['task_index'] == 0):
            self.saver_directory = saver_spec['directory']
            hooks.append(tf.train.CheckpointSaverHook(
                checkpoint_dir=self.saver_directory,
                save_secs=saver_spec.get('seconds', None if 'steps' in saver_spec else 600),
                save_steps=saver_spec.get('steps'),  # Either one or the other has to be set.
                saver=None,  # None since given via 'scaffold' argument.
                checkpoint_basename=saver_spec.get('basename', 'model.ckpt'),
                scaffold=self.scaffold,
                listeners=None
            ))
        else:
            self.saver_directory = None

        # Summary saver hook
        if summary_spec is None:
            self.summary_writer_hook = None
        else:
            # TensorFlow summary writer object
            summary_writer = tf.summary.FileWriter(
                logdir=summary_spec['directory'],
                graph=self.graph,
                max_queue=10,
                flush_secs=120,
                filename_suffix=None
            )
            self.summary_writer_hook = util.UpdateSummarySaverHook(
                update_input=self.update_input,
                save_steps=summary_spec.get('steps'),  # Either one or the other has to be set.
                save_secs=summary_spec.get('seconds', None if 'steps' in summary_spec else 120),
                output_dir=None,  # None since given via 'summary_writer' argument.
                summary_writer=summary_writer,
                scaffold=self.scaffold,
                summary_op=None  # None since given via 'scaffold' argument.
            )
            hooks.append(self.summary_writer_hook)

        # Stop at step hook
        # hooks.append(tf.train.StopAtStepHook(
        #     num_steps=???,  # This makes more sense, if load and continue training.
        #     last_step=None  # Either one or the other has to be set.
        # ))

        # # Step counter hook
        # hooks.append(tf.train.StepCounterHook(
        #     every_n_steps=counter_config.get('steps', 100),  # Either one or the other has to be set.
        #     every_n_secs=counter_config.get('secs'),  # Either one or the other has to be set.
        #     output_dir=None,  # None since given via 'summary_writer' argument.
        #     summary_writer=summary_writer
        # ))

        # Other available hooks:
        # tf.train.FinalOpsHook(final_ops, final_ops_feed_dict=None)
        # tf.train.GlobalStepWaiterHook(wait_until_step)
        # tf.train.LoggingTensorHook(tensors, every_n_iter=None, every_n_secs=None)
        # tf.train.NanTensorHook(loss_tensor, fail_on_nan_loss=True)
        # tf.train.ProfilerHook(save_steps=None, save_secs=None, output_dir='', show_dataflow=True, show_memory=False)

        if distributed_spec is None:
            # TensorFlow non-distributed monitored session object
            self.monitored_session = tf.train.SingularMonitoredSession(
                hooks=hooks,
                scaffold=self.scaffold,
                master='',  # Default value.
                config=None,  # always the same?
                checkpoint_dir=None
            )

        else:
            server = tf.train.Server(
                server_or_cluster_def=distributed_spec['cluster_spec'],
                job_name='worker',
                task_index=distributed_spec['task_index'],
                protocol=distributed_spec.get('protocol'),
                config=None,
                start=True
            )

            if distributed_spec['task_index'] == 0:
                # TensorFlow chief session creator object
                session_creator = tf.train.ChiefSessionCreator(
                    scaffold=self.scaffold,
                    master=server.target,
                    config=None,
                    checkpoint_dir=None,
                    checkpoint_filename_with_path=None
                )
            else:
                # TensorFlow worker session creator object
                session_creator = tf.train.WorkerSessionCreator(
                    scaffold=self.scaffold,
                    master=server.target,
                    config=None
                )

            # TensorFlow monitored session object
            self.monitored_session = tf.train.MonitoredSession(
                session_creator=session_creator,
                hooks=hooks,
                stop_grace_period_secs=120  # Default value.
            )

        default_graph.__exit__(None, None, None)
        self.graph.finalize()
        self.monitored_session.__enter__()
        self.session = self.monitored_session._tf_sess()
            tf.add_to_collection("encoder_var", i)

    train_var = tf.get_collection("encoder_var")

    strtime = str(time.localtime().tm_mday) + "-" + str(
        time.localtime().tm_hour) + "-" + str(time.localtime().tm_min)
    session_file = open("session_file-" + strtime + ".txt", 'w')

    x_train_test = np.random.randint(0, 255, size=(256, 224, 224, 3))
    y_train_test = np.random.randint(0, 255, size=(256, 1))
    y_train_test = y_train_test / 256.0
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        saver = tf.train.Saver(var_list=resnet50_list)
        writer = tf.summary.FileWriter(r"./logs/batch128epochs40", sess.graph)
        #sess.run(tf.global_variables_initializer())
        sess.run(tf.variables_initializer(var_list=train_var))
        saver.restore(sess, path_to_ckpt)
        total_step = 1

        for e in range(epochs):
            current_epoch = e

            ## test code
            # _, losses, score_v, predictions_v, summary_v = sess.run(
            #         [train_step, total_loss, inputs_Y, predictions, merge_all],
            #         feed_dict={inputs_X: x_train_test,
            #                    inputs_Y: y_train_test})
            # writer.add_summary(summary_v, total_step)
            # total_step +=1
            # print("++Train::Eopchs = " + str(e).ljust(15) +"Loss = " + str(losses).ljust(15))
Example #48
0
    def train(self):
        with tf.Session() as sess:
            tvars = tf.trainable_variables()
            (assignment_map, initialized_variable_names
             ) = modeling.get_assignment_map_from_checkpoint(
                 tvars, self.__bert_checkpoint_path)
            print("init bert model params")
            tf.train.init_from_checkpoint(self.__bert_checkpoint_path,
                                          assignment_map)
            print("init bert model params done")
            sess.run(tf.variables_initializer(tf.global_variables()))

            current_step = 0
            start = time.time()
            for epoch in range(self.config["epochs"]):
                print("----- Epoch {}/{} -----".format(epoch + 1,
                                                       self.config["epochs"]))

                for batch in self.data_obj.next_batch(self.t_in_ids,
                                                      self.t_in_masks,
                                                      self.t_seg_ids,
                                                      self.t_lab_ids,
                                                      self.t_seq_len):

                    loss, true_y, predictions = self.model.train(
                        sess, batch, self.config["keep_prob"])

                    f1, precision, recall = gen_metrics(
                        pred_y=predictions,
                        true_y=true_y,
                        label_to_index=self.lab_to_idx)
                    print(
                        "train: step: {}, loss: {}, recall: {}, precision: {}, f1: {}"
                        .format(current_step, loss, recall, precision, f1))

                    current_step += 1
                    if self.data_obj and current_step % self.config[
                            "checkpoint_every"] == 0:

                        eval_losses = []
                        eval_recalls = []
                        eval_precisions = []
                        eval_f1s = []
                        for eval_batch in self.data_obj.next_batch(
                                self.e_in_ids, self.e_in_masks, self.e_seg_ids,
                                self.e_lab_ids, self.e_seq_len):
                            eval_loss, eval_true_y, eval_predictions = self.model.eval(
                                sess, eval_batch)

                            eval_losses.append(eval_loss)

                            f1, precision, recall = gen_metrics(
                                pred_y=eval_predictions,
                                true_y=eval_true_y,
                                labels=self.lab_to_idx)
                            eval_recalls.append(recall)
                            eval_precisions.append(precision)
                            eval_f1s.append(f1)
                        print("\n")
                        print(
                            "eval:  loss: {}, recall: {}, precision: {}, f1: {}"
                            .format(mean(eval_losses), mean(eval_recalls),
                                    mean(eval_precisions), mean(eval_f1s)))
                        print("\n")

                        if self.config["ckpt_model_path"]:
                            save_path = self.config["ckpt_model_path"]
                            if not os.path.exists(save_path):
                                os.makedirs(save_path)
                            model_save_path = os.path.join(
                                save_path, self.config["model_name"])
                            self.model.saver.save(sess,
                                                  model_save_path,
                                                  global_step=current_step)

            end = time.time()
            print("total train time: ", end - start)
Example #49
0
 def set_session(self, session):
     self.session = session
     init_op = tf.variables_initializer(self.params)
     self.session.run(init_op)
Example #50
0
 def reinitializeWeights(self, scopeName):
     variables = tf.contrib.framework.get_variables(scopeName)
     init = tf.variables_initializer(variables)
     self.sess.run(init)
Example #51
0
    def setup(self, cost_function, tf_compatible):
        """Sets up this optimizer using a given cost function.

        Arguments:
            cost_function (func): A function for computing costs over a batch of candidate solutions.
            tf_compatible (bool): True if the cost function provided is tf.Tensor-valued.

        Returns: None
        """
        if not tf_compatible or self.tf_sess is None:
            raise RuntimeError("Cannot pass in a tf.Tensor-valued cost function without passing in a TensorFlow "
                               "session into the constructor")

        self.tf_compatible = tf_compatible

        def continue_optimization(t, mean, var, best_val, best_sol,
                                  elites, returns):
            return tf.logical_and(tf.less(t, self.max_iters),
                                  tf.reduce_max(var) > self.epsilon)

        def iteration(t, mean, var, best_val, best_sol, elites, returns):
            # TODO: no sigmoid at the output?
            samples = tf.truncated_normal([self.popsize, self.sol_dim],
                                          mean, tf.sqrt(var))  # the noise

            costs = cost_function(
                samples, cem_type=self._params.cem_cfg.cem_type,
                tf_data_dict={'policy_network': self._policy_network,
                              'proposed_act_seqs': self._proposed_act_seqs_res}
            )
            values, indices = tf.nn.top_k(-costs, k=self.num_elites,
                                          sorted=True)

            # TODO: how do deal with different particles?
            best_val, best_sol = tf.cond(
                tf.less(-values[0], best_val),
                lambda: (-values[0], samples[indices[0]]),
                lambda: (best_val, best_sol)
            )

            elites = tf.gather(samples, indices)
            returns = -tf.gather(costs, indices)
            new_mean = tf.reduce_mean(elites, axis=0)
            new_var = tf.reduce_mean(tf.square(elites - new_mean), axis=0)

            mean = self.alpha * mean + (1 - self.alpha) * new_mean
            var = self.alpha * var + (1 - self.alpha) * new_var

            # return t + 1, mean, var, best_val, best_sol, trajs, acs, returns
            return t + 1, mean, var, best_val, best_sol, elites, returns

        with self.tf_sess.graph.as_default():
            self.init_returns = tf.Variable(np.zeros([self.num_elites]),
                                            dtype=tf.float32)

            self.init_elites = tf.tile(self.init_mean[None, :],
                                       [self.num_elites, 1])

            self.num_opt_iters, self.mean, self.var, self.best_val, \
                self.best_sol, self.elites, self.returns = \
                tf.while_loop(cond=continue_optimization, body=iteration,
                              loop_vars=[0, self.init_mean, self.init_var,
                                         float("inf"), self.init_mean,
                                         self.init_elites, self.init_returns])
            self.tf_sess.run(tf.variables_initializer(tf.global_variables()))
Example #52
0
    def train(self):
        """
        Trains policy on env using algo

        Pseudocode:
            for itr in n_itr:
                for step in num_inner_grad_steps:
                    sampler.sample()
                    algo.compute_updated_dists()
                algo.optimize_policy()
                sampler.update_goals()
        """
        with self.sess.as_default() as sess:

            # initialize uninitialized vars  (only initialize vars that were not loaded)
            uninit_vars = [
                var for var in tf.global_variables()
                if not sess.run(tf.is_variable_initialized(var))
            ]
            sess.run(tf.variables_initializer(uninit_vars))

            start_time = time.time()
            for itr in range(self.start_itr, self.n_itr):
                itr_start_time = time.time()
                logger.log(
                    "\n ---------------- Iteration %d ----------------" % itr)

                time_env_sampling_start = time.time()

                if self.initial_random_samples and itr == 0:
                    logger.log(
                        "Obtaining random samples from the environment...")
                    env_paths = self.sampler.obtain_samples(log=True,
                                                            random=True,
                                                            log_prefix='')
                elif self.initial_sinusoid_samples and itr == 0:
                    logger.log(
                        "Obtaining sinusoidal samples from the environment using the policy..."
                    )
                    env_paths = self.sampler.obtain_samples(log=True,
                                                            log_prefix='',
                                                            sinusoid=True)
                else:
                    logger.log(
                        "Obtaining samples from the environment using the policy..."
                    )
                    env_paths = self.sampler.obtain_samples(log=True,
                                                            log_prefix='')

                logger.record_tabular('Time-EnvSampling',
                                      time.time() - time_env_sampling_start)
                logger.log("Processing environment samples...")

                # first processing just for logging purposes
                time_env_samp_proc = time.time()
                samples_data = self.dynamics_sample_processor.process_samples(
                    env_paths, log=True, log_prefix='EnvTrajs-')
                logger.record_tabular('Time-EnvSampleProc',
                                      time.time() - time_env_samp_proc)
                ''' --------------- fit dynamics model --------------- '''

                time_fit_start = time.time()

                logger.log("Training dynamics model for %i epochs ..." %
                           (self.dynamics_model_max_epochs))
                self.dynamics_model.fit(samples_data['observations'],
                                        samples_data['actions'],
                                        samples_data['next_observations'],
                                        epochs=self.dynamics_model_max_epochs,
                                        verbose=False,
                                        log_tabular=True)

                logger.record_tabular('Time-ModelFit',
                                      time.time() - time_fit_start)
                """ ------------------- Logging Stuff --------------------------"""
                logger.logkv('Itr', itr)
                logger.logkv('n_timesteps',
                             self.sampler.total_timesteps_sampled)

                logger.logkv('Time', time.time() - start_time)
                logger.logkv('ItrTime', time.time() - itr_start_time)

                logger.log("Saving snapshot...")
                params = self.get_itr_snapshot(itr)
                self.log_diagnostics(env_paths, '')
                logger.save_itr_params(itr, params)
                logger.log("Saved")

                logger.dumpkvs()
                if itr == 0:
                    sess.graph.finalize()

        logger.log("Training finished")
        self.sess.close()
Example #53
0
def main(_):
    global ckpt_path
    global last_f1
    if not os.path.exists(ckpt_path):
        os.makedirs(ckpt_path)
    if not os.path.exists(summary_path):
        os.makedirs(summary_path)
    elif not FLAGS.is_retrain:  # 重新训练本模型,删除以前的 summary
        shutil.rmtree(summary_path)
        os.makedirs(summary_path)
    if not os.path.exists(summary_path):
        os.makedirs(summary_path)

    print('1.Loading data...')
    W_embedding = np.load(embedding_path)
    print('training sample_num = %d' % n_tr_batches)
    print('valid sample_num = %d' % n_va_batches)

    # Initial or restore the model
    print('2.Building model...')
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        model = RCNN(W_embedding, settings)
        with tf.variable_scope('training_ops') as vs:
            learning_rate = tf.train.exponential_decay(FLAGS.lr, model.global_step, FLAGS.decay_step,
                                                   FLAGS.decay_rate, staircase=True)
            # two optimizer: op1, update embedding; op2, do not update embedding.
            with tf.variable_scope('Optimizer1'):
                tvars1 = tf.trainable_variables()
                grads1 = tf.gradients(model.loss, tvars1)
                optimizer1 = tf.train.AdamOptimizer(learning_rate=learning_rate)
                train_op1 = optimizer1.apply_gradients(zip(grads1, tvars1),
                                                   global_step=model.global_step)
            with tf.variable_scope('Optimizer2'):
                tvars2 = [tvar for tvar in tvars1 if 'embedding' not in tvar.name]
                grads2 = tf.gradients(model.loss, tvars2)
                optimizer2 = tf.train.AdamOptimizer(learning_rate=learning_rate)
                train_op2 = optimizer2.apply_gradients(zip(grads2, tvars2),
                                                   global_step=model.global_step)
            update_op = tf.group(*model.update_emas)
            merged = tf.summary.merge_all()  # summary
            train_writer = tf.summary.FileWriter(summary_path + 'train', sess.graph)
            test_writer = tf.summary.FileWriter(summary_path + 'test')
            training_ops = [v for v in tf.global_variables() if v.name.startswith(vs.name+'/')]

        # 如果已经保存过模型,导入上次的模型
        if os.path.exists(ckpt_path + "checkpoint"):
            print("Restoring Variables from Checkpoint...")
            model.saver.restore(sess, tf.train.latest_checkpoint(ckpt_path))
            last_valid_cost, precision, recall, last_f1 = valid_epoch(data_valid_path, sess, model)
            print(' valid cost=%g; p=%g, r=%g, f1=%g' % (last_valid_cost, precision, recall, last_f1))
            sess.run(tf.variables_initializer(training_ops))
            train_op2 = train_op1
        else:
            print('Initializing Variables...')
            sess.run(tf.global_variables_initializer())

        print('3.Begin training...')
        print('max_epoch=%d, max_max_epoch=%d' % (FLAGS.max_epoch, FLAGS.max_max_epoch))
        train_op = train_op2
        for epoch in range(FLAGS.max_max_epoch):
            global_step = sess.run(model.global_step)
            print('Global step %d, lr=%g' % (global_step, sess.run(learning_rate)))
            if epoch == FLAGS.max_epoch:  # update the embedding
                train_op = train_op1
            train_fetches = [merged, model.loss, model.accuracy, train_op, update_op]
            valid_fetches = [merged, model.loss,model.accuracy]
            train_epoch(data_train_path, sess, model, train_fetches, valid_fetches, train_writer, test_writer)
        # 最后再做一次验证
        valid_cost, precision, recall, f1 = valid_epoch(data_valid_path, sess, model)
        print('END.Global_step=%d: valid cost=%g; p=%g, r=%g, f1=%g' % (
            sess.run(model.global_step), valid_cost, precision, recall, f1))
        if f1 > last_f1:  # save the better model
            saving_path = model.saver.save(sess, model_path, sess.run(model.global_step)+1)
            print('saved new model to %s ' % saving_path)
Example #54
0
File: net.py Project: czgdp1807/wql
    def _build(self, convnet_pars):
        with tf.variable_scope(None, default_name=self._name):
            self._scope_name = tf.get_default_graph().get_name_scope() + '/'

            with tf.variable_scope('State'):
                self._x = tf.placeholder(tf.float32,
                                         shape=[None] +
                                         list(convnet_pars['input_shape']),
                                         name='input')

            with tf.variable_scope('Action'):
                self._action = tf.placeholder('uint8', [None], name='action')

                action_one_hot = tf.one_hot(self._action,
                                            convnet_pars['output_shape'][0],
                                            name='action_one_hot')

            with tf.variable_scope('Mask'):
                self._mask = tf.placeholder(
                    tf.float32, shape=[None, convnet_pars['n_approximators']])

            if convnet_pars['n_states'] is not None:
                x = tf.one_hot(tf.cast(self._x[..., 0], tf.int32),
                               convnet_pars['n_states'])
            else:
                x = self._x[...]

            self._features = list()
            self._features2 = list()
            self._q = list()
            self._q_acted = list()
            self.n_approximators = convnet_pars['n_approximators']
            self.q_min = convnet_pars['q_min']
            self.q_max = convnet_pars['q_max']
            self.init_type = convnet_pars['init_type']

            if self.init_type == 'boot':
                kernel_initializer = lambda _: tf.glorot_uniform_initializer()
                bias_initializer = lambda _: tf.zeros_initializer()
            else:
                initial_values = np.linspace(self.q_min, self.q_max,
                                             self.n_approximators)
                kernel_initializer = lambda _: tf.glorot_uniform_initializer()
                bias_initializer = lambda i: tf.constant_initializer(
                    initial_values[i])

            for i in range(self.n_approximators):
                with tf.variable_scope('head_' + str(i)):
                    if convnet_pars["net_type"] == 'features':
                        self._features.append(
                            tf.layers.dense(x,
                                            24,
                                            activation=tf.nn.relu,
                                            kernel_initializer=tf.
                                            glorot_uniform_initializer(),
                                            name='features_' + str(i)))
                        self._features2.append(
                            tf.layers.dense(self._features[i],
                                            48,
                                            activation=tf.nn.relu,
                                            kernel_initializer=tf.
                                            glorot_uniform_initializer(),
                                            name='features2_' + str(i)))
                        self._q.append(
                            tf.layers.dense(
                                self._features2[i],
                                convnet_pars['output_shape'][0],
                                kernel_initializer=kernel_initializer(i),
                                bias_initializer=bias_initializer(i),
                                name='q_' + str(i)))
                        self._q_acted.append(
                            tf.reduce_sum(self._q[i] * action_one_hot,
                                          axis=1,
                                          name='q_acted_' + str(i)))
                    else:
                        self._q.append(
                            tf.layers.dense(
                                x,
                                convnet_pars['output_shape'][0],
                                kernel_initializer=kernel_initializer(i),
                                bias_initializer=bias_initializer(i),
                                name='q_' + str(i)))
                        self._q_acted.append(
                            tf.reduce_sum(self._q[i] * action_one_hot,
                                          axis=1,
                                          name='q_acted_' + str(i)))

            self._q_acted = tf.transpose(self._q_acted)

            self._target_q = tf.placeholder(
                'float32', [None, convnet_pars['n_approximators']],
                name='target_q')
            self._margin = tf.placeholder('float32', (), name='margin')
            self._q_acted_sorted = tf.contrib.framework.sort(self._q_acted,
                                                             axis=1)
            self._target_q_sorted = tf.contrib.framework.sort(self._target_q,
                                                              axis=1)

            loss = 0.
            if convnet_pars["loss"] == "huber_loss":
                self.loss_fuction = tf.losses.huber_loss
            else:
                self.loss_fuction = tf.losses.mean_squared_error
            k = convnet_pars['n_approximators']
            if convnet_pars["loss"] == "triple_loss":

                loss = SimpleNet.triple_loss(self._q_acted_sorted,
                                             self._target_q_sorted, k,
                                             self._margin)
            else:
                for i in range(convnet_pars['n_approximators']):

                    loss += self.loss_fuction(self._target_q_sorted[:, i],
                                              self._q_acted_sorted[:, i])

            self._prob_exploration = tf.placeholder('float32', (),
                                                    name='prob_exploration')
            tf.summary.scalar(convnet_pars["loss"], loss)
            tf.summary.scalar('average_q', tf.reduce_mean(self._q))
            # tf.summary.scalar('average_std', tf.reduce_mean(tf.sqrt(tf.nn.moments(self._q, axes=[0])[1])))
            tf.summary.scalar('prob_exploration', self._prob_exploration)
            tf.summary.scalar(
                'std_acted',
                tf.reduce_mean(tf.nn.moments(self._q_acted, axes=1)[1]))
            tf.summary.histogram('qs', tf.reduce_mean(self._q_acted, axis=0))

            self._merged = tf.summary.merge(
                tf.get_collection(tf.GraphKeys.SUMMARIES,
                                  scope=self._scope_name))

            optimizer = convnet_pars['optimizer']
            if optimizer['name'] == 'rmspropcentered':
                opt = tf.train.RMSPropOptimizer(learning_rate=optimizer['lr'],
                                                decay=optimizer['decay'],
                                                epsilon=optimizer['epsilon'],
                                                centered=True)
            elif optimizer['name'] == 'rmsprop':
                opt = tf.train.RMSPropOptimizer(learning_rate=optimizer['lr'],
                                                decay=optimizer['decay'],
                                                epsilon=optimizer['epsilon'])
            elif optimizer['name'] == 'adam':
                opt = tf.train.AdamOptimizer(learning_rate=optimizer['lr'])
            elif optimizer['name'] == 'adadelta':
                opt = tf.train.AdadeltaOptimizer(learning_rate=optimizer['lr'])
            else:
                raise ValueError('Unavailable optimizer selected.')

            self._train_step = opt.minimize(loss=loss)

            initializer = tf.variables_initializer(
                tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                  scope=self._scope_name))

        self._session.run(initializer)

        if self._folder_name is not None:
            self._train_writer = tf.summary.FileWriter(
                self._folder_name + '/' + self._scope_name[:-1],
                graph=tf.get_default_graph())

        self._train_count = 0

        self._add_collection()
Example #55
0
def main():
  print('Starting...')
  model_dir = ModelDir(OPTS.model)
  model = model_dir.get_model()
  if OPTS.elmo:
    # Fix absolute path names from other codalab runs
    lm = model.lm_model
    if lm.lm_vocab_file.startswith('/0x'):
      lm.lm_vocab_file = os.sep.join(lm.lm_vocab_file.split(os.sep)[2:])
    if lm.options_file.startswith('/0x'):
      lm.options_file = os.sep.join(lm.options_file.split(os.sep)[2:])
    if lm.weight_file.startswith('/0x'):
      lm.weight_file = os.sep.join(lm.weight_file.split(os.sep)[2:])
    if lm.weight_file.startswith('/0x'):
      lm.embed_weights_file = os.sep.join(lm.embed_weights_file.split(os.sep)[2:])
    lm.embed_weights_file = None

  #if not isinstance(model, ParagraphQuestionModel):
  #  raise ValueError("This script is built to work for ParagraphQuestionModel models only")
  input_data, vocab = read_input_data(model)

  print('Loading word vectors...')
  model.set_input_spec(ParagraphAndQuestionSpec(batch_size=None), vocab)

  print('Starting Tensorflow session...')
  config = tf.ConfigProto(allow_soft_placement=True)
  config.gpu_options.allow_growth = True
  sess = tf.Session(config=config)
  with sess.as_default():
    prediction = model.get_prediction()
    # Take 0-th here because we know we only truncate to one paragraph
    start_logits_tf = prediction.start_logits[0]
    end_logits_tf = prediction.end_logits[0]
    none_logit_tf = prediction.none_logit[0]
  if OPTS.elmo:
    # See elmo/run_on_user_text.py
    all_vars = tf.global_variables() + tf.get_collection(tf.GraphKeys.SAVEABLE_OBJECTS)
    lm_var_names = {x.name for x in all_vars if x.name.startswith("bilm")}
    vars = [x for x in all_vars if x.name not in lm_var_names]
    model_dir.restore_checkpoint(sess, vars)
    sess.run(tf.variables_initializer([x for x in all_vars if x.name in lm_var_names]))
  else:
    model_dir.restore_checkpoint(sess)

  pred_obj = {}
  na_prob_obj = {}
  pred_always_ans_obj = {}
  analysis_obj = {}

  for context_raw, context_toks, ex in tqdm(input_data):
    encoded = model.encode(ex, is_train=False)
    start_logits, end_logits, none_logit = sess.run(
        [start_logits_tf, end_logits_tf, none_logit_tf],
        feed_dict=encoded)
    # beam, p_na = logits_to_probs(
    #     context_raw, context_toks, start_logits, end_logits, none_logit,
    #     beam_size=DEFAULT_BEAM_SIZE)
    beam, p_na = logits_to_probs(
        context_raw, context_toks, start_logits, end_logits, none_logit,
        beam_size=10)

    # print(beam[0][0])

    ans = beam[0][0]
    # start, end = beam[0][2],beam[0][3]
    non_empty_ans = [x[0] for x in beam if x[0]][0]
    qid = ex[0].question_id

    pred_obj[qid] = ans
    na_prob_obj[qid] = p_na
    pred_always_ans_obj[qid] = non_empty_ans
    analysis_obj[qid] = [{'answer': b[0], 'span':[b[2], b[3]], 'prob':b[1]} for b in beam] 
    # print(analysis_obj[qid])

  with open(OPTS.output_file, 'w') as f:
    json.dump(pred_obj, f)
  if OPTS.na_prob_file:
    with open(OPTS.na_prob_file, 'w') as f:
      json.dump(na_prob_obj, f)
  if OPTS.always_answer_file:
    with open(OPTS.always_answer_file, 'w') as f:
      json.dump(pred_always_ans_obj, f)
  if OPTS.analysis_file:
    with open(OPTS.analysis_file, 'w') as f:
      json.dump(analysis_obj, f, indent=2)
Example #56
0
    def __init__(
            self,
            base_kwargs,
            env,
            arr_actor,
            best_actor,
            dict_ph,
            arr_initial_exploration_policy,
            with_best = False,
            initial_beta_t = 1,
            plotter=None,
            specific_type=0,

            target_noise_scale=0.2,
            target_noise_clip=0.5,
            target_ratio=2,
            target_range=0.04,
            lr=3e-3,
            discount=0.99,
            tau=0.01,
            policy_update_interval=2,
            best_update_interval=2,
            reparameterize=False,

            save_full_state=False,
    ):
        """
        Args:
            base_kwargs (dict): dictionary of base arguments that are directly
                passed to the base `RLAlgorithm` constructor.

            env (`rllab.Env`): rllab environment object. 
            policy: (`rllab.NNPolicy`): A policy function approximator.
            initial_exploration_policy: ('Policy'): A policy that we use
                for initial exploration which is not trained by the algorithm.

            qf1 (`valuefunction`): First Q-function approximator.
            qf2 (`valuefunction`): Second Q-function approximator. Usage of two
                Q-functions improves performance by reducing overestimation
                bias.
            vf (`ValueFunction`): Soft value function approximator.

            pool (`PoolBase`): Replay buffer to add gathered samples to.
            plotter (`QFPolicyPlotter`): Plotter instance to be used for
                visualizing Q-function during training.

            lr (`float`): Learning rate used for the function approximators.
            discount (`float`): Discount factor for Q-function updates.
            tau (`float`): Soft value function target update weight.
            target_update_interval ('int'): Frequency at which target network
                updates occur in iterations.

            reparameterize ('bool'): If True, we use a gradient estimator for
                the policy derived using the reparameterization trick. We use
                a likelihood ratio based estimator otherwise. 
            save_full_state (`bool`): If True, save the full class in the
                snapshot. See `self.get_snapshot` for more information.
        """

        Serializable.quick_init(self, locals())
        super(P3S_TD3, self).__init__(**base_kwargs)

        self._env = env
        self._max_actions = int(self._env.action_space.high[0])

        self._arr_actor = arr_actor
        self._best_actor = best_actor
        self._best_actor_num = -1
        self._num_iter_select_best = 1

        assert len(self._env.envs) == len(self._arr_actor)
        self._num_actor = len(self._arr_actor)
        self._n_train_repeat = self._num_actor
        self._dict_ph = dict_ph

        self._arr_initial_exploration_policy = arr_initial_exploration_policy
        self._with_best = with_best
        self._best_flag = np.ones(self._num_actor)
        self._beta_t = initial_beta_t
        self._plotter = plotter

        self._target_noise_scale = target_noise_scale
        self._target_noise_clip = target_noise_clip

        self._target_ratio = target_ratio
        self._target_range = target_range
        self._policy_lr = lr
        self._qf_lr = lr
        self._vf_lr = lr
        self._discount = discount
        self._tau = tau
        self._policy_update_interval = policy_update_interval
        self._best_update_interval = best_update_interval

        # Reparameterize parameter must match between the algorithm and the 
        # policy actions are sampled from.

        self._save_full_state = save_full_state
        self._saver = tf.train.Saver(max_to_keep=1000)
        self._save_dir = '/home/wisrl/wyjung/Result/log/Mujoco/ant_delay20/test_IPE_TD3_NA4_TRatio2_Trange0.03_update1_ver3_new_201906/iter6/'
        # '/test_IPE_TD3_NA' + str(NUM_ACTORS) + '_TRatio' + str(TARGET_RATIO) + '_TRange' + str(
        #     TARGET_RANGE) + '_update' + str(UPDATE_BEST_ITER) + '_ver' + str(VERSION) + '_new_201906'
        self._save_iter_num = 40000

        self._Da = self._env.action_space.flat_dim
        self._Do = self._env.observation_space.flat_dim

        if self._best_actor is not None:
            self._init_critic_update(actor=self._best_actor)
            self._init_actor_update(actor=self._best_actor)
            self._init_target_ops(actor=self._best_actor)

        for actor in self._arr_actor:
            self._init_critic_update(actor=actor)
            self._init_actor_update(actor=actor)
            self._init_target_ops(actor=actor)
            self._init_update_old_new_ops(actor=actor)

        self._sess.run(tf.variables_initializer([
            variable for variable in tf.global_variables()
            if 'low_level_policy' not in variable.name
        ]))

        self._update_old_new()

        for actor in self._arr_actor:
            source_params = actor.current_params()
            target_params = actor.target_params()
            copy_ops = [
                tf.assign(target, source)
                for target, source in zip(target_params, source_params)
            ]

            self._sess.run(copy_ops)

        if self._best_actor is not None:
            source_params = self._best_actor.current_params()
            target_params = self._best_actor.target_params()
            copy_ops = [
                tf.assign(target, source)
                for target, source in zip(target_params, source_params)
            ]

            self._sess.run(copy_ops)

            for actor in self._arr_actor:
                source_params = self._best_actor.trainable_params()
                target_params = actor.trainable_params()

                copy_ops = [
                    tf.assign(target, source)
                    for target, source in zip(target_params, source_params)
                ]

                self._sess.run(copy_ops)

        print("Initialization is finished!")
def run(filename,mode,batch_size=1000,repeat_times=100,test_ep=10,save_ep=50,lamb=0.00001,sess=None,restore_ep=0):
    dataset=load_from_disk([filename])

    params={"batch_size":batch_size,"repeat_times":repeat_times}

    # sess.run(mat)
    if mode.startswith("train"):
        mat = input(dataset, mode, params)
        # op,grads=model(mat,mode,lamb=lamb)
        if mode=='train_multi':
            opt=model(mat,mode,lamb=lamb)
        else:
            met_opt,opt=model(mat,mode,lamb=lamb)
        # gd=[]
        # var=[]
        # for pair in(grads):
        #     gd.append(pair[0])
        #     var.append(pair[1])
        tr_metric_init_op = tf.variables_initializer(
            tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="AE_TR_METRIC"))
    else:
        assert mode.startswith('test')
        mat1,mat2=input(dataset,mode,params)
        op=model(tr_mat=mat1,mode=mode,lamb=lamb,tst_mat=mat2)
        tr_metric_init_op = tf.variables_initializer(
            tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="AE_TST_METRIC"))


    if sess==None:
        config = tf.ConfigProto(
            allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)



    if mode.startswith("train"):
        num_steps = math.ceil(FLAGS.item_num / batch_size)
        saver=tf.train.Saver()
        if restore_ep==0:
            init = tf.global_variables_initializer()
            sess.run(init)
        else:
            saver.restore(sess,"%s/ae%s%d"%(FLAGS.model_path,mode,restore_ep))
        # Training
        ti=time.time()
        for epoch in range(1,repeat_times+1):
            sess.run(tr_metric_init_op)
            for i in range(1, num_steps + 1):
                print("epoch:%d,step:%d   " % (restore_ep + epoch, i))
                if mode=='train_multi':
                    _=sess.run([opt])
                else:
                    metric,_=sess.run([met_opt,opt])
                    print(str(metric))

                # for j1,j2 in zip(gdval,var):
                #     print(str(j1)+'hh')
                #     print(str(j2))
            dur=time.time()-ti
            ti=time.time()
            print("time:"+str(dur))

            if (epoch+restore_ep)%save_ep==0 or epoch==repeat_times:
                save_path="%s/ae%s%d"%(FLAGS.model_path,mode,epoch+restore_ep)
                saver.save(sess,save_path)
                print("Model Saved!")
            if (epoch+restore_ep) % test_ep == 0 or epoch == repeat_times:
                run(FLAGS.test_file,'test',sess=sess,batch_size=2000)


    if mode.startswith("test"):
        num_steps = math.ceil(FLAGS.test_item_num / batch_size)
        sess.run(tr_metric_init_op)
        for i in range(1,num_steps+1):
            metric=sess.run(op)
            print("MSRE:"+str(metric))
Example #58
0
def main(_):
    tic = time.time()
    print('tensorflow version:', tf.__version__)
    tf.logging.set_verbosity(tf.logging.INFO)
    if not FLAGS.dataset_dir:
        raise ValueError(
            'You must supply the dataset directory with --dataset_dir')
    # init
    net_name_scope_pruned = FLAGS.net_name_scope_pruned
    net_name_scope_checkpoint = FLAGS.net_name_scope_checkpoint
    indexed_prune_scopes_for_units = valid_indexed_prune_scopes_for_units
    kept_percentages = sorted(map(float, FLAGS.kept_percentages.split(',')))

    num_options = len(kept_percentages)
    num_units = len(indexed_prune_scopes_for_units)
    print('num_options=%d, num_blocks=%d' % (num_options, num_units))
    print('HG: total number of configurations=%d' % (num_options**num_units))

    # find the  configurations to evaluate
    if FLAGS.configuration_type == 'sample':
        configs = get_sampled_configurations(num_units, num_options,
                                             FLAGS.total_num_configurations)
    elif FLAGS.configuration_type == 'special':
        configs = get_special_configurations(num_units, num_options)
    num_configurations = len(configs)

    #Getting MPI rank integer
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    if rank >= num_configurations:
        print("ERROR: rank(%d) > num_configurations(%d)" %
              (rank, num_configurations))
        return
    FLAGS.configuration_index = FLAGS.start_configuration_index + rank
    config = configs[FLAGS.configuration_index]
    print('HG: kept_percentages=%s, start_config_index=%d, num_configs=%d, rank=%d, config_index=%d' \
           %(str(kept_percentages), FLAGS.start_configuration_index, num_configurations, rank, FLAGS.configuration_index))

    # prepare for training with the specific config
    indexed_prune_scopes, kept_percentage = config_to_indexed_prune_scopes(
        config, indexed_prune_scopes_for_units, kept_percentages)
    prune_info = indexed_prune_scopes_to_prune_info(indexed_prune_scopes,
                                                    kept_percentage)

    # prepare file system
    results_dir = os.path.join(
        FLAGS.train_dir, 'id' +
        str(FLAGS.configuration_index))  #+'_'+str(FLAGS.max_number_of_steps))
    train_dir = os.path.join(results_dir, 'train')

    if (not FLAGS.continue_training) or (
            not tf.train.latest_checkpoint(train_dir)):
        prune_scopes = indexed_prune_scopes_to_prune_scopes(
            indexed_prune_scopes, net_name_scope_checkpoint)
        shorten_scopes = indexed_prune_scopes_to_shorten_scopes(
            indexed_prune_scopes, net_name_scope_checkpoint)
        variables_init_value = get_init_values_for_pruned_layers(
            prune_scopes, shorten_scopes, kept_percentage)
        reinit_scopes = [
            re.sub(net_name_scope_checkpoint, net_name_scope_pruned, v)
            for v in prune_scopes + shorten_scopes
        ]

        prepare_file_system(train_dir)

    def write_detailed_info(info):
        with open(os.path.join(train_dir, 'train_details.txt'), 'a') as f:
            f.write(info + '\n')

    info = 'train_dir:' + train_dir + '\n'
    info += 'options:' + str(kept_percentages) + '\n'
    info += 'configuration: ' + str(config) + '\n'
    info += 'indexed_prune_scopes: ' + str(indexed_prune_scopes) + '\n'
    info += 'kept_percentage: ' + str(kept_percentage)
    print(info)
    write_detailed_info(info)

    with tf.Graph().as_default():

        #######################
        # Config model_deploy #
        #######################
        deploy_config = model_deploy.DeploymentConfig(
            num_clones=FLAGS.num_clones,
            clone_on_cpu=FLAGS.clone_on_cpu,
            replica_id=FLAGS.task,
            num_replicas=FLAGS.worker_replicas,
            num_ps_tasks=FLAGS.num_ps_tasks)

        ######################
        # Select the dataset #
        ######################
        dataset = dataset_factory.get_dataset(FLAGS.dataset_name,
                                              FLAGS.train_dataset_name,
                                              FLAGS.dataset_dir)
        test_dataset = dataset_factory.get_dataset(FLAGS.dataset_name,
                                                   FLAGS.test_dataset_name,
                                                   FLAGS.dataset_dir)

        batch_queue = train_inputs(dataset, deploy_config, FLAGS)
        test_images, test_labels = test_inputs(test_dataset, deploy_config,
                                               FLAGS)
        images, labels = batch_queue.dequeue()

        ######################
        # Select the network#
        ######################

        network_fn_pruned = nets_factory.get_network_fn_pruned(
            FLAGS.model_name,
            prune_info=prune_info,
            num_classes=(dataset.num_classes - FLAGS.labels_offset),
            weight_decay=FLAGS.weight_decay)
        print('HG: prune_info:')
        pprint(prune_info)

        ####################
        # Define the model #
        ####################
        logits_train, _ = network_fn_pruned(images,
                                            is_training=True,
                                            is_local_train=False,
                                            reuse_variables=False,
                                            scope=net_name_scope_pruned)
        logits_eval, _ = network_fn_pruned(test_images,
                                           is_training=False,
                                           is_local_train=False,
                                           reuse_variables=True,
                                           scope=net_name_scope_pruned)

        cross_entropy = add_cross_entropy(logits_train, labels)
        correct_prediction = add_correct_prediction(logits_eval, test_labels)

        #############################
        # Specify the loss function #
        #############################
        tf.add_to_collection('subgraph_losses', cross_entropy)
        # get regularization loss
        regularization_losses = get_regularization_losses_within_scopes()
        print_list('regularization_losses', regularization_losses)

        # total loss and its summary
        total_loss = tf.add_n(tf.get_collection('subgraph_losses'),
                              name='total_loss')
        for l in tf.get_collection('subgraph_losses') + [total_loss]:
            tf.summary.scalar(l.op.name + '/summary', l)

        #########################################
        # Configure the optimization procedure. #
        #########################################
        with tf.device(deploy_config.variables_device()):
            global_step = tf.Variable(0, trainable=False, name='global_step')

        with tf.device(deploy_config.optimizer_device()):
            learning_rate = configure_learning_rate(dataset.num_samples,
                                                    global_step, FLAGS)
            optimizer = configure_optimizer(learning_rate, FLAGS)
            tf.summary.scalar('learning_rate', learning_rate)

        #############################
        # Add train operation       #
        #############################
        variables_to_train = get_trainable_variables_within_scopes()
        train_op = add_train_op(optimizer,
                                total_loss,
                                global_step,
                                var_list=variables_to_train)
        print_list("variables_to_train", variables_to_train)

        # Gather update_ops: the updates for the batch_norm variables created by network_fn_pruned.
        update_ops = get_update_ops_within_scopes()
        print_list("update_ops", update_ops)

        # add train_tensor
        update_ops.append(train_op)
        update_op = tf.group(*update_ops)
        with tf.control_dependencies([update_op]):
            train_tensor = tf.identity(total_loss, name='train_op')

        # add summary op
        summary_op = tf.summary.merge_all()

        print("HG: trainable_variables=", len(tf.trainable_variables()))
        print("HG: model_variables=", len(tf.model_variables()))
        print("HG: global_variables=", len(tf.global_variables()))

        sess_config = tf.ConfigProto(intra_op_parallelism_threads=16,
                                     inter_op_parallelism_threads=16)
        with tf.Session(config=sess_config) as sess:
            ###########################
            # Prepare for filewriter. #
            ###########################
            train_writer = tf.summary.FileWriter(train_dir, sess.graph)

            # if restart the training or there is no checkpoint in the train_dir
            if (not FLAGS.continue_training) or (
                    not tf.train.latest_checkpoint(train_dir)):
                #########################################
                # Reinit  pruned model variable  #
                #########################################
                variables_to_reinit = get_model_variables_within_scopes(
                    reinit_scopes)
                print_list("Initialize pruned variables", variables_to_reinit)
                assign_ops = []
                for v in variables_to_reinit:
                    key = re.sub(net_name_scope_pruned,
                                 net_name_scope_checkpoint, v.op.name)
                    if key in variables_init_value:
                        value = variables_init_value.get(key)
                        # print(key, value)
                        assign_ops.append(
                            tf.assign(v,
                                      tf.convert_to_tensor(value),
                                      validate_shape=True))
                        # v.set_shape(value.shape)
                    else:
                        raise ValueError(
                            "Key not in variables_init_value, key=", key)
                assign_op = tf.group(*assign_ops)
                sess.run(assign_op)

                #################################################
                # Restore unchanged model variable. #
                #################################################
                variables_to_restore = {
                    re.sub(net_name_scope_pruned, net_name_scope_checkpoint,
                           v.op.name): v
                    for v in get_model_variables_within_scopes()
                    if v not in variables_to_reinit
                }
                print_list("restore model variables",
                           variables_to_restore.values())
                load_checkpoint(sess,
                                FLAGS.checkpoint_path,
                                var_list=variables_to_restore)

            else:
                ###########################################
                ## Restore all variables from checkpoint ##
                ###########################################
                variables_to_restore = get_global_variables_within_scopes()
                load_checkpoint(sess, train_dir, var_list=variables_to_restore)

            #################################################
            # init unitialized global variable. #
            #################################################
            variables_to_init = get_global_variables_within_scopes(
                sess.run(tf.report_uninitialized_variables()))
            print_list("init unitialized variables", variables_to_init)
            sess.run(tf.variables_initializer(variables_to_init))

            init_global_step_value = sess.run(global_step)
            print('initial global step: ', init_global_step_value)
            if init_global_step_value >= FLAGS.max_number_of_steps:
                print('Exit: init_global_step_value (%d) >= FLAG.max_number_of_steps (%d)' \
                    %(init_global_step_value, FLAGS.max_number_of_steps))
                return

            ###########################
            # Record CPU usage  #
            ###########################
            mpstat_output_filename = os.path.join(train_dir, "cpu-usage.log")
            os.system("mpstat -P ALL 1 > " + mpstat_output_filename +
                      " 2>&1 &")

            ###########################
            # Kicks off the training. #
            ###########################
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            saver = tf.train.Saver(max_to_keep=FLAGS.max_to_keep)
            print('HG: # of threads=', len(threads))

            duration = 0
            duration_cnt = 0
            train_time = 0
            train_only_cnt = 0

            print("start to train at:", datetime.now())
            for i in range(init_global_step_value,
                           FLAGS.max_number_of_steps + 1):
                # run optional meta data, or summary, while run train tensor
                #if i < FLAGS.max_number_of_steps:
                if i > init_global_step_value:
                    # train while run metadata
                    if i % FLAGS.runmeta_every_n_steps == FLAGS.runmeta_every_n_steps - 1:
                        run_options = tf.RunOptions(
                            trace_level=tf.RunOptions.FULL_TRACE)
                        run_metadata = tf.RunMetadata()

                        loss_value = sess.run(train_tensor,
                                              options=run_options,
                                              run_metadata=run_metadata)
                        train_writer.add_run_metadata(run_metadata,
                                                      'step%d-train' % i)

                        # Create the Timeline object, and write it to a json file
                        fetched_timeline = timeline.Timeline(
                            run_metadata.step_stats)
                        chrome_trace = fetched_timeline.generate_chrome_trace_format(
                        )
                        with open(
                                os.path.join(train_dir,
                                             'timeline_' + str(i) + '.json'),
                                'w') as f:
                            f.write(chrome_trace)

                    # train while record summary
                    elif i % FLAGS.summary_every_n_steps == 0:
                        train_summary, loss_value = sess.run(
                            [summary_op, train_tensor])
                        train_writer.add_summary(train_summary, i)

                    # train only
                    else:
                        start_time = time.time()
                        loss_value = sess.run(train_tensor)
                        train_only_cnt += 1
                        train_time += time.time() - start_time
                        duration_cnt += 1
                        duration += time.time() - start_time

                    # log loss information
                    if i % FLAGS.log_every_n_steps == 0 and duration_cnt > 0:
                        log_frequency = duration_cnt
                        examples_per_sec = log_frequency * FLAGS.batch_size / duration
                        sec_per_batch = float(duration / log_frequency)
                        summary = tf.Summary()
                        summary.value.add(tag='examples_per_sec',
                                          simple_value=examples_per_sec)
                        summary.value.add(tag='sec_per_batch',
                                          simple_value=sec_per_batch)
                        train_writer.add_summary(summary, i)
                        format_str = (
                            '%s: step %d, loss = %.3f (%.1f examples/sec; %.3f sec/batch)'
                        )
                        print(format_str % (datetime.now(), i, loss_value,
                                            examples_per_sec, sec_per_batch))
                        duration = 0
                        duration_cnt = 0

                        info = format_str % (datetime.now(), i, loss_value,
                                             examples_per_sec, sec_per_batch)
                        write_detailed_info(info)
                else:
                    # run only total loss when i=0
                    train_summary, loss_value = sess.run(
                        [summary_op,
                         total_loss])  #loss_value = sess.run(total_loss)
                    train_writer.add_summary(train_summary, i)
                    format_str = ('%s: step %d, loss = %.3f')
                    print(format_str % (datetime.now(), i, loss_value))
                    info = format_str % (datetime.now(), i, loss_value)
                    write_detailed_info(info)

                # record the evaluation accuracy
                is_last_step = (i == FLAGS.max_number_of_steps)
                if i % FLAGS.evaluate_every_n_steps == 0 or is_last_step:
                    #run_meta = (i==FLAGS.evaluate_every_n_steps)
                    test_accuracy, run_metadata = evaluate_accuracy(
                        sess,
                        coord,
                        test_dataset.num_samples,
                        test_images,
                        test_labels,
                        test_images,
                        test_labels,
                        correct_prediction,
                        FLAGS.test_batch_size,
                        run_meta=False)
                    summary = tf.Summary()
                    summary.value.add(tag='accuracy',
                                      simple_value=test_accuracy)
                    train_writer.add_summary(summary, i)
                    #if run_meta:
                    #    eval_writer.add_run_metadata(run_metadata, 'step%d-eval' % i)

                    info = ('%s: step %d, test_accuracy = %.6f') % (
                        datetime.now(), i, test_accuracy)
                    print(info)
                    write_detailed_info(info)

                    ###########################
                    # Save model parameters . #
                    ###########################
                    #saver = tf.train.Saver(var_list=get_model_variables_within_scopes([net_name_scope_pruned+'/']))
                    save_path = saver.save(
                        sess, os.path.join(train_dir, 'model.ckpt-' + str(i)))
                    print("HG: Model saved in file: %s" % save_path)

            coord.request_stop()
            coord.join(threads)
            total_time = time.time() - tic
            train_speed = train_time * 1.0 / train_only_cnt
            train_time = train_speed * (
                FLAGS.max_number_of_steps
            )  # - init_global_step_value) #/train_only_cnt
            info = "HG: training speed(sec/batch): %.6f\n" % (train_speed)
            info += "HG: training time(min): %.1f, total time(min): %.1f" % (
                train_time / 60.0, total_time / 60.0)
            print(info)
            write_detailed_info(info)
Example #59
0
def initialize():
    """Initialize all the uninitialized variables in the global scope."""
    new_variables = set(tf.global_variables()) - ALREADY_INITIALIZED
    get_session().run(tf.variables_initializer(new_variables))
    ALREADY_INITIALIZED.update(new_variables)
Example #60
0
from tensorflow.python.saved_model.signature_constants import REGRESS_METHOD_NAME
from tensorflow.python.saved_model.tag_constants import TRAINING, SERVING
from tensorflow.python.saved_model.utils import build_tensor_info

x = tf.placeholder(tf.float32, name='x')
y = tf.placeholder(tf.float32, name='y')

w = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='w')
b = tf.Variable(tf.zeros([1]), name='b')
y_hat = w * x + b

loss = tf.reduce_mean(tf.square(y_hat - y))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss, name='train')

init = tf.variables_initializer(tf.global_variables(), name='init')

directory = 'examples/saved-regression-model'
builder = SavedModelBuilder(directory)

with tf.Session(graph=tf.get_default_graph()) as sess:
    sess.run(init)

    signature_inputs = {"x": build_tensor_info(x), "y": build_tensor_info(y)}
    signature_outputs = {"out": build_tensor_info(y_hat)}
    signature_def = build_signature_def(signature_inputs, signature_outputs,
                                        REGRESS_METHOD_NAME)
    builder.add_meta_graph_and_variables(
        sess, [TRAINING, SERVING],
        signature_def_map={REGRESS_METHOD_NAME: signature_def},
        assets_collection=tf.get_collection(tf.GraphKeys.ASSET_FILEPATHS))