tfrecords
The tfrecords module provides an implementation for the TFRecords files read/write
TFRecords
¶
This class allows to convert Dataset objects to TFRecords and to load them in dataset tensorflow format.
Source code in otbtf/tfrecords.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
__init__(path)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Can be a directory where TFRecords must be saved/loaded |
required |
Source code in otbtf/tfrecords.py
ds2tfrecord(dataset, n_samples_per_shard=100, drop_remainder=True)
¶
Convert and save samples from dataset object to tfrecord files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Any
|
Dataset object to convert into a set of tfrecords |
required |
n_samples_per_shard |
int
|
Number of samples per shard |
100
|
drop_remainder |
bool
|
Whether additional samples should be dropped.
Advisable if using multiworkers training. If True, all
TFRecords will have |
True
|
Source code in otbtf/tfrecords.py
load(filepath)
staticmethod
¶
Return data from JSON format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
Input file name |
required |
parse_tfrecord(example, target_keys, preprocessing_fn=None, **kwargs)
¶
Parse example object to sample dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
example |
Any
|
Example object to parse |
required |
target_keys |
List[str]
|
list of keys of the targets |
required |
preprocessing_fn |
Callable
|
Optional. A preprocessing function that process the input example |
None
|
kwargs |
some keywords arguments for preprocessing_fn |
{}
|
Source code in otbtf/tfrecords.py
read(batch_size, target_keys, n_workers=1, drop_remainder=True, shuffle_buffer_size=None, preprocessing_fn=None, shard_policy=tf.data.experimental.AutoShardPolicy.AUTO, prefetch_buffer_size=tf.data.experimental.AUTOTUNE, num_parallel_calls=tf.data.experimental.AUTOTUNE, **kwargs)
¶
Read all tfrecord files matching with pattern and convert data to tensorflow dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size |
int
|
Size of tensorflow batch |
required |
target_keys |
List[str]
|
Keys of the target, e.g. ['s2_out'] |
required |
n_workers |
int
|
number of workers, e.g. 4 if using 4 GPUs, e.g. 12 if using 3 nodes of 4 GPUs |
1
|
drop_remainder |
bool
|
whether the last batch should be dropped in the
case it has fewer than |
True
|
shuffle_buffer_size |
int
|
if None, shuffle is not used. Else, blocks of shuffle_buffer_size elements are shuffled using uniform random. |
None
|
preprocessing_fn |
Callable
|
Optional. A preprocessing function that takes
input examples as args and returns the preprocessed input
examples. Typically, examples are composed of model inputs and
targets. Model inputs and model targets must be computed
accordingly to (1) what the model outputs and (2) what
training loss needs. For instance, for a classification
problem, the model will likely output the softmax, or
activation neurons, for each class, and the cross entropy loss
requires labels in one hot encoding. In this case, the
|
None
|
shard_policy |
sharding policy for the TFRecord dataset options |
AUTO
|
|
prefetch_buffer_size |
int
|
buffer size for the prefetch operation |
AUTOTUNE
|
num_parallel_calls |
int
|
number of parallel calls for the parsing + preprocessing step |
AUTOTUNE
|
kwargs |
some keywords arguments for |
{}
|
Source code in otbtf/tfrecords.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
save(data, filepath)
staticmethod
¶
Save data to JSON format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict[str, Any]
|
Data to save json format |
required |
filepath |
str
|
Output file name |
required |