Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用更高效、更准确的insightface模型教程 #16

Open
dev-fox-101 opened this issue Aug 24, 2024 · 14 comments
Open

使用更高效、更准确的insightface模型教程 #16

dev-fox-101 opened this issue Aug 24, 2024 · 14 comments
Labels
documentation Improvements or additions to documentation

Comments

@dev-fox-101
Copy link

dev-fox-101 commented Aug 24, 2024

kqstone 大佬已经集成了insightface的模型
https://github.com/kqstone/mt-photos-insightface-unofficial

insightface是开源中准确率最高的预训练模型,特别是针对亚洲人脸型,比官方仓库使用的Facenet512准确率高很多
可以大幅改善识别完成后,一个人物被识别为多个人物的情况;

使用方法:

Docker容器运行

  • 下载镜像
docker pull kqstone/mt-photos-insightface-unofficial:latest
  • 创建及运行容器
docker run -i -p 8066:8066 -e API_AUTH_KEY=mt_photos_ai_extra --name mt-photos-insightface-unofficial --restart="unless-stopped" kqstone/mt-photos-insightface-unofficial:latest

docker-compose模板

version: "3"

services:
  mtphotos:
    image: registry.cn-hangzhou.aliyuncs.com/mtphotos/mt-photos:latest
    container_name: mtphotos
    restart: always
    ports:
      - 8063:8063
    volumes:
      - /volume1/docker/mt_photos/config:/config
      - /volume1/photos/mt_photos_upload:/upload
      - /volume1/xxx/其他需要映射的目录:/photos
    environment:
      - TZ=Asia/Shanghai
      - LANG=C.UTF-8
    dns:
      - 114.114.114.114
    depends_on:
      - mtphotos_ai
      - mtphotos_face_api
  mtphotos_ai:
    image: registry.cn-hangzhou.aliyuncs.com/mtphotos/mt-photos-ai:onnx-latest
    container_name: mtphotos_ai
    restart: always
    ports:
      - 8060:8000
    environment:
      - API_AUTH_KEY=mt_photos_ai_extra
  mtphotos_face_api:
    image: registry.cn-hangzhou.aliyuncs.com/vito0101/mt-photos-insightface-unofficial:latest
    container_name: mtphotos_face_api
    restart: always
    ports:
      - 8066:8066
    environment:
      - API_AUTH_KEY=mt_photos_ai_extra

API配置参数

人脸置信度阈值设置 建议填 0.65
人脸匹配差异度阈值 建议填 0.5

image

无法下载镜像

以下2个镜像已内置 buffalo_l 模型

可以使用这个国内的地址拉取镜像

docker pull registry.cn-hangzhou.aliyuncs.com/vito0101/mt-photos-insightface-unofficial:latest

docker tag registry.cn-hangzhou.aliyuncs.com/vito0101/mt-photos-insightface-unofficial:latest  kqstone/mt-photos-insightface-unofficial:latest

第1条命令为拉取镜像
第2条命令为将镜像标记为Docker hub源,这样可以在图形化创建容器时使用

armV8版本镜像

docker pull registry.cn-hangzhou.aliyuncs.com/vito0101/mt-photos-insightface-unofficial:arm-latest

docker tag registry.cn-hangzhou.aliyuncs.com/vito0101/mt-photos-insightface-unofficial:arm-latest  kqstone/mt-photos-insightface-unofficial:arm-latest

# 启动容器
docker run -i -p 8066:8066 -e API_AUTH_KEY=mt_photos_ai_extra --name mt-photos-insightface-unofficial --restart="unless-stopped" kqstone/mt-photos-insightface-unofficial:arm-latest

启动容器后,测试api提示不是人脸识别API

第一次启动容器后需要下载模型,如果由于网络问题,下载失败,可以按下面的方法手动下载镜像

1、创建容器时,要映射 /root/.insightface/models 这个目录

不能直接映射/root   不能直接映射/root  不能直接映射/root

2、将模型文件 buffalo_l.zip 解压到映射的目录中;
模型文件下载地址:https://github.com/kqstone/mt-photos-insightface-unofficial/releases/tag/models
或者百度网盘:https://pan.baidu.com/s/1SsY7_2t7aORh2jCvGWtD1A?pwd=1234

比如目录映射为: -v /mnt/docker/insightface/models:/root/.insightface/models
需要把buffalo_l.zip的文件解压到/mnt/docker/insightface/models/buffalo_l

解压完成后,在 /mnt/docker/insightface/models/buffalo_l 下面可以看到 1k3d68.onnx、2d106det.onnx、det_10g.onnx、genderage.onnx、w600k_r50.onnx 5个文件

目录结构

创建容器的完整命令:

docker run -i -p 8066:8066 -e API_AUTH_KEY=mt_photos_ai_extra -v /mnt/docker/insightface/models:/root/.insightface/models --name mt-photos-insightface-unofficial --restart="unless-stopped" kqstone/mt-photos-insightface-unofficial:latest

创建容器

Windows如何使用

参考官方Windows版本的pyinstaller打包,我将kqstone/mt-photos-insightface-unofficial仓库的代码打包为Windows可以直接运行的exe文件

下载附件中的 mt-photos-insightface-unofficial-exe.zip 压缩包,解压后查看内部的 使用说明.txt

API配置参数

人脸置信度阈值设置 建议填 0.65
人脸匹配差异度阈值 建议填 0.5

image

Windows版本及模型下载

百度网盘:

https://pan.baidu.com/s/1SsY7_2t7aORh2jCvGWtD1A?pwd=1234

@dev-fox-101
Copy link
Author

mt-photos-insightface-unofficial-exe.zip 也可以从这里下载

https://github.com/dev-fox-101/mt-photos-insightface-unofficial/releases/tag/1.0.0

@tza17313 tza17313 added the documentation Improvements or additions to documentation label Aug 24, 2024
@tza17313 tza17313 pinned this issue Aug 24, 2024
@xayane
Copy link

xayane commented Aug 24, 2024

请问下这个有cuda版本吗?

@xayane
Copy link

xayane commented Aug 24, 2024

或者cuda的应该怎么打包镜像

@tza17313
Copy link
Contributor

或者cuda的应该怎么打包镜像

dockerfile里,把最上面的 基础镜像改下;

FROM python:3.8.10-buster
改为
FROM nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04

应该就可以打包了

@xayane
Copy link

xayane commented Aug 26, 2024

或者cuda的应该怎么打包镜像

dockerfile里,把最上面的 基础镜像改下;

FROM python:3.8.10-buster 改为 FROM nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04

应该就可以打包了

试了可行,同时要在添加python的包

@xayane
Copy link

xayane commented Aug 26, 2024

或者cuda的应该怎么打包镜像

dockerfile里,把最上面的 基础镜像改下;

FROM python:3.8.10-buster 改为 FROM nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04

应该就可以打包了

感谢!

@iYongHsu
Copy link

iYongHsu commented Sep 1, 2024

确实比较精准了。

@sumsethan
Copy link

试了下 cuda 编译,Dockerfile如果只是替换 FROM 还是会只用 CPU 跑,还需要安装python ;然后 requirement 里onnxruntime替换成onnxruntime-gpu,再加一个python-multipart。现在实测是可以在 GPU 上跑起来了

@yhhqjw
Copy link

yhhqjw commented Sep 2, 2024

试了下 cuda 编译,Dockerfile如果只是替换 FROM 还是会只用 CPU 跑,还需要安装python ;然后 requirement 里onnxruntime替换成onnxruntime-gpu,再加一个python-multipart。现在实测是可以在 GPU 上跑起来了

可以分享下dockerfile吗,我按这个写,一直报错。

@sumsethan
Copy link

