def apply_gradients(self, grads_and_vars, global_step=None, name=None):
    with tf.init_scope():
      self._create_slots([v for (_, v) in grads_and_vars])

    accums = []
    variables = []

    for g, v in grads_and_vars:
      accum = self.get_slot(v, 'grad_accum')
      variables.append(v)
      if isinstance(g, tf.IndexedSlices):
        scaled_grad = tf.IndexedSlices(
            g.values / self._grad_steps, g.indices, dense_shape=g.dense_shape)
        accums.append(accum.assign_add(scaled_grad))  # pytype: disable=attribute-error
      else:
        accums.append(accum.assign_add(g / self._grad_steps))  # pytype: disable=attribute-error

    def _apply_and_zero():
      apply_op = self._opt.apply_gradients(list(zip(accums, variables)))
      with tf.control_dependencies([apply_op]):
        zero_op = [tf.assign(accum, tf.zeros_like(accum)) for accum in accums]
      return tf.group(zero_op, tf.assign_add(self._counter, 1))

    def _accum():
      return tf.group(accums)

    accum_step = tf.cond(
        tf.equal(tf.mod(global_step, self._grad_steps), self._grad_steps - 1),
        _apply_and_zero, _accum)

    with tf.control_dependencies([accum_step]):
      global_step = tf.assign_add(global_step, 1)
      return tf.group(global_step)
示例#2
0
    def __call__(self, inputs, start_index=None):
        dtype = inputs.dtype
        inputs_shape = tf.shape(inputs)
        batch_size = inputs_shape[0]
        length = inputs_shape[1]
        channels = inputs_shape[2]
        if start_index is None:
            start_index = tf.zeros((batch_size, 1), tf.int32)

        position = tf.expand_dims(tf.range(length), 0)
        position = tf.tile(position, [batch_size, 1]) + start_index
        position = tf.cast(position, dtype)
        num_timescales = channels // 2
        log_timescale_increment = (
            math.log(_MAX_TIMESCALE / _MIN_TIMESCALE) /
            tf.maximum(tf.cast(num_timescales, dtype) - 1, 1))
        inv_timescales = _MIN_TIMESCALE * tf.exp(
            tf.cast(tf.range(num_timescales), dtype) *
            -log_timescale_increment)
        scaled_time = tf.expand_dims(position, 2) * tf.reshape(
            inv_timescales, [1, 1, -1])
        signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=2)
        signal = tf.pad(signal, [[0, 0], [0, 0], [0, tf.mod(channels, 2)]])
        signal = tf.reshape(signal, [-1, length, channels])
        return inputs + signal
    def _finish(self, update_ops, name_scope):
        """Updates beta_power variables every n batches and incrs counter."""
        iter_ = self._get_iter_variable()
        beta1_power, beta2_power = self._get_beta_accumulators()
        with tf.control_dependencies(update_ops):
            with tf.colocate_with(iter_):

                def update_beta_op():
                    update_beta1 = beta1_power.assign(
                        beta1_power * self._beta1_t,
                        use_locking=self._use_locking)
                    update_beta2 = beta2_power.assign(
                        beta2_power * self._beta2_t,
                        use_locking=self._use_locking)
                    return tf.group(update_beta1, update_beta2)

                maybe_update_beta = tf.cond(tf.equal(iter_, 0), update_beta_op,
                                            tf.no_op)
                with tf.control_dependencies([maybe_update_beta]):
                    # TODO(cuong): It is suboptimal here because we have to cast twice
                    # (float to int, and then int to float)
                    update_iter = iter_.assign(tf.cast(
                        tf.mod(tf.cast(iter_ + 1.0, tf.int32), self._n_t),
                        tf.float32),
                                               use_locking=self._use_locking)
        return tf.group(*update_ops + [update_iter, maybe_update_beta],
                        name=name_scope)
