Press "Enter" to skip to content

目标检测 YOLOv5 – 模型的样子

本站内容均来自兴趣收集,如不慎侵害的您的相关权益,请留言告知,我们将尽快删除.谢谢.

 

目标检测 YOLOv5 – 模型的样子

 

flyfish

 

文章目录

目标检测 YOLOv5 – 模型的样子

版本: YOLOv5:v5

 

以yolov5s.pt为例查看模型的样子

 

开始加载模型文件

 

yolov5s.pt

 

def attempt_load(weights, map_location=None):

 

模型内容如下

 

ckpt: {
 
'epoch': -1,
'best_fitness': array([0.37287]),
'training_results': None,
'model': Model(
(model): Sequential(
(0): Focus(
(conv): Conv(
(conv): Conv2d(12, 32, kernel_size = (3, 3), stride = (1, 1), padding = (1, 1), bias = False)
(bn): BatchNorm2d(32, eps = 0.001, momentum = 0.03, affine = True, track_running_stats = True)
(act): SiLU()
)
)
......
(24): Detect(
(m): ModuleList(
(0): Conv2d(128, 255, kernel_size = (1, 1), stride = (1, 1))
(1): Conv2d(256, 255, kernel_size = (1, 1), stride = (1, 1))
(2): Conv2d(512, 255, kernel_size = (1, 1), stride = (1, 1))
)
)
)
),
'optimizer': None,
'wandb_id': None
}

 

模型的层

 

具体哪一层可以通过model.model[下标]的形式获取。负数表示倒着获取

 

倒数第一层

 

model.model[-1]

 

Detect(
  (m): ModuleList(
    (0): Conv2d(128, 255, kernel_size=(1, 1), stride=(1, 1))
    (1): Conv2d(256, 255, kernel_size=(1, 1), stride=(1, 1))
    (2): Conv2d(512, 255, kernel_size=(1, 1), stride=(1, 1))
  )
)

 

倒数第二层

 

model.model[-2]

 

C3(
  (cv1): Conv(
    (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1))
    (act): SiLU(inplace=True)
  )
  (cv2): Conv(
    (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1))
    (act): SiLU(inplace=True)
  )
  (cv3): Conv(
    (conv): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1))
    (act): SiLU(inplace=True)
  )
  (m): Sequential(
    (0): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU(inplace=True)
      )
      (cv2): Conv(
        (conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (act): SiLU(inplace=True)
      )
    )
  )
)

 

模型的属性

 

for name,value in vars(model).items():
        print('%s=%s'%(name,value))

 

例如获取类别名称model.names

 

可以获取的属性包括以下内容

 

