本文整理汇总了Python中tensorflow.parse_single_example方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.parse_single_example方法的具体用法?Python tensorflow.parse_single_example怎么用?Python tensorflow.parse_single_example使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.parse_single_example方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _mapper
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def _mapper(example_proto):
features = {
'samples': tf.FixedLenSequenceFeature([1], tf.float32, allow_missing=True),
'label': tf.FixedLenSequenceFeature([], tf.string, allow_missing=True)
}
example = tf.parse_single_example(example_proto, features)
wav = example['samples'][:, 0]
wav = wav[:16384]
wav_len = tf.shape(wav)[0]
wav = tf.pad(wav, [[0, 16384 - wav_len]])
label = tf.reduce_join(example['label'], 0)
return wav, label
示例2: read_from_tfrecord
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def read_from_tfrecord(filenames):
tfrecord_file_queue = tf.train.string_input_producer(filenames, name='queue')
reader = tf.TFRecordReader()
_, tfrecord_serialized = reader.read(tfrecord_file_queue)
tfrecord_features = tf.parse_single_example(tfrecord_serialized, features={
'label': tf.FixedLenFeature([],tf.int64),
'shape': tf.FixedLenFeature([],tf.string),
'image': tf.FixedLenFeature([],tf.string),
}, name='features')
image = tf.decode_raw(tfrecord_features['image'], tf.uint8)
shape = tf.decode_raw(tfrecord_features['shape'], tf.int32)
image = tf.reshape(image, shape)
label = tfrecord_features['label']
return label, shape, image
示例3: read_and_decode
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def read_and_decode(filename_queue):
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),
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.reshape(image, [227, 227, 6])
# Convert from [0, 255] -> [-0.5, 0.5] floats.
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
return tf.split(image, 2, 2) # 3rd dimension two parts
示例4: read_and_decode_aug
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def read_and_decode_aug(filename_queue):
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),
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.image.random_flip_left_right(tf.reshape(image, [227, 227, 6]))
# Convert from [0, 255] -> [-0.5, 0.5] floats.
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
image = tf.image.random_brightness(image, 0.01)
image = tf.image.random_contrast(image, 0.95, 1.05)
return tf.split(image, 2, 2) # 3rd dimension two parts
示例5: parse_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def parse_fn(self, serialized_example):
features={
'image/id_name': tf.FixedLenFeature([], tf.string),
'image/height' : tf.FixedLenFeature([], tf.int64),
'image/width' : tf.FixedLenFeature([], tf.int64),
'image/encoded': tf.FixedLenFeature([], tf.string),
}
for name in self.feature_list:
features[name] = tf.FixedLenFeature([], tf.int64)
example = tf.parse_single_example(serialized_example, features=features)
image = tf.decode_raw(example['image/encoded'], tf.uint8)
raw_height = tf.cast(example['image/height'], tf.int32)
raw_width = tf.cast(example['image/width'], tf.int32)
image = tf.reshape(image, [raw_height, raw_width, 3])
image = tf.image.resize_images(image, size=[self.height, self.width])
# from IPython import embed; embed(); exit()
feature_val_list = [tf.cast(example[name], tf.float32) for name in self.feature_list]
return image, feature_val_list
示例6: parse_fun
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def parse_fun(serialized_example):
""" Data parsing function.
"""
features = tf.parse_single_example(serialized_example,
features={'image': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'depth': tf.FixedLenFeature([], tf.int64)})
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
depth = tf.cast(features['depth'], tf.int32)
image = tf.decode_raw(features['image'], tf.float32)
image = tf.reshape(image, shape=[height * width * depth])
image.set_shape([28 * 28 * 1])
image = tf.cast(image, tf.float32) * (1. / 255)
label = tf.cast(features['label'], tf.int32)
features = {'images': image, 'labels': label}
return(features)
示例7: parse_color_data
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def parse_color_data(example_proto):
features = {"img_raw": tf.FixedLenFeature([], tf.string),
"label": tf.FixedLenFeature([], tf.string),
"width": tf.FixedLenFeature([], tf.int64),
"height": tf.FixedLenFeature([], tf.int64)}
parsed_features = tf.parse_single_example(example_proto, features)
img = parsed_features["img_raw"]
img = tf.decode_raw(img, tf.uint8)
width = parsed_features["width"]
height = parsed_features["height"]
img = tf.reshape(img, [height, width, 3])
img = tf.cast(img, tf.float32) * (1. / 255.) - 0.5
label = parsed_features["label"]
label = tf.decode_raw(label, tf.float32)
return img, label
示例8: _read_raw
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def _read_raw(self):
"""Read raw data from TFRecord.
Returns:
:return: data list [input_raw, label_raw].
"""
self._reader = tf.TFRecordReader()
_, serialized_example = self._reader.read(self._queue)
features = tf.parse_single_example(serialized_example,
features={
'name': tf.FixedLenFeature([], tf.string),
'block': tf.FixedLenFeature([], tf.string)
})
input_raw, label_raw = decode_block(features['block'], tensor_size=self._raw_size)
if self._with_key:
return input_raw, label_raw, features['name']
return input_raw, label_raw
示例9: read_and_decode
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape([784])
image = tf.cast(image, tf.float32) * (1. / 255)
label = tf.cast(features['label'], tf.int32)
return image, label
示例10: load_patch_coordinates_from_filename_queue
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def load_patch_coordinates_from_filename_queue(filename_queue):
"""Loads coordinates and volume names from filename queue.
Args:
filename_queue: Tensorflow queue created from create_filename_queue()
Returns:
Tuple of coordinates (shape `[1, 3]`) and volume name (shape `[1]`) tensors.
"""
record_options = tf.python_io.TFRecordOptions(
tf.python_io.TFRecordCompressionType.GZIP)
keys, protos = tf.TFRecordReader(options=record_options).read(filename_queue)
examples = tf.parse_single_example(protos, features=dict(
center=tf.FixedLenFeature(shape=[1, 3], dtype=tf.int64),
label_volume_name=tf.FixedLenFeature(shape=[1], dtype=tf.string),
))
coord = examples['center']
volname = examples['label_volume_name']
return coord, volname
示例11: read_and_decode
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example, features = {
"image/encoded": tf.FixedLenFeature([], tf.string),
"image/height": tf.FixedLenFeature([], tf.int64),
"image/width": tf.FixedLenFeature([], tf.int64),
"image/filename": tf.FixedLenFeature([], tf.string),
"image/class/label": tf.FixedLenFeature([], tf.int64),})
image_encoded = features["image/encoded"]
image_raw = tf.image.decode_jpeg(image_encoded, channels=3)
current_image_object = image_object()
current_image_object.image = tf.image.resize_image_with_crop_or_pad(image_raw, FLAGS.image_height, FLAGS.image_width) # cropped image with size 299x299
# current_image_object.image = tf.cast(image_crop, tf.float32) * (1./255) - 0.5
current_image_object.height = features["image/height"] # height of the raw image
current_image_object.width = features["image/width"] # width of the raw image
current_image_object.filename = features["image/filename"] # filename of the raw image
current_image_object.label = tf.cast(features["image/class/label"], tf.int32) # label of the raw image
return current_image_object
示例12: dataset_parser
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def dataset_parser(value):
keys_to_features = {
'image/encoded': tf.FixedLenFeature((), tf.string, ''),
'image/format': tf.FixedLenFeature((), tf.string, 'jpeg'),
'image/class/label': tf.FixedLenFeature([], tf.int64, -1)
}
parsed = tf.parse_single_example(value, keys_to_features)
image_bytes = tf.reshape(parsed['image/encoded'], shape=[])
# Preprocess the images.
image = tf.image.decode_jpeg(image_bytes)
image = tf.image.random_flip_left_right(image)
image = tf.image.resize_images(image, [IMAGE_SIZE, IMAGE_SIZE])
image = tf.image.convert_image_dtype(
image, dtype=tf.bfloat16)
# Subtract one so that labels are in [0, 1000).
label = tf.cast(
tf.reshape(parsed['image/class/label'], shape=[]), dtype=tf.int32) - 1
return image, label
示例13: decode_pred
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def decode_pred(serialized_example):
"""Parses prediction data from the given `serialized_example`."""
features = tf.parse_single_example(
serialized_example,
features={
'T1':tf.FixedLenFeature([],tf.string),
'T2':tf.FixedLenFeature([], tf.string)
})
patch_shape = [conf.patch_size, conf.patch_size, conf.patch_size]
# Convert from a scalar string tensor
image_T1 = tf.decode_raw(features['T1'], tf.int16)
image_T1 = tf.reshape(image_T1, patch_shape)
image_T2 = tf.decode_raw(features['T2'], tf.int16)
image_T2 = tf.reshape(image_T2, patch_shape)
# Convert dtype.
image_T1 = tf.cast(image_T1, tf.float32)
image_T2 = tf.cast(image_T2, tf.float32)
label = tf.zeros(image_T1.shape) # pseudo label
return image_T1, image_T2, label
示例14: test_parse_spec
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def test_parse_spec():
fc = FeatureColumns(
True,
False,
VOCAB_FILE,
VOCAB_SIZE,
10,
10,
1000,
10)
parse_spec = tf.feature_column.make_parse_example_spec(fc)
print parse_spec
reader = tf.python_io.tf_record_iterator(INPUT_FILE)
sess = tf.Session()
for record in reader:
example = tf.parse_single_example(
record,
parse_spec)
print sess.run(example)
break
示例15: parse_tfrecord_tf
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import parse_single_example [as 别名]
def parse_tfrecord_tf(record, res, rnd_crop):
features = tf.parse_single_example(record, features={
'shape': tf.FixedLenFeature([3], tf.int64),
'data': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([1], tf.int64)})
# label is always 0 if uncondtional
# to get CelebA attr, add 'attr': tf.FixedLenFeature([40], tf.int64)
data, label, shape = features['data'], features['label'], features['shape']
label = tf.cast(tf.reshape(label, shape=[]), dtype=tf.int32)
img = tf.decode_raw(data, tf.uint8)
if rnd_crop:
# For LSUN Realnvp only - random crop
img = tf.reshape(img, shape)
img = tf.random_crop(img, [res, res, 3])
img = tf.reshape(img, [res, res, 3])
return img, label # to get CelebA attr, also return attr