Пример #1
0
def parse_example_proto(example_serialized):
    # Dense features in Example proto.
    feature_map = {
        'image/encoded':
        tf.FixedLenFeature([], dtype=tf.string, default_value=''),
        'image/class/label':
        tf.FixedLenFeature([1], dtype=tf.int64, default_value=-1),
    }

    features = tf.parse_single_example(example_serialized, feature_map)
    label = tf.cast(features['image/class/label'], dtype=tf.int32)

    return features['image/encoded'], label
Пример #2
0
def _parse_function(example_proto):
    features = {
        "image": tf.FixedLenFeature((), tf.string, default_value=""),
        "label": tf.FixedLenFeature((), tf.int64, default_value=0)
    }
    parsed_features = tf.parse_single_example(example_proto, features)
    images = parsed_features["image"]
    images = tf.decode_raw(images, tf.uint8)
    # channel first
    images = tf.reshape(images, [3, 32, 32])
    images = tf.cast(images, tf.float32)
    images = (images - 127) / 128.0 * 4
    return images, parsed_features["label"]
Пример #3
0
 def _full_tfrecord_parser(serialized_example):
   """Parses a tf.Example into (image, label index, bottleneck) Tensors."""
   features = {
       'image_path':
           tf.FixedLenFeature((), tf.string),
       'label':
           tf.FixedLenFeature((), tf.string),
       'bottleneck':
           tf.FixedLenFeature([INCEPTION_V3_BOTTLENECK_SIZE], tf.float32),
   }
   example = tf.parse_single_example(serialized_example, features=features)
   label_index = label_table.lookup(example['label'])
   return example['image_path'], label_index, example['bottleneck']
Пример #4
0
 def read_fn(example):
     features = {
         "image": tf.FixedLenFeature([], tf.string),
         "caption": tf.VarLenFeature(tf.int64),
     }
     example = tf.parse_single_example(example, features)
     label = tf.sparse.to_dense(example["caption"],
                                example["caption"].dense_shape[0])
     image = decode_img(example["image"], params["dataset"]["image_size"],
                        params["n_channels"])
     label = truncate_or_pad_label(label, params)
     label = tf.cast(label, tf.int32)
     return image, label  # returns a dataset of (image, label) pairs
Пример #5
0
def preprocess_example(example_proto, hparams, is_training):
    """Process an Example proto into input tensors."""
    features = {
        'id': tf.FixedLenFeature(shape=(), dtype=tf.string),
        'sequence': tf.FixedLenFeature(shape=(), dtype=tf.string),
        'audio': tf.FixedLenFeature(shape=(), dtype=tf.string),
        'velocity_range': tf.FixedLenFeature(shape=(), dtype=tf.string),
    }
    record = tf.parse_single_example(example_proto, features)
    input_tensors = preprocess_data(record['id'], record['sequence'],
                                    record['audio'], record['velocity_range'],
                                    hparams, is_training)
    return input_tensors
Пример #6
0
    def _decode_record(record, name_to_features):
        """Decodes a record to a TensorFlow example."""
        example = tf.parse_single_example(record, name_to_features)

        # tf.Example only supports tf.int64, but the TPU only supports tf.int32.
        # So cast all int64 to int32.
        for name in list(example.keys()):
            t = example[name]
            if t.dtype == tf.int64:
                t = tf.to_int32(t)
            example[name] = t

        return example