training = False
_parameters = OrderedDict()
_buffers = OrderedDict()
_non_persistent_buffers_set = set()
_backward_hooks = OrderedDict()
_forward_hooks = OrderedDict()
_forward_pre_hooks = OrderedDict()
_state_dict_hooks = OrderedDict()
_load_state_dict_pre_hooks = OrderedDict()
_modules = OrderedDict([('model', Sequential(
(0): Focus(
(conv): Conv(
(conv): Conv2d(12, 32, kernel_size = (3, 3), stride = (1, 1), padding = (1, 1))
(act): SiLU(inplace = True)
)
)
......
(24): Detect(
(m): ModuleList(
(0): Conv2d(128, 255, kernel_size = (1, 1), stride = (1, 1))
(1): Conv2d(256, 255, kernel_size = (1, 1), stride = (1, 1))
(2): Conv2d(512, 255, kernel_size = (1, 1), stride = (1, 1))
)
)
))])
yaml = {
 
'nc': 80,
'depth_multiple': 0.33,
'width_multiple': 0.5,
'anchors': [
[10, 13, 16, 30, 33, 23],
[30, 61, 62, 45, 59, 119],
[116, 90, 156, 198, 373, 326]
],
'backbone': [
[-1, 1, 'Focus', [64, 3]],
[-1, 1, 'Conv', [128, 3, 2]],
[-1, 3, 'C3', [128]],
[-1, 1, 'Conv', [256, 3, 2]],
[-1, 9, 'C3', [256]],
[-1, 1, 'Conv', [512, 3, 2]],
[-1, 9, 'C3', [512]],
[-1, 1, 'Conv', [1024, 3, 2]],
[-1, 1, 'SPP', [1024, [5, 9, 13]]],
[-1, 3, 'C3', [1024, False]]
],
'head': [
[-1, 1, 'Conv', [512, 1, 1]],
[-1, 1, 'nn.Upsample', ['None', 2, 'nearest']],
[
[-1, 6], 1, 'Concat', [1]
],
[-1, 3, 'C3', [512, False]],
[-1, 1, 'Conv', [256, 1, 1]],
[-1, 1, 'nn.Upsample', ['None', 2, 'nearest']],
[
[-1, 4], 1, 'Concat', [1]
],
[-1, 3, 'C3', [256, False]],
[-1, 1, 'Conv', [256, 3, 2]],
[
[-1, 14], 1, 'Concat', [1]
],
[-1, 3, 'C3', [512, False]],
[-1, 1, 'Conv', [512, 3, 2]],
[
[-1, 10], 1, 'Concat', [1]
],
[-1, 3, 'C3', [1024, False]],
[
[17, 20, 23], 1, 'Detect', ['nc', 'anchors']
]
],
'ch': 3
}
save = [4, 6, 10, 14, 17, 20, 23]
names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 
 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 
 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
stride = tensor([8., 16., 32.])
nc = 80
hyp = {
 
'lr0': 0.01,
'lrf': 0.2,
'momentum': 0.937,
'weight_decay': 0.0005,
'warmup_epochs': 3.0,
'warmup_momentum': 0.8,
'warmup_bias_lr': 0.1,
'box': 0.05,
'cls': 0.3,
'cls_pw': 1.0,
'obj': 0.7,
'obj_pw': 1.0,
'iou_t': 0.2,
'anchor_t': 4.0,
'fl_gamma': 0.0,
'hsv_h': 0.015,
'hsv_s': 0.7,
'hsv_v': 0.4,
'degrees': 0.0,
'translate': 0.1,
'scale': 0.9,
'shear': 0.0,
'perspective': 0.0,
'flipud': 0.0,
'fliplr': 0.5,
'mosaic': 1.0,
'mixup': 0.01
}
gr = 1.0
class_weights = tensor([1.20752e-02, ......, 2.12184e+00, 6.56878e-01, 1.56888e+01, 1.59711e+00
], dtype = torch.float64)
_is_full_backward_hook = None

 

模块的名称以及模块本身

 

代码

 

for name, module in model.named_modules():
    print(name)

 

这里只输出名字

 

model
model.0
model.0.conv
model.0.conv.conv
model.0.conv.act
model.1
model.1.conv
model.1.act
......
model.22
model.23
model.23.cv1
model.23.cv1.conv
model.23.cv1.act
model.23.cv2
model.23.cv2.conv
model.23.cv2.act
model.23.cv3
model.23.cv3.conv
model.23.cv3.act
model.23.m
model.23.m.0
model.23.m.0.cv1
model.23.m.0.cv1.conv
model.23.m.0.cv1.act
model.23.m.0.cv2
model.23.m.0.cv2.conv
model.23.m.0.cv2.act
model.24
model.24.m
model.24.m.0
model.24.m.1
model.24.m.2

 

模型的权重

 

代码

 

list(model.parameters())[0]

 

内容

 

Parameter containing:
tensor([[[[-2.73007e-01, -1.44108e+00, -1.18709e+00],
          [-3.13122e-01, -9.47540e-01, -7.23963e-01],
          [ 4.31564e-01,  2.05478e+00,  1.50800e+00]],
......
         [[ 8.07673e-01,  1.51624e+00,  2.86977e-01],
          [-1.81044e+00, -1.48112e+00, -6.51210e-01],
          [-5.19720e-01, -1.99577e-01,  1.39392e-01]]]])

 

模型权重的名字和权重

 