示例#4
0
      def check_integrity_and_batch(*datasets):
        """Checks whether a sequence of frames are from the same video.

        Args:
          *datasets: datasets each skipping 1 frame from the previous one.

        Returns:
          batched data and the integrity flag.
        """
        not_broken = tf.constant(True)
        if "frame_number" in datasets[0]:
          frame_numbers = [dataset["frame_number"][0] for dataset in datasets]

          not_broken = tf.equal(frame_numbers[-1] - frame_numbers[0],
                                num_frames - 1)
          if self.only_keep_videos_from_0th_frame:
            not_broken = tf.logical_and(not_broken, tf.equal(
                frame_numbers[0], 0))
          if self.avoid_overlapping_frames:
            non_overlap = tf.equal(tf.mod(frame_numbers[0], num_frames), 0)
            not_broken = tf.logical_and(not_broken, non_overlap)
        else:
          tf.logging.warning("use_not_breaking_batching is True but "
                             "no frame_number is in the dataset.")

        features = {}
        for key in datasets[0].keys():
          values = [dataset[key] for dataset in datasets]
          batch = tf.stack(values)
          features[key] = batch
        return features, not_broken
示例#5
0
def unwrap(p, discont=np.pi, axis=-1):
    """Unwrap a cyclical phase tensor.

  Args:
    p: Phase tensor.
    discont: Float, size of the cyclic discontinuity.
    axis: Axis of which to unwrap.

  Returns:
    unwrapped: Unwrapped tensor of same size as input.
  """
    dd = diff(p, axis=axis)
    ddmod = tf.mod(dd + np.pi, 2.0 * np.pi) - np.pi
    idx = tf.logical_and(tf.equal(ddmod, -np.pi), tf.greater(dd, 0))
    ddmod = tf.where(idx, tf.ones_like(ddmod) * np.pi, ddmod)
    ph_correct = ddmod - dd
    idx = tf.less(tf.abs(dd), discont)
    ddmod = tf.where(idx, tf.zeros_like(ddmod), dd)
    ph_cumsum = tf.cumsum(ph_correct, axis=axis)

    shape = p.get_shape().as_list()
    shape[axis] = 1
    ph_cumsum = tf.concat([tf.zeros(shape, dtype=p.dtype), ph_cumsum],
                          axis=axis)
    unwrapped = p + ph_cumsum
    return unwrapped
示例#6
0
def get_timing_signal_1d_given_position(channels,
                                        position,
                                        min_timescale=1.0,
                                        max_timescale=1.0e4):
    """Get sinusoids of diff frequencies, with timing position given.

  Adapted from add_timing_signal_1d_given_position in
  //third_party/py/tensor2tensor/layers/common_attention.py

  Args:
    channels: scalar, size of timing embeddings to create. The number of
        different timescales is equal to channels / 2.
    position: a Tensor with shape [batch, seq_len]
    min_timescale: a float
    max_timescale: a float

  Returns:
    a Tensor of timing signals [batch, seq_len, channels]
  """
    num_timescales = channels // 2
    log_timescale_increment = (
        math.log(float(max_timescale) / float(min_timescale)) /
        (tf.to_float(num_timescales) - 1))
    inv_timescales = min_timescale * tf.exp(
        tf.to_float(tf.range(num_timescales)) * -log_timescale_increment)
    scaled_time = (tf.expand_dims(tf.to_float(position), 2) *
                   tf.expand_dims(tf.expand_dims(inv_timescales, 0), 0))
    signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=2)
    signal = tf.pad(signal, [[0, 0], [0, 0], [0, tf.mod(channels, 2)]])
    return signal
