Exemplo n.º 1
0
def prune_conv(layer: nn.modules.conv._ConvNd,
               idxs: list,
               inplace: bool = True,
               dry_run: bool = False):
    """Prune `filters` for the convolutional layer, e.g. [256 x 128 x 3 x 3] => [192 x 128 x 3 x 3]

    Args:
        - layer: a convolution layer.
        - idxs: pruning index.
    """
    idxs = list(set(idxs))
    num_pruned = len(idxs) * reduce(mul, layer.weight.shape[1:]) + (
        len(idxs) if layer.bias is not None else 0)
    if dry_run:
        return layer, num_pruned

    if not inplace:
        layer = deepcopy(layer)

    keep_idxs = [idx for idx in range(layer.out_channels) if idx not in idxs]
    layer.out_channels = layer.out_channels - len(idxs)
    if isinstance(layer, (nn.ConvTranspose2d, nn.ConvTranspose3d)):
        layer.weight = torch.nn.Parameter(layer.weight.data.clone()[:,
                                                                    keep_idxs])
    else:
        layer.weight = torch.nn.Parameter(layer.weight.data.clone()[keep_idxs])
    if layer.bias is not None:
        layer.bias = torch.nn.Parameter(layer.bias.data.clone()[keep_idxs])
    return layer, num_pruned
Exemplo n.º 2
0
def prune_related_conv(layer: nn.modules.conv._ConvNd,
                       idxs: list,
                       inplace: bool = True,
                       dry_run: bool = False):
    """Prune `kernels` for the related (affected) convolutional layer, e.g. [256 x 128 x 3 x 3] => [256 x 96 x 3 x 3]

    Args:
        layer: a convolutional layer.
        idxs: pruning index.
    """
    idxs = list(set(idxs))
    num_pruned = len(idxs) * layer.weight.shape[0] * reduce(
        mul, layer.weight.shape[2:])
    if dry_run:
        return layer, num_pruned
    if not inplace:
        layer = deepcopy(layer)

    keep_idxs = [i for i in range(layer.in_channels) if i not in idxs]

    layer.in_channels = layer.in_channels - len(idxs)

    if isinstance(layer, (nn.ConvTranspose2d, nn.ConvTranspose3d)):
        layer.weight = torch.nn.Parameter(
            layer.weight.data.clone()[keep_idxs, :])
    else:
        layer.weight = torch.nn.Parameter(layer.weight.data.clone()[:,
                                                                    keep_idxs])
    # no bias pruning because it does not change the output size
    return layer, num_pruned
Exemplo n.º 3
0
def prune_conv(layer: nn.modules.conv._ConvNd,
               idxs: list,
               inplace: bool = True,
               dry_run: bool = False):  #line:34
    ""  #line:40
    idxs = list(set(idxs))  #line:41
    OO000O0000OO0OO00 = len(idxs) * reduce(mul, layer.weight.shape[1:]) + (
        len(idxs) if layer.bias is not None else 0)  #line:42
    if dry_run:  #line:43
        return layer, OO000O0000OO0OO00  #line:44
    if not inplace:  #line:46
        layer = deepcopy(layer)  #line:47
    OO00O00OOOOO0O00O = [
        O0000OOOO00OOO0O0 for O0000OOOO00OOO0O0 in range(layer.out_channels)
        if O0000OOOO00OOO0O0 not in idxs
    ]  #line:49
    layer.out_channels = layer.out_channels - len(idxs)  #line:50
    if isinstance(layer, (nn.ConvTranspose2d, nn.ConvTranspose3d)):  #line:51
        layer.weight = torch.nn.Parameter(
            layer.weight.data.clone()[:, OO00O00OOOOO0O00O])  #line:52
    else:  #line:53
        layer.weight = torch.nn.Parameter(
            layer.weight.data.clone()[OO00O00OOOOO0O00O])  #line:54
    if layer.bias is not None:  #line:55
        layer.bias = torch.nn.Parameter(
            layer.bias.data.clone()[OO00O00OOOOO0O00O])  #line:56
    return layer, OO000O0000OO0OO00  #line:57
Exemplo n.º 4
0
def prune_group_conv(layer: nn.modules.conv._ConvNd,
                     idxs: list,
                     inplace: bool = True,
                     dry_run: bool = False):
    """Prune `filters` for the convolutional layer, e.g. [256 x 128 x 3 x 3] => [192 x 128 x 3 x 3]

    Args:
        - layer: a convolution layer.
        - idxs: pruning index.
    """

    if layer.groups > 1:
        assert layer.groups == layer.in_channels and layer.groups == layer.out_channels, "only group conv with in_channel==groups==out_channels is supported"

    idxs = list(set(idxs))
    num_pruned = len(idxs) * reduce(mul, layer.weight.shape[1:]) + (
        len(idxs) if layer.bias is not None else 0)
    if dry_run:
        return layer, num_pruned
    if not inplace:
        layer = deepcopy(layer)
    keep_idxs = [idx for idx in range(layer.out_channels) if idx not in idxs]
    layer.out_channels = layer.out_channels - len(idxs)
    layer.in_channels = layer.in_channels - len(idxs)

    layer.groups = layer.groups - len(idxs)
    layer.weight = torch.nn.Parameter(layer.weight.data.clone()[keep_idxs])
    if layer.bias is not None:
        layer.bias = torch.nn.Parameter(layer.bias.data.clone()[keep_idxs])

    return layer, num_pruned
