Beispiel #1
0
def resize_im(image, image_size, pad_val, channels, elements_boxes=None):
  """Decodes and resizes the image.

  Args:
    image: Image to resize.
    image_size: The desired max image size.
    pad_val: The value to pad with.
    channels: The number of channels in the image.
    elements_boxes: The boxes from elements to resize.

  Returns:
    Resized image with possible padded regions,
    and possibly the resized elements boxes.
  """
  [width, height, got_channels] = preprocess_utils.resolve_shape(image, rank=3)

  new_width, new_height = get_resize_dim(width, height, image_size)

  image = tf.reshape(image, [width, height, -1])
  image = tf.cond(
      tf.logical_and(channels == 3, tf.equal(got_channels, 1)),
      true_fn=lambda: tf.image.grayscale_to_rgb(image),
      false_fn=lambda: image,
  )

  image = tf.image.resize_images(image, [new_width, new_height])

  image = preprocess_utils.pad_to_bounding_box(image, 0, 0, image_size,
                                               image_size, pad_val)
  if elements_boxes is not None:
    return image, elements_boxes / tf.to_float(tf.maximum(width, height))

  return image
def resize_im(image, image_size, pad_val, channels, features=None):
    """Decodes and resizes the image.

  Args:
    image: Image to resize.
    image_size: The desired max image size.
    pad_val: The value to pad with.
    channels: The number of channels in the image.
    features: Other features to resize.

  Returns:
    Resized image with possible padded regions,
    and possibly the resized elements boxes.
  """
    [height, width, got_channels] = preprocess_utils.resolve_shape(image,
                                                                   rank=3)

    new_height, new_width = get_resize_dim(height, width, image_size)

    image = tf.reshape(image, [height, width, -1])
    image = tf.cond(
        tf.logical_and(channels == 3, tf.equal(got_channels, 1)),
        true_fn=lambda: tf.image.grayscale_to_rgb(image),
        false_fn=lambda: image,
    )

    image = tf.image.resize_images(image, [new_height, new_width])

    image = preprocess_utils.pad_to_bounding_box(image, 0, 0, image_size,
                                                 image_size, pad_val)
    if features is not None:
        width, height = tf.to_float(width), tf.to_float(height)
        max_dim = tf.to_float(tf.maximum(width, height))
        features[ELEMENTS_BOX_ID] = features[ELEMENTS_BOX_ID] / max_dim
        if GROUNDTRUTH_XMIN_ID in features:
            features[GROUNDTRUTH_XMIN_ID] *= width / max_dim
            features[GROUNDTRUTH_XMAX_ID] *= width / max_dim
            features[GROUNDTRUTH_YMIN_ID] *= height / max_dim
            features[GROUNDTRUTH_YMAX_ID] *= height / max_dim
    return image