| 14 | |
| 15 | |
| 16 | class CachedReader(DBFileReader): |
| 17 | |
| 18 | default_name_suffix = 'cached_reader' |
| 19 | |
| 20 | """Reader with persistent in-file cache. |
| 21 | |
| 22 | Example usage: |
| 23 | cached_reader = CachedReader( |
| 24 | reader, |
| 25 | db_path='/tmp/cache.db', |
| 26 | db_type='LevelDB', |
| 27 | ) |
| 28 | build_cache_step = cached_reader.build_cache_step() |
| 29 | with LocalSession() as session: |
| 30 | session.run(build_cache_step) |
| 31 | |
| 32 | Every time new CachedReader is created, it's expected that |
| 33 | db_path exists before calling .setup_ex(...) and .read(...). |
| 34 | |
| 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. |
| 37 | |
| 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). |
| 42 | |
| 43 | Args: |
| 44 | original_reader: Reader. |
| 45 | If provided, it's the original reader used to build the cache file. |
| 46 | db_path: str. |
| 47 | |
| 48 | Optional Args: |
| 49 | db_type: str. DB type of file. A db_type is registed by |
| 50 | `REGISTER_CAFFE2_DB(<db_type>, <DB Class>)`. |
| 51 | Default to 'LevelDB'. |
| 52 | name: str or None. Name of CachedReader. |
| 53 | Optional name to prepend to blobs that will store the data. |
| 54 | Default to '<db_name>_<default_name_suffix>'. |
| 55 | batch_size: int. |
| 56 | How many examples are read for each time the read_net is run. |
| 57 | Defaults to 100. |
| 58 | loop_over: bool. |
| 59 | If True given, will go through examples in random order endlessly. |
| 60 | Defaults to False. |
| 61 | """ |
| 62 | def __init__( |
| 63 | self, |
| 64 | original_reader, |
| 65 | db_path, |
| 66 | db_type='LevelDB', |
| 67 | name=None, |
| 68 | batch_size=100, |
| 69 | loop_over=False, |
| 70 | ): |
| 71 | assert original_reader is not None, "original_reader can't be None" |
| 72 | self.original_reader = original_reader |
| 73 |
no outgoing calls
searching dependent graphs…