Пример #7
0
  def dataset_parser(self, value):
    """Parse an ImageNet record from a serialized string Tensor."""
    keys_to_features = {
        'image/encoded':
            tf.FixedLenFeature((), tf.string, ''),
        'image/format':
            tf.FixedLenFeature((), tf.string, 'jpeg'),
        'image/class/label':
            tf.FixedLenFeature([], tf.int64, -1),
        'image/class/text':
            tf.FixedLenFeature([], tf.string, ''),
        'image/object/bbox/xmin':
            tf.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/ymin':
            tf.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/xmax':
            tf.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/ymax':
            tf.VarLenFeature(dtype=tf.float32),
        'image/object/class/label':
            tf.VarLenFeature(dtype=tf.int64),
    }

    parsed = tf.parse_single_example(value, keys_to_features)
    image_bytes = tf.reshape(parsed['image/encoded'], shape=[])

    image = self.image_preprocessing_fn(
        image_bytes=image_bytes,
        is_training=self.is_training,
        use_bfloat16=self.use_bfloat16,
        image_size=self.image_size,
        resize_method=self.resize_method)

    # Subtract one so that labels are in [0, 1000), and cast to float32 for
    # Keras model.
    if self.one_hot:
      # TODO(ywenxu): The number of classes is hard coded for now.
      label = tf.cast(parsed['image/class/label'], tf.int32) - 1
      label = tf.one_hot(label, 1000, dtype=tf.float32)
    else:
      label = tf.cast(tf.reshape(
          parsed['image/class/label'], shape=[1]), dtype=tf.int32) - 1
      label = tf.cast(label, tf.float32)

    if self.normalize_input:
      mean = np.reshape(IMAGENET_MEAN, [1, 1, 3])
      stddev = np.reshape(IMAGENET_STDDEV, [1, 1, 3])
      image = (tf.cast(image, tf.float32) - mean) / stddev
      if self.use_bfloat16:
        image = tf.cast(image, tf.bfloat16)
    return image, label
Пример #8
0
def parse_tfexample_pm_v1(example_proto, action_size=None):
    """Parse TFExamples saved by episode_to_transitions_pm with all metrics.

  Args:
    example_proto: tf.String tensor representing a serialized protobuf.
    action_size: Size of continuous actions. If None, actions are assumed to be
      integer-encoded discrete actions.

  Returns:
    NamedTuple of type SARSTransition containing unbatched Tensors.
  """
    if action_size is None:
        # Is discrete.
        action_feature_spec = tf.FixedLenFeature((), tf.int64)
    else:
        # Vector-encoded float feature.
        action_feature_spec = tf.FixedLenFeature((action_size, ), tf.float32)

    features = {
        'A': action_feature_spec,
        'R': tf.FixedLenFeature((), tf.float32),
        'done': tf.FixedLenFeature((), tf.int64),
        't': tf.FixedLenFeature((), tf.int64)
    }

    for key in ['x', 'v', 'vtg', 'vtg_gt', 'prev_a', 'ae_steps', 'as_steps']:
        features[key] = tf.FixedLenFeature((), tf.float32)
        features[key + '_tp1'] = tf.FixedLenFeature((), tf.float32)

    parsed_features = tf.parse_single_example(example_proto, features)
    state_t = [parsed_features['x'], parsed_features['v']]
    state_tp1 = [parsed_features['x_tp1'], parsed_features['v_tp1']]
    vtg_t = parsed_features['vtg']
    vtg_tp1 = parsed_features['vtg_tp1']
    vtg_gt_t = parsed_features['vtg_gt']
    vtg_gt_tp1 = parsed_features['vtg_gt_tp1']
    prev_a_t = parsed_features['prev_a']
    prev_a_tp1 = parsed_features['prev_a_tp1']
    ae_steps_t = parsed_features['ae_steps']
    ae_steps_tp1 = parsed_features['ae_steps_tp1']
    as_steps_t = parsed_features['as_steps']
    as_steps_tp1 = parsed_features['as_steps_tp1']
    action = parsed_features['A']
    reward = parsed_features['R']
    done = tf.cast(parsed_features['done'], tf.float32)
    step = tf.cast(parsed_features['t'], tf.int32)
    aux = {'step': step}
    return SARSTransition(
        (state_t, step, vtg_t, vtg_gt_t, prev_a_t, ae_steps_t, as_steps_t),
        action, reward, (state_tp1, step + 1, vtg_tp1, vtg_gt_tp1, prev_a_tp1,
                         ae_steps_tp1, as_steps_tp1), done, aux)