示例#7
0
文件: vcsmc.py 项目: amoretti86/phylo
    def extend_partial_state(self, JCK, r):
        """
        Extends partial state by sampling two states to coalesce (Gumbel-max trick to sample without replacement)
        JumpChain (JC_K) is a tensor formed from a numpy array of lists of strings, returns a new JumpChain tensor
        """
        # Compute combinatorial term
        # pdb.set_trace()
        q = 1 / ncr(self.N - r, 2)
        data = tf.reshape(tf.range((self.N - r) * self.K), (self.K, self.N - r))
        data = tf.mod(data, (self.N - r))
        data = tf.cast(data, dtype=tf.float32)
        # Gumbel-max trick to sample without replacement
        z = -tf.math.log(-tf.math.log(tf.random.uniform(tf.shape(data), 0, 1)))
        top_values, coalesced_indices = tf.nn.top_k(data + z, 2)
        bottom_values, remaining_indices = tf.nn.top_k(tf.negative(data + z), self.N - r - 2)
        JC_keep = tf.gather(tf.reshape(JCK, [self.K * (self.N - r)]), remaining_indices)
        particles = tf.gather(tf.reshape(JCK, [self.K * (self.N - r)]), coalesced_indices)
        particle1 = particles[:, 0]
        particle2 = particles[:, 1]
        # Form new state
        particle_coalesced = particle1 + '+' + particle2
        # Form new Jump Chain
        JCK = tf.concat([JC_keep, tf.expand_dims(particle_coalesced, axis=1)], axis=1)

        #return particle1, particle2, particle_coalesced, coalesced_indices, remaining_indices, q, JCK
        return coalesced_indices, remaining_indices, q, JCK
示例#8
0
    def _link(self, h_clock, x, **kwargs):
        # Sanity check
        self._check_state(h_clock, (self._state_size, 1))
        h, clock = h_clock

        # Execute all modules (TODO: too much of brutal force)
        h_x = tf.concat([h, x], axis=1, name='h_x')
        results = []
        for m in self._modules:
            assert isinstance(m, Module)
            results.append(m.execute(h_x))
        result = tf.concat(results, axis=1)

        # Bias
        bias = None
        if self._use_bias: bias = self._get_bias('bias', self._state_size)

        # Calculate f(h * Wh + x * Wx + b)
        y = self._activation(tf.nn.bias_add(result, bias))

        # Calculate and apply mask
        mask = tf.cast(tf.equal(tf.mod(clock, self._periods), 0), tf.float32)
        # mask = tf.Print(mask, [clock, mask, self._periods], '[Clock, Mask] = ')

        updated = tf.multiply(y, mask)
        new_h = updated + tf.multiply(1.0 - mask, h)

        # Return output and new state
        new_clock = tf.add(clock, 1)
        return new_h, (new_h, new_clock)
示例#9
0
 def _maybe_apply_grads_and_zero(self, distribution, global_step,
                                 accum_grads, var_list):
     cond_return = tf.cond(
         tf.equal(tf.mod(global_step, self._grad_steps),
                  self._grad_steps - 1), lambda: self._apply_and_zero(
                      distribution, global_step, accum_grads, var_list),
         lambda: self._accum(global_step))
     return cond_return
def make_hash_fn():
    session = tf.Session()
    placeholder = tf.placeholder(tf.string, [])
    hash_bucket = tf.strings.to_hash_bucket_fast(placeholder, 100000)
    result = tf.mod(hash_bucket, 10)

    def _hash_fn(x):
        return session.run(result, feed_dict={placeholder: x})

    return _hash_fn
示例#11
0
    def get_bmu(self):
        square_difference = tf.square(self.input - self.weight)
        distance = tf.sqrt(tf.reduce_mean(square_difference, axis=1))

        bmu_index = tf.argmin(distance)
        bmu_location = tf.to_float(
            [tf.div(bmu_index, self.width),
             tf.mod(bmu_index, self.width)])

        return bmu_location
示例#12
0
 def false_fn():
     """add mutations."""
     mask = tf.cast(
         tf.multinomial(tf.log([[1 - mutation_rate, mutation_rate]]),
                        seq_len), tf.int32)[0]
     possible_mutations = tf.random_uniform([seq_len],
                                            minval=1,
                                            maxval=4,
                                            dtype=tf.int32)
     x_new = tf.mod(x + mask * possible_mutations, 4)
     return x_new
