import argparse         # 解析命令行参数模块
import math             # 数学公式模块
import os               # 与操作系统进行交互的模块 包含文件路径操作和解析
import random           # 生成随机数的模块
import subprocess       # 该模块允许在 Python 程序中生成新的进程,连接到它们的输入/输出/错误管道,并获取它们的返回代码。这可以在 Python 程序中运行其他程序或命令。
import sys              # sys系统模块 包含了与Python解释器和它的环境有关的函数
import time             # 时间模块 更底层
from copy import deepcopy           # 深拷贝模块
from datetime import datetime       # 基本日期和时间类型模块
from pathlib import Path            # Path模块将str转换为Path对象 使字符串路径易于操作

try:
    import comet_ml                 # must be imported before torch (if installed)   必须在torch之前导入(如果安装)
except ImportError:                 # 用于导入Comet ML库,启用实验管理和机器学习项目跟踪的功能。
    comet_ml = None

import numpy as np                      # numpy数组操作模块
import torch                            # torch深度学习框架
import torch.distributed as dist        # 分布式训练模块
import torch.nn as nn                   # 对torch.nn.functional的类的封装 有很多和oneflow.nn.functional相同的函数
import yaml                             # 操作yaml文件模块
from torch.optim import lr_scheduler    # 学习率模块
from tqdm import tqdm                   # 进度条模块

FILE = Path(__file__).resolve()                         # 获取当前脚本文件的绝对路径,并将其存储在 FILE 变量中,方便在代码中引用和操作当前脚本文件。
ROOT = FILE.parents[0]                                  # YOLOv5 root directory (YOLOv5根目录)
if str(ROOT) not in sys.path:
    sys.path.append(str(ROOT))                          # add ROOT to PATH (添加根目录到路径)
ROOT = Path(os.path.relpath(ROOT, Path.cwd()))          # relative

import val as validate                                  # for end-of-epoch mAP  (导入val.py)
from models.experimental import attempt_load            # 导入在线下载模块
from models.yolo import Model                           # 导入YOLOv5的模型定义
from utils.autoanchor import check_anchors              # 导入检查anchors合法性的函数
from utils.autobatch import check_train_batch_size      # 用于在YOLOv5训练过程中检查和验证批处理大小,以确保与GPU内存兼容。
from utils.callbacks import Callbacks                   # 和日志相关的回调函数
from utils.dataloaders import create_dataloader         # 加载数据集的函数
from utils.downloads import attempt_download, is_url    # 尝试下载指定的文件,并提供断点续传功能,确保下载的可靠性和完整性//判断当前字符串是否是链接
from utils.general import (LOGGER, TQDM_BAR_FORMAT, check_amp, check_dataset, check_file, check_git_info,
                           check_git_status, check_img_size, check_requirements, check_suffix, check_yaml, colorstr,
                           get_latest_run, increment_path, init_seeds, intersect_dicts, labels_to_class_weights,
                           labels_to_image_weights, methods, one_cycle, print_args, print_mutation, strip_optimizer,
                           yaml_save)                               # 导入utils.general文件中各种函数
from utils.loggers import Loggers                                   # 导入日志管理模块
from utils.loggers.comet.comet_utils import check_comet_resume      # 用于检查是否可以从Comet ML平台恢复实验,以便继续记录和分析。
from utils.loss import ComputeLoss                                  # 导入计算Loss的模块
from utils.metrics import fitness                                   # 在YOLOv5中,fitness函数实现对 [P, R, [email protected], [email protected]] 指标进行加权
from utils.plots import plot_evolve                                 # 用于绘制进化算法的进化图,可视化进化过程和结果,帮助用户理解算法效果和变化趋势。
from utils.torch_utils import (EarlyStopping, ModelEMA, de_parallel, select_device, smart_DDP, smart_optimizer,
                               smart_resume, torch_distributed_zero_first)      # 导入utils.torch_utils文件中各种函数

# LOCAL_RANK:当前进程对应的GPU号。
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1))   # <https://pytorch.org/docs/stable/elastic/run.html>
RANK = int(os.getenv('RANK', -1))               # RANK:当前进程的序号,用于进程间通讯,rank = 0 的主机为 master 节点。
WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))    # WORLD_SIZE:总的进程数量 (原则上第一个process占用一个GPU是较优的)。
GIT_INFO = check_git_info()                     # 获取当前代码的 Git 信息,并将其存储在 GIT_INFO 变量中以供使用。

check_train_batch_size

utils.general

from utils.downloads import attempt_download

from utils.loggers.comet.comet_utils

import comet_ml

from utils.plots import plot_evolve

from utils.torch_utils

GIT_INFO = check_git_info()

FILE = Path(__file__).resolve()