Пример #9
0
    def dataset_parser(self, serialized_proto):
        """Parse an Imagenet record from value."""
        keys_to_features = {
            'image/encoded':
            tf.FixedLenFeature((), tf.string, default_value=''),
            'image/format':
            tf.FixedLenFeature((), tf.string, default_value='jpeg'),
            'image/class/label':
            tf.FixedLenFeature([], dtype=tf.int64, default_value=-1),
            'image/class/text':
            tf.FixedLenFeature([], dtype=tf.string, default_value=''),
            'image/object/bbox/xmin':
            tf.VarLenFeature(dtype=tf.float32),
            'image/object/bbox/ymin':
            tf.VarLenFeature(dtype=tf.float32),
            'image/object/bbox/xmax':
            tf.VarLenFeature(dtype=tf.float32),
            'image/object/bbox/ymax':
            tf.VarLenFeature(dtype=tf.float32),
            'image/object/class/label':
            tf.VarLenFeature(dtype=tf.int64),
        }

        features = tf.parse_single_example(serialized_proto, keys_to_features)

        bbox = None
        if FLAGS.use_annotated_bbox:
            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)

            # 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.transpose(bbox, [0, 2, 1])

        image = features['image/encoded']
        image = preprocess_raw_bytes(image,
                                     is_training=self.is_training,
                                     bbox=bbox)
        label = tf.cast(tf.reshape(features['image/class/label'], shape=[]),
                        dtype=tf.int32)

        if self.use_bfloat16:
            image = tf.cast(image, tf.bfloat16)

        return image, label
Пример #10
0
def read_bd_rm_record(data_path, batch_size=1, size=512):
	feature = {'image': tf.FixedLenFeature(shape=(), dtype=tf.string),
				'boundary': tf.FixedLenFeature(shape=(), dtype=tf.string),
				'room': tf.FixedLenFeature(shape=(), dtype=tf.string),
				'door': tf.FixedLenFeature(shape=(), dtype=tf.string)}
	
	print("data_path=",data_path)

	# Create a list of filenames and pass it to a queue
	filename_queue = tf.train.string_input_producer([data_path], num_epochs=None, shuffle=False, capacity=batch_size*128)
	# filename_queue = tf.data.Dataset.from_tensor_slices([data_path]).shuffle(tf.shape([data_path], out_type=tf.int64)[0]).repeat(None)
	
	# Define a reader and read the next record
	reader = tf.TFRecordReader()
	_, serialized_example = reader.read(filename_queue)

	# Decode the record read by the reader
	features = tf.parse_single_example(serialized_example, features=feature)

	# Convert the image data from string back to the numbers
	image = tf.decode_raw(features['image'], tf.uint8)
	boundary = tf.decode_raw(features['boundary'], tf.uint8)
	room = tf.decode_raw(features['room'], tf.uint8)
	door = tf.decode_raw(features['door'], tf.uint8)

	# Cast data
	image = tf.cast(image, dtype=tf.float32)

	# Reshape image data into the original shape
	image = tf.reshape(image, [size, size, 3])
	boundary = tf.reshape(boundary, [size, size])
	room = tf.reshape(room, [size, size])
	door = tf.reshape(door, [size, size])

	# Any preprocessing here ...
	# normalize 
	image = tf.divide(image, tf.constant(255.0))

	# Genereate one hot room label
	label_boundary = tf.one_hot(boundary, 3, axis=-1)
	label_room = tf.one_hot(room, 9, axis=-1)

	# Creates batches by randomly shuffling tensors
	images, label_boundaries, label_rooms, label_doors = tf.train.shuffle_batch([image, label_boundary, label_room, door], 
						batch_size=batch_size, capacity=batch_size*128, num_threads=1, min_after_dequeue=batch_size*32)	

	# images, walls = tf.train.shuffle_batch([image, wall], 
						# batch_size=batch_size, capacity=batch_size*128, num_threads=1, min_after_dequeue=batch_size*32)	

	return {'images': images, 'label_boundaries': label_boundaries, 'label_rooms': label_rooms, 'label_doors': label_doors}
Пример #11
0
def parse_examples(serialized_example):
    """Make retrieval examples."""
    feature_spec = dict(input_ids=tf.FixedLenSequenceFeature([], tf.int64,
                                                             True),
                        key=tf.FixedLenSequenceFeature([], tf.int64, True))
    features = tf.parse_single_example(serialized_example, feature_spec)
    features = {k: tf.cast(v, tf.int32) for k, v in features.items()}
    block_ids, block_mask, block_segment_ids = pad_or_truncate_pair(
        token_ids=features["input_ids"], sequence_length=FLAGS.block_seq_len)
    key = tf.ensure_shape(features["key"], [1])
    return dict(block_ids=block_ids,
                block_mask=block_mask,
                block_segment_ids=block_segment_ids,
                key=key)
