Yolox code combing

1. Registration class

I was confused at the beginning. The configuration file read is exp/default/yolox_s.py, but this file does not exist. The similar file is exps/default/yolox_s.py. This file exists, but the path and configuration are wrong. I once thought it was a typo. After all, there is not much difference between exp and exps. until I see the content of init.py

First, we configured yolox_s in train.py
insert image description here
and then looked at
insert image description here
insert image description here
insert image description here
this function. First, replace - _ and then combine them together to get module_name=yolox.exp.default.yolox_s
and then import the exp() object of this file. But we did not find this file in this path.
insert image description here
There is only one init.py without yolox_s, but it will go to init.py to execute first, then init.py is the one below

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Copyright (c) Megvii Inc. All rights reserved.

# This file is used for package installation and find default exp file

import importlib
import sys
from pathlib import Path

_EXP_PATH = Path(__file__).resolve().parent.parent.parent.parent / "exps" / "default"  # 正确的路径

if _EXP_PATH.is_dir():  # 如果这个路径存在
    # This is true only for in-place installation (pip install -e, setup.py develop),
    # where setup(package_dir=) does not work: https://github.com/pypa/setuptools/issues/230

    class _ExpFinder(importlib.abc.MetaPathFinder): 
        
        def find_spec(self, name, path, target=None):  #然后自动执行find_spec方法  name就是传进来的module_name=yolox.exp.default.yolox_s
            if not name.startswith("yolox.exp.default"):
                return 
            project_name = name.split(".")[-1] + ".py"   #最后一个就是yolox_s.py
            target_file = _EXP_PATH / project_name  #路径拼接就是 exps/default/yolox_s.py
            if not target_file.is_file():
                return
            return importlib.util.spec_from_file_location(name, target_file)   然后返回yolox_s.py模块

    sys.meta_path.append(_ExpFinder())  # 就会把这个_expfinder加入字典中

After getting the yolox_s.py module, you can only generate exp() objects

2. The following code is really nothing bright, it is all step by step, boring

Guess you like

Origin blog.csdn.net/qq_33228039/article/details/127549094