2016-01-12 05:17:31 +08:00
|
|
|
# OpenFace training tests.
|
|
|
|
#
|
2016-01-14 03:52:52 +08:00
|
|
|
# Copyright 2015-2016 Carnegie Mellon University
|
2016-01-12 05:17:31 +08:00
|
|
|
#
|
|
|
|
# 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 os
|
|
|
|
import shutil
|
2017-04-25 06:01:10 +08:00
|
|
|
import sys
|
2016-01-12 05:17:31 +08:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
np.set_printoptions(precision=2)
|
|
|
|
import pandas as pd
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
|
|
|
openfaceDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
modelDir = os.path.join(openfaceDir, 'models')
|
|
|
|
|
|
|
|
exampleImages = os.path.join(openfaceDir, 'images', 'examples')
|
|
|
|
lfwSubset = os.path.join(openfaceDir, 'data', 'lfw-subset')
|
|
|
|
|
|
|
|
|
|
|
|
def test_dnn_training():
|
2016-06-07 02:27:33 +08:00
|
|
|
assert os.path.isdir(
|
|
|
|
lfwSubset), "Get lfw-subset by running ./data/download-lfw-subset.sh"
|
2016-01-12 05:17:31 +08:00
|
|
|
|
2016-01-12 06:50:31 +08:00
|
|
|
imgWorkDir = tempfile.mkdtemp(prefix='OpenFaceTrainingTest-Img-')
|
2017-04-25 06:01:10 +08:00
|
|
|
cmd = [sys.executable, os.path.join(openfaceDir, 'util', 'align-dlib.py'),
|
2016-01-12 05:17:31 +08:00
|
|
|
os.path.join(lfwSubset, 'raw'), 'align', 'outerEyesAndNose',
|
2016-03-07 08:45:37 +08:00
|
|
|
os.path.join(imgWorkDir, 'aligned')]
|
2017-04-25 05:50:16 +08:00
|
|
|
p = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
|
2016-01-12 05:17:31 +08:00
|
|
|
(out, err) = p.communicate()
|
2016-01-13 02:31:00 +08:00
|
|
|
print(out)
|
|
|
|
print(err)
|
2016-01-12 05:17:31 +08:00
|
|
|
assert p.returncode == 0
|
|
|
|
|
2017-04-25 06:01:10 +08:00
|
|
|
cmd = [sys.executable, os.path.join(openfaceDir, 'util', 'align-dlib.py'),
|
2016-06-07 02:27:33 +08:00
|
|
|
os.path.join(lfwSubset, 'raw'), 'align', 'outerEyesAndNose',
|
2016-07-16 04:00:43 +08:00
|
|
|
os.path.join(imgWorkDir, 'aligned')]
|
2017-04-25 05:50:16 +08:00
|
|
|
p = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
|
2016-06-07 02:27:33 +08:00
|
|
|
(out, err) = p.communicate()
|
|
|
|
print(out)
|
|
|
|
print(err)
|
|
|
|
assert p.returncode == 0
|
|
|
|
|
2016-01-12 06:50:31 +08:00
|
|
|
netWorkDir = tempfile.mkdtemp(prefix='OpenFaceTrainingTest-Net-')
|
2016-06-14 02:22:59 +08:00
|
|
|
saveDir = os.path.join(netWorkDir, '1')
|
2016-01-12 05:17:31 +08:00
|
|
|
cmd = ['th', './main.lua',
|
2016-01-12 06:50:31 +08:00
|
|
|
'-data', os.path.join(imgWorkDir, 'aligned'),
|
2016-01-12 05:17:31 +08:00
|
|
|
'-modelDef', '../models/openface/nn4.def.lua',
|
|
|
|
'-peoplePerBatch', '3',
|
2016-01-14 03:33:17 +08:00
|
|
|
'-imagesPerPerson', '10',
|
|
|
|
'-nEpochs', '10',
|
|
|
|
'-epochSize', '1',
|
2016-01-12 06:50:31 +08:00
|
|
|
'-cache', netWorkDir,
|
2016-06-14 02:22:59 +08:00
|
|
|
'-save', saveDir,
|
2016-06-14 02:06:12 +08:00
|
|
|
'-cuda', '-cudnn', '-testing',
|
2016-01-12 05:17:31 +08:00
|
|
|
'-nDonkeys', '-1']
|
2016-06-07 02:27:33 +08:00
|
|
|
p = Popen(cmd, stdout=PIPE, stderr=PIPE,
|
2017-04-25 05:50:16 +08:00
|
|
|
cwd=os.path.join(openfaceDir, 'training'), universal_newlines=True)
|
2016-01-12 05:17:31 +08:00
|
|
|
(out, err) = p.communicate()
|
2016-01-13 02:31:00 +08:00
|
|
|
print(out)
|
|
|
|
print(err)
|
2016-01-12 05:17:31 +08:00
|
|
|
assert p.returncode == 0
|
|
|
|
|
|
|
|
# Training won't make much progress on lfw-subset, but as a sanity check,
|
2016-06-14 02:22:59 +08:00
|
|
|
# make sure the training code runs and doesn't get worse than 0.2.
|
|
|
|
trainLoss = pd.read_csv(os.path.join(saveDir, 'train.log'),
|
2016-01-12 05:17:31 +08:00
|
|
|
sep='\t').as_matrix()[:, 0]
|
2016-01-13 04:46:49 +08:00
|
|
|
assert np.mean(trainLoss) < 0.3
|
2016-01-12 05:17:31 +08:00
|
|
|
|
2016-01-13 04:46:49 +08:00
|
|
|
shutil.rmtree(imgWorkDir)
|
2016-01-12 06:50:31 +08:00
|
|
|
shutil.rmtree(netWorkDir)
|