Skip to content

Commit

Permalink
version_upgrade_for_mac
Browse files Browse the repository at this point in the history
  • Loading branch information
KennardWang committed Oct 24, 2023
1 parent b9f19df commit bd768b3
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 32 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/flake8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7"]
python-version: ["3.8"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -37,7 +37,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.8

- name: Install dependencies
run: |
Expand All @@ -57,7 +57,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.8

- name: Install dependencies
run: |
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
with:
activate-environment: l2d-vtb
environment-file: environment.yml
python-version: "3.7"
python-version: "3.8"
- run: |
conda env list
conda activate l2d-vtb
Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:
with:
activate-environment: l2d-vtb
environment-file: environment.yml
python-version: "3.7"
python-version: "3.8"
- run: |
conda env list
conda activate l2d-vtb
Expand All @@ -61,7 +61,7 @@ jobs:
pip install -r requirements.txt
conda install -c conda-forge dlib
pip install scipy>0.16
conda install pytorch==1.10.2 torchvision==0.11.3 torchaudio==0.10.2 -c pytorch
conda install pytorch torchvision torchaudio -c pytorch-nightly
- name: Run tests
run: |
Expand All @@ -80,7 +80,7 @@ jobs:
with:
activate-environment: l2d-vtb
environment-file: environment.yml
python-version: "3.7"
python-version: "3.8"
- run: |
conda env list
conda activate l2d-vtb
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ channels:
- defaults
- conda-forge
dependencies:
- python=3.7
- python=3.8
- pip
- flake8
- pytest
3 changes: 3 additions & 0 deletions face_alignment/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def __init__(self, landmarks_type, network_size=NetworkSize.LARGE, use_onnx=Fals

if 'cuda' in device:
torch.backends.cudnn.benchmark = True

if 'mps' in device:
torch.backends.mps.benchmark = True

# Get the face detector
face_detector_module = __import__('face_alignment.detection.' + face_detector,
Expand Down
Binary file added face_alignment/ckpts/2DFAN-4.pth.tar
Binary file not shown.
Binary file added face_alignment/ckpts/s3fd_convert.pth
Binary file not shown.
6 changes: 3 additions & 3 deletions face_alignment/detection/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def __init__(self, device, verbose):
logger = logging.getLogger(__name__)
logger.warning("Detection running on CPU, this may be potentially slow.")

if 'cpu' not in device and 'cuda' not in device:
if 'cpu' not in device and 'cuda' not in device and 'mps' not in device:
if verbose:
logger.error("Expected values for device are: {cpu, cuda} but got: %s", device)
logger.error("Expected values for device are: {cpu, cuda, mps} but got: %s", device)
raise ValueError

def detect_from_image(self, tensor_or_path):
Expand Down Expand Up @@ -123,7 +123,7 @@ def tensor_or_path_to_ndarray(tensor_or_path, rgb=True):
if isinstance(tensor_or_path, str):
return cv2.imread(tensor_or_path) if not rgb else io.imread(tensor_or_path)
elif torch.is_tensor(tensor_or_path):
# Call cpu in case its coming from cuda
# Call cpu in case its coming from cuda or mps
return tensor_or_path.cpu().numpy()[..., ::-1].copy() if not rgb else tensor_or_path.cpu().numpy()
elif isinstance(tensor_or_path, np.ndarray):
return tensor_or_path[..., ::-1].copy() if not rgb else tensor_or_path
Expand Down
4 changes: 2 additions & 2 deletions face_alignment/detection/dlib/dlib_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, device, path_to_detector=None, verbose=False):
base_path = os.path.join(appdata_dir('face_alignment'), "data")

# Initialise the face detector
if 'cuda' in device:
if 'cuda' in device or 'mps' in device:
if path_to_detector is None:
path_to_detector = os.path.join(
base_path, "mmod_human_face_detector.dat")
Expand Down Expand Up @@ -47,7 +47,7 @@ def detect_from_image(self, tensor_or_path):

detected_faces = self.face_detector(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY))

if 'cuda' not in self.device:
if 'cuda' not in self.device and 'mps' not in self.device:
detected_faces = [[d.left(), d.top(), d.right(), d.bottom()] for d in detected_faces]
else:
detected_faces = [[d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom()] for d in detected_faces]
Expand Down
3 changes: 3 additions & 0 deletions face_alignment/detection/sfd/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def detect(net, img, device):

if 'cuda' in device:
torch.backends.cudnn.benchmark = True

if 'mps' in device:
torch.backends.mps.benchmark = True

img = torch.FloatTensor(img).to(device)
BB, CC, HH, WW = img.size()
Expand Down
2 changes: 1 addition & 1 deletion face_pose/pose_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def draw_annotation_box(self, image, rotation_vector, translation_vector, color=
point_3d.append((front_size, front_size, front_depth))
point_3d.append((front_size, -front_size, front_depth))
point_3d.append((-front_size, -front_size, front_depth))
point_3d = np.array(point_3d, dtype=np.float).reshape(-1, 3)
point_3d = np.array(point_3d, dtype=np.float32).reshape(-1, 3)

# Map to 2d image points
(point_2d, _) = cv2.projectPoints(point_3d,
Expand Down
24 changes: 18 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import cv2
import numpy as np
import time
import dlib
import face_alignment
import utils

from argparse import ArgumentParser
Expand All @@ -16,20 +14,34 @@


def run():
# Define operating system
cap = utils.def_cap(args.cam)
# Get operating system
os = utils.get_os()

if os == 'Windows': # Windows OS
cap = cv2.VideoCapture(args.cam + cv2.CAP_DSHOW)
else: # Linux & Mac OS
cap = cv2.VideoCapture(args.cam)

cap.set(cv2.CAP_PROP_FPS, 30)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
_, sample_frame = cap.read()

# Setup face detection models
if not args.gpu: # CPU: use dlib
import dlib
dlib_model_path = 'face_pose/shape_predictor_68_face_landmarks.dat'
shape_predictor = dlib.shape_predictor(dlib_model_path)
face_detector = dlib.get_frontal_face_detector()

else: # GPU: use FAN (better)
fa = face_alignment.FaceAlignment(
face_alignment.LandmarksType._2D, flip_input=False)
import face_alignment
if os == 'Linux': # Linux
fa = face_alignment.FaceAlignment(
face_alignment.LandmarksType._2D, device='cuda', flip_input=False)
if os == 'Darwin': # MacOS
fa = face_alignment.FaceAlignment(
face_alignment.LandmarksType._2D, device='mps', flip_input=False)

face_detector = fa.face_detector

# Introduce pose estimator to solve pose, get one frame to setup the estimator according to the image size
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
numpy==1.21.6
numba==0.56.3
opencv_python==4.4.0.44
numpy==1.24.4
numba==0.58.1
opencv_python==4.8.1.78
4 changes: 2 additions & 2 deletions tests/test_mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def test_module_install_gpu():
v1 = pkg_resources.get_distribution(modules[1]).version
assert v1 is not None

# Test CUDA
if m1.cuda.is_available():
# Test M1 GPU
if m1.backends.mps.is_available() and m1.backends.mps.is_built():
assert True
else:
print(m1.__version__)
Expand Down
10 changes: 3 additions & 7 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import cv2


def def_cap(cam):
"""Define capture"""
if system() in ['Windows']: # Windows OS
cap = cv2.VideoCapture(cam + cv2.CAP_DSHOW)
else: # Linux & Mac OS
cap = cv2.VideoCapture(cam)
return cap
def get_os():
"""Get operating system"""
return system()


def get_face(detector, image, gpu=False):
Expand Down

0 comments on commit bd768b3

Please sign in to comment.