Пример #12
0
    def parser_tfrecords(serialized_example):
        """Parses a single tf.Example into image and label tensors."""
        data = {}
        if params['test_small_sample']:
            data['image_raw'] = serialized_example
            data['label'] = tf.constant(0, tf.int32)
            data['human_label'] = tf.constant('human_label', tf.string)
            data['key_'] = tf.constant('key', tf.string)
            return data
        else:
            features = tf.parse_single_example(
                serialized_example,
                features={
                    'key_':
                    tf.FixedLenFeature([], dtype=tf.string, default_value=''),
                    'image/encoded':
                    tf.FixedLenFeature((), tf.string, default_value=''),
                    'image/class/label':
                    tf.FixedLenFeature([1], dtype=tf.int64, default_value=-1),
                    'image/class/text':
                    tf.FixedLenFeature([], dtype=tf.string, default_value=''),
                })

            image_bytes = tf.reshape(features['image/encoded'], shape=[])
            label = tf.cast(tf.reshape(features['image/class/label'],
                                       shape=[]),
                            dtype=tf.int32) - 1
            image = tf.image.decode_jpeg(features['image/encoded'], 3)

            human_label = tf.cast(tf.reshape(features['image/class/text'],
                                             shape=[]),
                                  dtype=tf.string)
            if params['task'] == 'imagenet_training':
                # training is set to false in prediction mode
                image = preprocessing_helper.preprocess_image(image=image,
                                                              image_size=224,
                                                              is_training=True)
            else:
                # training is set to false in prediction mode
                image = preprocessing_helper.preprocess_image(
                    image=image, image_size=224, is_training=False)

            if params['task'] == 'pie_dataset_gen':
                data['image_raw'] = image_bytes
            else:
                data['image_raw'] = image
            data['label'] = label
            data['human_label'] = human_label
            data['key_'] = tf.reshape(features['key_'], shape=[])
            return data
Пример #13
0
def decode(value):
  """Decode serialized example into image and segmentation label."""
  keys_to_features = {
      'image/encoded':
          tf.FixedLenFeature((), tf.string, default_value=''),
      'image/height':
          tf.FixedLenFeature((), tf.int64, default_value=0),
      'image/width':
          tf.FixedLenFeature((), tf.int64, default_value=0),
      'image/segmentation/class/encoded':
          tf.FixedLenFeature((), tf.string, default_value='')
  }
  data = tf.parse_single_example(value, keys_to_features)
  return data
Пример #14
0
 def _parse_example(ex_ser):
     """Parse serialized Example containing Wikipedia article content."""
     features = {
         "url": tf.VarLenFeature(tf.string),
         "title": tf.VarLenFeature(tf.string),
         "section_titles": tf.VarLenFeature(tf.string),
         "section_texts": tf.VarLenFeature(tf.string),
     }
     ex = tf.parse_single_example(ex_ser, features)
     for k in ex.keys():
         ex[k] = ex[k].values
     ex["url"] = ex["url"][0]
     ex["title"] = ex["title"][0]
     return ex