Exemplo n.º 5
0
def prune_group_conv(layer: nn.modules.conv._ConvNd,
                     idxs: list,
                     inplace: bool = True,
                     dry_run: bool = False):  #line:9
    ""  #line:15
    if layer.groups > 1:  #line:16
        assert layer.groups == layer.in_channels and layer.groups == layer.out_channels, "only group conv with in_channel==groups==out_channels is supported"  #line:17
    idxs = list(set(idxs))  #line:19
    OO0OOOO0OOOOO0OO0 = len(idxs) * reduce(mul, layer.weight.shape[1:]) + (
        len(idxs) if layer.bias is not None else 0)  #line:20
    if dry_run:  #line:21
        return layer, OO0OOOO0OOOOO0OO0  #line:22
    if not inplace:  #line:23
        layer = deepcopy(layer)  #line:24
    OO00OOO0O0O0OOOOO = [
        OOO0OOOOO0O00OOO0 for OOO0OOOOO0O00OOO0 in range(layer.out_channels)
        if OOO0OOOOO0O00OOO0 not in idxs
    ]  #line:25
    layer.out_channels = layer.out_channels - len(idxs)  #line:26
    layer.in_channels = layer.in_channels - len(idxs)  #line:27
    layer.groups = layer.groups - len(idxs)  #line:28
    layer.weight = torch.nn.Parameter(
        layer.weight.data.clone()[OO00OOO0O0O0OOOOO])  #line:29
    if layer.bias is not None:  #line:30
        layer.bias = torch.nn.Parameter(
            layer.bias.data.clone()[OO00OOO0O0O0OOOOO])  #line:31
    return layer, OO0OOOO0OOOOO0OO0  #line:32
Exemplo n.º 6
0
def prune_related_conv(OO000O000O0O0O0O0: nn.modules.conv._ConvNd,
                       O0O0000000O0O0OO0: list,
                       inplace: bool = True,
                       dry_run: bool = False):  #line:59
    ""  #line:65
    O0O0000000O0O0OO0 = list(set(O0O0000000O0O0OO0))  #line:66
    O0OO00O0O0OOOO000 = len(
        O0O0000000O0O0OO0) * OO000O000O0O0O0O0.weight.shape[0] * reduce(
            mul, OO000O000O0O0O0O0.weight.shape[2:])  #line:67
    if dry_run:  #line:68
        return OO000O000O0O0O0O0, O0OO00O0O0OOOO000  #line:69
    if not inplace:  #line:70
        OO000O000O0O0O0O0 = deepcopy(OO000O000O0O0O0O0)  #line:71
    OO0O0OO0OOO0OO0O0 = [
        O0O0000OOO0OOOO0O
        for O0O0000OOO0OOOO0O in range(OO000O000O0O0O0O0.in_channels)
        if O0O0000OOO0OOOO0O not in O0O0000000O0O0OO0
    ]  #line:74
    OO000O000O0O0O0O0.in_channels = OO000O000O0O0O0O0.in_channels - len(
        O0O0000000O0O0OO0)  #line:76
    if isinstance(OO000O000O0O0O0O0,
                  (nn.ConvTranspose2d, nn.ConvTranspose3d)):  #line:78
        OO000O000O0O0O0O0.weight = torch.nn.Parameter(
            OO000O000O0O0O0O0.weight.data.clone()[
                OO0O0OO0OOO0OO0O0, :])  #line:79
    else:  #line:80
        OO000O000O0O0O0O0.weight = torch.nn.Parameter(
            OO000O000O0O0O0O0.weight.data.clone()[:,
                                                  OO0O0OO0OOO0OO0O0])  #line:81
    return OO000O000O0O0O0O0, O0OO00O0O0OOOO000  #line:83
