Python tensorflow.compat.v1.maximum() Examples
The following are 30
code examples of tensorflow.compat.v1.maximum().
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.compat.v1
, or try the search function
.
Example #1
Source File: learning_rate_schedules.py From mesh with Apache License 2.0 | 6 votes |
def truncated_rsqrt(step, total_train_steps, warmup_steps=10000): """Noam's favorite learning-rate schedule. rsqrt(max(step_num, warmup_steps) Args: step: a tf.scalar representing the step we want the learning rate for. total_train_steps: a number, the total number of training steps. warmup_steps: a number Returns: a tf.Scalar, the learning rate for the step. """ del total_train_steps step_num = tf.cast(step, tf.float32) return tf.math.rsqrt(tf.maximum(step_num, warmup_steps))
Example #2
Source File: autoencoders.py From tensor2tensor with Apache License 2.0 | 6 votes |
def bottleneck(self, x): hparams = self.hparams z_size = hparams.bottleneck_bits x_shape = common_layers.shape_list(x) with tf.variable_scope("vae"): mu = tf.layers.dense(x, z_size, name="mu") if hparams.mode != tf.estimator.ModeKeys.TRAIN: return mu, 0.0 # No sampling or kl loss on eval. log_sigma = tf.layers.dense(x, z_size, name="log_sigma") epsilon = tf.random_normal(x_shape[:-1] + [z_size]) z = mu + tf.exp(log_sigma / 2) * epsilon kl = 0.5 * tf.reduce_mean( tf.expm1(log_sigma) + tf.square(mu) - log_sigma, axis=-1) free_bits = z_size // 4 kl_loss = tf.reduce_mean(tf.maximum(kl - free_bits, 0.0)) return z, kl_loss * hparams.kl_beta
Example #3
Source File: t2t_model.py From tensor2tensor with Apache License 2.0 | 6 votes |
def average_sharded_losses(sharded_losses): """Average losses across datashards. Args: sharded_losses: list<dict<str loss_name, Tensor loss>>. The loss can be a single Tensor or a 2-tuple (numerator and denominator). Returns: losses: dict<str loss_name, Tensor avg_loss> """ losses = {} for loss_name in sorted(sharded_losses[0]): all_shards = [shard_losses[loss_name] for shard_losses in sharded_losses] if isinstance(all_shards[0], tuple): sharded_num, sharded_den = zip(*all_shards) mean_loss = ( tf.add_n(sharded_num) / tf.maximum( tf.cast(1.0, sharded_den[0].dtype), tf.add_n(sharded_den))) else: mean_loss = tf.reduce_mean(all_shards) losses[loss_name] = mean_loss return losses
Example #4
Source File: slicenet.py From tensor2tensor with Apache License 2.0 | 6 votes |
def rank_loss(sentence_emb, image_emb, margin=0.2): """Experimental rank loss, thanks to kkurach@ for the code.""" with tf.name_scope("rank_loss"): # Normalize first as this is assumed in cosine similarity later. sentence_emb = tf.nn.l2_normalize(sentence_emb, 1) image_emb = tf.nn.l2_normalize(image_emb, 1) # Both sentence_emb and image_emb have size [batch, depth]. scores = tf.matmul(image_emb, tf.transpose(sentence_emb)) # [batch, batch] diagonal = tf.diag_part(scores) # [batch] cost_s = tf.maximum(0.0, margin - diagonal + scores) # [batch, batch] cost_im = tf.maximum( 0.0, margin - tf.reshape(diagonal, [-1, 1]) + scores) # [batch, batch] # Clear diagonals. batch_size = tf.shape(sentence_emb)[0] empty_diagonal_mat = tf.ones_like(cost_s) - tf.eye(batch_size) cost_s *= empty_diagonal_mat cost_im *= empty_diagonal_mat return tf.reduce_mean(cost_s) + tf.reduce_mean(cost_im)
Example #5
Source File: area_attention.py From tensor2tensor with Apache License 2.0 | 6 votes |
def lengths_to_area_mask(feature_length, length, max_area_size): """Generates a non-padding mask for areas based on lengths. Args: feature_length: a tensor of [batch_size] length: the length of the batch max_area_size: the maximum area size considered Returns: mask: a tensor in shape of [batch_size, num_areas] """ paddings = tf.cast(tf.expand_dims( tf.logical_not( tf.sequence_mask(feature_length, maxlen=length)), 2), tf.float32) _, _, area_sum, _, _ = compute_area_features(paddings, max_area_width=max_area_size) mask = tf.squeeze(tf.logical_not(tf.cast(area_sum, tf.bool)), [2]) return mask
Example #6
Source File: multi_problem.py From tensor2tensor with Apache License 2.0 | 6 votes |
def get_max_num_classes(self): """Compute the maximum number of classes any subtask has. This is useful for modifying the size of the softmax to include the output labels for the classification tasks. Currently, labels from different tasks are overloaded. Returns: num: Highest number of output classes in any text classification sub-task within this MultiProblem. """ num = 0 for task in self.task_list: if hasattr(task, "num_classes"): if num < task.num_classes: num = task.num_classes return num
Example #7
Source File: grouping_regularizers.py From morph-net with Apache License 2.0 | 6 votes |
def __init__(self, regularizers_to_group): """Creates an instance. Args: regularizers_to_group: A list of generic_regularizers.OpRegularizer objects.Their regularization_vector (alive_vector) are expected to be of the same length. Raises: ValueError: regularizers_to_group is not of length at least 2. """ if len(regularizers_to_group) < 2: raise ValueError('Groups must be of at least size 2.') first = regularizers_to_group[0] regularization_vector = first.regularization_vector alive_vector = first.alive_vector for index in range(1, len(regularizers_to_group)): regularizer = regularizers_to_group[index] regularization_vector = tf.maximum(regularization_vector, regularizer.regularization_vector) alive_vector = tf.logical_or(alive_vector, regularizer.alive_vector) self._regularization_vector = regularization_vector self._alive_vector = alive_vector
Example #8
Source File: layers.py From tf-slim with Apache License 2.0 | 6 votes |
def _lower_bound(inputs, bound, name=None): """Same as tf.maximum, but with helpful gradient for inputs < bound. The gradient is overwritten so that it is passed through if the input is not hitting the bound. If it is, only gradients that push `inputs` higher than the bound are passed through. No gradients are passed through to the bound. Args: inputs: input tensor bound: lower bound for the input tensor name: name for this op Returns: tf.maximum(inputs, bound) """ with ops.name_scope(name, 'GDNLowerBound', [inputs, bound]) as scope: inputs = ops.convert_to_tensor(inputs, name='inputs') bound = ops.convert_to_tensor(bound, name='bound') with ops.get_default_graph().gradient_override_map( {'Maximum': 'GDNLowerBound'}): return math_ops.maximum(inputs, bound, name=scope)
Example #9
Source File: region_similarity_calculator.py From Object_Detection_Tracking with Apache License 2.0 | 6 votes |
def intersection(boxlist1, boxlist2, scope=None): """Compute pairwise intersection areas between boxes. Args: boxlist1: BoxList holding N boxes boxlist2: BoxList holding M boxes scope: name scope. Returns: a tensor with shape [N, M] representing pairwise intersections """ with tf.name_scope(scope, 'Intersection'): y_min1, x_min1, y_max1, x_max1 = tf.split( value=boxlist1.get(), num_or_size_splits=4, axis=1) y_min2, x_min2, y_max2, x_max2 = tf.split( value=boxlist2.get(), num_or_size_splits=4, axis=1) all_pairs_min_ymax = tf.minimum(y_max1, tf.transpose(y_max2)) all_pairs_max_ymin = tf.maximum(y_min1, tf.transpose(y_min2)) intersect_heights = tf.maximum(0.0, all_pairs_min_ymax - all_pairs_max_ymin) all_pairs_min_xmax = tf.minimum(x_max1, tf.transpose(x_max2)) all_pairs_max_xmin = tf.maximum(x_min1, tf.transpose(x_min2)) intersect_widths = tf.maximum(0.0, all_pairs_min_xmax - all_pairs_max_xmin) return intersect_heights * intersect_widths
Example #10
Source File: relative_bounds.py From interval-bound-propagation with Apache License 2.0 | 6 votes |
def apply_linear(self, wrapper, w, b): """Propagates the bounds through a linear layer. Args: wrapper: Contains prior bounds from a previous iteration. w: 2D tensor of shape (input_size, output_size) containing weights for the linear layer. b: 1D tensor of shape (output_size) containing biases for the linear layer, or `None` if no bias. Returns: Output bounds. """ w_pos = tf.maximum(w, 0) w_neg = tf.minimum(w, 0) lb = (tf.matmul(self.lower_offset, w_pos) + tf.matmul(self.upper_offset, w_neg)) ub = (tf.matmul(self.upper_offset, w_pos) + tf.matmul(self.lower_offset, w_neg)) nominal_out = tf.matmul(self.nominal, w) if b is not None: nominal_out += b return RelativeIntervalBounds(lb, ub, nominal_out)
Example #11
Source File: crown.py From interval-bound-propagation with Apache License 2.0 | 6 votes |
def concretize(self): """Returns lower and upper interval bounds.""" lb = ub = None if self.lower is not None: lb = ( tf.einsum('nsi,ni->ns', self._reshape_to_rank(tf.maximum(self.lower.w, 0), 3), self._reshape_to_rank(self.lower.lower, 2)) + tf.einsum('nsi,ni->ns', self._reshape_to_rank(tf.minimum(self.lower.w, 0), 3), self._reshape_to_rank(self.lower.upper, 2))) lb += self.lower.b if self.upper is not None: ub = ( tf.einsum('nsi,ni->ns', self._reshape_to_rank(tf.maximum(self.upper.w, 0), 3), self._reshape_to_rank(self.upper.upper, 2)) + tf.einsum('nsi,ni->ns', self._reshape_to_rank(tf.minimum(self.upper.w, 0), 3), self._reshape_to_rank(self.upper.lower, 2))) ub += self.upper.b return bounds.IntervalBounds(lb, ub)
Example #12
Source File: crown.py From interval-bound-propagation with Apache License 2.0 | 6 votes |
def apply_increasing_monotonic_fn(self, wrapper, fn, *args): """Propagate CROWN bounds backward through a increasing monotonic fn.""" # Function _get_monotonic_fn_bound returns matrix and bias term for linear # relaxation. (ub_scaling_matrix, lb_scaling_matrix, ub_bias, lb_bias) = self._get_monotonic_fn_bound(wrapper, fn) def _propagate_monotonic_fn(bound, ub_mult, lb_mult): # Matrix multiplication by a diagonal matrix. new_bound_w = ub_mult * ub_scaling_matrix + lb_mult * lb_scaling_matrix # Matrix vector product for the bias term. ub_bias or lb_bias might be 0 # or a constant, or need broadcast. They will be handled optimally. b = self._matvec(ub_mult, ub_bias) + self._matvec(lb_mult, lb_bias) return fastlin.LinearExpression(w=new_bound_w, b=bound.b + b, lower=wrapper.input_bounds.lower, upper=wrapper.input_bounds.upper) # Multiplies w to upper or lower scaling terms according to its sign. ub_expr = _propagate_monotonic_fn( self.upper, tf.maximum(self.upper.w, 0), tf.minimum(self.upper.w, 0)) if self.upper else None lb_expr = _propagate_monotonic_fn( self.lower, tf.minimum(self.lower.w, 0), tf.maximum(self.lower.w, 0)) if self.lower else None return BackwardBounds(lb_expr, ub_expr)
Example #13
Source File: fastlin.py From interval-bound-propagation with Apache License 2.0 | 6 votes |
def _concretize_bounds(lower, upper): """Returns lower and upper interval bounds.""" if len(lower.b.shape) == 2: equation = 'ijk,ij->ik' elif len(lower.b.shape) == 3: equation = 'ijnc,ij->inc' elif len(lower.b.shape) == 4: equation = 'ijhwc,ij->ihwc' else: raise NotImplementedError('Shape unsupported: {}'.format(lower.b.shape)) lb = (tf.einsum(equation, tf.maximum(lower.w, 0), lower.lower) + tf.einsum(equation, tf.minimum(lower.w, 0), lower.upper) + lower.b) ub = (tf.einsum(equation, tf.maximum(upper.w, 0), upper.upper) + tf.einsum(equation, tf.minimum(upper.w, 0), upper.lower) + upper.b) return lb, ub
Example #14
Source File: quantile_adaptive_clip_sum_query.py From privacy with Apache License 2.0 | 6 votes |
def get_noised_result(self, sample_state, global_state): """See base class.""" noised_vectors, sum_state = self._sum_query.get_noised_result( sample_state.sum_state, global_state.sum_state) del sum_state # To be set explicitly later when we know the new clip. new_l2_norm_clip, new_quantile_estimator_state = ( self._quantile_estimator_query.get_noised_result( sample_state.quantile_estimator_state, global_state.quantile_estimator_state)) new_l2_norm_clip = tf.maximum(new_l2_norm_clip, 0.0) new_sum_stddev = new_l2_norm_clip * global_state.noise_multiplier new_sum_query_state = self._sum_query.make_global_state( l2_norm_clip=new_l2_norm_clip, stddev=new_sum_stddev) new_global_state = self._GlobalState( global_state.noise_multiplier, new_sum_query_state, new_quantile_estimator_state) return noised_vectors, new_global_state
Example #15
Source File: quantile_estimator_query.py From privacy with Apache License 2.0 | 5 votes |
def get_noised_result(self, sample_state, global_state): """See base class.""" below_estimate_result, new_below_estimate_state = ( self._below_estimate_query.get_noised_result( sample_state, global_state.below_estimate_state)) # Unshift below_estimate percentile by 0.5. (See comment in initializer.) below_estimate = below_estimate_result + 0.5 # Protect against out-of-range estimates. below_estimate = tf.minimum(1.0, tf.maximum(0.0, below_estimate)) # Loss function is convex, with derivative in [-1, 1], and minimized when # the true quantile matches the target. loss_grad = below_estimate - global_state.target_quantile update = global_state.learning_rate * loss_grad if self._geometric_update: new_estimate = global_state.current_estimate * tf.math.exp(-update) else: new_estimate = global_state.current_estimate - update new_global_state = global_state._replace( current_estimate=new_estimate, below_estimate_state=new_below_estimate_state) return new_estimate, new_global_state
Example #16
Source File: fastlin.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def apply_conv1d(self, wrapper, w, b, padding, stride): w_pos = tf.maximum(w, 0) w_neg = tf.minimum(w, 0) lb = self._add_expression( self._conv1d_expression(self.lower, w_pos, padding, stride), self._conv1d_expression(self.upper, w_neg, padding, stride)) lb = self._add_bias(lb, b) ub = self._add_expression( self._conv1d_expression(self.upper, w_pos, padding, stride), self._conv1d_expression(self.lower, w_neg, padding, stride)) ub = self._add_bias(ub, b) return SymbolicBounds(lb, ub).with_priors(wrapper.output_bounds)
Example #17
Source File: fastlin.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def concretize(self): """Returns lower and upper interval bounds.""" if self._concretized is None: # Construct once and cache. lb, ub = self._concretize_bounds(self.lower, self.upper) # Apply intersections with prior runs. if self._prior_bounds is not None: lb = tf.maximum(lb, self._prior_bounds.lower) ub = tf.minimum(ub, self._prior_bounds.upper) self._concretized = basic_bounds.IntervalBounds(lb, ub) return self._concretized
Example #18
Source File: crown.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def _get_monotonic_fn_bound(self, wrapper, fn): """Compute CROWN upper and lower linear bounds for a given function fn.""" # Get lower and upper bounds from forward IBP pass. lb, ub = wrapper.input_bounds.lower, wrapper.input_bounds.upper if fn.__name__ == 'relu': # CROWN upper and lower linear bounds for ReLU. f_lb = tf.minimum(lb, 0) f_ub = tf.maximum(ub, 0) # When both ub and lb are very close to 0 we might have NaN issue, # so we have to avoid this happening. f_ub = tf.maximum(f_ub, f_lb + 1e-8) # CROWN upper/lower scaling matrices and biases. ub_scaling_matrix = f_ub / (f_ub - f_lb) ub_bias = -f_lb * ub_scaling_matrix # Expand dimension for using broadcast later. ub_scaling_matrix = tf.expand_dims(ub_scaling_matrix, 1) lb_scaling_matrix = tf.cast(tf.greater(ub_scaling_matrix, .5), dtype=tf.float32) lb_bias = 0. # For 'apply' fn we need to differentiate them through the wrapper. elif isinstance(wrapper, verifiable_wrapper.ImageNormWrapper): inner_module = wrapper.inner_module ub_scaling_matrix = lb_scaling_matrix = inner_module.scale ub_bias = - inner_module.offset * inner_module.scale lb_bias = ub_bias else: raise NotImplementedError('monotonic fn {} is not supported ' 'by BackwardBounds'.format(fn.__name__)) return ub_scaling_matrix, lb_scaling_matrix, ub_bias, lb_bias
Example #19
Source File: data_aug_lib.py From mesh with Apache License 2.0 | 5 votes |
def _gen_rand_mask(ratio_mean, ratio_stddev, scale, shape, smoothness=0): """Generate a binary mask.""" scale = max(scale, 1) ratio = tf.random.normal( shape=[], mean=ratio_mean, stddev=ratio_stddev) low_bound = tf.maximum(0.0, ratio_mean - 2 * ratio_stddev) up_bound = tf.minimum(1.0, ratio_mean + 2 * ratio_stddev) percentil_q = tf.cast( 100.0 * tf.clip_by_value(ratio, low_bound, up_bound), tf.int32) pattern = _rand_noise(0.0, 1.0, scale, shape) if smoothness > 0: smoothness = int(smoothness) // 2 * 2 + 1 pattern = tf.expand_dims(tf.expand_dims(pattern, 0), -1) pattern = tf.nn.conv3d( pattern, filter=tf.ones([smoothness, smoothness, smoothness, 1, 1]), strides=[1, 1, 1, 1, 1], padding='SAME', dilations=[1, 1, 1, 1, 1]) pattern = tf.reduce_sum(pattern, 0) pattern = tf.reduce_sum(pattern, -1) thres = tfp.stats.percentile(pattern, q=percentil_q) rand_mask = tf.less(pattern, thres) return rand_mask
Example #20
Source File: learning_rate.py From tensor2tensor with Apache License 2.0 | 5 votes |
def _legacy_sqrt_decay(step): """Decay like 1 / sqrt(step), multiplied by 500 to normalize.""" return 500.0 / tf.sqrt(tf.maximum(step, 1.0))
Example #21
Source File: relative_bounds.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def apply_batch_norm(self, wrapper, mean, variance, scale, bias, epsilon): """Propagates the bounds through a batch norm layer. Args: wrapper: Contains prior bounds from a previous iteration. mean: Learnt batch mean. variance: Learnt batch variance. scale: Trained component-wise scale variable. bias: Trained component-wise bias variable. epsilon: Epsilon for avoiding instability when `variance` is very small. Returns: Output bounds. """ lb = tf.nn.batch_normalization(self.lower_offset, tf.zeros_like(mean), variance, None, scale, epsilon) ub = tf.nn.batch_normalization(self.upper_offset, tf.zeros_like(mean), variance, None, scale, epsilon) # It's just possible that the batchnorm's scale is negative. lb, ub = tf.minimum(lb, ub), tf.maximum(lb, ub) nominal_out = tf.nn.batch_normalization(self.nominal, mean, variance, bias, scale, epsilon) return RelativeIntervalBounds(lb, ub, nominal_out)
Example #22
Source File: relative_bounds.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def apply_conv2d(self, wrapper, w, b, padding, strides): """Propagates the bounds through a 2D convolution layer. Args: wrapper: Contains prior bounds from a previous iteration. w: 4D tensor of shape (kernel_height, kernel_width, input_channels, output_channels) containing weights for the convolution. b: 1D tensor of shape (output_channels) containing biases for the convolution, or `None` if no bias. padding: `"VALID"` or `"SAME"`, the convolution's padding algorithm. strides: Integer list of length N: `[vertical_stride, horizontal_stride]`. Returns: Output bounds. """ w_pos = tf.maximum(w, 0) w_neg = tf.minimum(w, 0) lb = (tf.nn.convolution(self.lower_offset, w_pos, padding=padding, strides=strides) + tf.nn.convolution(self.upper_offset, w_neg, padding=padding, strides=strides)) ub = (tf.nn.convolution(self.upper_offset, w_pos, padding=padding, strides=strides) + tf.nn.convolution(self.lower_offset, w_neg, padding=padding, strides=strides)) nominal_out = tf.nn.convolution(self.nominal, w, padding=padding, strides=strides) if b is not None: nominal_out += b return RelativeIntervalBounds(lb, ub, nominal_out)
Example #23
Source File: relative_bounds.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def apply_conv1d(self, wrapper, w, b, padding, stride): """Propagates the bounds through a 1D convolution layer. Args: wrapper: Contains prior bounds from a previous iteration. w: 3D tensor of shape (kernel_length, input_channels, output_channels) containing weights for the convolution. b: 1D tensor of shape (output_channels) containing biases for the convolution, or `None` if no bias. padding: `"VALID"` or `"SAME"`, the convolution's padding algorithm. stride: Integer stride. Returns: Output bounds. """ w_pos = tf.maximum(w, 0) w_neg = tf.minimum(w, 0) lb = (tf.nn.conv1d(self.lower_offset, w_pos, padding=padding, stride=stride) + tf.nn.conv1d(self.upper_offset, w_neg, padding=padding, stride=stride)) ub = (tf.nn.conv1d(self.upper_offset, w_pos, padding=padding, stride=stride) + tf.nn.conv1d(self.lower_offset, w_neg, padding=padding, stride=stride)) nominal_out = tf.nn.conv1d(self.nominal, w, padding=padding, stride=stride) if b is not None: nominal_out += b return RelativeIntervalBounds(lb, ub, nominal_out)
Example #24
Source File: adafactor.py From tensor2tensor with Apache License 2.0 | 5 votes |
def _parameter_scale(self, var): """Estimate the scale of the parameters from the current values. We include a minimum value of 0.001 to give it a chance to escape 0 if it was zero-initialized. Instead of using the value, we could impute the scale from the shape, as initializers do. Args: var: a variable or Tensor. Returns: a Scalar """ return tf.maximum(reduce_rms(var), self._epsilon2)
Example #25
Source File: anchors.py From Object_Detection_Tracking with Apache License 2.0 | 5 votes |
def __init__(self, min_level, max_level, num_scales, aspect_ratios, anchor_scale, image_size): """Constructs multiscale RetinaNet anchors. Args: min_level: integer number of minimum level of the output feature pyramid. max_level: integer number of maximum level of the output feature pyramid. num_scales: integer number representing intermediate scales added on each level. For instances, num_scales=2 adds two additional anchor scales [2^0, 2^0.5] on each level. aspect_ratios: list of tuples representing the aspect ratio anchors added on each level. For instances, aspect_ratios = [(1, 1), (1.4, 0.7), (0.7, 1.4)] adds three anchors on each level. anchor_scale: float number representing the scale of size of the base anchor to the feature stride 2^level. image_size: integer number or tuple of integer number of input image size. """ self.min_level = min_level self.max_level = max_level self.num_scales = num_scales self.aspect_ratios = aspect_ratios self.anchor_scale = anchor_scale if isinstance(image_size, int): self.image_size = (image_size, image_size) else: self.image_size = image_size self.feat_sizes = utils.get_feat_sizes(image_size, max_level) self.config = self._generate_configs() self.boxes = self._generate_boxes()
Example #26
Source File: anchors.py From Object_Detection_Tracking with Apache License 2.0 | 5 votes |
def _generate_anchor_configs(feat_sizes, min_level, max_level, num_scales, aspect_ratios): """Generates mapping from output level to a list of anchor configurations. A configuration is a tuple of (num_anchors, scale, aspect_ratio). Args: feat_sizes: list of dict of integer numbers of feature map sizes. min_level: integer number of minimum level of the output feature pyramid. max_level: integer number of maximum level of the output feature pyramid. num_scales: integer number representing intermediate scales added on each level. For instances, num_scales=2 adds two additional anchor scales [2^0, 2^0.5] on each level. aspect_ratios: list of tuples representing the aspect ratio anchors added on each level. For instances, aspect_ratios = [(1, 1), (1.4, 0.7), (0.7, 1.4)] adds three anchors on each level. Returns: anchor_configs: a dictionary with keys as the levels of anchors and values as a list of anchor configuration. """ anchor_configs = {} for level in range(min_level, max_level + 1): anchor_configs[level] = [] for scale_octave in range(num_scales): for aspect in aspect_ratios: anchor_configs[level].append( ((feat_sizes[0]['height'] / float(feat_sizes[level]['height']), feat_sizes[0]['width'] / float(feat_sizes[level]['width'])), scale_octave / float(num_scales), aspect)) return anchor_configs
Example #27
Source File: vanilla_gan.py From tensor2tensor with Apache License 2.0 | 5 votes |
def lrelu(input_, leak=0.2, name="lrelu"): return tf.maximum(input_, leak * input_, name=name)
Example #28
Source File: dataloader.py From Object_Detection_Tracking with Apache License 2.0 | 5 votes |
def set_training_random_scale_factors(self, scale_min, scale_max): """Set the parameters for multiscale training.""" # Select a random scale factor. random_scale_factor = tf.random_uniform([], scale_min, scale_max) scaled_y = tf.cast(random_scale_factor * self._output_size[0], tf.int32) scaled_x = tf.cast(random_scale_factor * self._output_size[1], tf.int32) # Recompute the accurate scale_factor using rounded scaled image size. height = tf.cast(tf.shape(self._image)[0], tf.float32) width = tf.cast(tf.shape(self._image)[1], tf.float32) image_scale_y = tf.cast(scaled_y, tf.float32) / height image_scale_x = tf.cast(scaled_x, tf.float32) / width image_scale = tf.minimum(image_scale_x, image_scale_y) # Select non-zero random offset (x, y) if scaled image is larger than # self._output_size. scaled_height = tf.cast(height * image_scale, tf.int32) scaled_width = tf.cast(width * image_scale, tf.int32) offset_y = tf.cast(scaled_height - self._output_size[0], tf.float32) offset_x = tf.cast(scaled_width - self._output_size[1], tf.float32) offset_y = tf.maximum(0.0, offset_y) * tf.random_uniform([], 0, 1) offset_x = tf.maximum(0.0, offset_x) * tf.random_uniform([], 0, 1) offset_y = tf.cast(offset_y, tf.int32) offset_x = tf.cast(offset_x, tf.int32) self._image_scale = image_scale self._scaled_height = scaled_height self._scaled_width = scaled_width self._crop_offset_x = offset_x self._crop_offset_y = offset_y
Example #29
Source File: preprocessor.py From Object_Detection_Tracking with Apache License 2.0 | 5 votes |
def _compute_new_dynamic_size(image, min_dimension, max_dimension): """Compute new dynamic shape for resize_to_range method.""" image_shape = tf.shape(image) orig_height = tf.to_float(image_shape[0]) orig_width = tf.to_float(image_shape[1]) num_channels = image_shape[2] orig_min_dim = tf.minimum(orig_height, orig_width) # Calculates the larger of the possible sizes min_dimension = tf.constant(min_dimension, dtype=tf.float32) large_scale_factor = min_dimension / orig_min_dim # Scaling orig_(height|width) by large_scale_factor will make the smaller # dimension equal to min_dimension, save for floating point rounding errors. # For reasonably-sized images, taking the nearest integer will reliably # eliminate this error. large_height = tf.to_int32(tf.round(orig_height * large_scale_factor)) large_width = tf.to_int32(tf.round(orig_width * large_scale_factor)) large_size = tf.stack([large_height, large_width]) if max_dimension: # Calculates the smaller of the possible sizes, use that if the larger # is too big. orig_max_dim = tf.maximum(orig_height, orig_width) max_dimension = tf.constant(max_dimension, dtype=tf.float32) small_scale_factor = max_dimension / orig_max_dim # Scaling orig_(height|width) by small_scale_factor will make the larger # dimension equal to max_dimension, save for floating point rounding # errors. For reasonably-sized images, taking the nearest integer will # reliably eliminate this error. small_height = tf.to_int32(tf.round(orig_height * small_scale_factor)) small_width = tf.to_int32(tf.round(orig_width * small_scale_factor)) small_size = tf.stack([small_height, small_width]) new_size = tf.cond( tf.to_float(tf.reduce_max(large_size)) > max_dimension, lambda: small_size, lambda: large_size) else: new_size = large_size return tf.stack(tf.unstack(new_size) + [num_channels])
Example #30
Source File: fastlin.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def apply_conv2d(self, wrapper, w, b, padding, strides): w_pos = tf.maximum(w, 0) w_neg = tf.minimum(w, 0) lb = self._add_expression( self._conv2d_expression(self.lower, w_pos, padding, strides), self._conv2d_expression(self.upper, w_neg, padding, strides)) lb = self._add_bias(lb, b) ub = self._add_expression( self._conv2d_expression(self.upper, w_pos, padding, strides), self._conv2d_expression(self.lower, w_neg, padding, strides)) ub = self._add_bias(ub, b) return SymbolicBounds(lb, ub).with_priors(wrapper.output_bounds)