本文整理汇总了Python中tensorflow.string方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.string方法的具体用法?Python tensorflow.string怎么用?Python tensorflow.string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.string方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _mapper
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [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 string [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: testSimple
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def testSimple(self):
labels = [9, 3, 0]
records = [self._record(labels[0], 0, 128, 255),
self._record(labels[1], 255, 0, 1),
self._record(labels[2], 254, 255, 0)]
contents = b"".join([record for record, _ in records])
expected = [expected for _, expected in records]
filename = os.path.join(self.get_temp_dir(), "cifar")
open(filename, "wb").write(contents)
with self.test_session() as sess:
q = tf.FIFOQueue(99, [tf.string], shapes=())
q.enqueue([filename]).run()
q.close().run()
result = cifar10_input.read_cifar10(q)
for i in range(3):
key, label, uint8image = sess.run([
result.key, result.label, result.uint8image])
self.assertEqual("%s:%d" % (filename, i), tf.compat.as_text(key))
self.assertEqual(labels[i], label)
self.assertAllEqual(expected[i], uint8image)
with self.assertRaises(tf.errors.OutOfRangeError):
sess.run([result.key, result.uint8image])
示例4: _ImageProcessing
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def _ImageProcessing(image_buffer, shape):
"""Convert a PNG string into an input tensor.
We allow for fixed and variable sizes.
Does fixed conversion to floats in the range [-1.28, 1.27].
Args:
image_buffer: Tensor containing a PNG encoded image.
shape: ImageShape with the desired shape of the input.
Returns:
image: Decoded, normalized image in the range [-1.28, 1.27].
"""
image = tf.image.decode_png(image_buffer, channels=shape.depth)
image.set_shape([shape.height, shape.width, shape.depth])
image = tf.cast(image, tf.float32)
image = tf.subtract(image, 128.0)
image = tf.multiply(image, 1 / 100.0)
return image
示例5: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def __init__(self):
# Create a single Session to run all image coding calls.
self._sess = tf.Session()
# Initializes function that converts PNG to JPEG data.
self._png_data = tf.placeholder(dtype=tf.string)
image = tf.image.decode_png(self._png_data, channels=3)
self._png_to_jpeg = tf.image.encode_jpeg(image, format='rgb', quality=100)
# Initializes function that converts CMYK JPEG data to RGB JPEG data.
self._cmyk_data = tf.placeholder(dtype=tf.string)
image = tf.image.decode_jpeg(self._cmyk_data, channels=0)
self._cmyk_to_rgb = tf.image.encode_jpeg(image, format='rgb', quality=100)
# Initializes function that decodes RGB JPEG data.
self._decode_jpeg_data = tf.placeholder(dtype=tf.string)
self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3)
示例6: _find_human_readable_labels
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def _find_human_readable_labels(synsets, synset_to_human):
"""Build a list of human-readable labels.
Args:
synsets: list of strings; each string is a unique WordNet ID.
synset_to_human: dict of synset to human labels, e.g.,
'n02119022' --> 'red fox, Vulpes vulpes'
Returns:
List of human-readable strings corresponding to each synset.
"""
humans = []
for s in synsets:
assert s in synset_to_human, ('Failed to find: %s' % s)
humans.append(synset_to_human[s])
return humans
示例7: _find_image_bounding_boxes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def _find_image_bounding_boxes(filenames, image_to_bboxes):
"""Find the bounding boxes for a given image file.
Args:
filenames: list of strings; each string is a path to an image file.
image_to_bboxes: dictionary mapping image file names to a list of
bounding boxes. This list contains 0+ bounding boxes.
Returns:
List of bounding boxes for each image. Note that each entry in this
list might contain from 0+ entries corresponding to the number of bounding
box annotations for the image.
"""
num_image_bbox = 0
bboxes = []
for f in filenames:
basename = os.path.basename(f)
if basename in image_to_bboxes:
bboxes.append(image_to_bboxes[basename])
num_image_bbox += 1
else:
bboxes.append([])
print('Found %d images with bboxes out of %d images' % (
num_image_bbox, len(filenames)))
return bboxes
示例8: _process_dataset
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def _process_dataset(name, directory, num_shards, synset_to_human,
image_to_bboxes):
"""Process a complete data set and save it as a TFRecord.
Args:
name: string, unique identifier specifying the data set.
directory: string, root path to the data set.
num_shards: integer number of shards for this data set.
synset_to_human: dict of synset to human labels, e.g.,
'n02119022' --> 'red fox, Vulpes vulpes'
image_to_bboxes: dictionary mapping image file names to a list of
bounding boxes. This list contains 0+ bounding boxes.
"""
filenames, synsets, labels = _find_image_files(directory, FLAGS.labels_file)
humans = _find_human_readable_labels(synsets, synset_to_human)
bboxes = _find_image_bounding_boxes(filenames, image_to_bboxes)
_process_image_files(name, filenames, synsets, labels,
humans, bboxes, num_shards)
示例9: decode_jpeg
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def decode_jpeg(image_buffer, scope=None):
"""Decode a JPEG string into one 3-D float image Tensor.
Args:
image_buffer: scalar string Tensor.
scope: Optional scope for name_scope.
Returns:
3-D float Tensor with values ranging from [0, 1).
"""
with tf.name_scope(values=[image_buffer], name=scope,
default_name='decode_jpeg'):
# Decode the string as an RGB JPEG.
# Note that the resulting image contains an unknown height and width
# that is set dynamically by decode_jpeg. In other words, the height
# and width of image is unknown at compile-time.
image = tf.image.decode_jpeg(image_buffer, channels=3)
# After this point, all image pixels reside in [0,1)
# until the very end, when they're rescaled to (-1, 1). The various
# adjust_* ops all require this range for dtype float.
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
return image
示例10: read_and_decode
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [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
示例11: read_and_decode_aug
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [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
示例12: example_reading_spec
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def example_reading_spec(self):
extra_data_fields, extra_data_items_to_decoders = self.extra_reading_spec
data_fields = {
"image/encoded": tf.FixedLenFeature((), tf.string),
"image/format": tf.FixedLenFeature((), tf.string),
}
data_fields.update(extra_data_fields)
data_items_to_decoders = {
"frame":
tf.contrib.slim.tfexample_decoder.Image(
image_key="image/encoded",
format_key="image/format",
shape=[self.frame_height, self.frame_width, self.num_channels],
channels=self.num_channels),
}
data_items_to_decoders.update(extra_data_items_to_decoders)
return data_fields, data_items_to_decoders
示例13: serving_input_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def serving_input_fn(self, hparams):
"""Input fn for serving export, starting from serialized example."""
mode = tf.estimator.ModeKeys.PREDICT
serialized_example = tf.placeholder(
dtype=tf.string, shape=[None], name="serialized_example")
dataset = tf.data.Dataset.from_tensor_slices(serialized_example)
dataset = dataset.map(self.decode_example)
dataset = dataset.map(lambda ex: self.preprocess_example(ex, mode, hparams))
dataset = dataset.map(self.maybe_reverse_and_copy)
dataset = dataset.map(data_reader.cast_ints_to_int32)
dataset = dataset.padded_batch(
tf.shape(serialized_example, out_type=tf.int64)[0],
dataset.output_shapes)
dataset = dataset.map(standardize_shapes)
features = tf.contrib.data.get_single_element(dataset)
if self.has_inputs:
features.pop("targets", None)
return tf.estimator.export.ServingInputReceiver(
features=features, receiver_tensors=serialized_example)
示例14: parse_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [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
示例15: _tf_example_input_placeholder
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import string [as 别名]
def _tf_example_input_placeholder():
"""Returns input that accepts a batch of strings with tf examples.
Returns:
a tuple of input placeholder and the output decoded images.
"""
batch_tf_example_placeholder = tf.placeholder(
tf.string, shape=[None], name='tf_example')
def decode(tf_example_string_tensor):
tensor_dict = tf_example_decoder.TfExampleDecoder().decode(
tf_example_string_tensor)
image_tensor = tensor_dict[fields.InputDataFields.image]
return image_tensor
return (batch_tf_example_placeholder,
shape_utils.static_or_dynamic_map_fn(
decode,
elems=batch_tf_example_placeholder,
dtype=tf.uint8,
parallel_iterations=32,
back_prop=False))