Пример #15
0
def parse_tfexample_sequence(example_proto,
                             img_height=48,
                             img_width=48,
                             action_size=None,
                             episode_length=16):
  """Parse TFExamples saved by episode_to_transitions.

  Args:
    example_proto: tf.String tensor representing a serialized protobuf.
    img_height: Height of parsed image tensors.
    img_width: Width of parsed image tensors.
    action_size: Size of continuous actions. If None, actions are assumed to be
      integer-encoded discrete actions.
    episode_length: Intended length of each episode.
  Returns:
    NamedTuple of type SARSTransition containing unbatched Tensors.
  """
  if action_size is None:
    # Is discrete.
    action_feature_spec = tf.FixedLenFeature((episode_length,), tf.int64)
  else:
    # Vector-encoded float feature.
    action_feature_spec = tf.FixedLenFeature((episode_length, action_size),
                                             tf.float32)

  features = {'S/img': tf.FixedLenFeature((episode_length,), tf.string),
              'A': action_feature_spec,
              'R': tf.FixedLenFeature((episode_length,), tf.float32),
              'S_p1/img': tf.FixedLenFeature((episode_length,), tf.string),
              'done': tf.FixedLenFeature((episode_length,), tf.int64),
              't': tf.FixedLenFeature((episode_length,), tf.int64)}
  parsed_features = tf.parse_single_example(example_proto, features)
  # Decode the jpeg-encoded images into numeric tensors.
  states = []
  for key in 'S/img', 'S_p1/img':
    state = tf.stack(
        [tf.image.decode_jpeg(img, channels=3)
         for img in tf.unstack(parsed_features[key], num=episode_length)])
    state.set_shape([episode_length, img_height, img_width, 3])
    states.append(tf.cast(state, tf.float32))

  action = parsed_features['A']
  reward = parsed_features['R']
  done = tf.cast(parsed_features['done'], tf.float32)
  step = tf.cast(parsed_features['t'], tf.int32)
  aux = {'step': step}

  return SARSTransition(
      (states[0], step), action, reward, (states[1], step + 1), done, aux)
Пример #16
0
    def parse(serialized_example):
      """Parses a single tensorflow example."""

      def parse_feature(features, key):
        features[key] = tf.FixedLenFeature([], tf.int64)
        return features

      data = tf.parse_single_example(serialized_example,
        features=reduce(parse_feature, FEATURE_KEYS + LABEL_KEYS, {}))

      features = [tf.convert_to_tensor(tf.cast(data[key], tf.int32))
        for key in FEATURE_KEYS]
      labels = [tf.convert_to_tensor(tf.cast(data[key], tf.int32))
        for key in LABEL_KEYS]
      return features, labels
Пример #17
0
def decode(tfrecord_serialized):
    tfrecord_features = tf.parse_single_example(
        tfrecord_serialized,
        features={
            'image/height': tf.FixedLenFeature([], tf.int64),
            'image/width': tf.FixedLenFeature([], tf.int64),
            'image/class/label': tf.FixedLenFeature([], tf.int64),
            'image/raw': tf.FixedLenFeature([], tf.string),
        },
        name='features')
    image = tf.decode_raw(tfrecord_features['image/raw'], tf.float32)
    image.set_shape([784])
    label = tf.cast(tfrecord_features['image/class/label'], tf.int32)
    #	image_batch, label_batch = tf.train.batch([image, label], batch_size=bs)
    return image, label
Пример #18
0
    def to_tensors(serialized_example):
        # change from proto to values
        features, feature_info = DetectorTrainer.feature_dict()
        features = tf.parse_single_example(serialized_example, features=features)
        for feature in features.keys():
            features[feature] = tf.decode_raw(features[feature], feature_info[feature])

        # reshape so images
        height, width = features['height'][0], features['width'][0]
        im_shape = tf.cast(tf.stack([height, width]), tf.int32)
        image = tf.reshape(features['image'], im_shape)
        segmentation = tf.reshape(features['segmentation'], im_shape)
        location = tf.reshape(features['location'], im_shape)

        return image, segmentation, location, height, width
Пример #19
0
 def label_dst_parser(value):
     keys_to_features = {
         'probabilities':
         tf.FixedLenFeature([FLAGS.num_label_classes], tf.float32),
         'classes':
         tf.FixedLenFeature([], tf.int64),
     }
     parsed = tf.parse_single_example(value, keys_to_features)
     features = {}
     features['probabilities'] = tf.cast(tf.reshape(
         parsed['probabilities'], shape=[FLAGS.num_label_classes]),
                                         dtype=tf.float32)
     features['classes'] = tf.cast(tf.reshape(parsed['classes'], shape=[]),
                                   dtype=tf.int32)
     return features
