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
python类decode_raw()的实例源码
def read_decode_tfrecords(records_path, num_epochs=1020, batch_size=Flags.batch_size, num_threads=2):
if gfile.IsDirectory(records_path):
records_path = [os.path.join(records_path, i) for i in os.listdir(records_path)]
else:
records_path = [records_path]
records_path_queue = tf.train.string_input_producer(records_path, seed=123,
num_epochs=num_epochs,
name="string_input_producer")
reader = tf.TFRecordReader()
_, serialized_example = reader.read(records_path_queue, name="serialized_example")
features = tf.parse_single_example(serialized=serialized_example,
features={"img_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)},
name="parse_single_example")
image = tf.decode_raw(features["img_raw"], tf.uint8, name="decode_raw")
image.set_shape([height * width * 3])
image = tf.cast(image, tf.float32) * (1.0 / 255) - 0.5
label = tf.cast(features["label"], tf.int32)
images, labels = tf.train.shuffle_batch([image, label], batch_size=batch_size, num_threads=num_threads,
name="shuffle_bath", capacity=1020, min_after_dequeue=64)
print("images' shape is :", str(images.shape))
return images, labels
def read_decode_tfrecords(records_path, num_epochs=1, batch_size=Flags.batch_size, num_threads=1):
if gfile.IsDirectory(records_path):
records_path = [os.path.join(records_path, i) for i in os.listdir(records_path)]
else:
records_path = [records_path]
records_path_queue = tf.train.string_input_producer(records_path, seed=123,
num_epochs=None,
name="string_input_producer")
reader = tf.TFRecordReader()
_, serialized_example = reader.read(records_path_queue, name="serialized_example")
features = tf.parse_single_example(serialized=serialized_example,
features={"img_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)},
name="parse_single_example")
image = tf.decode_raw(features["img_raw"], tf.uint8, name="decode_raw")
image.set_shape([IMAGE_PIXELS])
image = tf.cast(image, tf.float32) * (1.0 / 255) - 0.5
label = tf.cast(features["label"], tf.int32)
images, labels = tf.train.shuffle_batch([image, label], batch_size=batch_size, num_threads=num_threads,
name="shuffle_bath", capacity=1020, min_after_dequeue=50)
return images, labels
def read_decode_tfrecords(records_path, num_epochs=1020, batch_size=Flags.batch_size, num_threads=2):
if gfile.IsDirectory(records_path):
records_path = [os.path.join(records_path, i) for i in os.listdir(records_path)]
else:
records_path = [records_path]
records_path_queue = tf.train.string_input_producer(records_path, seed=123,
# num_epochs=num_epochs,
name="string_input_producer")
reader = tf.TFRecordReader()
_, serialized_example = reader.read(records_path_queue, name="serialized_example")
features = tf.parse_single_example(serialized=serialized_example,
features={"img_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)},
name="parse_single_example")
image = tf.decode_raw(features["img_raw"], tf.uint8, name="decode_raw")
image.set_shape([IMAGE_PIXELS])
image = tf.cast(image, tf.float32) * (1.0 / 255) - 0.5
label = tf.cast(features["label"], tf.int32)
# images, labels = tf.train.shuffle_batch([image, label], batch_size=batch_size, num_threads=num_threads,
# name="shuffle_bath", capacity=1020, min_after_dequeue=64)
return image, label
def read_and_decode(filename):
# generate a queue with a given file name
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) # return the file and the name of file
features = tf.parse_single_example(serialized_example, # see parse_single_sequence_example for sequence example
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})
# You can do more image distortion here for training data
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [224, 224, 3])
# img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
return img, label
def read_and_decode(filename):
# generate a queue with a given file name
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) # return the file and the name of file
features = tf.parse_single_example(serialized_example, # see parse_single_sequence_example for sequence example
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})
# You can do more image distortion here for training data
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [224, 224, 3])
# img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
return img, label
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.VarLenFeature(tf.int64),
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.reshape(image, [730, 38])
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
return image, label
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.VarLenFeature(tf.int64),
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.reshape(image, [730, 38])
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
return image, label
def _read_example(filename_queue, n_labels=50, n_samples=59049):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'raw_labels': tf.FixedLenFeature([], tf.string),
'raw_segment': tf.FixedLenFeature([], tf.string)
})
segment = tf.decode_raw(features['raw_segment'], tf.float32)
segment.set_shape([n_samples])
labels = tf.decode_raw(features['raw_labels'], tf.uint8)
labels.set_shape([n_labels])
labels = tf.cast(labels, tf.float32)
return segment, labels
def read_and_decode(filename, one_hot=True, n_classes=None):
""" Return tensor to read from TFRecord """
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([],
tf.int64),
'image_raw': tf.FixedLenFeature([],
tf.string),
})
# You can do more image distortion here for training data
img = tf.decode_raw(features['image_raw'], tf.uint8)
img.set_shape([28 * 28])
img = tf.reshape(img, [28, 28, 1])
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
if one_hot and n_classes:
label = tf.one_hot(label, n_classes)
return img, label
def read_and_decode_cifar(filename_queue):
label_bytes = 1
height = 32
width = 32
depth = 3
image_bytes = height * width * depth
record_bytes = label_bytes + image_bytes
reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
_, value = reader.read(filename_queue)
record_bytes = tf.decode_raw(value, tf.uint8)
depth_major = tf.reshape(tf.slice(record_bytes, [label_bytes], [image_bytes]),
[depth, height, width])
image = tf.transpose(depth_major, [1, 2, 0])
image = tf.cast(image, tf.float32) * (2. / 255) - 1
return image
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)
# label and image are stored as bytes but could be stored as
# int64 or float64 values in a serialized tf.Example protobuf.
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 was saved as uint8, so we have to decode as uint8.
image = tf.decode_raw(tfrecord_features['image'], tf.uint8)
shape = tf.decode_raw(tfrecord_features['shape'], tf.int32)
# the image tensor is flattened out, so we have to reconstruct the shape
image = tf.reshape(image, shape)
label = tfrecord_features['label']
return label, shape, image
def read_and_decode(filename):
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw': tf.FixedLenFeature([], tf.string),
})
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [28, 28, 3])
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
return img, label
def read_tfrecord(filename_queuetemp):
filename_queue = tf.train.string_input_producer([filename_queuetemp])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'width': tf.FixedLenFeature([], tf.int64),
'depth': tf.FixedLenFeature([], tf.int64),
'label': tf.FixedLenFeature([], tf.int64)
}
)
image = tf.decode_raw(features['image_raw'], tf.uint8)
# image
depth = features['depth']
tf.reshape(image, [299, 299, 3])
# normalize
image = tf.cast(image, tf.float32) * (1. /255) - 0.5
# label
label = tf.cast(features['label'], tf.int32)
return image, label
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'ind': tf.FixedLenFeature([], tf.string),
'val': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
})
ind = tf.decode_raw(features['ind'], tf.int32)
val = tf.decode_raw(features['val'], tf.float32)
ind.set_shape([39])
val.set_shape([39])
ind = tf.cast(ind, tf.int32)
val = tf.cast(val, tf.float32)
label = tf.cast(features['label'], tf.int64)
return ind, val, label
def read_and_decode(self, 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),
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape([FLAGS.output_size*FLAGS.output_size*3])
image = tf.reshape(image, [FLAGS.output_size,FLAGS.output_size,3])
image = tf.cast(image, tf.float32) * (1. / 127.5) - 1.0
return image
def read_raw_images(sess, data_set):
filename = ['./data/' + data_set + '_data.bin']
filename_queue = tf.train.string_input_producer(filename)
print filename
record_bytes = (FLAGS.height) * (FLAGS.width) * FLAGS.depth + 1
image_bytes = (FLAGS.height) * (FLAGS.width) * FLAGS.depth
reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
key, value = reader.read(filename_queue)
record_bytes = tf.decode_raw(value, tf.uint8)
#record_label = tf.decode_raw(value, tf.int32)
tf.train.start_queue_runners(sess=sess)
for i in range(0, 10):
result = sess.run(record_bytes)
print i, result[0], len(result)
image = result[1:len(result)]
print image
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_raw': tf.FixedLenFeature([], tf.string),
})
image = tf.decode_raw(features['image_raw'], tf.int16)
image.set_shape([IMAGE_HEIGHT * IMAGE_WIDTH])
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
reshape_image = tf.reshape(image, [IMAGE_HEIGHT, IMAGE_WIDTH, 1])
label = tf.decode_raw(features['label_raw'], tf.uint8)
label.set_shape([CHARS_NUM * CLASSES_NUM])
reshape_label = tf.reshape(label, [CHARS_NUM, CLASSES_NUM])
return tf.cast(reshape_image, tf.float32), tf.cast(reshape_label, tf.float32)
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'channels': tf.FixedLenFeature([], tf.int64),
'image_data': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
})
image = tf.decode_raw(features['image_data'], tf.uint8)
image = tf.reshape(image, [100, 100, 3])
image = tf.cast(image, tf.float32)
label = tf.cast(features['label'], tf.int32)
return image, label
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'channels': tf.FixedLenFeature([], tf.int64),
'image_data': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
})
image = tf.decode_raw(features['image_data'], tf.uint8)
image = tf.reshape(image, [100, 100, 3])
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
#image = tf.cast(image, tf.float32)
label = tf.cast(features['label'], tf.int32)
return image, label