Best Python code snippet using hypothesis
shufflenetv2_to_mmcls.py
Source:shufflenetv2_to_mmcls.py  
1# Copyright (c) OpenMMLab. All rights reserved.2import argparse3from collections import OrderedDict4import torch5def convert_conv1(model_key, model_weight, state_dict, converted_names):6    if model_key.find('conv1.0') >= 0:7        new_key = model_key.replace('conv1.0', 'backbone.conv1.conv')8    else:9        new_key = model_key.replace('conv1.1', 'backbone.conv1.bn')10    state_dict[new_key] = model_weight11    converted_names.add(model_key)12    print(f'Convert {model_key} to {new_key}')13def convert_conv5(model_key, model_weight, state_dict, converted_names):14    if model_key.find('conv5.0') >= 0:15        new_key = model_key.replace('conv5.0', 'backbone.layers.3.conv')16    else:17        new_key = model_key.replace('conv5.1', 'backbone.layers.3.bn')18    state_dict[new_key] = model_weight19    converted_names.add(model_key)20    print(f'Convert {model_key} to {new_key}')21def convert_head(model_key, model_weight, state_dict, converted_names):22    new_key = model_key.replace('fc', 'head.fc')23    state_dict[new_key] = model_weight24    converted_names.add(model_key)25    print(f'Convert {model_key} to {new_key}')26def convert_block(model_key, model_weight, state_dict, converted_names):27    split_keys = model_key.split('.')28    layer, block, branch = split_keys[:3]29    layer_id = int(layer[-1]) - 230    new_key = model_key.replace(layer, f'backbone.layers.{layer_id}')31    if branch == 'branch1':32        if new_key.find('branch1.0') >= 0:33            new_key = new_key.replace('branch1.0', 'branch1.0.conv')34        elif new_key.find('branch1.1') >= 0:35            new_key = new_key.replace('branch1.1', 'branch1.0.bn')36        elif new_key.find('branch1.2') >= 0:37            new_key = new_key.replace('branch1.2', 'branch1.1.conv')38        elif new_key.find('branch1.3') >= 0:39            new_key = new_key.replace('branch1.3', 'branch1.1.bn')40    elif branch == 'branch2':41        if new_key.find('branch2.0') >= 0:42            new_key = new_key.replace('branch2.0', 'branch2.0.conv')43        elif new_key.find('branch2.1') >= 0:44            new_key = new_key.replace('branch2.1', 'branch2.0.bn')45        elif new_key.find('branch2.3') >= 0:46            new_key = new_key.replace('branch2.3', 'branch2.1.conv')47        elif new_key.find('branch2.4') >= 0:48            new_key = new_key.replace('branch2.4', 'branch2.1.bn')49        elif new_key.find('branch2.5') >= 0:50            new_key = new_key.replace('branch2.5', 'branch2.2.conv')51        elif new_key.find('branch2.6') >= 0:52            new_key = new_key.replace('branch2.6', 'branch2.2.bn')53        else:54            raise ValueError(f'Unsupported conversion of key {model_key}')55    else:56        raise ValueError(f'Unsupported conversion of key {model_key}')57    print(f'Convert {model_key} to {new_key}')58    state_dict[new_key] = model_weight59    converted_names.add(model_key)60def convert(src, dst):61    """Convert keys in torchvision pretrained ShuffleNetV2 models to mmcls62    style."""63    # load pytorch model64    blobs = torch.load(src, map_location='cpu')65    # convert to pytorch style66    state_dict = OrderedDict()67    converted_names = set()68    for key, weight in blobs.items():69        if 'conv1' in key:70            convert_conv1(key, weight, state_dict, converted_names)71        elif 'fc' in key:72            convert_head(key, weight, state_dict, converted_names)73        elif key.startswith('s'):74            convert_block(key, weight, state_dict, converted_names)75        elif 'conv5' in key:76            convert_conv5(key, weight, state_dict, converted_names)77    # check if all layers are converted78    for key in blobs:79        if key not in converted_names:80            print(f'not converted: {key}')81    # save checkpoint82    checkpoint = dict()83    checkpoint['state_dict'] = state_dict84    torch.save(checkpoint, dst)85def main():86    parser = argparse.ArgumentParser(description='Convert model keys')87    parser.add_argument('src', help='src detectron model path')88    parser.add_argument('dst', help='save path')89    args = parser.parse_args()90    convert(args.src, args.dst)91if __name__ == '__main__':...key_expander.py
Source:key_expander.py  
1"""2AES Key Expansion3Expands 128, 192, or 256 bit key for use with AES4Algorithm per NIST FIPS-197 http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf5Copyright (c) 2010, Adam Newman http://www.caller9.com6Licensed under the MIT license http://www.opensource.org/licenses/mit-license.php7"""8__all__ = "expandKey",9from .aes_tables import sbox,rcon10_expanded_key_length={16:176,24:208,32:240}11def expandKey(new_key):12    """Expand the encryption key per AES key schedule specifications13        http://en.wikipedia.org/wiki/Rijndael_key_schedule#Key_schedule_description"""14    _n=len(new_key)15    if _n not in (16,24,32):16        raise RuntimeError('expand(): key size is invalid')17    rcon_iter=118    _nn16=_n!=1619    _n32=_n==3220    n0=new_key[-4]21    n1=new_key[-3]22    n2=new_key[-2]23    n3=new_key[-1]24    _n0=-_n25    _n1=1-_n26    _n2=2-_n27    _n3=3-_n28    _n=_expanded_key_length[_n]-_n29    nex=new_key.extend30    while 1:31        #Copy last 4 bytes of extended key, apply core, increment rcon_iter,32        #core Append the list of elements 1-3 and list comprised of element 0 (circular rotate left)33        #core For each element of this new list, put the result of sbox into output array.34        #xor with 4 bytes n bytes from end of extended key35        #First byte of output array is XORed with rcon(iter)36        nx=n0,n1,n2,n3=(sbox[n1]^rcon[rcon_iter]^new_key[_n0],37            sbox[n2]^new_key[_n1],38            sbox[n3]^new_key[_n2],39            sbox[n0]^new_key[_n3])40        nex(nx)41        rcon_iter += 142        #Run three passes of 4 byte expansion using copy of 4 byte tail of extended key43        #which is then xor'd with 4 bytes n bytes from end of extended key44        nx=n0,n1,n2,n3=(n0^new_key[_n0],45            n1^new_key[_n1],46            n2^new_key[_n2],47            n3^new_key[_n3])48        nex(nx)49        nx=n0,n1,n2,n3=(n0^new_key[_n0],50            n1^new_key[_n1],51            n2^new_key[_n2],52            n3^new_key[_n3])53        nex(nx)54        nx=n0,n1,n2,n3=(n0^new_key[_n0],55            n1^new_key[_n1],56            n2^new_key[_n2],57            n3^new_key[_n3])58        nex(nx)59        _n -= 1660        if _n <= 0:return new_key61        elif _nn16:62            #If key length is 256 and key is not complete, add 4 bytes tail of extended key63            #run through sbox before xor with 4 bytes n bytes from end of extended key64            if _n32:65                nx=n0,n1,n2,n3=(sbox[n0]^new_key[_n0],66                    sbox[n1]^new_key[_n1],67                    sbox[n2]^new_key[_n2],68                    sbox[n3]^new_key[_n3])69                nex(nx)70                _n -= 471                if _n <= 0:return new_key72            #If key length in (192, 256) and key is not complete, run 2 or 3 passes respectively73            #of 4 byte tail of extended key xor with 4 bytes n bytes from end of extended key74            nx=n0,n1,n2,n3=(n0^new_key[_n0],75                n1^new_key[_n1],76                n2^new_key[_n2],77                n3^new_key[_n3])78            nex(nx)79            nx=n0,n1,n2,n3=(n0^new_key[_n0],80                n1^new_key[_n1],81                n2^new_key[_n2],82                n3^new_key[_n3])83            nex(nx)84            if _n32:85                nx=n0,n1,n2,n3=(n0^new_key[_n0],86                    n1^new_key[_n1],87                    n2^new_key[_n2],88                    n3^new_key[_n3])89                nex(nx)90                _n -= 1291            else:_n -= 8...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
