Răsfoiți Sursa

ImageNet pretrained for YOLOv8

yjh0410 2 ani în urmă
părinte
comite
36d7f8295a
4 a modificat fișierele cu 69 adăugiri și 12 ștergeri
  1. 5 5
      README.md
  2. 5 5
      README_CN.md
  3. 5 0
      config/yolov8_config.py
  4. 54 2
      models/yolov8/yolov8_backbone.py

+ 5 - 5
README.md

@@ -110,11 +110,11 @@ python train.py --cuda -d coco --root path/to/COCO -v yolov1 -bs 16 --max_epoch
 | YOLOv7-Tiny   | ELANNet-Tiny       |  640  |  √   |  300  |       |                        |                   |  |
 | YOLOv7-Large  | ELANNet-Large      |  640  |  √   |  300  |       |                        |                   |  |
 | YOLOv7-Huge   | ELANNet-Huge       |  640  |  √   |  300  |       |                        |                   |  |
-| YOLOv8-Nano   | CSP-ELANNet-Nano   |  640  |  ×   |  500  |       |                        |                   |  |
-| YOLOv8-Small  | CSP-ELANNet-Small  |  640  |  ×   |  500  |       |                        |                   |  |
-| YOLOv8-Medium | CSP-ELANNet-Medium |  640  |  ×   |  500  |       |                        |                   |  |
-| YOLOv8-Large  | CSP-ELANNet-Large  |  640  |  ×   |  500  |       |                        |                   |  |
-| YOLOv8-Huge   | CSP-ELANNet-Large  |  640  |  ×   |  500  |       |                        |                   |  |
+| YOLOv8-Nano   | CSP-ELANNet-Nano   |  640  |  √   |  300  |       |                        |                   |  |
+| YOLOv8-Small  | CSP-ELANNet-Small  |  640  |  √   |  300  |       |                        |                   |  |
+| YOLOv8-Medium | CSP-ELANNet-Medium |  640  |  √   |  300  |       |                        |                   |  |
+| YOLOv8-Large  | CSP-ELANNet-Large  |  640  |  √   |  300  |       |                        |                   |  |
+| YOLOv8-Huge   | CSP-ELANNet-Large  |  640  |  √   |  300  |       |                        |                   |  |
 
 *All models are trained with ImageNet pretrained weight (IP). All FLOPs are measured with a 640x640 image size on COCO val2017. The FPS is measured with batch size 1 on 3090 GPU from the model inference to the NMS operation.*
 

+ 5 - 5
README_CN.md

@@ -113,11 +113,11 @@ python train.py --cuda -d coco --root path/to/COCO -v yolov1 -bs 16 --max_epoch
 | YOLOv7-Tiny   | ELANNet-Tiny       |  640  |  √   |  300  |       |                        |                   |  |
 | YOLOv7-Large  | ELANNet-Large      |  640  |  √   |  300  |       |                        |                   |  |
 | YOLOv7-Huge   | ELANNet-Huge       |  640  |  √   |  300  |       |                        |                   |  |
-| YOLOv8-Nano   | CSP-ELANNet-Nano   |  640  |  ×   |  500  |       |                        |                   |  |
-| YOLOv8-Small  | CSP-ELANNet-Small  |  640  |  ×   |  500  |       |                        |                   |  |
-| YOLOv8-Medium | CSP-ELANNet-Medium |  640  |  ×   |  500  |       |                        |                   |  |
-| YOLOv8-Large  | CSP-ELANNet-Large  |  640  |  ×   |  500  |       |                        |                   |  |
-| YOLOv8-Huge   | CSP-ELANNet-Large  |  640  |  ×   |  500  |       |                        |                   |  |
+| YOLOv8-Nano   | CSP-ELANNet-Nano   |  640  |  √   |  300  |       |                        |                   |  |
+| YOLOv8-Small  | CSP-ELANNet-Small  |  640  |  √   |  300  |       |                        |                   |  |
+| YOLOv8-Medium | CSP-ELANNet-Medium |  640  |  √   |  300  |       |                        |                   |  |
+| YOLOv8-Large  | CSP-ELANNet-Large  |  640  |  √   |  300  |       |                        |                   |  |
+| YOLOv8-Huge   | CSP-ELANNet-Large  |  640  |  √   |  300  |       |                        |                   |  |
 
 *所有的模型都使用了ImageNet预训练权重(IP),所有的FLOPs都是在COCO-val数据集上以640x640或1280x1280的输入尺寸来测试的。FPS指标是在一张3090型号的GPU上以batch size=1的输入来测试的,请注意,测速的内容包括模型前向推理、后处理以及NMS操作。*
 

+ 5 - 0
config/yolov8_config.py

