def make_data_provider(self, **kwargs): splitter_source = split_tokens_decoder.SplitTokensDecoder( tokens_feature_name="source_tokens", length_feature_name="source_len", append_token="SEQUENCE_END", delimiter=self.params["source_delimiter"]) splitter_target = split_tokens_decoder.SplitTokensDecoder( tokens_feature_name="target_tokens", length_feature_name="target_len", prepend_token="SEQUENCE_START", append_token="SEQUENCE_END", delimiter=self.params["target_delimiter"]) keys_to_features = { self.params["source_field"]: tf.FixedLenFeature((), tf.string), self.params["target_field"]: tf.FixedLenFeature((), tf.string, default_value="") } items_to_handlers = {} items_to_handlers[ "source_tokens"] = tfexample_decoder.ItemHandlerCallback( keys=[self.params["source_field"]], func=lambda dict: splitter_source.decode( dict[self.params["source_field"]], ["source_tokens"])[0]) items_to_handlers[ "source_len"] = tfexample_decoder.ItemHandlerCallback( keys=[self.params["source_field"]], func=lambda dict: splitter_source.decode( dict[self.params["source_field"]], ["source_len"])[0]) items_to_handlers[ "target_tokens"] = tfexample_decoder.ItemHandlerCallback( keys=[self.params["target_field"]], func=lambda dict: splitter_target.decode( dict[self.params["target_field"]], ["target_tokens"])[0]) items_to_handlers[ "target_len"] = tfexample_decoder.ItemHandlerCallback( keys=[self.params["target_field"]], func=lambda dict: splitter_target.decode( dict[self.params["target_field"]], ["target_len"])[0]) decoder = tfexample_decoder.TFExampleDecoder(keys_to_features, items_to_handlers) dataset = tf.contrib.slim.dataset.Dataset( data_sources=self.params["files"], reader=tf.TFRecordReader, decoder=decoder, num_samples=None, items_to_descriptions={}) return tf.contrib.slim.dataset_data_provider.DatasetDataProvider( dataset=dataset, shuffle=self.params["shuffle"], num_epochs=self.params["num_epochs"], **kwargs)
def make_data_provider(self, **kwargs): data_files = [] tf.logging.info(self.params["file_input_pattern"].split(",")) for pattern in self.params["file_input_pattern"].split(","): data_files.extend(tf.gfile.Glob(pattern)) if not data_files: tf.logging.fatal("Found no input files matching %s", self.params["file_input_pattern"]) else: tf.logging.info("Prefetching values from %d files matching %s", len(data_files), self.params["file_input_pattern"]) splitter_target = split_tokens_decoder.SplitTokensDecoder( tokens_feature_name="target_tokens", length_feature_name="target_len", prepend_token="SEQUENCE_START", append_token="SEQUENCE_END", delimiter=self.params["target_delimiter"]) context_keys_to_features = { self.params["caption_tokens_field"]: tf.FixedLenFeature( [], dtype=tf.string), } sequence_keys_to_features = { self.params["video_field"]: tf.FixedLenSequenceFeature( [], dtype=tf.string), } items_to_handlers = { "source_tokens": tfexample_decoder.Tensor(self.params["video_field"]), "source_len": tfexample_decoder.ItemHandlerCallback( keys=[self.params["video_field"]], func=lambda x: tf.size(x[self.params["video_field"]])), "target_tokens": tfexample_decoder.ItemHandlerCallback( keys=[self.params["caption_tokens_field"]], func=lambda dict: splitter_target.decode( dict[self.params["caption_tokens_field"]], ["target_tokens"])[0]), "target_len": tfexample_decoder.ItemHandlerCallback( keys=[self.params["caption_tokens_field"]], func=lambda dict: splitter_target.decode( dict[self.params["caption_tokens_field"]], ["target_len"])[0]) } decoder = TFSEquenceExampleDecoder(context_keys_to_features, sequence_keys_to_features, items_to_handlers) dataset = tf.contrib.slim.dataset.Dataset( data_sources=data_files, reader=tf.TFRecordReader, decoder=decoder, num_samples=None, items_to_descriptions={}) return tf.contrib.slim.dataset_data_provider.DatasetDataProvider( dataset=dataset, shuffle=self.params["shuffle"], num_epochs=self.params["num_epochs"], **kwargs)
def testDecodeImageWithItemHandlerCallback(self): image_shape = (2, 3, 3) for image_encoding in ['jpeg', 'png']: image, serialized_example = self.GenerateImage( image_format=image_encoding, image_shape=image_shape) with self.test_session(): def ConditionalDecoding(keys_to_tensors): """See base class.""" image_buffer = keys_to_tensors['image/encoded'] image_format = keys_to_tensors['image/format'] def DecodePng(): return image_ops.decode_png(image_buffer, 3) def DecodeJpg(): return image_ops.decode_jpeg(image_buffer, 3) image = control_flow_ops.case( { math_ops.equal(image_format, 'png'): DecodePng, }, default=DecodeJpg, exclusive=True) image = array_ops.reshape(image, image_shape) return image keys_to_features = { 'image/encoded': parsing_ops.FixedLenFeature((), dtypes.string, default_value=''), 'image/format': parsing_ops.FixedLenFeature((), dtypes.string, default_value='jpeg') } items_to_handlers = { 'image': tfexample_decoder.ItemHandlerCallback( ['image/encoded', 'image/format'], ConditionalDecoding) } decoder = tfexample_decoder.TFExampleDecoder( keys_to_features, items_to_handlers) [tf_image] = decoder.decode(serialized_example, ['image']) decoded_image = tf_image.eval() if image_encoding == 'jpeg': # For jenkins: image = image.astype(np.float32) decoded_image = decoded_image.astype(np.float32) self.assertAllClose(image, decoded_image, rtol=.5, atol=1.001) else: self.assertAllClose(image, decoded_image, atol=0)
def make_data_provider(self, **kwargs): context_keys_to_features = { self.params["image_field"]: tf.FixedLenFeature([], dtype=tf.string), "image/format": tf.FixedLenFeature([], dtype=tf.string, default_value=self.params["image_format"]), } sequence_keys_to_features = { self.params["caption_ids_field"]: tf.FixedLenSequenceFeature([], dtype=tf.int64), self.params["caption_tokens_field"]: tf.FixedLenSequenceFeature([], dtype=tf.string) } items_to_handlers = { "image": tfexample_decoder.Image(image_key=self.params["image_field"], format_key="image/format", channels=3), "target_ids": tfexample_decoder.Tensor(self.params["caption_ids_field"]), "target_tokens": tfexample_decoder.Tensor(self.params["caption_tokens_field"]), "target_len": tfexample_decoder.ItemHandlerCallback( keys=[self.params["caption_tokens_field"]], func=lambda x: tf.size(x[self.params["caption_tokens_field"]])) } decoder = TFSEquenceExampleDecoder(context_keys_to_features, sequence_keys_to_features, items_to_handlers) dataset = tf.contrib.slim.dataset.Dataset( data_sources=self.params["files"], reader=tf.TFRecordReader, decoder=decoder, num_samples=None, items_to_descriptions={}) return tf.contrib.slim.dataset_data_provider.DatasetDataProvider( dataset=dataset, shuffle=self.params["shuffle"], num_epochs=self.params["num_epochs"], **kwargs)
def testDecodeExampleWithItemHandlerCallback(self): np.random.seed(0) tensor_shape = (2, 3, 1) np_array = np.random.rand(2, 3, 1) example = example_pb2.Example(features=feature_pb2.Features( feature={ 'image/depth_map': self._EncodedFloatFeature(np_array), })) serialized_example = example.SerializeToString() with self.test_session(): serialized_example = array_ops.reshape(serialized_example, shape=[]) keys_to_features = { 'image/depth_map': parsing_ops.FixedLenFeature( tensor_shape, dtypes.float32, default_value=array_ops.zeros(tensor_shape)) } def HandleDepth(keys_to_tensors): depth = list(keys_to_tensors.values())[0] depth += 1 return depth items_to_handlers = { 'depth': tfexample_decoder.ItemHandlerCallback('image/depth_map', HandleDepth) } decoder = tfexample_decoder.TFExampleDecoder( keys_to_features, items_to_handlers) [tf_depth] = decoder.decode(serialized_example, ['depth']) depth = tf_depth.eval() self.assertAllClose(np_array, depth - 1)
def _create_tfrecord_dataset(config): """Create tfrecord dataset for DatasetDataProvider. Args: config: an instance of AdsExample proto. Returns: dataset: a slim.data.dataset.Dataset instance. """ def _handle_frame_features_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/features', config.feature_dims) def _handle_climax_features_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/climax_features', 1) def _handle_climax_predictions_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/climax_predictions', 1) def _handle_common_object_features_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/common_object_features', config.common_object_feature_dims) def _handle_place_features_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/place_features', config.place_feature_dims) def _handle_emotic_features_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/emotic_features', config.emotic_feature_dims) def _handle_affectnet_features_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/affectnet_features', config.affectnet_feature_dims) def _handle_shot_boundary_features_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/shot_boundary_features', config.shot_boundary_feature_dims) def _handle_optical_flow_features_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/optical_flow_features', config.optical_flow_feature_dims) def _handle_audio_features_wrapper(keys_to_tensors): return _handle_frame_features(keys_to_tensors, 'video/audio_features', config.audio_feature_dims) item_handler_frame_features = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/features'], func=_handle_frame_features_wrapper) item_handler_climax_features = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/climax_features'], func=_handle_climax_features_wrapper) item_handler_climax_predictions = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/climax_predictions'], func=_handle_climax_predictions_wrapper) item_handler_common_object_features = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/common_object_features'], func=_handle_common_object_features_wrapper) item_handler_place_features = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/place_features'], func=_handle_place_features_wrapper) item_handler_emotic_features = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/emotic_features'], func=_handle_emotic_features_wrapper) item_handler_affectnet_features = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/affectnet_features'], func=_handle_affectnet_features_wrapper) item_handler_shot_boundary_features = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/shot_boundary_features'], func=_handle_shot_boundary_features_wrapper) item_handler_optical_flow_features = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/optical_flow_features'], func=_handle_optical_flow_features_wrapper) item_handler_audio_features = tfexample_decoder.ItemHandlerCallback( keys=['video/n_frames', 'video/audio_features'], func=_handle_audio_features_wrapper) keys_to_features = { 'video/source_id': tf.FixedLenFeature(shape=(), dtype=tf.string, default_value=''), 'video/n_frames': tf.FixedLenFeature((), tf.int64, default_value=0), 'video/features': tf.VarLenFeature(tf.float32), 'video/climax_features': tf.VarLenFeature(tf.float32), 'video/climax_predictions': tf.VarLenFeature(tf.float32), 'video/common_object_features': tf.VarLenFeature(tf.float32), 'video/place_features': tf.VarLenFeature(tf.float32), 'video/emotic_features': tf.VarLenFeature(tf.float32), 'video/affectnet_features': tf.VarLenFeature(tf.float32), 'video/shot_boundary_features': tf.VarLenFeature(tf.float32), 'video/optical_flow_features': tf.VarLenFeature(tf.float32), 'video/audio_features': tf.VarLenFeature(tf.float32), 'anno/topic': tf.FixedLenFeature((), tf.int64), 'anno/sentiment': tf.FixedLenFeature((), tf.int64), 'anno/sentiment_list': tf.FixedLenFeature([config.sentiment_num_classes], tf.float32), } items_to_handlers = { 'video_id': tfexample_decoder.Tensor('video/source_id'), 'n_frames': tfexample_decoder.Tensor('video/n_frames'), 'topic': tfexample_decoder.Tensor('anno/topic'), 'sentiment': tfexample_decoder.Tensor('anno/sentiment'), 'frame_features': item_handler_frame_features, 'climax_features': item_handler_climax_features, 'climax_predictions': item_handler_climax_predictions, 'common_object_features': item_handler_common_object_features, 'place_features': item_handler_place_features, 'emotic_features': item_handler_emotic_features, 'affectnet_features': item_handler_affectnet_features, 'shot_boundary_features': item_handler_shot_boundary_features, 'optical_flow_features': item_handler_optical_flow_features, 'audio_features': item_handler_audio_features, 'sentiment_list': tfexample_decoder.Tensor('anno/sentiment_list'), } #if config.use_sent_list: # keys_to_features['anno/sentiment_list'] = tf.FixedLenFeature([config.sentiment_num_classes], tf.float32), # items_to_handlers['sentiment_list'] = tfexample_decoder.Tensor('anno/sentiment_list') decoder = tfexample_decoder.TFExampleDecoder(keys_to_features, items_to_handlers) input_paths = [ config.input_path[i] for i in xrange(len(config.input_path)) ] return dataset.Dataset(data_sources=input_paths, reader=tf.TFRecordReader, decoder=decoder, num_samples=config.num_examples, items_to_descriptions=None)