def arcface_template(embedding, annotation_type, fixed_positions=None): # DEFINE CROPPING cropped_image_size = (112, 112) if annotation_type == "eyes-center" or annotation_type == "bounding-box": # Hard coding eye positions for backward consistency # cropped_positions = { cropped_positions = cropped_positions_arcface() if annotation_type == "bounding-box": # This will allow us to use `BoundingBoxAnnotatorCrop` cropped_positions.update( {"topleft": (0, 0), "bottomright": cropped_image_size} ) elif isinstance(annotation_type, list): cropped_positions = cropped_positions_arcface(annotation_type) else: cropped_positions = dnn_default_cropping( cropped_image_size, annotation_type ) annotator = MTCNN(min_size=40, factor=0.709, thresholds=(0.1, 0.2, 0.2)) transformer = embedding_transformer( cropped_image_size=cropped_image_size, embedding=embedding, cropped_positions=cropped_positions, fixed_positions=fixed_positions, color_channel="rgb", annotator=annotator, ) algorithm = Distance() return PipelineSimple(transformer, algorithm)
def _make_pipeline_simple(): transformer = make_pipeline(FunctionTransformer(func=_do_nothing_fn)) biometric_algorithm = Distance("euclidean", factor=1) pipeline_simple = PipelineSimple(transformer, biometric_algorithm, score_writer=None) return pipeline_simple
def _run_with_failure(score_all_vs_all, sporadic_fail): with tempfile.TemporaryDirectory() as dir_name: database = DummyDatabase( some_fta=sporadic_fail, all_fta=not sporadic_fail ) transformer = _make_transformer(dir_name) biometric_algorithm = Distance() pipeline_simple = PipelineSimple( transformer, biometric_algorithm, None, ) scores = pipeline_simple( database.background_model_samples(), database.references(), database.probes(), score_all_vs_all=score_all_vs_all, ) assert len(scores) == 10 for sample_scores in scores: assert len(sample_scores) == 10 for score in sample_scores: assert isinstance(score.data, float)
def run_pipeline(with_dask, score_all_vs_all): database = DummyDatabase() transformer = _make_transformer(dir_name) biometric_algorithm = Distance() pipeline_simple = PipelineSimple( transformer, biometric_algorithm, None, ) if with_dask: pipeline_simple = dask_bio_pipeline( pipeline_simple, npartitions=2 ) scores = pipeline_simple( database.background_model_samples(), database.references(), database.probes(), score_all_vs_all=score_all_vs_all, ) if with_dask: scores = scores.compute(scheduler="single-threaded") assert len(scores) == 10 for sample_scores in scores: assert len(sample_scores) == 10 for score in sample_scores: assert isinstance(score.data, float)
def run_pipeline( with_dask, score_writer=None, ): database = DummyDatabase() transformer = _make_transformer(dir_name) biometric_algorithm = BioAlgCheckpointWrapper( Distance(), base_dir=dir_name ) pipeline_simple = PipelineSimple( transformer, biometric_algorithm, score_writer ) if with_dask: pipeline_simple = dask_bio_pipeline( pipeline_simple, npartitions=2 ) scores = pipeline_simple( database.background_model_samples(), database.references(), database.probes(), score_all_vs_all=database.score_all_vs_all, ) if pipeline_simple.score_writer is None: if with_dask: scores = scores.compute(scheduler="single-threaded") assert len(scores) == 10 for sset in scores: if isinstance(sset[0], DelayedSample): for s in sset: assert len(s.data) == 10 else: assert len(sset) == 10 else: writed_scores = pipeline_simple.write_scores(scores) scores_dev_path = os.path.join(dir_name, "scores-dev") concatenated_scores = pipeline_simple.post_process( writed_scores, scores_dev_path ) if with_dask: concatenated_scores = concatenated_scores.compute( scheduler="single-threaded" ) if isinstance( pipeline_simple.score_writer, FourColumnsScoreWriter ): assert len(open(scores_dev_path).readlines()) == 100 else: assert ( len(open(scores_dev_path).readlines()) == 101 ) # Counting the header.
def test_pipeline_simple_passthrough(): """Ensure that PipelineSimple accepts a passthrough Estimator.""" passthrough = make_pipeline(None) pipeline = PipelineSimple(passthrough, Distance()) assert isinstance(pipeline, PipelineSimple) pipeline_with_passthrough = make_pipeline("passthrough") pipeline = PipelineSimple(pipeline_with_passthrough, Distance()) assert isinstance(pipeline, PipelineSimple) db = DummyDatabase() scores = pipeline( db.background_model_samples(), db.references(), db.probes() ) assert len(scores) == 10 for sample_scores in scores: assert len(sample_scores) == 10 for score in sample_scores: assert isinstance(score.data, float)
def facenet_template(embedding, annotation_type, fixed_positions=None): """ Facenet baseline template. This one will crop the face at :math:`160 \\times 160` Parameters ---------- embedding: obj Transformer that takes a cropped face and extract the embeddings annotation_type: str Type of the annotations (e.g. `eyes-center') fixed_positions: dict Set it if in your face images are registered to a fixed position in the image """ # DEFINE CROPPING cropped_image_size = (160, 160) if annotation_type == "eyes-center" or annotation_type == "bounding-box": # Hard coding eye positions for backward consistency # cropped_positions = { cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type="eyes-center") if annotation_type == "bounding-box": # This will allow us to use `BoundingBoxAnnotatorCrop` cropped_positions.update({ "topleft": (0, 0), "bottomright": cropped_image_size }) else: cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type) annotator = MTCNN(min_size=40, factor=0.709, thresholds=(0.1, 0.2, 0.2)) # ASSEMBLE TRANSFORMER transformer = embedding_transformer( cropped_image_size=cropped_image_size, embedding=embedding, cropped_positions=cropped_positions, fixed_positions=fixed_positions, color_channel="rgb", annotator=annotator, ) algorithm = Distance() return PipelineSimple(transformer, algorithm)
def oxford_vgg2_resnets( model_name, annotation_type, fixed_positions=None, memory_demanding=False ): """ Get the pipeline for the resnet based models from Oxford. All these models were training the the VGG2 dataset. Models taken from: https://www.robots.ox.ac.uk/~albanie Parameters ---------- model_name: str One of the 4 models available (`resnet50_scratch_dag`, `resnet50_ft_dag`, `senet50_ft_dag`, `senet50_scratch_dag`). annotation_type: str Type of the annotations (e.g. `eyes-center') fixed_positions: dict Set it if in your face images are registered to a fixed position in the image """ # DEFINE CROPPING cropped_image_size = (224, 224) if annotation_type == "eyes-center": # Coordinates taken from : https://www.merlin.uzh.ch/contributionDocument/download/14240 cropped_positions = {"leye": (100, 159), "reye": (100, 65)} else: cropped_positions = dnn_default_cropping( cropped_image_size, annotation_type ) transformer = embedding_transformer( cropped_image_size=cropped_image_size, embedding=OxfordVGG2Resnets( model_name=model_name, memory_demanding=memory_demanding ), cropped_positions=cropped_positions, fixed_positions=fixed_positions, color_channel="rgb", annotator="mtcnn", ) algorithm = Distance() from bob.bio.base.pipelines import PipelineSimple return PipelineSimple(transformer, algorithm)
def run_experiment(dataset): def linearize(X): X = np.asarray(X) return np.reshape(X, (X.shape[0], -1)) # Testing it in a real recognition systems transformer = wrap( ["sample"], make_pipeline(FunctionTransformer(linearize)) ) pipeline_simple = PipelineSimple(transformer, Distance()) return pipeline_simple( dataset.background_model_samples(), dataset.references(), dataset.probes(), )
def vgg16_oxford_baseline(annotation_type, fixed_positions=None): """ Get the VGG16 pipeline which will crop the face :math:`224 \\times 224` use the :py:class:`VGG16_Oxford` Parameters ---------- annotation_type: str Type of the annotations (e.g. `eyes-center') fixed_positions: dict Set it if in your face images are registered to a fixed position in the image """ # DEFINE CROPPING cropped_image_size = (224, 224) if annotation_type == "eyes-center" or annotation_type == "bounding-box": # Hard coding eye positions for backward consistency # cropped_positions = { cropped_positions = {"reye": (112, 82), "leye": (112, 142)} if annotation_type == "bounding-box": # This will allow us to use `BoundingBoxAnnotatorCrop` cropped_positions.update( {"topleft": (0, 0), "bottomright": cropped_image_size} ) else: cropped_positions = dnn_default_cropping( cropped_image_size, annotation_type ) annotator = MTCNN(min_size=40, factor=0.709, thresholds=(0.1, 0.2, 0.2)) transformer = embedding_transformer( cropped_image_size=cropped_image_size, embedding=VGG16_Oxford(), cropped_positions=cropped_positions, fixed_positions=fixed_positions, color_channel="rgb", annotator=annotator, ) algorithm = Distance() return PipelineSimple(transformer, algorithm)
def afffe_baseline( annotation_type, fixed_positions=None, memory_demanding=False, device=torch.device("cpu"), ): """ Get the AFFFE pipeline which will crop the face :math:`224 \\times 224` use the :py:class:`AFFFE_2021` Parameters ---------- annotation_type: str Type of the annotations (e.g. `eyes-center') fixed_positions: dict Set it if in your face images are registered to a fixed position in the image """ # DEFINE CROPPING cropped_image_size = (224, 224) if annotation_type == "eyes-center": # Hard coding eye positions for backward consistency cropped_positions = {"leye": (110, 144), "reye": (110, 96)} else: cropped_positions = dnn_default_cropping( cropped_image_size, annotation_type ) transformer = embedding_transformer( cropped_image_size=cropped_image_size, embedding=AFFFE_2021(memory_demanding=memory_demanding, device=device), cropped_positions=cropped_positions, fixed_positions=fixed_positions, color_channel="rgb", annotator="mtcnn", ) algorithm = Distance() from bob.bio.base.pipelines import PipelineSimple return PipelineSimple(transformer, algorithm)
def load(annotation_type, fixed_positions=None): transform_extra_arguments = (("annotations", "annotations"), ) transformer = make_pipeline( wrap( ["sample"], ToGray(), transform_extra_arguments=transform_extra_arguments, ), wrap( ["sample"], FunctionTransformer(lambda X: [x.flatten() for x in X]), ), ) algorithm = Distance() return PipelineSimple(transformer, algorithm)
def run_pipeline(with_dask, score_writer=None): database = DummyDatabase(one_d=False) transformer = _make_transformer(dir_name) biometric_algorithm = Distance() pipeline_simple = PipelineSimple(transformer, biometric_algorithm, score_writer) z_norm_postprocessor = ZNormScores() z_pipeline_simple = PipelineScoreNorm(pipeline_simple, z_norm_postprocessor) # Checkpointing everything pipeline_simple = checkpoint_pipeline_simple(pipeline_simple, base_dir=dir_name) if with_dask: z_pipeline_simple = dask_bio_pipeline(z_pipeline_simple, npartitions=2) (raw_scores, z_scores) = z_pipeline_simple( database.background_model_samples(), database.references(), database.probes(), database.zprobes(), score_all_vs_all=database.score_all_vs_all, ) def _concatenate(pipeline, scores, path): writed_scores = pipeline.write_scores(scores) concatenated_scores = pipeline.post_process( writed_scores, path) return concatenated_scores if isinstance(score_writer, CSVScoreWriter): raw_scores = _concatenate( z_pipeline_simple, raw_scores, os.path.join(dir_name, "scores-dev", "raw_scores"), ) z_scores = _concatenate( z_pipeline_simple, z_scores, os.path.join(dir_name, "scores-dev", "z_scores"), ) """ t_scores = _concatenate( z_pipeline_simple, t_scores, os.path.join(dir_name, "scores-dev", "t_scores"), ) zt_scores = _concatenate( vanilla_biometrics_pipeline, zt_scores, os.path.join(dir_name, "scores-dev", "zt_scores"), ) s_scores = _concatenate( vanilla_biometrics_pipeline, s_scores, os.path.join(dir_name, "scores-dev", "s_scores"), ) """ if with_dask: raw_scores = raw_scores.compute(scheduler="single-threaded") z_scores = z_scores.compute(scheduler="single-threaded") # t_scores = t_scores.compute(scheduler="single-threaded") # zt_scores = zt_scores.compute(scheduler="single-threaded") # s_scores = s_scores.compute(scheduler="single-threaded") if isinstance(score_writer, CSVScoreWriter): assert (len( open( os.path.join(dir_name, "scores-dev", "raw_scores"), "r", ).readlines()) == 101) assert (len( open( os.path.join(dir_name, "scores-dev", "z_scores"), "r", ).readlines()) == 101) """ assert ( len( open( os.path.join(dir_name, "scores-dev", "t_scores"), "r" ).readlines() ) == 101 ) assert ( len( open( os.path.join(dir_name, "scores-dev", "zt_scores"), "r" ).readlines() ) == 101 ) assert ( len( open( os.path.join(dir_name, "scores-dev", "s_scores"), "r" ).readlines() ) == 101 ) """ else: assert len(raw_scores) == 10 assert len(z_scores) == 10
from sklearn.pipeline import make_pipeline from bob.bio.base.algorithm import Distance from bob.bio.base.pipelines import PipelineSimple from bob.bio.spear.extractor import SpeechbrainEmbeddings # from bob.bio.spear.transformer import Resample from bob.pipelines import wrap transformer_pipeline = make_pipeline( # wrap(["sample"], Resample(target_sample_rate=16000)), wrap(["sample"], SpeechbrainEmbeddings()), ) pipeline = PipelineSimple(transformer_pipeline, Distance(average_on_enroll=True))