试了下 cuda 编译,Dockerfile如果只是替换 FROM 还是会只用 CPU 跑,还需要安装python ;然后 requirement 里onnxruntime替换成onnxruntime-gpu,再加一个python-multipart。现在实测是可以在 GPU 上跑起来了

可以分享下dockerfile吗,我按这个写,一直报错。

root:~/mt-photos-insightface-unofficial# cat Dockerfile

#FROM python:3.8.10-buster
FROM nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04
USER root

RUN apt update && \
    apt install -y python3 pip && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/log/*

WORKDIR /app
COPY requirements.txt .

# 安装依赖包
RUN pip3 install --no-cache-dir -r requirements.txt --index-url=https://pypi.tuna.tsinghua.edu.cn/simple/

COPY server.py .

ENV API_AUTH_KEY=mt_photos_ai_extra
ENV RECOGNITION_MODEL=buffalo_l
ENV DETECTION_THRESH=0.65
EXPOSE 8066

VOLUME ["/root/.insightface/models"]

CMD [ "python3", "server.py" ]

root:~/mt-photos-insightface-unofficial# cat requirements.txt

fastapi
python-dotenv
uvicorn
insightface
onnxruntime-gpu
python-multipart

不知道能不能通用,我这里是可以跑的

@yhhqjw
Copy link

yhhqjw commented Sep 3, 2024

试了下 cuda 编译,Dockerfile如果只是替换 FROM 还是会只用 CPU 跑,还需要安装python ;然后 requirement 里onnxruntime替换成onnxruntime-gpu,再加一个python-multipart。现在实测是可以在 GPU 上跑起来了

可以分享下dockerfile吗,我按这个写,一直报错。

root:~/mt-photos-insightface-unofficial# cat Dockerfile

#FROM python:3.8.10-buster
FROM nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04
USER root

RUN apt update && \
    apt install -y python3 pip && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/log/*

WORKDIR /app
COPY requirements.txt .

# 安装依赖包
RUN pip3 install --no-cache-dir -r requirements.txt --index-url=https://pypi.tuna.tsinghua.edu.cn/simple/

COPY server.py .

ENV API_AUTH_KEY=mt_photos_ai_extra
ENV RECOGNITION_MODEL=buffalo_l
ENV DETECTION_THRESH=0.65
EXPOSE 8066

VOLUME ["/root/.insightface/models"]

CMD [ "python3", "server.py" ]

root:~/mt-photos-insightface-unofficial# cat requirements.txt

fastapi
python-dotenv
uvicorn
insightface
onnxruntime-gpu
python-multipart

不知道能不能通用,我这里是可以跑的

2024-09-03 17:58:31 Traceback (most recent call last):
2024-09-03 17:58:31 File "/app/server.py", line 44, in
2024-09-03 17:58:31 faceAnalysis = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'], allowed_modules=['detection', 'recognition'], name=recognition_model)
2024-09-03 17:58:31 File "/usr/local/lib/python3.10/dist-packages/insightface/app/face_analysis.py", line 43, in init
2024-09-03 17:58:31 assert 'detection' in self.models
2024-09-03 17:58:31 AssertionError

我这个docker运行会出现这个报错。。

@blakejia
Copy link

blakejia commented Sep 4, 2024

mac arm的话,可以用么?

@zhuosheng7458
Copy link

纯小白问一下window版
MT原版的人脸识别,那个程序,需要完全卸载掉,然后再安装这个新模型版本,是吧?
新版本安装完之后,MT系统里面,人脸识别数据该怎么办呢?重建全部数据?

@ptbsare
Copy link

ptbsare commented Oct 15, 2024

你好,非常不错的项目。我是使用cuda版本,请问这个docker的server.py能不能优化一下让它仅在人脸识别运行的时候才调用GPU资源,否则释放显存。多数人都是在nas上部署此类项目追求低功耗,目前的docker只要开着就一定调用GPU,一是浪费计算资源和显存。二是显卡保持唤醒不能休眠,增加了功耗。实际人脸识别运行时间占比全天时间很少。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

9 participants