Ejemplo n.º 1
0
def linear_warmup(global_step, warmup_steps, name=None):
    """"Linearly increase the output from 0 to 1 over `warmup_steps` steps.

  Args:
    global_step: A scalar int32 or int64 Tensor or a Python integer.
    warmup_steps: Non-negative integer, number of warmup steps at the beginning
        of training.
    name: Optional name for the TensorFlow scope.

  Returns:
    A scalar float Tensor between 0 and 1.
  """
    with tf.name_scope(name, 'linear_warmup', [global_step, warmup_steps]):
        if warmup_steps < 0:
            raise ValueError(
                'Invalid warmup_steps, must be a non-negative integer: {}'.
                format(warmup_steps))
        elif warmup_steps == 0:
            return tf.constant(1.0, dtype=tf.float32)
        else:  # warmup_steps > 0
            global_step_float = tf.cast(global_step, tf.float32)
            warmup_steps_float = tf.cast(warmup_steps, tf.float32)
            result = global_step_float / warmup_steps_float
            result = tf.where_v2(global_step >= warmup_steps, 1.0, result)
            result = tf.where_v2(global_step <= 0, 0.0, result)
            return result
Ejemplo n.º 2
0
def _replace_sample_with_probability(value, log_prob, replace_prob,
                                     replace_value):
    """Replace `value` with `replace_value` with probability `replace_prob`."""
    if replace_prob is None:
        return (value, log_prob)

    replace_prob = tf.convert_to_tensor(replace_prob, tf.float32)
    should_replace = tf.less(tf.random_uniform(()), replace_prob)
    new_value = tf.where_v2(should_replace, replace_value, value)
    new_log_prob = tf.where_v2(should_replace, tf.log(replace_prob),
                               tf.log(1 - replace_prob) + log_prob)
    return (new_value, new_log_prob)
Ejemplo n.º 3
0
def _sharpness(image, factor):
    """Implements Sharpness function from PIL using TF ops."""
    orig_image = image
    image = tf.cast(image, tf.float32)
    # Make image 4D for conv operation.
    image = tf.expand_dims(image, 0)
    # SMOOTH PIL Kernel.
    kernel = tf.constant([[1, 1, 1], [1, 5, 1], [1, 1, 1]],
                         dtype=tf.float32,
                         shape=[3, 3, 1, 1]) / 13.
    # Tile across channel dimension.
    kernel = tf.tile(kernel, [1, 1, 3, 1])
    strides = [1, 1, 1, 1]
    degenerate = tf.nn.depthwise_conv2d(image,
                                        kernel,
                                        strides,
                                        padding='VALID',
                                        dilations=[1, 1])
    degenerate = tf.clip_by_value(degenerate, 0.0, 255.0)
    degenerate = tf.squeeze(tf.cast(degenerate, tf.uint8), [0])

    # For the borders of the resulting image, fill in the values of the
    # original image.
    mask = tf.ones_like(degenerate)
    padded_mask = tf.pad(mask, [[1, 1], [1, 1], [0, 0]])
    padded_degenerate = tf.pad(degenerate, [[1, 1], [1, 1], [0, 0]])
    result = tf.where_v2(tf.equal(padded_mask, 1), padded_degenerate,
                         orig_image)

    # Blend the final result.
    return blend(result, orig_image, factor)
Ejemplo n.º 4
0
def nms_tf(dets, thresh):
  """Non-maximum suppression with tf graph mode."""
  x1 = dets[:, 0]
  y1 = dets[:, 1]
  x2 = dets[:, 2]
  y2 = dets[:, 3]
  scores = dets[:, 4]

  areas = (x2 - x1 + 1) * (y2 - y1 + 1)
  order = tf.argsort(scores, direction='DESCENDING')

  keep = tf.TensorArray(tf.int32, size=0, dynamic_size=True)
  index = 0
  while tf.shape(order)[0] > 0:
    i = order[0]
    keep = keep.write(index, i)
    xx1 = tf.maximum(x1[i], tf.gather(x1, order[1:]))
    yy1 = tf.maximum(y1[i], tf.gather(y1, order[1:]))
    xx2 = tf.minimum(x2[i], tf.gather(x2, order[1:]))
    yy2 = tf.minimum(y2[i], tf.gather(y2, order[1:]))

    w = tf.maximum(0.0, xx2 - xx1 + 1)
    h = tf.maximum(0.0, yy2 - yy1 + 1)
    intersection = w * h
    overlap = intersection / (
        areas[i] + tf.gather(areas, order[1:]) - intersection)

    inds = tf.where_v2(overlap <= thresh)
    order = tf.concat(tf.gather(order, inds + 1), axis=1)
    order = tf.squeeze(order, axis=-1)
    index += 1
  return keep.stack()