示例#13
0
def tf_angle2class(angle):
    ''' Convert continuous angle to discrete class and residual.
        num_class: int scalar, number of classes N

    Input:
        angle: rad scalar, from 0-2pi (or -pi~pi), class center at
            0, 1*(2pi/N), 2*(2pi/N) ...  (N-1)*(2pi/N)
    Output:
        class_id, int, among 0,1,...,N-1
        residual_angle: float, a number such that
            class*(2pi/N) + residual_angle = angle
    '''
    twopi = tf.constant(2.0 * np.pi)
    angle = tf.mod(angle, twopi)
    angle_per_class = twopi / tf.to_float(cfg.model.angles.num_bins)
    shifted_angle = tf.mod(angle + angle_per_class / 2.0, twopi)
    class_id = tf.to_int32(shifted_angle / angle_per_class)
    residual_angle = shifted_angle - (tf.to_float(class_id) * angle_per_class +
                                      angle_per_class / 2.0)
    return class_id[:, 0], residual_angle
示例#14
0
def get_maximum_index(response):
    """Get the index of the maximum value in the response map"""
    response_shape = response.get_shape().as_list()
    response_spatial_size = response_shape[-2:]  # e.g. [29, 29]
    length = response_spatial_size[0] * response_spatial_size[1]

    # Get maximum response index (note index starts from zero)
    ind_max = tf.argmax(tf.reshape(response, [-1, length]), 1)
    ind_row = tf.div(ind_max, response_spatial_size[1])
    ind_col = tf.mod(ind_max, response_spatial_size[1])
    return ind_row, ind_col
示例#15
0
def get_mask(inputs, reverse_mask, data_format='NHWC', dtype=tf.float32):
    shape = inputs.get_shape().as_list()
    if len(shape) == 2:
        N = shape[-1]
        range_n = tf.range(N)
        odd_ind = tf.mod(range_n, 2)

        odd_ind = tf.reshape(odd_ind, [-1, N])
        checker = odd_ind

    elif len(shape) == 4:
        H = shape[2] if data_format == 'NCHW' else shape[1]
        W = shape[3] if data_format == 'NCHW' else shape[2]

        range_h = tf.range(H)
        range_w = tf.range(W)

        odd_ind_h = tf.cast(tf.mod(range_h, 2), dtype=tf.bool)
        odd_ind_w = tf.cast(tf.mod(range_w, 2), dtype=tf.bool)

        odd_h = tf.tile(tf.expand_dims(odd_ind_h, -1), [1, W])
        odd_w = tf.tile(tf.expand_dims(odd_ind_w, 0), [H, 1])

        checker = tf.logical_xor(odd_h, odd_w)

        reshape = [-1, 1, H, W] if data_format == 'NCHW' else [-1, H, W, 1]
        checker = tf.reshape(checker, reshape)

    else:
        raise ValueError(
            'Invalid tensor shape. Dimension of the tensor shape must be '
            '2 (NxD) or 4 (NxCxHxW or NxHxWxC), got {}.'.format(
                inputs.get_shape().as_list()))

    if checker.dtype != dtype:
        checker = tf.cast(checker, dtype)

    if reverse_mask:
        checker = 1. - checker

    return checker
示例#16
0
    def kfac_update_ops(self):
        """Sets up the KFAC factor update ops.

    Returns:
      An op that when run will run the update ops at their update frequencies.
    """
        # This if-statement is a trick/hack to maintain compatibility with
        # CrossShardOptimizer or other optimizers that might not call our
        # custom minimize() method (that would otherwise always make the variables).
        if not self._made_vars_already:
            (cov_update_thunks,
             inv_update_thunks) = self.make_vars_and_create_op_thunks()
            logging.warning(
                "It looks like apply_gradients() was called before "
                "minimze() was called. This is not recommended, and you "
                "should avoid using optimizer wrappers like "
                "CrossShardOptimizer with K-FAC that try to bypass the "
                "minimize() method. The burn-in feature won't work when "
                "the class is used this way, for example. And K-FAC does "
                "its own cross-relica syncronization.")
        else:
            (_, cov_update_thunks, _,
             inv_update_thunks) = self.create_ops_and_vars_thunks()

        should_do_cov_updates = tf.equal(
            tf.mod(self.counter, self._cov_update_every), 0)
        maybe_cov_updates = utils.smart_cond(
            should_do_cov_updates,
            lambda: tf.group(*(thunk() for thunk in cov_update_thunks)),
            tf.no_op)

        maybe_pre_update_adapt_damping = self.maybe_pre_update_adapt_damping()
        with tf.control_dependencies(
            [maybe_cov_updates, maybe_pre_update_adapt_damping]):
            should_do_inv_updates = tf.equal(
                tf.mod(self.counter, self._invert_every), 0)
            maybe_inv_updates = utils.smart_cond(
                should_do_inv_updates,
                lambda: tf.group(*(thunk() for thunk in inv_update_thunks)),
                tf.no_op)
            return maybe_inv_updates
