3 from __future__
import absolute_import
4 from __future__
import division
5 from __future__
import print_function
6 from __future__
import unicode_literals
18 default_name_suffix =
'cached_reader' 20 """Reader with persistent in-file cache. 23 cached_reader = CachedReader( 25 db_path='/tmp/cache.db', 28 build_cache_step = cached_reader.build_cache_step() 29 with LocalSession() as session: 30 session.run(build_cache_step) 32 Every time new CachedReader is created, it's expected that 33 db_path exists before calling .setup_ex(...) and .read(...). 35 If db_path doesn't exist, it's expected build_cache_step to be called 36 first to build a cache at db_path. 38 build_cache_step will check existence of provided db_path and in case 39 it's missing will initialize it by reading data from original reader. 40 All consequent attempts to read will ignore original reader 41 (i.e. no additional data will be read from it). 44 original_reader: Reader. 45 If provided, it's the original reader used to build the cache file. 47 db_type: str. DB type of file. A db_type is registed by 48 `REGISTER_CAFFE2_DB(<db_type>, <DB Class>)`. 50 name: str or None. Name of CachedReader. 51 Optional name to prepend to blobs that will store the data. 52 Default to '<db_name>_<default_name_suffix>'. 54 How many examples are read for each time the read_net is run. 64 assert original_reader
is not None,
"original_reader can't be None" 67 super(CachedReader, self).__init__(
74 def _init_reader_schema(self, *args, **kwargs):
75 """Prepare the reader schema. 77 Since an original reader is given, 78 use it's schema as ground truth. 81 schema: schema.Struct. Used in Reader.__init__(...). 83 return self.original_reader._schema
86 """Build a step for generating cache DB file. 88 If self.db_path exists and not overwritting, build an empty step. 89 Overwise, build a step as follows. 90 Pipe original reader to the _DatasetWriter, 91 so that dataset field blobs are populated. 92 Then save these blobs into a file. 95 overwrite: bool. If true, ignore the existing file 96 and build a new one overwritting the existing one anyway. 99 build_cache_step: ExcutionStep. 100 The step to be run for building a cache DB file. 102 if os.path.exists(self.
db_path)
and not overwrite:
104 return core.execution_step(
'build_step', [])
110 copy_step = copy_tg.to_task().get_step()
114 return core.execution_step(
'build_cache', [init_net, copy_step, save_net])
116 def _save_field_blobs_to_db_file(self, net):
117 """Save dataset field blobs to a DB file at db_path""" 123 blob_name_overrides=self.ds.field_names(),
def _init_field_blobs_as_empty(self, init_net)
def build_cache_step(self, overwrite=False)
def _save_field_blobs_to_db_file(self, net)