Coverage for src/bob/bio/spear/config/pipeline/mfcc60_ivector_postprocess.py: 0%

23 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.discriminant_analysis import LinearDiscriminantAnalysis 

18from sklearn.pipeline import Pipeline 

19 

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

21from bob.bio.base.pipelines import PipelineSimple 

22from bob.bio.spear.annotator import Energy_2Gauss 

23from bob.bio.spear.extractor import Cepstral 

24from bob.bio.spear.transformer import ReferenceIdEncoder 

25from bob.learn.em import WCCN, IVectorMachine, KMeansMachine, Whitening 

26from bob.pipelines import wrap 

27 

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

29n_gaussians = 256 

30 

31# Kmeans machine used for GMM initialization 

32kmeans_trainer = KMeansMachine( 

33 n_clusters=n_gaussians, 

34 max_iter=25, 

35 convergence_threshold=1e-6, 

36 init_max_iter=5, 

37 oversampling_factor=64, 

38) 

39 

40# GMM used as transformer 

41ubm = GMM( 

42 n_gaussians=n_gaussians, 

43 max_fitting_steps=25, 

44 convergence_threshold=1e-6, 

45 k_means_trainer=kmeans_trainer, 

46 random_state=2, 

47 return_stats_in_transform=True, 

48) 

49 

50ivector_transformer = IVectorMachine(ubm=ubm, dim_t=200, max_iterations=4) 

51 

52 

53class LengthNorm(BaseEstimator): 

54 def transform(self, X): 

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

56 

57 def _more_tags(self): 

58 return {"requires_fit": False} 

59 

60 

61# Transformer part of PipelineSimple 

62transformer = Pipeline( 

63 [ 

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

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

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

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

68 ("whitening", wrap(["sample"], Whitening())), 

69 ("length-norm", wrap(["sample"], LengthNorm())), 

70 ("reference_id_encoder", wrap(["sample"], ReferenceIdEncoder())), 

71 ( 

72 "lda", 

73 wrap( 

74 ["sample"], 

75 LinearDiscriminantAnalysis(), 

76 fit_extra_arguments=[("y", "reference_id_int")], 

77 ), 

78 ), 

79 ( 

80 "wccn", 

81 wrap( 

82 ["sample"], 

83 WCCN(), 

84 fit_extra_arguments=[("y", "reference_id_int")], 

85 ), 

86 ), 

87 ] 

88) 

89 

90 

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

92pipeline = PipelineSimple(transformer, Distance())