Blame view
complexnn/utils.py
4.91 KB
8a1d43c41 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
#!/usr/bin/env python # -*- coding: utf-8 -*- # Contributors: Titouan Parcollet # Authors: Dmitriy Serdyuk, Olexa Bilaniuk, Chiheb Trabelsi import keras.backend as K from keras.layers import Layer, Lambda ################ # Quaternions # ################ def get_rpart(x): image_format = K.image_data_format() ndim = K.ndim(x) input_shape = K.shape(x) if (image_format == 'channels_first' and ndim != 3) or ndim == 2: input_dim = input_shape[1] // 4 return x[:, :input_dim] input_dim = input_shape[-1] // 4 if ndim == 3: return x[:, :, :input_dim] elif ndim == 4: return x[:, :, :, :input_dim] elif ndim == 5: return x[:, :, :, :, :input_dim] def get_ipart(x): image_format = K.image_data_format() ndim = K.ndim(x) input_shape = K.shape(x) if (image_format == 'channels_first' and ndim != 3) or ndim == 2: input_dim = input_shape[1] // 4 return x[:, input_dim:input_dim*2] input_dim = input_shape[-1] // 4 if ndim == 3: return x[:, :, input_dim:input_dim*2] elif ndim == 4: return x[:, :, :, input_dim:input_dim*2] elif ndim == 5: return x[:, :, :, :, input_dim:input_dim*2] def get_jpart(x): image_format = K.image_data_format() ndim = K.ndim(x) input_shape = K.shape(x) if (image_format == 'channels_first' and ndim != 3) or ndim == 2: input_dim = input_shape[1] // 4 return x[:, input_dim*2:input_dim*3] input_dim = input_shape[-1] // 4 if ndim == 3: return x[:, :, input_dim*2:input_dim*3] elif ndim == 4: return x[:, :, :, input_dim*2:input_dim*3] elif ndim == 5: return x[:, :, :, :, input_dim*2:input_dim*3] def get_kpart(x): image_format = K.image_data_format() ndim = K.ndim(x) input_shape = K.shape(x) if (image_format == 'channels_first' and ndim != 3) or ndim == 2: input_dim = input_shape[1] // 4 return x[:, input_dim*3:] input_dim = input_shape[-1] // 4 if ndim == 3: return x[:, :, input_dim*3:] elif ndim == 4: return x[:, :, :, input_dim*3:] elif ndim == 5: return x[:, :, :, :, input_dim*3:] class GetR(Layer): def call(self, inputs): return get_rpart(inputs) def compute_output_shape(self, input_shape): return getpart_quaternion_output_shape(input_shape) class GetI(Layer): def call(self, inputs): return get_ipart(inputs) def compute_output_shape(self, input_shape): return getpart_quaternion_output_shape(input_shape) class GetJ(Layer): def call(self, inputs): return get_jpart(inputs) def compute_output_shape(self, input_shape): return getpart_quaternion_output_shape(input_shape) class GetK(Layer): def call(self, inputs): return get_kpart(inputs) def compute_output_shape(self, input_shape): return getpart_quaternion_output_shape(input_shape) def getpart_quaternion_output_shape(input_shape): returned_shape = list(input_shape[:]) image_format = K.image_data_format() ndim = len(returned_shape) if (image_format == 'channels_first' and ndim != 3) or ndim == 2: axis = 1 else: axis = -1 returned_shape[axis] = returned_shape[axis] // 4 return tuple(returned_shape) # # GetReal/GetImag Lambda layer Implementation # def get_realpart(x): image_format = K.image_data_format() ndim = K.ndim(x) input_shape = K.shape(x) if (image_format == 'channels_first' and ndim != 3) or ndim == 2: input_dim = input_shape[1] // 2 return x[:, :input_dim] input_dim = input_shape[-1] // 2 if ndim == 3: return x[:, :, :input_dim] elif ndim == 4: return x[:, :, :, :input_dim] elif ndim == 5: return x[:, :, :, :, :input_dim] def get_imagpart(x): image_format = K.image_data_format() ndim = K.ndim(x) input_shape = K.shape(x) if (image_format == 'channels_first' and ndim != 3) or ndim == 2: input_dim = input_shape[1] // 2 return x[:, input_dim:] input_dim = input_shape[-1] // 2 if ndim == 3: return x[:, :, input_dim:] elif ndim == 4: return x[:, :, :, input_dim:] elif ndim == 5: return x[:, :, :, :, input_dim:] def get_abs(x): real = get_realpart(x) imag = get_imagpart(x) return K.sqrt(real * real + imag * imag) def getpart_output_shape(input_shape): returned_shape = list(input_shape[:]) image_format = K.image_data_format() ndim = len(returned_shape) if (image_format == 'channels_first' and ndim != 3) or ndim == 2: axis = 1 else: axis = -1 returned_shape[axis] = returned_shape[axis] // 2 return tuple(returned_shape) class GetReal(Layer): def call(self, inputs): return get_realpart(inputs) def compute_output_shape(self, input_shape): return getpart_output_shape(input_shape) class GetImag(Layer): def call(self, inputs): return get_imagpart(inputs) def compute_output_shape(self, input_shape): return getpart_output_shape(input_shape) class GetAbs(Layer): def call(self, inputs): return get_abs(inputs) def compute_output_shape(self, input_shape): return getpart_output_shape(input_shape) |