示例#17
0
def tf_class2angle(pred_cls, residual, to_label_format=True):
    ''' Inverse function to angle2class.
    If to_label_format, adjust angle to the range as in labels.
    '''
    tf_pi = tf.constant(np.pi, dtype=tf.float32)
    angle_per_class = 2.0 * tf_pi / tf.to_float(cfg.model.angles.num_bins)
    angle_center = tf.to_float(pred_cls) * angle_per_class
    angle = angle_center + residual
    if to_label_format:
        angle = tf.mod(angle + tf_pi,
                       2.0 * tf_pi) - tf_pi  # Transfer to range -pi,pi
    return angle
示例#18
0
def noise_from_step_num():
  """Quantization noise equal to (phi * (step_num + 1)) mod 1.0.

  Not using random_uniform here due to a problem on TPU in that random seeds
  are not respected, which may cause the parameters on different replicas
  to go out-of-sync.

  Returns:
    a float32 scalar
  """
  step = tf.to_int32(tf.train.get_or_create_global_step()) + 1
  phi = ((5 ** 0.5) - 1) / 2
  # Naive computation tf.mod(phi * step, 1.0) in float32 would be disastrous
  # due to loss of precision when the step number gets large.
  # Computation in doubles does not work on TPU, so we use this complicated
  # alternative computation which does not suffer from these roundoff errors.
  ret = 0.0
  for i in range(30):
    ret += (((phi * (2 ** i)) % 1.0)  # double-precision computation in python
            * to_float(tf.mod(step // (2 ** i), 2)))
  return tf.mod(ret, 1.0)
示例#19
0
def tf_popvec(y):
    """Population vector read-out in tensorflow."""

    num_units = y.get_shape().as_list()[-1]
    pref = np.arange(0, 2 * np.pi, 2 * np.pi / num_units)  # preferences
    cos_pref = np.cos(pref)
    sin_pref = np.sin(pref)
    temp_sum = tf.reduce_sum(y, axis=-1)
    temp_cos = tf.reduce_sum(y * cos_pref, axis=-1) / temp_sum
    temp_sin = tf.reduce_sum(y * sin_pref, axis=-1) / temp_sum
    loc = tf.atan2(temp_sin, temp_cos)
    return tf.mod(loc, 2 * np.pi)
示例#20
0
 def test(self, restore_model, save_dir, is_normalize_img=True):
     from test_datasets import BasicDataset
     dataset = BasicDataset(data_list_file=self.dataset_config['data_list_file'], img_dir=self.dataset_config['img_dir'], is_normalize_img=is_normalize_img)
     save_name_list = dataset.data_list[:, -1]
     iterator = dataset.create_one_shot_iterator(dataset.data_list, num_parallel_calls=self.num_input_threads)
     batch_img0, batch_img1, batch_img2 = iterator.get_next()
     img_shape = tf.shape(batch_img0)
     h = img_shape[1]
     w = img_shape[2]
     
     new_h = tf.where(tf.equal(tf.mod(h, 64), 0), h, (tf.to_int32(tf.floor(h / 64) + 1)) * 64)
     new_w = tf.where(tf.equal(tf.mod(w, 64), 0), w, (tf.to_int32(tf.floor(w / 64) + 1)) * 64)
     
     batch_img0 = tf.image.resize_images(batch_img0, [new_h, new_w], method=1, align_corners=True)
     batch_img1 = tf.image.resize_images(batch_img1, [new_h, new_w], method=1, align_corners=True)
     batch_img2 = tf.image.resize_images(batch_img2, [new_h, new_w], method=1, align_corners=True)
     
     flow_fw, flow_bw = pyramid_processing(batch_img0, batch_img1, batch_img2, train=False, trainable=False, is_scale=True) 
     flow_fw['full_res'] = flow_resize(flow_fw['full_res'], [h, w], method=1)
     flow_bw['full_res'] = flow_resize(flow_bw['full_res'], [h, w], method=1)
     
     flow_fw_color = flow_to_color(flow_fw['full_res'], mask=None, max_flow=256)
     flow_bw_color = flow_to_color(flow_bw['full_res'], mask=None, max_flow=256)
     
     restore_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) 
     saver = tf.train.Saver(var_list=restore_vars)
     sess = tf.Session()
     sess.run(tf.global_variables_initializer()) 
     sess.run(iterator.initializer) 
     saver.restore(sess, restore_model)
     if not os.path.exists(save_dir):
         os.makedirs(save_dir)           
     for i in range(dataset.data_num):
         np_flow_fw, np_flow_bw, np_flow_fw_color, np_flow_bw_color = sess.run([flow_fw['full_res'], flow_bw['full_res'], flow_fw_color, flow_bw_color])
         misc.imsave('%s/flow_fw_color_%s.png' % (save_dir, save_name_list[i]), np_flow_fw_color[0])
         misc.imsave('%s/flow_bw_color_%s.png' % (save_dir, save_name_list[i]), np_flow_bw_color[0])
         write_flo('%s/flow_fw_%s.flo' % (save_dir, save_name_list[i]), np_flow_fw[0])
         write_flo('%s/flow_bw_%s.flo' % (save_dir, save_name_list[i]), np_flow_bw[0])
         print('Finish %d/%d' % (i+1, dataset.data_num))
示例#21
0
    def apply_gradients(self, grads_and_vars, global_step=None, name=None):
        counter = tf.get_variable(shape=[],
                                  initializer=tf.zeros_initializer,
                                  name="counter")
        accums = []
        update_op = []
        variables = []
        for (grad, param) in grads_and_vars:
            if grad is None or param is None:
                continue

            if self._grad_clipping is not None:
                grad_clipping = self._steps * self._grad_clipping
                grad = tf.clip_by_value(grad, -grad_clipping, grad_clipping)

            variables.append(param)
            param_name = self._get_variable_name(param.name)

            accum = tf.get_variable(name=param_name + "/accum",
                                    shape=param.shape.as_list(),
                                    dtype=tf.float32,
                                    trainable=False,
                                    initializer=tf.zeros_initializer())
            accums.append(accum)

            if isinstance(grad, tf.IndexedSlices):
                scaled_grad = tf.IndexedSlices(grad.values / self._steps,
                                               grad.indices,
                                               dense_shape=grad.dense_shape)
                update_op.append(accum.assign_add(scaled_grad))
            else:
                update_op.append(accum.assign_add(grad / self._steps))

        def _apply_and_zero():
            with tf.control_dependencies(update_op):
                apply_op = self._opt.apply_gradients(
                    list(zip(accums, variables)), global_step, name)
            with tf.control_dependencies([apply_op]):
                zero_op = [
                    tf.assign(accum, tf.zeros_like(accum))
                    for accum in accums + [counter]
                ]
            return tf.group(zero_op)

        def _accum():
            return tf.group(update_op)

        # Control that the counter has been incremented already
        with tf.control_dependencies([counter.assign_add(1)]):
            return tf.cond(tf.equal(tf.mod(counter, self._steps), 0),
                           _apply_and_zero, _accum)
示例#22
0
    def getBMU(self):
        #Best Matching Unit

        #Eucledian distance
        square_distance = tf.square(self.input - self.weight)
        distance = tf.sqrt(tf.reduce_sum(square_distance, axis=1))

        #Get BMU index
        bmu_index = tf.argmin(distance)
        #Get the position
        bmu_position = tf.to_float(
            [tf.div(bmu_index, self.width),
             tf.mod(bmu_index, self.width)])
        return bmu_position
示例#23
0
    def _data_augmentation(self, image_blur, image_sharp):

        rot = tf.random_uniform(shape=[1], minval=0, maxval=3,
                                dtype=tf.int32)[0]
        flip_rl = tf.random_uniform(shape=[1],
                                    minval=0,
                                    maxval=3,
                                    dtype=tf.int32)[0]
        flip_updown = tf.random_uniform(shape=[1],
                                        minval=0,
                                        maxval=3,
                                        dtype=tf.int32)[0]

        image_blur = tf.image.rot90(image_blur, rot)
        image_sharp = tf.image.rot90(image_sharp, rot)

        rl = tf.equal(tf.mod(flip_rl, 2), 0)
        ud = tf.equal(tf.mod(flip_updown, 2), 0)

        image_blur = tf.cond(
            rl,
            true_fn=lambda: tf.image.flip_left_right(image_blur),
            false_fn=lambda: (image_blur))
        image_sharp = tf.cond(
            rl,
            true_fn=lambda: tf.image.flip_left_right(image_sharp),
            false_fn=lambda: (image_sharp))

        image_blur = tf.cond(ud,
                             true_fn=lambda: tf.image.flip_up_down(image_blur),
                             false_fn=lambda: (image_blur))
        image_sharp = tf.cond(
            ud,
            true_fn=lambda: tf.image.flip_up_down(image_sharp),
            false_fn=lambda: (image_sharp))

        return image_blur, image_sharp
示例#24
0
def tf_class2angle2(pred_cls, residuals, to_label_format=True):
    ''' Inverse function to angle2class.
    If to_label_format, adjust angle to the range as in labels.
    '''
    residual = tf.gather_nd(
        residuals,
        tf.transpose(
            tf.stack([tf.range(pred_cls.shape[0], dtype=tf.int64), pred_cls])))
    tf_pi = tf.constant(np.pi, dtype=tf.float32)
    angle_per_class = 2.0 * tf_pi / tf.to_float(cfg.model.angles.num_bins)
    angle_center = tf.to_float(pred_cls) * angle_per_class
    angle = angle_center + residual
    if to_label_format:
        angle = tf.mod(angle + tf_pi,
                       2.0 * tf_pi) - tf_pi  # Transfer to range -pi,pi
    return angle
示例#25
0
文件: util.py 项目: dandiagusm/lsgn
def add_timing_signal_1d_given_position(x,
                                        position,
                                        min_timescale=1.0,
                                        max_timescale=1.0e4):
    channels = tf.shape(x)[2]
    num_timescales = channels // 2
    log_timescale_increment = (
        math.log(float(max_timescale) / float(min_timescale)) /
        (tf.to_float(num_timescales) - 1))
    inv_timescales = min_timescale * tf.exp(
        tf.to_float(tf.range(num_timescales)) * -log_timescale_increment)
    scaled_time = (tf.expand_dims(tf.to_float(position), 2) *
                   tf.expand_dims(tf.expand_dims(inv_timescales, 0), 0))
    signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=2)
    signal = tf.pad(signal, [[0, 0], [0, 0], [0, tf.mod(channels, 2)]])
    return x + signal
示例#26
0
    def _update_history(self, hist_idx, hist_size, zs, Hzs, s_hists, y_hists):
        ops = []

        for z, Hz, s_hist, y_hist in zip(zs, Hzs, s_hists, y_hists):
            # Use the Hessian or the Gauss-Newton approximation instead of a simple
            # difference of gradients.
            ops.append(tf.scatter_update(s_hist, hist_idx, z))
            ops.append(tf.scatter_update(y_hist, hist_idx, Hz))

        hist_idx_new = tf.mod(hist_idx + 1, self._conf['hist'])
        hist_size_new = tf.minimum(hist_size + 1, self._conf['hist'])

        with tf.control_dependencies(ops):
            ops.append(tf.assign(hist_idx, hist_idx_new))
            ops.append(tf.assign(hist_size, hist_size_new))

        return tf.group(ops)
示例#27
0
文件: util.py 项目: dandiagusm/lsgn
def get_timing_signal_1d(length,
                         channels,
                         min_timescale=1.0,
                         max_timescale=1.0e4):
    position = tf.to_float(tf.range(length))
    num_timescales = channels // 2
    log_timescale_increment = (
        math.log(float(max_timescale) / float(min_timescale)) /
        (tf.to_float(num_timescales) - 1))
    inv_timescales = min_timescale * tf.exp(
        tf.to_float(tf.range(num_timescales)) * -log_timescale_increment)
    scaled_time = tf.expand_dims(position, 1) * tf.expand_dims(
        inv_timescales, 0)
    signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=1)
    signal = tf.pad(signal, [[0, 0], [0, tf.mod(channels, 2)]])
    signal = tf.reshape(signal, [1, length, channels])
    return signal
