Esempio n. 1
0
 def _pyramidal_stack(self, outputs, sequence_length):
     max_time = tf.shape(outputs)[1]
     num_units = outputs.get_shape().as_list()[-1]
     paddings = [[0, 0], [0, tf.floormod(max_time, 2)], [0, 0]]
     outputs = tf.pad(outputs, paddings)
     concat_outputs = tf.reshape(outputs, (self._config.batch_size, -1, num_units * 2))
     return concat_outputs, tf.floordiv(sequence_length, 2) + tf.floormod(sequence_length, 2)
Esempio n. 2
0
def meanDistance(y_true, y_pred):
    in_shape = tf.shape(y_true)

    # Flatten height/width dims
    flat_true = tf.reshape(y_true, [in_shape[0], -1, in_shape[-1]])
    flat_pred = tf.reshape(y_pred, [in_shape[0], -1, in_shape[-1]])

    # Find peaks in linear indices
    idx_true = tf.argmax(flat_true, axis=1)
    idx_pred = tf.argmax(flat_pred, axis=1)

    # Convert linear indices to subscripts
    rows_true = tf.floor_div(tf.cast(idx_true, tf.int32), in_shape[2])
    cols_true = tf.floormod(tf.cast(idx_true, tf.int32), in_shape[2])

    rows_pred = tf.floor_div(tf.cast(idx_pred, tf.int32), in_shape[2])
    cols_pred = tf.floormod(tf.cast(idx_pred, tf.int32), in_shape[2])

    row_diff = tf.square(
        tf.subtract(tf.cast(rows_true, tf.float32),
                    tf.cast(rows_pred, tf.float32)))
    col_diff = tf.square(
        tf.subtract(tf.cast(cols_true, tf.float32),
                    tf.cast(cols_pred, tf.float32)))
    distances = tf.sqrt(tf.add(row_diff, col_diff))

    return tf.reduce_mean(distances)
Esempio n. 3
0
def idx1d_to_3d(vidx_1d):
    idx3d_2 = tf.expand_dims(tf.floormod(vidx_1d, 2), -1)
    tmp = vidx_1d / 2
    idx3d_1 = tf.expand_dims(tf.floormod(tmp, 2), -1)
    idx3d_0 = tf.expand_dims(tmp / 2, -1)
    idx3d = tf.concat([idx3d_0, idx3d_1, idx3d_2], -1)
    return idx3d
def sph_modulo(x):
    [phi_batch, theta_batch] = tf.split(x, [1, 1], 1)
    phi_batch = tf.floormod(phi_batch, 360)
    theta_batch = tf.where(phi_batch > 180, theta_batch - 180, theta_batch)
    phi_batch = tf.where(phi_batch > 180, 360 - phi_batch, phi_batch)
    theta_batch = tf.floormod(theta_batch, 360)
    output = tf.concat([phi_batch, theta_batch], 1)
    return output
Esempio n. 5
0
def get_keypoint(image, predictions, heatmap_size, height, width, category, clip_at_zero=True, data_format='channels_last', name=None):
    # expand_border = 10

    # pad_pred = tf.pad(predictions, tf.constant([[0, 0], [0, 0], [expand_border, expand_border], [expand_border, expand_border]]),
    #               mode='CONSTANT', name='pred_padding', constant_values=0)

    # blur_pred = gaussian_blur(pad_pred, config.class_num_joints[category], 3.5, 'channels_first', 'pred_blur')

    # predictions = tf.slice(blur_pred, [0, 0, expand_border, expand_border], [1, config.class_num_joints[category], heatmap_size, heatmap_size])
    predictions = tf.reshape(predictions, [1, -1, heatmap_size*heatmap_size])

    pred_max = tf.reduce_max(predictions, axis=-1)
    pred_max_indices = tf.argmax(predictions, axis=-1)
    pred_max_x, pred_max_y = tf.cast(tf.floormod(pred_max_indices, heatmap_size), tf.float32), tf.cast(tf.floordiv(pred_max_indices, heatmap_size), tf.float32)
    # mask the max elements to zero
    mask_predictions = predictions * tf.one_hot(pred_max_indices, heatmap_size*heatmap_size, on_value=0., off_value=1., dtype=tf.float32)
    # get the second max prediction
    pred_next_max = tf.reduce_max(mask_predictions, axis=-1)
    pred_next_max_indices = tf.argmax(mask_predictions, axis=-1)
    pred_next_max_x, pred_next_max_y = tf.cast(tf.floormod(pred_next_max_indices, heatmap_size), tf.float32), tf.cast(tf.floordiv(pred_next_max_indices, heatmap_size), tf.float32)

    dist = tf.pow(tf.pow(pred_next_max_x - pred_max_x, 2.) + tf.pow(pred_next_max_y - pred_max_y, 2.), .5)

    pred_x = tf.where(dist < 1e-3, pred_max_x, pred_max_x + (pred_next_max_x - pred_max_x) * 0.25 / dist)
    pred_y = tf.where(dist < 1e-3, pred_max_y, pred_max_y + (pred_next_max_y - pred_max_y) * 0.25 / dist)

    pred_indices_ = tf.squeeze(tf.cast(pred_x, tf.int64) + tf.cast(pred_y, tf.int64) * heatmap_size)

    width, height = tf.cast(width, tf.float32), tf.cast(height, tf.float32)
    width_ratio, height_ratio = width / tf.cast(heatmap_size, tf.float32), height / tf.cast(heatmap_size, tf.float32)

    pred_x, pred_y = pred_x * width_ratio, pred_y * height_ratio
    #pred_x, pred_y = pred_x * width_ratio + width_ratio/2., pred_y * height_ratio + height_ratio/2.

    if clip_at_zero:
      pred_x, pred_y =  pred_x * tf.cast(pred_max>0, tf.float32), pred_y * tf.cast(pred_max>0, tf.float32)
      pred_x = pred_x * tf.cast(pred_max>0, tf.float32) + tf.cast(pred_max<=0, tf.float32) * (width / 2.)
      pred_y = pred_y * tf.cast(pred_max>0, tf.float32) + tf.cast(pred_max<=0, tf.float32) * (height / 2.)

    if config.PRED_DEBUG:
      image_ = tf.squeeze(image) * 255.
      pred_heatmap = tf.one_hot(pred_indices_, heatmap_size*heatmap_size, on_value=255, off_value=0, axis=-1, dtype=tf.int32)

      pred_heatmap = tf.reshape(pred_heatmap, [-1, heatmap_size, heatmap_size])
      if data_format == 'channels_first':
        image_ = tf.transpose(image_, perm=(1, 2, 0))
      save_image_op = tf.py_func(save_image_with_heatmap,
                                  [image_, height, width,
                                  heatmap_size,
                                  pred_heatmap,
                                  tf.reshape(predictions, [-1, heatmap_size, heatmap_size]),
                                  config.left_right_group_map[category][0],
                                  config.left_right_group_map[category][1],
                                  config.left_right_group_map[category][2]],
                                  tf.int64, stateful=True)
      with tf.control_dependencies([save_image_op]):
        pred_x, pred_y = pred_x * 1., pred_y * 1.
    return pred_x, pred_y
