Skip to content

Commit

Permalink
Merge pull request #235 from 1adrianb/devel
Browse files Browse the repository at this point in the history
Fix multiple issues, restructure code partially
  • Loading branch information
1adrianb authored Dec 16, 2020
2 parents 632481f + 1b0dd21 commit 5ffd6d8
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 434 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"

addons:
apt_packages:
Expand All @@ -24,7 +25,7 @@ install:
# Useful for debugging any issues with conda
- conda info -a

- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION pytorch-cpu -c pytorch
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION pytorch cpuonly -c pytorch
- source activate test-environment
- travis_retry pip install --upgrade pip setuptools wheel
- travis_retry pip install -r requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Based on https://github.com/pytorch/pytorch/blob/master/Dockerfile
# Based on a older version of https://github.com/pytorch/pytorch/blob/master/Dockerfile
FROM nvidia/cuda:10.1-cudnn7-devel-ubuntu18.04

RUN apt-get update && apt-get install -y --no-install-recommends \
Expand Down
44 changes: 6 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ Please also see the ``examples`` folder

### Requirements

* Python 3.5+ or Python 2.7 (it may work with other versions too). Support for Python 2.7 is deprecated.
* Python 3.5+ (it may work with other versions too). Last version with support for python 2.7 was v1.1.1
* Linux, Windows or macOS
* pytorch (>=1.0)
* pytorch (>=1.2)

While not required, for optimal performance(especially for the detector) it is **highly** recommended to run the code using a CUDA enabled GPU.

Expand All @@ -104,39 +104,7 @@ Alternatively, bellow, you can find instruction to build it from source.

### From source

