2021SC@SDUSC Application and practice of software engineering in school of software, Shandong University -- yolov5 code analysis -- Part 5 -- train py

catalogue

Import third party libraries

parse_opt function

Import third party libraries

import argparse
import logging
import math
import os
import random
import sys
import time
from copy import deepcopy
from pathlib import Path

import numpy as np
import torch
import torch.distributed as dist
import torch.nn as nn
import yaml
from torch.cuda import amp
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.optim import Adam, SGD, lr_scheduler
from tqdm import tqdm

FILE = Path(__file__).resolve()
ROOT = FILE.parents[0]  # YOLOv5 root directory
if str(ROOT) not in sys.path:
    sys.path.append(str(ROOT))  # add ROOT to PATH

import val  # for end-of-epoch mAP
from models.experimental import attempt_load
from models.yolo import Model
from utils.autoanchor import check_anchors
from utils.datasets import create_dataloader
from utils.general import labels_to_class_weights, increment_path, labels_to_image_weights, init_seeds, \
    strip_optimizer, get_latest_run, check_dataset, check_git_status, check_img_size, check_requirements, \
    check_file, check_yaml, check_suffix, print_args, print_mutation, set_logging, one_cycle, colorstr, methods
from utils.downloads import attempt_download
from utils.loss import ComputeLoss
from utils.plots import plot_labels, plot_evolve
from utils.torch_utils import EarlyStopping, ModelEMA, de_parallel, intersect_dicts, select_device, \
    torch_distributed_zero_first
from utils.loggers.wandb.wandb_utils import check_wandb_resume
from utils.metrics import fitness
from utils.loggers import Loggers
from utils.callbacks import Callbacks

argparse: parse command line parameter module

loggin: log module

math: mathematical formula module

os: the module that interacts with the operating system, including file path operation and parsing

Random: module for generating random numbers

Sys: the sys system module contains functions related to the Python interpreter and its environment

Time: time module

warnings: module for issuing warning messages

Deep copy: deep copy module

Path: path is a module that converts str to a path object, making string paths easy to manipulate

Thread: thread operation module

parse_opt function

def parse_opt(known=False):
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', type=str, default='yolov5s.pt', help='initial weights path')
    parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
    parser.add_argument('--data', type=str, default='data/coco128.yaml', help='dataset.yaml path')
    parser.add_argument('--hyp', type=str, default='data/hyps/hyp.scratch.yaml', help='hyperparameters path')
    parser.add_argument('--epochs', type=int, default=300)
    parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs')
    parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)')
    parser.add_argument('--rect', action='store_true', help='rectangular training')
    parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
    parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
    parser.add_argument('--noval', action='store_true', help='only validate final epoch')
    parser.add_argument('--noautoanchor', action='store_true', help='disable autoanchor check')
    parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')
    parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
    parser.add_argument('--cache', type=str, nargs='?', const='ram', help='--cache images in "ram" (default) or "disk"')
    parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
    parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
    parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
    parser.add_argument('--adam', action='store_true', help='use torch.optim.Adam() optimizer')
    parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
    parser.add_argument('--workers', type=int, default=8, help='maximum number of dataloader workers')
    parser.add_argument('--project', default='runs/train', help='save to project/name')
    parser.add_argument('--entity', default=None, help='W&B entity')
    parser.add_argument('--name', default='exp', help='save to project/name')
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
    parser.add_argument('--quad', action='store_true', help='quad dataloader')
    parser.add_argument('--linear-lr', action='store_true', help='linear LR')
    parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
    parser.add_argument('--upload_dataset', action='store_true', help='Upload dataset as W&B artifact table')
    parser.add_argument('--bbox_interval', type=int, default=-1, help='Set bounding-box image logging interval for W&B')
    parser.add_argument('--save_period', type=int, default=-1, help='Log model after every "save_period" epoch')
    parser.add_argument('--artifact_alias', type=str, default="latest", help='version of dataset artifact to be used')
    parser.add_argument('--local_rank', type=int, default=-1, help='DDP parameter, do not modify')
    parser.add_argument('--freeze', type=int, default=0, help='Number of layers to freeze. backbone=10, all=24')
    parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')
    opt = parser.parse_known_args()[0] if known else parser.parse_args()
    return opt

weights: weight file

cfg: the model configuration file includes nc and depth_multiple,width_multiple, anchors, backbone, head, etc

data: dataset configuration files include path, train, val, test, nc, names, download, etc

hyp: initial hyperparameter file

Epichs: training rounds

Batch size: training batch size

IMG size: enter the image resolution size of the network

resume: continue training at breakpoint. Continue training from the training result interrupted last time. The default is False

nosave: do not save the model. Default: false (save)

notest: do you want to test only the last round of default False

Workers: maximum number of work in dataloader (number of threads)

Device: training device

Single CLS: whether the dataset has only one category. The default is False

rect: whether the training set adopts rectangular training. The default is False

noautoanchor: do not automatically adjust anchor. Default: false (automatically adjust anchor)

evolve: whether to carry out hyperparametric evolution. The default is False

Multi scale: whether to use multi-scale training. The default is False

Label smoothing: the default value of label smoothing enhancement is 0.0. If you want to enhance it, it is generally set to 0.1

adam: whether to use adam optimizer default false (use SGD)

Sync bn: whether to use cross card synchronization bn operation, and then use the default False in DDP

linear lr: whether to use linear lr linear learning rate. The default is False. Use cosine lr

Cache image: whether to cache pictures to memory cache in advance to speed up training. Default: False

Image weights: whether to use the image adoption policy (selection img to training by class weights). The default is False and not used

bucket: Google cloud disk buckets are generally not used

project: the root directory where training results are saved is runs/train by default

name: the directory for saving training results is exp by default: runs/train/exp

Exist ok: if the file exists, ok. If it does not exist, create a new file or increment name. The default is false (the default file does not exist)

Quad: whether to use collate when the dataloader fetches data_ Fn4 instead of collate_fn default False

save_period: Log model after every "save_period" epoch default - 1 no log model information is required

artifact_ Alias: which version of dataset artifact to be striped? The default lastest does not seem to use this parameter

local_ Rank: when rank is the process number - 1 and gpu=1, no distributed - 1 is performed, and multiple GPUs use DataParallel mode

entity: wandb entity defaults to None

upload_dataset: whether to upload dataset to wandb tower (view, query, filter and analyze dataset in browser as interactive dsviz table). Default: False

bbox_interval: set bounding box image logging interval for W & B default - 1 opt epochs // 10

Keywords: TensorFlow Pytorch Deep Learning yolov5

Added by daredevil88 on Tue, 28 Dec 2021 15:46:03 +0200