(src, dst, depth)
Convert keys in detectron pretrained ResNet models to pytorch style.
Convert keys in detectron pretrained ResNet models to pytorch style.
Convert keys in detectron pretrained ResNet models to pytorch style .
def convert(src, dst, depth):
    """Convert keys in detectron pretrained ResNet models to pytorch style."""
    # load arch_settings
    if depth not in arch_settings:
        raise ValueError('Only support ResNet-50 and ResNet-101 currently')
    block_nums = arch_settings[depth]
    # load caffe model
    caffe_model = mmcv.load(src, encoding='latin1')
    blobs = caffe_model['blobs'] if 'blobs' in caffe_model else caffe_model
    # convert to pytorch style
    state_dict = OrderedDict()
    converted_names = set()
    convert_conv_fc(blobs, state_dict, 'conv1', 'conv1', converted_names)
    convert_bn(blobs, state_dict, 'res_conv1_bn', 'bn1', converted_names)
    for i in range(1, len(block_nums) + 1):
        for j in range(block_nums[i - 1]):
            if j == 0:
                convert_conv_fc(blobs, state_dict,
                                'res{}_{}_branch1'.format(i + 1, j),
                                'layer{}.{}.downsample.0'.format(i, j),
                                converted_names)
                convert_bn(blobs, state_dict,
                           'res{}_{}_branch1_bn'.format(i + 1, j),
                           'layer{}.{}.downsample.1'.format(i, j),
                           converted_names)
            for k, letter in enumerate(['a', 'b', 'c']):
                convert_conv_fc(blobs, state_dict,
                                'res{}_{}_branch2{}'.format(i + 1, j, letter),
                                'layer{}.{}.conv{}'.format(i, j, k + 1),
                                converted_names)
                convert_bn(blobs, state_dict,
                           'res{}_{}_branch2{}_bn'.format(i + 1, j, letter),
                           'layer{}.{}.bn{}'.format(i, j,
                                                    k + 1), converted_names)
    # check if all layers are converted
    for key in blobs:
        if key not in converted_names:
            print('Not Convert: {}'.format(key))
    # save checkpoint
    checkpoint = dict()
    checkpoint['state_dict'] = state_dict
    torch.save(checkpoint, dst)
def convert ( src , dst , depth ) : # load arch_settings if depth not in arch_settings : raise ValueError ( 'Only support ResNet-50 and ResNet-101 currently' ) block_nums = arch_settings [ depth ] # load caffe model caffe_model = mmcv . load ( src , encoding = 'latin1' ) blobs = caffe_model [ 'blobs' ] if 'blobs' in caffe_model else caffe_model # convert to pytorch style state_dict = OrderedDict ( ) converted_names = set ( ) convert_conv_fc ( blobs , state_dict , 'conv1' , 'conv1' , converted_names ) convert_bn ( blobs , state_dict , 'res_conv1_bn' , 'bn1' , converted_names ) for i in range ( 1 , len ( block_nums ) + 1 ) : for j in range ( block_nums [ i - 1 ] ) : if j == 0 : convert_conv_fc ( blobs , state_dict , 'res{}_{}_branch1' . format ( i + 1 , j ) , 'layer{}.{}.downsample.0' . format ( i , j ) , converted_names ) convert_bn ( blobs , state_dict , 'res{}_{}_branch1_bn' . format ( i + 1 , j ) , 'layer{}.{}.downsample.1' . format ( i , j ) , converted_names ) for k , letter in enumerate ( [ 'a' , 'b' , 'c' ] ) : convert_conv_fc ( blobs , state_dict , 'res{}_{}_branch2{}' . format ( i + 1 , j , letter ) , 'layer{}.{}.conv{}' . format ( i , j , k + 1 ) , converted_names ) convert_bn ( blobs , state_dict , 'res{}_{}_branch2{}_bn' . format ( i + 1 , j , letter ) , 'layer{}.{}.bn{}' . format ( i , j , k + 1 ) , converted_names ) # check if all layers are converted for key in blobs : if key not in converted_names : print ( 'Not Convert: {}' . format ( key ) ) # save checkpoint checkpoint = dict ( ) checkpoint [ 'state_dict' ] = state_dict torch . save ( checkpoint , dst )