def _inference(self, x, dropout): with tf.name_scope('conv1'): # Transform to Fourier domain x_2d = tf.reshape(x, [-1, 28, 28]) x_2d = tf.complex(x_2d, 0) xf_2d = tf.fft2d(x_2d) xf = tf.reshape(xf_2d, [-1, NFEATURES]) xf = tf.expand_dims(xf, 1) # NSAMPLES x 1 x NFEATURES xf = tf.transpose(xf) # NFEATURES x 1 x NSAMPLES # Filter Wreal = self._weight_variable([int(NFEATURES/2), self.F, 1]) Wimg = self._weight_variable([int(NFEATURES/2), self.F, 1]) W = tf.complex(Wreal, Wimg) xf = xf[:int(NFEATURES/2), :, :] yf = tf.matmul(W, xf) # for each feature yf = tf.concat([yf, tf.conj(yf)], axis=0) yf = tf.transpose(yf) # NSAMPLES x NFILTERS x NFEATURES yf_2d = tf.reshape(yf, [-1, 28, 28]) # Transform back to spatial domain y_2d = tf.ifft2d(yf_2d) y_2d = tf.real(y_2d) y = tf.reshape(y_2d, [-1, self.F, NFEATURES]) # Bias and non-linearity b = self._bias_variable([1, self.F, 1]) # b = self._bias_variable([1, self.F, NFEATURES]) y += b # NSAMPLES x NFILTERS x NFEATURES y = tf.nn.relu(y) with tf.name_scope('fc1'): W = self._weight_variable([self.F*NFEATURES, NCLASSES]) b = self._bias_variable([NCLASSES]) y = tf.reshape(y, [-1, self.F*NFEATURES]) y = tf.matmul(y, W) + b return y
def _link(self, input_, **kwargs): assert isinstance(input_, tf.Tensor) # If this layer has been linked once, variables should be reused if self.weights is not None: tf.get_variable_scope().reuse_variables() # Get the shape and data type of input input_shape = input_.get_shape().as_list() dtype = input_.dtype weight_shape = (input_shape[-1], self._output_dim) bias_shape = (self._output_dim, ) # Use lambda to make getting variable easier get_weight_variable = lambda name, fixed_zero=False: self._get_variable( name, weight_shape, fixed_zero, self._weight_initializer, self. _weight_regularizer) get_bias_variable = lambda name, fixed_zero=False: self._get_variable( name, bias_shape, fixed_zero, self._bias_initializer, self. _bias_regularizer) # Get variable if dtype in [tf.complex64, tf.complex128]: # Get complex weights and biases self.weights = tf.complex(get_weight_variable('weights_real'), get_weight_variable( 'weights_imag', self._force_real), name='weights') if self._use_bias: self.biases = tf.complex(get_bias_variable('biases_real'), get_bias_variable( 'biases_imag', self._force_real), name='biases') else: # Get real weights and biases self.weights = get_weight_variable('weights') if self._use_bias: self.biases = get_bias_variable('biases') # Calculate output output = tf.matmul(input_, self.weights) if self._use_bias: output += self.biases # Monitor # if hub.monitor_weight or hub.monitor_grad: # tfr.monitor.add_weight(self.weights) self.output_tensor = output return output
def frequency_impulse_response(magnitudes: tf.Tensor, window_size: int = 0) -> tf.Tensor: """Get windowed impulse responses using the frequency sampling method. Follows the approach in: https://ccrma.stanford.edu/~jos/sasp/Windowing_Desired_Impulse_Response.html Args: magnitudes: Frequency transfer curve. Float32 Tensor of shape [batch, n_frames, n_frequencies] or [batch, n_frequencies]. The frequencies of the last dimension are ordered as [0, f_nyqist / (n_frames -1), ..., f_nyquist], where f_nyquist is (sample_rate / 2). Automatically splits the audio into equally sized frames to match frames in magnitudes. window_size: Size of the window to apply in the time domain. If window_size is less than 1, it defaults to the impulse_response size. Returns: impulse_response: Time-domain FIR filter of shape [batch, frames, window_size] or [batch, window_size]. Raises: ValueError: If window size is larger than fft size. """ # Get the IR (zero-phase form). magnitudes = tf.complex(magnitudes, tf.zeros_like(magnitudes)) impulse_response = tf.signal.irfft(magnitudes) # Window and put in causal form. impulse_response = apply_window_to_impulse_response( impulse_response, window_size) return impulse_response
def Dnn_Decoder_full(x, weights, biases, train_flag): with tf.name_scope("full_layer1") as scope: fc1 = tf.add(tf.matmul(x, weights['full_wf1']), biases['full_bf1']) fc1 = tf.nn.relu(fc1) with tf.name_scope("full_layer2") as scope: fc2 = tf.add(tf.matmul(fc1, weights['full_wf2']), biases['full_bf2']) fc2 = tf.nn.relu(fc2) with tf.name_scope("full_layer3") as scope: fc3 = tf.add(tf.matmul(fc2, weights['full_wf3']), biases['full_bf3']) fc3 = tf.nn.relu(fc3) with tf.name_scope("full_layer4") as scope: fc4 = tf.add(tf.matmul(fc3, weights['full_wf4']), biases['full_bf4']) # Normalization for i in range(n_tx): fc4_temp = fc4[:, n_tx * complex_size * i:n_tx * complex_size * (i + 1)] fc4_complex = tf.expand_dims(tf.complex(fc4_temp[:, 0:n_tx], fc4_temp[:, n_tx:]), axis=2) fc4_norm = tf.expand_dims(tf.norm(fc4_complex, ord=2, axis=1), axis=2) if i == 0: fc4_final = fc4_complex / fc4_norm else: fc4_final = tf.concat([fc4_final, fc4_complex / fc4_norm], 2) print(fc4_final) return fc4_final
def q2c(y): """ Convert from quads in y to complex numbers in z. """ # Arrange pixels from the corners of the quads into # 2 subimages of alternate real and imag pixels. # a----b # | | # | | # c----d # Combine (a,b) and (d,c) to form two complex subimages. a, b = y[:, 0::2, 0::2], y[:, 0::2, 1::2] c, d = y[:, 1::2, 0::2], y[:, 1::2, 1::2] p = tf.complex(a / np.sqrt(2), b / np.sqrt(2)) # p = (a + jb) / sqrt(2) q = tf.complex(d / np.sqrt(2), -c / np.sqrt(2)) # q = (d - jc) / sqrt(2) # Form the 2 highpasses in z. return (p - q, p + q)
def relu(input_): if input_.dtype in [tf.complex64, tf.complex128]: with tf.name_scope("c_relu"): eps = 1e-7 real = tf.real(input_) # Handle small value issue sign = real / tf.maximum(eps, tf.abs(real)) mask = tf.maximum(sign, 0) mask = tf.complex(mask, tf.zeros_like(mask, dtype=mask.dtype)) return input_ * mask else: return tf.nn.relu(input_, name='relu')
def tf_so8_sugra_potential(t_v70): """Returns dict with key tensors from the SUGRA potential's TF graph.""" tc_28_8_8 = tf.constant(su8.m_28_8_8) t_e7_generator_v70 = tf.einsum( 'v,vIJ->JI', tf.complex(t_v70, tf.constant([0.0] * 70, dtype=tf.float64)), tf.constant(e7.t_a_ij_kl[:70, :, :], dtype=tf.complex128)) t_complex_vielbein = tf.linalg.expm(t_e7_generator_v70) def expand_ijkl(t_ab): return 0.5 * tf.einsum('ijB,BIJ->ijIJ', tf.einsum('AB,Aij->ijB', t_ab, tc_28_8_8), tc_28_8_8) # t_u_ijIJ = expand_ijkl(t_complex_vielbein[:28, :28]) t_u_klKL = expand_ijkl(t_complex_vielbein[28:, 28:]) t_v_ijKL = expand_ijkl(t_complex_vielbein[:28, 28:]) t_v_klIJ = expand_ijkl(t_complex_vielbein[28:, :28]) # t_uv = t_u_klKL + t_v_klIJ t_uuvv = (tf.einsum('lmJK,kmKI->lkIJ', t_u_ijIJ, t_u_klKL) - tf.einsum('lmJK,kmKI->lkIJ', t_v_ijKL, t_v_klIJ)) t_T = tf.einsum('ijIJ,lkIJ->lkij', t_uv, t_uuvv) t_A1 = (-4.0 / 21.0) * tf.trace(tf.einsum('mijn->ijmn', t_T)) t_A2 = (-4.0 / (3 * 3)) * ( # Antisymmetrize in last 3 indices, taking into account antisymmetry # in last two indices. t_T + tf.einsum('lijk->ljki', t_T) + tf.einsum('lijk->lkij', t_T)) t_A1_real = tf.real(t_A1) t_A1_imag = tf.imag(t_A1) t_A2_real = tf.real(t_A2) t_A2_imag = tf.imag(t_A2) t_A1_potential = (-3.0 / 4) * (tf.einsum('ij,ij->', t_A1_real, t_A1_real) + tf.einsum('ij,ij->', t_A1_imag, t_A1_imag)) t_A2_potential = (1.0 / 24) * (tf.einsum('ijkl,ijkl->', t_A2_real, t_A2_real) + tf.einsum('ijkl,ijkl->', t_A2_imag, t_A2_imag)) t_potential = t_A1_potential + t_A2_potential # return dict(v70=t_v70, vielbein=t_complex_vielbein, tee_tensor=t_T, a1=t_A1, a2=t_A2, potential=t_potential)
def cexpm(t_m_complex, max_squarings=20, taylor_n_max=20, complex_arg=True): """Drop-in replacement for tf.linalg.expm(), optionally for complex arg.""" if complex_arg: t_m = tf.stack([tf.math.real(t_m_complex), tf.math.imag(t_m_complex)]) else: t_m = t_m_complex fn_product = _complex_matmul if complex_arg else tf.matmul t_num_squarings = tf.minimum(_c64(max_squarings), _get_num_squarings(t_m)) t_m_scaled = t_m * tf.pow(_c64(0.5), t_num_squarings) exp_t_m_scaled = tf_shallow_expm_taylor(t_m_scaled, n_max=taylor_n_max) ret = _get_squaring_cascade(t_num_squarings, exp_t_m_scaled, prod=fn_product, max_squarings=max_squarings) if complex_arg: return tf.complex(ret[0], ret[1]) else: return ret
def polar2rect(mag, phase_angle): """Convert polar-form complex number to its rectangular form.""" mag = tf.complex(mag, tf.convert_to_tensor(0.0, dtype=mag.dtype)) phase = tf.complex(tf.cos(phase_angle), tf.sin(phase_angle)) return mag * phase
def build(self): self.audios = tf.placeholder(tf.float32, [self.batch_size, self.n_speaker, None], name='input_signals') self.mix_input = tf.reduce_sum(self.audios, axis=1) with tf.variable_scope("encoder"): # [batch, encode_len, channels] encoded_input = tf.layers.Conv1D( filters=self.config["model"]["filters"]["ae"], kernel_size=self.fft_len, strides=self.fft_hop, activation=tf.nn.relu, name="conv1d_relu")(tf.expand_dims(self.mix_input, -1)) stfts_mix = tf.signal.stft(self.mix_input, frame_length=self.fft_len, frame_step=self.fft_hop, fft_length=self.fft_len, window_fn=self.fft_wnd) magni_mix = tf.abs(stfts_mix) phase_mix = tf.atan2(tf.imag(stfts_mix), tf.real(stfts_mix)) with tf.variable_scope("bottle_start"): norm_input = self.cLN( tf.concat([encoded_input, tf.log1p(magni_mix)], axis=-1), "layer_norm") block_input = tf.layers.Conv1D( filters=self.config["model"]["filters"]["1*1-conv"], kernel_size=1)(norm_input) for stack_i in range(self.num_stacks): for dilation in self.dilations: with tf.variable_scope("conv_block_{}_{}".format( stack_i, dilation)): block_output = tf.layers.Conv1D( filters=self.config["model"]["filters"]["d-conv"], kernel_size=1)(block_input) block_output = self.prelu(block_output, name='1st-prelu', shared_axes=[1]) block_output = self.gLN(block_output, "first") block_output = self._depthwise_conv1d( block_output, dilation) block_output = self.prelu(block_output, name='2nd-prelu', shared_axes=[1]) block_output = self.gLN(block_output, "second") block_output = tf.layers.Conv1D( filters=self.config["model"]["filters"]["1*1-conv"], kernel_size=1)(block_output) block_input += block_output if self.output_ratio == 1: embed_channel = self.config["model"]["filters"]["ae"] feature_map = encoded_input elif self.output_ratio == 0: embed_channel = self.stft_ch feature_map = magni_mix else: embed_channel = self.concat_channels feature_map = tf.concat([encoded_input, magni_mix], axis=-1) with tf.variable_scope('separator'): s_embed = tf.layers.Dense( embed_channel * self.config["model"]["embed_size"])(block_input) s_embed = tf.reshape(s_embed, [ self.batch_size, -1, embed_channel, self.config["model"]["embed_size"] ]) # Estimate attractor from best combination from anchors v_anchors = tf.get_variable( 'anchors', [self.n_anchor, self.config["model"]["embed_size"]], dtype=tf.float32) c_combs = tf.constant(list( itertools.combinations(range(self.n_anchor), self.n_speaker)), name='combs') s_anchor_sets = tf.gather(v_anchors, c_combs) s_anchor_assignment = tf.einsum('btfe,pce->bptfc', s_embed, s_anchor_sets) s_anchor_assignment = tf.nn.softmax(s_anchor_assignment) s_attractor_sets = tf.einsum('bptfc,btfe->bpce', s_anchor_assignment, s_embed) s_attractor_sets /= tf.expand_dims( tf.reduce_sum(s_anchor_assignment, axis=(2, 3)), -1) sp = tf.matmul(s_attractor_sets, tf.transpose(s_attractor_sets, [0, 1, 3, 2])) diag = tf.fill(sp.shape[:-1], float("-inf")) sp = tf.linalg.set_diag(sp, diag) s_in_set_similarities = tf.reduce_max(sp, axis=(-1, -2)) s_subset_choice = tf.argmin(s_in_set_similarities, axis=1) s_subset_choice_nd = tf.transpose( tf.stack([ tf.range(self.batch_size, dtype=tf.int64), s_subset_choice ])) s_attractors = tf.gather_nd(s_attractor_sets, s_subset_choice_nd) s_logits = tf.einsum('btfe,bce->bctf', s_embed, s_attractors) output_code = s_logits * tf.expand_dims(feature_map, 1) with tf.variable_scope("decoder"): conv_out = pred_istfts = 0 if self.output_ratio != 0: output_frame = tf.layers.Dense( self.config["model"]["kernel_size"]["ae"])(output_code[ ..., :self.config["model"]["filters"]["ae"]]) conv_out = tf.signal.overlap_and_add(signal=output_frame, frame_step=self.fft_hop) if self.output_ratio != 1: phase_mix_expand = tf.expand_dims(phase_mix, 1) pred_stfts = tf.complex( tf.cos(phase_mix_expand) * output_code[..., -self.stft_ch:], tf.sin(phase_mix_expand) * output_code[..., -self.stft_ch:]) pred_istfts = tf.signal.inverse_stft( pred_stfts, frame_length=self.fft_len, frame_step=self.fft_hop, fft_length=self.fft_len, window_fn=tf.signal.inverse_stft_window_fn( self.fft_hop, forward_window_fn=self.fft_wnd)) self.data_out = conv_out * self.output_ratio + pred_istfts * ( 1 - self.output_ratio) self.loss, self.pred_output, self.sdr, self.perm_idxs = loss.pit_loss( self.audios, self.data_out, self.config, self.batch_size, self.n_speaker, self.n_output) ### fixed loss not implemented yet !!!!!! ### self.loss_fix, self.pred_output_fix, self.sdr_fix, self.perm_idxs_fix = loss.pit_loss( self.audios, self.data_out, self.config, self.batch_size, self.n_speaker, self.n_output)
def fitness(lr, node_layer11, node_layer12, node_layer13, node_layer21, node_layer22, node_layer23): # start the model making process and create our first layer tf.reset_default_graph() # Erase all the info from TF models # print('################################## Neurons number - ', neurons) learning_rate = 1e-3 seedd = 1234 training_epochs = 5000 batch_size = 1000 n_taps = lr # Number of forward and backward taps n_sym = 2 * n_taps + 1 Best_BER_NN = 1 # Construct NMSE estimator def NMSE_est(x, x_ref): return 20. * np.log10( np.linalg.norm(x - x_ref) / np.linalg.norm(x_ref)) def BER_est(x, x_ref): QAM_order = QAM return BER_calc.QAM_BER_gray(x, x_ref, QAM_order) def ph_norm(x, x_ref): nl_shift = np.dot(np.transpose(np.conjugate(x_ref)), x_ref) / np.dot( np.transpose(np.conjugate(x_ref)), x) return x * nl_shift, nl_shift # Define dataset constructor out of input raw data def create_dataset(x_pol_in_complex, y_pol_in_complex, x_pol_des_complex, n_sym1, batch_size1): raw_size = x_pol_in_complex.shape[0] dataset_size = raw_size - 2 * n_sym1 dataset_range = n_sym1 + np.arange(dataset_size) dataset_x_pol_des = x_pol_des_complex[dataset_range] dataset_x_pol_in = np.empty([dataset_size, n_sym1], dtype='complex128') dataset_x_pol_in[:] = np.nan + 1j * np.nan dataset_y_pol_in = np.empty([dataset_size, n_sym1], dtype='complex128') dataset_y_pol_in[:] = np.nan + 1j * np.nan bnd_vec = int(np.floor(n_sym1 / 2)) for vec_idx, center_vec in enumerate(dataset_range): local_range = center_vec + np.arange(-bnd_vec, bnd_vec + 1) if np.any(local_range < 0) or np.any(local_range > raw_size): ValueError( 'Local range steps out of the data range during dataset creation!!!' ) else: dataset_x_pol_in[vec_idx, :] = x_pol_in_complex[local_range] dataset_y_pol_in[vec_idx, :] = y_pol_in_complex[local_range] if np.any(np.isnan(dataset_x_pol_in)) or np.any( np.isnan(dataset_y_pol_in)): ValueError('Dataset matrix wasn' 't fully filled by data!!!') # Cut the excessive datapoint not fitting into batches batches_limit = range( int(np.floor(dataset_size / batch_size1) * batch_size1)) dataset_x_pol_in = dataset_x_pol_in[batches_limit] dataset_y_pol_in = dataset_y_pol_in[batches_limit] dataset_x_pol_des = dataset_x_pol_des[batches_limit] dataset_x_pol_in, dataset_y_pol_in, dataset_x_pol_des = shuffle( dataset_x_pol_in, dataset_y_pol_in, dataset_x_pol_des) return dataset_x_pol_in, dataset_y_pol_in, dataset_x_pol_des # Create the testing dataset dataset_x_pol_in_test, dataset_y_pol_in_test, dataset_x_pol_des_test = create_dataset( x_pol_in_test_complex, y_pol_in_test_complex, x_pol_des_test_complex, n_sym, batch_size) # Create the train dataset dataset_x_pol_in_train, dataset_y_pol_in_train, dataset_x_pol_des_train = create_dataset( x_pol_in_train_complex, y_pol_in_train_complex, x_pol_des_train_complex, n_sym, batch_size) # tf Graph input tf_x_pol_in = tf.placeholder(tf.complex128, [None, n_sym]) tf_y_pol_in = tf.placeholder(tf.complex128, [None, n_sym]) tf_x_pol_des = tf.placeholder(tf.complex128, [None, 1]) # Y2 = tf.placeholder(tf.complex128, [None, 1]) weight_init_mean = 0 weight_init_std = 0.1 # Store layers weight & bias tf.set_random_seed(seedd) neurons_l11 = node_layer11 # number of neurons layer 11 neurons_l12 = node_layer12 # number of neurons layer 12 neurons_l13 = node_layer13 # number of neurons layer 12 neurons_l21 = node_layer21 # number of neurons layer 21 neurons_l22 = node_layer22 # number of neurons layer 22 neurons_l23 = node_layer23 # number of neurons layer 22 weights = { 'h1': tf.Variable( tf.truncated_normal([n_sym, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h2': tf.Variable( tf.truncated_normal([n_sym, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h3': tf.Variable( tf.truncated_normal([n_sym, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h4': tf.Variable( tf.truncated_normal([n_sym, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h5': tf.Variable( tf.truncated_normal([n_sym, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h6': tf.Variable( tf.truncated_normal([n_sym, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h7': tf.Variable( tf.truncated_normal([n_sym, neurons_l11], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h8': tf.Variable( tf.truncated_normal([n_sym, neurons_l11], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h9': tf.Variable( tf.truncated_normal([n_sym, neurons_l11], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h10': tf.Variable( tf.truncated_normal([n_sym, neurons_l11], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h11': tf.Variable( tf.truncated_normal([neurons_l11, neurons_l12], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h12': tf.Variable( tf.truncated_normal([neurons_l11, neurons_l12], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h13': tf.Variable( tf.truncated_normal([neurons_l12, neurons_l13], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h14': tf.Variable( tf.truncated_normal([neurons_l12, neurons_l13], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h15': tf.Variable( tf.truncated_normal([neurons_l13, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h16': tf.Variable( tf.truncated_normal([neurons_l13, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h17': tf.Variable( tf.truncated_normal([n_sym, neurons_l21], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h18': tf.Variable( tf.truncated_normal([n_sym, neurons_l21], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h19': tf.Variable( tf.truncated_normal([n_sym, neurons_l21], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h20': tf.Variable( tf.truncated_normal([n_sym, neurons_l21], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h21': tf.Variable( tf.truncated_normal([neurons_l21, neurons_l22], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h22': tf.Variable( tf.truncated_normal([neurons_l21, neurons_l22], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h23': tf.Variable( tf.truncated_normal([neurons_l22, neurons_l23], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h24': tf.Variable( tf.truncated_normal([neurons_l22, neurons_l23], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h25': tf.Variable( tf.truncated_normal([neurons_l23, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), 'h26': tf.Variable( tf.truncated_normal([neurons_l23, 1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)), } weights_complex = { 'w1': tf.complex(weights['h1'], weights['h2']), 'w2': tf.complex(weights['h3'], weights['h4']), 'w3': tf.complex(weights['h5'], weights['h6']), 'w4': tf.complex(weights['h7'], weights['h8']), 'w5': tf.complex(weights['h9'], weights['h10']), 'w6': tf.complex(weights['h11'], weights['h12']), 'w7': tf.complex(weights['h13'], weights['h14']), 'w8': tf.complex(weights['h15'], weights['h16']), 'w9': tf.complex(weights['h17'], weights['h18']), 'w10': tf.complex(weights['h19'], weights['h20']), 'w11': tf.complex(weights['h21'], weights['h22']), 'w12': tf.complex(weights['h23'], weights['h24']), 'w13': tf.complex(weights['h25'], weights['h26']), } x_gamma_accum_r = tf.Variable( -tf.truncated_normal([1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)) x_gamma_accum_i = tf.Variable( -tf.truncated_normal([1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)) y_gamma_accum_r = tf.Variable( -tf.truncated_normal([1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)) y_gamma_accum_i = tf.Variable( -tf.truncated_normal([1], seed=seedd, mean=weight_init_mean, stddev=weight_init_std, dtype=tf.dtypes.float64)) # Define model def multilayer_perceptron(x_pol_tapped, y_pol_tapped): # Estimates nonlinear distortion made onto X-pol by X- and Y-pols def act_cust(x): return tf.nn.tanh(x) log_x_pol_adap_tap = tf.math.log( tf.matmul(x_pol_tapped, weights_complex['w1'])) abs_sq_x_pol_adap_tap = tf.pow( tf.math.abs(tf.matmul(x_pol_tapped, weights_complex['w2'])), 2) abs_sq_y_pol_adap_tap = tf.pow( tf.math.abs((tf.matmul(y_pol_tapped, weights_complex['w3']))), 2) x_pol_nl_ph_rot = tf.multiply( tf.cast(abs_sq_x_pol_adap_tap, tf.complex128), tf.cast(tf.complex(x_gamma_accum_r, x_gamma_accum_i), tf.complex128)) y_pol_nl_ph_rot = tf.multiply( tf.cast(abs_sq_y_pol_adap_tap, tf.complex128), tf.cast(tf.complex(y_gamma_accum_r, y_gamma_accum_i), tf.complex128)) layer_21_linear = tf.matmul(x_pol_tapped, weights_complex['w4']) + tf.matmul( y_pol_tapped, weights_complex['w5']) layer_21_real = act_cust(tf.math.real(layer_21_linear)) layer_21_imag = act_cust(tf.math.imag(layer_21_linear)) layer_21 = tf.complex(layer_21_real, layer_21_imag) layer_22_linear = tf.matmul(layer_21, weights_complex['w6']) layer_22_real = act_cust(tf.math.real(layer_22_linear)) layer_22_imag = act_cust(tf.math.imag(layer_22_linear)) layer_22 = tf.complex(layer_22_real, layer_22_imag) layer_23_linear = tf.matmul(layer_22, weights_complex['w7']) layer_23_real = (act_cust(tf.math.real(layer_23_linear))) layer_23_imag = (act_cust(tf.math.imag(layer_23_linear))) Xi = tf.complex(layer_23_real, layer_23_imag) layer_11_linear = tf.matmul(x_pol_tapped, weights_complex['w9']) + tf.matmul( y_pol_tapped, weights_complex['w10']) layer_11_real = act_cust(tf.math.real(layer_11_linear)) layer_11_imag = act_cust(tf.math.imag(layer_11_linear)) layer_11 = tf.complex(layer_11_real, layer_11_imag) layer_12_linear = tf.matmul(layer_11, weights_complex['w11']) layer_12_real = act_cust(tf.math.real(layer_12_linear)) layer_12_imag = act_cust(tf.math.imag(layer_12_linear)) layer_12 = tf.complex(layer_12_real, layer_12_imag) layer_13_linear = tf.matmul(layer_12, weights_complex['w12']) layer_13_real = (act_cust(tf.math.real(layer_13_linear))) layer_13_imag = (act_cust(tf.math.imag(layer_13_linear))) Theta = tf.complex(layer_13_real, layer_13_imag) # Output out_layer1 = tf.exp( log_x_pol_adap_tap + x_pol_nl_ph_rot + y_pol_nl_ph_rot + tf.matmul(Theta, weights_complex['w13'])) + tf.matmul( Xi, weights_complex['w8']) return out_layer1 # Construct model tf_x_pol_pred = multilayer_perceptron(tf_x_pol_in, tf_y_pol_in) # Define loss and optimizer MSE = tf.pow(tf.abs(tf_x_pol_pred - tf_x_pol_des), 2) loss_op = tf.math.reduce_mean(MSE) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op) # Initializing the variables init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # NMSE estimation NMSE_train_noNN_norm = np.zeros(training_epochs) NMSE_train_NN_norm = np.zeros(training_epochs) NMSE_train_NN = np.zeros(training_epochs) NMSE_test_noNN_norm = np.zeros(training_epochs) NMSE_test_NN_norm = np.zeros(training_epochs) NMSE_test_NN = np.zeros(training_epochs) # BER estimation BER_train_noNN_norm = np.zeros(training_epochs) BER_train_NN_norm = np.zeros(training_epochs) BER_train_NN = np.zeros(training_epochs) BER_test_noNN_norm = np.zeros(training_epochs) BER_test_NN_norm = np.zeros(training_epochs) BER_test_NN = np.zeros(training_epochs) # Training cycle for epoch_idx in range(training_epochs): # Train the NN on batches dataset_length_train = np.arange(2**18) dataset_length_test = np.arange(int(0.2 * 2**18)) batch_num_train = int( np.floor(len(dataset_length_train) / batch_size)) dataset_x_pol_pred_train = np.empty(batch_num_train * batch_size, dtype='complex128') dataset_x_pol_pred_train[:] = np.nan + 1j * np.nan dataset_x_pol_in_train, dataset_y_pol_in_train, dataset_x_pol_des_train = shuffle( dataset_x_pol_in_train, dataset_y_pol_in_train, dataset_x_pol_des_train) dataset_x_pol_in_trainn = dataset_x_pol_in_train[ dataset_length_train] dataset_y_pol_in_trainn = dataset_y_pol_in_train[ dataset_length_train] dataset_x_pol_des_trainn = dataset_x_pol_des_train[ dataset_length_train] dataset_x_pol_in_testt = dataset_x_pol_in_test[dataset_length_test] dataset_y_pol_in_testt = dataset_y_pol_in_test[dataset_length_test] for batch_idx in range(batch_num_train): batch_range = batch_idx * batch_size + np.arange(batch_size) batch_x_pol_in_train = dataset_x_pol_in_trainn[batch_range] batch_y_pol_in_train = dataset_y_pol_in_trainn[batch_range] batch_x_pol_des_train = dataset_x_pol_des_trainn[batch_range] # Run optimization op (backprop) and cost op (to get loss value) _, _, batch_x_pol_pred_train = sess.run( [train_op, loss_op, tf_x_pol_pred], feed_dict={ tf_x_pol_in: batch_x_pol_in_train, tf_y_pol_in: batch_y_pol_in_train, tf_x_pol_des: batch_x_pol_des_train.reshape(-1, 1) }) batch_x_pol_pred_train = np.squeeze(batch_x_pol_pred_train) dataset_x_pol_pred_train[batch_range] = batch_x_pol_pred_train if np.any(np.isnan(dataset_x_pol_pred_train)): ValueError('Training prediction vector wasn' 't fully filled!!!') # Estimate the NN on batches batch_num_test = int( np.floor(len(dataset_length_test) / batch_size)) dataset_x_pol_pred_test = np.zeros(batch_num_test * batch_size, dtype='complex128') dataset_x_pol_pred_test[:] = np.nan + 1j * np.nan for batch_idx in range(batch_num_test): batch_range = batch_idx * batch_size + np.arange(batch_size) batch_x_pol_in_test = dataset_x_pol_in_testt[batch_range] batch_y_pol_in_test = dataset_y_pol_in_testt[batch_range] # Run optimization op (backprop) and cost op (to get loss value) batch_x_pol_pred_test, best_w = sess.run( [tf_x_pol_pred, weights_complex], feed_dict={ tf_x_pol_in: batch_x_pol_in_test, tf_y_pol_in: batch_y_pol_in_test }) batch_x_pol_pred_test = np.squeeze(batch_x_pol_pred_test) dataset_x_pol_pred_test[batch_range] = batch_x_pol_pred_test if np.any(np.isnan(dataset_x_pol_pred_test)): ValueError('Testing prediction vector wasn' 't fully filled!!!') x_pol_norm_noNN_train, _ = ph_norm( dataset_x_pol_in_trainn[ np.arange(len(dataset_x_pol_pred_train)), n_taps], dataset_x_pol_des_train[np.arange( len(dataset_x_pol_pred_train))]) x_pol_norm_noNN_test, _ = ph_norm( dataset_x_pol_in_testt[np.arange(len(dataset_x_pol_pred_test)), n_taps], dataset_x_pol_des_test[np.arange( len(dataset_x_pol_pred_test))]) x_pol_norm_NN_train, _ = ph_norm( dataset_x_pol_pred_train, dataset_x_pol_des_train[np.arange( len(dataset_x_pol_pred_train))]) x_pol_norm_NN_test, _ = ph_norm( dataset_x_pol_pred_test, dataset_x_pol_des_test[np.arange( len(dataset_x_pol_pred_test))]) # NMSE estimation NMSE_train_noNN_norm[epoch_idx] = NMSE_est( x_pol_norm_noNN_train, dataset_x_pol_des_train[np.arange( len(dataset_x_pol_pred_train))]) NMSE_train_NN_norm[epoch_idx] = NMSE_est( x_pol_norm_NN_train, dataset_x_pol_des_train[np.arange( len(dataset_x_pol_pred_train))]) NMSE_train_NN[epoch_idx] = NMSE_est( dataset_x_pol_pred_train, dataset_x_pol_des_train[np.arange( len(dataset_x_pol_pred_train))]) NMSE_test_noNN_norm[epoch_idx] = NMSE_est( x_pol_norm_noNN_test, dataset_x_pol_des_test[np.arange( len(dataset_x_pol_pred_test))]) NMSE_test_NN_norm[epoch_idx] = NMSE_est( x_pol_norm_NN_test, dataset_x_pol_des_test[np.arange( len(dataset_x_pol_pred_test))]) NMSE_test_NN[epoch_idx] = NMSE_est( dataset_x_pol_pred_test, dataset_x_pol_des_test[np.arange( len(dataset_x_pol_pred_test))]) # BER estimation BER_train_noNN_norm[epoch_idx] = BER_est( x_pol_norm_noNN_train, dataset_x_pol_des_train[np.arange( len(dataset_x_pol_pred_train))]) BER_train_NN_norm[epoch_idx] = BER_est( x_pol_norm_NN_train, dataset_x_pol_des_train[np.arange( len(dataset_x_pol_pred_train))]) BER_train_NN[epoch_idx] = BER_est( dataset_x_pol_pred_train, dataset_x_pol_des_train[np.arange( len(dataset_x_pol_pred_train))]) BER_test_noNN_norm[epoch_idx] = BER_est( x_pol_norm_noNN_test, dataset_x_pol_des_test[np.arange( len(dataset_x_pol_pred_test))]) BER_test_NN_norm[epoch_idx] = BER_est( x_pol_norm_NN_test, dataset_x_pol_des_test[np.arange( len(dataset_x_pol_pred_test))]) BER_test_NN[epoch_idx] = BER_est( dataset_x_pol_pred_test, dataset_x_pol_des_test[np.arange( len(dataset_x_pol_pred_test))]) if BER_test_NN[epoch_idx] < Best_BER_NN: Best_BER_NN = BER_test_NN[epoch_idx] Best_Epoch_NN = epoch_idx Best_NMSE_NN = NMSE_test_NN[epoch_idx] Best_BER_no_NN = BER_test_noNN_norm[epoch_idx] Best_NMSE_no_NN = NMSE_test_noNN_norm[epoch_idx] elif BER_test_NN[epoch_idx] == Best_BER_NN: if NMSE_test_NN[epoch_idx] < Best_NMSE_NN: Best_BER_NN = BER_test_NN[epoch_idx] Best_Epoch_NN = epoch_idx Best_NMSE_NN = NMSE_test_NN[epoch_idx] Best_BER_no_NN = BER_test_noNN_norm[epoch_idx] Best_NMSE_no_NN = NMSE_test_noNN_norm[epoch_idx] optt = (Best_BER_no_NN - Best_BER_NN) * 100 / Best_BER_no_NN print('################## Improvement', np.round(optt, 3), ' % ########################') optt = (Best_BER_no_NN - Best_BER_NN) * 100 / Best_BER_no_NN sess.close() now = datetime.now() dt_string = now.strftime("%d/%m/%Y %H:%M:%S") if optt > 0: Q_prop_ref2 = 20 * np.log10( np.sqrt(2) * special.erfcinv(2 * Best_BER_NN)) print( '######################################################################', dt_string) print('################## Improvement', np.round(optt, 3), ' % ########################') print("NMSE x with NN", Best_NMSE_NN, "BER x with NN", Best_BER_NN, "NMSE x without NN", Best_NMSE_no_NN, "BER x without NN", Best_BER_no_NN, "best Epoch", Best_Epoch_NN) print("delay= ", n_taps, "n11= ", node_layer11, "n12 =", node_layer12, "n13 =", node_layer13, "n21 =", node_layer21, "n22 =", node_layer22, "n23 =", node_layer23) print('Best Qfactor', Q_prop_ref2) print( '######################################################################' ) else: print( '######################################################################', dt_string) print('################## NO Improvement ') print( '######################################################################' ) # the optimizer aims for the lowest score, so we return our negative accuracy return -optt
def multilayer_perceptron(x_pol_tapped, y_pol_tapped): # Estimates nonlinear distortion made onto X-pol by X- and Y-pols def act_cust(x): return tf.nn.tanh(x) log_x_pol_adap_tap = tf.math.log( tf.matmul(x_pol_tapped, weights_complex['w1'])) abs_sq_x_pol_adap_tap = tf.pow( tf.math.abs(tf.matmul(x_pol_tapped, weights_complex['w2'])), 2) abs_sq_y_pol_adap_tap = tf.pow( tf.math.abs((tf.matmul(y_pol_tapped, weights_complex['w3']))), 2) x_pol_nl_ph_rot = tf.multiply( tf.cast(abs_sq_x_pol_adap_tap, tf.complex128), tf.cast(tf.complex(x_gamma_accum_r, x_gamma_accum_i), tf.complex128)) y_pol_nl_ph_rot = tf.multiply( tf.cast(abs_sq_y_pol_adap_tap, tf.complex128), tf.cast(tf.complex(y_gamma_accum_r, y_gamma_accum_i), tf.complex128)) layer_21_linear = tf.matmul(x_pol_tapped, weights_complex['w4']) + tf.matmul( y_pol_tapped, weights_complex['w5']) layer_21_real = act_cust(tf.math.real(layer_21_linear)) layer_21_imag = act_cust(tf.math.imag(layer_21_linear)) layer_21 = tf.complex(layer_21_real, layer_21_imag) layer_22_linear = tf.matmul(layer_21, weights_complex['w6']) layer_22_real = act_cust(tf.math.real(layer_22_linear)) layer_22_imag = act_cust(tf.math.imag(layer_22_linear)) layer_22 = tf.complex(layer_22_real, layer_22_imag) layer_23_linear = tf.matmul(layer_22, weights_complex['w7']) layer_23_real = (act_cust(tf.math.real(layer_23_linear))) layer_23_imag = (act_cust(tf.math.imag(layer_23_linear))) Xi = tf.complex(layer_23_real, layer_23_imag) layer_11_linear = tf.matmul(x_pol_tapped, weights_complex['w9']) + tf.matmul( y_pol_tapped, weights_complex['w10']) layer_11_real = act_cust(tf.math.real(layer_11_linear)) layer_11_imag = act_cust(tf.math.imag(layer_11_linear)) layer_11 = tf.complex(layer_11_real, layer_11_imag) layer_12_linear = tf.matmul(layer_11, weights_complex['w11']) layer_12_real = act_cust(tf.math.real(layer_12_linear)) layer_12_imag = act_cust(tf.math.imag(layer_12_linear)) layer_12 = tf.complex(layer_12_real, layer_12_imag) layer_13_linear = tf.matmul(layer_12, weights_complex['w12']) layer_13_real = (act_cust(tf.math.real(layer_13_linear))) layer_13_imag = (act_cust(tf.math.imag(layer_13_linear))) Theta = tf.complex(layer_13_real, layer_13_imag) # Output out_layer1 = tf.exp( log_x_pol_adap_tap + x_pol_nl_ph_rot + y_pol_nl_ph_rot + tf.matmul(Theta, weights_complex['w13'])) + tf.matmul( Xi, weights_complex['w8']) return out_layer1
def test(self): b = 12 plt.ion() self.fgt = plt.figure() self.fs0_gt = plt.imshow(np.random.rand(2048 * 2496).reshape( (2048, 2496)), cmap='gray') plt.title('GT S0') self.fgt.canvas.flush_events() self.fgen = plt.figure() self.fs0_gen = plt.imshow(np.random.rand(2048 * 2496).reshape( (2048, 2496)), cmap='gray') plt.title('Gen S0') self.fgen.canvas.flush_events() for batch_i, (full_image, gt_img, ugrid_name, stokes_name) in enumerate(self.load_test_data()): count = 1 print('') print('[Test Image %s]' % (ugrid_name)) full_image_channels = 1 full_image_rows, full_image_cols = full_image.shape print('[Image Rows %s]' % (str(full_image_rows))) print('[Image Cols %s]' % (str(full_image_cols))) print('[Image Depth %s]' % (str(full_image_channels))) sub_image_rows = self.img_rows sub_image_cols = self.img_cols row_iterations = (np.floor(full_image_rows / (sub_image_rows - (2 * b + 3)))).astype(np.int) * 2 col_iterations = (np.floor(full_image_cols / (sub_image_cols - (2 * b + 3)))).astype(np.int) * 2 print('[Row Iterations %s]' % (str(row_iterations))) print('[Col Iterations %s]' % (str(col_iterations))) print('[Overlap %s]' % (str(b))) image_count = 1 print('[Processing Sub Images %d of %d]' % (count, row_iterations * col_iterations)) sub_image = np.zeros( (1, sub_image_rows, sub_image_cols, self.input_channels), dtype=np.float64) tmp = np.zeros((sub_image_rows, sub_image_cols), dtype=np.float64) gen_image = np.zeros( (1, sub_image_rows, sub_image_cols, self.output_channels), dtype=np.float64) stokes_image = np.zeros((full_image_rows, full_image_cols, 3), dtype=np.float64) ys = 0 ye = 0 breakoutflag = 0 for r in range(row_iterations + 1): if (breakoutflag): break xs = 0 xe = 0 if r == 0: ys = 0 ye = sub_image_rows - 1 else: ys = ye - 3 - 2 * b ye = ys + sub_image_rows - 1 for c in range(col_iterations + 1): if c == 0: xs = 0 xe = sub_image_cols - 1 else: xs = xe - 3 - 2 * b xe = xs + sub_image_cols - 1 if ye >= full_image_rows: if xe >= full_image_cols: ye = full_image_rows - 1 ys = ye - sub_image_rows + 1 xe = full_image_cols - 1 xs = xe - sub_image_cols + 1 x = tf.cast( tf.reduce_sum( tf.abs(full_image[ys:ye + 1, xs:xe + 1])), tf.complex128) tmp = tf.signal.fft2d( tf.cast(full_image[ys:ye + 1, xs:xe + 1], tf.complex128)) / x sub_image[0, :, :, 0] = tf.cast(tf.math.real(tmp), dtype=tf.float64) sub_image[0, :, :, 1] = tf.cast(tf.math.imag(tmp), dtype=tf.float64) gen_image[:, :, :, :] = self.generator.predict( sub_image) stokes_image[ys:ye + 1, xs:xe + 1, 0] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 0], gen_image[0, :, :, 1]) * x), dtype=tf.float64).numpy() stokes_image[ys:ye + 1, xs:xe + 1, 1] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 2], gen_image[0, :, :, 3]) * x), dtype=tf.float64).numpy() stokes_image[ys:ye + 1, xs:xe + 1, 2] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 4], gen_image[0, :, :, 5]) * x), dtype=tf.float64).numpy() breakoutflag = 1 break else: ye = full_image_rows - 1 ys = ye - sub_image_rows + 1 if xe >= full_image_cols: xe = full_image_cols - 1 xs = xe - sub_image_cols + 1 x = tf.cast( tf.reduce_sum( tf.abs(full_image[ys:ye + 1, xs:xe + 1])), tf.complex128) tmp = tf.signal.fft2d( tf.cast(full_image[ys:ye + 1, xs:xe + 1], tf.complex128)) / x sub_image[0, :, :, 0] = tf.cast(tf.math.real(tmp), dtype=tf.float64) sub_image[0, :, :, 1] = tf.cast(tf.math.imag(tmp), dtype=tf.float64) gen_image[:, :, :, :] = self.generator.predict( sub_image) stokes_image[ys:ye + 1, xs:xe + 1, 0] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 0], gen_image[0, :, :, 1]) * x), dtype=tf.float64).numpy() stokes_image[ys:ye + 1, xs:xe + 1, 1] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 2], gen_image[0, :, :, 3]) * x), dtype=tf.float64).numpy() stokes_image[ys:ye + 1, xs:xe + 1, 2] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 4], gen_image[0, :, :, 5]) * x), dtype=tf.float64).numpy() if (ye == full_image_rows - 1): breakoutflag = 1 break x = tf.cast( tf.reduce_sum(tf.abs(full_image[ys:ye + 1, xs:xe + 1])), tf.complex128) tmp = tf.signal.fft2d( tf.cast(full_image[ys:ye + 1, xs:xe + 1], tf.complex128)) / x sub_image[0, :, :, 0] = tf.cast(tf.math.real(tmp), dtype=tf.float64) sub_image[0, :, :, 1] = tf.cast(tf.math.imag(tmp), dtype=tf.float64) if (r == 0): if (c == 0): gen_image[:, :, :, :] = self.generator.predict( sub_image) stokes_image[ys:ye + 1, xs:xe + 1, 0] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 0], gen_image[0, :, :, 1]) * x), dtype=tf.float64).numpy() stokes_image[ys:ye + 1, xs:xe + 1, 1] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 2], gen_image[0, :, :, 3]) * x), dtype=tf.float64).numpy() stokes_image[ys:ye + 1, xs:xe + 1, 2] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 4], gen_image[0, :, :, 5]) * x), dtype=tf.float64).numpy() else: gen_image[:, :, :, :] = self.generator.predict( sub_image) stokes_image[ys:ye + 1, xs + b:xe + 1, 0] = tf.cast( tf.signal.ifft2d( tf.complex( gen_image[0, :, :, 0], gen_image[0, :, :, 1]) * x)[:, b::], dtype=tf.float64).numpy() stokes_image[ys:ye + 1, xs + b:xe + 1, 1] = tf.cast( tf.signal.ifft2d( tf.complex( gen_image[0, :, :, 2], gen_image[0, :, :, 3]) * x)[:, b::], dtype=tf.float64).numpy() stokes_image[ys:ye + 1, xs + b:xe + 1, 2] = tf.cast( tf.signal.ifft2d( tf.complex( gen_image[0, :, :, 4], gen_image[0, :, :, 5]) * x)[:, b::], dtype=tf.float64).numpy() elif (c == 0): gen_image[:, :, :, :] = self.generator.predict( sub_image) stokes_image[ys + b:ye + 1, xs:xe + 1, 0] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 0], gen_image[0, :, :, 1]) * x)[b::, :], dtype=tf.float64).numpy() stokes_image[ys + b:ye + 1, xs:xe + 1, 1] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 2], gen_image[0, :, :, 3]) * x)[b::, :], dtype=tf.float64).numpy() stokes_image[ys + b:ye + 1, xs:xe + 1, 2] = tf.cast( tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 4], gen_image[0, :, :, 5]) * x)[b::, :], dtype=tf.float64).numpy() else: gen_image[:, :, :, :] = self.generator.predict( sub_image) stokes_image[ys + b:ye + 1, xs + b:xe + 1, 0] = tf.cast(tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 0], gen_image[0, :, :, 1]) * x)[b::, b::], dtype=tf.float64).numpy() stokes_image[ys + b:ye + 1, xs + b:xe + 1, 1] = tf.cast(tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 2], gen_image[0, :, :, 3]) * x)[b::, b::], dtype=tf.float64).numpy() stokes_image[ys + b:ye + 1, xs + b:xe + 1, 2] = tf.cast(tf.signal.ifft2d( tf.complex(gen_image[0, :, :, 4], gen_image[0, :, :, 5]) * x)[b::, b::], dtype=tf.float64).numpy() system('clear') print('[Test Image %s]' % (ugrid_name)) print('[Image Rows %s]' % (str(full_image_rows))) print('[Image Cols %s]' % (str(full_image_cols))) print('[Image Depth %s]' % (str(full_image_channels))) print('[Row Iterations %d of %s]' % (r, str(row_iterations))) print('[Col Iterations %d of %s]' % (c, str(col_iterations))) print('[Overlap %s]' % (str(b))) print('[Processing Sub Images %d of %d]' % (count, row_iterations * col_iterations)) count = count + 1 save_name = '%sgenerated_in_fft_out_stokes_%s' % (saveFolder, stokes_name) sio.savemat(save_name, {'stokes': stokes_image.astype(np.float64)}) image_count = image_count + 1 self.fs0_gt.set_data( self.imgscale(gt_img[:, :, 0], stype="statistical")) self.fgt.canvas.flush_events() self.fs0_gen.set_data( self.imgscale(stokes_image[:, :, 0].astype(np.float64), stype="statistical")) self.fgen.canvas.flush_events()
def train(self): start_time = datetime.datetime.now() valid = np.ones((self.minibatchSize, ) + self.disc_patch) fake = np.zeros((self.minibatchSize, ) + self.disc_patch) disc_acc = np.zeros([self.epochs]) disc_loss = np.zeros([self.epochs]) gen_loss = np.zeros([self.epochs]) d_loss = 0.0 elapsed_time = datetime.datetime.now() - start_time prev_elapsed_time = elapsed_time system('clear') print('Training...') print('Time: %s' % (elapsed_time)) plt.ion() self.fgt = plt.figure() self.fs0_gt = plt.imshow(np.random.rand( self.img_rows * self.img_cols).reshape( (self.img_rows, self.img_cols)), cmap='gray') plt.title('GT S0 FFT') self.fgt.canvas.flush_events() self.fgt2 = plt.figure() self.fs0_gt2 = plt.imshow(np.random.rand( self.img_rows * self.img_cols).reshape( (self.img_rows, self.img_cols)), cmap='gray') plt.title('GT S0') self.fgt2.canvas.flush_events() self.fgen = plt.figure() self.fs0_gen = plt.imshow(np.random.rand( self.img_rows * self.img_cols).reshape( (self.img_rows, self.img_cols)), cmap='gray') plt.title('Gen S0 FFT') self.fgen.canvas.flush_events() self.fgen2 = plt.figure() self.fs0_gen2 = plt.imshow(np.random.rand( self.img_rows * self.img_cols).reshape( (self.img_rows, self.img_cols)), cmap='gray') plt.title('Gen S0') self.fgen2.canvas.flush_events() gen_img = np.zeros((self.minibatchSize, self.img_rows, self.img_cols, self.output_channels)) for epoch in range(epochs): g_avg = 0 d_avg = 0 count = 0 for batch_i, (input_img, gt_img, ugrid_name, stokes_name) in enumerate( self.load_training_data_fft()): gen_img = self.generator.predict(input_img) d_loss_real = self.discriminator.train_on_batch( [gt_img, input_img], valid) d_loss_fake = self.discriminator.train_on_batch( [gen_img, input_img], fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) g_loss = self.combined.train_on_batch([gt_img, input_img], [valid, gt_img]) elapsed_time = datetime.datetime.now() - start_time g_avg = g_avg + g_loss[0] d_avg = d_avg + 100 * d_loss[1] count = count + 1 now = datetime.datetime.now() system('clear') print('Training...') print('Time: %s' % (elapsed_time)) print('[Epoch %d/%d]' % (epoch + 1, epochs)) print('[G Loss: %d]' % (g_avg / count)) print('[D Accuracy: %d]' % (d_avg / count)) print('[Timestamp: %s]' % (str(now.strftime('%m/%d/%Y %H:%M:%S')))) prev_elapsed_time = elapsed_time if (epoch + 1) % self.sample_interval == 0: stokes_image = tf.clip_by_value( tf.cast(tf.signal.ifft2d( tf.complex(gt_img[0, :, :, 0], gt_img[0, :, :, 1])), dtype=tf.float64), 0, 2**16).numpy() self.fs0_gt2.set_data( self.imgscale(stokes_image, stype="statistical")) self.fgt2.canvas.flush_events() stokes_image = tf.clip_by_value( tf.cast(tf.signal.ifft2d( tf.complex(gen_img[0, :, :, 0], gen_img[0, :, :, 1])), dtype=tf.float64), 0, 2**16).numpy() self.fs0_gen2.set_data( self.imgscale(stokes_image, stype="statistical")) self.fgen2.canvas.flush_events() self.fs0_gt.set_data( self.imgscale(np.log10(1 + np.abs( tf.signal.fftshift( tf.complex(gt_img[0, :, :, 0], gt_img[0, :, :, 1])).numpy())), stype="statistical")) self.fgt.canvas.flush_events() self.fs0_gen.set_data( self.imgscale(np.log10(1 + np.abs( tf.signal.fftshift( tf.complex(gen_img[0, :, :, 0], gen_img[0, :, :, 1])).numpy())), stype="statistical")) self.fgen.canvas.flush_events() self.save_model(epoch + 1)