2222# Dependency imports
2323
2424import numpy as np
25+ import tensorflow as tf
2526
26- from tensorflow .python .framework import dtypes
27- from tensorflow .python .ops import array_ops
28- from tensorflow .python .ops import math_ops
29- from tensorflow .python .ops import variables
3027from tensorflow .python .platform import test
31- from tensorflow .python .training import gradient_descent
3228
33- from tensorflow_compression . python . layers import entropy_models
29+ import tensorflow_compression as tfc
3430
3531
3632class EntropyBottleneckTest (test .TestCase ):
3733
3834 def test_noise (self ):
3935 # Tests that the noise added is uniform noise between -0.5 and 0.5.
40- inputs = array_ops .placeholder (dtypes .float32 , (None , 1 ))
41- layer = entropy_models .EntropyBottleneck ()
36+ inputs = tf .placeholder (tf .float32 , (None , 1 ))
37+ layer = tfc .EntropyBottleneck ()
4238 noisy , _ = layer (inputs , training = True )
4339 with self .test_session () as sess :
44- sess .run (variables .global_variables_initializer ())
40+ sess .run (tf .global_variables_initializer ())
4541 values = np .linspace (- 50 , 50 , 100 )[:, None ]
4642 noisy , = sess .run ([noisy ], {inputs : values })
4743 self .assertFalse (np .allclose (values , noisy , rtol = 0 , atol = .49 ))
@@ -50,14 +46,14 @@ def test_noise(self):
5046 def test_quantization (self ):
5147 # Tests that inputs are quantized to full integer values, even after
5248 # quantiles have been updated.
53- inputs = array_ops .placeholder (dtypes .float32 , (None , 1 ))
54- layer = entropy_models .EntropyBottleneck (optimize_integer_offset = False )
49+ inputs = tf .placeholder (tf .float32 , (None , 1 ))
50+ layer = tfc .EntropyBottleneck (optimize_integer_offset = False )
5551 quantized , _ = layer (inputs , training = False )
56- opt = gradient_descent .GradientDescentOptimizer (learning_rate = 1 )
52+ opt = tf . train .GradientDescentOptimizer (learning_rate = 1 )
5753 self .assertTrue (len (layer .losses ) == 1 )
5854 step = opt .minimize (layer .losses [0 ])
5955 with self .test_session () as sess :
60- sess .run (variables .global_variables_initializer ())
56+ sess .run (tf .global_variables_initializer ())
6157 sess .run (step )
6258 values = np .linspace (- 50 , 50 , 100 )[:, None ]
6359 quantized , = sess .run ([quantized ], {inputs : values })
@@ -67,14 +63,14 @@ def test_quantization_optimized_offset(self):
6763 # Tests that inputs are not quantized to full integer values after quantiles
6864 # have been updated. However, the difference between input and output should
6965 # be between -0.5 and 0.5, and the offset must be consistent.
70- inputs = array_ops .placeholder (dtypes .float32 , (None , 1 ))
71- layer = entropy_models .EntropyBottleneck (optimize_integer_offset = True )
66+ inputs = tf .placeholder (tf .float32 , (None , 1 ))
67+ layer = tfc .EntropyBottleneck (optimize_integer_offset = True )
7268 quantized , _ = layer (inputs , training = False )
73- opt = gradient_descent .GradientDescentOptimizer (learning_rate = 1 )
69+ opt = tf . train .GradientDescentOptimizer (learning_rate = 1 )
7470 self .assertTrue (len (layer .losses ) == 1 )
7571 step = opt .minimize (layer .losses [0 ])
7672 with self .test_session () as sess :
77- sess .run (variables .global_variables_initializer ())
73+ sess .run (tf .global_variables_initializer ())
7874 sess .run (step )
7975 values = np .linspace (- 50 , 50 , 100 )[:, None ]
8076 quantized , = sess .run ([quantized ], {inputs : values })
@@ -86,17 +82,17 @@ def test_quantization_optimized_offset(self):
8682 def test_codec (self ):
8783 # Tests that inputs are compressed and decompressed correctly, and quantized
8884 # to full integer values, even after quantiles have been updated.
89- inputs = array_ops .placeholder (dtypes .float32 , (1 , None , 1 ))
90- layer = entropy_models .EntropyBottleneck (
85+ inputs = tf .placeholder (tf .float32 , (1 , None , 1 ))
86+ layer = tfc .EntropyBottleneck (
9187 data_format = "channels_last" , init_scale = 60 ,
9288 optimize_integer_offset = False )
9389 bitstrings = layer .compress (inputs )
94- decoded = layer .decompress (bitstrings , array_ops .shape (inputs )[1 :])
95- opt = gradient_descent .GradientDescentOptimizer (learning_rate = 1 )
90+ decoded = layer .decompress (bitstrings , tf .shape (inputs )[1 :])
91+ opt = tf . train .GradientDescentOptimizer (learning_rate = 1 )
9692 self .assertTrue (len (layer .losses ) == 1 )
9793 step = opt .minimize (layer .losses [0 ])
9894 with self .test_session () as sess :
99- sess .run (variables .global_variables_initializer ())
95+ sess .run (tf .global_variables_initializer ())
10096 sess .run (step )
10197 self .assertTrue (len (layer .updates ) == 1 )
10298 sess .run (layer .updates [0 ])
@@ -109,17 +105,17 @@ def test_codec_optimized_offset(self):
109105 # quantized to full integer values after quantiles have been updated.
110106 # However, the difference between input and output should be between -0.5
111107 # and 0.5, and the offset must be consistent.
112- inputs = array_ops .placeholder (dtypes .float32 , (1 , None , 1 ))
113- layer = entropy_models .EntropyBottleneck (
108+ inputs = tf .placeholder (tf .float32 , (1 , None , 1 ))
109+ layer = tfc .EntropyBottleneck (
114110 data_format = "channels_last" , init_scale = 60 ,
115111 optimize_integer_offset = True )
116112 bitstrings = layer .compress (inputs )
117- decoded = layer .decompress (bitstrings , array_ops .shape (inputs )[1 :])
118- opt = gradient_descent .GradientDescentOptimizer (learning_rate = 1 )
113+ decoded = layer .decompress (bitstrings , tf .shape (inputs )[1 :])
114+ opt = tf . train .GradientDescentOptimizer (learning_rate = 1 )
119115 self .assertTrue (len (layer .losses ) == 1 )
120116 step = opt .minimize (layer .losses [0 ])
121117 with self .test_session () as sess :
122- sess .run (variables .global_variables_initializer ())
118+ sess .run (tf .global_variables_initializer ())
123119 sess .run (step )
124120 self .assertTrue (len (layer .updates ) == 1 )
125121 sess .run (layer .updates [0 ])
@@ -133,13 +129,13 @@ def test_codec_optimized_offset(self):
133129 def test_codec_clipping (self ):
134130 # Tests that inputs are compressed and decompressed correctly, and clipped
135131 # to the expected range.
136- inputs = array_ops .placeholder (dtypes .float32 , (1 , None , 1 ))
137- layer = entropy_models .EntropyBottleneck (
132+ inputs = tf .placeholder (tf .float32 , (1 , None , 1 ))
133+ layer = tfc .EntropyBottleneck (
138134 data_format = "channels_last" , init_scale = 40 )
139135 bitstrings = layer .compress (inputs )
140- decoded = layer .decompress (bitstrings , array_ops .shape (inputs )[1 :])
136+ decoded = layer .decompress (bitstrings , tf .shape (inputs )[1 :])
141137 with self .test_session () as sess :
142- sess .run (variables .global_variables_initializer ())
138+ sess .run (tf .global_variables_initializer ())
143139 self .assertTrue (len (layer .updates ) == 1 )
144140 sess .run (layer .updates [0 ])
145141 values = np .linspace (- 50 , 50 , 100 )[None , :, None ]
@@ -150,15 +146,15 @@ def test_codec_clipping(self):
150146 def test_channels_last (self ):
151147 # Test the layer with more than one channel and multiple input dimensions,
152148 # with the channels in the last dimension.
153- inputs = array_ops .placeholder (dtypes .float32 , (None , None , None , 2 ))
154- layer = entropy_models .EntropyBottleneck (
149+ inputs = tf .placeholder (tf .float32 , (None , None , None , 2 ))
150+ layer = tfc .EntropyBottleneck (
155151 data_format = "channels_last" , init_scale = 50 )
156152 noisy , _ = layer (inputs , training = True )
157153 quantized , _ = layer (inputs , training = False )
158154 bitstrings = layer .compress (inputs )
159- decoded = layer .decompress (bitstrings , array_ops .shape (inputs )[1 :])
155+ decoded = layer .decompress (bitstrings , tf .shape (inputs )[1 :])
160156 with self .test_session () as sess :
161- sess .run (variables .global_variables_initializer ())
157+ sess .run (tf .global_variables_initializer ())
162158 self .assertTrue (len (layer .updates ) == 1 )
163159 sess .run (layer .updates [0 ])
164160 values = 5 * np .random .normal (size = (7 , 5 , 3 , 2 ))
@@ -171,15 +167,15 @@ def test_channels_last(self):
171167 def test_channels_first (self ):
172168 # Test the layer with more than one channel and multiple input dimensions,
173169 # with the channel dimension right after the batch dimension.
174- inputs = array_ops .placeholder (dtypes .float32 , (None , 3 , None , None ))
175- layer = entropy_models .EntropyBottleneck (
170+ inputs = tf .placeholder (tf .float32 , (None , 3 , None , None ))
171+ layer = tfc .EntropyBottleneck (
176172 data_format = "channels_first" , init_scale = 50 )
177173 noisy , _ = layer (inputs , training = True )
178174 quantized , _ = layer (inputs , training = False )
179175 bitstrings = layer .compress (inputs )
180- decoded = layer .decompress (bitstrings , array_ops .shape (inputs )[1 :])
176+ decoded = layer .decompress (bitstrings , tf .shape (inputs )[1 :])
181177 with self .test_session () as sess :
182- sess .run (variables .global_variables_initializer ())
178+ sess .run (tf .global_variables_initializer ())
183179 self .assertTrue (len (layer .updates ) == 1 )
184180 sess .run (layer .updates [0 ])
185181 values = 5 * np .random .normal (size = (2 , 3 , 5 , 7 ))
@@ -193,13 +189,13 @@ def test_compress(self):
193189 # Test compression and decompression, and produce test data for
194190 # `test_decompress`. If you set the constant at the end to `True`, this test
195191 # will fail and the log will contain the new test data.
196- inputs = array_ops .placeholder (dtypes .float32 , (2 , 3 , 10 ))
197- layer = entropy_models .EntropyBottleneck (
192+ inputs = tf .placeholder (tf .float32 , (2 , 3 , 10 ))
193+ layer = tfc .EntropyBottleneck (
198194 data_format = "channels_first" , filters = (), init_scale = 2 )
199195 bitstrings = layer .compress (inputs )
200- decoded = layer .decompress (bitstrings , array_ops .shape (inputs )[1 :])
196+ decoded = layer .decompress (bitstrings , tf .shape (inputs )[1 :])
201197 with self .test_session () as sess :
202- sess .run (variables .global_variables_initializer ())
198+ sess .run (tf .global_variables_initializer ())
203199 self .assertTrue (len (layer .updates ) == 1 )
204200 sess .run (layer .updates [0 ])
205201 values = 5 * np .random .uniform (size = (2 , 3 , 10 )) - 2.5
@@ -236,54 +232,54 @@ def test_compress(self):
236232 def test_decompress (self ):
237233 # Test that decompression of values compressed with a previous version
238234 # works, i.e. that the file format doesn't change across revisions.
239- bitstrings = array_ops .placeholder (dtypes .string )
240- input_shape = array_ops .placeholder (dtypes .int32 )
241- quantized_cdf = array_ops .placeholder (dtypes .int32 )
242- layer = entropy_models .EntropyBottleneck (
243- data_format = "channels_first" , filters = (), dtype = dtypes .float32 )
235+ bitstrings = tf .placeholder (tf .string )
236+ input_shape = tf .placeholder (tf .int32 )
237+ quantized_cdf = tf .placeholder (tf .int32 )
238+ layer = tfc .EntropyBottleneck (
239+ data_format = "channels_first" , filters = (), dtype = tf .float32 )
244240 layer .build (self .expected .shape )
245241 layer ._quantized_cdf = quantized_cdf
246242 decoded = layer .decompress (bitstrings , input_shape [1 :])
247243 with self .test_session () as sess :
248- sess .run (variables .global_variables_initializer ())
244+ sess .run (tf .global_variables_initializer ())
249245 decoded , = sess .run ([decoded ], {
250246 bitstrings : self .bitstrings , input_shape : self .expected .shape ,
251247 quantized_cdf : self .quantized_cdf })
252248 self .assertAllClose (self .expected , decoded , rtol = 0 , atol = 1e-6 )
253249
254250 def test_build_decompress (self ):
255251 # Test that layer can be built when `decompress` is the first call to it.
256- bitstrings = array_ops .placeholder (dtypes .string )
257- input_shape = array_ops .placeholder (dtypes .int32 , shape = [3 ])
258- layer = entropy_models .EntropyBottleneck (dtype = dtypes .float32 )
252+ bitstrings = tf .placeholder (tf .string )
253+ input_shape = tf .placeholder (tf .int32 , shape = [3 ])
254+ layer = tfc .EntropyBottleneck (dtype = tf .float32 )
259255 layer .decompress (bitstrings , input_shape [1 :], channels = 5 )
260256 self .assertTrue (layer .built )
261257
262258 def test_pmf_normalization (self ):
263259 # Test that probability mass functions are normalized correctly.
264- layer = entropy_models .EntropyBottleneck (dtype = dtypes .float32 )
260+ layer = tfc .EntropyBottleneck (dtype = tf .float32 )
265261 layer .build ((None , 10 ))
266262 with self .test_session () as sess :
267- sess .run (variables .global_variables_initializer ())
263+ sess .run (tf .global_variables_initializer ())
268264 pmf , = sess .run ([layer ._pmf ])
269265 self .assertAllClose (np .ones (10 ), np .sum (pmf , axis = - 1 ), rtol = 0 , atol = 1e-6 )
270266
271267 def test_visualize (self ):
272268 # Test that summary op can be constructed.
273- layer = entropy_models .EntropyBottleneck (dtype = dtypes .float32 )
269+ layer = tfc .EntropyBottleneck (dtype = tf .float32 )
274270 layer .build ((None , 10 ))
275271 summary = layer .visualize ()
276272 with self .test_session () as sess :
277- sess .run (variables .global_variables_initializer ())
273+ sess .run (tf .global_variables_initializer ())
278274 sess .run ([summary ])
279275
280276 def test_normalization (self ):
281277 # Test that densities are normalized correctly.
282- inputs = array_ops .placeholder (dtypes .float32 , (None , 1 ))
283- layer = entropy_models .EntropyBottleneck (filters = (2 ,))
278+ inputs = tf .placeholder (tf .float32 , (None , 1 ))
279+ layer = tfc .EntropyBottleneck (filters = (2 ,))
284280 _ , likelihood = layer (inputs , training = True )
285281 with self .test_session () as sess :
286- sess .run (variables .global_variables_initializer ())
282+ sess .run (tf .global_variables_initializer ())
287283 x = np .repeat (np .arange (- 200 , 201 ), 1000 )[:, None ]
288284 likelihood , = sess .run ([likelihood ], {inputs : x })
289285 self .assertEqual (x .shape , likelihood .shape )
@@ -292,16 +288,16 @@ def test_normalization(self):
292288
293289 def test_entropy_estimates (self ):
294290 # Test that entropy estimates match actual range coding.
295- inputs = array_ops .placeholder (dtypes .float32 , (1 , None , 1 ))
296- layer = entropy_models .EntropyBottleneck (
291+ inputs = tf .placeholder (tf .float32 , (1 , None , 1 ))
292+ layer = tfc .EntropyBottleneck (
297293 filters = (2 , 3 ), data_format = "channels_last" )
298294 _ , likelihood = layer (inputs , training = True )
299- diff_entropy = math_ops .reduce_sum (math_ops .log (likelihood )) / - np .log (2 )
295+ diff_entropy = tf .reduce_sum (tf .log (likelihood )) / - np .log (2 )
300296 _ , likelihood = layer (inputs , training = False )
301- disc_entropy = math_ops .reduce_sum (math_ops .log (likelihood )) / - np .log (2 )
297+ disc_entropy = tf .reduce_sum (tf .log (likelihood )) / - np .log (2 )
302298 bitstrings = layer .compress (inputs )
303299 with self .test_session () as sess :
304- sess .run (variables .global_variables_initializer ())
300+ sess .run (tf .global_variables_initializer ())
305301 self .assertTrue (len (layer .updates ) == 1 )
306302 sess .run (layer .updates [0 ])
307303 diff_entropy , disc_entropy , bitstrings = sess .run (
0 commit comments