Skip to content

Commit

Permalink
Add pytorch version
Browse files Browse the repository at this point in the history
  • Loading branch information
arodik committed Feb 28, 2024
1 parent ddf32da commit 9f2de25
Show file tree
Hide file tree
Showing 92 changed files with 12,236 additions and 51 deletions.
13 changes: 12 additions & 1 deletion embedding-calculator/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ requests==2.31.0
pylama==8.4.1

# dependencies for both scanner backends
Pillow==8.3.2
Pillow==9.0.0
imagecodecs==2020.5.30
numpy==1.19.5
scipy==1.4.1
Expand All @@ -95,5 +95,16 @@ scikit-image==0.17.2
scikit-learn==0.23.2
joblib==0.17.0

# dependencies for pytorch version
torch==1.13.1
pytorch-lightning==1.8.6
tqdm==4.66.1
bcolz-zipline==1.2.6
prettytable==3.10.0
menpo==0.11.0
mxnet==1.9.0
torchvision==0.14.1
pandas==1.4.4

# web server
uWSGI==2.0.19
114 changes: 66 additions & 48 deletions embedding-calculator/src/_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from src.services.imgtools.read_img import read_img
from src.services.utils.pyutils import Constants
from src.services.imgtools.test.files import IMG_DIR
from src.services.facescan.plugins.adaface.inference import *
import base64
from src.constants import SKIPPED_PLUGINS

Expand All @@ -44,18 +45,18 @@ def face_detection_skip_check(face_plugins):


def endpoints(app):
@app.before_first_request
def init_model() -> None:
detector = managers.plugin_manager.detector
face_plugins = managers.plugin_manager.face_plugins
face_plugins = face_detection_skip_check(face_plugins)
detector(
img=read_img(str(IMG_DIR / 'einstein.jpeg')),
det_prob_threshold=_get_det_prob_threshold(),
face_plugins=face_plugins
)
print("Starting to load ML models")
return None
#@app.before_first_request
#def init_model() -> None:
# detector = managers.plugin_manager.detector
# face_plugins = managers.plugin_manager.face_plugins
# face_plugins = face_detection_skip_check(face_plugins)
# detector(
# img=read_img(str(IMG_DIR / 'einstein.jpeg')),
# det_prob_threshold=_get_det_prob_threshold(),
# face_plugins=face_plugins
# )
# print("Starting to load ML models")
# return None

@app.route('/healthcheck')
def healthcheck():
Expand All @@ -77,50 +78,67 @@ def status_get():

@app.route('/find_faces_base64', methods=['POST'])
def find_faces_base64_post():
detector = managers.plugin_manager.detector
face_plugins = managers.plugin_manager.filter_face_plugins(
_get_face_plugin_names()
)
face_plugins = face_detection_skip_check(face_plugins)
rawfile = base64.b64decode(request.get_json()["file"])

faces = detector(
img=read_img(rawfile),
det_prob_threshold=_get_det_prob_threshold(),
face_plugins=face_plugins
)
plugins_versions = {p.slug: str(p) for p in [detector] + face_plugins}
faces = _limit(faces, request.values.get(ARG.LIMIT))
FaceDetection.SKIPPING_FACE_DETECTION = False
return jsonify(plugins_versions=plugins_versions, result=faces)
if ENV.SWITCHER:
rawfile = base64.b64decode(request.get_json()["file"])
return jsonify(inference_detector(image_path = rawfile))
else:
detector = managers.plugin_manager.detector
face_plugins = managers.plugin_manager.filter_face_plugins(
_get_face_plugin_names()
)
face_plugins = face_detection_skip_check(face_plugins)
rawfile = base64.b64decode(request.get_json()["file"])

faces = detector(
img=read_img(rawfile),
det_prob_threshold=_get_det_prob_threshold(),
face_plugins=face_plugins
)
plugins_versions = {p.slug: str(p) for p in [detector] + face_plugins}
faces = _limit(faces, request.values.get(ARG.LIMIT))
FaceDetection.SKIPPING_FACE_DETECTION = False
return jsonify(plugins_versions=plugins_versions, result=faces)

@app.route('/find_faces', methods=['POST'])
@needs_attached_file
def find_faces_post():
detector = managers.plugin_manager.detector
face_plugins = managers.plugin_manager.filter_face_plugins(
_get_face_plugin_names()
)
face_plugins = face_detection_skip_check(face_plugins)
faces = detector(
img=read_img(request.files['file']),
det_prob_threshold=_get_det_prob_threshold(),
face_plugins=face_plugins
)
plugins_versions = {p.slug: str(p) for p in [detector] + face_plugins}
faces = _limit(faces, request.values.get(ARG.LIMIT))
FaceDetection.SKIPPING_FACE_DETECTION = False
return jsonify(plugins_versions=plugins_versions, result=faces)
if ENV.SWITCHER:
img = request.files['file']
return jsonify(inference_detector(image_path = img))
else:
detector = managers.plugin_manager.detector
face_plugins = managers.plugin_manager.filter_face_plugins(
_get_face_plugin_names()
)
face_plugins = face_detection_skip_check(face_plugins)
faces = detector(
img=read_img(request.files['file']),
det_prob_threshold=_get_det_prob_threshold(),
face_plugins=face_plugins
)
plugins_versions = {p.slug: str(p) for p in [detector] + face_plugins}
faces = _limit(faces, request.values.get(ARG.LIMIT))
FaceDetection.SKIPPING_FACE_DETECTION = False
return jsonify(plugins_versions=plugins_versions, result=faces)

