From 9ed7a42c8533852cc4af6800280d96cfe557318b Mon Sep 17 00:00:00 2001 From: Brandon Amos Date: Mon, 11 Jan 2016 12:13:14 -0500 Subject: [PATCH] Move tests to separate directory. --- .travis.yml | 2 +- run-tests.sh | 7 ++ tests/__init__.py | 0 tests/openface_api_tests.py | 96 +++++++++++++++++++++++++ test.py => tests/openface_demo_tests.py | 66 +++-------------- 5 files changed, 114 insertions(+), 57 deletions(-) create mode 100644 run-tests.sh create mode 100644 tests/__init__.py create mode 100644 tests/openface_api_tests.py rename test.py => tests/openface_demo_tests.py (59%) diff --git a/.travis.yml b/.travis.yml index 5010cb3..f629f65 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,4 +26,4 @@ script: wget http://openface-models.storage.cmusatyalab.org/nn4.v1.t7 \ -O ./models/openface/nn4.v1.t7 && \ python2 setup.py install && \ - nosetests-2.7 -v -d test.py" + ./run-tests.sh" diff --git a/run-tests.sh b/run-tests.sh new file mode 100644 index 0000000..689858c --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +cd $(dirname $0) + +nosetests-2.7 -v diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/openface_api_tests.py b/tests/openface_api_tests.py new file mode 100644 index 0000000..d195385 --- /dev/null +++ b/tests/openface_api_tests.py @@ -0,0 +1,96 @@ +# OpenFace tests, run with `nosetests-2.7 -v -d test.py` +# +# Copyright 2015 Carnegie Mellon University +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import cv2 +import os +import re +import shutil + +import numpy as np +np.set_printoptions(precision=2) +from numpy.linalg import norm + +import scipy +import scipy.spatial + +import openface + +from subprocess import Popen, PIPE + +openfaceDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +modelDir = os.path.join(openfaceDir, 'models') +dlibModelDir = os.path.join(modelDir, 'dlib') +openfaceModelDir = os.path.join(modelDir, 'openface') + +exampleImages = os.path.join(openfaceDir, 'images', 'examples') +lfwSubset = os.path.join(openfaceDir, 'data', 'lfw-subset') + +dlibFacePredictor = os.path.join(dlibModelDir, + "shape_predictor_68_face_landmarks.dat") +nn4_v1_model = os.path.join(openfaceModelDir, 'nn4.v1.t7') +nn4_v2_model = os.path.join(openfaceModelDir, 'nn4.v2.t7') +imgDim = 96 + +align = openface.AlignDlib(dlibFacePredictor) +nn4_v1 = openface.TorchNeuralNet(nn4_v1_model, imgDim=imgDim) +nn4_v2 = openface.TorchNeuralNet(nn4_v2_model, imgDim=imgDim) + + +def test_v1_pipeline(): + imgPath = os.path.join(exampleImages, 'lennon-1.jpg') + bgrImg = cv2.imread(imgPath) + if bgrImg is None: + raise Exception("Unable to load image: {}".format(imgPath)) + rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB) + assert np.isclose(norm(rgbImg), 11.1355) + + bb = align.getLargestFaceBoundingBox(rgbImg) + assert bb.left() == 341 + assert bb.right() == 1006 + assert bb.top() == 193 + assert bb.bottom() == 859 + + # Should be INNER_EYES_AND_BOTTOM_LIP by default. + alignedFace = align.align(imgDim, rgbImg, bb) + assert np.isclose(norm(alignedFace), 8.30662) + + alignedFace_alt = align.align(imgDim, rgbImg, bb, + landmarkIndices=openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP) + assert np.isclose(norm(alignedFace), norm(alignedFace_alt)) + + +def test_v2_pipeline(): + imgPath = os.path.join(exampleImages, 'lennon-1.jpg') + bgrImg = cv2.imread(imgPath) + if bgrImg is None: + raise Exception("Unable to load image: {}".format(imgPath)) + rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB) + assert np.isclose(norm(rgbImg), 11.1355) + + bb = align.getLargestFaceBoundingBox(rgbImg) + assert bb.left() == 341 + assert bb.right() == 1006 + assert bb.top() == 193 + assert bb.bottom() == 859 + + alignedFace = align.align(imgDim, rgbImg, bb, + landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE) + assert np.isclose(norm(alignedFace), 7.61577) + + rep = nn4_v2.forward(alignedFace) + cosDist = scipy.spatial.distance.cosine(rep, np.ones(128)) + assert np.isclose(cosDist, 0.981229293936) diff --git a/test.py b/tests/openface_demo_tests.py similarity index 59% rename from test.py rename to tests/openface_demo_tests.py index 5e603a4..297b1ef 100644 --- a/test.py +++ b/tests/openface_demo_tests.py @@ -31,13 +31,13 @@ import openface from subprocess import Popen, PIPE -fileDir = os.path.dirname(os.path.realpath(__file__)) -modelDir = os.path.join(fileDir, 'models') +openfaceDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +modelDir = os.path.join(openfaceDir, 'models') dlibModelDir = os.path.join(modelDir, 'dlib') openfaceModelDir = os.path.join(modelDir, 'openface') -exampleImages = os.path.join(fileDir, 'images', 'examples') -lfwSubset = os.path.join(fileDir, 'data', 'lfw-subset') +exampleImages = os.path.join(openfaceDir, 'images', 'examples') +lfwSubset = os.path.join(openfaceDir, 'data', 'lfw-subset') dlibFacePredictor = os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat") @@ -50,54 +50,8 @@ nn4_v1 = openface.TorchNeuralNet(nn4_v1_model, imgDim=imgDim) nn4_v2 = openface.TorchNeuralNet(nn4_v2_model, imgDim=imgDim) -def test_v1_pipeline(): - imgPath = os.path.join(exampleImages, 'lennon-1.jpg') - bgrImg = cv2.imread(imgPath) - if bgrImg is None: - raise Exception("Unable to load image: {}".format(imgPath)) - rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB) - assert np.isclose(norm(rgbImg), 11.1355) - - bb = align.getLargestFaceBoundingBox(rgbImg) - assert bb.left() == 341 - assert bb.right() == 1006 - assert bb.top() == 193 - assert bb.bottom() == 859 - - # Should be INNER_EYES_AND_BOTTOM_LIP by default. - alignedFace = align.align(imgDim, rgbImg, bb) - assert np.isclose(norm(alignedFace), 8.30662) - - alignedFace_alt = align.align(imgDim, rgbImg, bb, - landmarkIndices=openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP) - assert np.isclose(norm(alignedFace), norm(alignedFace_alt)) - - -def test_v2_pipeline(): - imgPath = os.path.join(exampleImages, 'lennon-1.jpg') - bgrImg = cv2.imread(imgPath) - if bgrImg is None: - raise Exception("Unable to load image: {}".format(imgPath)) - rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB) - assert np.isclose(norm(rgbImg), 11.1355) - - bb = align.getLargestFaceBoundingBox(rgbImg) - assert bb.left() == 341 - assert bb.right() == 1006 - assert bb.top() == 193 - assert bb.bottom() == 859 - - alignedFace = align.align(imgDim, rgbImg, bb, - landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE) - assert np.isclose(norm(alignedFace), 7.61577) - - rep = nn4_v2.forward(alignedFace) - cosDist = scipy.spatial.distance.cosine(rep, np.ones(128)) - assert np.isclose(cosDist, 0.981229293936) - - def test_compare_demo(): - cmd = ['python2', os.path.join(fileDir, 'demos', 'compare.py'), + cmd = ['python2', os.path.join(openfaceDir, 'demos', 'compare.py'), os.path.join(exampleImages, 'lennon-1.jpg'), os.path.join(exampleImages, 'lennon-2.jpg')] p = Popen(cmd, stdout=PIPE, stderr=PIPE) @@ -107,9 +61,9 @@ def test_compare_demo(): def test_classification_demo_pretrained(): - cmd = ['python2', os.path.join(fileDir, 'demos', 'classifier.py'), + cmd = ['python2', os.path.join(openfaceDir, 'demos', 'classifier.py'), 'infer', - os.path.join(fileDir, 'models', 'openface', + os.path.join(openfaceDir, 'models', 'openface', 'celeb-classifier.nn4.v2.pkl'), os.path.join(exampleImages, 'carell.jpg')] p = Popen(cmd, stdout=PIPE, stderr=PIPE) @@ -122,7 +76,7 @@ def test_classification_demo_training(): # Get lfw-subset by running ./data/download-lfw-subset.sh assert os.path.isdir(lfwSubset) - cmd = ['python2', os.path.join(fileDir, 'util', 'align-dlib.py'), + cmd = ['python2', os.path.join(openfaceDir, 'util', 'align-dlib.py'), os.path.join(lfwSubset, 'raw'), 'align', 'outerEyesAndNose', os.path.join(lfwSubset, 'aligned')] p = Popen(cmd, stdout=PIPE, stderr=PIPE) @@ -136,14 +90,14 @@ def test_classification_demo_training(): (out, err) = p.communicate() assert p.returncode == 0 - cmd = ['python2', os.path.join(fileDir, 'demos', 'classifier.py'), + cmd = ['python2', os.path.join(openfaceDir, 'demos', 'classifier.py'), 'train', os.path.join(lfwSubset, 'reps')] p = Popen(cmd, stdout=PIPE, stderr=PIPE) (out, err) = p.communicate() assert p.returncode == 0 - cmd = ['python2', os.path.join(fileDir, 'demos', 'classifier.py'), + cmd = ['python2', os.path.join(openfaceDir, 'demos', 'classifier.py'), 'infer', os.path.join(lfwSubset, 'reps', 'classifier.pkl'), os.path.join(lfwSubset, 'raw', 'Adrien_Brody', 'Adrien_Brody_0001.jpg')]