Esempio n. 6
0
 def split_scale(x):
     i3 = tf.floordiv(x, p_granularity**3)
     i2 = tf.floordiv(tf.floormod(x, p_granularity**3),
                      p_granularity**2)
     i1 = tf.floordiv(tf.floormod(x, p_granularity**2),
                      p_granularity)
     i0 = tf.floormod(x, p_granularity)
     return tf.truediv(tf.concat([i3, i2, i1, i0], axis=-1),
                       tf.cast(p_granularity, tf.float32))
def reshape_pyramidal(outputs, sequence_length):
    shape = tf.shape(outputs)
    batch_size, max_time = shape[0], shape[1]
    num_units = outputs.get_shape().as_list()[-1]

    pads = [[0, 0], [0, tf.floormod(max_time, 2)], [0, 0]]
    outputs = tf.pad(outputs, pads)

    concat_outputs = tf.reshape(outputs, (batch_size, -1, num_units * 2))
    return concat_outputs, tf.floordiv(sequence_length, 2) + tf.floormod(
        sequence_length, 2)
Esempio n. 8
0
    def call(self, inputs, states, training=None):
        count, state_in = states
        (p_zs, p_mus, p_sigmas, p_hs, p_flatten_memory,
         q_zs, q_mus, q_sigmas, q_hs, q_flatten_memory
         ) = array_ops.split(
            state_in,
            [self.units, self.units, self.units, self.units,
             self.units * self.num_memory_slots,
             self.units, self.units, self.units, self.units,
             self.units * self.num_memory_slots],
            axis=-1)

        # prior_inputs = q_hs
        prior_inputs = K.concatenate([q_hs, p_hs])
        (p_next_zs, p_next_mus, p_next_sigmas,
         p_next_hs, p_next_flatten_memory) = self._call_one_layer(
            prior_inputs, p_flatten_memory, training, self.p_ws)

        p_next_zs = tf.where_v2(
            tf.floormod(count, self.time_scale) > 0,
            p_zs, p_next_zs)
        p_next_mus = tf.where_v2(
            tf.floormod(count, self.time_scale) > 0,
            p_mus, p_next_mus)
        p_next_sigmas = tf.where_v2(
            tf.floormod(count, self.time_scale) > 0,
            p_sigmas, p_next_sigmas)
        p_next_hs = tf.where_v2(
            tf.floormod(count, self.time_scale) > 0,
            p_hs, p_next_hs)
        p_next_flatten_memory = tf.where_v2(
            tf.floormod(count, self.time_scale) > 0,
            p_flatten_memory, p_next_flatten_memory)

        posterior_inputs = K.concatenate(
            [inputs, p_next_mus, p_next_sigmas, q_zs])
        (q_next_zs, q_next_mus, q_next_sigmas,
         q_next_hs, q_next_flatten_memory) = self._call_one_layer(
            posterior_inputs, q_flatten_memory, training, self.q_ws)

        state_out = K.concatenate(
            [p_next_zs, p_next_mus, p_next_sigmas,
             p_next_hs, p_next_flatten_memory,
             q_next_zs, q_next_mus, q_next_sigmas,
             q_next_hs, q_next_flatten_memory])

        return ({"p_zs": p_next_zs,
                 "p_mus": p_next_mus,
                 "p_sigmas": p_next_sigmas,
                 "q_zs": q_next_zs,
                 "q_mus": q_next_mus,
                 "q_sigmas": q_next_sigmas},
                [count + 1.0, state_out])
Esempio n. 9
0
def update_ising(v, beta):
    """Given spin state v, propose new state and accept / reject as appropriate
        v: a tensor taking values in -1, 1.
        beta: inverse temp

    Returns: newstate, """
    Nchain = tf.shape(v)[0]
    L = tf.shape(v)[1]

    #(Nchain,) tensor indicating which site is selected for each chain
    indices_to_flip = tf.squeeze(
        tf.random.categorical(logits=tf.ones((Nchain, L)),
                              num_samples=1,
                              dtype=tf.int32))
    #indices to the left and right
    left_indices = tf.floormod(indices_to_flip - 1, L)
    right_indices = tf.floormod(indices_to_flip + 1, L)

    #N,3 tensor of all relevant indices
    indices_all = tf.stack([left_indices, indices_to_flip, right_indices],
                           axis=1)
    #N, 3 tensor indexing the chains
    chain_indices = tf.tile(
        tf.expand_dims(tf.range(Nchain, dtype=tf.int32), axis=1), [1, 3])
    #N,3,2 tensor
    indices_full = tf.stack([chain_indices, indices_all], axis=2)

    #N, 3 tensor of spin values at selected sites
    relevant_spins = tf.gather_nd(v, indices_full)

    #defined such that p(new)/p(old) = exp(-beta * energy cost)
    #(N,) tensor
    energy_cost = tf.cast(
        2 * relevant_spins[:, 1] *
        (relevant_spins[:, 0] + relevant_spins[:, 2]), tf.float32)
    boltzmann_ratios = tf.exp(-beta * energy_cost)

    paccept = tf.minimum(1.0, boltzmann_ratios)

    accepted_flips = tf.expand_dims(
        tf.cast(
            tfp.distributions.Bernoulli(probs=paccept).sample(), tf.float32),
        1)

    #N, L tensor, nonzero at flipped sites
    flip_mask = tf.cast(tf.one_hot(indices_to_flip, depth=L), tf.float32)
    #flipped state
    flippedstate = flip_mask * (-v) + (1 - flip_mask) * v
    #updated state
    newstate = accepted_flips * flippedstate + (1 - accepted_flips) * v

    return newstate
