Skip to content

Instantly share code, notes, and snippets.

@braindevices
Last active June 30, 2024 01:05
Show Gist options
  • Select an option

  • Save braindevices/7a7d290acfb32a6763d51f25f73ec302 to your computer and use it in GitHub Desktop.

Select an option

Save braindevices/7a7d290acfb32a6763d51f25f73ec302 to your computer and use it in GitHub Desktop.

Revisions

  1. braindevices revised this gist Jun 30, 2024. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion cal_yolov8_rf.py
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,12 @@
    btn*1 # bottom-up C2f 3*d
    ]

    blocks_dets = [
    _b + [
    [(3,1)]*2
    ]
    for _b in [blocks_P3, blocks_P4, blocks_P5]
    ]

    rfs_Cs = get_feature_node_RF(blocks, idx_Cs)
    for i, rf in enumerate(rfs_Cs):
    @@ -40,4 +46,8 @@
    rf_P4 = get_feature_node_RF(blocks_P4, [len(blocks_P4)-1])[-1]
    rf_P5 = get_feature_node_RF(blocks_P5, [len(blocks_P5)-1])[-1]
    for i, rf in enumerate([rf_P3, rf_P4, rf_P5]):
    print(f"P{i+3} has RF= {rf}x{rf}")
    print(f"P{i+3} has RF= {rf}x{rf}")

    for i, _b in enumerate(blocks_dets):
    rf = get_feature_node_RF(_b, [len(_b)-1])[-1]
    print(f"DET_P{i+3} has RF= {rf}x{rf}")
  2. braindevices revised this gist Jun 30, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cal_yolov8_rf.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # each bottleneck blcok has 3 conv that k>1
    btn = [(3,1)]*3
    # each bottleneck blcok has 4 conv that k>1
    btn = [(3,1)]*4
    # stem, C1 (1), C2 (2), conv, C3 (4), conv, C4 (6), conv, C2f, C5 (SPPF) (9)
    idx_Cs = [1,2,4,6,9]
    blocks = [
  3. braindevices revised this gist Jun 30, 2024. 1 changed file with 43 additions and 0 deletions.
    43 changes: 43 additions & 0 deletions cal_yolov8_rf.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    # each bottleneck blcok has 3 conv that k>1
    btn = [(3,1)]*3
    # stem, C1 (1), C2 (2), conv, C3 (4), conv, C4 (6), conv, C2f, C5 (SPPF) (9)
    idx_Cs = [1,2,4,6,9]
    blocks = [
    [(3,2)], # conv
    [(3,2)], # conv
    btn*1, # C2f n=3*d
    [(3,2)], # conv
    btn*2, # C2f n=6*d
    [(3,2)],
    btn*2, # C2f n=6*d
    [(3,2)],
    btn*1, # C2f n=3*d
    [(3,1), (5,1), (5,1), (5,1), (3, 1)] # SPPF
    ]

    blocks_P3 = blocks + [
    btn*1, # top-down C2f 3*d
    btn*1 # top-down C2f 3*d
    ]
    blocks_P4 = blocks_P3 + [
    [
    (3,2)], # bottom up Conv
    btn*1 # bottom-up C2f 3*d
    ]

    blocks_P5 = blocks_P4 + [
    [
    (3,2)], # bottom up Conv
    btn*1 # bottom-up C2f 3*d
    ]


    rfs_Cs = get_feature_node_RF(blocks, idx_Cs)
    for i, rf in enumerate(rfs_Cs):
    print(f"C{i+1} has RF= {rf}x{rf}")

    rf_P3 = get_feature_node_RF(blocks_P3, [len(blocks_P3)-1])[-1]
    rf_P4 = get_feature_node_RF(blocks_P4, [len(blocks_P4)-1])[-1]
    rf_P5 = get_feature_node_RF(blocks_P5, [len(blocks_P5)-1])[-1]
    for i, rf in enumerate([rf_P3, rf_P4, rf_P5]):
    print(f"P{i+3} has RF= {rf}x{rf}")
  4. braindevices created this gist Jun 30, 2024.
    33 changes: 33 additions & 0 deletions receptive_field.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    def calculate_receptive_fields(layers):
    """
    Calculate the receptive field for each layer given a list of (kernel size, stride) tuples.
    Parameters:
    layers (list of tuples): Each tuple contains (kernel size, stride) for a layer.
    Returns:
    list: Receptive field for each layer.
    """
    receptive_fields = []
    receptive_field = 1 # The initial receptive field for the first layer

    for i, (kernel_size, stride) in enumerate(layers):
    if i == 0:
    receptive_field = kernel_size
    else:
    receptive_field += (kernel_size - 1) * stride
    receptive_fields.append(receptive_field)

    return receptive_fields

    from itertools import chain
    from typing import List, List, Tuple
    import torch
    def get_feature_node_RF(blocks: List[List[Tuple[int, int]]], idxs_feature_node: List[int]):
    lens_block = [len(l) for l in blocks]
    idxs_block_out_layer = torch.cumsum(torch.tensor(lens_block), -1)-1
    idxs_layer_ft = idxs_block_out_layer[idxs_feature_node]
    layers_flatten = list(chain.from_iterable(blocks))
    rfs = calculate_receptive_fields(layers_flatten)
    return torch.tensor(rfs)[idxs_layer_ft]