第二次分析(第一次的代码以找回,在下面一节)
在yolo.py里面加入如下代码并运行,注意在前面用sys.path.appen()加入utils和models文件夹的路径。
if __name__ == '__main__':
cfg = 'yolov5s.yaml'
ch = 3
import yaml
with open(cfg, encoding='ascii', errors='ignore') as f:
cfg = yaml.safe_load(f) #
model,save = parse_model(cfg,[ch])
y = []
x = torch.rand(4,3,640,640)
for m in model:
print(f'layer name:{
m.type}, from :{
m.f} index: {
m.i}')
if m.f != -1 :
x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f]
if isinstance(x,torch.Tensor):
print(f'shape of input: {
x.shape }')

本文详细分析了YoloV5s模型的内部结构,通过跟踪输入和输出的形状,揭示了模型中降维卷积、CSP结构、SPP模块、上采样和concat操作如何逐步改变特征尺寸并增加通道数,以实现目标检测。代码示例展示了每一层的作用,包括Focus、Conv、BottleneckCSP、SPP、Upsample和Detect等关键组件,最后指出Detect层接收来自不同尺度的特征图进行预测输出。
最低0.47元/天 解锁文章
2032

被折叠的 条评论
为什么被折叠?



