|
| 1 | +# Test unit for decomon with Dense layers |
| 2 | + |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | +import tensorflow.python.keras.backend as K |
| 7 | +from tensorflow.keras.layers import Input, Conv2D |
| 8 | +from tensorflow.keras.models import Model |
| 9 | + |
| 10 | +from decomon.backward_layers.backward_layers import get_backward |
| 11 | +from decomon.layers.decomon_layers import DecomonDense, to_monotonic |
| 12 | + |
| 13 | +from decomon.backward_layers.utils_conv import get_toeplitz |
| 14 | +from numpy.testing import assert_almost_equal |
| 15 | + |
| 16 | + |
| 17 | +def test_toeplitz_from_Keras(channels, filter_size, strides, flatten, data_format, |
| 18 | + padding, floatx, helpers): |
| 19 | + |
| 20 | + # filter_size, strides, flatten, |
| 21 | + K.set_floatx("float{}".format(floatx)) |
| 22 | + eps = K.epsilon() |
| 23 | + decimal = 5 |
| 24 | + if floatx == 16: |
| 25 | + K.set_epsilon(1e-2) |
| 26 | + decimal = 0 |
| 27 | + |
| 28 | + if data_format == "channels_first" and not len(K._get_available_gpus()): |
| 29 | + return |
| 30 | + |
| 31 | + #filter_size=3 |
| 32 | + #strides=1 |
| 33 | + #flatten = True |
| 34 | + odd, m_0, m_1 = 0, 0, 1 |
| 35 | + |
| 36 | + # should be working either with convolution of conv2D |
| 37 | + layer = Conv2D(channels, (filter_size, filter_size), strides=strides, use_bias=False, |
| 38 | + padding=padding, dtype=K.floatx()) |
| 39 | + |
| 40 | + inputs = helpers.get_tensor_decomposition_images_box(data_format, odd) |
| 41 | + inputs_ = helpers.get_standard_values_images_box(data_format, odd, m0=m_0, m1=m_1) |
| 42 | + y = inputs[1] |
| 43 | + result_ref = layer(y) |
| 44 | + W = get_toeplitz(layer, flatten) |
| 45 | + |
| 46 | + if not flatten: |
| 47 | + w_in, h_in, c_in, w_out, h_out, c_out = W.shape |
| 48 | + W = K.reshape(W, (w_in*h_in*c_in, w_out*h_out*c_out)) |
| 49 | + |
| 50 | + n_in, n_out = W.shape |
| 51 | + y_flat = K.reshape(y, (-1, n_in, 1)) |
| 52 | + result_flat = K.reshape(result_ref, (-1, n_out)) |
| 53 | + result_toeplitz = K.sum(W[None]*y_flat, 1) |
| 54 | + output_test = K.sum((result_toeplitz-result_flat)**2) |
| 55 | + f_test = K.function(y, output_test) |
| 56 | + |
| 57 | + output_test_ = f_test(inputs_[1]) |
| 58 | + assert_almost_equal( |
| 59 | + output_test_, |
| 60 | + np.zeros_like(output_test_), |
| 61 | + decimal=decimal, |
| 62 | + err_msg="wrong toeplitz matrix", |
| 63 | + ) |
| 64 | + |
| 65 | + K.set_floatx("float{}".format(32)) |
| 66 | + K.set_epsilon(eps) |
| 67 | + |
| 68 | + |
| 69 | +def test_toeplitz_from_Decomon(channels, filter_size, strides, flatten, data_format, |
| 70 | + padding, floatx, helpers): |
| 71 | + |
| 72 | + # filter_size, strides, flatten, |
| 73 | + K.set_floatx("float{}".format(floatx)) |
| 74 | + eps = K.epsilon() |
| 75 | + decimal = 5 |
| 76 | + if floatx == 16: |
| 77 | + K.set_epsilon(1e-2) |
| 78 | + decimal = 0 |
| 79 | + |
| 80 | + if data_format == "channels_first" and not len(K._get_available_gpus()): |
| 81 | + return |
| 82 | + |
| 83 | + #filter_size=3 |
| 84 | + #strides=1 |
| 85 | + #flatten = True |
| 86 | + odd, m_0, m_1 = 0, 0, 1 |
| 87 | + |
| 88 | + # should be working either with convolution of conv2D |
| 89 | + layer = Conv2D(channels, (filter_size, filter_size), strides=strides, use_bias=False, |
| 90 | + padding=padding, dtype=K.floatx()) |
| 91 | + |
| 92 | + inputs = helpers.get_tensor_decomposition_images_box(data_format, odd) |
| 93 | + inputs_ = helpers.get_standard_values_images_box(data_format, odd, m0=m_0, m1=m_1) |
| 94 | + y = inputs[1] |
| 95 | + result_ref = layer(y) |
| 96 | + # toeplitz matrix should be compatible with a DecomonLayer |
| 97 | + input_dim = inputs[0].shape[-1] |
| 98 | + decomon_layer = to_monotonic(layer, input_dim)[0] |
| 99 | + |
| 100 | + W = get_toeplitz(decomon_layer, flatten) |
| 101 | + |
| 102 | + if not flatten: |
| 103 | + w_in, h_in, c_in, w_out, h_out, c_out = W.shape |
| 104 | + W = K.reshape(W, (w_in*h_in*c_in, w_out*h_out*c_out)) |
| 105 | + |
| 106 | + n_in, n_out = W.shape |
| 107 | + y_flat = K.reshape(y, (-1, n_in, 1)) |
| 108 | + result_flat = K.reshape(result_ref, (-1, n_out)) |
| 109 | + result_toeplitz = K.sum(W[None]*y_flat, 1) |
| 110 | + output_test = K.sum((result_toeplitz-result_flat)**2) |
| 111 | + f_test = K.function(y, output_test) |
| 112 | + |
| 113 | + output_test_ = f_test(inputs_[1]) |
| 114 | + assert_almost_equal( |
| 115 | + output_test_, |
| 116 | + np.zeros_like(output_test_), |
| 117 | + decimal=decimal, |
| 118 | + err_msg="wrong toeplitz matrix", |
| 119 | + ) |
| 120 | + |
| 121 | + K.set_floatx("float{}".format(32)) |
| 122 | + K.set_epsilon(eps) |
| 123 | + |
| 124 | +""" |
| 125 | +def test_toeplitz_from_Decomon(channels, filter_size, strides, flatten, data_format, |
| 126 | + padding, floatx, helpers): |
| 127 | +
|
| 128 | + # filter_size, strides, flatten, |
| 129 | + K.set_floatx("float{}".format(floatx)) |
| 130 | + eps = K.epsilon() |
| 131 | + decimal = 5 |
| 132 | + if floatx == 16: |
| 133 | + K.set_epsilon(1e-2) |
| 134 | + decimal = 0 |
| 135 | + |
| 136 | + if data_format == "channels_first" and not len(K._get_available_gpus()): |
| 137 | + return |
| 138 | +
|
| 139 | + #filter_size=3 |
| 140 | + #strides=1 |
| 141 | + #flatten = True |
| 142 | + odd, m_0, m_1 = 0, 0, 1 |
| 143 | +
|
| 144 | + # should be working either with convolution of conv2D |
| 145 | + layer = Conv2D(channels, (filter_size, filter_size), strides=strides, use_bias=False, |
| 146 | + padding=padding, dtype=K.floatx()) |
| 147 | +
|
| 148 | + inputs = helpers.get_tensor_decomposition_images_box(data_format, odd) |
| 149 | + inputs_ = helpers.get_standard_values_images_box(data_format, odd, m0=m_0, m1=m_1) |
| 150 | +
|
| 151 | + result_ref = layer(inputs[1]) |
| 152 | + # toeplitz matrix should be compatible with a DecomonLayer |
| 153 | + input_dim = inputs[0].shape[-1] |
| 154 | + decomon_layer = to_monotonic(layer, input_dim) |
| 155 | +
|
| 156 | + W = get_toeplitz(layer, flatten) |
| 157 | +
|
| 158 | + if not flatten: |
| 159 | + w_in, h_in, c_in, w_out, h_out, c_out = W.shape |
| 160 | + W = K.reshape(W, (w_in*h_in*c_in, w_out*h_out*c_out)) |
| 161 | +
|
| 162 | + n_in, n_out = W.shape |
| 163 | + y_flat = K.reshape(inputs[1], (-1, n_in, 1)) |
| 164 | + result_flat = K.reshape(result_ref, (-1, n_out)) |
| 165 | + result_toeplitz = K.sum(W[None]*y_flat, 1) |
| 166 | + output_test = K.sum((result_toeplitz-result_flat)**2) |
| 167 | + f_test = K.function(y, output_test) |
| 168 | +
|
| 169 | + output_test_ = f_test(inputs_[1]) |
| 170 | + assert_almost_equal( |
| 171 | + output_test_, |
| 172 | + np.zeros_like(output_test_), |
| 173 | + decimal=decimal, |
| 174 | + err_msg="wrong toeplitz matrix", |
| 175 | + ) |
| 176 | +
|
| 177 | + K.set_floatx("float{}".format(32)) |
| 178 | + K.set_epsilon(eps) |
| 179 | +""" |
0 commit comments