Blame view

complexnn/fft.py 5.45 KB
8a1d43c41   Parcollet Titouan   V1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
76
77
78
79
80
81
82
83
84
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  #
  # Authors: Olexa Bilaniuk
  #
  
  import keras.backend                        as KB
  import keras.engine                         as KE
  import keras.layers                         as KL
  import keras.optimizers                     as KO
  import theano                               as T
  import theano.ifelse                        as TI
  import theano.tensor                        as TT
  import theano.tensor.fft                    as TTF
  import numpy                                as np
  
  
  #
  # FFT functions:
  #
  #  fft():   Batched 1-D FFT  (Input: (Batch, TimeSamples))
  #  ifft():  Batched 1-D IFFT (Input: (Batch, FreqSamples))
  #  fft2():  Batched 2-D FFT  (Input: (Batch, TimeSamplesH, TimeSamplesW))
  #  ifft2(): Batched 2-D IFFT (Input: (Batch, FreqSamplesH, FreqSamplesW))
  #
  
  def fft(z):
  	B      = z.shape[0]//2
  	L      = z.shape[1]
  	C      = TT.as_tensor_variable(np.asarray([[[1,-1]]], dtype=T.config.floatX))
  	Zr, Zi = TTF.rfft(z[:B], norm="ortho"), TTF.rfft(z[B:], norm="ortho")
  	isOdd  = TT.eq(L%2, 1)
  	Zr     = TI.ifelse(isOdd, TT.concatenate([Zr, C*Zr[:,1:  ][:,::-1]], axis=1),
  	                          TT.concatenate([Zr, C*Zr[:,1:-1][:,::-1]], axis=1))
  	Zi     = TI.ifelse(isOdd, TT.concatenate([Zi, C*Zi[:,1:  ][:,::-1]], axis=1),
  	                          TT.concatenate([Zi, C*Zi[:,1:-1][:,::-1]], axis=1))
  	Zi     = (C*Zi)[:,:,::-1]  # Zi * i
  	Z      = Zr+Zi
  	return TT.concatenate([Z[:,:,0], Z[:,:,1]], axis=0)
  def ifft(z):
  	B      = z.shape[0]//2
  	L      = z.shape[1]
  	C      = TT.as_tensor_variable(np.asarray([[[1,-1]]], dtype=T.config.floatX))
  	Zr, Zi = TTF.rfft(z[:B], norm="ortho"), TTF.rfft(z[B:]*-1, norm="ortho")
  	isOdd  = TT.eq(L%2, 1)
  	Zr     = TI.ifelse(isOdd, TT.concatenate([Zr, C*Zr[:,1:  ][:,::-1]], axis=1),
  	                          TT.concatenate([Zr, C*Zr[:,1:-1][:,::-1]], axis=1))
  	Zi     = TI.ifelse(isOdd, TT.concatenate([Zi, C*Zi[:,1:  ][:,::-1]], axis=1),
  	                          TT.concatenate([Zi, C*Zi[:,1:-1][:,::-1]], axis=1))
  	Zi     = (C*Zi)[:,:,::-1]  # Zi * i
  	Z      = Zr+Zi
  	return TT.concatenate([Z[:,:,0], Z[:,:,1]*-1], axis=0)
  def fft2(x):
  	tt = x
  	tt = KB.reshape(tt, (x.shape[0] *x.shape[1], x.shape[2]))
  	tf = fft(tt)
  	tf = KB.reshape(tf, (x.shape[0], x.shape[1], x.shape[2]))
  	tf = KB.permute_dimensions(tf, (0, 2, 1))
  	tf = KB.reshape(tf, (x.shape[0] *x.shape[2], x.shape[1]))
  	ff = fft(tf)
  	ff = KB.reshape(ff, (x.shape[0], x.shape[2], x.shape[1]))
  	ff = KB.permute_dimensions(ff, (0, 2, 1))
  	return ff
  def ifft2(x):
  	ff = x
  	ff = KB.permute_dimensions(ff, (0, 2, 1))
  	ff = KB.reshape(ff, (x.shape[0] *x.shape[2], x.shape[1]))
  	tf = ifft(ff)
  	tf = KB.reshape(tf, (x.shape[0], x.shape[2], x.shape[1]))
  	tf = KB.permute_dimensions(tf, (0, 2, 1))
  	tf = KB.reshape(tf, (x.shape[0] *x.shape[1], x.shape[2]))
  	tt = ifft(tf)
  	tt = KB.reshape(tt, (x.shape[0], x.shape[1], x.shape[2]))
  	return tt
  
  #
  # FFT Layers:
  #
  #  FFT:   Batched 1-D FFT  (Input: (Batch, FeatureMaps, TimeSamples))
  #  IFFT:  Batched 1-D IFFT (Input: (Batch, FeatureMaps, FreqSamples))
  #  FFT2:  Batched 2-D FFT  (Input: (Batch, FeatureMaps, TimeSamplesH, TimeSamplesW))
  #  IFFT2: Batched 2-D IFFT (Input: (Batch, FeatureMaps, FreqSamplesH, FreqSamplesW))
  #
  
  class FFT(KL.Layer):
  	def call(self, x, mask=None):
  		a = KB.permute_dimensions(x, (1,0,2))
  		a = KB.reshape(a, (x.shape[1] *x.shape[0], x.shape[2]))
  		a = fft(a)
  		a = KB.reshape(a, (x.shape[1], x.shape[0], x.shape[2]))
  		return KB.permute_dimensions(a, (1,0,2))
  class IFFT(KL.Layer):
  	def call(self, x, mask=None):
  		a = KB.permute_dimensions(x, (1,0,2))
  		a = KB.reshape(a, (x.shape[1] *x.shape[0], x.shape[2]))
  		a = ifft(a)
  		a = KB.reshape(a, (x.shape[1], x.shape[0], x.shape[2]))
  		return KB.permute_dimensions(a, (1,0,2))
  class FFT2(KL.Layer):
  	def call(self, x, mask=None):
  		a = KB.permute_dimensions(x, (1,0,2,3))
  		a = KB.reshape(a, (x.shape[1] *x.shape[0], x.shape[2], x.shape[3]))
  		a = fft2(a)
  		a = KB.reshape(a, (x.shape[1], x.shape[0], x.shape[2], x.shape[3]))
  		return KB.permute_dimensions(a, (1,0,2,3))
  class IFFT2(KL.Layer):
  	def call(self, x, mask=None):
  		a = KB.permute_dimensions(x, (1,0,2,3))
  		a = KB.reshape(a, (x.shape[1] *x.shape[0], x.shape[2], x.shape[3]))
  		a = ifft2(a)
  		a = KB.reshape(a, (x.shape[1], x.shape[0], x.shape[2], x.shape[3]))
  		return KB.permute_dimensions(a, (1,0,2,3))
  
  
  
  #
  # Tests
  #
  # Note: The IFFT is the conjugate of the FFT of the conjugate.
  #
  #     np.fft.ifft(x) == np.conj(np.fft.fft(np.conj(x)))
  #
  
  if __name__ == "__main__":
  	# Numpy
  	np.random.seed(1)
  	L   = 19
  	r   = np.random.normal(0.8, size=(L,))
  	i   = np.random.normal(0.8, size=(L,))
  	x   = r+i*1j
  	R   = np.fft.rfft(r, norm="ortho")
  	I   = np.fft.rfft(i, norm="ortho")
  	X   = np.fft.fft (x, norm="ortho")
  	
  	if L&1:
  		R   = np.concatenate([R, np.conj(R[1:  ][::-1])])
  		I   = np.concatenate([I, np.conj(I[1:  ][::-1])])
  	else:
  		R   = np.concatenate([R, np.conj(R[1:-1][::-1])])
  		I   = np.concatenate([I, np.conj(I[1:-1][::-1])])
  	Y   = R+I*1j
  	print np.allclose(X, Y)
  	
  	
  	# Theano
  	z   = TT.dmatrix()
  	f   = T.function([z], ifft(fft(z)))
  	v   = np.concatenate([np.real(x)[np.newaxis,:], np.imag(x)[np.newaxis,:]], axis=0)
  	print v
  	print f(v)
  	print np.allclose(v, f(v))
  	
  	
  	# Keras
  	x = i = KL.Input(shape=(128, 32,32))
  	x = IFFT2()(x)
  	model = KE.Model([i],[x])
  	
  	loss  = "mse"
  	opt   = KO.Adam()
  	
  	model.compile(opt, loss)
  	model._make_train_function()
  	model._make_predict_function()
  	model._make_test_function()
  	
  	v = np.random.normal(size=(13,128,32,32))
  	#print v
  	V = model.predict(v)
  	#print V
  	print V.shape