Decode block definition string Gets a list of block arg (dicts) through a string notation of arguments. E.g. ir_r2_k3_s2_e1_i32_o16_se0.25_noskip All args can exist in any order with the exception of the leading string which is assumed to indicate the block type. leading string
(block_str)
| 54 | |
| 55 | |
| 56 | def decode_block_str(block_str): |
| 57 | """ Decode block definition string |
| 58 | Gets a list of block arg (dicts) through a string notation of arguments. |
| 59 | E.g. ir_r2_k3_s2_e1_i32_o16_se0.25_noskip |
| 60 | All args can exist in any order with the exception of the leading string which |
| 61 | is assumed to indicate the block type. |
| 62 | leading string - block type ( |
| 63 | ir = InvertedResidual, ds = DepthwiseSep, dsa = DeptwhiseSep with pw act, cn = ConvBnAct) |
| 64 | r - number of repeat blocks, |
| 65 | k - kernel size, |
| 66 | s - strides (1-9), |
| 67 | e - expansion ratio, |
| 68 | c - output channels, |
| 69 | se - squeeze/excitation ratio |
| 70 | n - activation fn ('re', 'r6', 'hs', or 'sw') |
| 71 | Args: |
| 72 | block_str: a string representation of block arguments. |
| 73 | Returns: |
| 74 | A list of block args (dicts) |
| 75 | Raises: |
| 76 | ValueError: if the string def not properly specified (TODO) |
| 77 | """ |
| 78 | assert isinstance(block_str, str) |
| 79 | ops = block_str.split('_') |
| 80 | block_type = ops[0] # take the block type off the front |
| 81 | ops = ops[1:] |
| 82 | options = {} |
| 83 | noskip = False |
| 84 | for op in ops: |
| 85 | # string options being checked on individual basis, combine if they |
| 86 | # grow |
| 87 | if op == 'noskip': |
| 88 | noskip = True |
| 89 | elif op.startswith('n'): |
| 90 | # activation fn |
| 91 | key = op[0] |
| 92 | v = op[1:] |
| 93 | if v == 're': |
| 94 | value = nn.ReLU |
| 95 | elif v == 'r6': |
| 96 | value = nn.ReLU6 |
| 97 | elif v == 'sw': |
| 98 | value = Swish |
| 99 | else: |
| 100 | continue |
| 101 | options[key] = value |
| 102 | else: |
| 103 | # all numeric options |
| 104 | splits = re.split(r'(\d.*)', op) |
| 105 | if len(splits) >= 2: |
| 106 | key, value = splits[:2] |
| 107 | options[key] = value |
| 108 | |
| 109 | # if act_layer is None, the model default (passed to model init) will be |
| 110 | # used |
| 111 | act_layer = options['n'] if 'n' in options else None |
| 112 | exp_kernel_size = parse_ksize(options['a']) if 'a' in options else 1 |
| 113 | pw_kernel_size = parse_ksize(options['p']) if 'p' in options else 1 |
no test coverage detected