Esempio n. 10
0
def LensSurface(
    position,
    power,
    aperatureRadius,
    facingLeft,
    materialIn,
    materialOut,
    maxRadius=100.0,
):
    with tf.name_scope("buildLensSurfaces") as scope:
        # extend shape to be indexable, in the case that we were fed scalars and only seek to make a single surface
        shape = position.shape
        if len(shape) == 0:
            shape = [1]

        # extend aperatureRadius to be indexable
        aperatureRadius = tf.cast(aperatureRadius, tf.float64)
        if len(aperatureRadius.shape) == 0:
            aperatureRadius = tf.tile([aperatureRadius], [shape[0]])

        # cast everything, to be sure we are working with float64
        position = tf.cast(position, tf.float64)
        power = tf.cast(power, tf.float64)
        materialIn = tf.reshape(tf.cast(materialIn, tf.float64), shape)
        materialOut = tf.reshape(tf.cast(materialOut, tf.float64), shape)
        maxRadius = tf.cast(maxRadius, tf.float64)
        maxRadius = tf.tile([maxRadius], [shape[0]])

        # calculate the parameters of the arcs
        radius = (aperatureRadius**2 + power**2) / (2 * power)
        radius = tf.where(tf.equal(power, 0), maxRadius, radius)
        radius = tf.clip_by_value(radius, -maxRadius, maxRadius)

        angularExtent = tf.atan2(
            tf.sign(radius) * aperatureRadius, power - radius)

        xpos = tf.where(facingLeft, position + radius, position - radius)
        ypos = tf.zeros(shape, dtype=tf.float64)
        angleStart = tf.floormod(
            tf.where(facingLeft, angularExtent, angularExtent + PI), 2 * PI)
        angleEnd = tf.floormod(
            tf.where(facingLeft, -angularExtent, -angularExtent + PI), 2 * PI)
        # radius
        # materialIn
        # materialOut

        return tf.stack([
            xpos, ypos, angleStart, angleEnd, radius, materialIn, materialOut
        ],
                        axis=1)
Esempio n. 11
0
def cycle_learning_rate(learning_rate_low,
                        learning_rate_high,
                        global_step,
                        period,
                        name=None):
    if global_step is None:
        raise ValueError("global_step is required for cycle_learning_rate.")
    with ops.name_scope(
            name, "CycleLR",
        [learning_rate_high, learning_rate_low, global_step, period]) as name:
        learning_rate_high = ops.convert_to_tensor(learning_rate_high,
                                                   name="learning_rate_high")
        learning_rate_low = ops.convert_to_tensor(learning_rate_low,
                                                  name="learning_rate_low")

        dtype = learning_rate_high.dtype
        global_step = math_ops.cast(global_step, dtype)
        period = math_ops.cast(period, dtype)
        step_size = tf.divide(
            tf.subtract(learning_rate_high, learning_rate_low), period
        )  # increment by this until period is reached, then decrement
        cycle_stage = tf.abs(
            tf.subtract(
                tf.floormod(global_step,
                            tf.multiply(math_ops.cast(2.0, dtype), period)),
                period))
        return tf.add(learning_rate_low,
                      tf.multiply(tf.subtract(period, cycle_stage), step_size),
                      name=name)
Esempio n. 12
0
def matrix_other():
    isess = tf.InteractiveSession()
    X = tf.Variable(tf.eye(3))
    W = tf.Variable(tf.random_normal(shape=(3, 3)))

    X.initializer.run()
    W.initializer.run()
    logger.info("X\n%s" % X.eval())
    logger.info("W\n%s" % W.eval())

    logger.info("tf.div(X,W)\n%s" % tf.div(X, W).eval())
    logger.info("tf.truediv(X,W)\n%s" % tf.truediv(X, W).eval())
    logger.info("tf.floordiv(X,W)\n%s" % tf.floordiv(X, W).eval())
    logger.info("tf.realdiv(X,W)\n%s" % tf.realdiv(X, W).eval())

    # logger.info("tf.truncatediv(X,W)\n%s" % tf.truncatediv(X, W).eval())
    logger.info("tf.floor_div(X,W)\n%s" % tf.floor_div(X, W).eval())
    logger.info("tf.truncatemod(X,W)\n%s" % tf.truncatemod(X, W).eval())
    logger.info("tf.floormod(X,W)\n%s" % tf.floormod(X, W).eval())

    logger.info("tf.cross(X,W)\n%s" % tf.cross(X, W).eval())
    logger.info("tf.add_n(X,W)\n%s" % tf.add_n([X, W]).eval())
    logger.info("tf.squared_difference(X,W)\n%s" %
                tf.squared_difference(X, W).eval())

    isess.close()
Esempio n. 13
0
            def upsampel_impl(now_count, need_count):
                # sample with replacement
                left_count = need_count - now_count
                select_indices = tf.random_shuffle(tf.range(now_count))[:tf.floormod(left_count, now_count)]
                select_indices = tf.concat([tf.tile(tf.range(now_count), [tf.floor_div(left_count, now_count) + 1]), select_indices], axis = 0)

                return select_indices
