You can control the hardware device, data type, and compilation behavior when initializing FaceAlignment:
device: Specify the device (e.g., 'cuda' for NVIDIA GPUs, 'mps' for Apple M GPUs, or 'cpu').dtype: Specify the torch data type (e.g., torch.bfloat16).compile: Boolean. The network is compiled with torch.compile by default for faster inference. Set compile=False to skip the initial compilation delay (approx. 25s) for instant startup.max_batch_size: Integer. Limit the batch size for multi-face images on low-memory GPUs (default is 1).
Example configuration:
import torch
import face_alignment
# Use CUDA with bfloat16
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType.TWO_D, dtype=torch.bfloat16, device='cuda')
# Use CPU and skip compilation for instant startup
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType.TWO_D, device='cpu', compile=False)
# Limit batch size for low-memory GPUs
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType.TWO_D, device='cuda', max_batch_size=8)