{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "939ad4fa-f9d7-4d1f-9757-648ba15d7551",
   "metadata": {
    "tags": []
   },
   "source": [
    "# CenterFace\n",
    "\n",
    "## 1. 模型简介\n",
    "CenterFace是一种基于单阶段人脸检测算法，作者借鉴了CenterNet的思想，将人脸检测转换为标准点问题，根据人脸中心点来回归人脸框的大小和五个标志点。\n",
    "\n",
    "\n",
    "**模型结构**\n",
    "\n",
    "CenterFace采用了轻量级网络mobileNetV2作为主干网络，结合特征金字塔网络（FPN）实现anchor free的人脸检测。\n",
    "\n",
    "<div align=center>\n",
    "    <img src=\"./doc/Architecture of the CenterFace.png\"/>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa7b7b67-9d3b-4926-b039-ba9840eefa4d",
   "metadata": {},
   "source": [
    "¶\n",
    "## 2. 环境检查及依赖补全\n",
    "\n",
    "### 2.1 环境检查\n",
    "\n",
    "推荐环境：pytorch=1.13.1 py38\n",
    "推荐环境：dcu=23.04\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检查torch版本\n",
    "import torch\n",
    "version = torch.__version__\n",
    "num = float(version[:3])\n",
    "assert num >= 1.10\n",
    "device = \"cpu\"\n",
    "# 检查硬件环境\n",
    "if \"dtk\" in version:\n",
    "    device = \"dtk\"\n",
    "    !rocm-smi\n",
    "elif \"cu\" in version:\n",
    "    device = \"cuda\"\n",
    "    !nvidia-smi\n",
    "print(\"pytorch version:\",version)\n",
    "print(\"device =\", device)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa7b7b67-9d3b-4926-b039-ba9840eefacc",
   "metadata": {},
   "source": [
    "### 2.2 依赖安装\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64c534dd-ad8f-493d-a5e4-435676d4f162",
   "metadata": {},
   "source": [
    "## 3. 素材准备\n",
    "### 3.1 数据集准备\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "项目已经预制了轻量数据进行训练验证,请确保当前项目中包含datasets目录且结构如下:\n",
    "\n",
    "```\n",
    "|----datasets\\\n",
    "│   |----images\n",
    "│       |----train\n",
    "│           |----0--Parade\n",
    "│           |----1--Handshaking\n",
    "│       |----val\n",
    "│           |----0--Parade\n",
    "│           |----1--Handshaking\n",
    "│   |----annotations\n",
    "│       |----train\n",
    "│           |----train.txt\n",
    "│       |----val\n",
    "│           |----val.txt\n",
    "│   |----labels\n",
    "│       |----train_face.json\n",
    "│       |----val_face.json\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 执行以下代码，将预置标注数据转换成coco形式\n",
    "!python3 datasets/gen_data.py"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24b83a20-7b60-44f6-a800-602bf428e08a",
   "metadata": {},
   "source": [
    "主要目录结构如下\n",
    "```\n",
    "centerface_pytorch\\\n",
    "|----datasets\\\n",
    "│   |----images\n",
    "│       |----train\n",
    "│           |----0--Parade\n",
    "│           |----1--Handshaking\n",
    "│       |----val\n",
    "│           |----0--Parade\n",
    "│           |----1--Handshaking\n",
    "│   |----annotations\n",
    "│       |----train\n",
    "│           |----train.txt\n",
    "│       |----val\n",
    "│           |----val.txt\n",
    "│   |----labels\n",
    "│       |----train_face.json\n",
    "│       |----val_face.json\n",
    "│   |----gen_data.py\n",
    "|----docker\\\n",
    "│   |----Dockerfile\n",
    "|----models\\\n",
    "|    |----model_best.pth\n",
    "|----test_img\\\n",
    "|    |----0_Parade_marchingband_1_5.jpg\n",
    "|    |----0_Parade_marchingband_1_35.jpg\n",
    "|    |----data_utils.py\n",
    "|----evaluate\\\n",
    "|    |----make.sh\n",
    "|----README.md\n",
    "|----model.properties\n",
    "|----requirements.txt\n",
    "|----centerface-pytorch.ipynb\n",
    "|----...\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef0faf15-d9ab-454f-9368-e026372752ad",
   "metadata": {},
   "source": [
    "## 4 训练\n",
    "### 4.1 开始训练\n",
    "\n",
    "根据需求选择单卡或多卡训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单卡训练\n",
    "!python3 src/main.py --input_h 640 --input_w 640 --batch_size 8 --lr 5e-4 --data_dir datasets/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d95c0e6-4143-40de-b90c-3978c02cb169",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 多卡训练（需要2个加速卡）\n",
    "!python3 src/main.py --input_h 640 --input_w 640 --batch_size 8 --lr 5e-4 --data_dir datasets/ --gpus 0,1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f1986b2-1ea1-45be-8f56-dfaacfba7694",
   "metadata": {},
   "source": [
    "## 5. 推理\n",
    "## 5.1 开始推理\n",
    "\n",
    "提供了一个推理脚本来测试模型，执行下面的脚本来测试模型输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de9d22f1-4764-4c16-a4c9-52e9581669ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "!bash src/lib/external/make.sh\n",
    "!python3 src/test_wider_face.py\n",
    "# 执行完成后会在当前目录下生成draw_img.jpg的图像"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfe3acde",
   "metadata": {},
   "source": [
    "## 6. 相关文献和引用\n",
    "https://github.com/chenjun2hao/CenterFace.pytorch\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
