mi@0
|
1 """
|
mi@0
|
2 C-NMF method for segmentation, modified from here:
|
mi@0
|
3
|
mi@0
|
4 Nieto, O., Jehan, T., Convex Non-negative Matrix Factorization For Automatic
|
mi@0
|
5 Music Structure Identification. Proc. of the 38th IEEE International Conference
|
mi@0
|
6 on Acoustics, Speech, and Signal Processing (ICASSP). Vancouver, Canada, 2013.
|
mi@0
|
7 """
|
mi@0
|
8
|
mi@0
|
9 __author__ = "Oriol Nieto"
|
mi@0
|
10 __copyright__ = "Copyright 2014, Music and Audio Research Lab (MARL)"
|
mi@0
|
11 __license__ = "GPL"
|
mi@0
|
12 __version__ = "1.0"
|
mi@0
|
13 __email__ = "oriol@nyu.edu"
|
mi@0
|
14
|
mi@0
|
15 import numpy as np
|
mi@0
|
16 import pymf
|
mi@0
|
17
|
mi@0
|
18 # Local stuff
|
mi@0
|
19 from utils import SegUtil
|
mi@0
|
20
|
mi@0
|
21
|
mi@0
|
22 def cnmf(S, rank, niter=500):
|
mi@0
|
23 """(Convex) Non-Negative Matrix Factorization.
|
mi@0
|
24
|
mi@0
|
25 Parameters
|
mi@0
|
26 ----------
|
mi@0
|
27 S: np.array(p, N)
|
mi@0
|
28 Features matrix. p row features and N column observations.
|
mi@0
|
29 rank: int
|
mi@0
|
30 Rank of decomposition
|
mi@0
|
31 niter: int
|
mi@0
|
32 Number of iterations to be used
|
mi@0
|
33
|
mi@0
|
34 Returns
|
mi@0
|
35 -------
|
mi@0
|
36 F: np.array
|
mi@0
|
37 Cluster matrix (decomposed matrix)
|
mi@0
|
38 G: np.array
|
mi@0
|
39 Activation matrix (decomposed matrix)
|
mi@0
|
40 (s.t. S ~= F * G)
|
mi@0
|
41 """
|
mi@0
|
42 nmf_mdl = pymf.CNMF(S, num_bases=rank)
|
mi@0
|
43 nmf_mdl.factorize(niter=niter)
|
mi@0
|
44 F = np.asarray(nmf_mdl.W)
|
mi@0
|
45 G = np.asarray(nmf_mdl.H)
|
mi@0
|
46 return F, G
|
mi@0
|
47
|
mi@0
|
48
|
mi@0
|
49 def most_frequent(x):
|
mi@0
|
50 """Returns the most frequent value in x."""
|
mi@0
|
51 return np.argmax(np.bincount(x))
|
mi@0
|
52
|
mi@0
|
53
|
mi@0
|
54 def compute_labels(X, rank, R, bound_idxs, niter=300):
|
mi@0
|
55 """Computes the labels using the bounds."""
|
mi@0
|
56
|
mi@0
|
57 X = X.T
|
mi@0
|
58 try:
|
mi@0
|
59 F, G = cnmf(X, rank, niter=niter)
|
mi@0
|
60 except:
|
mi@0
|
61 return [1]
|
mi@0
|
62
|
mi@0
|
63 label_frames = filter_activation_matrix(G.T, R)
|
mi@0
|
64 label_frames = np.asarray(label_frames, dtype=int)
|
mi@0
|
65
|
mi@0
|
66 # Get labels from the label frames
|
mi@0
|
67 labels = []
|
mi@0
|
68 bound_inters = zip(bound_idxs[:-1], bound_idxs[1:])
|
mi@0
|
69 for bound_inter in bound_inters:
|
mi@0
|
70 if bound_inter[1] - bound_inter[0] <= 0:
|
mi@0
|
71 labels.append(np.max(label_frames) + 1)
|
mi@0
|
72 else:
|
mi@0
|
73 labels.append(most_frequent(
|
mi@0
|
74 label_frames[bound_inter[0]:bound_inter[1]]))
|
mi@0
|
75
|
mi@0
|
76 return labels
|
mi@0
|
77
|
mi@0
|
78
|
mi@0
|
79 def filter_activation_matrix(G, R):
|
mi@0
|
80 """Filters the activation matrix G, and returns a flattened copy."""
|
mi@0
|
81 idx = np.argmax(G, axis=1)
|
mi@0
|
82 max_idx = np.arange(G.shape[0])
|
mi@0
|
83 max_idx = (max_idx, idx.flatten())
|
mi@0
|
84 G[:, :] = 0
|
mi@0
|
85 G[max_idx] = idx + 1
|
mi@0
|
86 G = np.sum(G, axis=1)
|
mi@0
|
87 G = utils.median_filter(G[:, np.newaxis], R)
|
mi@0
|
88 return G.flatten()
|
mi@0
|
89
|
mi@0
|
90
|
mi@0
|
91 def segmentation(X, rank, R, h, niter=300):
|
mi@0
|
92 """
|
mi@0
|
93 Gets the segmentation (boundaries and labels) from the factorization
|
mi@0
|
94 matrices.
|
mi@0
|
95
|
mi@0
|
96 Parameters
|
mi@0
|
97 ----------
|
mi@0
|
98 X: np.array()
|
mi@0
|
99 Features matrix (e.g. chromagram)
|
mi@0
|
100 rank: int
|
mi@0
|
101 Rank of decomposition
|
mi@0
|
102 R: int
|
mi@0
|
103 Size of the median filter for activation matrix
|
mi@0
|
104 niter: int
|
mi@0
|
105 Number of iterations for k-means
|
mi@0
|
106 bound_idxs : list
|
mi@0
|
107 Use previously found boundaries (None to detect them)
|
mi@0
|
108
|
mi@0
|
109 Returns
|
mi@0
|
110 -------
|
mi@0
|
111 bounds_idx: np.array
|
mi@0
|
112 Bound indeces found
|
mi@0
|
113 labels: np.array
|
mi@0
|
114 Indeces of the labels representing the similarity between segments.
|
mi@0
|
115 """
|
mi@0
|
116
|
mi@0
|
117 # Filter
|
mi@0
|
118 X = utils.median_filter(X, M=h)
|
mi@0
|
119 X = X.T
|
mi@0
|
120
|
mi@0
|
121 # Find non filtered boundaries
|
mi@0
|
122 bound_idxs = None
|
mi@0
|
123 while True:
|
mi@0
|
124 if bound_idxs is None:
|
mi@0
|
125 try:
|
mi@0
|
126 F, G = cnmf(X, rank, niter=niter)
|
mi@0
|
127 except:
|
mi@0
|
128 return np.empty(0), [1]
|
mi@0
|
129
|
mi@0
|
130 # Filter G
|
mi@0
|
131 G = filter_activation_matrix(G.T, R)
|
mi@0
|
132 if bound_idxs is None:
|
mi@0
|
133 bound_idxs = np.where(np.diff(G) != 0)[0] + 1
|
mi@0
|
134
|
mi@0
|
135 if len(np.unique(bound_idxs)) <= 2:
|
mi@0
|
136 rank += 1
|
mi@0
|
137 bound_idxs = None
|
mi@0
|
138 else:
|
mi@0
|
139 break
|
mi@0
|
140
|
mi@0
|
141 return bound_idxs
|