Пример #20
0
def _read_and_decode(filename_queue, image_pixel=28, distort=0):
    """Read tf records of MNIST images and labels."""
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized_example,
        # Defaults are not specified since both keys are required.
        features={
            'image_raw': tf.FixedLenFeature([], tf.string),
            'label': tf.FixedLenFeature([], tf.int64),
            'height': tf.FixedLenFeature([], tf.int64),
            'width': tf.FixedLenFeature([], tf.int64),
            'depth': tf.FixedLenFeature([], tf.int64)
        })

    # Convert from a scalar string tensor (whose single string has
    # length image_pixels) to a uint8 tensor with shape
    # [image_pixels].
    image = tf.decode_raw(features['image_raw'], tf.uint8)
    image = tf.reshape(image, [image_pixel, image_pixel, 1])
    print(image.get_shape()[0].value)
    image.set_shape([image_pixel, image_pixel, 1])

    # OPTIONAL: Could reshape into a 28x28 image and apply distortions
    # here.  Since we are not applying any distortions in this
    # example, and the next step expects the image to be flattened
    # into a vector, we don't bother.

    # Convert from [0, 255] -> [-0.5, 0.5] floats.
    image = tf.cast(image, tf.float32) * (1. / 255)
    if distort == 1:
        image = tf.reshape(image, [28, 28])
        image = tf.random_crop(image, [24, 24])
        # 0.26179938779 is 15 degress in radians
        # image = contrib_image.rotate(image,
        #                             random.uniform(-0.26179938779, 0.26179938779))
        image = tf.reshape(image, [24, 24, 1])
    elif distort == 2:
        image = tf.reshape(image, [28, 28])
        image = tf.expand_dims(image, 2)
        image = tf.image.central_crop(image, central_fraction=24 / 28)
        image = tf.squeeze(image, 2)
        image = tf.reshape(image, [24, 24, 1])

    # Convert label from a scalar uint8 tensor to an int32 scalar.
    label = tf.cast(features['label'], tf.int32)

    return image, label
Пример #21
0
  def dataset_parser(value):
    """Parses an image and its label from a serialized ResNet-50 TFExample."""
    parsed = tf.parse_single_example(
        value, {
            'image/encoded': tf.FixedLenFeature((), tf.string, ''),
            'image/class/label': tf.FixedLenFeature([], tf.int64, -1)
        })
    image_bytes = tf.reshape(parsed['image/encoded'], [])
    label = tf.cast(tf.reshape(parsed['image/class/label'], []), tf.int32) - 1

    def preprocess_fn():
      """Preprocess the image."""
      shape = tf.image.extract_jpeg_shape(image_bytes)
      if is_training:
        bbox = tf.constant([0.0, 0.0, 1.0, 1.0],
                           dtype=tf.float32,
                           shape=[1, 1, 4])
        bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
            tf.image.extract_jpeg_shape(image_bytes),
            bbox,
            min_object_covered=0.1,
            aspect_ratio_range=(0.75, 1.33),
            area_range=(0.05, 1.0),
            max_attempts=10,
            use_image_if_no_bounding_boxes=True)
        offset_y, offset_x, _ = tf.unstack(bbox_begin)
        target_height, target_width, _ = tf.unstack(bbox_size)
        crop_window = tf.stack(
            [offset_y, offset_x, target_height, target_width])
      else:
        crop_size = tf.cast(
            ((image_size / (image_size + CROP_PADDING)) *
             tf.cast(tf.minimum(shape[0], shape[1]), tf.float32)), tf.int32)
        offset_y, offset_x = [
            ((shape[i] - crop_size) + 1) // 2 for i in range(2)
        ]
        crop_window = tf.stack([offset_y, offset_x, crop_size, crop_size])

      image = tf.image.decode_and_crop_jpeg(
          image_bytes, crop_window, channels=3)
      image = tf.image.resize_bicubic([image], [image_size, image_size])[0]
      if is_training:
        image = tf.image.random_flip_left_right(image)
      image = tf.reshape(image, [image_size, image_size, 3])
      return tf.image.convert_image_dtype(image, dtype)

    empty_example = tf.zeros([image_size, image_size, 3], dtype)
    return tf.cond(label < 0, lambda: empty_example, preprocess_fn), label