代码

 

list(model.named_parameters())

 

model.0.conv.conv.weight是名字

 

('model.0.conv.conv.weight', Parameter containing:
tensor([[[[-2.73007e-01, -1.44108e+00, -1.18709e+00],
          [-3.13122e-01, -9.47540e-01, -7.23963e-01],
          [ 4.31564e-01,  2.05478e+00,  1.50800e+00]],
......
         [[ 8.07673e-01,  1.51624e+00,  2.86977e-01],
          [-1.81044e+00, -1.48112e+00, -6.51210e-01],
          [-5.19720e-01, -1.99577e-01,  1.39392e-01]]]]))

 

完整的model

 

Model(
  (model): Sequential(
    (0): Focus(
      (conv): Conv(
        (conv): Conv2d(12, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (act): SiLU()
      )
    )
    (1): Conv(
      (conv): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
      (act): SiLU()
    )
    (2): C3(
      (cv1): Conv(
        (conv): Conv2d(64, 32, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(64, 32, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv3): Conv(
        (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (m): Sequential(
        (0): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(32, 32, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
      )
    )
    (3): Conv(
      (conv): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
      (act): SiLU()
    )
    (4): C3(
      (cv1): Conv(
        (conv): Conv2d(128, 64, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(128, 64, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv3): Conv(
        (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (m): Sequential(
        (0): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
        (1): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
        (2): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
      )
    )
    (5): Conv(
      (conv): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
      (act): SiLU()
    )
    (6): C3(
      (cv1): Conv(
        (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv3): Conv(
        (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (m): Sequential(
        (0): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
        (1): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
        (2): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
      )
    )
    (7): Conv(
      (conv): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
      (act): SiLU()
    )
    (8): SPP(
      (cv1): Conv(
        (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (m): ModuleList(
        (0): MaxPool2d(kernel_size=5, stride=1, padding=2, dilation=1, ceil_mode=False)
        (1): MaxPool2d(kernel_size=9, stride=1, padding=4, dilation=1, ceil_mode=False)
        (2): MaxPool2d(kernel_size=13, stride=1, padding=6, dilation=1, ceil_mode=False)
      )
    )
    (9): C3(
      (cv1): Conv(
        (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv3): Conv(
        (conv): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (m): Sequential(
        (0): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
      )
    )
    (10): Conv(
      (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1))
      (act): SiLU()
    )
    (11): Upsample(scale_factor=2.0, mode=nearest)
    (12): Concat()
    (13): C3(
      (cv1): Conv(
        (conv): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv3): Conv(
        (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (m): Sequential(
        (0): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
      )
    )
    (14): Conv(
      (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1))
      (act): SiLU()
    )
    (15): Upsample(scale_factor=2.0, mode=nearest)
    (16): Concat()
    (17): C3(
      (cv1): Conv(
        (conv): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv3): Conv(
        (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (m): Sequential(
        (0): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
      )
    )
    (18): Conv(
      (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
      (act): SiLU()
    )
    (19): Concat()
    (20): C3(
      (cv1): Conv(
        (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv3): Conv(
        (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (m): Sequential(
        (0): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
      )
    )
    (21): Conv(
      (conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
      (act): SiLU()
    )
    (22): Concat()
    (23): C3(
      (cv1): Conv(
        (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (cv3): Conv(
        (conv): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1))
        (act): SiLU()
      )
      (m): Sequential(
        (0): Bottleneck(
          (cv1): Conv(
            (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1))
            (act): SiLU()
          )
          (cv2): Conv(
            (conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
            (act): SiLU()
          )
        )
      )
    )
    (24): Detect(
      (m): ModuleList(
        (0): Conv2d(128, 255, kernel_size=(1, 1), stride=(1, 1))
        (1): Conv2d(256, 255, kernel_size=(1, 1), stride=(1, 1))
        (2): Conv2d(512, 255, kernel_size=(1, 1), stride=(1, 1))
      )
    )
  )
)

 

完整模型图片展示

 

图片较大,可下载看

 

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。