@@ -7,6 +7,7 @@ yolov8_cfg = {
         'multi_scale': [0.5, 1.5],   # 320 -> 960
         # model
         'backbone': 'elan_cspnet',
+        'pretrained': True,
         'bk_act': 'silu',
         'bk_norm': 'BN',
         'bk_dpw': False,
@@ -67,6 +68,7 @@ yolov8_cfg = {
         'multi_scale': [0.5, 1.5],   # 320 -> 960
         # model
         'backbone': 'elan_cspnet',
+        'pretrained': True,
         'bk_act': 'silu',
         'bk_norm': 'BN',
         'bk_dpw': False,
@@ -127,6 +129,7 @@ yolov8_cfg = {
         'multi_scale': [0.5, 1.5],   # 320 -> 960
         # model
         'backbone': 'elan_cspnet',
+        'pretrained': True,
         'bk_act': 'silu',
         'bk_norm': 'BN',
         'bk_dpw': False,
@@ -187,6 +190,7 @@ yolov8_cfg = {
         'multi_scale': [0.5, 1.5],   # 320 -> 960
         # model
         'backbone': 'elan_cspnet',
+        'pretrained': True,
         'bk_act': 'silu',
         'bk_norm': 'BN',
         'bk_dpw': False,
@@ -247,6 +251,7 @@ yolov8_cfg = {
         'multi_scale': [0.5, 1.5],   # 320 -> 960
         # model
         'backbone': 'elan_cspnet',
+        'pretrained': True,
         'bk_act': 'silu',
         'bk_norm': 'BN',
         'bk_dpw': False,

+ 54 - 2
models/yolov8/yolov8_backbone.py

@@ -7,7 +7,17 @@ except:
     from yolov8_basic import Conv, ELAN_CSP_Block
 
 
-# ---------------------------- Backbones ----------------------------
+# ---------------------------- ImageNet pretrained weights ----------------------------
+model_urls = {
+    'elan_cspnet_nano': "https://github.com/yjh0410/image_classification_pytorch/releases/download/weight/elan_cspnet_nano.pth",
+    'elan_cspnet_small': None,
+    'elan_cspnet_medium': None,
+    'elan_cspnet_large': "https://github.com/yjh0410/image_classification_pytorch/releases/download/weight/elan_cspnet_large.pth",
+    'elan_cspnet_huge': None,
+}
+
+
+# ---------------------------- Basic functions ----------------------------
 ## ELAN-CSPNet
 class ELAN_CSPNet(nn.Module):
     def __init__(self, width=1.0, depth=1.0, ratio=1.0, act_type='silu', norm_type='BN', depthwise=False):
@@ -56,6 +66,36 @@ class ELAN_CSPNet(nn.Module):
 
 
 # ---------------------------- Functions ----------------------------
+## load pretrained weight
+def load_weight(model, model_name):
+    # load weight
+    print('Loading pretrained weight ...')
+    url = model_urls[model_name]
+    if url is not None:
+        checkpoint = torch.hub.load_state_dict_from_url(
+            url=url, map_location="cpu", check_hash=True)
+        # checkpoint state dict
+        checkpoint_state_dict = checkpoint.pop("model")
+        # model state dict
+        model_state_dict = model.state_dict()
+        # check
+        for k in list(checkpoint_state_dict.keys()):
+            if k in model_state_dict:
+                shape_model = tuple(model_state_dict[k].shape)
+                shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
+                if shape_model != shape_checkpoint:
+                    checkpoint_state_dict.pop(k)
+            else:
+                checkpoint_state_dict.pop(k)
+                print(k)
+
+        model.load_state_dict(checkpoint_state_dict)
+    else:
+        print('No pretrained for {}'.format(model_name))
+
+    return model
+
+
 ## build ELAN-Net
 def build_backbone(cfg): 
     # model
@@ -67,8 +107,20 @@ def build_backbone(cfg):
         norm_type=cfg['bk_norm'],
         depthwise=cfg['bk_dpw']
         )
-        
     feat_dims = backbone.feat_dims
+        
+    # check whether to load imagenet pretrained weight
+    if cfg['pretrained']:
+        if cfg['width'] == 0.25 and cfg['depth'] == 0.34 and cfg['ratio'] == 2.0:
+            backbone = load_weight(backbone, model_name='elan_cspnet_nano')
+        elif cfg['width'] == 0.5 and cfg['depth'] == 0.34 and cfg['ratio'] == 2.0:
+            backbone = load_weight(backbone, model_name='elan_cspnet_small')
+        elif cfg['width'] == 0.75 and cfg['depth'] == 0.67 and cfg['ratio'] == 1.5:
+            backbone = load_weight(backbone, model_name='elan_cspnet_medium')
+        elif cfg['width'] == 1.0 and cfg['depth'] == 1.0 and cfg['ratio'] == 1.0:
+            backbone = load_weight(backbone, model_name='elan_cspnet_large')
+        elif cfg['width'] == 1.25 and cfg['depth'] == 1.34 and cfg['ratio'] == 1.0:
+            backbone = load_weight(backbone, model_name='elan_cspnet_huge')
 
     return backbone, feat_dims