我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.decode_raw()。
def _read_sequence_example(filename_queue, n_labels=50, n_samples=59049, n_segments=10): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) context, sequence = tf.parse_single_sequence_example( serialized_example, context_features={ 'raw_labels': tf.FixedLenFeature([], dtype=tf.string) }, sequence_features={ 'raw_segments': tf.FixedLenSequenceFeature([], dtype=tf.string) }) segments = tf.decode_raw(sequence['raw_segments'], tf.float32) segments.set_shape([n_segments, n_samples]) labels = tf.decode_raw(context['raw_labels'], tf.uint8) labels.set_shape([n_labels]) labels = tf.cast(labels, tf.float32) return segments, labels
def parse_example(serialized_example): features = tf.parse_single_example( serialized_example, # Defaults are not specified since both keys are required. features={ 'shape': tf.FixedLenFeature([], tf.string), 'img_raw': tf.FixedLenFeature([], tf.string), 'gt_raw': tf.FixedLenFeature([], tf.string), 'example_name': tf.FixedLenFeature([], tf.string) }) with tf.variable_scope('decoder'): shape = tf.decode_raw(features['shape'], tf.int32) image = tf.decode_raw(features['img_raw'], tf.float32) ground_truth = tf.decode_raw(features['gt_raw'], tf.uint8) example_name = features['example_name'] with tf.variable_scope('image'): # reshape and add 0 dimension (would be batch dimension) image = tf.expand_dims(tf.reshape(image, shape), 0) with tf.variable_scope('ground_truth'): # reshape ground_truth = tf.cast(tf.reshape(ground_truth, shape[:-1]), tf.float32) return image, ground_truth, example_name
def read_example(self, filename_queue): # TFRecoard reader reader = tf.TFRecordReader() key, serialized_example = reader.read(filename_queue) # read data from serialized examples features = tf.parse_single_example( serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'image_raw': tf.FixedLenFeature([], tf.string) }) label = features['label'] image = features['image_raw'] # decode raw image data as integers if self.image_format == 'jpeg': decoded_image = tf.image.decode_jpeg( image, channels=self.image_channels) else: decoded_image = tf.decode_raw(image, tf.uint8) return decoded_image, label
def read_whole_features(file_pattern, num_epochs=1): ''' Return `feature`: `dict` whose keys are `sp`, `ap`, `f0`, `en`, `speaker` ''' files = tf.gfile.Glob(file_pattern) print('{} files found'.format(len(files))) filename_queue = tf.train.string_input_producer(files, num_epochs=num_epochs) reader = tf.WholeFileReader() key, value = reader.read(filename_queue) print("Processing {}".format(key), flush=True) value = tf.decode_raw(value, tf.float32) value = tf.reshape(value, [-1, FEAT_DIM]) return { 'sp': value[:, :SP_DIM], 'ap': value[:, SP_DIM : 2*SP_DIM], 'f0': value[:, SP_DIM * 2], 'en': value[:, SP_DIM * 2 + 1], 'speaker': tf.cast(value[:, SP_DIM * 2 + 2], tf.int64), 'filename': key, }
def _parse_example(self, serialized): """Unpack a serialized example to Tensor.""" feats = self._get_data_features() sz_feats = self._get_sz_features() for s in sz_feats: feats[s] = sz_feats[s] sample = tf.parse_single_example(serialized, features=feats) data = {} for i, f in enumerate(self.FEATURES): s = tf.to_int32(sample[f+'_sz']) data[f] = tf.decode_raw(sample[f], self.dtypes[f], name='decode_{}'.format(f)) data[f] = tf.reshape(data[f], s) return data
def parse_mnist_tfrec(tfrecord, features_shape): tfrecord_features = tf.parse_single_example( tfrecord, features={ 'features': tf.FixedLenFeature([], tf.string), 'targets': tf.FixedLenFeature([], tf.string) } ) features = tf.decode_raw(tfrecord_features['features'], tf.uint8) features = tf.reshape(features, features_shape) features = tf.cast(features, tf.float32) targets = tf.decode_raw(tfrecord_features['targets'], tf.uint8) targets = tf.reshape(targets, []) targets = tf.one_hot(indices=targets, depth=10, on_value=1, off_value=0) targets = tf.cast(targets, tf.float32) return features, targets
def parse_mnist_tfrec(tfrecord, name, features_shape, scalar_targs=False): tfrecord_features = tf.parse_single_example( tfrecord, features={ 'features': tf.FixedLenFeature([], tf.string), 'targets': tf.FixedLenFeature([], tf.string) }, name=name+'_data' ) with tf.variable_scope('features'): features = tf.decode_raw( tfrecord_features['features'], tf.uint8 ) features = tf.reshape(features, features_shape) features = tf.cast(features, tf.float32) with tf.variable_scope('targets'): targets = tf.decode_raw(tfrecord_features['targets'], tf.uint8) if scalar_targs: targets = tf.reshape(targets, []) targets = tf.one_hot( indices=targets, depth=10, on_value=1, off_value=0 ) targets = tf.cast(targets, tf.float32) return features, targets
def read_and_decode(filename_queue, batch_size): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) feature = features() feature = tf.parse_single_example( serialized_example, features = feature, ) hr_image = tf.decode_raw(feature['hr_image'], tf.uint8) height = tf.cast(feature['height'], tf.int32) width = tf.cast(feature['width'], tf.int32) print(height) image_shape = tf.stack([128, 128,3 ]) hr_image = tf.reshape(hr_image, image_shape) hr_image = tf.image.random_flip_left_right(hr_image) hr_image = tf.image.random_contrast(hr_image, 0.5, 1.3) hr_images = tf.train.shuffle_batch([hr_image], batch_size = batch_size, capacity = 30, num_threads = 2, min_after_dequeue = 10) return hr_images
def read_and_decode(self, example_serialized): """ Read and decode binarized, raw MNIST dataset from .tfrecords file generated by MNIST.py """ num = self.flags['num_classes'] # Parse features from binary file features = tf.parse_single_example( example_serialized, features={ 'image': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([num], tf.int64, default_value=[-1] * num), 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'depth': tf.FixedLenFeature([], tf.int64), }) # Return the converted data label = features['label'] image = tf.decode_raw(features['image'], tf.float32) image.set_shape([784]) image = tf.reshape(image, [28, 28, 1]) image = (image - 0.5) * 2 # max value = 1, min value = -1 return image, tf.cast(label, tf.int32)
def read_and_decode(self, example_serialized): """ Read and decode binarized, raw MNIST dataset from .tfrecords file generated by MNIST.py """ features = tf.parse_single_example( example_serialized, features={ 'image': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([self.flags['num_classes']], tf.int64, default_value=[-1]*self.flags['num_classes']), 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'depth': tf.FixedLenFeature([], tf.int64), }) # now return the converted data label = features['label'] image = tf.decode_raw(features['image'], tf.float32) image.set_shape([784]) image = tf.reshape(image, [28, 28, 1]) image = (image - 0.5) * 2 # max value = 1, min value = -1 return image, tf.cast(label, tf.int32)
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_embedding(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={ 'label': tf.FixedLenFeature( [], tf.int64), 'sequence_raw': tf.FixedLenFeature( [], tf.string), }) sequence = features['sequence_raw'] # preprocess s_decode = tf.decode_raw(sequence, tf.int32) s_decode.set_shape([FLAGS.embed_length]) # Convert label from a scalar uint8 tensor to an int32 scalar. label = tf.cast(features['label'], tf.int32) return s_decode, label
def decode_raw(image_buffer, orig_height, orig_width, scope=None): """Decode a RAW string into one 3-D float image Tensor. Args: image_buffer: scalar string Tensor. [orig_height, orig_width]: the size of original image scope: Optional scope for op_scope. Returns: 3-D float Tensor with values ranging from [0, 1). """ with tf.op_scope([image_buffer], scope, 'decode_raw'): # Decode the string as an raw RGB. image = tf.decode_raw(image_buffer, tf.uint8) image = tf.reshape(image, tf.concat([orig_height,orig_width,[3]],0)) # After this point, all image pixels reside in [0,1) # The various adjust_* ops all require this range for dtype float. image = tf.image.convert_image_dtype(image, dtype=tf.float32) return image
def decode_from_tfrecords(filename,num_epoch=None): filename_queue=tf.train.string_input_producer([filename],num_epochs=num_epoch)#??????????????????????????????????????? reader=tf.TFRecordReader() _,serialized=reader.read(filename_queue) example=tf.parse_single_example(serialized,features={ 'height':tf.FixedLenFeature([],tf.int64), 'width':tf.FixedLenFeature([],tf.int64), 'nchannel':tf.FixedLenFeature([],tf.int64), 'image':tf.FixedLenFeature([],tf.string), 'label':tf.FixedLenFeature([],tf.int64) }) label=tf.cast(example['label'], tf.int32) image=tf.decode_raw(example['image'],tf.uint8) image=tf.reshape(image,tf.pack([ tf.cast(example['height'], tf.int32), tf.cast(example['width'], tf.int32), tf.cast(example['nchannel'], tf.int32)])) return image,label
def read_and_decode(filename, batch_size): # ??????????? 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) print('xxxx: ', img.get_shape()) img = tf.reshape(img, [512, 144, 3]) img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 label = tf.cast(features['label'], tf.int32) image_batch, label_batch = tf.train.batch([img, label], batch_size=batch_size, num_threads=64, capacity=2000) return image_batch, tf.reshape(label_batch, [batch_size])
def read_and_decode(filename, img_size=128, depth=1): if not filename.endswith('.tfrecords'): print "Invalid file \"{:s}\"".format(filename) return [], [] else: data_queue = tf.train.string_input_producer([filename]) reader = tf.TFRecordReader() _, serialized_example = reader.read(data_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, [img_size, img_size, depth]) # Normalize the image img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 label = tf.cast(features['label'], tf.int32) label_onehot = tf.stack(tf.one_hot(label, n_classes)) return img, label_onehot #read_and_decode('test.tfrecords')
def read_and_decode(filename, img_size=128, depth=1): if not filename.endswith('.tfrecords'): print "Invalid file \"{:s}\"".format(filename) return [], [] else: data_queue = tf.train.string_input_producer([filename]) reader = tf.TFRecordReader() _, serialized_example = reader.read(data_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, [img_size, img_size, depth]) # Normalize the image img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 label = tf.cast(features['label'], tf.int32) label_onehot = tf.stack(tf.one_hot(label, n_classes)) return img, label_onehot
def _decode(self, image_buffer): """Decodes the image buffer. Args: image_buffer: The tensor representing the encoded image tensor. Returns: A tensor that represents decoded image of self._shape, or (?, ?, self._channels) if self._shape is not specified. """ def decode_raw(): """Decodes a raw image.""" return tf.decode_raw(image_buffer, out_type=self._dtype) image = decode_raw() # image.set_shape([None, None, self._channels]) if self._shape is not None: image = tf.reshape(image, self._shape) return image
def decode_image_objects(paths): with tf.name_scope(inspect.stack()[0][3]): with tf.name_scope('parse_example'): reader = tf.TFRecordReader() _, serialized = reader.read(tf.train.string_input_producer(paths)) example = tf.parse_single_example(serialized, features={ 'imagepath': tf.FixedLenFeature([], tf.string), 'imageshape': tf.FixedLenFeature([3], tf.int64), 'objects': tf.FixedLenFeature([2], tf.string), }) imagepath = example['imagepath'] objects = example['objects'] with tf.name_scope('decode_objects'): objects_class = tf.decode_raw(objects[0], tf.int64, name='objects_class') objects_coord = tf.decode_raw(objects[1], tf.float32) objects_coord = tf.reshape(objects_coord, [-1, 4], name='objects_coord') with tf.name_scope('load_image'): imagefile = tf.read_file(imagepath) image = tf.image.decode_jpeg(imagefile, channels=3) return image, example['imageshape'], objects_class, objects_coord
def decode_record(filename_queue, patch_size, channel_num=3): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'image': tf.FixedLenFeature([], tf.string), }) img = tf.decode_raw(features['image'], tf.uint8) img = tf.reshape(img, [patch_size, patch_size, channel_num]) 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, # Defaults are not specified since both keys are required. features={ 'image_left': tf.FixedLenFeature([], tf.string), 'image_right': tf.FixedLenFeature([], tf.string), }) image_left = tf.decode_raw(features['image_left'], tf.uint8) image_right = tf.decode_raw(features['image_right'], tf.uint8) width = 960 height = 540 depth = 4 image_left.set_shape([width*height*depth]) image_right.set_shape([width*height*depth]) return image_left, image_right
def _image_op_cifar10(filenames, relative_colors): label_bytes = 1 height = 32 width = 32 depth = 3 image_bytes = height * width * depth record_bytes = label_bytes + image_bytes filename_queue = tf.train.string_input_producer(filenames, num_epochs=1) 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) if relative_colors: image = util.absolute_to_relative_colors(image) return image
def _read_image(filename_queue): # copied from # https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_input.py # CIFAR-10 specification 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) label = tf.cast(tf.slice(record_bytes, [0], [label_bytes]), tf.int32) depth_major = tf.reshape(tf.slice(record_bytes, [label_bytes], [image_bytes]), [depth, height, width]) uint8image = tf.transpose(depth_major, [1, 2, 0]) image = tf.cast(uint8image, tf.float32) return image, tf.squeeze(label)
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), }) image = tf.decode_raw(features['image_raw'], tf.uint8) image.set_shape(128 * 128 * 3) image = tf.reshape(image, [128, 128, 3]) image = tf.cast(image, tf.float32) * (2. / 255) - 1. return image
def read_and_decode_with_labels(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(128 * 128 * 3) image = tf.reshape(image, [128, 128, 3]) image = tf.cast(image, tf.float32) * (2. / 255) - 1. 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={ 'image_raw': tf.FixedLenFeature([], tf.string), 'label_raw': tf.FixedLenFeature([], tf.string)}) image = tf.cast(tf.decode_raw(features['image_raw'], tf.int16), tf.float32) labels = tf.decode_raw(features['label_raw'], tf.int16) #PW 2017/03/03: Zero-center data here? image.set_shape([IMG_DIM*IMG_DIM*IMG_DIM]) image = tf.reshape(image, [IMG_DIM,IMG_DIM,IMG_DIM,1]) labels.set_shape([IMG_DIM*IMG_DIM*IMG_DIM]) labels = tf.reshape(image, [IMG_DIM,IMG_DIM,IMG_DIM]) # Dimensions (X, Y, Z, channles) return image, labels
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
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
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
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_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={ '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