Пример #22
0
def parser(serialized_example):
    """Parses a single tf.Example into image and label tensors."""
    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'image':
                                           tf.FixedLenFeature([], tf.string),
                                           'label':
                                           tf.FixedLenFeature([], tf.int64),
                                       })
    image = tf.decode_raw(features['image'], tf.uint8)
    image.set_shape([3 * 32 * 32])
    # Normalize the values of the image from the range [0, 255] to [-1.0, 1.0]
    image = tf.cast(image, tf.float32) * (2.0 / 255) - 1.0
    image = tf.transpose(tf.reshape(image, [3, 32 * 32]))
    label = tf.cast(features['label'], tf.int32)
    return image, label
Пример #23
0
    def _decode_record(record, name_to_features):
        """Decodes a record to a TensorFlow example."""
        example = tf.parse_single_example(record, name_to_features)

        # tf.Example only supports tf.int64, but the TPU only supports tf.int32.
        # So cast all int64 to int32.
        for name in list(example.keys()):
            t = example[name]
            if t.dtype == tf.int64:
                t = tf.to_int32(t)
            example[name] = t

        # Zero question because we are generating.
        example["input_ids"] = example["input_ids"] * example["segment_ids"]

        return example
Пример #24
0
    def parse_and_preprocess(self, example_proto):
        """
        Returns:
            image: a float tensor with shape [height, width, 3],
                an RGB image with pixel values in the range [0, 1].
            boxes: a float tensor with shape [num_boxes, 4].
            num_boxes: an int tensor with shape [].
        """
        features = {
            'image': tf.FixedLenFeature([], tf.string),
            'num_persons': tf.FixedLenFeature([], tf.int64),
            'boxes': tf.FixedLenSequenceFeature([],
                                                tf.float32,
                                                allow_missing=True)
        }
        parsed_features = tf.parse_single_example(example_proto, features)

        # get an image
        image = tf.image.decode_jpeg(parsed_features['image'], channels=3)
        image = tf.image.convert_image_dtype(image, tf.float32)
        # now pixel values are scaled to the [0, 1] range

        # get number of people on the image
        num_boxes = tf.to_int32(parsed_features['num_persons'])
        # it is assumed that num_boxes > 0

        # get groundtruth boxes, they are in absolute coordinates
        boxes = tf.reshape(parsed_features['boxes'], [num_boxes, 4])

        # to the [0, 1] range
        height, width = tf.shape(image)[0], tf.shape(image)[1]
        scaler = tf.to_float(tf.stack([height, width, height, width]))
        boxes /= scaler

        if self.is_training:
            image, boxes = augmentation(image, boxes, self.image_size)
        else:
            image, boxes = resize_keeping_aspect_ratio(image, boxes,
                                                       self.min_dimension,
                                                       DIVISOR)

        # it could change after augmentations
        num_boxes = tf.shape(boxes)[0]

        features = {'images': image}
        labels = {'boxes': boxes, 'num_boxes': num_boxes}
        return features, labels
Пример #25
0
        def _parse_nsynth(record):
            """Parsing function for NSynth dataset."""
            features = {
                'pitch': tf.FixedLenFeature([1], dtype=tf.int64),
                'audio': tf.FixedLenFeature([length], dtype=tf.float32),
                'qualities': tf.FixedLenFeature([10], dtype=tf.int64),
                'instrument_source': tf.FixedLenFeature([1], dtype=tf.int64),
                'instrument_family': tf.FixedLenFeature([1], dtype=tf.int64),
            }

            example = tf.parse_single_example(record, features)
            wave, label = example['audio'], example['pitch']
            wave = spectral_ops.crop_or_pad(wave[tf.newaxis, :, tf.newaxis],
                                            length, channels)[0]
            one_hot_label = tf.one_hot(label_index_table.lookup(label),
                                       depth=len(pitches))[0]
            return wave, one_hot_label, label, example['instrument_source']
Пример #26
0
def read_tfRecord(tfRecord_path):
    filename_queue = tf.train.string_input_producer([tfRecord_path],
                                                    shuffle=True)
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'label':
                                           tf.FixedLenFeature([10], tf.int64),
                                           'img_raw':
                                           tf.FixedLenFeature([], tf.string)
                                       })
    img = tf.decode_raw(features['img_raw'], tf.uint8)
    img = tf.reshape(img, [32, 32, 3])
    img = tf.cast(img, tf.float32) * (1.0 / 255)
    label = tf.cast(features['label'], tf.float32)
    return img, label