# ----------------------------------------------------------------
# ----------------------------------------------------------------
# ----------------------------------------------------------------


@app.route('/scan_faces', methods=['POST'])
@needs_attached_file
def scan_faces_post():
faces = scanner.scan(
img=read_img(request.files['file']),
det_prob_threshold=_get_det_prob_threshold()
)
faces = _limit(faces, request.values.get(ARG.LIMIT))
return jsonify(calculator_version=scanner.ID, result=faces)
if ENV.SWITCHER:
img = request.files['file']
return jsonify(inference_scaner(image_path=img))
else:
faces = scanner.scan(
img=read_img(request.files['file']),
det_prob_threshold=_get_det_prob_threshold()
)
faces = _limit(faces, request.values.get(ARG.LIMIT))
return jsonify(calculator_version=scanner.ID, result=faces)


def _get_det_prob_threshold():
Expand Down
16 changes: 14 additions & 2 deletions embedding-calculator/src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class ENV(Constants):
ML_PORT = int(get_env('ML_PORT', '3000'))
IMG_LENGTH_LIMIT = int(get_env('IMG_LENGTH_LIMIT', '640'))

FACE_DETECTION_PLUGIN = get_env('FACE_DETECTION_PLUGIN', 'facenet.FaceDetector')
CALCULATION_PLUGIN = get_env('CALCULATION_PLUGIN', 'facenet.Calculator')
#FACE_DETECTION_PLUGIN = get_env('FACE_DETECTION_PLUGIN', 'facenet.FaceDetector')
FACE_DETECTION_PLUGIN = get_env('FACE_DETECTION_PLUGIN', 'insightface.FaceDetector@retinaface_mnet025_v1')
#FACE_DETECTION_PLUGIN = get_env('FACE_DETECTION_PLUGIN', 'adaface.FaceDetector@mtcnn')
CALCULATION_PLUGIN = get_env('CALCULATION_PLUGIN', 'insightface.Calculator@arcface_mobilefacenet')
EXTRA_PLUGINS = get_env_split('EXTRA_PLUGINS', 'facenet.LandmarksDetector,agegender.AgeDetector,agegender.GenderDetector,facenet.facemask.MaskDetector,facenet.PoseEstimator')

LOGGING_LEVEL_NAME = get_env('LOGGING_LEVEL_NAME', 'debug').upper()
Expand All @@ -36,7 +38,17 @@ class ENV(Constants):

RUN_MODE = get_env_bool('RUN_MODE', False)

# PyTorch settings
# SWITCHER = False - using all standard CompreFace settings
# SWITCHER = True - using PyTorch settings
SWITCHER = get_env_bool('SWITCHER', True)
DEVICE = get_env('DEVICE', 'cpu')
RECOGNITION_MODEL = get_env('RECOGNITION_MODEL', 'ir_50')
RECOGNITION_MODEL_PATH = get_env('RECOGNITION_MODEL_PATH', 'services/facescan/plugins/adaface/pretrained/adaface_ir50_ms1mv2.ckpt')
DETECTOR_NAME = get_env('DETECTOR_NAME', 'mtcnn')


LOGGING_LEVEL = logging._nameToLevel[ENV.LOGGING_LEVEL_NAME]
ENV_MAIN = ENV
SKIPPED_PLUGINS = ["insightface.PoseEstimator", "facemask.MaskDetector", "facenet.PoseEstimator"]

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.pyc
.vscode
*.ckpt
.idea
experiments
playground
validation_ijbs
*.pkl
*.csv
result
scripts/run_debug*
21 changes: 21 additions & 0 deletions embedding-calculator/src/services/facescan/plugins/adaface/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Minchul Kim

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions embedding-calculator/src/services/facescan/plugins/adaface/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pytorch-lightning = "==1.8.6"
torch = "<=1.13.1"
tqdm = "*"
bcolz-zipline = "*"
prettytable = "*"
menpo = "*"
mxnet = "*"
opencv-python = "*"
torchvision = "*"
pandas = "*"
scikit-learn = "*"

[dev-packages]

[requires]
python_version = "3.9"
python_full_version = "3.9.16"
Loading

0 comments on commit 9f2de25

Please sign in to comment.