Args: model (str, Path): model to load or create type (str): Type/version of models to use. Defaults to "v8". """ self.type = type self.ModelClass = None # model class self.TrainerClass = None # trainer class self.ValidatorClass = None # validator class self.PredictorClass = None # predictor class self.model = None # model object self.trainer = None # trainer object self.task = None # task type self.ckpt = None # if loaded from *.pt self.ckpt_path = None self.cfg = None # if loaded from *.yaml self.overrides = {} # overrides for trainer object self.init_disabled = False # disable model initialization
# Load or create new YOLO model {'.pt': self._load, '.yaml': self._new}[Path(model).suffix](model)
读取模型和配置是在
"__init__"
的最后一行:
# Load or create new YOLO model {'.pt': self._load, '.yaml': self._new}[Path(model).suffix](model)
def attempt_load_weights(weights, device=None, inplace=True, fuse=False): # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a from ultralytics.yolo.utils.downloads import attempt_download
model = Ensemble() for w in weights if isinstance(weights, list) else [weights]: ckpt = torch.load(attempt_download(w), map_location='cpu') # load args = {**DEFAULT_CONFIG_DICT, **ckpt['train_args']} # combine model and default args, preferring model args ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model
...
5.2、YOLOv5读取权重
def attempt_load(weights, device=None, inplace=True, fuse=True): # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a from models.yolo import Detect, Model
model = Ensemble() for w in weights if isinstance(weights, list) else [weights]: ckpt = torch.load(attempt_download(w), map_location='cpu') # load ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model