Exemplo n.º 7
0
def prune_conv(OOOOOOOO00000O00O: nn.modules.conv._ConvNd,
               OO00OO0OOOO0000O0: list,
               inplace: bool = True,
               dry_run: bool = False):  #line:34
    ""  #line:40
    OO00OO0OOOO0000O0 = list(set(OO00OO0OOOO0000O0))  #line:41
    O00O00OO00O00OO0O = len(OO00OO0OOOO0000O0) * reduce(
        mul, OOOOOOOO00000O00O.weight.shape[1:]) + (
            len(OO00OO0OOOO0000O0) if OOOOOOOO00000O00O.bias is not None else 0
        )  #line:42
    if dry_run:  #line:43
        return OOOOOOOO00000O00O, O00O00OO00O00OO0O  #line:44
    if not inplace:  #line:46
        OOOOOOOO00000O00O = deepcopy(OOOOOOOO00000O00O)  #line:47
    O000O0O0OO0O0000O = [
        OO0OOO0OO0O0OOO0O
        for OO0OOO0OO0O0OOO0O in range(OOOOOOOO00000O00O.out_channels)
        if OO0OOO0OO0O0OOO0O not in OO00OO0OOOO0000O0
    ]  #line:49
    OOOOOOOO00000O00O.out_channels = OOOOOOOO00000O00O.out_channels - len(
        OO00OO0OOOO0000O0)  #line:50
    if isinstance(OOOOOOOO00000O00O,
                  (nn.ConvTranspose2d, nn.ConvTranspose3d)):  #line:51
        OOOOOOOO00000O00O.weight = torch.nn.Parameter(
            OOOOOOOO00000O00O.weight.data.clone()[:,
                                                  O000O0O0OO0O0000O])  #line:52
    else:  #line:53
        OOOOOOOO00000O00O.weight = torch.nn.Parameter(
            OOOOOOOO00000O00O.weight.data.clone()[O000O0O0OO0O0000O])  #line:54
    if OOOOOOOO00000O00O.bias is not None:  #line:55
        OOOOOOOO00000O00O.bias = torch.nn.Parameter(
            OOOOOOOO00000O00O.bias.data.clone()[O000O0O0OO0O0000O])  #line:56
    return OOOOOOOO00000O00O, O00O00OO00O00OO0O  #line:57
Exemplo n.º 8
0
def prune_group_conv(OOOO0OOOOO00000OO: nn.modules.conv._ConvNd,
                     OOOO00OO00O00O000: list,
                     inplace: bool = True,
                     dry_run: bool = False):  #line:9
    ""  #line:15
    if OOOO0OOOOO00000OO.groups > 1:  #line:16
        assert OOOO0OOOOO00000OO.groups == OOOO0OOOOO00000OO.in_channels and OOOO0OOOOO00000OO.groups == OOOO0OOOOO00000OO.out_channels, "only group conv with in_channel==groups==out_channels is supported"  #line:17
    OOOO00OO00O00O000 = list(set(OOOO00OO00O00O000))  #line:19
    OOO0OO0O0OO0O000O = len(OOOO00OO00O00O000) * reduce(
        mul, OOOO0OOOOO00000OO.weight.shape[1:]) + (
            len(OOOO00OO00O00O000) if OOOO0OOOOO00000OO.bias is not None else 0
        )  #line:20
    if dry_run:  #line:21
        return OOOO0OOOOO00000OO, OOO0OO0O0OO0O000O  #line:22
    if not inplace:  #line:23
        OOOO0OOOOO00000OO = deepcopy(OOOO0OOOOO00000OO)  #line:24
    OOOO0000000000OO0 = [
        O0O0OO0OO0O00O0O0
        for O0O0OO0OO0O00O0O0 in range(OOOO0OOOOO00000OO.out_channels)
        if O0O0OO0OO0O00O0O0 not in OOOO00OO00O00O000
    ]  #line:25
    OOOO0OOOOO00000OO.out_channels = OOOO0OOOOO00000OO.out_channels - len(
        OOOO00OO00O00O000)  #line:26
    OOOO0OOOOO00000OO.in_channels = OOOO0OOOOO00000OO.in_channels - len(
        OOOO00OO00O00O000)  #line:27
    OOOO0OOOOO00000OO.groups = OOOO0OOOOO00000OO.groups - len(
        OOOO00OO00O00O000)  #line:28
    OOOO0OOOOO00000OO.weight = torch.nn.Parameter(
        OOOO0OOOOO00000OO.weight.data.clone()[OOOO0000000000OO0])  #line:29
    if OOOO0OOOOO00000OO.bias is not None:  #line:30
        OOOO0OOOOO00000OO.bias = torch.nn.Parameter(
            OOOO0OOOOO00000OO.bias.data.clone()[OOOO0000000000OO0])  #line:31
    return OOOO0OOOOO00000OO, OOO0OO0O0OO0O000O  #line:32
Exemplo n.º 9
0
def prune_related_conv(layer: nn.modules.conv._ConvNd,
                       idxs: list,
                       inplace: bool = True,
                       dry_run: bool = False):  #line:59
    ""  #line:65
    idxs = list(set(idxs))  #line:66
    O0O0O0O000OO000O0 = len(idxs) * layer.weight.shape[0] * reduce(
        mul, layer.weight.shape[2:])  #line:67
    if dry_run:  #line:68
        return layer, O0O0O0O000OO000O0  #line:69
    if not inplace:  #line:70
        layer = deepcopy(layer)  #line:71
    O0O00O0OO0000O00O = [
        OO0O0O00O00O0OO0O for OO0O0O00O00O0OO0O in range(layer.in_channels)
        if OO0O0O00O00O0OO0O not in idxs
    ]  #line:74
    layer.in_channels = layer.in_channels - len(idxs)  #line:76
    if isinstance(layer, (nn.ConvTranspose2d, nn.ConvTranspose3d)):  #line:78
        layer.weight = torch.nn.Parameter(
            layer.weight.data.clone()[O0O00O0OO0000O00O, :])  #line:79
    else:  #line:80
        layer.weight = torch.nn.Parameter(
            layer.weight.data.clone()[:, O0O00O0OO0000O00O])  #line:81
    return layer, O0O0O0O000OO000O0  #line:83