Python tensorflow.stack() Examples
The following are 30
code examples of tensorflow.stack().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.

Example #1
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 6 votes |
def _create_autosummary_var(name, value_expr): assert not _autosummary_finalized v = tf.cast(value_expr, tf.float32) if v.shape.ndims is 0: v = [v, np.float32(1.0)] elif v.shape.ndims is 1: v = [tf.reduce_sum(v), tf.cast(tf.shape(v)[0], tf.float32)] else: v = [tf.reduce_sum(v), tf.reduce_prod(tf.cast(tf.shape(v), tf.float32))] v = tf.cond(tf.is_finite(v[0]), lambda: tf.stack(v), lambda: tf.zeros(2)) with tf.control_dependencies(None): var = tf.Variable(tf.zeros(2)) # [numerator, denominator] update_op = tf.cond(tf.is_variable_initialized(var), lambda: tf.assign_add(var, v), lambda: tf.assign(var, v)) if name in _autosummary_vars: _autosummary_vars[name].append(var) else: _autosummary_vars[name] = [var] return update_op #---------------------------------------------------------------------------- # Call filewriter.add_summary() with all summaries in the default graph, # automatically finalizing and merging them on the first call.
Example #2
Source File: unet.py From spleeter with MIT License | 6 votes |
def softmax_unet(input_tensor, instruments, params={}): """ Apply softmax to multitrack unet in order to have mask suming to one. :param input_tensor: Tensor to apply blstm to. :param instruments: Iterable that provides a collection of instruments. :param params: (Optional) dict of BLSTM parameters. :returns: Created output tensor dict. """ logit_mask_list = [] for instrument in instruments: out_name = f'{instrument}_spectrogram' logit_mask_list.append( apply_unet( input_tensor, output_name=out_name, params=params, output_mask_logit=True)) masks = Softmax(axis=4)(tf.stack(logit_mask_list, axis=4)) output_dict = {} for i, instrument in enumerate(instruments): out_name = f'{instrument}_spectrogram' output_dict[out_name] = Multiply(name=out_name)([ masks[..., i], input_tensor]) return output_dict
Example #3
Source File: decoder.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 6 votes |
def loop_decode(self): # decoder_initial_state: Tuple Tensor (c,h) of size [batch_size x cell.state_size] # decoder_first_input: Tensor [batch_size x cell.state_size] # Loop the decoding process and collect results s,i = self.decoder_initial_state, tf.cast(self.decoder_first_input,tf.float32) for step in range(self.seq_length): s, i = self.decode(s,i,step) # Return to start self.positions.append(self.first_city) # Stack visited indices self.positions=tf.stack(self.positions,axis=1) # [Batch,seq_length+1] # Sum log_softmax over output steps self.log_softmax=tf.add_n(self.log_softmax) # [Batch,seq_length] # Stack attending & pointing distribution self.attending=tf.stack(self.attending,axis=1) # [Batch,seq_length,seq_length] self.pointing=tf.stack(self.pointing,axis=1) # [Batch,seq_length,seq_length] # Return stacked lists of visited_indices and log_softmax for backprop return self.positions,self.log_softmax
Example #4
Source File: layers.py From ARU-Net with GNU General Public License v2.0 | 6 votes |
def images_to_sequence(tensor): """Convert a batch of images into a batch of sequences. Args: tensor: a (num_images, height, width, depth) tensor Returns: (width, num_images*height, depth) sequence tensor """ transposed = tf.transpose(tensor, [2, 0, 1, 3]) shapeT = tf.shape(transposed) shapeL = transposed.get_shape().as_list() # Calculate the ouput size of the upsampled tensor n_shape = tf.stack([ shapeT[0], shapeT[1]*shapeT[2], shapeL[3] ]) reshaped = tf.reshape(transposed, n_shape) return reshaped
Example #5
Source File: layers.py From ARU-Net with GNU General Public License v2.0 | 6 votes |
def sequence_to_images(tensor, num_batches): """Convert a batch of sequences into a batch of images. Args: tensor: (num_steps, num_batchesRNN, depth) sequence tensor num_batches: the number of image batches Returns: (num_batches, height, width, depth) tensor """ shapeT = tf.shape(tensor) shapeL = tensor.get_shape().as_list() # Calculate the ouput size of the upsampled tensor height = tf.to_int32(shapeT[1] / num_batches) n_shape = tf.stack([ shapeT[0], num_batches, height, shapeL[2] ]) reshaped = tf.reshape(tensor, n_shape) return tf.transpose(reshaped, [1, 2, 0, 3])
Example #6
Source File: vision_baseline_lstm.py From DOTA_models with Apache License 2.0 | 6 votes |
def lstm_online(cell_fn, num_steps, inputs, state, varscope): # inputs is B x num_steps x C, C channels. # state is 2 tuple with B x 1 x C1, B x 1 x C2 # Output state is always B x 1 x C inputs = tf.unstack(inputs, axis=1, num=num_steps) state = tf.unstack(state, axis=1, num=1)[0] outputs = [] if num_steps > 1: varscope.reuse_variables() for s in range(num_steps): output, state = cell_fn(inputs[s], state) outputs.append(output) outputs = tf.stack(outputs, axis=1) state = tf.stack([state], axis=1) return outputs, state
Example #7
Source File: network_units.py From DOTA_models with Apache License 2.0 | 6 votes |
def convert_network_state_tensorarray(tensorarray): """Converts a source TensorArray to a source Tensor. Performs a permutation between the steps * [stride, D] shape of a source TensorArray and the (flattened) [stride * steps, D] shape of a source Tensor. The TensorArrays used during recurrence have an additional zeroth step that needs to be removed. Args: tensorarray: TensorArray object to be converted. Returns: Tensor object after conversion. """ tensor = tensorarray.stack() # Results in a [steps, stride, D] tensor. tensor = tf.slice(tensor, [1, 0, 0], [-1, -1, -1]) # Lop off the 0th step. tensor = tf.transpose(tensor, [1, 0, 2]) # Switch steps and stride. return tf.reshape(tensor, [-1, tf.shape(tensor)[2]])
Example #8
Source File: ops.py From DOTA_models with Apache License 2.0 | 6 votes |
def one_hot_encoding(labels, num_classes, scope=None): """Transform numeric labels into onehot_labels. Args: labels: [batch_size] target labels. num_classes: total number of classes. scope: Optional scope for name_scope. Returns: one hot encoding of the labels. """ with tf.name_scope(scope, 'OneHotEncoding', [labels]): batch_size = labels.get_shape()[0] indices = tf.expand_dims(tf.range(0, batch_size), 1) labels = tf.cast(tf.expand_dims(labels, 1), indices.dtype) concated = tf.concat(axis=1, values=[indices, labels]) onehot_labels = tf.sparse_to_dense( concated, tf.stack([batch_size, num_classes]), 1.0, 0.0) onehot_labels.set_shape([batch_size, num_classes]) return onehot_labels
Example #9
Source File: box_coder_test.py From DOTA_models with Apache License 2.0 | 6 votes |
def test_batch_decode(self): mock_anchor_corners = tf.constant( [[0, 0.1, 0.2, 0.3], [0.2, 0.4, 0.4, 0.6]], tf.float32) mock_anchors = box_list.BoxList(mock_anchor_corners) mock_box_coder = MockBoxCoder() expected_boxes = [[[0.0, 0.1, 0.5, 0.6], [0.5, 0.6, 0.7, 0.8]], [[0.1, 0.2, 0.3, 0.4], [0.7, 0.8, 0.9, 1.0]]] encoded_boxes_list = [mock_box_coder.encode( box_list.BoxList(tf.constant(boxes)), mock_anchors) for boxes in expected_boxes] encoded_boxes = tf.stack(encoded_boxes_list) decoded_boxes = box_coder.batch_decode( encoded_boxes, mock_box_coder, mock_anchors) with self.test_session() as sess: decoded_boxes_result = sess.run(decoded_boxes) self.assertAllClose(expected_boxes, decoded_boxes_result)
Example #10
Source File: distributions.py From DOTA_models with Apache License 2.0 | 6 votes |
def __init__(self, x_bxu, z_size, name, var_min=0.0): """Create an input dependent diagonal Gaussian distribution. Args: x: The input tensor from which the mean and variance are computed, via a linear transformation of x. I.e. mu = Wx + b, log(var) = Mx + c z_size: The size of the distribution. name: The name to prefix to learned variables. var_min (optional): Minimal variance allowed. This is an additional way to control the amount of information getting through the stochastic layer. """ size_bxn = tf.stack([tf.shape(x_bxu)[0], z_size]) self.mean_bxn = mean_bxn = linear(x_bxu, z_size, name=(name+"/mean")) logvar_bxn = linear(x_bxu, z_size, name=(name+"/logvar")) if var_min > 0.0: logvar_bxn = tf.log(tf.exp(logvar_bxn) + var_min) self.logvar_bxn = logvar_bxn self.noise_bxn = noise_bxn = tf.random_normal(size_bxn) self.noise_bxn.set_shape([None, z_size]) self.sample_bxn = mean_bxn + tf.exp(0.5 * logvar_bxn) * noise_bxn
Example #11
Source File: expert_utils.py From fine-lm with MIT License | 6 votes |
def combine(self, expert_out, multiply_by_gates=True): """Sum together the expert output, multiplied by the corresponding gates. Args: expert_out: a list of `num_experts` `Tensor`s, each with shape `[expert_batch_size_i, <extra_output_dims>]`. multiply_by_gates: a boolean. Returns: a list of num_datashards `Tensor`s with shapes `[batch_size[d], <extra_output_dims>]`. """ expert_part_sizes = tf.unstack( tf.stack([d.part_sizes for d in self._dispatchers]), num=self._ep.n, axis=1) # list of lists of shape [num_experts][num_datashards] expert_output_parts = self._ep(tf.split, expert_out, expert_part_sizes) expert_output_parts_t = transpose_list_of_lists(expert_output_parts) def my_combine(dispatcher, parts): return dispatcher.combine( common_layers.convert_gradient_to_tensor(tf.concat(parts, 0)), multiply_by_gates=multiply_by_gates) return self._dp(my_combine, self._dispatchers, expert_output_parts_t)
Example #12
Source File: select_dim_value.py From post--memorization-in-rnns with MIT License | 6 votes |
def select_dim_value(x, indices, name=None): with tf.name_scope(name, "select-dim-value", values=[x, indices]): # x.shape = (rest..., dims) rest = tf.shape(x)[:-1] dims = tf.shape(x)[-1] size = tf.size(indices, out_type=indices.dtype) # reshape to (size, dims) t = tf.reshape(x, shape=[-1, dims]) # then index as ([1,2,3,...,size], indices.ravel()) nd_indices = tf.stack([ tf.range(0, size, dtype=indices.dtype), tf.reshape(indices, shape=[-1]) ], axis=1) t = tf.gather_nd(t, indices=nd_indices) # reshape back to (rest...) t = tf.reshape(t, rest) t.set_shape(x.get_shape()[:-1]) return t
Example #13
Source File: box_coder_test.py From object_detector_app with MIT License | 6 votes |
def test_batch_decode(self): mock_anchor_corners = tf.constant( [[0, 0.1, 0.2, 0.3], [0.2, 0.4, 0.4, 0.6]], tf.float32) mock_anchors = box_list.BoxList(mock_anchor_corners) mock_box_coder = MockBoxCoder() expected_boxes = [[[0.0, 0.1, 0.5, 0.6], [0.5, 0.6, 0.7, 0.8]], [[0.1, 0.2, 0.3, 0.4], [0.7, 0.8, 0.9, 1.0]]] encoded_boxes_list = [mock_box_coder.encode( box_list.BoxList(tf.constant(boxes)), mock_anchors) for boxes in expected_boxes] encoded_boxes = tf.stack(encoded_boxes_list) decoded_boxes = box_coder.batch_decode( encoded_boxes, mock_box_coder, mock_anchors) with self.test_session() as sess: decoded_boxes_result = sess.run(decoded_boxes) self.assertAllClose(expected_boxes, decoded_boxes_result)
Example #14
Source File: tf_logits.py From Black-Box-Audio with MIT License | 5 votes |
def compute_mfcc(audio, **kwargs): """ Compute the MFCC for a given audio waveform. This is identical to how DeepSpeech does it, but does it all in TensorFlow so that we can differentiate through it. """ batch_size, size = audio.get_shape().as_list() audio = tf.cast(audio, tf.float32) # 1. Pre-emphasizer, a high-pass filter audio = tf.concat((audio[:, :1], audio[:, 1:] - 0.97*audio[:, :-1], np.zeros((batch_size,1000),dtype=np.float32)), 1) # 2. windowing into frames of 320 samples, overlapping windowed = tf.stack([audio[:, i:i+400] for i in range(0,size-320,160)],1) # 3. Take the FFT to convert to frequency space ffted = tf.spectral.rfft(windowed, [512]) ffted = 1.0 / 512 * tf.square(tf.abs(ffted)) # 4. Compute the Mel windowing of the FFT energy = tf.reduce_sum(ffted,axis=2)+1e-30 filters = np.load("filterbanks.npy").T feat = tf.matmul(ffted, np.array([filters]*batch_size,dtype=np.float32))+1e-30 # 5. Take the DCT again, because why not feat = tf.log(feat) feat = tf.spectral.dct(feat, type=2, norm='ortho')[:,:,:26] # 6. Amplify high frequencies for some reason _,nframes,ncoeff = feat.get_shape().as_list() n = np.arange(ncoeff) lift = 1 + (22/2.)*np.sin(np.pi*n/22) feat = lift*feat width = feat.get_shape().as_list()[1] # 7. And now stick the energy next to the features feat = tf.concat((tf.reshape(tf.log(energy),(-1,width,1)), feat[:, :, 1:]), axis=2) return feat
Example #15
Source File: tf_logits.py From Black-Box-Audio with MIT License | 5 votes |
def get_logits(new_input, length, first=[]): """ Compute the logits for a given waveform. First, preprocess with the TF version of MFC above, and then call DeepSpeech on the features. """ # new_input = tf.Print(new_input, [tf.shape(new_input)]) # We need to init DeepSpeech the first time we're called if first == []: first.append(False) # Okay, so this is ugly again. # We just want it to not crash. tf.app.flags.FLAGS.alphabet_config_path = "DeepSpeech/data/alphabet.txt" DeepSpeech.initialize_globals() print('initialized deepspeech globals') batch_size = new_input.get_shape()[0] # 1. Compute the MFCCs for the input audio # (this is differentable with our implementation above) empty_context = np.zeros((batch_size, 9, 26), dtype=np.float32) new_input_to_mfcc = compute_mfcc(new_input)[:, ::2] features = tf.concat((empty_context, new_input_to_mfcc, empty_context), 1) # 2. We get to see 9 frames at a time to make our decision, # so concatenate them together. features = tf.reshape(features, [new_input.get_shape()[0], -1]) features = tf.stack([features[:, i:i+19*26] for i in range(0,features.shape[1]-19*26+1,26)],1) features = tf.reshape(features, [batch_size, -1, 19*26]) # 3. Whiten the data mean, var = tf.nn.moments(features, axes=[0,1,2]) features = (features-mean)/(var**.5) # 4. Finally we process it with DeepSpeech logits = DeepSpeech.BiRNN(features, length, [0]*10) return logits
Example #16
Source File: interaction.py From icme2019 with MIT License | 5 votes |
def call(self, inputs, **kwargs): if K.ndim(inputs) != 3: raise ValueError( "Unexpected inputs dimensions %d, expect to be 3 dimensions" % (K.ndim(inputs))) querys = tf.tensordot(inputs, self.W_Query, axes=(-1, 0)) # None F D*head_num keys = tf.tensordot(inputs, self.W_key, axes=(-1, 0)) values = tf.tensordot(inputs, self.W_Value, axes=(-1, 0)) # head_num None F D querys = tf.stack(tf.split(querys, self.head_num, axis=2)) keys = tf.stack(tf.split(keys, self.head_num, axis=2)) values = tf.stack(tf.split(values, self.head_num, axis=2)) inner_product = tf.matmul( querys, keys, transpose_b=True) # head_num None F F self.normalized_att_scores = tf.nn.softmax(inner_product) result = tf.matmul(self.normalized_att_scores, values) # head_num None F D result = tf.concat(tf.split(result, self.head_num, ), axis=-1) result = tf.squeeze(result, axis=0) # None F D*head_num if self.use_res: result += tf.tensordot(inputs, self.W_Res, axes=(-1, 0)) result = tf.nn.relu(result) return result
Example #17
Source File: loss.py From disentangling_conditional_gans with MIT License | 5 votes |
def extract_patches(matrix, ws, h, w): val = [] for yo in range(ws): for xo in range(ws): MN = matrix[:, yo:yo+h-ws+1, xo:xo+w-ws+1, :] val.append(MN) win_ids = tf.stack(val, 3) return win_ids #---------------------------------------------------------------------------- # Compute structural loss
Example #18
Source File: qaLSTMNet.py From QA with GNU General Public License v3.0 | 5 votes |
def biLSTMCell(x, hiddenSize): input_x = tf.transpose(x, [1, 0, 2]) input_x = tf.unstack(input_x) lstm_fw_cell = tf.contrib.rnn.BasicLSTMCell(hiddenSize, forget_bias=1.0, state_is_tuple=True) lstm_bw_cell = tf.contrib.rnn.BasicLSTMCell(hiddenSize, forget_bias=1.0, state_is_tuple=True) output, _, _ = tf.contrib.rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, input_x, dtype=tf.float32) output = tf.stack(output) output = tf.transpose(output, [1, 0, 2]) return output
Example #19
Source File: attacks_tf.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _compute_gradients(self, loss_fn, x, unused_optim_state): """Compute gradient estimates using SPSA.""" # Assumes `x` is a list, containing a [1, H, W, C] image assert len(x) == 1 and x[0].get_shape().as_list()[0] == 1 x = x[0] x_shape = x.get_shape().as_list() def body(i, grad_array): delta = self._delta delta_x = self._get_delta(x, delta) delta_x = tf.concat([delta_x, -delta_x], axis=0) loss_vals = tf.reshape( loss_fn(x + delta_x), [2 * self._num_samples] + [1] * (len(x_shape) - 1)) avg_grad = reduce_mean(loss_vals * delta_x, axis=0) / delta avg_grad = tf.expand_dims(avg_grad, axis=0) new_grad_array = grad_array.write(i, avg_grad) return i + 1, new_grad_array def cond(i, _): return i < self._num_iters _, all_grads = tf.while_loop( cond, body, loop_vars=[ 0, tf.TensorArray(size=self._num_iters, dtype=tf_dtype) ], back_prop=False, parallel_iterations=1) avg_grad = reduce_sum(all_grads.stack(), axis=0) return [avg_grad]
Example #20
Source File: __init__.py From spleeter with MIT License | 5 votes |
def _build_mwf_output_waveform(self): """ Perform separation with multichannel Wiener Filtering using Norbert. Note: multichannel Wiener Filtering is not coded in Tensorflow and thus may be quite slow. :returns: dictionary of separated waveforms (key: instrument name, value: estimated waveform of the instrument) """ import norbert # pylint: disable=import-error output_dict = self.model_outputs x = self.stft_feature v = tf.stack( [ pad_and_reshape( output_dict[f'{instrument}_spectrogram'], self._frame_length, self._F)[:tf.shape(x)[0], ...] for instrument in self._instruments ], axis=3) input_args = [v, x] stft_function = tf.py_function( lambda v, x: norbert.wiener(v.numpy(), x.numpy()), input_args, tf.complex64), return { instrument: self._inverse_stft(stft_function[0][:, :, :, k]) for k, instrument in enumerate(self._instruments) }
Example #21
Source File: decoder.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 5 votes |
def loop_decode(self, decoder_initial_state): # decoder_initial_state: Tuple Tensor (c,h) of size [batch_size x cell.state_size] # decoder_first_input: Tensor [batch_size x cell.state_size] # Decoder initial input is depot (start) decoder_first_input = tf.gather(self.h,self.depot_position)[0] # Loop the decoding process and collect results s,i = decoder_initial_state, decoder_first_input for step in range(self.seq_length-1): s, i = self.decode(s,i,step) # Return to depot self.positions.append(self.depot_position) # Stack visited indices self.positions=tf.stack(self.positions,axis=1) # [Batch,seq_length+1] # Sum log_softmax over output steps self.log_softmax=tf.add_n(self.log_softmax) # [Batch,seq_length-1] # Stack attending & pointing distribution self.attending=tf.stack(self.attending,axis=1) # [Batch,seq_length-1,seq_length] self.pointing=tf.stack(self.pointing,axis=1) # [Batch,seq_length-1,seq_length] # Return stacked lists of visited_indices and log_softmax for backprop return self.positions,self.log_softmax, self.attending, self.pointing
Example #22
Source File: layers.py From ARU-Net with GNU General Public License v2.0 | 5 votes |
def deconv2d_bn_lrn_drop(scope_or_name, inputs, kernel_shape, out_shape, subS=2, activation=tf.nn.relu, use_bn=False, use_mvn=False, is_training=True, use_lrn=False, keep_prob=1.0, dropout_maps=False, initOpt=0): with tf.variable_scope(scope_or_name): if initOpt == 0: stddev = np.sqrt(2.0 / (kernel_shape[0] * kernel_shape[1] * kernel_shape[2] + kernel_shape[3])) if initOpt == 1: stddev = 5e-2 if initOpt == 2: stddev = min(np.sqrt(2.0 / (kernel_shape[0] * kernel_shape[1] * kernel_shape[2])),5e-2) kernel = tf.get_variable("weights", kernel_shape, initializer=tf.random_normal_initializer(stddev=stddev)) bias = tf.get_variable("bias", kernel_shape[2], initializer=tf.constant_initializer(value=0.1)) conv=tf.nn.conv2d_transpose(inputs, kernel, out_shape, strides=[1, subS, subS, 1], padding='SAME', name='conv') outputs = tf.nn.bias_add(conv, bias, name='preActivation') if use_bn: # outputs = tf.layers.batch_normalization(outputs, axis=3, training=is_training, name="batchNorm") outputs = batch_norm(outputs, is_training=is_training, scale=True, fused=True, scope="batchNorm") if use_mvn: outputs = feat_norm(outputs, kernel_shape[3]) if activation: outputs = activation(outputs, name='activation') if use_lrn: outputs = tf.nn.local_response_normalization(outputs, name='localResponseNorm') if dropout_maps: conv_shape = tf.shape(outputs) n_shape = tf.stack([conv_shape[0], 1, 1, conv_shape[3]]) outputs = tf.nn.dropout(outputs, keep_prob, noise_shape=n_shape) else: outputs = tf.nn.dropout(outputs, keep_prob) return outputs
Example #23
Source File: vgg_preprocessing.py From DOTA_models with Apache License 2.0 | 5 votes |
def _crop(image, offset_height, offset_width, crop_height, crop_width): """Crops the given image using the provided offsets and sizes. Note that the method doesn't assume we know the input image size but it does assume we know the input image rank. Args: image: an image of shape [height, width, channels]. offset_height: a scalar tensor indicating the height offset. offset_width: a scalar tensor indicating the width offset. crop_height: the height of the cropped image. crop_width: the width of the cropped image. Returns: the cropped (and resized) image. Raises: InvalidArgumentError: if the rank is not 3 or if the image dimensions are less than the crop size. """ original_shape = tf.shape(image) rank_assertion = tf.Assert( tf.equal(tf.rank(image), 3), ['Rank of image must be equal to 3.']) with tf.control_dependencies([rank_assertion]): cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]]) size_assertion = tf.Assert( tf.logical_and( tf.greater_equal(original_shape[0], crop_height), tf.greater_equal(original_shape[1], crop_width)), ['Crop size greater than the image size.']) offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) # Use tf.slice instead of crop_to_bounding box as it accepts tensors to # define the crop size. with tf.control_dependencies([size_assertion]): image = tf.slice(image, offsets, cropped_shape) return tf.reshape(image, cropped_shape)
Example #24
Source File: cmp.py From DOTA_models with Apache License 2.0 | 5 votes |
def running_combine(fss_logits, confs_probs, incremental_locs, incremental_thetas, previous_sum_num, previous_sum_denom, previous_max_denom, map_size, num_steps): # fss_logits is B x N x H x W x C # confs_logits is B x N x H x W x C # incremental_locs is B x N x 2 # incremental_thetas is B x N x 1 # previous_sum_num etc is B x 1 x H x W x C with tf.name_scope('combine_{:d}'.format(num_steps)): running_sum_nums_ = []; running_sum_denoms_ = []; running_max_denoms_ = []; fss_logits_ = tf.unstack(fss_logits, axis=1, num=num_steps) confs_probs_ = tf.unstack(confs_probs, axis=1, num=num_steps) incremental_locs_ = tf.unstack(incremental_locs, axis=1, num=num_steps) incremental_thetas_ = tf.unstack(incremental_thetas, axis=1, num=num_steps) running_sum_num = tf.unstack(previous_sum_num, axis=1, num=1)[0] running_sum_denom = tf.unstack(previous_sum_denom, axis=1, num=1)[0] running_max_denom = tf.unstack(previous_max_denom, axis=1, num=1)[0] for i in range(num_steps): # Rotate the previous running_num and running_denom running_sum_num, running_sum_denom, running_max_denom = rotate_preds( incremental_locs_[i], incremental_thetas_[i], map_size, [running_sum_num, running_sum_denom, running_max_denom], output_valid_mask=False)[0] # print i, num_steps, running_sum_num.get_shape().as_list() running_sum_num = running_sum_num + fss_logits_[i] * confs_probs_[i] running_sum_denom = running_sum_denom + confs_probs_[i] running_max_denom = tf.maximum(running_max_denom, confs_probs_[i]) running_sum_nums_.append(running_sum_num) running_sum_denoms_.append(running_sum_denom) running_max_denoms_.append(running_max_denom) running_sum_nums = tf.stack(running_sum_nums_, axis=1) running_sum_denoms = tf.stack(running_sum_denoms_, axis=1) running_max_denoms = tf.stack(running_max_denoms_, axis=1) return running_sum_nums, running_sum_denoms, running_max_denoms
Example #25
Source File: network_units.py From DOTA_models with Apache License 2.0 | 5 votes |
def create(self, fixed_embeddings, linked_embeddings, context_tensor_arrays, attention_tensor, during_training, stride=None): """Requires |stride|; otherwise see base class.""" # TODO(googleuser): Normalize the arguments to create(). 'stride' # is unused by the recurrent network units, while 'context_tensor_arrays' # and 'attenion_tensor_array' is unused by bulk network units. b/33587044 if stride is None: raise ValueError("PairwiseConvNetwork needs 'stride'") input_tensor = get_input_tensor_with_stride(fixed_embeddings, linked_embeddings, stride) # TODO(googleuser): Add dropout. del context_tensor_arrays, attention_tensor, during_training # Unused. num_steps = tf.shape(input_tensor)[1] arg1 = tf.expand_dims(input_tensor, 1) arg1 = tf.tile(arg1, tf.stack([1, num_steps, 1, 1])) arg2 = tf.expand_dims(input_tensor, 2) arg2 = tf.tile(arg2, tf.stack([1, 1, num_steps, 1])) conv = tf.concat([arg1, arg2], 3) for i in xrange(self._num_layers): with tf.variable_scope('conv%d' % i, reuse=True) as scope: conv = tf.nn.conv2d( conv, self._component.get_variable('weights'), [1, 1, 1, 1], padding='SAME') conv = tf.nn.bias_add(conv, self._component.get_variable('biases')) if i in self._relu_layers: conv = tf.nn.relu(conv, name=scope.name) return [tf.reshape(conv, [-1, num_steps], name='reshape_activations')]
Example #26
Source File: per_example_gradients.py From DOTA_models with Apache License 2.0 | 5 votes |
def __call__(self, w, z_grads): idx = list(self.op.inputs).index(w) # Make sure that `op` was actually applied to `w` assert idx != -1 assert len(z_grads) == len(self.op.outputs) # The following assert may be removed when we are ready to use this # for general purpose code. # This assert is only expected to hold in the contex of our preliminary # MNIST experiments. assert idx == 1 # We expect convolution weights to be arg 1 images, filters = self.op.inputs strides = self.op.get_attr("strides") padding = self.op.get_attr("padding") # Currently assuming that one specifies at most these four arguments and # that all other arguments to conv2d are set to default. conv, w_px = self._PxConv2DBuilder(images, filters, strides, padding) z_grads, = z_grads gradients_list = tf.gradients(conv, w_px, z_grads, colocate_gradients_with_ops= self.colocate_gradients_with_ops, gate_gradients=self.gate_gradients) return tf.stack(gradients_list)
Example #27
Source File: ops.py From DOTA_models with Apache License 2.0 | 5 votes |
def dense_to_sparse_boxes(dense_locations, dense_num_boxes, num_classes): """Converts bounding boxes from dense to sparse form. Args: dense_locations: a [max_num_boxes, 4] tensor in which only the first k rows are valid bounding box location coordinates, where k is the sum of elements in dense_num_boxes. dense_num_boxes: a [max_num_classes] tensor indicating the counts of various bounding box classes e.g. [1, 0, 0, 2] means that the first bounding box is of class 0 and the second and third bounding boxes are of class 3. The sum of elements in this tensor is the number of valid bounding boxes. num_classes: number of classes Returns: box_locations: a [num_boxes, 4] tensor containing only valid bounding boxes (i.e. the first num_boxes rows of dense_locations) box_classes: a [num_boxes] tensor containing the classes of each bounding box (e.g. dense_num_boxes = [1, 0, 0, 2] => box_classes = [0, 3, 3] """ num_valid_boxes = tf.reduce_sum(dense_num_boxes) box_locations = tf.slice(dense_locations, tf.constant([0, 0]), tf.stack([num_valid_boxes, 4])) tiled_classes = [tf.tile([i], tf.expand_dims(dense_num_boxes[i], 0)) for i in range(num_classes)] box_classes = tf.concat(tiled_classes, 0) box_locations.set_shape([None, 4]) return box_locations, box_classes
Example #28
Source File: trainer_test.py From DOTA_models with Apache License 2.0 | 5 votes |
def loss(self, prediction_dict): """Compute scalar loss tensors with respect to provided groundtruth. Calling this function requires that groundtruth tensors have been provided via the provide_groundtruth function. Args: prediction_dict: a dictionary holding predicted tensors Returns: a dictionary mapping strings (loss names) to scalar tensors representing loss values. """ batch_reg_targets = tf.stack( self.groundtruth_lists(fields.BoxListFields.boxes)) batch_cls_targets = tf.stack( self.groundtruth_lists(fields.BoxListFields.classes)) weights = tf.constant( 1.0, dtype=tf.float32, shape=[len(self.groundtruth_lists(fields.BoxListFields.boxes)), 1]) location_losses = self._localization_loss( prediction_dict['box_encodings'], batch_reg_targets, weights=weights) cls_losses = self._classification_loss( prediction_dict['class_predictions_with_background'], batch_cls_targets, weights=weights) loss_dict = { 'localization_loss': tf.reduce_sum(location_losses), 'classification_loss': tf.reduce_sum(cls_losses), } return loss_dict
Example #29
Source File: ssd_meta_arch.py From DOTA_models with Apache License 2.0 | 5 votes |
def _summarize_input(self, groundtruth_boxes_list, match_list): """Creates tensorflow summaries for the input boxes and anchors. This function creates four summaries corresponding to the average number (over images in a batch) of (1) groundtruth boxes, (2) anchors marked as positive, (3) anchors marked as negative, and (4) anchors marked as ignored. Args: groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4] containing corners of the groundtruth boxes. match_list: a list of matcher.Match objects encoding the match between anchors and groundtruth boxes for each image of the batch, with rows of the Match objects corresponding to groundtruth boxes and columns corresponding to anchors. """ num_boxes_per_image = tf.stack( [tf.shape(x)[0] for x in groundtruth_boxes_list]) pos_anchors_per_image = tf.stack( [match.num_matched_columns() for match in match_list]) neg_anchors_per_image = tf.stack( [match.num_unmatched_columns() for match in match_list]) ignored_anchors_per_image = tf.stack( [match.num_ignored_columns() for match in match_list]) tf.summary.scalar('Input/AvgNumGroundtruthBoxesPerImage', tf.reduce_mean(tf.to_float(num_boxes_per_image))) tf.summary.scalar('Input/AvgNumPositiveAnchorsPerImage', tf.reduce_mean(tf.to_float(pos_anchors_per_image))) tf.summary.scalar('Input/AvgNumNegativeAnchorsPerImage', tf.reduce_mean(tf.to_float(neg_anchors_per_image))) tf.summary.scalar('Input/AvgNumIgnoredAnchorsPerImage', tf.reduce_mean(tf.to_float(ignored_anchors_per_image)))
Example #30
Source File: target_assigner.py From DOTA_models with Apache License 2.0 | 5 votes |
def _create_regression_targets(self, anchors, groundtruth_boxes, match): """Returns a regression target for each anchor. Args: anchors: a BoxList representing N anchors groundtruth_boxes: a BoxList representing M groundtruth_boxes match: a matcher.Match object Returns: reg_targets: a float32 tensor with shape [N, box_code_dimension] """ matched_anchor_indices = match.matched_column_indices() unmatched_ignored_anchor_indices = (match. unmatched_or_ignored_column_indices()) matched_gt_indices = match.matched_row_indices() matched_anchors = box_list_ops.gather(anchors, matched_anchor_indices) matched_gt_boxes = box_list_ops.gather(groundtruth_boxes, matched_gt_indices) matched_reg_targets = self._box_coder.encode(matched_gt_boxes, matched_anchors) unmatched_ignored_reg_targets = tf.tile( self._default_regression_target(), tf.stack([tf.size(unmatched_ignored_anchor_indices), 1])) reg_targets = tf.dynamic_stitch( [matched_anchor_indices, unmatched_ignored_anchor_indices], [matched_reg_targets, unmatched_ignored_reg_targets]) # TODO: summarize the number of matches on average. return reg_targets