Esempio n. 14
0
def tf_find_peaks(x):
    """ Finds the maximum value in each channel and returns the location and value.
    Args:
        x: rank-4 tensor (samples, height, width, channels)

    Returns:
        peaks: rank-3 tensor (samples, [x, y, val], channels)
    """

    # Store input shape
    in_shape = tf.shape(x)

    # Flatten height/width dims
    flattened = tf.reshape(x, [in_shape[0], -1, in_shape[-1]])

    # Find peaks in linear indices
    idx = tf.argmax(flattened, axis=1)

    # Convert linear indices to subscripts
    rows = tf.floor_div(tf.cast(idx,tf.int32), in_shape[1])
    cols = tf.floormod(tf.cast(idx,tf.int32), in_shape[1])

    # Dumb way to get actual values without indexing
    vals = tf.reduce_max(flattened, axis=1)

    # Return N x 3 x C tensor
    return tf.stack([
        tf.cast(cols, tf.float32),
        tf.cast(rows, tf.float32),
        vals
    ], axis=1)
Esempio n. 15
0
def cycle_learning_rate(learning_rate_low,
                        learning_rate_high,
                        global_step,
                        period,
                        name=None):
    """This function determines the learning rate of the model,
	uses tensorflow.
	Input: High and low rate values, global variable for rate step, period
	Output: Rate of model (relative to all values)"""
    if global_step is None:
        raise ValueError("global_step is required for cycle_learning_rate.")
    with ops.name_scope(
            name, "CycleLR",
        [learning_rate_high, learning_rate_low, global_step, period]) as name:
        ## Converting np arrays to tensor
        learning_rate_high = ops.convert_to_tensor(learning_rate_high,
                                                   name="learning_rate_high")
        learning_rate_low = ops.convert_to_tensor(learning_rate_low,
                                                  name="learning_rate_low")
        ## Initialize (gloabl) variables and types
        dtype = learning_rate_high.dtype
        global_step = math_ops.cast(global_step, dtype)
        period = math_ops.cast(period, dtype)
        step_size = tf.divide(
            tf.subtract(learning_rate_high, learning_rate_low), period
        )  # increment by this until period is reached, then decrement
        cycle_stage = tf.abs(
            tf.subtract(
                tf.floormod(global_step,
                            tf.multiply(math_ops.cast(2.0, dtype), period)),
                period))
        return tf.add(learning_rate_low,
                      tf.multiply(tf.subtract(period, cycle_stage), step_size),
                      name=name)
Esempio n. 16
0
def tf_find_peaks(x):
    """ Finds the maximum value in each channel and returns the location and value.
    Args:
        x: rank-4 tensor (samples, height, width, channels)

    Returns:
        peaks: rank-3 tensor (samples, [x, y, val], channels)
    """

    # Store input shape
    in_shape = tf.shape(x)

    # Flatten height/width dims
    flattened = tf.reshape(x, [in_shape[0], -1, in_shape[-1]])

    # Find peaks in linear indices
    idx = tf.argmax(flattened, axis=1)

    # Convert linear indices to subscripts
    rows = tf.floor_div(tf.cast(idx, tf.int32), in_shape[1])
    cols = tf.floormod(tf.cast(idx, tf.int32), in_shape[1])

    # Dumb way to get actual values without indexing
    vals = tf.reduce_max(flattened, axis=1)

    # Return N x 3 x C tensor
    return tf.stack(
        [tf.cast(cols, tf.float32),
         tf.cast(rows, tf.float32), vals], axis=1)
