1 安装系统环境(ubuntu)
1.1 安装显卡驱动
- 打开software & updates ->选择Additional Drivers 安装一个驱动
- 使用 nvidia-smi 查看显卡是否正常
1.2 安装CUDA
1.2.1 下载CUDA
-
i https://developer.nvidia.com/cuda-toolkit-archive
-
选择对应版本的CUDA
-
选择 -> Linux x86_64 -> Ubuntu-> version -> Runfile (local)
下面这个是根据版本选择得到的
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sudo sh cuda_11.8.0_520.61.05_linux.run
等待一段时间,会弹出一个窗口,选择continue 回车 输入 accept->进入CUDA Installer界面 选择Driver 点击空格取消选择Driver
–> Install
安装完成后,需要进行环境变量配置
输入 vim ~/.bashrc
添加以下内容: -
export PATH=/usr/local/cuda-11.8/binKaTeX parse error: Expected '}', got 'EOF' at end of input: {PATH:+:{PATH}}
-
export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
-
输入nvcc -V 查看是否安装成功
1.3 安装cudnn
1.3.1 下载cudnn
点击下面的链接,进行对应版本的选择
- https://developer.nvidia.com/rdp/cudnn-archive
- sudo apt-get install
1.4 安装 Nvidia-docker
进入官网link
选择 install with apt
- Configure the production repository:
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list |
sed ‘s#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g’ |
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list - Update the packages list from the repository:
sudo apt-get update - Install the NVIDIA Container Toolkit packages:
sudo apt-get install -y nvidia-container-toolkit
2 配置训练环境
2.1 拉取docker镜像
- modelscope github 仓库地址link
GPU镜像
py37
- registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.3.0-py37-torch1.11.0-tf1.15.5-1.6.1
py38
- registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.8.0-py38-torch2.0.1-tf2.13.0-1.9.5
我拉取的是 py38 版本
- docker pull registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.8.0-py38-torch2.0.1-tf2.13.0-1.9.5
查看镜像 - docker images
启动镜像 -v /home/ubuntu/:/home/ubuntu/ 本地文件映射到容器内 - docker run -it --gpus all -v /home/ubuntu/:/home/ubuntu/ registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.8.0-py38-torch2.0.1-tf2.13.0-1.9.5 -n /bin/bash
2.2 拉取训练模型
- git clone https://github.com/biubug6/Pytorch_Retinaface.git
3 数据集制作
3.1 数据集标注
- 数据集标注使用LabelMe进行数据标注
文件夹样式 - Dataset
- annotations
- images
- flags.txt
- 目标数据格式
./data/widerface/
train/
images/
label.txt
val/
images/
wider_val.txt
- 标注内容
- 人脸边界框:上额头到下额头,左右脸边缘
- 左眼、右眼、鼻子、左嘴角、右嘴角
- 标签文件转换
import os
import json
import random # 导入random模块
def convert_json_to_format(json_data, folder_name):
# 提取图片路径
image_path = json_data['imagePath']
# 获取图像尺寸
image_width = json_data['imageWidth']
image_height = json_data['imageHeight']
# 初始化输出字符串
output = ""
# 初始化各部位坐标值为 -1
face_box_x, face_box_y, face_box_w, face_box_h = -1, -1, -1, -1
left_eye_x, left_eye_y = -1, -1
right_eye_x, right_eye_y = -1, -1
nose_x, nose_y = -1, -1
left_mouse_x, left_mouse_y = -1, -1
right_mouse_x, right_mouse_y = -1, -1
# 处理形状数据
shapes = json_data['shapes']
for shape in shapes:
label = shape['label']
points = shape['points']
if label == 'face' and len(points) == 2:
# 转换矩形框坐标
x1, y1 = points[0]
x2, y2 = points[1]
face_box_x = min(x1, x2)
face_box_y = min(y1, y2)
face_box_w = abs(x2 - x1)
face_box_h = abs(y2 - y1)
elif label == 'left_eye':
# 添加左眼位置信息
left_eye_x, left_eye_y = points[0]
elif label == 'right_eye':
# 添加右眼位置信息
right_eye_x, right_eye_y = points[0]
elif label == 'nose':
# 添加鼻子位置信息
nose_x, nose_y = points[0]
elif label == 'left_mouse':
# 添加左嘴角位置信息
left_mouse_x, left_mouse_y = points[0]
elif label == 'right_mouse':
# 添加右嘴角位置信息
right_mouse_x, right_mouse_y = points[0]
# 构建输出字符串
output = f"{int(face_box_x)} {int(face_box_y)} {int(face_box_w)} {int(face_box_h)} "
output += f"{float(left_eye_x):.3f} {float(left_eye_y):.3f} 0.0 "
output += f"{float(right_eye_x):.3f} {float(right_eye_y):.3f} 0.0 "
output += f"{float(nose_x):.3f} {float(nose_y):.3f} 0.0 "
output += f"{float(left_mouse_x):.3f} {float(left_mouse_y):.3f} 0.0 "
output += f"{float(right_mouse_x):.3f} {float(right_mouse_y):.3f} 0.0 "
# 添加随机置信度值,范围在0.8到0.99之间
confidence = random.uniform(0.8, 0.99)
output += f"{confidence:.2f}"
# 返回结果
return f"# {folder_name}/{os.path.basename(image_path)}\n{output}\n"
def process_folder(folder_path, output_file):
# 打开输出文件
with open(output_file, 'w', encoding='utf-8') as out_file:
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
if filename.endswith('.json'):
file_path = os.path.join(folder_path, filename)
with open(file_path, 'r', encoding='utf-8') as file:
json_data = json.load(file)
# 获取文件夹名
folder_name = os.path.basename(os.path.dirname(file_path))
result = convert_json_to_format(json_data, folder_name)
out_file.write(result)
# 指定文件夹路径和输出文件路径
folder_path = r''
output_file = r''
# 调用函数
process_folder(folder_path, output_file)
验证集标签文件制作
import os
def save_folder_and_image_names(folder_path, output_file):
"""
遍历指定文件夹中的所有图片,将文件夹名和图片名组合后保存到输出文件中。
:param folder_path: 包含图片的文件夹路径
:param output_file: 输出文件的路径
"""
# 检查文件夹是否存在
if not os.path.isdir(folder_path):
print(f"错误:{folder_path} 不是一个有效的文件夹路径")
return
# 图片文件可能的扩展名列表
image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
with open(output_file, 'w', encoding='utf-8') as file:
for root, dirs, files in os.walk(folder_path):
for name in files:
# 获取文件扩展名
ext = os.path.splitext(name)[1].lower()
if ext in image_extensions:
# 组合文件夹名和图片名
folder_name = os.path.basename(root)
full_name = f"{folder_name}/{name}"
file.write(full_name + '\n')
if __name__ == "__main__":
# 修改这里的文件夹路径和输出文件路径
folder_path = r''
output_file = r''
save_folder_and_image_names(folder_path, output_file)
print(f"图片信息已保存至 {output_file}")
4 windows 环境搭建
4.1 conda安装
进入网址
- https://anaconda.org/anaconda/conda
下载conda 默认步骤安装即可 - conda create -n 环境名 python==版本号 -y
- conda activate 环境名
- 配置国内镜像源地址
- pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple some-package
- 安装 pytorch 2.0.1
- pytorch下载网址
- pillow==8.2.0
- opencv-python==4.5.2.54
- numpy==1.22.0
- matplotlib==3.5.1
- tensorboard==2.5.0
-大概就可以使用了,如果出现版本不兼容,灵活调整
注 - 如果要使用GPU训练,请先安装环境
- 显卡驱动、CUDA、CUDNN