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 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 parse_tf_example(example, is_training, image_size): features = { 'image/label': tf.FixedLenFeature([], dtype=tf.int64), 'image/encoded': tf.FixedLenFeature([], dtype=tf.string) } parsed = tf.parse_single_example(serialized=example, features=features) label = parsed['image/label'] encode_image = parsed['image/encoded'] # return image of [0,1),shape(?,?,3) image = preprocessing.decode_image(encode_image) # return image of [-1,1),need 4-D of input,but shape(?,?,3) is also ok from my test. image = preprocessing.preprocess_image( image, is_training=is_training, height=image_size[0], width=image_size[1], min_scale=0.8, max_scale=1, p_scale_up=0.5, aug_color=True, ) return image, label