예제 #1
0
def parse_record(raw_record, is_training=True, dtype=tf.float32):
    """Parses a record containing a training example of an image.
    The input record is parsed into a label and image, and the image is passed
    through preprocessing steps (cropping, flipping, and so on).
    Args:
        raw_record: scalar Tensor tf.string containing a serialized
        Example protocol buffer.
        is_training: A boolean denoting whether the input is for training.
        dtype: data type to use for images/features.
    Returns:
        Tuple with processed image tensor and one-hot-encoded label tensor.
    """
    image_buffer, label, bbox = _parse_example_proto(raw_record)

    image = imagenet_preprocessing.preprocess_image(
        image_buffer=image_buffer,
        bbox=bbox,
        output_height=DEFAULT_IMAGE_SIZE,
        output_width=DEFAULT_IMAGE_SIZE,
        num_channels=NUM_CHANNELS,
        is_training=is_training
    )
    image = tf.cast(image, dtype)

    return image, label
def parse_record(raw_record, is_training):
    """Parses a record containing a training example of an image.

  The input record is parsed into a label and image, and the image is passed
  through preprocessing steps (cropping, flipping, and so on).

  Args:
    raw_record: scalar Tensor tf.string containing a serialized
      Example protocol buffer.
    is_training: A boolean denoting whether the input is for training.

  Returns:
    Tuple with processed image tensor and one-hot-encoded label tensor.
  """
    image_buffer, label, bbox = _parse_example_proto(raw_record)

    image = imagenet_preprocessing.preprocess_image(
        image_buffer=image_buffer,
        bbox=bbox,
        output_height=_DEFAULT_IMAGE_SIZE,
        output_width=_DEFAULT_IMAGE_SIZE,
        num_channels=_NUM_CHANNELS,
        is_training=is_training)

    label = tf.one_hot(tf.reshape(label, shape=[]), _NUM_CLASSES)

    return image, label
예제 #3
0
def parse_record(
    raw_record,
    is_training,
    dtype,
    data_format="channels_last",
    image_size=defaults.DEFAULT_IMAGE_SIZE,
    num_channels=defaults.NUM_CHANNELS,
):
    """Parses a record containing a training example of an image.
    The input record is parsed into a label and image, and the image is passed
    through preprocessing steps (cropping, flipping, and so on).

    Args:
        raw_record: scalar Tensor tf.string containing a serialized
          Example protocol buffer.
        is_training: A boolean denoting whether the input is for training.
        dtype: data type to use for images/features.
        data_format: the axis order of the matrix, channels_last NHWC or channels_first NCHW

    Returns:
        Tuple with processed image tensor and one-hot-encoded label tensor.
    """
    image_buffer, label = _parse_example_proto(raw_record)

    image = imagenet_preprocessing.preprocess_image(
        image_buffer=image_buffer,
        output_height=image_size,
        output_width=image_size,
        num_channels=num_channels,
        is_training=is_training,
        data_format=data_format,
    )
    image = tf.cast(image, dtype)

    return image, label
예제 #4
0
def get_sample(input_img_filename, input_xml_filename, coder=None):
    if coder is None:
        coder = ImageCoder()
    image_buffer, height, width = _process_image(input_img_filename, coder)

    boxes = ProcessXMLAnnotation(input_xml_filename)
    # print(boxes[0].xmin, boxes[0].ymin, boxes[0].xmax, boxes[0].ymax)
    # print(boxes[0].xmin_scaled, boxes[0].ymin_scaled, boxes[0].xmax_scaled, boxes[0].ymax_scaled)

    xmin = tf.expand_dims(boxes[0].xmin_scaled, 0)
    ymin = tf.expand_dims(boxes[0].ymin_scaled, 0)
    xmax = tf.expand_dims(boxes[0].xmax_scaled, 0)
    ymax = tf.expand_dims(boxes[0].ymax_scaled, 0)

    # Note that we impose an ordering of (y, x) just to make life difficult.
    bbox = tf.concat([ymin, xmin, ymax, xmax], 0)

    # Force the variable number of bounding boxes into the shape
    # [1, num_boxes, coords].
    bbox = tf.expand_dims(bbox, 0)
    bbox = tf.expand_dims(bbox, 0)
    # bbox = tf.transpose(a=bbox, perm=[0, 2, 1])

    image = imagenet_preprocessing.preprocess_image(
        image_buffer=image_buffer,
        bbox=bbox,
        output_height=DEFAULT_IMAGE_SIZE,
        output_width=DEFAULT_IMAGE_SIZE,
        num_channels=NUM_CHANNELS,
        is_training=False)

    return image
