quantization.hpp 10.9 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
25
26
#include "Utils/cgognStream.h"

Sylvain Thery's avatar
Sylvain Thery committed
27
#include <cmath>
Pierre Kraemer's avatar
Pierre Kraemer committed
28
29
#include <limits>

Sylvain Thery's avatar
Sylvain Thery committed
30

Pierre Kraemer's avatar
Pierre Kraemer committed
31
32
33
namespace CGoGN
{

Sylvain Thery's avatar
Sylvain Thery committed
34
namespace Utils
Pierre Kraemer's avatar
Pierre Kraemer committed
35
36
{

37
38
typedef unsigned int uint32;

Pierre Kraemer's avatar
Pierre Kraemer committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
template <typename VEC>
void zero(VEC& v)
{
	for(unsigned int i = 0; i < v.dimension(); ++i)
		v[i] = typename VEC::DATA_TYPE(0) ;
}

template <typename VEC>
void set(VEC& v, typename VEC::DATA_TYPE val)
{
	for(unsigned int i = 0; i < v.dimension(); ++i)
		v[i] = val ;
}


template <typename VEC>
Quantization<VEC>::Quantization(const std::vector<VEC>& source) : sourceVectors(source)
{
	associatedCodeVectors.resize(sourceVectors.size()) ;
	nbCodeVectors = 0 ;
	computeMeanSourceVector() ;
	computeDifferentialEntropy() ;
}

template <typename VEC>
void Quantization<VEC>::computeMeanSourceVector()
{
	zero<VEC>(meanSourceVector) ;
	for(unsigned int i = 0; i < sourceVectors.size(); i++)
		meanSourceVector += sourceVectors[i] ;
	meanSourceVector /= sourceVectors.size() ;
}

// Nearest neighbour search : naive algorithm
template <typename VEC>
typename Quantization<VEC>::CodeVectorID Quantization<VEC>::nearestNeighbour(int v)
{
76
	typedef typename VEC::DATA_TYPE REAL;
Pierre Kraemer's avatar
Pierre Kraemer committed
77
	VEC x = sourceVectors[v] ;
78
	REAL dist_min = std::numeric_limits<REAL>::max();
Pierre Kraemer's avatar
Pierre Kraemer committed
79
80
81
82
83
	CodeVectorID nearest = codeVectors.begin() ;
	/* search minimum of squared length between v and each vector of codebook */
	for(CodeVectorID cv = codeVectors.begin(); cv != codeVectors.end() ; ++cv)
	{
		VEC vec = x - cv->v ;
84
		REAL l = vec.norm2();
Pierre Kraemer's avatar
Pierre Kraemer committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
		if(l < dist_min)
		{
			dist_min = l ;
			nearest = cv ;
		}
	}
	return nearest ;
}

template <typename VEC>
void Quantization<VEC>::algoLloydMax()
{
	unsigned int nbLloydIt = 0 ;
	bool finished = false ;
	do
	{
		++nbLloydIt ;

		VEC z ;
		zero<VEC>(z) ;

		// initialize the codeVectors region properties
		for(CodeVectorID cv = codeVectors.begin(); cv != codeVectors.end(); ++cv)
		{
			cv->regionNbVectors = 0 ;
			cv->regionVectorsSum = z ;
			cv->regionDistortion = 0.0f ;
		}

		// For each sourceVector, find its nearest neighbour among the current codeVectors
		// and update nbVectors and distortion associated to each codeVector.
		// In the same time, compute for each codeVector the sum of the sourceVectors of its region
		// (needed if one has to update the positions of the codeVectors)
		for(unsigned int i = 0; i < sourceVectors.size(); ++i)
		{
			CodeVectorID cv = nearestNeighbour(i) ;
			associatedCodeVectors[i] = cv ;
			cv->regionNbVectors += 1 ;
			VEC vec = sourceVectors[i] - cv->v ;
			cv->regionDistortion += vec.norm2() ;
			cv->regionVectorsSum += sourceVectors[i] ;
		}

		float oldDistortion = distortion ;
		distortion = 0.0f ;

		// update the distortion associated to each codeVector
		// and compute the total distortion
		for(CodeVectorID cv = codeVectors.begin(); cv != codeVectors.end(); ++cv)
		{
			if(cv->regionNbVectors > 0)
				distortion += cv->regionDistortion ;
			else
				codeVectors.erase(cv) ;
		}
		distortion /= sourceVectors.size() ;

		if((oldDistortion - distortion) / oldDistortion < epsilonDistortion)
			finished = true ;

		if(!finished)
		{
			// update the codeVectors as the average of the sourceVectors of its region
			for(CodeVectorID cv = codeVectors.begin(); cv != codeVectors.end(); ++cv)
				cv->v = cv->regionVectorsSum / typename VEC::DATA_TYPE(cv->regionNbVectors) ;
		}
	}
	while(!finished) ;

	// sort the codeVectors by ascending distortion
155
	CGoGNout << "nbLloydIt -> " << nbLloydIt << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	codeVectors.sort() ;
}

// Scalar quantization
//template <typename VEC>
//void Quantization<VEC>::scalarQuantization(unsigned int nbCodeVectors, std::vector<VEC>& result)
//{
//	// Initial codebook
//	for(unsigned int i = 0; i < nbCodeVectors; ++i)
//		codeVectors.push_back(sourceVectors[i]) ;
//
//	// compute the average distortion
//	for(unsigned int i = 0; i < sourceVectors.size(); ++i)
//	{
//		VEC vec = sourceVectors[i] - nearestNeighbour(i) ;
//		distortion += gmtl::lengthSquared(vec) ;
//	}
//	distortion /= (sourceVectors.size() * VEC::Size);
//
//	// Lloyd Iteration
//	algoLloydMax() ;
//
//	for(unsigned int i = 0; i < sourceVectors.size() ; ++i)
//		result.push_back(codeVectors[associatedCodeVectors[i]]) ;
//
//	computeDiscreteEntropy() ;
//}

template <typename VEC>
void Quantization<VEC>::vectorQuantizationInit()
{
	codeVectors.clear() ;
	nbCodeVectors = 0 ;
	distortion = 0.0f ;
	// compute the average distortion
	for(unsigned int i = 0; i < sourceVectors.size(); ++i)
	{
		VEC vec = sourceVectors[i] - meanSourceVector ;
		distortion += vec.norm2() ;
	}
	distortion /= sourceVectors.size() ;

	CodeVector<VEC> mcv ;
	mcv.v = meanSourceVector ;
200
	mcv.regionNbVectors = uint32(sourceVectors.size()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
201
202
203
204
205
206
207
208
209
210
211
212
	mcv.regionDistortion = distortion ;
	codeVectors.push_back(mcv) ;
	++nbCodeVectors ;
}

// Vectorial quantization with size of codebook as ending case
template <typename VEC>
void Quantization<VEC>::vectorQuantizationNbRegions(unsigned int nbRegions, std::vector<VEC>& result)
{
	vectorQuantizationInit() ;

	// do not want to have more codeVectors than sourceVectors
213
	nbRegions = nbRegions > uint32(sourceVectors.size()) ? uint32(sourceVectors.size()) : nbRegions;
Pierre Kraemer's avatar
Pierre Kraemer committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

	VEC eps ;
	set<VEC>(eps, epsSplitVector) ;
	while(nbCodeVectors < nbRegions)
	{
		unsigned int nbNewCV = nbCodeVectors / 3 + 1 ;
		if(nbCodeVectors + nbNewCV > nbRegions)
			nbNewCV = nbRegions - nbCodeVectors ;
		CodeVectorID lastOldCV = --codeVectors.end() ;
		CodeVectorID cv = codeVectors.begin() ;
		unsigned int nbAddedCV = 0 ;
		do
		{
			if(cv->regionNbVectors > 1)
			{
				CodeVector<VEC> newCV = (*cv) ;
				newCV.v -= eps ;
				cv->v += eps ;
				codeVectors.push_back(newCV) ;
				++nbCodeVectors ;
				++nbAddedCV ;
			}
			++cv ;
		} while(nbAddedCV < nbNewCV && cv != lastOldCV) ;

		// Lloyd Iteration
		algoLloydMax() ;
	}

	result.resize(sourceVectors.size()) ;
	for(unsigned int i = 0; i < sourceVectors.size() ; ++i)
		result[i] = associatedCodeVectors[i]->v ;

	computeDiscreteEntropy() ;
}

// Vectorial quantization with distortion as ending case
template <typename VEC>
void Quantization<VEC>::vectorQuantizationDistortion(float distortionGoal, std::vector<VEC>& result)
{
	vectorQuantizationInit() ;

	VEC eps ;
	set<VEC>(eps, epsSplitVector) ;
	while(distortion > distortionGoal)
	{
		unsigned int nbNewCV = nbCodeVectors / 3 + 1 ;
		if(nbCodeVectors + nbNewCV > sourceVectors.size())
			nbNewCV = sourceVectors.size() - nbCodeVectors ;
		CodeVectorID cv = codeVectors.begin() ;
		for(unsigned int i = 0; i < nbNewCV; ++i)
		{
			if(cv->regionNbVectors > 1)
			{
				CodeVector<VEC> newCV = (*cv) ;
				newCV.v -= eps ;
				cv->v += eps ;
				codeVectors.push_back(newCV) ;
				++nbCodeVectors ;
			}
			++cv ;
		}
		// Lloyd Iteration
		algoLloydMax() ;
	}

	result.resize(sourceVectors.size()) ;
	for(unsigned int i = 0; i < sourceVectors.size() ; ++i)
		result[i] = associatedCodeVectors[i]->v ;

	computeDiscreteEntropy() ;
}

287
inline float log2(float x)
Pierre Kraemer's avatar
Pierre Kraemer committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
{
    return log(x) / log(2.0f) ;
}

template <typename VEC>
void Quantization<VEC>::computeDiscreteEntropy()
{
	discreteEntropy = 0.0f ;
	for(CodeVectorID cv = codeVectors.begin(); cv != codeVectors.end(); ++cv)
	{
		float p = float(cv->regionNbVectors) / float(sourceVectors.size()) ;
		discreteEntropy += -1.0f * p * log2(p) ;
	}
}

template <typename VEC>
void Quantization<VEC>::computeDifferentialEntropy()
{
306
	typedef typename VEC::DATA_TYPE REAL;
Pierre Kraemer's avatar
Pierre Kraemer committed
307
308
309
310
311
312
313
314
315
316
317
318
319
	if(VEC::DIMENSION == 1) // unidimensional case
	{
		float variance = 0 ;
		// variance computation
		for(unsigned int i = 0; i < sourceVectors.size(); i++)
		{
			VEC vec = sourceVectors[i] - meanSourceVector ;
			variance += vec.norm2() ;
		}
		variance /= sourceVectors.size() ;

		determinantSigma = variance ;
		traceSigma = variance ;
320
		differentialEntropy = std::log(REAL(variance * 2 * M_PI * std::exp(1.0f))) / 2.0f ;
Pierre Kraemer's avatar
Pierre Kraemer committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
	}
	else // 2D or 3D case
	{
		float* matrice = new float[VEC::DIMENSION * VEC::DIMENSION] ; // covariance matrix
		float trace = 0.0f ;

		// covariance matrix computation
		for(unsigned int i = 0; i < VEC::DIMENSION; i++)
		{
			for(unsigned int j = i; j < VEC::DIMENSION; j++)
			{
				float cov = covariance(i,j) ;
				matrice[i*VEC::DIMENSION + j] = cov ;
				if(i!=j)
					matrice[j*VEC::DIMENSION + i] = cov ;
				else
					trace += cov ;
			}
		}

		determinantSigma = determinant(matrice) ;
		traceSigma = trace ;
343
		differentialEntropy = REAL(std::log(std::pow(2.0*M_PI*std::exp(1.0), VEC::DIMENSION)* determinantSigma) / 2.0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

		delete[] matrice ;
	}
}

template <typename VEC>
float Quantization<VEC>::covariance(int x, int y)
{
	float cov = 0.0f ;
	for(unsigned int i = 0; i < sourceVectors.size(); i++)
	{
		VEC v = sourceVectors[i] ;
		float vx = v[x] - meanSourceVector[x] ;
		float vy = v[y] - meanSourceVector[y] ;
		cov += vx * vy ;
	}
	cov /= sourceVectors.size() ;
	return cov ;
}

template <typename VEC>
float Quantization<VEC>::determinant(float* matrice)
{
	float a = matrice[0] ;
	float b = matrice[1] ;
	float c = matrice[2] ;
	float d = matrice[3] ;

	if(VEC::DIMENSION == 2)
		return a*d - b*c ;
	else // if in tridimensional case
	{
		float e = matrice[4] ;
		float f = matrice[5] ;
		float g = matrice[6] ;
		float h = matrice[7] ;
		float i = matrice[8] ;

		return (a*e*i + d*h*c + g*b*f) - (g*e*c + a*h*f + d*b*i) ;
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
386
} //namespace Utils
Pierre Kraemer's avatar
Pierre Kraemer committed
387
388

} //namespace CGoGN