示例#28
0
    def preprocess_example(self, example, mode, hparams):
        if self.has_inputs:
            # Stack encoded score components depthwise as inputs.
            inputs = []
            for name, _ in self.score_encoders():
                inputs.append(tf.expand_dims(example[name], axis=1))
                del example[name]
            example['inputs'] = tf.stack(inputs, axis=2)

        if self.random_crop_in_train and mode == tf_estimator.ModeKeys.TRAIN:
            # Take a random crop of the training example.
            assert not self.has_inputs
            max_offset = tf.maximum(
                tf.shape(example['targets'])[0] -
                hparams.max_target_seq_length, 0)
            offset = tf.cond(
                max_offset > 0, lambda: tf.random_uniform(
                    [], maxval=max_offset, dtype=tf.int32), lambda: 0)
            example['targets'] = (
                example['targets'][offset:offset +
                                   hparams.max_target_seq_length])
            return example

        elif self.split_in_eval and mode == tf_estimator.ModeKeys.EVAL:
            # Split the example into non-overlapping segments.
            assert not self.has_inputs
            length = tf.shape(example['targets'])[0]
            extra_length = tf.mod(length, hparams.max_target_seq_length)
            examples = {
                'targets':
                tf.reshape(example['targets'][:length - extra_length],
                           [-1, hparams.max_target_seq_length, 1, 1])
            }
            extra_example = {
                'targets':
                tf.reshape(example['targets'][-extra_length:], [1, -1, 1, 1])
            }
            dataset = tf.data.Dataset.from_tensor_slices(examples)
            extra_dataset = tf.data.Dataset.from_tensor_slices(extra_example)
            return dataset.concatenate(extra_dataset)

        else:
            # If not cropping or splitting, do standard preprocessing.
            return super(Score2PerfProblem,
                         self).preprocess_example(example, mode, hparams)