예제 #5
0
def preprocess_image(file_name,
                     output_height=224,
                     output_width=224,
                     num_channels=3):
    """Run standard ImageNet preprocessing on the passed image file.
    Args:
      file_name: string, path to file containing a JPEG image
      output_height: int, final height of image
      output_width: int, final width of image
      num_channels: int, depth of input image
    Returns:
      Float array representing processed image with shape
        [output_height, output_width, num_channels]
    Raises:
      ValueError: if image is not a JPEG.
    """
    if imghdr.what(file_name) != "jpeg":
        raise ValueError("At this time, only JPEG images are supported. "
                         "Please try another image.")

    image_buffer = tf.read_file(file_name)
    normalized = imagenet_preprocessing.preprocess_image(
        image_buffer=image_buffer,
        bbox=None,
        output_height=output_height,
        output_width=output_width,
        num_channels=num_channels,
        is_training=False)

    with tf.Session(config=get_gpu_config()) as sess:
        result = sess.run([normalized])
    #result = [normalized]

    return result[0]
예제 #6
0
def preprocess_image(file_name,
                     output_height=RESNET_IMAGE_SIZE,
                     output_width=RESNET_IMAGE_SIZE,
                     num_channels=3):
    """Run standard ImageNet preprocessing on the passed image file.
  Args:
    file_name: string, path to file containing a JPEG image
    output_height: int, final height of image
    output_width: int, final width of image
    num_channels: int, depth of input image
  Returns:
    Float array representing processed image with shape
      [output_height, output_width, num_channels]
  Raises:
    ValueError: if image is not a JPEG.
  """
    with tf.device('/cpu:0'):
        image_buffer = tf.read_file(file_name)
        normalized = imagenet_preprocessing.preprocess_image(
            image_buffer=image_buffer,
            bbox=None,
            output_height=output_height,
            output_width=output_width,
            num_channels=num_channels,
            is_training=False)

    with tf.Session() as sess:
        result = sess.run([normalized])

    return result[0]
예제 #7
0
def parse_record(raw_record, is_training, return_label_text=True, fixed_crop=False):
  """Parses a record containing a training example of an image.

  The input record is parsed into a label and image, and the image is passed
  through preprocessing steps (cropping, flipping, and so on).

  Args:
    raw_record: scalar Tensor tf.string containing a serialized
      Example protocol buffer.
    is_training: A boolean denoting whether the input is for training.

  Returns:
    Tuple with processed image tensor and one-hot-encoded label tensor.
  """
  image_buffer, label, bbox, label_text, filename = _parse_example_proto(raw_record)

  image = imagenet_preprocessing.preprocess_image(
      image_buffer=image_buffer,
      bbox=bbox,
      output_height=_DEFAULT_IMAGE_SIZE,
      output_width=_DEFAULT_IMAGE_SIZE,
      num_channels=_NUM_CHANNELS,
      is_training=is_training,
      fixed_crop=fixed_crop)

  if return_label_text:
    return image, label, label_text, filename
  else:
    return image, label
예제 #8
0
def parse_record(raw_record, is_training, dtype=tf.float32):
    image_buffer, label = _parse_example_proto(raw_record)
    image_buffer = imagenet_preprocessing.preprocess_image(
        image_buffer=image_buffer,
        output_height=224,
        output_width=224,
        num_channels=3,
        is_training=is_training)
    image = tf.cast(image_buffer, dtype)
    return image, label
예제 #9
0
 def _preprocess_image(image_bytes):
     """Preprocess a single raw image."""
     # Bounding box around the whole image.
     bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=dtype, shape=[1, 1, 4])
     height, width, num_channels = image_shape
     image = imagenet_preprocessing.preprocess_image(image_bytes,
                                                     bbox,
                                                     height,
                                                     width,
                                                     num_channels,
                                                     is_training=False)
     return image
예제 #10
0
def parse_record(raw_record, is_training):
    image_buffer, label, bbox = _parse_example_proto(raw_record)

    image = imagenet_preprocessing.preprocess_image(
        image_buffer=image_buffer,
        bbox=bbox,
        output_height=_DEFAULT_IMAGE_SIZE,
        output_width=_DEFAULT_IMAGE_SIZE,
        num_channels=_NUM_CHANNELS,
        is_training=is_training)
    image = tf.cast(image, tf.float32)
    label = tf.one_hot(tf.reshape(label, []), _NUM_CLASSES)
    return image, label
예제 #11
0
    def parse_record(raw_record, is_training, dtype):

        image_buffer, label, bbox = _parse_example_proto(raw_record)

        image = preprocess_image(
            image_buffer=image_buffer,
            bbox=bbox,
            output_height=DEFAULT_IMAGE_SIZE,
            output_width=DEFAULT_IMAGE_SIZE,
            num_channels=NUM_CHANNELS,
            is_training=is_training)
        image = tf.cast(image, dtype)

        return image, label