Install pytorch and pytorch dependencies. Instructions taken from [pytorch readme](https://github.com/pytorch/pytorch). For a more updated version check the framework github page.

On Linux
```bash
export CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" # [anaconda root directory]

# Install basic dependencies
conda install numpy pyyaml mkl setuptools cmake gcc cffi

# Add LAPACK support for the GPU
conda install -c soumith magma-cuda80 # or magma-cuda75 if CUDA 7.5
```

On OSX
```bash
export CMAKE_PREFIX_PATH=[anaconda root directory]
conda install numpy pyyaml setuptools cmake cffi
```
#### Get the PyTorch source
```bash
git clone --recursive https://github.com/pytorch/pytorch
```

#### Install PyTorch
On Linux
```bash
python setup.py install
```

On OSX
```bash
MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install
```
Install pytorch and pytorch dependencies. Please check the [pytorch readme](https://github.com/pytorch/pytorch) for this.

#### Get the Face Alignment source code
```bash
Expand All @@ -150,7 +118,7 @@ python setup.py install

### Docker image

A Dockerfile is provided to build images with cuda support and cudnn v5. For more instructions about running and building a docker image check the orginal Docker documentation.
A Dockerfile is provided to build images with cuda support and cudnn. For more instructions about running and building a docker image check the orginal Docker documentation.
```
docker build -t face-alignment .
```
Expand All @@ -161,7 +129,7 @@ While here the work is presented as a black-box, if you want to know more about

## Contributions

All contributions are welcomed. If you encounter any issue (including examples of images where it fails) feel free to open an issue.
All contributions are welcomed. If you encounter any issue (including examples of images where it fails) feel free to open an issue. If you plan to add a new features please open an issue to discuss this prior to making a pull request.

## Citation

Expand All @@ -180,4 +148,4 @@ For citing dlib, pytorch or any other packages used here please check the origin

* To the [pytorch](http://pytorch.org/) team for providing such an awesome deeplearning framework
* To [my supervisor](http://www.cs.nott.ac.uk/~pszyt/) for his patience and suggestions.
* To all other python developers that made available the rest of the packages used in this repository.
* To all other python developers that made available the rest of the packages used in this repository.
2 changes: 1 addition & 1 deletion face_alignment/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

__author__ = """Adrian Bulat"""
__email__ = 'adrian[email protected]'
__email__ = 'adrian@adrianbulat.com'
__version__ = '1.1.1'

from .api import FaceAlignment, LandmarksType, NetworkSize
19 changes: 0 additions & 19 deletions face_alignment/api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
from __future__ import print_function
import os
import torch
from torch.utils.model_zoo import load_url
from enum import Enum
from skimage import io
from skimage import color
import numpy as np
import cv2
try:
import urllib.request as request_file
except BaseException:
import urllib as request_file

from .models import FAN, ResNetDepth
from .utils import *
Expand Down Expand Up @@ -269,15 +262,3 @@ def get_landmarks_from_directory(self, path, extensions=['.jpg', '.png'], recurs
predictions[image_path] = preds

return predictions

@staticmethod
def remove_models(self):
base_path = os.path.join(appdata_dir('face_alignment'), "data")
for data_model in os.listdir(base_path):
file_path = os.path.join(base_path, data_model)
try:
if os.path.isfile(file_path):
print('Removing ' + data_model + ' ...')
os.unlink(file_path)
except Exception as e:
print(e)
15 changes: 2 additions & 13 deletions face_alignment/detection/blazeface/blazeface_detector.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import os
import cv2
from torch.utils.model_zoo import load_url

from ..core import FaceDetector
from ...utils import load_file_from_url

from .net_blazeface import BlazeFace
from .detect import *

import requests
import io


def load_numpy_from_url(url):
response = requests.get(url)
response.raise_for_status()
data = np.load(io.BytesIO(response.content)) # Works!
return data

models_urls = {
'blazeface_weights': 'https://github.com/hollance/BlazeFace-PyTorch/blob/master/blazeface.pth?raw=true',
'blazeface_anchors': 'https://github.com/hollance/BlazeFace-PyTorch/blob/master/anchors.npy?raw=true'
Expand All @@ -30,7 +19,7 @@ def __init__(self, device, path_to_detector=None, path_to_anchor=None, verbose=F
# Initialise the face detector
if path_to_detector is None:
model_weights = load_url(models_urls['blazeface_weights'])
model_anchors = load_numpy_from_url(models_urls['blazeface_anchors'])
model_anchors = np.load(load_file_from_url(models_urls['blazeface_anchors']))
else:
model_weights = torch.load(path_to_detector)
model_anchors = np.load(path_to_anchor)
Expand Down
10 changes: 0 additions & 10 deletions face_alignment/detection/blazeface/detect.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import torch
import torch.nn.functional as F

import os
import sys
import cv2
import random
import datetime
import math
import argparse
import numpy as np

import scipy.io as sio
import zipfile

from .utils import *
# from .net_blazeface import s3fd


def detect(net, img, device):
Expand Down
32 changes: 5 additions & 27 deletions face_alignment/detection/dlib/dlib_detector.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
import os
import warnings
import cv2
import dlib

try:
import urllib.request as request_file
except BaseException:
import urllib as request_file

from ..core import FaceDetector
from ...utils import appdata_dir
from ...utils import load_file_from_url


class DlibDetector(FaceDetector):
def __init__(self, device, path_to_detector=None, verbose=False):
super().__init__(device, verbose)

print('Warning: this detector is deprecated. Please use a different one, i.e.: S3FD.')
base_path = os.path.join(appdata_dir('face_alignment'), "data")
os.makedirs(base_path, exist_ok=True)
warnings.warn('Warning: this detector is deprecated. Please use a different one, i.e.: S3FD.')

# Initialise the face detector
if 'cuda' in device:
if path_to_detector is None:
path_to_detector = os.path.join(
base_path, "mmod_human_face_detector.dat")

if not os.path.isfile(path_to_detector):
print("Downloading the face detection CNN. Please wait...")

path_to_temp_detector = os.path.join(
base_path, "mmod_human_face_detector.dat.download")

if os.path.isfile(path_to_temp_detector):
os.remove(os.path.join(path_to_temp_detector))

request_file.urlretrieve(
"https://www.adrianbulat.com/downloads/dlib/mmod_human_face_detector.dat",
os.path.join(path_to_temp_detector))

os.rename(os.path.join(path_to_temp_detector), os.path.join(path_to_detector))
path_to_detector = load_file_from_url(
"https://www.adrianbulat.com/downloads/dlib/mmod_human_face_detector.dat")

self.face_detector = dlib.cnn_face_detection_model_v1(path_to_detector)
else:
Expand Down
1 change: 0 additions & 1 deletion face_alignment/detection/sfd/bbox.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import os
import sys
import cv2
Expand Down
2 changes: 2 additions & 0 deletions face_alignment/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .fan import FAN
from .resnet import ResNetDepth
Loading

0 comments on commit 5ffd6d8

Please sign in to comment.