Пример #27
0
 def parser(serialized_example):
     """Parses a single tf.Example into image and label tensors."""
     features = tf.parse_single_example(serialized_example,
                                        features={
                                            "image":
                                            tf.FixedLenFeature([],
                                                               tf.string),
                                            "label":
                                            tf.FixedLenFeature([],
                                                               tf.int64),
                                        })
     image = tf.decode_raw(features["image"], tf.uint8)
     image.set_shape([3 * 32 * 32])
     image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
     image = tf.transpose(tf.reshape(image, [3, 32, 32]))
     label = tf.cast(features["label"], tf.int32)
     return image, label
Пример #28
0
    def parse_function(self, data):

        features = tf.parse_single_example(
            data,
            features={
                'image': tf.FixedLenFeature([], tf.string),
                'caption': tf.FixedLenFeature([self.config.max_length],
                                              tf.int64)
            })

        image = self.image_decode(features['image'])
        image = image_processing.image_processing(image, self.config.img_size,
                                                  self.is_training)
        caption = self.caption_processing(features['caption'],
                                          self.is_training)

        return image, caption
Пример #29
0
def parser(serialized_example):
    """Parses a single Example into image and label tensors."""
    features = tf.parse_single_example(
        serialized_example,
        features={
            'image_raw': tf.FixedLenFeature([], tf.string),
            'label': tf.FixedLenFeature([], tf.int64)  # label is unused
        })
    image = tf.decode_raw(features['image_raw'], tf.uint8)
    image.set_shape([28 * 28])
    image = tf.reshape(image, [28, 28, 1])

    # Normalize the values of the image from [0, 255] to [-1.0, 1.0]
    image = tf.cast(image, tf.float32) * (2.0 / 255) - 1.0

    label = tf.cast(tf.reshape(features['label'], shape=[]), dtype=tf.int32)
    return image, label
Пример #30
0
    def _decode_record(record, name_to_features):
        """Decodes a record to a TensorFlow example."""
        example = tf.parse_single_example(record, name_to_features)

        # Convert image to float tensor.
        image = example["image"]
        image_decoded = tf.image.decode_jpeg(image, channels=3)
        image_decoded.set_shape([None, None, 3])
        image_float = tf.image.convert_image_dtype(image_decoded, tf.float32)
        image_resized = tf.image.resize_image_with_pad(image_float, IMG_HEIGHT,
                                                       IMG_WIDTH)
        example["image"] = tf.reshape(image_resized,
                                      [IMG_HEIGHT, IMG_WIDTH, 3])

        # Get bboxes.
        if FLAGS.use_bboxes:
            example["bbox_pos"] = tf.to_int32(example["bbox_pos"])
            bboxes = []
            for idx in range(FLAGS.max_num_bboxes):
                bboxes.append(
                    parse_bounding_box(IMG_HEIGHT, IMG_WIDTH, image_float,
                                       example["bbox_pos"][idx, :]))
            example["bboxes"] = tf.stack(bboxes)

            if FLAGS.use_bbox_position:
                # Resized bboxes.
                y, x, bbox_height, bbox_width = tf.unstack(example["bbox_pos"],
                                                           axis=1)
                orig_height, orig_width = modeling.get_shape_list(
                    image_float)[:2]
                example["bbox_pos"] = tf.cast(tf.stack([
                    IMG_HEIGHT * y / orig_height, IMG_WIDTH * x / orig_width,
                    IMG_HEIGHT * bbox_height / orig_height,
                    IMG_WIDTH * bbox_width / orig_width
                ], 1),
                                              dtype=tf.int32)

        # tf.Example only supports tf.int64, but the TPU only supports tf.int32.
        # So cast all int64 to int32.
        for name in list(example.keys()):
            t = example[name]
            if t.dtype == tf.int64:
                t = tf.to_int32(t)
            example[name] = t

        return example