Ejemplo n.º 5
0
    def scale_channel(im, c):
        """Scale the data in the channel to implement equalize."""
        im = tf.cast(im[:, :, c], tf.int32)
        # Compute the histogram of the image channel.
        histo = tf.histogram_fixed_width(im, [0, 255], nbins=256)

        # For the purposes of computing the step, filter out the nonzeros.
        nonzero = tf.where_v2(tf.not_equal(histo, 0))
        nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1])
        step = (tf.reduce_sum(nonzero_histo) - nonzero_histo[-1]) // 255

        def build_lut(histo, step):
            # Compute the cumulative sum, shifting by step // 2
            # and then normalization by step.
            lut = (tf.cumsum(histo) + (step // 2)) // step
            # Shift lut, prepending with 0.
            lut = tf.concat([[0], lut[:-1]], 0)
            # Clip the counts to be in range.  This is done
            # in the C code for image.point.
            return tf.clip_by_value(lut, 0, 255)

        # If step is zero, return the original image.  Otherwise, build
        # lut from the full histogram and step and then index from it.
        result = tf.cond(tf.equal(step, 0), lambda: im,
                         lambda: tf.gather(build_lut(histo, step), im))

        return tf.cast(result, tf.uint8)
Ejemplo n.º 6
0
 def _blend():
     image_1 = tf.image.convert_image_dtype(image1, tf.float32)
     image_2 = tf.image.convert_image_dtype(image2, tf.float32)
     output = image_1 + factor * (image_2 - image_1)
     output = tf.where_v2(
         tf.logical_and(tf.less(0., factor), tf.less(factor, 1.)),
         x=output, y=tf.clip_by_value(output, 0., 255.))
     return tf.image.convert_image_dtype(output, tf.uint8)
Ejemplo n.º 7
0
def solarize_add(image,
                 addition=0,
                 threshold=128):
    """If `pixel < threshold`, add `addition` to it and clip between 0 and 255."""
    threshold = tf.cast(threshold, image.dtype)
    added_image = tf.cast(image, tf.int32) + addition
    added_image = tf.cast(tf.clip_by_value(added_image, 0, 255), tf.uint8)
    return tf.where_v2(image < threshold, added_image, image)
 def my_fn(ex):
   inputs = ex["inputs"]
   res = ex.copy()
   res["inputs"] = tf.where_v2(
       tf.greater(inputs, 15),
       tf.constant(50, tf.int64),
       inputs)
   return res
Ejemplo n.º 9
0
def hz_to_midi(frequencies: Number) -> Number:
    """TF-compatible hz_to_midi function."""
    frequencies = tf_float32(frequencies)
    log2 = lambda x: tf.log(x) / tf.log(2.0)
    notes = 12.0 * (log2(frequencies) - log2(440.0)) + 69.0
    # Map 0 Hz to MIDI 0 (Replace -inf with 0.)
    cond = tf.equal(notes, -np.inf)
    notes = tf.where_v2(cond, 0.0, notes)
    return notes
Ejemplo n.º 10
0
    def decode_tf(self, ids):
        """Decode in TensorFlow.

    Args:
      ids: a 1d tf.Tensor with dtype tf.int32
    Returns:
      a tf Scalar with dtype tf.string
    """
        ids = tf.where_v2(tf.less(ids, self._tokenizer.GetPieceSize()), ids,
                          self._tokenizer.unk_id())

        return self._tf_tokenizer.detokenize(ids)
def tf_make_divisible(v, divisor):
    """Analogue of make_divisible() that operates on tf.Tensor objects."""
    v = tf.convert_to_tensor(v, preferred_dtype=tf.int32)
    divisor = tf.convert_to_tensor(divisor, dtype=v.dtype)

    new_v = tf.cast(v, tf.float32) + tf.cast(divisor, tf.float32) / 2
    new_v = tf.cast(new_v, v.dtype) // divisor * divisor
    new_v = tf.maximum(new_v, divisor)

    # Condition is equivalent to (new_v * 0.9*v), but works with integer inputs.
    new_v = tf.where_v2(10 * new_v < 9 * v, new_v + divisor, new_v)
    return new_v
Ejemplo n.º 12
0
def _cutout(image, pad_size, replace=0):
    """Apply cutout (https://arxiv.org/abs/1708.04552) to image.

  This operation applies a (2*pad_size x 2*pad_size) mask of zeros to
  a random location within `img`. The pixel values filled in will be of the
  value `replace`. The located where the mask will be applied is randomly
  chosen uniformly over the whole image.

  Args:
    image: An image Tensor of type uint8.
    pad_size: Specifies how big the zero mask that will be generated is that
      is applied to the image. The mask will be of size
      (2*pad_size x 2*pad_size).
    replace: What pixel value to fill in the image in the area that has
      the cutout mask applied to it.

  Returns:
    An image Tensor that is of type uint8.
  """
    image_height = tf.shape(image)[0]
    image_width = tf.shape(image)[1]

    # Samples the center location in the image where the zero mask is applied.
    cutout_center_height = tf.random.uniform(shape=[],
                                             minval=0,
                                             maxval=image_height,
                                             dtype=tf.int32)

    cutout_center_width = tf.random.uniform(shape=[],
                                            minval=0,
                                            maxval=image_width,
                                            dtype=tf.int32)

    lower_pad = tf.maximum(0, cutout_center_height - pad_size)
    upper_pad = tf.maximum(0, image_height - cutout_center_height - pad_size)
    left_pad = tf.maximum(0, cutout_center_width - pad_size)
    right_pad = tf.maximum(0, image_width - cutout_center_width - pad_size)

    cutout_shape = [
        image_height - (lower_pad + upper_pad),
        image_width - (left_pad + right_pad)
    ]
    padding_dims = [[lower_pad, upper_pad], [left_pad, right_pad]]
    mask = tf.pad(tf.zeros(cutout_shape, dtype=image.dtype),
                  padding_dims,
                  constant_values=1)
    mask = tf.expand_dims(mask, -1)
    mask = tf.tile(mask, [1, 1, 3])
    image = tf.where_v2(tf.equal(mask, 0),
                        tf.ones_like(image, dtype=image.dtype) * replace,
                        image)
    return image
Ejemplo n.º 13
0
 def build_graph(parameters):
   """Build the where op testing graph."""
   input_condition = tf.compat.v1.placeholder(
       dtype=tf.bool,
       name="input_condition",
       shape=parameters["input_condition_shape"])
   input_value1 = tf.compat.v1.placeholder(
       dtype=parameters["input_dtype"],
       name="input_x",
       shape=parameters["input_shape_set"][0])
   input_value2 = tf.compat.v1.placeholder(
       dtype=parameters["input_dtype"],
       name="input_y",
       shape=parameters["input_shape_set"][1])
   out = tf.where_v2(input_condition, input_value1, input_value2)
   return [input_condition, input_value1, input_value2], [out]
Ejemplo n.º 14
0
    def nq_map(ex):
        """Map Natural Questions example to text-to-text example."""
        inputs = tf.strings.join([prefix, 'question:', ex['question']['text']],
                                 separator=' ')

        annotations = ex['annotations']

        yes_no_labels = annotations['yes_no_answer']
        if drop_yes_no:
            yes_no_labels = -1 * tf.ones_like(yes_no_labels)
        yes_no_answers = tf.boolean_mask(yes_no_labels, yes_no_labels > -1)
        yes_no_answers = tf.where_v2(tf.equal(yes_no_answers, 1), 'yes', 'no')

        short_answers = annotations['short_answers']['text'].flat_values
        short_answer_starts = annotations['short_answers']['text'].row_starts()
        if max_tokens:
            start_tokens = annotations['short_answers']['start_token']
            end_tokens = annotations['short_answers']['end_token']
            dropped_answers = end_tokens - start_tokens > max_tokens
            short_answers = tf.boolean_mask(
                short_answers, tf.math.logical_not(dropped_answers.values))
            # Subtract dropped answers from row starts.
            row_drop_count = tf.math.reduce_sum(tf.cast(
                dropped_answers, tf.int64),
                                                axis=1)
            short_answer_starts -= tf.concat(
                [[0], tf.math.cumsum(row_drop_count[:-1])], axis=0)

        answers = tf.concat([yes_no_answers, short_answers], axis=0)
        if max_answers:
            answers = answers[:max_answers]
        targets = tf.strings.reduce_join('answer: ' + answers, separator=' ')

        return {
            'inputs': inputs,
            'targets': targets,
            'short_answers/values': short_answers,
            'short_answers/row_starts': short_answer_starts,
            'yes_no_answers': yes_no_labels
        }
Ejemplo n.º 15
0
def _unwrap(image, replace):
    """_unwraps an image produced by _wrap.

  Where there is a 0 in the last channel for every spatial position,
  the rest of the three channels in that spatial dimension are grayed
  (set to 128).  Operations like translate and shear on a _wrapped
  Tensor will leave 0s in empty locations.  Some transformations look
  at the intensity of values to do preprocessing, and we want these
  empty pixels to assume the 'average' value, rather than pure black.


  Args:
    image: A 3D Image Tensor with 4 channels.
    replace: A one or three value 1D tensor to fill empty pixels.

  Returns:
    image: A 3D image Tensor with 3 channels.
  """
    image_shape = tf.shape(image)
    # Flatten the spatial dimensions.
    flattened_image = tf.reshape(image, [-1, image_shape[2]])

    # Find all pixels where the last channel is zero.
    alpha_channel = tf.expand_dims(flattened_image[:, 3], axis=-1)
    alpha_channel = tf.tile(alpha_channel, [1, 4])

    replace = tf.concat([replace, tf.ones([1], image.dtype)], 0)

    # Where they are zero, fill them in with 'replace'.
    flattened_image = tf.where_v2(
        tf.equal(alpha_channel, 0),
        tf.ones_like(flattened_image, dtype=image.dtype) * replace,
        flattened_image)

    image = tf.reshape(flattened_image, image_shape)
    image = tf.slice(image, [0, 0, 0], [image_shape[0], image_shape[1], 3])
    return image
Ejemplo n.º 16
0
 def build_graph(parameters):
     """Build the where op testing graph."""
     # To actually use where op, x, y params to where_v2 needs to be None.
     # This is needed when type is not bool, so we actually use where op.
     if parameters["condition_dtype"] != tf.bool and parameters[
             "input_dtype"] is not None:
         parameters["condition_dtype"] = tf.bool
     input_condition = tf.compat.v1.placeholder(
         dtype=parameters["condition_dtype"],
         name="input_condition",
         shape=parameters["input_condition_shape"])
     input_value1 = None
     input_value2 = None
     if parameters["input_dtype"] is not None:
         input_value1 = tf.compat.v1.placeholder(
             dtype=parameters["input_dtype"],
             name="input_x",
             shape=parameters["input_shape_set"][0])
         input_value2 = tf.compat.v1.placeholder(
             dtype=parameters["input_dtype"],
             name="input_y",
             shape=parameters["input_shape_set"][1])
     out = tf.where_v2(input_condition, input_value1, input_value2)
     return [input_condition, input_value1, input_value2], [out]
Ejemplo n.º 17
0
def _solarize(image, threshold=128):
    """Keep a pixel if `pixel < threshold`, otherwise subtract 225 from it."""
    threshold = tf.cast(threshold, image.dtype)
    return tf.where_v2(image < threshold, image, 255 - image)
Ejemplo n.º 18
0
def _generate_detections_tf(cls_outputs, box_outputs, anchor_boxes, indices,
                            classes, image_id, image_scale, num_classes,
                            min_score_thresh=0.2, max_boxes_to_draw=50,
                            soft_nms_sigma=0.0, iou_threshold=0.5,
                            use_native_nms=False):
  """Generates detections with model outputs and anchors.

  Args:
    cls_outputs: a numpy array with shape [N, 1], which has the highest class
      scores on all feature levels. The N is the number of selected
      top-K total anchors on all levels.  (k being MAX_DETECTION_POINTS)
    box_outputs: a numpy array with shape [N, 4], which stacks box regression
      outputs on all feature levels. The N is the number of selected top-k
      total anchors on all levels. (k being MAX_DETECTION_POINTS)
    anchor_boxes: a numpy array with shape [N, 4], which stacks anchors on all
      feature levels. The N is the number of selected top-k total anchors on
      all levels.
    indices: a numpy array with shape [N], which is the indices from top-k
      selection.
    classes: a numpy array with shape [N], which represents the class
      prediction on all selected anchors from top-k selection.
    image_id: an integer number to specify the image id.
    image_scale: a float tensor representing the scale between original image
      and input image for the detector. It is used to rescale detections for
      evaluating with the original groundtruth annotations.
    num_classes: a integer that indicates the number of classes.
    min_score_thresh: A float representing the threshold for deciding when to
      remove boxes based on score.
    max_boxes_to_draw: Max number of boxes to draw.
    soft_nms_sigma: A scalar float representing the Soft NMS sigma parameter;
      See Bodla et al, https://arxiv.org/abs/1704.04503).  When
        `soft_nms_sigma=0.0` (which is default), we fall back to standard (hard)
        NMS.
    iou_threshold: A float representing the threshold for deciding whether boxes
      overlap too much with respect to IOU.
    use_native_nms: a bool that indicates whether to use native nms.

  Returns:
    detections: detection results in a tensor with each row representing
      [image_id, y, x, height, width, score, class]
  """
  anchor_boxes = tf.gather(anchor_boxes, indices)

  scores = tf.math.sigmoid(cls_outputs)
  # apply bounding box regression to anchors
  boxes = decode_box_outputs_tf(
      tf.transpose(box_outputs, [1, 0]), tf.transpose(anchor_boxes, [1, 0]))

  def _else(detections, class_id, indices):
    """Else branch for generating detections."""
    boxes_cls = tf.gather(boxes, indices)
    scores_cls = tf.gather(scores, indices)
    # Select top-scoring boxes in each class and apply non-maximum suppression
    # (nms) for boxes in the same class. The selected boxes from each class are
    # then concatenated for the final detection outputs.

    if use_native_nms:
      top_detection_idx, scores_cls = tf.image.non_max_suppression_with_scores(
          boxes_cls,
          scores_cls,
          max_boxes_to_draw,
          iou_threshold=iou_threshold,
          score_threshold=min_score_thresh,
          soft_nms_sigma=soft_nms_sigma)
      scores_cls = tf.expand_dims(scores_cls, axis=1)
      boxes_cls = tf.gather(boxes_cls, top_detection_idx)
      top_detections_cls = tf.concat([boxes_cls, scores_cls], axis=1)
    else:
      scores_cls = tf.expand_dims(scores_cls, axis=1)
      all_detections_cls = tf.concat([boxes_cls, scores_cls], axis=1)
      top_detection_idx = nms_tf(all_detections_cls, iou_threshold)
      top_detections_cls = tf.gather(all_detections_cls, top_detection_idx)
    height = top_detections_cls[:, 2] - top_detections_cls[:, 0]
    width = top_detections_cls[:, 3] - top_detections_cls[:, 1]
    top_detections_cls = tf.stack([top_detections_cls[:, 0] * image_scale,
                                   top_detections_cls[:, 1] * image_scale,
                                   height * image_scale, width * image_scale,
                                   top_detections_cls[:, 4]], axis=-1)

    top_detections_cls = tf.stack(
        [
            tf.cast(
                tf.repeat(image_id, tf.size(top_detection_idx)), tf.float32),
            *tf.unstack(top_detections_cls, 5, axis=1),
            tf.repeat(class_id + 1.0, tf.size(top_detection_idx))
        ],
        axis=1)

    detections = tf.concat([detections, top_detections_cls], axis=0)

    return detections

  detections = tf.constant([], tf.float32, [0, 7])
  for c in range(num_classes):
    indices_cls = tf.squeeze(tf.where_v2(tf.equal(classes, c)), axis=-1)
    detections = tf.cond(
        tf.equal(tf.size(indices), 0), lambda: detections,
        lambda id=c, id_cls=indices_cls: _else(detections, id, id_cls))
  indices_final = tf.argsort(detections[:, -2], direction='DESCENDING')
  detections = tf.gather(
      detections, indices_final[:max_boxes_to_draw], name='detection')
  return detections
Ejemplo n.º 19
0
def TransferConvert(l2g,
                    last,
                    transfer=None,
                    gamma=1.0,
                    epsilon=1e-8,
                    scope=None,
                    scope_default='TransferConvert'):
    # transfer characteristics
    if isinstance(transfer, str):
        transfer = TRANSFER_TABLE[transfer.upper()]
    if transfer is None or transfer in [2, 8]:
        formula = 0
    elif transfer == 4:
        formula = 0
        gamma *= 2.2
    elif transfer == 5:
        formula = 0
        gamma *= 2.8
    elif transfer in [1, 6, 14, 15]:
        formula = 1
        power = 0.45
        power_rec = 1 / power
        slope = 4.500
        alpha = 1.099296826809442
        beta = 0.018053968510807
        k0 = beta * slope
    elif transfer == 7:
        formula = 1
        power = 0.45
        power_rec = 1 / power
        slope = 4.0
        alpha = 1.1115
        k0 = 0.0912
        beta = k0 / slope
    elif transfer == 13:
        formula = 1
        power = 1 / 2.4
        power_rec = 2.4  # 12 / 5
        slope = 12.92  # 323 / 25
        alpha = 1.055  # 211 / 200
        k0 = 0.04045
        beta = k0 / slope
    elif transfer == 113:
        formula = 1
        power = 1 / 2.4
        power_rec = 2.4
        slope = 12.9232102
        alpha = 1.055
        k0 = 11 / 280
        beta = k0 / slope
    elif transfer == 9:
        formula = 2
        alpha = 1 / 2
        beta = 0.01
    elif transfer == 10:
        formula = 2
        alpha = 2 / 5
        beta = np.sqrt(10) / 1000
    else:
        raise ValueError(
            'transfer={} currently not supported'.format(transfer))
    with tf.variable_scope(scope, scope_default):
        # clipping
        if gamma != 1.0 or formula in [1, 2]:
            last = tf.clip_by_value(last, 0, 1)
        # apply transfer
        if formula == 0:
            pass
        elif formula == 1:
            if l2g:
                last = tf.where_v2(
                    last < beta, slope * last,
                    alpha * (last + epsilon)**power - (alpha - 1))
            else:
                last = tf.where_v2(last < k0, (1 / slope) * last,
                                   ((1 / alpha) * (last +
                                                   (alpha - 1)))**power_rec)
        elif formula == 2:
            if l2g:
                last = tf.math.maximum(
                    0.0,
                    1.0 + (alpha / np.log(10)) * tf.math.log(last + epsilon))
            else:
                last = tf.math.pow(10.0, (1 / alpha) * (last - 1.0))
        # pure gamma adjustment
        if gamma == 2.0:
            last = tf.math.sqrt(last +
                                epsilon) if l2g else tf.math.square(last)
        elif gamma == 0.5:
            last = tf.math.square(last) if l2g else tf.math.sqrt(last +
                                                                 epsilon)
        elif gamma > 1.0:
            last = (last + epsilon)**(1 / gamma) if l2g else last**gamma
        elif gamma < 1.0:
            last = last**(1 / gamma) if l2g else (last + epsilon)**gamma
    return last