Esempio n. 17
0
    def _loadImageFunction(self, filename, color, weight):
        if self.randomize_size:
            max_upper_dev = int((self.target_size * 0.3) // self.size_factor)
            max_lower_dev = -int((self.target_size * 0.3) // self.size_factor)
            delta_size = tf.random_uniform(shape=(), minval=max_lower_dev, maxval=max_upper_dev,
                                           dtype=tf.int32) * self.size_factor
        else:
            delta_size = 0
        image_size = self.target_size + delta_size

        image_string = tf.read_file(filename)
        image_decoded = tf.image.decode_png(image_string, 3)
        image_decoded.set_shape([None, None, 3])
        image_decoded = tf.cast(image_decoded, tf.float32)
        image_decoded = image_decoded / 255.0

        if self.random_crop:
            target_size = tf.cast(tf.multiply(tf.cast(image_size, dtype=tf.float32), 1.3), dtype=tf.int32)
            image_decoded = aspect_preserving_resize(image_decoded, target_size=target_size, resize_mode="crop")
            image_decoded = tf.random_crop(image_decoded, [image_size, image_size, 3])
        else:
            image_decoded = aspect_preserving_resize(image_decoded, target_size=image_size, resize_mode="pad")

        if self.crop_to_size_factor:
            resized_size = tf.shape(image_decoded)[:2]
            target_crop_size = (resized_size // self.size_factor) * self.size_factor
            image_decoded = image_decoded[:target_crop_size[0], :target_crop_size[1], :]



        if self.random_brightness:
            image_decoded = tf.image.random_brightness(image_decoded, max_delta=0.2)
        if self.random_contrast:
            image_decoded = tf.image.random_contrast(image_decoded, lower=.9, upper=1.1)
        if self.random_saturation:
            image_decoded = tf.image.random_saturation(image_decoded, lower=.9, upper=1.1)
        # image_decoded = tf.py_func(self._py_read_image, [filename], tf.uint8)

        image_shape = tf.shape(image_decoded)
        if self.square_pad:
            new_image_shape = tf.stack([tf.reduce_max(image_shape), tf.reduce_max(image_shape)])
            image_decoded = tf.image.resize_image_with_crop_or_pad(image_decoded, new_image_shape[0],
                                                                   new_image_shape[1])
        elif self.crop_to_size_factor:
            new_image_shape = image_shape
        else:
            new_image_shape = (tf.floor_div(image_shape, self.size_factor) +
                               tf.cast(tf.greater(tf.floormod(image_shape, self.size_factor), 0),
                                       dtype=tf.int32)) * self.size_factor
            image_decoded = tf.image.resize_image_with_crop_or_pad(image_decoded, new_image_shape[0],
                                                                   new_image_shape[1])
        offset_y = (new_image_shape[0] - image_shape[0]) // 2
        offset_x = (new_image_shape[1] - image_shape[1]) // 2

        if self.random_flip:
            image_decoded = tf.image.random_flip_left_right(image_decoded)

        image_decoded = (image_decoded * 2.0) - 1.0

        return image_decoded, color, weight, filename, [offset_y, offset_x], image_shape
Esempio n. 18
0
    def step_line_search_dir(self, global_step, lr, loss, d):
        """
        Step Online line search will compare the loss. If its high compared to
        Loss EMA, it will try to reduce the learning rate based on
        loss function ema change in N steps
        Args:
         global_step: global step
         lr: learning rate
         loss: Scalar loss tensor
         d: computed direction
        Returns:
         d: direction after online line search
        """
        mod = tf.floormod(global_step, self.step_line_search_period)
        d = tf.cond(
            tf.equal(mod, 0), lambda: self.online_line_search(
                lr, self.loss_ema, self.loss_ema_old, d, remove_old_dir=True),
            lambda: tf.identity(d))

        with tf.control_dependencies([d]):
            loss = tf.cond(tf.equal(mod, 0),
                           lambda: self.assign_loss_ema_old(self.loss_ema),
                           lambda: tf.identity(loss))

        with tf.control_dependencies([loss]):
            d = tf.identity(d)

        return d
    def _get_learning_rate(self):
        global_step = tf.cast(self.global_step, tf.float64)
        const_0 = tf.constant(0.0, dtype=tf.float64)
        const_1 = tf.constant(1, dtype=tf.float64)
        const_2 = tf.constant(2, dtype=tf.float64)
        slow_start_step_size = tf.constant(self.config.slow_start_step_size,
                                           tf.float64)
        cycle_step_size = tf.constant(self.config.cycle_step_size, tf.float64)

        max_lr = tf.constant(self.config.max_lr, tf.float64)
        min_lr = tf.constant(self.config.min_lr, tf.float64)
        max_lr_decay_step = tf.cond(
            tf.less_equal(global_step, slow_start_step_size), lambda: const_0,
            lambda: tf.floor(const_1 + (global_step - slow_start_step_size) /
                             cycle_step_size))

        max_lr_decay = tf.constant(self.config.max_lr_decay, tf.float64)
        max_lr = max_lr * (max_lr_decay**(max_lr_decay_step - const_1))
        cos_inner = (tf.constant(pi, tf.float64) *
                     tf.floormod(global_step - slow_start_step_size,
                                 cycle_step_size)) / cycle_step_size

        self.lr = tf.cast(
            tf.cond(
                tf.less_equal(global_step, slow_start_step_size),
                lambda: self.config.min_lr +
                (self.config.max_lr - self.config.min_lr
                 ) / slow_start_step_size * global_step, lambda:
                (max_lr - min_lr) / const_2 *
                (tf.cos(cos_inner) + const_1) + min_lr), tf.float32)
Esempio n. 20
0
            def _aspect_preserving_width_resize(image, width=512):
                # If training on ADE20k
                height_i = tf.shape(image)[0]
                new_height = height_i - tf.floormod(height_i, 16)

                return tf.image.resize_image_with_crop_or_pad(
                    image, new_height, width)
Esempio n. 21
0
      def mix_data(example):
        """Function to mix the different datasets according to a schedule."""
        del example
        # This block computes the probability of mixing the primary task with
        # the secondary tasks. 0 = only the primary task, 1 = only the secondary
        # tasks.
        if hparams.multiproblem_mixing_schedule == MixingSchedule.EXPONENTIAL:
          prob = get_exp_sched_prob()
        elif hparams.multiproblem_mixing_schedule == MixingSchedule.CONSTANT:
          prob = get_const_sched_prob()
        elif hparams.multiproblem_mixing_schedule == MixingSchedule.PRETRAIN:
          prob = get_pretrain_sched_prob()
        else:
          raise ValueError("Unknown schedule %s" % str(
              hparams.multiproblem_mixing_schedule))
        tf.logging.info("Using the %s schedule to "
                        "train the MultiProblem." % str(
                            hparams.multiproblem_mixing_schedule))
        tf.logging.info("Schedule mixing threshold "
                        "%.2f" % hparams.multiproblem_schedule_threshold)
        prob = tf.cond(
            tf.equal(tf.floormod(
                problem_step, tf.cast(5e6, dtype=tf.int64)), 0),
            lambda: tf.Print(prob, [prob], message="Probability"),
            lambda: prob)

        def sample_task(curr_task, num_tasks_left, randnum):
          """A recursive function to sample a task.

          This function treats the probability as the threshold for the primary
          task and divides the remaining probability mass across the other
          tasks.

          Args:
            curr_task: The index of the task being considered for sampling.
            num_tasks_left: Number of tasks remaining to possibly sample from.
            randnum: The random number used to select the dataset.

          Returns:
            A Tensor representing an example from the task that was sampled
            from.
          """

          if num_tasks_left == 0:
            return get_next_from_dataset(dataset_iterators[curr_task])

          # When curr_task is 0, the primary task, the new prob is the same as
          # the original probability. `tf.greater` indicates that the primary
          # task receives (1-prob) of the probability mass.
          # Otherwise, `prob` is divided equally amongst all the secondary
          # tasks.
          new_prob = prob - (curr_task * prob / (len(self.task_list)-1))
          return tf.cond(
              tf.greater(randnum, new_prob),
              lambda: get_next_from_dataset(dataset_iterators[curr_task]),
              lambda: sample_task(curr_task+1, num_tasks_left-1, randnum)
          )

        return tf.data.Dataset.from_tensors(
            sample_task(0, len(self.task_list)-1, tf.random_uniform([])))
Esempio n. 22
0
    def posenetLoss_nooffset(gt, pred, lossName, batchSize):
        predHeat, gtHeat = pred[:, :, :, :opt.totaljoints], gt[:, :, :, :opt.totaljoints]
        totaljoints = opt.totaljoints

        if opt.hm_lossselect == 'l2':
            heatmapLoss = tf.nn.l2_loss(predHeat - gtHeat, name=lossName + "_heatmapLoss")
        elif opt.hm_lossselect == 'wing':
            heatmapLoss = wing_loss(predHeat, gtHeat)
        elif opt.hm_lossselect == 'adaptivewing':
            heatmapLoss = adaptivewingLoss(predHeat, gtHeat)
        elif opt.hm_lossselect == 'smooth_l1':
            heatmapLoss = smooth_l1_loss(None, predHeat, gtHeat)
        else:
            raise ValueError("Your optimizer name is wrong")

        for recordId in range(batchSize):
            for jointId in range(totaljoints):
                print(str(recordId) + "/" + str(batchSize) + " : " + str(jointId))
                # ================================> decode <x,y> from gt heatmap
                inlinedPix = tf.reshape(gtHeat[recordId, :, :, jointId], [-1])
                pixId = tf.argmax(inlinedPix)
                x = tf.floormod(pixId, gtHeat.shape[2])
                y = tf.cast(tf.divide(pixId, gtHeat.shape[2]), tf.int64)


        print("start building huber loss")
        print("huber loss built")
        tf.summary.scalar(lossName + "_heatmapLoss", heatmapLoss)
        return heatmapLoss
Esempio n. 23
0
def accumulate_gradient(global_step, accumulate_gradients, opt_compute,
                        opt_apply):
    accu = tf.floormod(global_step, accumulate_gradients)
    if tf.equal(accu, 0) is not None:
        return opt_apply
    else:
        return opt_compute
def get_keypoint(image, targets, predictions, heatmap_size, height, width, category, clip_at_zero=True, data_format='channels_last', name=None):
    predictions = tf.reshape(predictions, [1, -1, heatmap_size*heatmap_size])

    pred_max = tf.reduce_max(predictions, axis=-1)
    pred_indices = tf.argmax(predictions, axis=-1)
    pred_x, pred_y = tf.cast(tf.floormod(pred_indices, heatmap_size), tf.float32), tf.cast(tf.floordiv(pred_indices, heatmap_size), tf.float32)

    width, height = tf.cast(width, tf.float32), tf.cast(height, tf.float32)
    pred_x, pred_y = pred_x * width / tf.cast(heatmap_size, tf.float32), pred_y * height / tf.cast(heatmap_size, tf.float32)

    if clip_at_zero:
      pred_x, pred_y =  pred_x * tf.cast(pred_max>0, tf.float32), pred_y * tf.cast(pred_max>0, tf.float32)
      pred_x = pred_x * tf.cast(pred_max>0, tf.float32) + tf.cast(pred_max<=0, tf.float32) * (width / 2.)
      pred_y = pred_y * tf.cast(pred_max>0, tf.float32) + tf.cast(pred_max<=0, tf.float32) * (height / 2.)

    if config.PRED_DEBUG:
      pred_indices_ = tf.squeeze(pred_indices)
      image_ = tf.squeeze(image) * 255.
      pred_heatmap = tf.one_hot(pred_indices_, heatmap_size*heatmap_size, on_value=1., off_value=0., axis=-1, dtype=tf.float32)

      pred_heatmap = tf.reshape(pred_heatmap, [-1, heatmap_size, heatmap_size])
      if data_format == 'channels_first':
        image_ = tf.transpose(image_, perm=(1, 2, 0))
      save_image_op = tf.py_func(save_image_with_heatmap,
                                  [image_, height, width,
                                  heatmap_size,
                                  tf.reshape(pred_heatmap * 255., [-1, heatmap_size, heatmap_size]),
                                  tf.reshape(predictions, [-1, heatmap_size, heatmap_size]),
                                  config.left_right_group_map[category][0],
                                  config.left_right_group_map[category][1],
                                  config.left_right_group_map[category][2]],
                                  tf.int64, stateful=True)
      with tf.control_dependencies([save_image_op]):
        pred_x, pred_y = pred_x * 1., pred_y * 1.
    return pred_x, pred_y
Esempio n. 25
0
 def _aspect_preserving_width_resize(image, width=512):
     height_i = tf.shape(image)[0]
     # width_i = tf.shape(image)[1]
     # ratio = tf.to_float(width_i) / tf.to_float(height_i)
     # new_height = tf.to_int32(tf.to_float(height_i) / ratio)
     new_height = height_i - tf.floormod(height_i, 16)
     return tf.image.resize_image_with_crop_or_pad(image, new_height, width)
Esempio n. 26
0
def one_hot_multiply(inputs, scale):
  """Performs (inputs * scale) % vocab_size in the one-hot space.

  Args:
    inputs: Tensor of shape `[..., vocab_size]`. Typically a soft/hard one-hot
      Tensor.
    scale: Tensor of shape `[..., vocab_size]`. Typically a soft/hard one-hot
      Tensor specifying how much to scale the corresponding one-hot vector in
      inputs. Soft values perform a "weighted scale": for example,
      scale=[0.2, 0.3, 0.5] performs a linear combination of
      0.2 * scaling by zero; 0.3 * scaling by one; and 0.5 * scaling by two.

  Returns:
    Tensor of same shape and dtype as inputs.
  """
  # TODO(trandustin): Implement with circular conv1d.
  inputs = tf.convert_to_tensor(inputs)
  scale = tf.cast(scale, inputs.dtype)
  batch_shape = inputs.shape[:-1].as_list()
  vocab_size = inputs.shape[-1].value
  # Form a [..., vocab_size, vocab_size] tensor. The ith row of the
  # batched vocab_size x vocab_size matrix represents scaling inputs by i.
  permutation_matrix = tf.floormod(
      tf.tile(tf.range(vocab_size)[:, tf.newaxis], [1, vocab_size]) *
      tf.range(vocab_size)[tf.newaxis], vocab_size)
  permutation_matrix = tf.one_hot(permutation_matrix, depth=vocab_size, axis=-1)
  # Scale the inputs according to the permutation matrix of all possible scales.
  scaled_inputs = tf.einsum('...v,avu->...au', inputs, permutation_matrix)
  scaled_inputs = tf.concat([tf.zeros(batch_shape + [1, vocab_size]),
                             scaled_inputs[..., 1:, :]], axis=-2)
  # Reduce rows of the scaled inputs by the scale values. This forms a
  # weighted linear combination of scaling by zero, scaling by one, and so on.
  outputs = tf.einsum('...v,...vu->...u', scale, scaled_inputs)
  return outputs
Esempio n. 27
0
      def mix_data(example):
        """Function to mix the different datasets according to a schedule."""
        del example
        # This block computes the probability of mixing the primary task with
        # the secondary tasks. 0 = only the primary task, 1 = only the secondary
        # tasks.
        if hparams.multiproblem_mixing_schedule == MixingSchedule.EXPONENTIAL:
          prob = get_exp_sched_prob()
          prob = tf.cond(
              tf.equal(tf.floormod(
                  problem_step, tf.cast(5e6, dtype=tf.int64)), 0),
              lambda: tf.Print(prob, [prob], message="Probability"),
              lambda: prob)
        elif hparams.multiproblem_mixing_schedule == MixingSchedule.CONSTANT:
          prob = get_const_sched_prob()
        elif hparams.multiproblem_mixing_schedule == MixingSchedule.PRETRAIN:
          prob = get_pretrain_sched_prob()
        else:
          raise ValueError("Unknown schedule %s" % str(
              hparams.multiproblem_mixing_schedule))
        tf.logging.info("Using the %s schedule to "
                        "train the MultiProblem." % str(
                            hparams.multiproblem_mixing_schedule))
        tf.logging.info("Schedule mixing threshold "
                        "%.2f" % hparams.multiproblem_schedule_threshold)

        def sample_task(curr_task, num_tasks_left, randnum):
          """A recursive function to sample a task.

          This function treats the probability as the threshold for the primary
          task and divides the remaining probability mass across the other
          tasks.

          Args:
            curr_task: The index of the task being considered for sampling.
            num_tasks_left: Number of tasks remaining to possibly sample from.
            randnum: The random number used to select the dataset.

          Returns:
            A Tensor representing an example from the task that was sampled
            from.
          """
          if num_tasks_left == 0:
            return get_next_from_dataset(dataset_iterators[curr_task])

          # When curr_task is 0, the primary task, the new prob is the same as
          # the original probability. `tf.greater` indicates that the primary
          # task receives (1-prob) of the probability mass.
          # Otherwise, `prob` is divided equally amongst all the secondary
          # tasks.
          new_prob = prob - (curr_task * prob / (len(self.task_list)-1))
          return tf.cond(
              tf.greater(randnum, new_prob),
              lambda: get_next_from_dataset(dataset_iterators[curr_task]),
              lambda: sample_task(curr_task+1, num_tasks_left-1, randnum)
          )

        return tf.data.Dataset.from_tensors(
            sample_task(0, len(self.task_list)-1, tf.random_uniform([])))
Esempio n. 28
0
    def fake_roi_pooling(self, tensor, output_dims=(8, 8)):

        # izvuci dimenzije slike
        height = tf.shape(tensor)[1]
        width = tf.shape(tensor)[2]
        input_channels = tensor.get_shape()[-1]

        # dim % roi_pool_size = rest
        h_rest = tf.floormod(height, output_dims[0])
        w_rest = tf.floormod(width, output_dims[1])

        # padd = roi_pool_size - rest
        h_padd = tf.sign(h_rest) * output_dims[0] - h_rest  # if 0 no change
        w_padd = tf.sign(w_rest) * output_dims[1] - w_rest

        # padd image
        h_div = tf.floordiv(h_padd, 2)
        h_mod = tf.mod(h_padd, 2)
        h_paddings = [h_div, h_div + h_mod]

        w_div = tf.floordiv(w_padd, 2)
        w_mod = tf.mod(w_padd, 2)
        w_paddings = [w_div, w_div + w_mod]

        paddings = tf.convert_to_tensor([[0, 0], h_paddings, w_paddings,
                                         [0, 0]])
        tensor = tf.pad(tensor, paddings=paddings, constant_values=-np.inf)

        splits = tf.split(tensor, output_dims[0], axis=1)
        polled = []

        for hsplit in splits:
            wsplits = tf.split(hsplit, output_dims[1], axis=2)

            pooled = [
                tf.reduce_max(wsplit, axis=(1, 2), keepdims=True)
                for wsplit in wsplits
            ]
            polled_w = tf.concat(pooled, axis=2)

            polled.append(polled_w)

        tensor = tf.concat(polled, axis=1)

        return tf.reshape(tensor,
                          [-1, output_dims[0], output_dims[1], input_channels])
Esempio n. 29
0
 def test_floor_mod(self):
     shape = [3, 4, 5]
     graph = tf.Graph()
     with graph.as_default() as g:
         a = tf.placeholder(tf.float32, shape=shape, name='a')
         b = tf.placeholder(tf.float32, shape=shape, name='b')
         out = tf.floormod(a, b)
     self._test_tf_model_constant(graph, {'a': shape, 'b': shape}, [out.op.name])
Esempio n. 30
0
 def is_last_day_of_season(t):
     t_ = dist_util.maybe_get_static_value(t)
     if t_ is not None:  # static case
         step_in_cycle = t_ % num_steps_per_cycle
         return any(step_in_cycle == changepoints)
     else:
         step_in_cycle = tf.floormod(t, num_steps_per_cycle)
         return tf.reduce_any(tf.equal(step_in_cycle, changepoints))
def int_to_bit(x_int, nbits):
  """Turn x_int representing numbers into a bitwise (lower-endian) tensor."""
  x_l = tf.expand_dims(x_int, axis=-1)
  x_labels = []
  for i in range(nbits):
    x_labels.append(tf.floormod(tf.floordiv(x_l, 2**i), 2))
  res = tf.concat(x_labels, axis=-1)
  return tf.to_float(res)
Esempio n. 32
0
def int_to_bit(x_int, nbits):
  """Turn x_int representing numbers into a bitwise (lower-endian) tensor."""
  x_l = tf.expand_dims(x_int, axis=-1)
  x_labels = []
  for i in range(nbits):
    x_labels.append(tf.floormod(tf.floordiv(x_l, 2**i), 2))
  res = tf.concat(x_labels, axis=-1)
  return tf.to_float(res)
Esempio n. 33
0
def pyramidal_stack(outputs, sequence_length):
    shape = tf.shape(outputs)
    batch_size, max_time = shape[0], shape[1]
    num_units = outputs.get_shape().as_list()[-1]
    paddings = [[0, 0], [0, tf.floormod(max_time, 2)], [0, 0]]
    outputs = tf.pad(outputs, paddings)
    '''
    even_time = outputs[:, ::2, :]
    odd_time = outputs[:, 1::2, :]

    concat_outputs = tf.concat([even_time, odd_time], -1)
    '''

    concat_outputs = tf.reshape(outputs, (batch_size, -1, num_units * 2))

    return concat_outputs, tf.floordiv(sequence_length, 2) + tf.floormod(
        sequence_length, 2)
Esempio n. 34
0
 def is_last_day_of_season(t):
   t_ = dist_util.maybe_get_static_value(t)
   if t_ is not None:  # static case
     step_in_cycle = t_ % num_steps_per_cycle
     return any(step_in_cycle == changepoints)
   else:
     step_in_cycle = tf.floormod(t, num_steps_per_cycle)
     return tf.reduce_any(tf.equal(step_in_cycle, changepoints))
Esempio n. 35
0
def ae_latent_softmax(latents_pred, latents_discrete, hparams):
  """Latent prediction and loss."""
  vocab_size = 2 ** hparams.z_size
  if hparams.num_decode_blocks < 2:
    latents_logits = tf.layers.dense(latents_pred, vocab_size,
                                     name="extra_logits")
    if hparams.logit_normalization:
      latents_logits *= tf.rsqrt(1e-8 +
                                 tf.reduce_mean(tf.square(latents_logits)))

    loss = None
    if latents_discrete is not None:
      if hparams.soft_em:
        # latents_discrete is actually one-hot of multinomial samples
        assert hparams.num_decode_blocks == 1
        loss = tf.nn.softmax_cross_entropy_with_logits_v2(
            labels=latents_discrete, logits=latents_logits)
      else:
        loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=latents_discrete, logits=latents_logits)
    sample = multinomial_sample(
        latents_logits, vocab_size, hparams.sampling_temp)
    return sample, loss

  # Multi-block case.
  vocab_bits = int(math.log(vocab_size, 2))
  assert vocab_size == 2**vocab_bits
  assert vocab_bits % hparams.num_decode_blocks == 0
  block_vocab_size = 2**(vocab_bits // hparams.num_decode_blocks)
  latents_logits = [
      tf.layers.dense(
          latents_pred, block_vocab_size, name="extra_logits_%d" % i)
      for i in range(hparams.num_decode_blocks)
  ]
  loss = None
  if latents_discrete is not None:
    losses = []
    for i in range(hparams.num_decode_blocks):
      d = tf.floormod(tf.floordiv(latents_discrete,
                                  block_vocab_size**i), block_vocab_size)
      losses.append(tf.nn.sparse_softmax_cross_entropy_with_logits(
          labels=d, logits=latents_logits[i]))
    loss = sum(losses)
  samples = [multinomial_sample(l, block_vocab_size, hparams.sampling_temp)
             for l in latents_logits]
  sample = sum([s * block_vocab_size**i for i, s in enumerate(samples)])
  return sample, loss
Esempio n. 36
0
def int_to_bit(x_int, num_bits, base=2):
  """Turn x_int representing numbers into a bitwise (lower-endian) tensor.

  Args:
    x_int: Tensor containing integer to be converted into base notation.
    num_bits: Number of bits in the representation.
    base: Base of the representation.

  Returns:
    Corresponding number expressed in base.
  """
  x_l = tf.to_int32(tf.expand_dims(x_int, axis=-1))
  x_labels = []
  for i in range(num_bits):
    x_labels.append(
        tf.floormod(
            tf.floordiv(tf.to_int32(x_l),
                        tf.to_int32(base)**i), tf.to_int32(base)))
  res = tf.concat(x_labels, axis=-1)
  return tf.to_float(res)
Esempio n. 37
0
def ae_latent_softmax(latents_pred, latents_discrete, hparams):
  """Latent prediction and loss."""
  vocab_size = hparams.v_size
  if hparams.bottleneck_kind == "semhash":
    vocab_size = 2**hparams.z_size
  if hparams.num_decode_blocks < 2:
    latents_logits = tf.layers.dense(latents_pred, vocab_size,
                                     name="extra_logits")
    loss = None
    if latents_discrete is not None:
      loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
          labels=latents_discrete, logits=latents_logits)
    sample = multinomial_sample(
        latents_logits, vocab_size, hparams.sampling_temp)
    return sample, loss

  # Multi-block case.
  vocab_bits = int(math.log(vocab_size, 2))
  assert vocab_size == 2**vocab_bits
  assert vocab_bits % hparams.num_decode_blocks == 0
  block_vocab_size = 2**(vocab_bits // hparams.num_decode_blocks)
  latents_logits = [
      tf.layers.dense(
          latents_pred, block_vocab_size, name="extra_logits_%d" % i)
      for i in xrange(hparams.num_decode_blocks)
  ]
  loss = None
  if latents_discrete is not None:
    losses = []
    for i in xrange(hparams.num_decode_blocks):
      d = tf.floormod(tf.floordiv(latents_discrete,
                                  block_vocab_size**i), block_vocab_size)
      losses.append(tf.nn.sparse_softmax_cross_entropy_with_logits(
          labels=d, logits=latents_logits[i]))
    loss = sum(losses)
  samples = [multinomial_sample(l, block_vocab_size, hparams.sampling_temp)
             for l in latents_logits]
  sample = sum([s * block_vocab_size**i for i, s in enumerate(samples)])
  return sample, loss