Commit 9f037558 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira Committed by Amir MOHAMMADI
Browse files

Porting replay attack to the new CSV interface

parent af2ae899
Pipeline #46489 failed with stage
in 11 minutes and 33 seconds
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
from bob.bio.base.database import AnnotationsLoader
from bob.pad.base.database import CSVPADDataset, CSVToSampleLoader
import bob.io.base
from bob.extension import rc
from bob.extension.download import get_file
filename = "replat-attack.tar.gz"
class ReplayAttackDatabase(CSVPADDataset):
def __init__(self, protocol):
# Downloading model if not exists
urls = [
"https://www.idiap.ch/software/bob/databases/latest/replay-attack.tar.gz",
"http://www.idiap.ch/software/bob/databases/latest/replay-attack.tar.gz",
]
final_filename = get_file(filename, urls)
self.annotation_type = ("bounding-box",)
self.fixed_positions = None
super().__init__(
final_filename,
protocol,
csv_to_sample_loader=CSVToSampleLoader(
data_loader=bob.io.base.load,
metadata_loader=AnnotationsLoader(
annotation_directory=rc["bob.db.replay.annotation_dir"],
annotation_extension=".json",
annotation_type="json",
),
dataset_original_directory=rc["bob.db.replay.directory"]
if rc["bob.db.replay.directory"]
else "",
extension=".mov",
),
)
......@@ -8,169 +8,218 @@ import bob.bio.base
from bob.bio.base.test.utils import db_available
@db_available('replay') # the name of the package
@db_available("replay") # the name of the package
def test_replay():
# replay-attack is the name of the entry point
replay_database_instance = bob.bio.base.load_resource(
'replay-attack',
'database',
preferred_package='bob.pad.face',
package_prefix='bob.pad.'
"replay-attack",
"database",
preferred_package="bob.pad.face",
package_prefix="bob.pad.",
).database
try:
assert len(
replay_database_instance.objects(
groups=['train', 'dev', 'eval'])) == 1200
assert len(
replay_database_instance.objects(groups=['train', 'dev'])) == 720
assert len(replay_database_instance.objects(groups=['train'])) == 360
assert len(
replay_database_instance.objects(
groups=['train', 'dev', 'eval'], protocol='grandtest')) == 1200
assert len(
replay_database_instance.objects(
groups=['train', 'dev', 'eval'],
protocol='grandtest',
purposes='real')) == 200
assert len(
replay_database_instance.objects(
groups=['train', 'dev', 'eval'],
protocol='grandtest',
purposes='attack')) == 1000
assert (
len(replay_database_instance.objects(groups=["train", "dev", "eval"]))
== 1200
)
assert len(replay_database_instance.objects(groups=["train", "dev"])) == 720
assert len(replay_database_instance.objects(groups=["train"])) == 360
assert (
len(
replay_database_instance.objects(
groups=["train", "dev", "eval"], protocol="grandtest"
)
)
== 1200
)
assert (
len(
replay_database_instance.objects(
groups=["train", "dev", "eval"],
protocol="grandtest",
purposes="real",
)
)
== 200
)
assert (
len(
replay_database_instance.objects(
groups=["train", "dev", "eval"],
protocol="grandtest",
purposes="attack",
)
)
== 1000
)
except IOError as e:
raise SkipTest(
"The database could not be queried; probably the db.sql3 file is missing. Here is the error: '%s'"
% e)
% e
)
@db_available('replaymobile')
@db_available("replaymobile")
def test_replaymobile():
replaymobile = bob.bio.base.load_resource(
'replay-mobile',
'database',
preferred_package='bob.pad.face',
package_prefix='bob.pad.').database
"replay-mobile",
"database",
preferred_package="bob.pad.face",
package_prefix="bob.pad.",
).database
try:
assert len(
replaymobile.objects(groups=['train', 'dev', 'eval'])) == 1030
assert len(replaymobile.objects(groups=['train', 'dev'])) == 728
assert len(replaymobile.objects(groups=['train'])) == 312
assert len(
replaymobile.objects(
groups=['train', 'dev', 'eval'], protocol='grandtest')) == 1030
assert len(
replaymobile.objects(
groups=['train', 'dev', 'eval'],
protocol='grandtest',
purposes='real')) == 390
assert len(
replaymobile.objects(
groups=['train', 'dev', 'eval'],
protocol='grandtest',
purposes='attack')) == 640
assert len(replaymobile.objects(groups=["train", "dev", "eval"])) == 1030
assert len(replaymobile.objects(groups=["train", "dev"])) == 728
assert len(replaymobile.objects(groups=["train"])) == 312
assert (
len(
replaymobile.objects(
groups=["train", "dev", "eval"], protocol="grandtest"
)
)
== 1030
)
assert (
len(
replaymobile.objects(
groups=["train", "dev", "eval"],
protocol="grandtest",
purposes="real",
)
)
== 390
)
assert (
len(
replaymobile.objects(
groups=["train", "dev", "eval"],
protocol="grandtest",
purposes="attack",
)
)
== 640
)
except IOError as e:
raise SkipTest(
"The database could not be queried; probably the db.sql3 file is missing. Here is the error: '%s'"
% e)
% e
)
# Test the maskattack database
@db_available('maskattack')
@db_available("maskattack")
def test_maskattack():
maskattack = bob.bio.base.load_resource(
'maskattack',
'database',
preferred_package='bob.pad.face',
package_prefix='bob.pad.').database
"maskattack",
"database",
preferred_package="bob.pad.face",
package_prefix="bob.pad.",
).database
try:
# all real sequences: 2 sessions, 5 recordings for 17 individuals
assert len(maskattack.objects(
groups=['train', 'dev', 'eval'], purposes='real')) == 170
assert (
len(maskattack.objects(groups=["train", "dev", "eval"], purposes="real"))
== 170
)
# all attacks: 1 session, 5 recordings for 17 individuals
assert len(maskattack.objects(
groups=['train', 'dev', 'eval'], purposes='attack')) == 85
assert (
len(maskattack.objects(groups=["train", "dev", "eval"], purposes="attack"))
== 85
)
# training real: 7 subjects, 2 sessions, 5 recordings
assert len(maskattack.objects(groups=['train'], purposes='real')) == 70
assert len(maskattack.objects(groups=["train"], purposes="real")) == 70
# training real: 7 subjects, 1 session, 5 recordings
assert len(maskattack.objects(
groups=['train'], purposes='attack')) == 35
assert len(maskattack.objects(groups=["train"], purposes="attack")) == 35
# dev and test contains the same number of sequences:
# real: 5 subjects, 2 sessions, 5 recordings
# attack: 5 subjects, 1 sessions, 5 recordings
assert len(maskattack.objects(groups=['dev'], purposes='real')) == 50
assert len(maskattack.objects(groups=['eval'], purposes='real')) == 50
assert len(maskattack.objects(groups=['dev'], purposes='attack')) == 25
assert len(maskattack.objects(
groups=['eval'], purposes='attack')) == 25
assert len(maskattack.objects(groups=["dev"], purposes="real")) == 50
assert len(maskattack.objects(groups=["eval"], purposes="real")) == 50
assert len(maskattack.objects(groups=["dev"], purposes="attack")) == 25
assert len(maskattack.objects(groups=["eval"], purposes="attack")) == 25
except IOError as e:
raise SkipTest(
"The database could not be queried; probably the db.sql3 file is missing. Here is the error: '%s'"
% e)
% e
)
# Test the casiasurf database
@db_available('casiasurf')
@db_available("casiasurf")
def test_casiasurf():
casiasurf = bob.bio.base.load_resource(
'casiasurf',
'database',
preferred_package='bob.pad.face',
package_prefix='bob.pad.').database
"casiasurf",
"database",
preferred_package="bob.pad.face",
package_prefix="bob.pad.",
).database
try:
assert len(casiasurf.objects(groups=['train'], purposes='real')) == 8942
assert len(casiasurf.objects(groups=['train'], purposes='attack')) == 20324
assert len(casiasurf.objects(groups=('dev',), purposes=('real',))) == 2994
assert len(casiasurf.objects(groups=('dev',), purposes=('attack',))) == 6614
assert len(casiasurf.objects(groups=('dev',), purposes=('real','attack'))) == 9608
assert len(casiasurf.objects(groups=('eval',), purposes=('real',))) == 17458
assert len(casiasurf.objects(groups=('eval',), purposes=('attack',))) == 40252
assert len(casiasurf.objects(groups=('eval',), purposes=('real','attack'))) == 57710
assert len(casiasurf.objects(groups=["train"], purposes="real")) == 8942
assert len(casiasurf.objects(groups=["train"], purposes="attack")) == 20324
assert len(casiasurf.objects(groups=("dev",), purposes=("real",))) == 2994
assert len(casiasurf.objects(groups=("dev",), purposes=("attack",))) == 6614
assert (
len(casiasurf.objects(groups=("dev",), purposes=("real", "attack"))) == 9608
)
assert len(casiasurf.objects(groups=("eval",), purposes=("real",))) == 17458
assert len(casiasurf.objects(groups=("eval",), purposes=("attack",))) == 40252
assert (
len(casiasurf.objects(groups=("eval",), purposes=("real", "attack")))
== 57710
)
except IOError as e:
raise SkipTest(
"The database could not be queried; probably the db.sql3 file is missing. Here is the error: '%s'"
% e)
% e
)
@db_available('casia_fasd')
@db_available("casia_fasd")
def test_casia_fasd():
casia_fasd = bob.bio.base.load_resource(
'casiafasd',
'database',
preferred_package='bob.pad.face',
package_prefix='bob.pad.').database
"casiafasd",
"database",
preferred_package="bob.pad.face",
package_prefix="bob.pad.",
).database
assert len(casia_fasd.objects()) == 600
assert len(casia_fasd.objects(purposes='real')) == 150
assert len(casia_fasd.objects(purposes='attack')) == 450
assert len(casia_fasd.objects(groups=('train', 'dev'))) == 240
assert len(casia_fasd.objects(groups='train')) == 180
assert len(casia_fasd.objects(groups='dev')) == 60
assert len(casia_fasd.objects(groups='eval')) == 360
assert len(casia_fasd.objects(purposes="real")) == 150
assert len(casia_fasd.objects(purposes="attack")) == 450
assert len(casia_fasd.objects(groups=("train", "dev"))) == 240
assert len(casia_fasd.objects(groups="train")) == 180
assert len(casia_fasd.objects(groups="dev")) == 60
assert len(casia_fasd.objects(groups="eval")) == 360
# test annotations since they are shipped with bob.db.casia_fasd
f = [f for f in casia_fasd.objects() if f.path == 'train_release/1/2'][0]
f = [f for f in casia_fasd.objects() if f.path == "train_release/1/2"][0]
assert len(f.annotations) == 132
a = f.annotations['0']
oracle = {'topleft': (102, 214), 'bottomright': (242, 354),
'reye': (151.0, 249.0), 'leye': (151.0, 319.0)}
a = f.annotations["0"]
oracle = {
"topleft": (102, 214),
"bottomright": (242, 354),
"reye": (151.0, 249.0),
"leye": (151.0, 319.0),
}
assert a == oracle, a
@db_available('casia_fasd')
@db_available("casia_fasd")
def test_casia_fasd_frames():
casia_fasd = bob.bio.base.load_resource(
'casiafasd',
'database',
preferred_package='bob.pad.face',
package_prefix='bob.pad.').database
"casiafasd",
"database",
preferred_package="bob.pad.face",
package_prefix="bob.pad.",
).database
# test frame loading if the db original files are available
try:
......@@ -179,10 +228,19 @@ def test_casia_fasd_frames():
for frame in f.frames:
assert frame.shape == (3, 1280, 720)
break
except (IOError, RuntimeError)as e:
except (IOError, RuntimeError) as e:
raise SkipTest(
"The database original files are missing. To run this test run "
"``bob config set bob.db.casia_fasd.directory "
"/path/to/casia_fasd_files`` in a terminal to point to the "
"original files on your computer. . Here is the error: '%s'"
% e)
"original files on your computer. . Here is the error: '%s'" % e
)
def test_replay_csv():
from bob.pad.face.database.replay_csv import ReplayAttackDatabase
database = ReplayAttackDatabase("grandtest")
assert len(database.fit_samples()) == 360
assert len(database.predict_samples(group="dev")) == 360
assert len(database.predict_samples(group="eval")) == 480
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment