Last active
June 30, 2024 01:05
-
-
Save braindevices/7a7d290acfb32a6763d51f25f73ec302 to your computer and use it in GitHub Desktop.
receptive field of yolov8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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,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 | |
] | |
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): | |
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}") | |
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment