网站流量用完,网站建设买阿里云云服务器,搜狐快站建站教程,投广告哪个平台好使用argparse模块来定义和解析命令行参数
创建一个ArgumentParser对象
parser argparse.ArgumentParser()
训练的轮数#xff0c;每批图像的大小#xff0c;更新模型参数之前累积梯度的次数,模型定义文件的路径。
parser.add_argument(--epochs, typeint, d…使用argparse模块来定义和解析命令行参数
创建一个ArgumentParser对象
parser argparse.ArgumentParser()
训练的轮数每批图像的大小更新模型参数之前累积梯度的次数,模型定义文件的路径。
parser.add_argument(--epochs, typeint, default100, helpnumber of epochs) #训练次数parser.add_argument(--batch_size, typeint, default1, helpsize of each image batch) #batch的大小parser.add_argument(--gradient_accumulations, typeint, default2, helpnumber of gradient accums before step)#在每一步更新模型参数之前累积梯度的次数”parser.add_argument(--model_def, typestr, defaultconfig/yolov3.cfg, helppath to model definition file) #模型的配置文件
数据配置文件的路径从预训练的模型权重开始训练生成批次数据时使用的CPU线程数。
parser.add_argument(--data_config, typestr, defaultconfig/coco.data, helppath to data config file) #数据的配置文件parser.add_argument(--pretrained_weights, typestr, helpif specified starts from checkpoint model) #预训练文件parser.add_argument(--n_cpu, typeint, default0, helpnumber of cpu threads to use during batch generation)#数据加载过程中应使用的CPU线程数。
每张图像的尺寸每隔多少个epoch保存一次模型权重每隔多少个epoch在验证集上进行一次评估每十批计算一次平均精度mAP是否允许多尺度训练
parser.add_argument(--img_size, typeint, default416, helpsize of each image dimension)parser.add_argument(--checkpoint_interval, typeint, default20, helpinterval between saving model weights)#隔多少个epoch保存一次模型权重parser.add_argument(--evaluation_interval, typeint, default20, helpinterval evaluations on validation set)#多少个epoch进行一次验证集的验证parser.add_argument(--compute_map, defaultFalse, helpif True computes mAP every tenth batch)#parser.add_argument(--multiscale_training, defaultTrue, helpallow for multi-scale training)
使用parse_args方法解析命令行参数
opt parser.parse_args() 使用TensorFlow 2.0以上版本中的tf.summary模块创建日志记录器Logger
import tensorflow as tf # 导入TensorFlow库并简写为tf
# 确保使用的是TensorFlow 2.0或更高版本class Logger(object):def __init__(self, log_dir):Create a summary writer logging to log_dir.这个类的构造函数接受一个参数log_dir它表示日志文件将要保存的目录。函数的作用是创建一个日志记录器用于记录TensorFlow的摘要信息例如训练过程中的损失、准确率等。self.writer tf.summary.create_file_writer(log_dir) # 创建一个文件写入器用于将摘要信息写入到指定的日志目录
调用Logger创建目录
logger Logger(logs)
# 创建Logger类的实例并将日志目录设置为logs。这意味着所有的日志信息将被写入到当前工作目录下的logs文件夹中。device torch.device(cuda if torch.cuda.is_available() else cpu)
# 这行代码使用PyTorch的torch.device来确定运行设备。如果系统有可用的CUDA即NVIDIA的GPU则device将被设置为cuda否则将使用CPU。os.makedirs(output, exist_okTrue)
# 使用os模块的makedirs函数创建一个名为output的目录。exist_okTrue参数意味着如果output目录已经存在不会抛出错误。os.makedirs(checkpoints, exist_okTrue)
# 类似地这行代码创建一个名为checkpoints的目录。这个目录通常用于存储模型的检查点或保存的状态以便后续可以恢复训练或进行模型评估。
定义 parse_data_config 的函数它用于解析数据配置文件(文件内容分类种类训练集路径测试集路径文件名称路径) def parse_data_config(path):Parses the data configuration fileoptions dict()options[gpus] 0,1,2,3options[num_workers] 10with open(path, r) as fp:lines fp.readlines()for line in lines:line line.strip()if line or line.startswith(#):#startswith()用于检查字符串是否以特定的子字符串开始。如果是它将返回True否则返回False。continuekey, value line.split()options[key.strip()] value.strip()return options
调用函数 data_config parse_data_config(opt.data_config)train_path data_config[train]valid_path data_config[valid]
定义了一个名为 load_classes 的函数它用于从指定路径加载类别标签
def load_classes(path):Loads class labels at pathfp open(path, r)names fp.read().split(\n)[:-1]return names调用函数
class_names load_classes(data_config[names])
定义了一个名为 parse_model_config 的函数它用于解析 YOLOv3 模型的配置文件 读取文件划分如卷积、池化、上采样、路由、快捷连接和 YOLO 层
def parse_model_config(path):Parses the yolo-v3 layer configuration file and returns module definitionsfile open(path, r)lines file.read().split(\n)lines [x for x in lines if x and not x.startswith(#)] #x.startswith(#)用于检查字符串变量x是否以#前缀开始。如果x以该前缀开头该方法将返回一个布尔值通常是True否则返回False。lines [x.rstrip().lstrip() for x in lines] # get rid of fringe whitespacesmodule_defs []for line in lines:if line.startswith([): # This marks the start of a new blockmodule_defs.append({})module_defs[-1][type] line[1:-1].rstrip()if module_defs[-1][type] convolutional:module_defs[-1][batch_normalize] 0else:key, value line.split()value value.strip()module_defs[-1][key.rstrip()] value.strip()return module_defs
这个函数的目的是将 YOLOv3 模型配置文件中的文本描述转换成 PyTorch 可以理解的网络层模块。它首先处理超参数然后逐个处理每个模块定义根据模块的类型如卷积、池化、上采样、路由、快捷连接和 YOLO 层创建相应的 PyTorch 层并添加到 module_list 中。
def create_modules(module_defs):Constructs module list of layer blocks from module configuration in module_defs# 从模块定义列表中弹出第一个元素它包含了超参数hyperparameters例如输入图像的尺寸等。hyperparams module_defs.pop(0)# 初始化输出过滤器列表它将存储每一层的输出通道数即卷积核的数量。# 这里假设第一个超参数中的 channels 键对应的值是网络输入层的通道数。output_filters [int(hyperparams[channels])]# 创建一个 PyTorch 的 ModuleList 对象用于存储网络层模块。module_list nn.ModuleList()# 遍历模块定义列表module_i 是索引module_def 是当前模块的定义。for module_i, module_def in enumerate(module_defs):# 对于每个模块创建一个 PyTorch 的 Sequential 对象用于线性堆叠网络层。modules nn.Sequential()# 如果模块类型是 convolutionalif module_def[type] convolutional:# 获取当前模块是否使用批归一化batch normalization。bn int(module_def[batch_normalize])# 获取卷积核的数量即输出通道数。filters int(module_def[filters])# 获取卷积核的大小。kernel_size int(module_def[size])# 计算填充值以保持输出尺寸与输入尺寸相同。pad (kernel_size - 1) // 2# 添加一个卷积层到 Sequential 对象中。modules.add_module(fconv_{module_i},nn.Conv2d(in_channelsoutput_filters[-1], # 输入特征图的数量。out_channelsfilters, # 输出特征图的数量。kernel_sizekernel_size, # 卷积核的大小。strideint(module_def[stride]), # 卷积核滑动的步长。paddingpad, # 填充值。biasnot bn, # 是否添加偏置项。),)# 如果使用批归一化则添加一个批归一化层。if bn:modules.add_module(fbatch_norm_{module_i}, nn.BatchNorm2d(filters, momentum0.9))# 如果激活函数是 leaky则添加一个 LeakyReLU 激活层。if module_def[activation] leaky:modules.add_module(fleaky_{module_i}, nn.LeakyReLU(0.1))# 如果模块类型是 maxpoolelif module_def[type] maxpool:# 获取池化层的大小和步长。kernel_size int(module_def[size])stride int(module_def[stride])# 如果池化核大小为2且步长为1添加一个填充层以保持尺寸。if kernel_size 2 and stride 1:modules.add_module(f_debug_padding_{module_i}, nn.ZeroPad2d((0, 1, 0, 1)))# 添加一个最大池化层。maxpool nn.MaxPool2d(kernel_sizekernel_size, stridestride, paddingint((kernel_size - 1) // 2))modules.add_module(fmaxpool_{module_i}, maxpool)# 如果模块类型是 upsampleelif module_def[type] upsample:# 添加一个上采样层。upsample Upsample(scale_factorint(module_def[stride]), modenearest)modules.add_module(fupsample_{module_i}, upsample)# 如果模块类型是 routeelif module_def[type] route:# 获取路由层的层索引。layers [int(x) for x in module_def[layers].split(,)]# 计算路由层的输出通道数。filters sum([output_filters[1:][i] for i in layers])# 添加一个空层作为路由层。modules.add_module(froute_{module_i}, EmptyLayer())# 如果模块类型是 shortcutelif module_def[type] shortcut:# 获取快捷连接的输入通道数。filters output_filters[1:][int(module_def[from])]# 添加一个空层作为快捷连接层。modules.add_module(fshortcut_{module_i}, EmptyLayer())# 如果模块类型是 yoloelif module_def[type] yolo:# 获取 YOLO 层的锚点索引和锚点值。anchor_idxs [int(x) for x in module_def[mask].split(,)]anchors [int(x) for x in module_def[anchors].split(,)]anchors [(anchors[i], anchors[i 1]) for i in range(0, len(anchors), 2)]anchors [anchors[i] for i in anchor_idxs]# 获取 YOLO 层的类别数和图像尺寸。num_classes int(module_def[classes])img_size int(hyperparams[height])# 创建一个 YOLO 层并添加到模块中。yolo_layer YOLOLayer(anchors, num_classes, img_size)modules.add_module(fyolo_{module_i}, yolo_layer)# 将构建好的模块添加到模块列表中。module_list.append(modules)# 更新输出过滤器列表添加当前模块的输出通道数。output_filters.append(filters)# 返回超参数和构建好的模块列表。return hyperparams, module_list
定义了一个名为 Darknet 的类它是用于构建 YOLOv3 目标检测模型的 PyTorch 神经网络类。
class Darknet(nn.Module):YOLOv3 object detection model# 这个类继承自 PyTorch 的 nn.Module 类表示它是一个神经网络模型。# YOLOv3 是一个流行的目标检测算法这个类实现了 YOLOv3 的网络结构。def __init__(self, config_path, img_size416):super(Darknet, self).__init__()# 类的构造函数接受两个参数config_path模型配置文件的路径和 img_size输入图像的尺寸默认为416。# super() 函数用于调用父类的构造函数即初始化 PyTorch 的 nn.Module。self.module_defs parse_model_config(config_path)# 调用 parse_model_config 函数解析配置文件并存储解析后的模块定义。self.hyperparams, self.module_list create_modules(self.module_defs)# 调用 create_modules 函数根据模块定义创建网络层并存储超参数和模块列表。self.yolo_layers [layer[0] for layer in self.module_list if hasattr(layer[0], metrics)]# 从模块列表中提取出包含 metrics 属性的层这些层通常是 YOLO 层用于目标检测。# hasattr() 函数检查对象是否具有给定的属性。self.img_size img_size# 存储输入图像的尺寸。self.seen 0# 用于跟踪训练过程中看到的数据量例如图像数量。self.header_info np.array([0, 0, 0, self.seen, 0], dtypenp.int32)# 创建一个 NumPy 数组用于存储与模型相关的头部信息如版本、修订号、seen、未知字段和 epoch 数。
调用函数 model Darknet(opt.model_def).to(device)model.apply(weights_init_normal)#model.apply(fn)表示将fn函数应用到神经网络的各个模块上包括该神经网络本身。这通常在初始化神经网络的参数时使用本处用于初始化神经网络的权值