Coverage for src/bob/bio/spear/config/pipeline/mfcc60_ivector.py: 90%

21 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-06 22:04 +0100

1#!/usr/bin/env python 

2# @author: Yannick Dayer <yannick.dayer@idiap.ch> 

3# @date: Wed 24 Aug 2022 18:56:07 UTC+02 

4 

5"""Creates a biometric PipelineSimple for the `bob bio pipeline simple` command. 

6 

7This pipeline is composed of the following steps: 

8 - annotator: Energy_2Gauss (VAD on 2 Gaussians) 

9 - extractor: Cepstral (MFCC, 60 features) 

10 - projector: IVector 

11 - algorithm: Distance 

12""" 

13 

14import numpy 

15 

16from sklearn.base import BaseEstimator 

17from sklearn.pipeline import Pipeline 

18 

19from bob.bio.base.algorithm import GMM, Distance 

20from bob.bio.base.pipelines import PipelineSimple 

21from bob.bio.spear.annotator import Energy_2Gauss 

22from bob.bio.spear.extractor import Cepstral 

23from bob.learn.em import IVectorMachine, KMeansMachine 

24from bob.pipelines import wrap 

25 

26# Number of Gaussians for the UBM (used by kmeans and GMM) 

27n_gaussians = 256 

28 

29# Kmeans machine used for GMM initialization 

30kmeans_trainer = KMeansMachine( 

31 n_clusters=n_gaussians, 

32 max_iter=25, 

33 convergence_threshold=1e-6, 

34 init_max_iter=5, 

35 oversampling_factor=64, 

36) 

37 

38# GMM used as transformer 

39ubm = GMM( 

40 n_gaussians=n_gaussians, 

41 max_fitting_steps=25, 

42 convergence_threshold=1e-6, 

43 k_means_trainer=kmeans_trainer, 

44 random_state=2, 

45 return_stats_in_transform=True, 

46) 

47 

48ivector_transformer = IVectorMachine(ubm=ubm, dim_t=200, max_iterations=8) 

49 

50 

51class LenghtNorm(BaseEstimator): 

52 def transform(self, X): 

53 return [x / numpy.linalg.norm(x) for x in X] 

54 

55 def _more_tags(self): 

56 return {"requires_fit": False} 

57 

58 

59# Transformer part of PipelineSimple 

60transformer = Pipeline( 

61 [ 

62 ("annotator", wrap(["sample"], Energy_2Gauss())), 

63 ("extractor", wrap(["sample"], Cepstral())), 

64 ("ubm", wrap(["sample"], ubm)), 

65 ("ivector", wrap(["sample"], ivector_transformer)), 

66 ] 

67) 

68 

69 

70# PipelineSimple instance used by `execute_pipeline_simple` or the `pipeline simple` command 

71pipeline = PipelineSimple(transformer, Distance())