Esempio n. 1
0
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.
  """
  #print(" I am here in parse_record function in imagenet_main")
  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)
  #print(" I am here in parse_record function in imagenet_main, image and label created")
  return image, label
Esempio n. 2
0
def parse_record(raw_record, is_training, dtype):
    """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
Esempio n. 3
0
 def preprocess(self, image_buffer, bbox, batch_position):
   # pylint: disable=g-import-not-at-top
   try:
     from official.resnet.imagenet_preprocessing import preprocess_image
   except ImportError:
     tf.logging.fatal('Please include tensorflow/models to the PYTHONPATH.')
     raise
   if self.train:
     image = preprocess_image(
         image_buffer, bbox, self.height, self.width, self.depth,
         is_training=True)
   else:
     image = preprocess_image(
         image_buffer, bbox, self.height, self.width, self.depth,
         is_training=False)
   return tf.cast(image, self.dtype)
Esempio n. 4
0
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)

  return image, label
Esempio n. 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])

  return result[0]
Esempio n. 6
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])

  return result[0]
Esempio n. 7
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
Esempio n. 8
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
Esempio n. 9
0
def _parse_function(example_proto, output_height, output_width, is_training=False,
                    resize_side_min=_RESIZE_SIDE_MIN):
    """Parse TF Records and preprocess image and labels
    """
    
    # parse the TFRecord
    feature_map = {
        "image/encoded": tf.FixedLenFeature((), tf.string, default_value=""),
        "image/format": tf.FixedLenFeature((), tf.string, default_value=""),
        "image/class/label": tf.VarLenFeature(tf.int64),
        "image/height": tf.FixedLenFeature((), tf.int64, default_value=0),
        "image/width": tf.FixedLenFeature((), tf.int64, default_value=0),
        "image/object/bbox/xmin": tf.VarLenFeature(tf.float32),
        "image/object/bbox/ymin": tf.VarLenFeature(tf.float32),
        "image/object/bbox/xmax": tf.VarLenFeature(tf.float32),
        "image/object/bbox/ymax": tf.VarLenFeature(tf.float32),
    }
    features = tf.parse_single_example(example_proto, feature_map)
    
    # parse bounding box
    xmin = tf.expand_dims(features['image/object/bbox/xmin'].values, 0)
    ymin = tf.expand_dims(features['image/object/bbox/ymin'].values, 0)
    xmax = tf.expand_dims(features['image/object/bbox/xmax'].values, 0)
    ymax = tf.expand_dims(features['image/object/bbox/ymax'].values, 0)
    
    bbox = tf.concat([ymin, xmin, ymax, xmax], 0)
    bbox = tf.expand_dims(bbox, 0)
    bbox = tf.transpose(bbox, [0, 2, 1])

    # ImageNet preprocessing 
    from official.resnet import imagenet_preprocessing
    image = imagenet_preprocessing.preprocess_image(
        image_buffer=features["image/encoded"],
        bbox=bbox,
        output_height=output_height,
        output_width=output_width,
        num_channels=_NUM_CHANNELS,
        is_training=is_training)
    
    # convert labels to dense
    labels = tf.sparse_tensor_to_dense(features["image/class/label"])
    
    return image, labels