def read_and_decode(filename, is_train): filename_queue = tf.train.string_input_producer([filename]) reader = tf.TFRecordReader() _,serialized_example = reader.read(filename_queue) if is_train == True: features = tf.parse_single_example(serialized_example, features={ "hat_label": tf.FixedLenFeature([], tf.int64), "hair_label": tf.FixedLenFeature([], tf.int64), "gender_label": tf.FixedLenFeature([], tf.int64), "top_label": tf.FixedLenFeature([], tf.int64), "down_label": tf.FixedLenFeature([], tf.int64), "shoes_label": tf.FixedLenFeature([], tf.int64), "bag_label": tf.FixedLenFeature([], tf.int64), "img_raw": tf.FixedLenFeature([], tf.string), }) img = tf.decode_raw(features['img_raw'], tf.uint8) img = tf.reshape(img, [128, 256, 3]) #image = Image.frombytes('RGB', (224, 224), img[0]) img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 #print(type(img)) #img = np.asarray(img, dtype=np.uint8) #print(type(img)) #tl.visualize.frame(I=img, second=5, saveable=False, name='frame', fig_idx=12836) hat_label = tf.cast(features['hat_label'], tf.int32) hair_label = tf.cast(features['hair_label'], tf.int32) gender_label = tf.cast(features['gender_label'], tf.int32) top_label = tf.cast(features['top_label'], tf.int32) down_label = tf.cast(features['down_label'], tf.int32) shoes_label = tf.cast(features['shoes_label'], tf.int32) bag_label = tf.cast(features['bag_label'], tf.int32) labels = {"hat":hat_label, "hair":hair_label, "gender":gender_label, "top":top_label, "down":down_label, "shoes":shoes_label, "bag":bag_label} return img, labels else: features = tf.parse_single_example(serialized_example, features={ "img_raw": tf.FixedLenFeature([], tf.string), }) img = tf.decode_raw(features['img_raw'], tf.uint8) img = tf.reshape(img, [128, 256, 3]) img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 #tl.visualize.frame(I=img, second=5, saveable=False, name='frame', fig_idx=12833) return img
def read_and_decode_single_example(filenames, image_shape, onlyDepth = False): # first construct a queue containing a list of filenames. # this lets a user split up there dataset in multiple files to keep # size down filename_queue = tf.train.string_input_producer(filenames,num_epochs=None) #symbolic reader to read one example at a time 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': tf.FixedLenFeature([image_shape[0]*image_shape[1]*image_shape[2]], tf.float32), 'label': tf.FixedLenFeature([], tf.int64), } ) # Convert from a scalar list to a proper shaped tensor image = features['image'] image = tf.reshape(image,image_shape) if onlyDepth: channels = tf.split(2,3,image) image = channels[2] label = features['label'] 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, # Defaults are not specified since both keys are required. features={ 'vector': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64), }) # features = tf.parse_single_example(serialized_example, dense_keys=['vector', 'label'], dense_types=[tf.string, tf.int64]) # Convert from a scalar string tensor (whose single string has # length tf_model.IMAGE_PIXELS) to a uint8 tensor with shape # [tf_model.IMAGE_PIXELS]. image = tf.decode_raw(features['vector'], tf.float32) image.set_shape([FEATURE_DIMENSIONALITY]) if FLAGS.transpose_input: image = tf.reshape(image, FEATURE_INPUT_SHAPE) image = tf.transpose(image, perm=[0,2,1]) image = tf.reshape(image, [-1]) # print("Image shape is %s" %(image.shape)) # 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) - 0.5 # Convert label from a scalar uint8 tensor to an int32 scalar. label = tf.cast(features['label'], tf.int32) return image, label
def __init__(self, input_files, num_epochs, batch_size): filename_queue = tf.train.string_input_producer(input_files, num_epochs=num_epochs) reader = tf.TFRecordReader() _, records = reader.read(filename_queue) decoded = tf.parse_single_example(records, dense_keys=['image', 'text', 'result', 'len'], dense_types=['float', 'int64', 'int64', 'int64'], dense_shapes=[(1, config.image_features_count), (config.sents_per_sample, config.max_len), (config.sents_per_sample, config.max_len), (config.sents_per_sample, 1)]) self.image, self.text, self.result, self.lens = \ decoded['image'], decoded['text'], decoded['result'], decoded['len'] self.image = tf.concat(0, [self.image] * config.sents_per_sample) # result requires one-hot encoding clamped_result = tf.minimum(self.result, config.output_words_count) sliced_result = [tf.squeeze(tensor, [0]) for tensor in tf.split(0, config.sents_per_sample, clamped_result)] sliced_categorical_result = [self.to_categorical(tensor) for tensor in sliced_result] self.categorical_result = tf.concat(0, [tf.expand_dims(tensor, 0) for tensor in sliced_categorical_result]) self.image_input, self.text_input, self.result_input, self.lens_input = tf.train.shuffle_batch( [self.image, self.text, self.categorical_result, self.lens], batch_size=batch_size, capacity=256+config.batch_size, min_after_dequeue=128, enqueue_many=True)
def read_from_tfrecord(tfrecord_path): with tf.Session() as sess: # create feature to hold data feature_dict = { 'image/height': tf.FixedLenFeature([], tf.int64), 'image/width': tf.FixedLenFeature([], tf.int64), 'image/filename': tf.VarLenFeature(tf.string), 'image/image': tf.FixedLenFeature([], tf.string), 'image/panel/bbox/xmin': tf.FixedLenFeature([], tf.float32), 'image/panel/bbox/ymin': tf.FixedLenFeature([], tf.float32), 'image/panel/bbox/xmax': tf.FixedLenFeature([], tf.float32), 'image/panel/bbox/ymax': tf.FixedLenFeature([], tf.float32), 'image/label/bbox/xmin': tf.FixedLenFeature([], tf.float32), 'image/label/bbox/ymin': tf.FixedLenFeature([], tf.float32), 'image/label/bbox/xmax': tf.FixedLenFeature([], tf.float32), 'image/label/bbox/ymax': tf.FixedLenFeature([], tf.float32), 'image/panel/label/text': tf.FixedLenFeature([], tf.string), 'image/panel/label/class': tf.FixedLenFeature([], tf.int64) } # create a list of tfrecord filenames and pass it to a queue filename_queue = tf.train.string_input_producer([tfrecord_path], num_epochs=1) # 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_dict) image_height = tf.case(features['image/height'], tf.int64) image_width = tf.case(features['image/width'], tf.int64) # convert the image data from string back to the numbers image = tf.decode_raw(features['image/image'], tf.uint8) image = tf.reshape(image, [image_height, image_width, 3])
def distorted_inputs (tfrecord_file_paths=[]): fqueue = tf.train.string_input_producer(tfrecord_file_paths) reader = tf.TFRecordReader() key, serialized_example = reader.read(fqueue) features = tf.parse_single_example(serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'image': tf.FixedLenFeature([], tf.string) }) image = tf.image.decode_jpeg(features['image'], channels=size['depth']) image = tf.cast(image, tf.float32) image.set_shape([size['width'], size['height'], size['depth']]) min_fraction_of_examples_in_queue = 0.4 min_queue_examples = int(cifar10.NUM_EXAMPLES_PER_EPOCH_FOR_EVAL * min_fraction_of_examples_in_queue) images, labels = tf.train.shuffle_batch( [tf.image.per_image_whitening(image), tf.cast(features['label'], tf.int32)], batch_size=BATCH_SIZE, capacity=min_queue_examples + 3 * BATCH_SIZE, min_after_dequeue=min_queue_examples ) images = tf.image.resize_images(images, size['input_width'], size['input_height']) tf.image_summary('images', images) return images, labels
def parse(example): para_limit = config.test_para_limit if is_test else config.para_limit ques_limit = config.test_ques_limit if is_test else config.ques_limit char_limit = config.char_limit features = tf.parse_single_example(example, features={ "context_idxs": tf.FixedLenFeature([], tf.string), "ques_idxs": tf.FixedLenFeature([], tf.string), "context_char_idxs": tf.FixedLenFeature([], tf.string), "ques_char_idxs": tf.FixedLenFeature([], tf.string), "y1": tf.FixedLenFeature([], tf.string), "y2": tf.FixedLenFeature([], tf.string), "id": tf.FixedLenFeature([], tf.int64) }) context_idxs = tf.reshape(tf.decode_raw( features["context_idxs"], tf.int32), [para_limit]) ques_idxs = tf.reshape(tf.decode_raw( features["ques_idxs"], tf.int32), [ques_limit]) context_char_idxs = tf.reshape(tf.decode_raw( features["context_char_idxs"], tf.int32), [para_limit, char_limit]) ques_char_idxs = tf.reshape(tf.decode_raw( features["ques_char_idxs"], tf.int32), [ques_limit, char_limit]) y1 = tf.reshape(tf.decode_raw( features["y1"], tf.float32), [para_limit]) y2 = tf.reshape(tf.decode_raw( features["y2"], tf.float32), [para_limit]) qa_id = features["id"] return context_idxs, ques_idxs, context_char_idxs, ques_char_idxs, y1, y2, qa_id
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={ 'image' : tf.FixedLenFeature([], tf.string), 'label0': tf.FixedLenFeature([], tf.int64), 'label1': tf.FixedLenFeature([], tf.int64), 'label2': tf.FixedLenFeature([], tf.int64), 'label3': tf.FixedLenFeature([], tf.int64), }) # 获取图片数据 image = tf.decode_raw(features['image'], tf.uint8) # tf.train.shuffle_batch必须确定shape image = tf.reshape(image, [224, 224]) # 图片预处理 image = tf.cast(image, tf.float32) / 255.0 image = tf.subtract(image, 0.5) image = tf.multiply(image, 2.0) # 获取label label0 = tf.cast(features['label0'], tf.int32) label1 = tf.cast(features['label1'], tf.int32) label2 = tf.cast(features['label2'], tf.int32) label3 = tf.cast(features['label3'], tf.int32) return image, label0, label1, label2, label3
def _test(self, kwargs, expected_values=None, expected_err_re=None): with self.test_session() as sess: # Pull out some keys to check shape inference dense_keys = kwargs["dense_keys"] if "dense_keys" in kwargs else [] sparse_keys = kwargs["sparse_keys"] if "sparse_keys" in kwargs else [] dense_shapes = kwargs["dense_shapes"] if "dense_shapes" in kwargs else [] # Returns dict w/ Tensors and SparseTensors out = tf.parse_single_example(**kwargs) # Check shapes self.assertEqual(len(dense_keys), len(dense_shapes)) for (k, s) in zip(dense_keys, dense_shapes): self.assertEqual(tuple(out[k].get_shape()), s) for k in sparse_keys: self.assertEqual(tuple(out[k].indices.get_shape().as_list()), (None, 1)) self.assertEqual(tuple(out[k].values.get_shape().as_list()), (None,)) self.assertEqual(tuple(out[k].shape.get_shape().as_list()), (1,)) # Check values result = flatten_values_tensors_or_sparse(out.values()) # flatten values if expected_err_re is None: tf_result = sess.run(result) _compare_output_to_expected(self, out, expected_values, tf_result) else: with self.assertRaisesOpError(expected_err_re): sess.run(result)
def _input_fn(): with tf.name_scope('input'): filename_queue = tf.train.string_input_producer( filenames, num_epochs=num_epochs) reader = tf.TFRecordReader() _, serialized_example = reader.read_up_to(filename_queue) features = tf.parse_single_example( serialized_examples, { 'words': tf.VarLenFeature(tf.string), 'subreddit': tf.FixedLenFeature([1], tf.int64) } ) padded_words = tf.sparse_to_dense( features['words'].indices, [sentence_length], features['words'].values, default_value='UNK' ) word_indices = tf.string_to_hash_bucket_fast( padded_words, vocab_size) sentences, subreddits = tf.train.shuffle_batch( [word_indices, features['subreddit']], batch_size, capacity=1000 + 3 * batch_size, min_after_dequeue=1000, enqueue_many=False ) return sentences, subreddits
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), 'label': tf.FixedLenFeature([], tf.int64), }) # Convert from a scalar string tensor (whose single string has # length mnist.IMAGE_PIXELS) to a uint8 tensor with shape # [mnist.IMAGE_PIXELS]. image = tf.decode_raw(features['image_raw'], tf.uint8) image.set_shape([57600]) # 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) - 0.5 # Convert label from a scalar uint8 tensor to an int32 scalar. #label = tf.cast(features['label'], tf.int32) <-- placeholder instead return tf.reshape(image, [160, 120, 3]), tf.placeholder(tf.int32) # TODO doublecheck this
def _parse_example_proto(example_serialized): """Parses an Example proto containing a training example of an image. The dataset contains serialized Example protocol buffers. The Example proto is expected to contain features named image/encoded (a JPEG-encoded string) and image/class/label (int) Args: example_serialized: scalar Tensor tf.string containing a serialized Example protocol buffer. Returns: image_buffer: Tensor tf.string containing the contents of a JPEG file. label: Tensor tf.int64 containing the label. """ # 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) return features['image/encoded'], features['image/class/label']
def parse_labeled_example( example_proto, view_index, preprocess_fn, image_attr_keys, label_attr_keys): """Parses a labeled test example from a specified view. Args: example_proto: A scalar string Tensor. view_index: Int, index on which view to parse. preprocess_fn: A function with the signature (raw_images, is_training) -> preprocessed_images, where raw_images is a 4-D float32 image `Tensor` of raw images, is_training is a Boolean describing if we're in training, and preprocessed_images is a 4-D float32 image `Tensor` holding preprocessed images. image_attr_keys: List of Strings, names for image keys. label_attr_keys: List of Strings, names for label attributes. Returns: data: A tuple of images, attributes and tasks `Tensors`. """ features = {} for attr_key in image_attr_keys: features[attr_key] = tf.FixedLenFeature((), tf.string) for attr_key in label_attr_keys: features[attr_key] = tf.FixedLenFeature((), tf.int64) parsed_features = tf.parse_single_example(example_proto, features) image_only_keys = [i for i in image_attr_keys if 'image' in i] view_image_key = image_only_keys[view_index] image = preprocessing.decode_image(parsed_features[view_image_key]) preprocessed = preprocess_fn(image, is_training=False) attributes = [parsed_features[k] for k in label_attr_keys] task = parsed_features['task'] return tuple([preprocessed] + attributes + [task])
def read_and_decode(filename_queue): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, dense_keys=['image_raw', 'label'], # Defaults are not specified since both keys are required. dense_types=[tf.string, tf.int64]) # Convert from a scalar string tensor (whose single string has # length mnist.IMAGE_PIXELS) to a uint8 tensor with shape # [mnist.IMAGE_PIXELS]. image = tf.decode_raw(features['image_raw'], tf.uint8) image.set_shape([mnist.IMAGE_PIXELS]) # 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) - 0.5 # Convert label from a scalar uint8 tensor to an int32 scalar. label = tf.cast(features['label'], tf.int32) return image, label
def getImage(filenames): # convert filenames to a queue for an input pipeline. filenameQ = tf.train.string_input_producer(filenames,num_epochs=None) # object to read records recordReader = tf.TFRecordReader() # read the full set of features for a single example key, fullExample = recordReader.read(filenameQ) # parse the full example into its' component features. features = tf.parse_single_example( fullExample, features={ 'image/height': tf.FixedLenFeature([], tf.int64), 'image/width': tf.FixedLenFeature([], tf.int64), 'image/depth': tf.FixedLenFeature([], tf.int64), 'image/class/label': tf.FixedLenFeature([],tf.int64), 'image/class/text': tf.FixedLenFeature([], dtype=tf.string,default_value=''), 'image/filename': tf.FixedLenFeature([], dtype=tf.string,default_value=''), 'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='') }) label = features['image/class/label'] image_buffer = features['image/encoded'] image = tf.decode_raw(image_buffer, tf.float32) image = tf.reshape(image, tf.stack([FLAGS.width*FLAGS.height*FLAGS.depth])) label=tf.stack(tf.one_hot(label-1, nLabel)) return label, image
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, image_size=self.image_size, use_bfloat16=self.use_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
def tfrecord_to_graph_ops(filenames, num_epochs): file_queue = tf.train.string_input_producer( filenames, name='file_queue', num_epochs=num_epochs ) reader = tf.TFRecordReader( options=tf.python_io.TFRecordOptions( compression_type=tf.python_io.TFRecordCompressionType.GZIP ) ) _, tfrecord = reader.read(file_queue) tfrecord_features = tf.parse_single_example( tfrecord, features={ 'images': tf.FixedLenFeature([], tf.string), 'labels': tf.FixedLenFeature([], tf.string), }, name='data' ) tfeat = tf.decode_raw(tfrecord_features['images'], tf.uint8) # note, 'NCHW' is only supported on GPUs, so use 'NHWC'... tfeat = tf.reshape(tfeat, [-1, 28, 28, 1]) ttarg = tf.decode_raw(tfrecord_features['labels'], tf.uint8) ttarg = tf.one_hot(indices=ttarg, depth=10, on_value=1, off_value=0) return tfeat, ttarg
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) # Initially reshaping to [H, W, C] does not work image = tf.reshape(image, [NUM_CHANNEL, IMAGE_HEIGHT, IMAGE_WIDTH]) # This is needed for `tf.image.resize_image_with_crop_or_pad` image = tf.transpose(image, [1, 2, 0]) image = tf.cast(image, dtype) label = tf.cast(features["label"], tf.int32) if data_aug: image = tf.image.resize_image_with_crop_or_pad(image, IMAGE_HEIGHT + 4, IMAGE_WIDTH + 4) image = tf.random_crop(image, [IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNEL]) image = tf.image.random_flip_left_right(image) if data_format == "channels_first": image = tf.transpose(image, [2, 0, 1]) if div255: image /= 255. return image, label
def read_and_preprocess(example_data): """parses tfrecord and returns image, label. Args: example_data (str): tfrecord Returns: img, label """ height = width = PATCH_SIZE(params) parsed = tf.parse_single_example( example_data, { 'ref': tf.VarLenFeature(tf.float32), 'ltg': tf.VarLenFeature(tf.float32), 'has_ltg': tf.FixedLenFeature([], tf.int64, 1), }) parsed['ref'] = _sparse_to_dense(parsed['ref'], height * width) parsed['ltg'] = _sparse_to_dense(parsed['ltg'], height * width) # keras wants labels to be float32 label = tf.cast( tf.reshape(parsed['has_ltg'], shape=[]), dtype=tf.float32) print('shape of label {}'.format(label.shape)) img = reshape_into_image(parsed, params) return img, label
def read_cifar10(filename_queue): """Reads and parses examples from CIFAR10 data files. Recommendation: if you want N-way read parallelism, call this function N times. This will give you N independent Readers reading different files & positions within those files, which will give better mixing of examples. Args: filename_queue: A queue of strings with the filenames to read from. Returns: An object representing a single example, with the following fields: height: number of rows in the result (32) width: number of columns in the result (32) depth: number of color channels in the result (3) key: a scalar string Tensor describing the filename & record number for this example. label: an int32 Tensor with the label in the range 0..9. uint8image: a [height, width, depth] uint8 Tensor with the image data """ class CIFAR10Record(object): pass result = CIFAR10Record() # Dimensions of the images in the CIFAR-10 dataset. # See http://www.cs.toronto.edu/~kriz/cifar.html for a description of the # input format. label_bytes = 1 # 2 for CIFAR-100 result.height = 256 result.width = 256 result.depth = 3 image_bytes = result.height * result.width * result.depth # Every record consists of a label followed by the image, with a # fixed number of bytes for each. record_bytes = label_bytes + image_bytes # Read a record, getting filenames from the filename_queue. No # header or footer in the CIFAR-10 format, so we leave header_bytes # and footer_bytes at their default of 0. 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), }) record_bytes = tf.decode_raw(features['image_raw'], tf.uint8) # depth_major = tf.reshape(record_bytes, [result.depth, result.height, result.width]) # Convert from [depth, height, width] to [height, width, depth]. result.uint8image = tf.reshape(record_bytes, [result.height, result.width, result.depth]) # The first bytes represent the label, which we convert from uint8->int32. result.label = tf.cast(features['label'], tf.int32) return result
def parser(example): features = { 'xywhc': tf.FixedLenFeature([150], tf.float32), 'img': tf.FixedLenFeature((), tf.string)} feats = tf.parse_single_example(example, features) coord = feats['xywhc'] coord = tf.reshape(coord, [30, 5]) img = tf.decode_raw(feats['img'], tf.float32) img = tf.reshape(img, [416, 416, 3]) img = tf.image.resize_images(img, [cfg.train.image_resized, cfg.train.image_resized]) rnd = tf.less(tf.random_uniform(shape=[], minval=0, maxval=2), 1) def flip_img_coord(_img, _coord): zeros = tf.constant([[0, 0, 0, 0, 0]]*30, tf.float32) img_flipped = tf.image.flip_left_right(_img) idx_invalid = tf.reduce_all(tf.equal(coord, 0), axis=-1) coord_temp = tf.concat([tf.minimum(tf.maximum(1 - _coord[:, :1], 0), 1), _coord[:, 1:]], axis=-1) coord_flipped = tf.where(idx_invalid, zeros, coord_temp) return img_flipped, coord_flipped img, coord = tf.cond(rnd, lambda: (tf.identity(img), tf.identity(coord)), lambda: flip_img_coord(img, coord)) img = tf.image.random_hue(img, max_delta=0.1) img = tf.image.random_contrast(img, lower=0.8, upper=1.2) img = tf.image.random_brightness(img, max_delta=0.1) img = tf.image.random_saturation(img, lower=0.8, upper=1.2) img = tf.minimum(img, 1.0) img = tf.maximum(img, 0.0) return img, coord
def deserialize(examples_serialized): """Called by Dataset.map() to convert batches of records to tensors.""" features = tf.parse_single_example(examples_serialized, feature_map) users = tf.reshape(tf.decode_raw( features[movielens.USER_COLUMN], tf.int32), (batch_size,)) items = tf.reshape(tf.decode_raw( features[movielens.ITEM_COLUMN], tf.uint16), (batch_size,)) if params["use_tpu"] or params["use_xla_for_gpu"]: items = tf.cast(items, tf.int32) # TPU and XLA disallows uint16 infeed. if not training: dupe_mask = tf.reshape(tf.cast(tf.decode_raw( features[rconst.DUPLICATE_MASK], tf.int8), tf.bool), (batch_size,)) return { movielens.USER_COLUMN: users, movielens.ITEM_COLUMN: items, rconst.DUPLICATE_MASK: dupe_mask, } labels = tf.reshape(tf.cast(tf.decode_raw( features["labels"], tf.int8), tf.bool), (batch_size,)) return { movielens.USER_COLUMN: users, movielens.ITEM_COLUMN: items, }, labels
def build_input(tfrecord_paths): """Builds the graph's input. Args: tfrecord_paths: List of paths to the input TFRecords Returns: serialized_example_tensor: The next serialized example. String scalar Tensor image_tensor: The decoded image of the example. Uint8 tensor, shape=[1, None, None,3] """ filename_queue = tf.train.string_input_producer( tfrecord_paths, shuffle=False, num_epochs=1) tf_record_reader = tf.TFRecordReader() _, serialized_example_tensor = tf_record_reader.read(filename_queue) features = tf.parse_single_example( serialized_example_tensor, features={ standard_fields.TfExampleFields.image_encoded: tf.FixedLenFeature([], tf.string), }) encoded_image = features[standard_fields.TfExampleFields.image_encoded] image_tensor = tf.image.decode_image(encoded_image, channels=3) image_tensor.set_shape([None, None, 3]) image_tensor = tf.expand_dims(image_tensor, 0) return serialized_example_tensor, image_tensor
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), 'label': tf.FixedLenFeature([], tf.int64), 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'depth': tf.FixedLenFeature([], tf.int64) }) image = tf.decode_raw(features['image_raw'], tf.uint8) img_height = tf.cast(features['height'], tf.int32) img_width = tf.cast(features['width'], tf.int32) img_depth = tf.cast(features['depth'], tf.int32) # Convert label from a scalar uint8 tensor to an int32 scalar. label = tf.cast(features['label'], tf.int32) image.set_shape([IMG_PIXELS]) image = tf.reshape(image, [IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS]) # Convert from [0, 255] -> [-0.5, 0.5] floats. image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 return image, label
def load_records_file(matcher): filename_queue = tf.train.string_input_producer( tf.train.match_filenames_once(".%s/*.tfrecords" % matcher)) reader = tf.TFRecordReader() _, serialized = reader.read(filename_queue) features = tf.parse_single_example( serialized, features={ 'label': tf.FixedLenFeature([], tf.string), 'image': tf.FixedLenFeature([], tf.string), }) record_image = tf.decode_raw(features['image'], tf.uint8) # Changing the image into this shape helps train and visualize the # output by converting it to be organized like an image. image = tf.reshape(record_image, [250, 151, 1]) label = tf.cast(features['label'], tf.string) min_after_dequeue = 10 batch_size = 3 capacity = min_after_dequeue + 3 * batch_size image_batch, label_batch = tf.train.shuffle_batch( [image, label], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue) return image_batch, label_batch
def record_parser_fn(value, is_training): """Parse an image record from `value`.""" keys_to_features = { 'width': tf.FixedLenFeature([], dtype=tf.int64, default_value=0), 'height': tf.FixedLenFeature([], dtype=tf.int64, default_value=0), 'image': tf.FixedLenFeature([], dtype=tf.string, default_value=''), 'label': tf.FixedLenFeature([], dtype=tf.int64, default_value=-1), 'name': tf.FixedLenFeature([], dtype=tf.string, default_value='') } parsed = tf.parse_single_example(value, keys_to_features) image = tf.image.decode_image(tf.reshape(parsed['image'], shape=[]), FLAGS.image_channels) image = tf.image.convert_image_dtype(image, dtype=tf.float32) bbox = tf.concat(axis=0, values=[ [[]], [[]], [[]], [[]] ]) bbox = tf.transpose(tf.expand_dims(bbox, 0), [0, 2, 1]) image = image_preprocess.preprocess_image( image=image, output_height=FLAGS.image_size, output_width=FLAGS.image_size, object_cover=0.0, area_cover=0.05, is_training=is_training, bbox=bbox) label = tf.cast(tf.reshape(parsed['label'], shape=[]),dtype=tf.int32) label = tf.one_hot(label, FLAGS.class_num) return image, label
def get_Image(nameOFfile): filename_queue = tf.train.string_input_producer([nameOFfile], num_epochs = 10) reader = tf.TFRecordReader() key, image = reader.read(nameOFfile) features = tf.parse_single_example(image, features={ 'image/height': tf.FixedLenFeature([], tf.int64), 'image/width': tf.FixedLenFeature([],tf.int64), 'image/colorspace': tf.FixedLenFeature([],dtype=tf.string, default_value = ''), 'image/channels': tf.FixedLenFeature([], tf.int64), 'image/class/label': tf.FixedLenFeature([], tf.int64), 'image/class/text': tf.FixedLenFeature([], dtype=tf.string, default_value = ''), 'image/format': tf.FixedLenFeature([], dtype=tf.string, default_value = ''), 'image/format': tf.FixedLenFeature([],dtype=tf.string, default_value=''), 'image/filename': tf.FixedLenFeature([], dtype=tf.string,default_value=''), 'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='') }) labe = features['image/class/label'] _buffer = features['image/encoded'] with tf.name_scope('decode_jpeg', [_buffer], None): pic = tf.image.decode_jpeg(_buffer, channels=3) pic = tf.image.convert_image_dtype(pic, dtype=tf.float32) pic = tf.reshape(1-tf.image.rgb_to_grayscale(image), [height*width]) labe = tf.stack(tf.one_hot(labe-1, nClass)) return labe, pic
def get_example(self, batch_size): """Get a single example from the tfrecord file. Args: batch_size: Int, minibatch size. Returns: tf.Example protobuf parsed from tfrecord. """ reader = tf.TFRecordReader() num_epochs = None if self.is_training else 1 capacity = batch_size path_queue = tf.train.input_producer( [self.record_path], num_epochs=num_epochs, shuffle=self.is_training, capacity=capacity) unused_key, serialized_example = reader.read(path_queue) features = { "note_str": tf.FixedLenFeature([], dtype=tf.string), "pitch": tf.FixedLenFeature([1], dtype=tf.int64), "velocity": tf.FixedLenFeature([1], dtype=tf.int64), "audio": tf.FixedLenFeature([64000], 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(serialized_example, features) return example
def read_image(file_queue): reader = tf.TFRecordReader() # key, value = reader.read(file_queue) _, serialized_example = reader.read(file_queue) features = tf.parse_single_example( serialized_example, features={ 'label': tf.FixedLenFeature([], tf.string), 'image_raw': tf.FixedLenFeature([], tf.string) }) image = tf.decode_raw(features['image_raw'], tf.uint8) # print('image ' + str(image)) image = tf.reshape(image, [INPUT_IMG_WIDE, INPUT_IMG_HEIGHT, INPUT_IMG_CHANNEL]) # image = tf.image.convert_image_dtype(image, dtype=tf.float32) # image = tf.image.resize_images(image, (IMG_HEIGHT, IMG_WIDE)) # image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 label = tf.decode_raw(features['label'], tf.uint8) # label = tf.cast(label, tf.int64) label = tf.reshape(label, [OUTPUT_IMG_WIDE, OUTPUT_IMG_HEIGHT]) # label = tf.decode_raw(features['image_raw'], tf.uint8) # print(label) # label = tf.reshape(label, shape=[1, 4]) return image, label
def run(): with tf.Session() as sess: print("start") feature = {'image': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64)} # Create a list of filenames and pass it to a queue print(data_path) filename_queue = tf.train.string_input_producer(data_path, num_epochs=1) # 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) # image = tf.cast(image, tf.int32) # Cast label data into int32 label = tf.cast(features['label'], tf.int32) # Reshape image data into the original shape init_op = [tf.global_variables_initializer(), tf.local_variables_initializer()] sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) train_list = [] for i in range(1000): example, l = sess.run([image, label]) train_list.append((example,l)) # print (example, l) coord.request_stop() coord.join(threads) return train_list # run()
def load_from_tfRecord(self, filename_queue, resize_size=None): 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), 'height': tf.FixedLenFeature([], tf.int64) }) image = tf.decode_raw(features['image_raw'], tf.float32) orig_height = tf.cast(features['height'], tf.int32) orig_width = tf.cast(features['width'], tf.int32) image_shape = tf.pack([orig_height, orig_width, 3]) image_tf = tf.reshape(image, image_shape) print image_shape resized_image = tf.image.resize_image_with_crop_or_pad( image_tf, target_height=resize_size[1], target_width=resize_size[0]) images = tf.train.shuffle_batch([resized_image], batch_size=self.batch_size, num_threads=1, capacity=50, min_after_dequeue=10) return images
def read_and_decode_from_tfrecord(filename): # 创建文件队列,不限读取的数量 filename_queue = tf.train.string_input_producer([filename]) # create a reader from file queue reader = tf.TFRecordReader() # reader从文件队列中读入一个序列化的样本 _, serialized_example = reader.read(filename_queue) # get feature from serialized example # 解析符号化的样本 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, [224, 224, 3]) #[W,H,C] # Normalize the values of the image from the range [0, 255] to [-0.5, 0.5] # image = tf.cast(img, tf.float32) / 255 - 0.5 img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 label = tf.cast(features['label'], tf.int32) return img, label
def get_split(config, split_name, dataset_dir, batch_size, file_pattern=None, reader=None): """Gets a dataset tuple with instructions for reading flowers. Args: split_name: A train/validation split name. dataset_dir: The base directory of the dataset sources. file_pattern: The file pattern to use when matching the dataset sources. It is assumed that the pattern contains a '%s' string so that the split name can be inserted. reader: The TensorFlow reader type. Returns: A `Dataset` namedtuple. Raises: ValueError: if `split_name` is not a valid train/validation split. """ all_file = [] reader = tf.TFRecordReader() batch_size = config.batch_size data_splitnum = config.data_split_num file_pattern = _FILE_PATTERN if split_name == 'train': num_epochs = None for i in range(data_splitnum): all_file.append( os.path.join(dataset_dir, 'mnist_fashion/', file_pattern % (split_name, i))) elif split_name == 'test': num_epochs, batch_size = 1, 1 all_file.append( os.path.join(dataset_dir, 'mnist_fashion/', file_pattern % (split_name, 0))) elif split_name not in SPLITS_TO_SIZES: raise ValueError('split name %s was not recognized.' % split_name) filename_queue = tf.train.string_input_producer(all_file, num_epochs=num_epochs, shuffle=False) _, 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_string': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.float32) }) image = tf.decode_raw(features['image_string'], tf.uint8) label = tf.cast(features['label'], tf.float32) height = tf.cast(features['height'], tf.int32) width = tf.cast(features['width'], tf.int32) image = tf.reshape(image, [height, width, 1]) resized_image = tf.image.resize_images(images=image, size=[IMAGE_HEIGHT, IMAGE_WIDTH]) min_after_dequeue = 10000 capacity = min_after_dequeue + 3 * batch_size images, labels = tf.train.shuffle_batch( [resized_image, label], batch_size=batch_size, capacity=capacity, num_threads=1, min_after_dequeue=min_after_dequeue, seed=config.random_seed) return images, labels, SPLITS_TO_SIZES[split_name]
def parse_record(example_proto): parsed_example = tf.parse_single_example(example_proto, tf_record_dict) for key in tf_record_dict.keys(): parsed_example[key] = tf.reshape(parsed_example[key], ()) label = parsed_example.pop('label') return parsed_example, label
def decode(self, data, items): """Decodes the data to return the tensors specified by the list of items with numpy support. Args: data: The TFRecord data(serialized example) to decode. items: A list of strings, each of which is the name of the resulting tensors to retrieve. Returns: A list of tensors, each of which corresponds to each item. """ # pylint: disable=too-many-branches feature_description = dict() for key, value in self._feature_original_types.items(): shape = [] ###debug if len(value) == 3: if isinstance(value[-1], int): shape = [value[-1]] elif isinstance(value[-1], list): shape = value if len(value) < 2 or value[1] == 'FixedLenFeature': feature_description.update({ key: tf.FixedLenFeature(shape, dtypes.get_tf_dtype(value[0])) }) elif value[1] == 'VarLenFeature': feature_description.update( {key: tf.VarLenFeature(dtypes.get_tf_dtype(value[0]))}) else: shape = [] decoded_data = tf.parse_single_example(data, feature_description) # Handle TFRecord containing images if isinstance(self._image_options, dict): self._decode_image_str_byte(self._image_options, decoded_data) elif isinstance(self._image_options, HParams): self._decode_image_str_byte(self._image_options.todict(), decoded_data) elif isinstance(self._image_options, list): _ = list( map(lambda x: self._decode_image_str_byte(x, decoded_data), self._image_options)) # Handle TFRecord containing numpy.ndarray if isinstance(self._numpy_options, dict): self._decode_numpy_ndarray_str_byte(self._numpy_options, decoded_data) elif isinstance(self._numpy_options, HParams): self._decode_numpy_ndarray_str_byte(self._numpy_options.todict(), decoded_data) # Convert Dtypes for key, value in self._feature_convert_types.items(): from_type = decoded_data[key].dtype to_type = dtypes.get_tf_dtype(value) if from_type is to_type: continue elif to_type is tf.string: decoded_data[key] = tf.dtypes.as_string(decoded_data[key]) elif from_type is tf.string: decoded_data[key] = tf.string_to_number( decoded_data[key], to_type) else: decoded_data[key] = tf.cast(decoded_data[key], to_type) outputs = decoded_data return [outputs[item] for item in items]
def parse_tfrecord_tf(record): features = tf.parse_single_example(record, features={ 'shape': tf.FixedLenFeature([3], tf.int64), 'data': tf.FixedLenFeature([], tf.string)}) data = tf.decode_raw(features['data'], tf.uint8) return tf.reshape(data, features['shape'])
f = file(output_file, 'rb') records_done = pkl.load(f) f.close() else: records_done = {} if record in records_done: print(record + ' : Skipped') print(len(records_done) / float(len(records_todo))) continue filepaths_queue = tf.train.string_input_producer([input_dir + record], num_epochs=1) reader = tf.TFRecordReader() _, serialized_example = reader.read(filepaths_queue) features = tf.parse_single_example(serialized_example, features=features_format) new_filepath = output_dir + record writer = tf.python_io.TFRecordWriter(new_filepath) with tf.Session() as sess: init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) counter = 0 try: while True: proc_features, = sess.run([features]) counter += 1
import tensorflow as tf reader = tf.TFRecordReader() filename_queue = tf.train.string_input_producer(["output.tfrecords"]) _, serialized_example = reader.read(filename_queue) #返回文件名和文件 features = tf.parse_single_example(serialized_example, features={ 'image_raw': tf.FixedLenFeature([], tf.string), 'pixels': tf.FixedLenFeature([], tf.int64), 'label': tf.FixedLenFeature([], tf.int64) }) #取出包含image和label的feature对象 images = tf.decode_raw(features['image_raw'], tf.uint8) labels = tf.cast(features['label'], tf.int32) pixels = tf.cast(features['pixels'], tf.int32) sess = tf.Session() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(10): image, label, pixel = sess.run([images, labels, pixels]) print(pixel)
def predict(file_name): num = [] cap = cv2.VideoCapture(file_name) file_name = (file_name.split(".mp4"))[0] ## Creating folder to save all the 100 frames from the video try: os.makedirs("ImageData/testingData/" + file_name) except OSError: print("Error: Creating directory of data") ## Setting the frame limit to 100 cap.set(cv2.CAP_PROP_FRAME_COUNT, 101) length = 101 count = 0 ## Running a loop to each frame and saving it in the created folder while cap.isOpened(): count += 1 if length == count: break _, frame = cap.read() if frame is None: continue ## Resizing it to 256*256 to save the disk space and fit into the model frame = cv2.resize(frame, (256, 256), interpolation=cv2.INTER_CUBIC) # Saves image of the current frame in jpg file name = ("ImageData/testingData/" + str(file_name) + "/frame" + str(count) + ".jpg") cv2.imwrite(name, frame) if cv2.waitKey(1) & 0xFF == ord("q"): break addrs = [] def load_image(addr): img = np.array(Image.open(addr).resize((224, 224), Image.ANTIALIAS)) img = img.astype(np.uint8) return img def _float_feature(value): return tf.train.Feature(float_list=tf.train.FloatList(value=[value])) def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) addrs = [] filelist = glob.glob("ImageData/testingData/" + str(file_name) + "/*.jpg") addrs += filelist train_addrs = addrs train_filename = "test.tfrecords" # address to save the TFRecords file writer = tf.python_io.TFRecordWriter(train_filename) for i in range(len(train_addrs)): # Load the image img = load_image(train_addrs[i]) feature = { "test/image": _bytes_feature(tf.compat.as_bytes(img.tostring())) } # Create an example protocol buffer example = tf.train.Example(features=tf.train.Features(feature=feature)) # Serialize to string and write on the file writer.write(example.SerializeToString()) writer.close() sys.stdout.flush() BATCH_SIZE = 20 REG_PENALTY = 0 NUM_IMAGES = 100 N_EPOCHS = 1 imgs = tf.placeholder("float", [None, 224, 224, 3], name="image_placeholder") gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8, allow_growth=True) config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options) with tf.Session(config=config) as sess: model = DAN(imgs, REG_PENALTY=REG_PENALTY, preprocess="vggface") tr_reader = tf.TFRecordReader() tr_filename_queue = tf.train.string_input_producer(["test.tfrecords"], num_epochs=N_EPOCHS) _, tr_serialized_example = tr_reader.read(tr_filename_queue) tr_feature = {"test/image": tf.FixedLenFeature([], tf.string)} tr_features = tf.parse_single_example(tr_serialized_example, features=tr_feature) tr_image = tf.decode_raw(tr_features["test/image"], tf.uint8) tr_image = tf.reshape(tr_image, [224, 224, 3]) tr_images = tf.train.shuffle_batch( [tr_image], batch_size=BATCH_SIZE, capacity=100, min_after_dequeue=BATCH_SIZE, allow_smaller_final_batch=True, ) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) file_list = ["param1.pkl", "param2.pkl"] epoch = 0 for pickle_file in file_list: error = 0 model.load_trained_model(pickle_file, sess) i = 0 while i < NUM_IMAGES: i += BATCH_SIZE try: epoch_x = sess.run(tr_images) except: if error >= 5: break error += 1 continue output = sess.run([model.output], feed_dict={imgs: epoch_x.astype(np.float32)}) num.append(output[0]) epoch += 1 coord.request_stop() # Wait for threads to stop coord.join(threads) a = np.round(np.mean(np.concatenate(num), axis=0), 3) a_json = { "Extraversion": a[0], "Neuroticism": a[1], "Agreeableness": a[2], "Conscientiousness": a[3], "Openness": a[4], } return a_json
def parse_example_proto( self, example_serialized): #TODO(lowres check this mainly) # Dense features in Example proto. feature_map = { 'image/encoded': tf.VarLenFeature(dtype=tf.string), 'image/speeds': tf.VarLenFeature(dtype=tf.float32), 'image/class/video_name': tf.FixedLenFeature([1], dtype=tf.string, default_value=''), } if FLAGS.only_seg == 1: feature_map.update({ 'image/segmentation': tf.VarLenFeature(dtype=tf.string), 'image/context': tf.VarLenFeature(dtype=tf.string) }) if FLAGS.use_speed_yaw: feature_map.update({ 'sensor/yaw_imu': tf.VarLenFeature(dtype=tf.float32), 'sensor/speed_steer': tf.VarLenFeature(dtype=tf.float32) }) features = tf.parse_single_example(example_serialized, feature_map) # if the data is downsampled by a temporal factor, the starting point should be random, such that we could use # all the data if FLAGS.non_random_temporal_downsample: tstart = 0 else: tstart = tf.random_uniform([], minval=0, maxval=FLAGS.temporal_downsample_factor, dtype=tf.int32) len_downsampled = FLAGS.FRAMES_IN_SEG // FLAGS.temporal_downsample_factor if FLAGS.only_seg == 1: seg = features['image/segmentation'].values[:] seg.set_shape([len_downsampled]) ctx = features['image/context'].values[:] ctx.set_shape([len_downsampled]) name = features['image/class/video_name'] encoded = features['image/encoded'].values[:FLAGS.FRAMES_IN_SEG] encoded_sub = encoded[tstart::FLAGS.temporal_downsample_factor] encoded_sub.set_shape([len_downsampled]) if FLAGS.no_image_input: # no image input is used, but the previous steps is done because # we assume we have an list of empty image inputs decoded = tf.zeros([ len_downsampled, FLAGS.IM_HEIGHT / FLAGS.decode_downsample_factor, FLAGS.IM_WIDTH / FLAGS.decode_downsample_factor, 3 ], tf.uint8) else: decoded = self.decode_jpeg(encoded_sub) if FLAGS.only_seg == 1: seg_decoded = self.decode_png(seg) ctx_decoded = tf.py_func(self.read_array, [ctx], [tf.float32])[0] ctx_decoded.set_shape( [len_downsampled, ctx_channel, ctx_height, ctx_width]) decoded_raw = decoded if FLAGS.resize_images != "": # should have format: new_height, new_width sp_size = FLAGS.resize_images.split(",") assert (len(sp_size) == 2) new_size = (int(sp_size[0]), int(sp_size[1])) decoded = tf.image.resize_bilinear(decoded, new_size) #decoded = tf.image.resize_nearest_neighbor(decoded, new_size) decoded = tf.cast(decoded, tf.uint8) speed = features['image/speeds'].values speed = tf.reshape(speed, [-1, 2]) speed = speed[:FLAGS.FRAMES_IN_SEG, :] speed = speed[tstart::FLAGS.temporal_downsample_factor, :] speed.set_shape([len_downsampled, 2]) # from speed to stop labels stop_label = tf.py_func( self.speed_to_future_has_stop, [speed, FLAGS.stop_future_frames, FLAGS.speed_limit_as_stop], [tf.int32])[0] #TODO(lowres: length of smoothed time) stop_label.set_shape([len_downsampled]) # Note that the turning heuristic is tuned for 3Hz video and urban area # Note also that stop_future_frames is reused for the turn turn = tf.py_func( self.turn_future_smooth, [speed, FLAGS.stop_future_frames, FLAGS.speed_limit_as_stop], [tf.float32])[0] #TODO(lowres) turn.set_shape([len_downsampled, self.naction]) if FLAGS.use_speed_yaw: yaw = features['sensor/yaw_imu'].values spd = features['sensor/speed_steer'].values ys = tf.pack([yaw, spd], axis=1, name="stack_yaw_speed") # Now the shape is N*2 ys = ys[ tstart:FLAGS.FRAMES_IN_SEG:FLAGS.temporal_downsample_factor, :] ys.set_shape([len_downsampled, 2]) # compute locs from ys ys = tf.pad(ys, [[0, FLAGS.stop_future_frames], [0, 0]], mode="SYMMETRIC", name="pad_afterwards") ys = ys[FLAGS.stop_future_frames:, :] ys.set_shape([len_downsampled, 2]) locs = ys print("data loader is using raw yaw and speed") else: # get the relative future location # Note that we again abuse the notation a little bit, reusing stop_future_frames # TODO: normalize the course and speed by time locs = tf.py_func(self.relative_future_course_speed, [ speed, FLAGS.stop_future_frames, FLAGS.frame_rate / FLAGS.temporal_downsample_factor ], [tf.float32])[0] locs.set_shape([len_downsampled, 2]) # batching one 10 second segments into several smaller segments batching_inputs = [decoded, speed, stop_label, turn, locs] if FLAGS.only_seg == 1: batching_inputs += [seg_decoded, ctx_decoded] decoded_raw_loc = 7 else: decoded_raw_loc = 5 batching_inputs += [decoded_raw] batched = [self.batching(x, len_downsampled) for x in batching_inputs] name = tf.tile(name, [batched[0].get_shape()[0].value]) ins = batched[0:2] + [name] outs = batched[2:5] if FLAGS.city_data: # city batch means how many batch does each video sequence forms FLAGS.city_batch = len_downsampled // FLAGS.n_sub_frame # here we want to read in the cityscape data and downsample in the loop city_im_queue, city_seg_queue = self.queue_cityscape( FLAGS.city_image_list, FLAGS.city_label_list) global city_pointer city_pointer = 0 read_n = city_frames * FLAGS.city_batch city_im, city_seg = tf.py_func( self.read_cityscape, [city_im_queue, city_seg_queue, read_n], [tf.float32, tf.int32]) city_im = tf.reshape(city_im, [ FLAGS.city_batch, city_frames, FLAGS.IM_HEIGHT, FLAGS.IM_WIDTH, city_im_channel ]) city_seg = tf.reshape(city_seg, [ FLAGS.city_batch, city_frames, FLAGS.IM_HEIGHT, FLAGS.IM_WIDTH, city_seg_channel ]) if FLAGS.resize_images != "": # should have format: new_height, new_width sp_size = FLAGS.resize_images.split(",") assert (len(sp_size) == 2) new_size = (int(sp_size[0]), int(sp_size[1])) city_im = tf.reshape(city_im, [ FLAGS.city_batch * city_frames, FLAGS.IM_HEIGHT, FLAGS.IM_WIDTH, city_im_channel ]) city_seg = tf.reshape(city_seg, [ FLAGS.city_batch * city_frames, FLAGS.IM_HEIGHT, FLAGS.IM_WIDTH, city_seg_channel ]) city_im = tf.image.resize_bilinear(city_im, new_size) city_seg = tf.image.resize_nearest_neighbor(city_seg, new_size) city_im = tf.reshape(city_im, [ FLAGS.city_batch, city_frames, new_size[0], new_size[1], city_im_channel ]) city_seg = tf.reshape(city_seg, [ FLAGS.city_batch, city_frames, new_size[0], new_size[1], city_seg_channel ]) ins += [city_im] outs += [city_seg] if FLAGS.only_seg == 1: ins = ins + batched[5:7] outs = outs # adding the raw images ins += batched[decoded_raw_loc:(decoded_raw_loc + 1)] # dropout non-stop videos if FLAGS.balance_drop_prob > 0: retained = tf.py_func(self.no_stop_dropout_valid, [outs[0], FLAGS.balance_drop_prob], [tf.bool])[0] retained.set_shape([outs[0].get_shape()[0].value]) select = lambda tensors, valid: [ util.bool_select(x, valid) for x in tensors ] ins = select(ins, retained) outs = select(outs, retained) return ins, outs
def evaluate(): graph = tf.Graph() batch_size = 50 locs = './data/' with graph.as_default(): data_path = [ locs + 'valid_' + str(i) + '.tfrecords' for i in range(2) ] # address to save the hdf5 file feature = { 'image': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64) } # Create a list of filenames and pass it to a queue filename_queue = tf.train.string_input_producer(data_path, num_epochs=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.float32) image = tf.reshape(image, [28, 28, 1]) # Cast label data into int32 label = tf.cast(features['label'], tf.int32) # Reshape image data into the original shape image_valid, label_valid = tf.train.batch([image, label], batch_size=batch_size,\ num_threads=2, capacity=5000, enqueue_many=False) logits = cnn.inference(image_valid, training=False) val_prediction = tf.nn.softmax(logits) correct_prediction = tf.equal( tf.cast(tf.argmax(val_prediction, 1), tf.int32), label_valid) valid_accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) eval_times = 100 with tf.Session(graph=graph) as sess: tf.global_variables_initializer().run() print('Initialized') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) saver = tf.train.Saver() if tf.train.checkpoint_exists('./logs/model2.ckpt'): saver.restore(sess, './logs/model2.ckpt') print('model resored from saved version') valid_accuracies = [] for eval_cnt in range(eval_times): valid_accuracy_val = sess.run([valid_accuracy]) valid_accuracies += valid_accuracy_val #print('set %d, accuracy = %f' %(eval_cnt, valid_accuracy_val[0])) print('average accuracy over %d batches are %f' % (eval_times, np.mean(valid_accuracies))) coord.request_stop() coord.join(threads) print('Finished validation')
def _parse_function(example_proto): features = { "image_string": tf.FixedLenFeature((), tf.string, default_value="") } parsed_features = tf.parse_single_example(example_proto, features) return parsed_features
def read_and_decode(filename): filename_queue = tf.train.string_input_producer([filename], shuffle=True) 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_raw1': tf.FixedLenFeature([], tf.string), 'img_raw2': tf.FixedLenFeature([], tf.string), 'img_raw3': tf.FixedLenFeature([], tf.string), 'img_raw4': tf.FixedLenFeature([], tf.string), 'img_raw5': tf.FixedLenFeature([], tf.string), 'img_raw6': tf.FixedLenFeature([], tf.string), 'img_raw7': tf.FixedLenFeature([], tf.string), 'img_raw8': tf.FixedLenFeature([], tf.string), 'img_raw9': tf.FixedLenFeature([], tf.string), 'img_raw10': tf.FixedLenFeature([], tf.string), 'img_raw11': tf.FixedLenFeature([], tf.string), 'img_raw12': tf.FixedLenFeature([], tf.string), #### }) img = tf.decode_raw(features['img_raw'], tf.uint8) ### img1 = tf.decode_raw(features['img_raw1'], tf.uint8) img2 = tf.decode_raw(features['img_raw2'], tf.uint8) img3 = tf.decode_raw(features['img_raw3'], tf.uint8) img4 = tf.decode_raw(features['img_raw4'], tf.uint8) img5 = tf.decode_raw(features['img_raw5'], tf.uint8) img6 = tf.decode_raw(features['img_raw6'], tf.uint8) img7 = tf.decode_raw(features['img_raw7'], tf.uint8) img8 = tf.decode_raw(features['img_raw8'], tf.uint8) img9 = tf.decode_raw(features['img_raw9'], tf.uint8) img10 = tf.decode_raw(features['img_raw10'], tf.uint8) img11 = tf.decode_raw(features['img_raw11'], tf.uint8) img12 = tf.decode_raw(features['img_raw12'], tf.uint8) ### img = tf.reshape(img, [128, 128, 3]) img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 ##### img1 = tf.reshape(img1, [128, 128, 3]) img1 = tf.cast(img1, tf.float32) * (1. / 255) - 0.5 img2 = tf.reshape(img2, [128, 128, 3]) img2 = tf.cast(img2, tf.float32) * (1. / 255) - 0.5 img3 = tf.reshape(img3, [128, 128, 3]) img3 = tf.cast(img3, tf.float32) * (1. / 255) - 0.5 img4 = tf.cast(img4, tf.float32) * (1. / 255) - 0.5 img4 = tf.reshape(img4, [128, 128, 3]) img5 = tf.cast(img5, tf.float32) * (1. / 255) - 0.5 img5 = tf.reshape(img5, [128, 128, 3]) img6 = tf.cast(img6, tf.float32) * (1. / 255) - 0.5 img6 = tf.reshape(img6, [128, 128, 3]) img7 = tf.cast(img7, tf.float32) * (1. / 255) - 0.5 img7 = tf.reshape(img7, [128, 128, 3]) img8 = tf.reshape(img8, [128, 128, 3]) img8 = tf.cast(img8, tf.float32) * (1. / 255) - 0.5 img9 = tf.reshape(img9, [128, 128, 3]) img9 = tf.cast(img9, tf.float32) * (1. / 255) - 0.5 img10 = tf.reshape(img10, [128, 128, 3]) img10 = tf.cast(img10, tf.float32) * (1. / 255) - 0.5 img11 = tf.reshape(img11, [128, 128, 3]) img11 = tf.cast(img11, tf.float32) * (1. / 255) - 0.5 img12 = tf.reshape(img12, [128, 128, 3]) img12 = tf.cast(img12, tf.float32) * (1. / 255) - 0.5 ##### label = tf.cast(features['label'], tf.int32) print(label) return img, label, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12
def style_image_inputs(style_dataset_file, batch_size=None, image_size=None, square_crop=False, shuffle=True): """Loads a style image at random. Args: style_dataset_file: str, path to the tfrecord dataset of style files. The dataset is produced via the create_style_dataset.py script and is made of Example protobufs with the following features: * 'image_raw': byte encoding of the JPEG string of the style image. * 'label': integer identifier of the style image in [0, N - 1], where N is the number of examples in the dataset. * 'vgg_16/<LAYER_NAME>': Gram matrix at layer <LAYER_NAME> of the VGG-16 network (<LAYER_NAME> in {conv,pool}{1,2,3,4,5}) for the style image. batch_size: int. If provided, batches style images. Defaults to None. image_size: int. The images will be resized bilinearly so that the smallest side has size image_size. Defaults to None. square_crop: bool. If True, square-crops to [image_size, image_size]. Defaults to False. shuffle: bool, whether to shuffle style files at random. Defaults to True. Returns: If batch_size is defined, a 4-D tensor of shape [batch_size, ?, ?, 3] with values in [0, 1] for the style image, and 1-D tensor for the style label. Raises: ValueError: if center cropping is requested but no image size is provided, or if batch size is specified but center-cropping is not requested. """ vgg_layers = [ 'vgg_16/conv1', 'vgg_16/pool1', 'vgg_16/conv2', 'vgg_16/pool2', 'vgg_16/conv3', 'vgg_16/pool3', 'vgg_16/conv4', 'vgg_16/pool4', 'vgg_16/conv5', 'vgg_16/pool5' ] if square_crop and image_size is None: raise ValueError('center-cropping requires specifying the image size.') if batch_size is not None and not square_crop: raise ValueError('batching requires center-cropping.') with tf.name_scope('style_image_processing'): filename_queue = tf.train.string_input_producer([style_dataset_file], shuffle=False, capacity=1, name='filename_queue') if shuffle: examples_queue = tf.RandomShuffleQueue( capacity=64, min_after_dequeue=32, dtypes=[tf.string], name='random_examples_queue') else: examples_queue = tf.FIFOQueue(capacity=64, dtypes=[tf.string], name='fifo_examples_queue') reader = tf.TFRecordReader() _, value = reader.read(filename_queue) enqueue_ops = [examples_queue.enqueue([value])] tf.train.queue_runner.add_queue_runner( tf.train.queue_runner.QueueRunner(examples_queue, enqueue_ops)) example_serialized = examples_queue.dequeue() features = tf.parse_single_example( example_serialized, features={ 'label': tf.FixedLenFeature([], tf.int64), 'image_raw': tf.FixedLenFeature([], tf.string), 'vgg_16/conv1': tf.FixedLenFeature([64, 64], tf.float32), 'vgg_16/pool1': tf.FixedLenFeature([64, 64], tf.float32), 'vgg_16/conv2': tf.FixedLenFeature([128, 128], tf.float32), 'vgg_16/pool2': tf.FixedLenFeature([128, 128], tf.float32), 'vgg_16/conv3': tf.FixedLenFeature([256, 256], tf.float32), 'vgg_16/pool3': tf.FixedLenFeature([256, 256], tf.float32), 'vgg_16/conv4': tf.FixedLenFeature([512, 512], tf.float32), 'vgg_16/pool4': tf.FixedLenFeature([512, 512], tf.float32), 'vgg_16/conv5': tf.FixedLenFeature([512, 512], tf.float32), 'vgg_16/pool5': tf.FixedLenFeature([512, 512], tf.float32) }) image = tf.image.decode_jpeg(features['image_raw']) label = features['label'] gram_matrices = [features[vgg_layer] for vgg_layer in vgg_layers] image.set_shape([None, None, 3]) if image_size: if square_crop: image = _aspect_preserving_resize(image, image_size + 2) image = _central_crop([image], image_size, image_size)[0] image.set_shape([image_size, image_size, 3]) else: image = _aspect_preserving_resize(image, image_size) image = tf.to_float(image) / 255.0 if batch_size is None: image = tf.expand_dims(image, 0) else: image_label_gram_matrices = tf.train.batch([image, label] + gram_matrices, batch_size=batch_size) image, label = image_label_gram_matrices[:2] gram_matrices = image_label_gram_matrices[2:] gram_matrices = dict([ (vgg_layer, gram_matrix) for vgg_layer, gram_matrix in zip(vgg_layers, gram_matrices) ]) return image, label, gram_matrices
# 有提供训练数据的TFRcord文件 files = tf.train.match_filenames_once( "/home/shenxj/tf-work/datasets/file_pattern-*") filename_queue = tf.train.string_input_producer(files, shuffle=False) # 使用类似7.1节中结婚嫂的方法解析TFRecord文件里的数据。这里假设image中存储的是图像 # 的原始数据,label为该样例所对应的标签。height,width和channels给出了图像的维度。 reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, features={ 'image': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64), 'height': tf.FixedLenFeature([], tf.int64), 'weigth': tf.FixedLenFeature([], tf.int64), 'channels': tf.FixedLenFeature([], tf.int64), }) image, label = features['image'], features['label'] height, width = features['height'], features['wigth'] channels = features['channels'] # 从原始图像数据解析出像素矩阵,并根据图像尺寸还原图像 decoded_image = tf.decode_raw(image, tf.uint8) decoded_image.set_shape([height, width, channels]) # 定义神经网络输入层图片的大小。 image_size = 299
def dataset_parser(self, value): """Parse an ImageNet record from a serialized string Tensor.""" if self.is_training: keys_to_features = { 'image': tf.FixedLenFeature((), tf.string), 'label': tf.FixedLenFeature([], tf.int64), 'heatmap': tf.FixedLenFeature([], tf.string, default_value=''), 'click_count': tf.FixedLenFeature([], tf.int64), 'bbox': tf.FixedLenFeature( [4], dtype=tf.int64, default_value=[0, 0, 0, 0]) } parsed = tf.parse_single_example(value, keys_to_features) ccount = tf.cast( tf.reshape(parsed['click_count'], shape=[]), dtype=tf.int32) image_bytes = tf.reshape(parsed['image'], shape=[]) hm_bytes = tf.reshape(parsed['heatmap'], shape=[]) bbox = tf.cast(tf.reshape(parsed['bbox'], shape=[4]), tf.int64) image, hm, bbox = self.image_preprocessing_fn( image_bytes=image_bytes, hm_bytes=hm_bytes, bbox=bbox, is_training=self.is_training, ) if self.use_bfloat: image = tf.cast(image, tf.bfloat16) hm = tf.cast(hm, tf.bfloat16) bbox = tf.cast(bbox, tf.bfloat16) image = {'image': image, 'hm': hm, 'bbox': bbox, 'ccount': ccount} else: keys_to_features = { 'image': tf.FixedLenFeature((), tf.string), 'label': tf.FixedLenFeature([], tf.int64), # 'heatmap': # tf.FixedLenFeature([], tf.string), # 'bbox': # tf.FixedLenFeature([4], dtype=tf.int64) } parsed = tf.parse_single_example(value, keys_to_features) image_bytes = tf.reshape(parsed['image'], shape=[]) # hm_bytes = tf.reshape(parsed['heatmap'], shape=[]) # bbox = tf.reshape(parsed['bbox'], shape=[4]) image = self.image_preprocessing_fn( image_bytes=image_bytes, hm_bytes=None, bbox=None, is_training=self.is_training, ) if self.use_bfloat: image = tf.cast(image, tf.bfloat16) # Subtract one so that labels are in [0, 1000). label = tf.cast( tf.reshape(parsed['label'], shape=[]), dtype=tf.int32) return image, label
'image/width': tf.FixedLenFeature((), tf.int64), 'image/filename': tf.FixedLenFeature((), tf.string), 'image/source_id': tf.FixedLenFeature((), tf.string), 'image/encoded': tf.FixedLenFeature((), tf.string), 'image/format': tf.FixedLenFeature((), tf.string), 'image/object/bbox/xmin': tf.VarLenFeature(tf.float32), 'image/object/bbox/xmax': tf.VarLenFeature(tf.float32), 'image/object/bbox/ymin': tf.VarLenFeature(tf.float32), 'image/object/bbox/ymax': tf.VarLenFeature(tf.float32), 'image/object/class/text': tf.VarLenFeature(tf.string), 'image/object/class/label': tf.VarLenFeature(tf.int64), }) 41 features=tf.parse_single_example(example,features={'image_raw':#解码 42 tf.FixedLenFeature([],tf.string), 43 'label':tf.FixedLenFeature([34,1],tf.int64)}) 44 image=tf.decode_raw(features['image_raw'],tf.uint8) 45 image.set_shape(rows*cols) 46 image=tf.cast(image,tf.float32)*(1./255)-0.5 47 label=tf.cast(features['label'],tf.int32) 48 return image,label 49 #定义解析数据函数 #入参example_proto也就是tf_serialized def pares_tf(example_proto): #定义解析的字典 dics = {} dics['label'] = tf.FixedLenFeature(shape=[],dtype=tf.int64)
def _parse_function(example_proto): features = {'adjs': tf.FixedLenFeature((), tf.string)} parsed_features = tf.parse_single_example(example_proto, features) data = tf.decode_raw(parsed_features['adjs'], tf.int32) return data
# 创建一个 reader 来读取 TFRecord 文件中的样例 reader = tf.TFRecordReader() # 创建一个队列来维护输入文件列表,在7.3.2小节中将更加详细的介绍 # tf.train.string_input_producer 函数 filename_queue = tf.train.string_input_producer(["build/output.tfrecords"]) # 从文件中读出一个样例。也可以使用 read_up_to 函数一次性读取多个样例 _, serialized_example = reader.read(filename_queue) # 解析读取的样例。如果需要解析多个样例,可以用 parse_example 函数 features = tf.parse_single_example( serialized_example, features={ # TensorFlow 提供两种不同属性解析方法。一种是方法是 tf.FixedLenFeature, # 这种方法解析的结果为一个 Tensor。另一种方法是 tf.VarLenFeature,这种方法 # 得到的解析结果为 SparseTensor,用于处理稀疏数据。这里解析数据的格式需要和 # 上面程序写入数据的格式一致 'image_raw': tf.FixedLenFeature([], tf.string), 'pixels': tf.FixedLenFeature([], tf.int64), 'label': tf.FixedLenFeature([], tf.int64) }) # tf.decode_raw 可以将字符串解析成图像对应的像素数组 images = tf.decode_raw(features['image_raw'], tf.uint8) labels = tf.cast(features['label'], tf.int32) pixels = tf.cast(features['pixels'], tf.int32) sess = tf.Session() # 启动多线程处理输入数据,7.3节将更加详细地介绍 TensorFlow 多线程处理 coord = tf.train.Coordinator()
def _parse_dtype(self, serialized): sample = tf.parse_single_example(serialized, features=self._get_dtype_features()) return sample
import tensorflow as tf # 使用tf.train.match_filenames_once函数获取文件列表 files = tf.train.match_filenames_once('./data.tfrecords-*') # 通过tf.train.string_input_producer函数创建输入队列,输入队列中的文件列表为 # tf.train.match_filenames_once函数获取的文件列表。这里将shuffle参数设为False # 来避免随机打乱读文件的顺序。但一般在解决真实问题时,会将shuffle参数设置为True filename_queue = tf.train.string_input_producer(files, shuffle=False) # 如前面所示读取并解析一个样本 reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, features={ 'i': tf.FixedLenFeature([], tf.int64), 'j': tf.FixedLenFeature([], tf.int64), }) # 使用前面的方法读取并解析得到的样例。这里假设Example结构中i表示一个样例的特征向量 # 比如一张图像的像素矩阵。而j表示该样例对应的标签。 example, label = features['i'], features['j'] # 一个batch中样例的个数。 batch_size = 3 # 组合样例的队列中最多可以存储的样例个数。这个队列如果太大,那么需要占用很多内存资源; # 如果太小,那么出队操作可能会因为没有数据而被阻碍(block),从而导致训练效率降低。 # 一般来说这个队列的大小会和每一个batch的大小相关,下面一行代码给出了设置队列大小的一种方式。 capacity = 1000 + 3 * batch_size # 使用tf.train.batch函数来组合样例。[example, label]参数给出了需要组合的元素,
def test(): reader = tf.TFRecordReader() _, serialized_example = reader.read( filename_queue) # return file_name and file features = tf.parse_single_example(serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img': tf.FixedLenFeature([], tf.string), 'width': tf.FixedLenFeature([], tf.int64), 'height': tf.FixedLenFeature([], tf.int64), 'channels': tf.FixedLenFeature([], tf.int64) }) # return image and label label = tf.cast(features['label'], tf.float32) # throw label tensor # height = tf.cast(features['height'],tf.int32) height = features['height'] width = tf.cast(features['width'], tf.int32) channels = tf.cast(features['channels'], tf.int32) img = tf.decode_raw(features['img'], tf.uint8) print(type(height)) img = tf.reshape(img, [227, 227, 3]) img_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size=1000, capacity=5173, min_after_dequeue=5172, num_threads=4) x = tf.placeholder(tf.float32, [ None, train_net.IMAGE_SIZE, train_net.IMAGE_SIZE, train_net.NUM_CHANNELS ], name='x-input') y_ = tf.placeholder(tf.int64, [None], name='y-input') # validata_feed = {x: reshape_xs, y_: mnist.validation.labels} y = inference.alex_net(X=x, output=2, dropout=train_net.DROPOUT, regularizer=None) #tf.argmax()返回向量中最大值位置,tf.equal()返回两个向量对应位置比较结果 返回值为布尔类型 correct_prediction = tf.equal(tf.argmax(y, 1), y_) #数据类型转换 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # variable_averages = tf.train.ExponentialMovingAverage(train_net.MOVING_AVERAGE_DECAY) # #加载变量的滑动平均值 # saver = tf.train.Saver(variable_averages.variables_to_restore()) #加载保存模型的变量 saver = tf.train.Saver() with tf.Session() as sess: #返回模型变量取值的路径 tf.global_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) xs, ys = sess.run([img_batch, label_batch]) ckpt = tf.train.get_checkpoint_state(train_net.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: #ckpt.model_checkpoint_path返回最新的模型变量取值的路径 saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] print('test accuracy is %g' % (sess.run(accuracy, feed_dict={ x: xs, y_: ys }))) else: print('NO checkpoint file found') return
def parse_tf_example(serialized, stage=""): """Parses a tensorflow.SequenceExample into an image and caption. Args: serialized: A scalar string Tensor; a single serialized TF Example. stage; If "tps_points", return a different set of variables. Returns: encoded_image: A scalar string Tensor containing a JPEG encoded image. encoded_prod_image: A JPEG encoded image string of the product image. body_segment: A h X w [0,1] Tensor indicating the body part. product_segment: A h X w [0,1] Tensor indicating the clothing part. skin_segment: A h X w Tensor indicating the skin part. pose_map: A 256 X 256 * 18 Tensor indicating pose. """ features = tf.parse_single_example(serialized, features={ "image_id": tf.FixedLenFeature([], tf.string), "height": tf.FixedLenFeature([], tf.int64), "width": tf.FixedLenFeature([], tf.int64), "image": tf.FixedLenFeature([], tf.string), "product_image": tf.FixedLenFeature([], tf.string), "pose_map": tf.FixedLenFeature([], tf.string), "segment_map": tf.FixedLenFeature([], tf.string), "tps_control_points": tf.VarLenFeature(tf.float32), "num_keypoints": tf.FixedLenFeature([], tf.int64), "keypoints": tf.VarLenFeature(tf.float32), "prod_keypoints": tf.VarLenFeature(tf.float32), }) encoded_product_image = features["product_image"] encoded_image = features["image"] height = tf.cast(features["height"], tf.int32) width = tf.cast(features["width"], tf.int32) pose_map = tf.decode_raw(features["pose_map"], tf.uint8) pose_map = tf.cast(pose_map, tf.float32) pose_map = tf.reshape(pose_map, tf.stack([256, 192, 18])) segment_map = tf.decode_raw(features["segment_map"], tf.uint8) segment_map = tf.reshape(segment_map, tf.stack([height, width])) body_segment, prod_segment, skin_segment = extract_segmentation( segment_map) if stage != "tps_points": return (encoded_image, encoded_product_image, body_segment, prod_segment, skin_segment, pose_map, features["image_id"]) # TPS control points reshape tps_points = features["tps_control_points"] tps_points = tf.sparse_tensor_to_dense(tps_points, default_value=0.) tps_points = tf.reshape(tps_points, tf.stack([2, 10, 10])) tps_points = tf.transpose(tf.reshape(tps_points, tf.stack([2, 100 ]))) * 2 - 1 return (encoded_image, encoded_product_image, body_segment, prod_segment, skin_segment, pose_map, features["image_id"], tps_points)
def decode(elem): model_features = tf.parse_single_example(elem, features=feature_spec) model_labels = tf.stack([model_features.pop(label) for label in labels]) return model_features, model_labels
def read_tfrecord(record_file): reader = tf.TFRecordReader() _, example = reader.read(record_file) features = tf.parse_single_example( example, features={'image': tf.FixedLenFeature([], tf.string)})
def read_data(filename_queue, is_train): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, features={ 'name': tf.FixedLenFeature([], tf.string), 'm': tf.FixedLenFeature([], tf.int64), 'n': tf.FixedLenFeature([], tf.int64), 'query': tf.FixedLenFeature([], tf.string), 'align': tf.FixedLenFeature([], tf.string), 'y': tf.FixedLenFeature([], tf.string), 'mask': tf.FixedLenFeature([], tf.string), 'gap': tf.VarLenFeature(tf.float32), 'identity': tf.VarLenFeature(tf.float32), 'identity_cons': tf.VarLenFeature(tf.float32), 'ss_dssp': tf.FixedLenFeature([], tf.string), 'asa_num': tf.VarLenFeature(tf.int64), }) name = features["name"] m = tf.cast(features["m"], tf.int32) n = tf.cast(features["n"], tf.int32) align = tf.reshape(tf.decode_raw(features["align"], tf.uint8), tf.stack([m, n])) query = tf.decode_raw(features["query"], tf.uint8) y = tf.reshape(tf.decode_raw(features["y"], tf.uint8), tf.stack([n, n])) mask = tf.reshape(tf.decode_raw(features["mask"], tf.uint8), tf.stack([n, n])) gap = features["gap"].values identity = features["identity"].values identity_cons = features["identity_cons"].values ss_dssp = tf.decode_raw(features["ss_dssp"], tf.uint8) asa_num = tf.cast(features["asa_num"].values, tf.int32) gap = features["gap"].values identity = features["identity"].values identity_cons = features["identity_cons"].values # clip def clipping(align, query, ss_dssp, asa_num, y, mask): begin = tf.random_uniform([], maxval=tf.shape(align)[1] - n_clip, dtype=tf.int32) align = align[:, begin:begin + n_clip] query = query[begin:begin + n_clip] ss_dssp = ss_dssp[begin:begin + n_clip] asa_num = asa_num[begin:begin + n_clip] y = y[begin:begin + n_clip, begin:begin + n_clip] mask = mask[begin:begin + n_clip, begin:begin + n_clip] return align, query, ss_dssp, asa_num, y, mask align, query, ss_dssp, asa_num, y, mask = tf.cond( (n > n_clip) & (is_train), lambda: clipping(align, query, ss_dssp, asa_num, y, mask), lambda: (align, query, ss_dssp, asa_num, y, mask)) # sampling def sampling(align, gap, identity, identity_cons): idx = tf.random_uniform([n_alignment], maxval=m, dtype=tf.int32) align = tf.gather_nd(align, tf.expand_dims(idx, 1)) gap = tf.gather_nd(gap, tf.expand_dims(idx, 1)) identity = tf.gather_nd(identity, tf.expand_dims(idx, 1)) identity_cons = tf.gather_nd(identity_cons, tf.expand_dims(idx, 1)) return align, gap, identity, identity_cons align, gap, identity, identity_cons = tf.cond( (m > n_alignment) & (is_train), lambda: sampling(align, gap, identity, identity_cons), lambda: (align, gap, identity, identity_cons)) return name, align, query, y, mask, gap, identity, identity_cons, ss_dssp, asa_num
def _parse_example_proto(example_serialized): """Parses an Example proto containing a training example of an image. The output of the build_image_data.py image preprocessing script is a dataset containing serialized Example protocol buffers. Each Example proto contains the following fields: image/height: 462 image/width: 581 image/colorspace: 'RGB' image/channels: 3 image/class/label: 615 image/class/synset: 'n03623198' image/class/text: 'knee pad' image/object/bbox/xmin: 0.1 image/object/bbox/xmax: 0.9 image/object/bbox/ymin: 0.2 image/object/bbox/ymax: 0.6 image/object/bbox/label: 615 image/format: 'JPEG' image/filename: 'ILSVRC2012_val_00041207.JPEG' image/encoded: <JPEG encoded string> Args: example_serialized: scalar Tensor tf.string containing a serialized Example protocol buffer. Returns: image_buffer: Tensor tf.string containing the contents of a JPEG file. label: Tensor tf.int32 containing the label. bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] where each coordinate is [0, 1) and the coordinates are arranged as [ymin, xmin, ymax, xmax]. text: Tensor tf.string containing the human-readable label. """ # 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), 'image/class/text': tf.FixedLenFeature([], dtype=tf.string, default_value=''), } sparse_float32 = tf.VarLenFeature(dtype=tf.float32) # Sparse features in Example proto. feature_map.update({ k: sparse_float32 for k in [ 'image/object/bbox/xmin', 'image/object/bbox/ymin', 'image/object/bbox/xmax', 'image/object/bbox/ymax' ] }) features = tf.parse_single_example(example_serialized, feature_map) label = tf.cast(features['image/class/label'], dtype=tf.int32) 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_v2([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]) return features['image/encoded'], label, bbox, features['image/class/text']
def predictor2(image_tru, num_face): # image = align_face(image_tru) image = image_tru ######**********************************************************##### # Encode data_file = 'tmp_aug.tfrecords' writer = tf.python_io.TFRecordWriter(data_file) # image = load_image('./') gender = np.array([0]) race = np.array([0]) age = np.array([0]) addrs = ['0'] feature = { 'val/gender': _int64_feature(gender.astype(np.int8)), 'val/race': _int64_feature(race.astype(np.int8)), 'val/age': _int64_feature(age.astype(np.int8)), 'val/image': _bytes_feature(tf.compat.as_bytes(image.tostring())), 'val/address': _bytes_feature(os.path.basename(addrs[0].encode())) } example = tf.train.Example(features=tf.train.Features(feature=feature)) writer.write(example.SerializeToString()) writer.close() sys.stdout.flush() # decode tf.reset_default_graph() filename_queue = tf.train.string_input_producer([data_file]) reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, features={ 'val/gender': tf.FixedLenFeature([], tf.int64), 'val/race': tf.FixedLenFeature([], tf.int64), 'val/age': tf.FixedLenFeature([], tf.int64), 'val/image': tf.FixedLenFeature([], tf.string), 'val/address': tf.FixedLenFeature([], tf.string) }) image = tf.decode_raw(features['val/image'], tf.float32) image = tf.cast(image, tf.uint8) image.set_shape([200 * 200 * 3]) image = tf.reshape(image, [200, 200, 3]) # image = tf.reverse_v2(image, [-1]) image = tf.image.per_image_standardization(image) images = tf.train.shuffle_batch([image], batch_size=1, capacity=256, num_threads=2, min_after_dequeue=32) train_mode = tf.placeholder(tf.bool) logits_gender, logits_race, logits_age, end_points, _ = build_model( images, train_mode) end_points['Predictions/gender'] = tf.nn.softmax(logits_gender, name='Predictions/gender') end_points['Predictions/race'] = tf.nn.softmax(logits_race, name='Predictions/race') end_points['Predictions/age'] = tf.nn.softmax(logits_age, name='Predictions/age') predictions1 = tf.argmax(end_points['Predictions/gender'], -1) predictions2 = tf.argmax(end_points['Predictions/race'], -1) predictions3 = tf.argmax(end_points['Predictions/age'], -1) pr1 = tf.to_float(tf.to_int32(predictions1)) pr2 = tf.to_float(tf.to_int32(predictions2)) pr3 = tf.to_float(tf.to_int32(predictions3)) with tf.Session() as sess: # # init_op = tf.group(tf.local_variables_initializer()) # sess.run(init_op) saver = tf.train.Saver(max_to_keep=100) ckpt = tf.train.get_checkpoint_state(model_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("restore and start evaluation!") coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) # writer = tf.summary.FileWriter(log_dir, graph=tf.get_default_graph()) pred1, pred2 = [], [] # for step in range(num_steps_per_epoch): acc1, acc2, acc3 = sess.run([pr1, pr2, pr3], {train_mode: False}) print(acc1, acc2, acc3) # Log some information # logging.info('Step %s: gender Accuracy: %.4f race Accuracy: %.4f loss: %.4f (%.2f sec/step)'step, acc1, acc2, l, time_elapsed) # writer.add_summary(curr_summary, step) # pred1.append(acc1) # pred2.append(acc2) # coord.request_stop() # coord.join(threads) # saver.save(sess, final_checkpoint_file) sess.close() # average_acc1 = np.mean(accuracies1) # average_acc2 = np.mean(accuracies2) # average_loss = np.mean(loss_list) gender = int(acc1) race = int(acc2) age = int(acc3) global Result Result = [gender, race, age, num_face]
def get_split(dataset_dir, is_training, split_name, batch_size, seq_length, buffer_size, subset=None): root_path = Path(dataset_dir) / split_name if subset is None: paths = [str(x) for x in root_path.glob('*.tfrecords')] else: paths = [str(x) for x in root_path.glob('*.tfrecords')] if split_name == "test": dataset = tf.data.TFRecordDataset(paths) dataset = dataset.map(lambda x: tf.parse_single_example( x, features={ 'sample_id': tf.FixedLenFeature([], tf.int64), 'subject_id': tf.FixedLenFeature([], tf.int64), 'gs_label': tf.FixedLenFeature([], tf.string), 'gs_label_shape': tf.FixedLenFeature([], tf.string), 'raw_audio': tf.FixedLenFeature([], tf.string), 'raw_audio_shape': tf.FixedLenFeature([], tf.string), 'audio_features': tf.FixedLenFeature([], tf.string), 'audio_features_shape': tf.FixedLenFeature([], tf.string), 'image_features_appearance': tf.FixedLenFeature([], tf.string), 'image_features_appearance_shape': tf.FixedLenFeature([], tf.string), 'image_features_geometric': tf.FixedLenFeature([], tf.string), 'image_features_geometric_shape': tf.FixedLenFeature([], tf.string), })) dataset = dataset.map( lambda x: { 'sample_id': tf.reshape(x['sample_id'], (1, )), 'subject_id': tf.reshape(x['subject_id'], (1, )), 'gs_label': tf.reshape(tf.decode_raw(x['gs_label'], tf.float32), (2, )), 'gs_label_shape': tf.reshape(tf.decode_raw(x['gs_label_shape'], tf.float32), (2, )), 'raw_audio': tf.reshape(tf.decode_raw(x['raw_audio'], tf.float32), (640, )), 'raw_audio_shape': tf.reshape(tf.decode_raw(x['raw_audio_shape'], tf.float32), (2, )), 'audio_features': tf.reshape(tf.decode_raw(x['audio_features'], tf.float32), (534, )), # 178, 534 'audio_features_shape': tf.reshape( tf.decode_raw(x['audio_features_shape'], tf.float32), (2, )), 'image_features_appearance': tf.reshape( tf.decode_raw(x['image_features_appearance'], tf.float32), (1014, )), # 338, 1014 'image_features_appearance_shape': tf.reshape( tf.decode_raw(x['image_features_appearance_shape'], tf. float32), (2, )), 'image_features_geometric': tf.reshape( tf.decode_raw(x['image_features_geometric'], tf.float32), (3798, )), # 1266, 3798 'image_features_geometric_shape': tf.reshape( tf.decode_raw(x['image_features_geometric_shape'], tf. float32), (2, )) }) else: dataset = tf.data.TFRecordDataset(paths) dataset = dataset.map(lambda x: tf.parse_single_example( x, features={ 'sample_id': tf.FixedLenFeature([], tf.int64), 'subject_id': tf.FixedLenFeature([], tf.int64), 'label': tf.FixedLenFeature([], tf.string), 'label_shape': tf.FixedLenFeature([], tf.string), 'raw_audio': tf.FixedLenFeature([], tf.string), 'raw_audio_shape': tf.FixedLenFeature([], tf.string), 'audio_features': tf.FixedLenFeature([], tf.string), 'audio_features_shape': tf.FixedLenFeature([], tf.string), 'image_features_appearance': tf.FixedLenFeature([], tf.string), 'image_features_appearance_shape': tf.FixedLenFeature([], tf.string), 'image_features_geometric': tf.FixedLenFeature([], tf.string), 'image_features_geometric_shape': tf.FixedLenFeature([], tf.string), })) dataset = dataset.map( lambda x: { 'sample_id': tf.reshape(x['sample_id'], (1, )), 'subject_id': tf.reshape(x['subject_id'], (1, )), 'label': tf.reshape(tf.decode_raw(x['label'], tf.float32), (12, )), 'label_shape': tf.reshape(tf.decode_raw(x['label_shape'], tf.float32), (2, )), 'raw_audio': tf.reshape(tf.decode_raw(x['raw_audio'], tf.float32), (640, )), 'raw_audio_shape': tf.reshape(tf.decode_raw(x['raw_audio_shape'], tf.float32), (2, )), 'audio_features': tf.reshape(tf.decode_raw(x['audio_features'], tf.float32), (534, )), # 178, 534 'audio_features_shape': tf.reshape( tf.decode_raw(x['audio_features_shape'], tf.float32), (2, )), 'image_features_appearance': tf.reshape( tf.decode_raw(x['image_features_appearance'], tf.float32), (1014, )), # 338, 1014 'image_features_appearance_shape': tf.reshape( tf.decode_raw(x['image_features_appearance_shape'], tf. float32), (2, )), 'image_features_geometric': tf.reshape( tf.decode_raw(x['image_features_geometric'], tf.float32), (3798, )), # 1266, 3798 'image_features_geometric_shape': tf.reshape( tf.decode_raw(x['image_features_geometric_shape'], tf. float32), (2, )) }) dataset = dataset.repeat() dataset = dataset.batch(seq_length) if is_training: dataset = dataset.shuffle(buffer_size=buffer_size) dataset = dataset.batch(batch_size) return dataset
def decode_tfrecord(datum): return tf.parse_single_example(datum, features=feature_def)