Block Decoder for readability, straight from the official TensorFlow repository.
| 290 | |
| 291 | |
| 292 | class BlockDecoder(object): |
| 293 | """Block Decoder for readability, |
| 294 | straight from the official TensorFlow repository. |
| 295 | """ |
| 296 | |
| 297 | @staticmethod |
| 298 | def _decode_block_string(block_string): |
| 299 | """Get a block through a string notation of arguments. |
| 300 | Args: |
| 301 | block_string (str): A string notation of arguments. |
| 302 | Examples: 'r1_k3_s11_e1_i32_o16_se0.25_noskip'. |
| 303 | Returns: |
| 304 | BlockArgs: The namedtuple defined at the top of this file. |
| 305 | """ |
| 306 | assert isinstance(block_string, str) |
| 307 | |
| 308 | ops = block_string.split('_') |
| 309 | options = {} |
| 310 | for op in ops: |
| 311 | splits = re.split(r'(\d.*)', op) |
| 312 | if len(splits) >= 2: |
| 313 | key, value = splits[:2] |
| 314 | options[key] = value |
| 315 | |
| 316 | # Check stride |
| 317 | assert (('s' in options and len(options['s']) == 1) or |
| 318 | (len(options['s']) == 2 and options['s'][0] == options['s'][1])) |
| 319 | |
| 320 | return BlockArgs( |
| 321 | num_repeat=int(options['r']), |
| 322 | kernel_size=int(options['k']), |
| 323 | stride=[int(options['s'][0])], |
| 324 | expand_ratio=int(options['e']), |
| 325 | input_filters=int(options['i']), |
| 326 | output_filters=int(options['o']), |
| 327 | se_ratio=float(options['se']) if 'se' in options else None, |
| 328 | id_skip=('noskip' not in block_string)) |
| 329 | |
| 330 | @staticmethod |
| 331 | def _encode_block_string(block): |
| 332 | """Encode a block to a string. |
| 333 | Args: |
| 334 | block (namedtuple): A BlockArgs type argument. |
| 335 | Returns: |
| 336 | block_string: A String form of BlockArgs. |
| 337 | """ |
| 338 | args = [ |
| 339 | 'r%d' % block.num_repeat, |
| 340 | 'k%d' % block.kernel_size, |
| 341 | 's%d%d' % (block.strides[0], block.strides[1]), |
| 342 | 'e%s' % block.expand_ratio, |
| 343 | 'i%d' % block.input_filters, |
| 344 | 'o%d' % block.output_filters |
| 345 | ] |
| 346 | if 0 < block.se_ratio <= 1: |
| 347 | args.append('se%s' % block.se_ratio) |
| 348 | if block.id_skip is False: |
| 349 | args.append('noskip') |
nothing calls this directly
no outgoing calls
no test coverage detected