示例#29
0
 def _compute_top_k(x):
     """Compute top-k values for each row in a batch."""
     cls_outputs_per_sample, box_outputs_per_sample = x
     cls_outputs_per_sample_reshape = tf.reshape(cls_outputs_per_sample,
                                                 [-1])
     _, cls_topk_indices = tf.nn.top_k(cls_outputs_per_sample_reshape,
                                       k=anchors.MAX_DETECTION_POINTS)
     # Gets top-k class and box scores.
     indices = tf.div(cls_topk_indices, params['num_classes'])
     classes = tf.mod(cls_topk_indices, params['num_classes'])
     cls_indices = tf.stack([indices, classes], axis=1)
     cls_outputs_after_topk = tf.gather_nd(cls_outputs_per_sample,
                                           cls_indices)
     box_outputs_after_topk = tf.gather_nd(box_outputs_per_sample,
                                           tf.expand_dims(indices, 1))
     return [
         indices, classes, cls_outputs_after_topk, box_outputs_after_topk
     ]
示例#30
0
def get_background_lr(global_step, steps_per_phase):
    """A periodic linear-warmp background learning rate schedule."""
    truncated_global_step = tf.mod(global_step, steps_per_phase)
    if steps_per_phase > 1:
        background = tf.minimum(
            tf.train.polynomial_decay(1.0,
                                      truncated_global_step,
                                      steps_per_phase,
                                      end_learning_rate=0.9,
                                      power=0.5),
            tf.train.polynomial_decay(0.0,
                                      truncated_global_step,
                                      steps_per_phase,
                                      end_learning_rate=10.0,
                                      power=1.0))
    else:
        background = 1.0
    return background