Skip to content
Snippets Groups Projects
Commit bfa032b5 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #37914 failed
Showing
with 430 additions and 0 deletions
import numpy as np
filters_multiply = np.load("results/filters/oulunpu_vs_replaymobile.npy")
model_dir = "results/deep_pix_bis_pruned_by_replaymobile"
import numpy as np
filters_multiply = np.load("results/filters/oulunpu_vs_swan.npy")
model_dir = "results/deep_pix_bis_pruned_by_swan"
import numpy as np
import os
import click
@click.command()
@click.argument("input")
@click.argument("output")
@click.option("--drop", default=0.2, show_default=True, type=click.FLOAT)
def find_filters(
input, output, drop,
):
d = np.load(input)
sort = np.argsort(d)
filters = np.ones_like(sort)
n_filters = len(d)
filters[sort[-int(n_filters * drop) :]] = 0
os.makedirs(os.path.dirname(output), exist_ok=True)
np.save(output, filters)
click.echo(f"Wrote the filter multiplier in {output}!")
protocol = "grandtest"
database.protocol = protocol
protocol = "grandtest-color-50-PrintReplay"
database.protocol = protocol
from bob.extension import rc
from bob.pad.base.database import PadDatabase, PadFile
from glob import glob
import os
import bob.io.base
import numpy as np
class IJBCCFile(PadFile):
"""The class for IJB-C cleaned images"""
def __init__(self, path):
client_id = file_id = path
attack_type = None
super().__init__(client_id, path, attack_type, file_id)
def load(self, directory=None, extension=None):
path = self.make_path(directory or self.original_directory, None)
img = bob.io.base.load(path)
img = np.pad(
img, [(0, 0), (11, 11), (11, 11)], mode="constant", constant_values=0
)
assert img.shape == (3, 246, 246), img.shape
return img
@property
def frames(self):
img = self.load()
yield img
@property
def annotations(self):
return {"0": {"leye": (67, 179), "reye": (67, 66.5)}}
class IJBCCDatabase(PadDatabase):
"""A database interface for IJB-C cleaned images."""
def __init__(self, original_directory=rc["bob.paper.icassp2020_domain_guided_pruning.ijbc_cleaned"], **kwargs):
super().__init__(name="ijbcc", original_directory=original_directory, **kwargs)
def objects(
self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs
):
if purposes != "real":
return []
if isinstance(groups, str):
groups = [groups]
groups = list(groups)
assert groups == ["train"], groups
paths = sorted(glob(os.path.join(self.original_directory, "*.png")))
paths = [os.path.basename(p) for p in paths]
files = [IJBCCFile(p) for p in paths]
for f in files:
f.original_directory = self.original_directory
return files
def annotations(self, file):
return file.annotations
database = IJBCCDatabase()
# coding: utf-8
from .transforms import (
deep_pix_pre_transform,
deep_pix_train_transform,
deep_pix_post_transform,
)
from bob.extension import rc
from bob.learn.tensorflow.dataset.generator import Generator
from bob.pad.base import padfile_to_label
from functools import partial
from multiprocessing import cpu_count
import os
import random
import tensorflow as tf
multiple_samples = True
video_container = True
def input_fn(mode, cache_only=False):
# Variables from other config files
VARIABLES = globals()
database = VARIABLES["database"]
groups = VARIABLES["groups"]
load_data = VARIABLES["load_data"]
shuffle_size = int(1e4)
batch_size = {
tf.estimator.ModeKeys.TRAIN: 32,
tf.estimator.ModeKeys.EVAL: 8,
tf.estimator.ModeKeys.PREDICT: 56,
}
if isinstance(batch_size, dict):
batch_size = batch_size[mode]
dataset_cache_after_generator = "faces-mtcnn-224-bfpa"
files_bf, files_pa = database.all_files(groups=groups, flat=False)
def reader(f):
key = str(f.make_path("", "")).encode("utf-8")
label = padfile_to_label(f)
data = load_data(database, f)
if multiple_samples:
for d in data:
yield {"data": d, "key": key}, label
else:
return {"data": data, "key": key}, label
if mode in (tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL):
random.shuffle(files_bf)
random.shuffle(files_pa)
gen_bf = Generator(files_bf, reader, multiple_samples=True)
data_bf = tf.data.Dataset.from_generator(
gen_bf, gen_bf.output_types, gen_bf.output_shapes
)
gen_pa = Generator(files_pa, reader, multiple_samples=True)
data_pa = tf.data.Dataset.from_generator(
gen_pa, gen_pa.output_types, gen_pa.output_shapes
)
# cache
cache_paths = []
if dataset_cache_after_generator is not None:
path_mask = "{temp}/processed/{db}/{protocol}/{cache}/{ty}-{mode}.dbcache"
path = path_mask.format(
db=database.name,
protocol=database.protocol,
mode=mode,
ty="bf",
temp=rc["temp"],
cache=dataset_cache_after_generator,
)
os.makedirs(os.path.dirname(path), exist_ok=True)
data_bf = data_bf.cache(path)
cache_paths.append(path)
path = path_mask.format(
db=database.name,
protocol=database.protocol,
mode=mode,
ty="pa",
temp=rc["temp"],
cache=dataset_cache_after_generator,
)
data_pa = data_pa.cache(path)
cache_paths.append(path)
if cache_only:
if all(os.path.isfile(path + ".index") for path in cache_paths):
data_bf = data_bf.take(1)
data_pa = data_pa.take(1)
return data_bf.concatenate(data_pa)
# balance the dataset for bf and pa
if mode == tf.estimator.ModeKeys.TRAIN:
# balance the dataset with repeat and sample_from_datasets
data_bf = data_bf.apply(
tf.data.experimental.shuffle_and_repeat(buffer_size=shuffle_size)
)
data_pa = data_pa.apply(
tf.data.experimental.shuffle_and_repeat(buffer_size=shuffle_size)
)
dataset = tf.data.experimental.sample_from_datasets((data_bf, data_pa))
elif mode == tf.estimator.ModeKeys.EVAL:
# balance the dataset with shuffle, zip, and flat_map
data_bf = data_bf.shuffle(buffer_size=shuffle_size)
data_pa = data_pa.shuffle(buffer_size=shuffle_size)
dataset = tf.data.Dataset.zip((data_bf, data_pa)).flat_map(
lambda x0, x1: tf.data.Dataset.from_tensors(x0).concatenate(
tf.data.Dataset.from_tensors(x1)
)
)
else:
dataset = data_bf.concatenate(data_pa)
drop_remainder = not mode == tf.estimator.ModeKeys.PREDICT
def aggregate_transform(features, labels):
images = features["data"]
images = deep_pix_pre_transform(images)
if mode == tf.estimator.ModeKeys.TRAIN:
images = deep_pix_train_transform(images)
images = deep_pix_post_transform(images)
features["data"] = images
return features, labels
dataset = dataset.apply(
tf.data.experimental.map_and_batch(
aggregate_transform,
batch_size,
drop_remainder=drop_remainder,
num_parallel_calls=max(cpu_count() - 1, 1),
)
)
gpu_device_name = tf.test.gpu_device_name()
if gpu_device_name:
return dataset.apply(tf.data.experimental.prefetch_to_device(gpu_device_name))
else:
return dataset.prefetch(-1)
train_input_fn = partial(input_fn, mode=tf.estimator.ModeKeys.TRAIN)
eval_input_fn = partial(input_fn, mode=tf.estimator.ModeKeys.EVAL)
predict_input_fn = partial(input_fn, mode=tf.estimator.ModeKeys.PREDICT)
This diff is collapsed.
from bob.pad.face.utils import the_giant_video_loader
from functools import partial
from .face_video_224 import cropper
from .face_normalizer_drop_120 import normalizer
multiple_samples = True
load_data = partial(
the_giant_video_loader, region="crop", cropper=cropper, normalizer=normalizer
)
from bob.pad.face.utils import the_giant_video_loader
from functools import partial
from .face_video_224 import cropper
multiple_samples = True
load_data = partial(the_giant_video_loader, region="crop", cropper=cropper)
from bob.db.oulunpu.config import database
from bob.extension import rc
database.annotation_directory = rc["bob.db.oulunpu.annotation_dir"]
database.protocol = "Protocol_1"
protocol = "pad_p2_face_f1"
database.protocol = protocol
from bob.pad.base.algorithm import VideoPredictions
from bob.bio.base.preprocessor import CallablePreprocessor
from bob.bio.base.extractor import CallableExtractor
from bob.bio.video.extractor import Wrapper
extracted_directory = "predictions"
algorithm = VideoPredictions(axis=0)
preprocessor = CallablePreprocessor(lambda x: x)
extractor = Wrapper(CallableExtractor(lambda x: x))
skip_preprocessing = True
skip_extraction = True
import click
@click.command()
@click.argument("dataset_path")
@click.argument("output_dir")
def prepare_ijbc_images(dataset_path, output_dir):
"""Prepares the IJB-C images for the experiments
Parameters
----------
dataset_path : str
Path to the dataset, ending in ``IJB-C/images/img``.
output_dir : str
Path to a folder where the new images will be saved.
"""
from .face_video_224 import cropper
from bob.io.base import load, save
from bob.ip.tensorflow_extractor import MTCNN
from glob import glob
from tqdm import tqdm
import bob.io.image # noqa: F401 import needed to load and save images
import os
import pandas as pd
import pkg_resources
# load the white-list
path = pkg_resources.resource_filename(__name__, "list_of_ijb-c_images.csv")
df = pd.read_csv(path)
detector = MTCNN()
all_images = glob(f"{dataset_path}/*.jpg")
# filter the images that we want only
all_images = list(
filter(lambda x: (os.path.basename(x) == df["paths"]).any(), all_images)
)
# Load, crop, and save faces
for img_path in tqdm(all_images):
filename = os.path.basename(img_path)
img = load(img_path)
detections = detector(img)
indices = df["paths"] == filename
wanted_faces = df[indices]["face_ids"].to_numpy()
for i, annots in enumerate(detections):
if i not in wanted_faces:
continue
face = cropper(img, annots)
# save face as a separate image
out = os.path.join(output_dir, f"{filename}_face_{i}.png")
save(face, out, create_directories=True)
from bob.pad.face.config.replay_mobile import database
from bob.extension import rc
database.original_directory = rc["bob.db.replaymobile.directory"]
database.name = "replaymobile"
database.annotation_directory = rc["bob.db.replaymobile.annotation_dir"]
database.protocol = "grandtest"
from bob.db.swan.config_pad_video import database
def filter_samples(sample):
return "IDIAP" in sample.client_id
database.all_files_options = dict(filter_samples=filter_samples)
database.protocol = "pad_p2_face_f1"
try:
groups
except NameError:
groups = []
groups += ["train"]
groups = ["train", "dev", "eval"]
import tensorflow as tf
from bob.learn.tensorflow.utils import to_channels_last
def deep_pix_pre_transform(image):
image = to_channels_last(image)
# Assuming image is uint8
image = tf.image.convert_image_dtype(image, tf.float32)
return image
def deep_pix_train_transform(image):
random_brightness_max_delta = 0.15
random_contrast_lower = 0.85
random_contrast_upper = 1.15
random_saturation_lower = 0.85
random_saturation_upper = 1.15
# random brightness
image = tf.image.random_brightness(image, max_delta=random_brightness_max_delta)
image = tf.clip_by_value(image, 0, 1)
# random contrast
image = tf.image.random_contrast(
image, lower=random_contrast_lower, upper=random_contrast_upper
)
image = tf.clip_by_value(image, 0, 1)
# random_hue
image = tf.image.random_saturation(
image, lower=random_saturation_lower, upper=random_saturation_upper
)
image = tf.clip_by_value(image, 0, 1)
# random_flip_left_right
image = tf.image.random_flip_left_right(image)
return image
def deep_pix_post_transform(image):
# normalize with ImageNet mean and std
mean = tf.constant([0.485, 0.456, 0.406], dtype="float32", name="imagenet_mean")
std = tf.constant([0.229, 0.224, 0.225], dtype="float32", name="imagenet_std")
image = (image - mean) / std
return image
copy.sh 0 → 100644
#!/bin/bash
set -ex
databases=(oulunpu replaymobile swan batl)
protocols_1=(Protocol_1 grandtest pad_p2_face_f1 grandtest_color_50_PrintReplay)
protocols_2=(Protocol_1 grandtest pad_p2_face_f1 grandtest-color-50-PrintReplay)
old_algo=(pixel_wise_densenet_29 pixel_wise_densenet_68 pixel_wise_densenet_65 pixel_wise_densenet_69)
new_algo=(deep_pix_bis blur_error autoencoder_error autoencoder_error_thresholded)
for ((i=0;i<${#databases[@]};i++)); do
for ((j=0;j<${#old_algo[@]};j++)); do
for grp in dev eval; do
mkdir -p "results/${databases[i]}/${protocols_1[i]}/${new_algo[j]}/${protocols_2[i]}/scores/"
cp "/idiap/user/amohammadi/${databases[i]}/oulunpu_Protocol_1_${old_algo[j]}/${protocols_2[i]}/scores/scores-${grp}" \
"results/${databases[i]}/${protocols_1[i]}/${new_algo[j]}/${protocols_2[i]}/scores/scores-${grp}"
done
done
done
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment