Python tensorflow.float() Examples
The following are 30
code examples of tensorflow.float().
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: multiple_grid_anchor_generator.py From motion-rcnn with MIT License | 6 votes |
def __init__(self, scales, aspect_ratios=[0.5, 1.0, 2.0], base_anchor_size=None): """Constructs a FpnAnchorGenerator. See the paper by Lin et al. Anchors that are returned by calling the `generate` method on the returned MultipleGridAnchorGenerator object are always in normalized coordinates and clipped to the unit square: (i.e. all coordinates lie in [0, 1]x[0, 1]). Args: num_layers: integer number of grid layers to create anchors for (actual grid sizes passed in at generation time) aspect_ratios: list or tuple of (float) aspect ratios to place on each pyramid location. base_anchor_size: base anchor size as [height, width]. """ self._pyramid_scales = scales box_specs_list = [[(scale, aspect_ratio) for aspect_ratio in aspect_ratios] for scale in scales] super().__init__(box_specs_list, base_anchor_size)
Example #2
Source File: mpnn.py From mpnn with Apache License 2.0 | 6 votes |
def __init__(self, input_dim, output_dim, hparams): """Class used to map final node states to an output vector. Args: input_dim: Dimension of the node states taken as input output_dim: Dimension of the vector valued output of the network hparams: Specifies the architecture of the output neural nets. Relevant hparams for this function: hparams.num_output_hidden_layers: (int) number of hidden layers in the output neural nets hparams.hidden_dim: (int) hidden dim shared by all hidden layers. hparams.activation: (str - 'relu' or 'tanh') indicates what activation fct to use in the neural nets hparams.normalizer: (str - 'layer' or 'none') whether or not to use layer norm in the neural nets hparams.keep_prob: (float) dropout keep prob for the output neural nets """ super(GraphLevelOutput, self).__init__(hparams) self.input_dim = input_dim self.output_dim = output_dim self.init_fprop()
Example #3
Source File: read_data.py From CVTron with Apache License 2.0 | 5 votes |
def distort_randomly_image_color(image_tensor, annotation_tensor, shapes): """Accepts image tensor of (width, height, 3) and returns color distorted image. The function performs random brightness, saturation, hue, contrast change as it is performed for inception model training in TF-Slim (you can find the link below in comments). All the parameters of random variables were originally preserved. There are two regimes for the function to work: fast and slow. Slow one performs only saturation and brightness random change is performed. Parameters ---------- image_tensor : Tensor of size (width, height, 3) of tf.int32 or tf.float Tensor with image with range [0,255] fast_mode : boolean Boolean value representing whether to use fast or slow mode Returns ------- img_float_distorted_original_range : Tensor of size (width, height, 3) of type tf.float. Image Tensor with distorted color in [0,255] intensity range """ fast_mode = False # Make the range to be in [0,1] img_float_zero_one_range = tf.to_float(image_tensor) / 255 # Randomly distort the color of image. There are 4 ways to do it. # Credit: TF-Slim # https://github.com/tensorflow/models/blob/master/slim/preprocessing/inception_preprocessing.py#L224 # Most probably the inception models were trainined using this color augmentation: # https://github.com/tensorflow/models/tree/master/slim#pre-trained-models distorted_image = apply_with_random_selector(img_float_zero_one_range, lambda x, ordering: distort_color(x, ordering, fast_mode=fast_mode), num_cases=4) img_float_distorted_original_range = distorted_image * 255 return img_float_distorted_original_range, annotation_tensor, shapes
Example #4
Source File: attention_cell.py From im2latex with Apache License 2.0 | 5 votes |
def __init__(self, cell, attention_mechanism, dropout, attn_cell_config, num_proj, dtype=tf.float32): """ Args: cell: (RNNCell) attention_mechanism: (AttentionMechanism) dropout: (tf.float) attn_cell_config: (dict) hyper params """ # variables and tensors self._cell = cell self._attention_mechanism = attention_mechanism self._dropout = dropout # hyperparameters and shapes self._n_channels = self._attention_mechanism._n_channels self._dim_e = attn_cell_config["dim_e"] self._dim_o = attn_cell_config["dim_o"] self._num_units = attn_cell_config["num_units"] self._dim_embeddings = attn_cell_config["dim_embeddings"] self._num_proj = num_proj self._dtype = dtype # for RNNCell self._state_size = AttentionState(self._cell._state_size, self._dim_o)
Example #5
Source File: dqn_agent.py From texar with Apache License 2.0 | 5 votes |
def _get_td_error(self, qnet_qvalues, actions, y): return y - tf.reduce_sum(qnet_qvalues * tf.cast(actions, tf.float), axis=1)
Example #6
Source File: augmentation.py From tf-image-segmentation with MIT License | 5 votes |
def distort_randomly_image_color(image_tensor, fast_mode=False): """Accepts image tensor of (width, height, 3) and returns color distorted image. The function performs random brightness, saturation, hue, contrast change as it is performed for inception model training in TF-Slim (you can find the link below in comments). All the parameters of random variables were originally preserved. There are two regimes for the function to work: fast and slow. Slow one performs only saturation and brightness random change is performed. Parameters ---------- image_tensor : Tensor of size (width, height, 3) of tf.int32 or tf.float Tensor with image with range [0,255] fast_mode : boolean Boolean value representing whether to use fast or slow mode Returns ------- img_float_distorted_original_range : Tensor of size (width, height, 3) of type tf.float. Image Tensor with distorted color in [0,255] intensity range """ # Make the range to be in [0,1] img_float_zero_one_range = tf.to_float(image_tensor) / 255 # Randomly distort the color of image. There are 4 ways to do it. # Credit: TF-Slim # https://github.com/tensorflow/models/blob/master/slim/preprocessing/inception_preprocessing.py#L224 # Most probably the inception models were trainined using this color augmentation: # https://github.com/tensorflow/models/tree/master/slim#pre-trained-models distorted_image = apply_with_random_selector(img_float_zero_one_range, lambda x, ordering: distort_color(x, ordering, fast_mode=fast_mode), num_cases=4) img_float_distorted_original_range = distorted_image * 255 return img_float_distorted_original_range
Example #7
Source File: mappers.py From transform with Apache License 2.0 | 5 votes |
def _annotate_buckets(x, bucket_boundaries): """Annotates a bucketized tensor with the boundaries that were applied. Creates a deferred annotation for the specified tensor. Args: x: The tensor to annotate. bucket_boundaries: A tensor of boundaries that were used to bucketize x. """ # The annotations proto currently isn't available in OSS builds, so schema # annotations are not supported. if not common.IS_ANNOTATIONS_PB_AVAILABLE: return from tensorflow_transform import annotations_pb2 # pylint: disable=g-import-not-at-top message_type = annotations_pb2.BucketBoundaries.DESCRIPTOR.full_name # The BucketBoundaries annotation expects a float field. bucket_boundaries = tf.cast(bucket_boundaries, tf.float32) # Some callers provide rank 2 boundaries like [[.25], [.5], [.75], [1.]], # whereas we expect rank 2 boundaries like [[.25, .5, .75, 1.]] bucket_boundaries = tf.reshape(bucket_boundaries, [-1]) bucket_boundaries = tf.expand_dims(bucket_boundaries, 0) size = (tf.shape(bucket_boundaries)[1],) message_proto = tf.raw_ops.EncodeProto(sizes=[size], values=[bucket_boundaries], field_names=['boundaries'], message_type=message_type) assert message_proto.shape == [1] message_proto = message_proto[0] type_url = os.path.join(common.ANNOTATION_PREFIX_URL, message_type) schema_inference.annotate(type_url, message_proto, tensor=x)
Example #8
Source File: mpnn.py From mpnn with Apache License 2.0 | 5 votes |
def __init__(self, input_dim, output_dim, hparams): """Class used to map final node states to an output vector. The output of the fprop method will be a target vector of dimension output_dim which will be invariant to the order of elements in the "node_states" tensor. This implements the "process" block described in https://arxiv.org/pdf/1511.06391.pdf. For more detailed documentation, see set2vec.py. Args: input_dim: Dimension of the node states taken as input output_dim: Dimension of the vector valued output of the network hparams: Specifies the architecture of the output neural nets. Relevant hparams for this function: hparams.num_output_hidden_layers: (int) number of hidden layers in the output neural nets hparams.hidden_dim: (int) hidden dim shared by all hidden layers. hparams.activation: (str - 'relu' or 'tanh') indicates what activation fct to use in the neural nets hparams.normalizer: (str - 'layer' or 'none') whether or not to use layer norm in the neural nets hparams.keep_prob: (float) dropout keep prob for the output neural nets """ super(Set2VecOutput, self).__init__(hparams) self.hparams = hparams self.input_dim = input_dim self.output_dim = output_dim self.init_fprop()
Example #9
Source File: mpnn.py From mpnn with Apache License 2.0 | 5 votes |
def _fprop( self, node_states, adjacency_in, distance, # pylint: disable=unused-argument reuse_graph_tensors=False): """Computes a_t from h_{t-1}, see bottom of page 3 in the paper. Args: node_states: [batch_size, num_nodes, node_dim] tensor (h_{t-1}) adjacency_in (tf.int32): [batch_size, num_nodes, num_nodes] distance (tf.float): [batch_size, num_nodes, num_nodes] NOT USED. reuse_graph_tensors: (boolean) must be set to True the first time that fprop is called so that we can compute the a_in and a_out tensors. Returns: a_t: [batch_size * num_nodes, node_dim] which is the node represenations after a single propgation step This also sets graph_precomputed to True to indicate that part of the graph has been cached and will be reused in future calls of _fprop """ # build the larger A matrices on the first call of _fprop if not reuse_graph_tensors: self._precompute_graph(adjacency_in) return message_pass(node_states, self._a_in, self._a_out, self.hparams.node_dim)
Example #10
Source File: augmentation.py From seg-mentor with MIT License | 5 votes |
def random_horiz_flip(image_tensor, annotation_tensor): """Accepts image tensor and annotation tensor and returns randomly flipped tensors of both. The function performs random flip of image and annotation tensors with probability of 1/2 The flip is performed or not performed for image and annotation consistently, so that annotation matches the image. Parameters ---------- image_tensor : Tensor of size (width, height, 3) Tensor with image annotation_tensor : Tensor of size (width, height, 1) Tensor with annotation Returns ------- randomly_flipped_img : Tensor of size (width, height, 3) of type tf.float. Randomly flipped image tensor randomly_flipped_annotation : Tensor of size (width, height, 1) Randomly flipped annotation tensor """ # Random variable: two possible outcomes (0 or 1) # with a 1 in 2 chance random_var = tf.random_uniform(maxval=2, dtype=tf.int32, shape=[]) randomly_flipped_img = control_flow_ops.cond(pred=tf.equal(random_var, 0), fn1=lambda: tf.image.flip_left_right(image_tensor), fn2=lambda: image_tensor) randomly_flipped_annotation = control_flow_ops.cond(pred=tf.equal(random_var, 0), fn1=lambda: tf.image.flip_left_right(annotation_tensor), fn2=lambda: annotation_tensor) return randomly_flipped_img, randomly_flipped_annotation
Example #11
Source File: read_data.py From CVTron with Apache License 2.0 | 5 votes |
def random_flip_image_and_annotation(image_tensor, annotation_tensor, shapes): """Accepts image tensor and annotation tensor and returns randomly flipped tensors of both. The function performs random flip of image and annotation tensors with probability of 1/2 The flip is performed or not performed for image and annotation consistently, so that annotation matches the image. Parameters ---------- image_tensor : Tensor of size (width, height, 3) Tensor with image annotation_tensor : Tensor of size (width, height, 1) Tensor with annotation Returns ------- randomly_flipped_img : Tensor of size (width, height, 3) of type tf.float. Randomly flipped image tensor randomly_flipped_annotation : Tensor of size (width, height, 1) Randomly flipped annotation tensor """ original_shape = tf.shape(annotation_tensor) # ensure the annotation tensor has shape (width, height, 1) annotation_tensor = tf.cond(tf.rank(annotation_tensor) < 3, lambda: tf.expand_dims(annotation_tensor, axis=2), lambda: annotation_tensor) # Random variable: two possible outcomes (0 or 1) # with a 1 in 2 chance random_var = tf.random_uniform(maxval=2, dtype=tf.int32, shape=[]) randomly_flipped_img = tf.cond(pred=tf.equal(random_var, 0), true_fn=lambda: tf.image.flip_left_right(image_tensor), false_fn=lambda: image_tensor) randomly_flipped_annotation = tf.cond( pred=tf.equal(random_var, 0), true_fn=lambda: tf.image.flip_left_right(annotation_tensor), false_fn=lambda: annotation_tensor) return randomly_flipped_img, tf.reshape(randomly_flipped_annotation, original_shape), shapes
Example #12
Source File: cond_fn.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def q_too_small(agent, state, action, transition_type, environment_steps, num_episodes, q_min=0.5): """True of q is too small. Args: agent: RL agent. state: A [num_state_dims] tensor representing a state. action: Action performed. transition_type: Type of transition after action environment_steps: Number of steps performed by environment. num_episodes: Number of episodes. q_min: Returns true if the qval is less than q_min Returns: cond: Returns an op that evaluates to true if qval is less than q_min. """ del transition_type, environment_steps, num_episodes state_for_reset_agent = tf.stack(state[:-1], tf.constant([0], dtype=tf.float)) qval = agent.BASE_AGENT_CLASS.critic_net( tf.expand_dims(state_for_reset_agent, 0), tf.expand_dims(action, 0))[0, :] cond = tf.greater(tf.constant(q_min), qval) return cond
Example #13
Source File: sequence_encoders.py From ludwig with Apache License 2.0 | 5 votes |
def __call__( self, input_sequence, regularizer, dropout_rate, is_training=True ): """ :param input_sequence: The input sequence fed into the encoder. Shape: [batch x sequence length], type tf.int32 :type input_sequence: Tensor :param regularizer: The regularizer to use for the weights of the encoder. :type regularizer: :param dropout_rate: Tensor (tf.float) of the probability of dropout :type dropout_rate: Tensor :param is_training: Tesnor (tf.bool) specifying if in training mode (important for dropout) :type is_training: Tensor """ # ================ Embeddings ================ embedded_sequence, embedding_size = self.embed_sequence( input_sequence, regularizer, dropout_rate, is_training=True ) hidden = reduce_sequence(embedded_sequence, self.reduce_output) return hidden, embedding_size
Example #14
Source File: sequence_encoders.py From ludwig with Apache License 2.0 | 5 votes |
def __call__( self, input_sequence, regularizer, dropout_rate, is_training=True ): """ :param input_sequence: The input sequence fed into the encoder. Shape: [batch x sequence length], type tf.int32 :type input_sequence: Tensor :param regularizer: The regularizer to use for the weights of the encoder. :type regularizer: :param dropout_rate: Tensor (tf.float) of the probability of dropout :type dropout_rate: Tensor :param is_training: Tesnor (tf.bool) specifying if in training mode (important for dropout) :type is_training: Tensor """ input_sequence = tf.cast(input_sequence, tf.float32) while len(input_sequence.shape) < 3: input_sequence = tf.expand_dims( input_sequence, -1) hidden_size = input_sequence.shape[-1] hidden = reduce_sequence(input_sequence, self.reduce_output) return hidden, hidden_size
Example #15
Source File: cond_fn.py From models with Apache License 2.0 | 5 votes |
def q_too_small(agent, state, action, transition_type, environment_steps, num_episodes, q_min=0.5): """True of q is too small. Args: agent: RL agent. state: A [num_state_dims] tensor representing a state. action: Action performed. transition_type: Type of transition after action environment_steps: Number of steps performed by environment. num_episodes: Number of episodes. q_min: Returns true if the qval is less than q_min Returns: cond: Returns an op that evaluates to true if qval is less than q_min. """ del transition_type, environment_steps, num_episodes state_for_reset_agent = tf.stack(state[:-1], tf.constant([0], dtype=tf.float)) qval = agent.BASE_AGENT_CLASS.critic_net( tf.expand_dims(state_for_reset_agent, 0), tf.expand_dims(action, 0))[0, :] cond = tf.greater(tf.constant(q_min), qval) return cond
Example #16
Source File: h3_encoders.py From ludwig with Apache License 2.0 | 5 votes |
def __call__( self, input_vector, regularizer, dropout_rate, is_training=True ): """ :param input_vector: The input vector fed into the encoder. Shape: [batch x 19], type tf.int8 :type input_vector: Tensor :param regularizer: The regularizer to use for the weights of the encoder. :type regularizer: :param dropout_rate: Tensor (tf.float) of the probability of dropout :type dropout_rate: Tensor :param is_training: Tesnor (tf.bool) specifying if in training mode (important for dropout) :type is_training: Tensor """ # ================ Embeddings ================ embedded_h3, _ = self.h3_embed( input_vector, regularizer, dropout_rate, is_training=is_training ) # ================ RNN ================ hidden, hidden_size = self.recurrent_stack( embedded_h3, regularizer=regularizer, dropout_rate=dropout_rate, is_training=is_training ) return hidden, hidden_size
Example #17
Source File: augmentation.py From seg-mentor with MIT License | 5 votes |
def distort_randomly_image_color(image_tensor, fast_mode=False): """Accepts image tensor of (width, height, 3) and returns color distorted image. The function performs random brightness, saturation, hue, contrast change as it is performed for inception model training in TF-Slim (you can find the link below in comments). All the parameters of random variables were originally preserved. There are two regimes for the function to work: fast and slow. Slow one performs only saturation and brightness random change is performed. Parameters ---------- image_tensor : Tensor of size (width, height, 3) of tf.int32 or tf.float Tensor with image with range [0,255] fast_mode : boolean Boolean value representing whether to use fast or slow mode Returns ------- img_float_distorted_original_range : Tensor of size (width, height, 3) of type tf.float. Image Tensor with distorted color in [0,255] intensity range """ # Make the range to be in [0,1] img_float_zero_one_range = tf.to_float(image_tensor) / 255 # Randomly distort the color of image. There are 4 ways to do it. # Credit: TF-Slim # https://github.com/tensorflow/models/blob/master/slim/preprocessing/inception_preprocessing.py#L224 # Most probably the inception models were trainined using this color augmentation: # https://github.com/tensorflow/models/tree/master/slim#pre-trained-models distorted_image = apply_with_random_selector(img_float_zero_one_range, lambda x, ordering: distort_color(x, ordering, fast_mode=fast_mode), num_cases=4) img_float_distorted_original_range = distorted_image * 255 return img_float_distorted_original_range
Example #18
Source File: anytime_fcn.py From petridishnn with MIT License | 5 votes |
def _parse_inputs(self, inputs): # NOTE # label_img is always NHWC/NHW/channel_last # If label_img is not one-hot, the distribution doesn't include void. # label_img is 0-vec for void labels image, label_img = inputs if not self.options.is_label_one_hot: # From now on label_img is tf.float one hot, void has 0-vector. # because we assume void >=num_classes label_img = tf.one_hot(label_img, self.num_classes, axis=-1) def nonvoid_mask(prob_img, name=None): mask = tf.cast(tf.greater(tf.reduce_sum(prob_img, axis=-1), self.options.eval_threshold), dtype=tf.float32) mask = tf.reshape(mask, [-1], name=name) # TODO is this actually beneficial; and which KeepProb to use? #mask = Dropout(name, mask, keep_prob=0.5) return mask def flatten_label(prob_img, name=None): return tf.reshape(prob_img, [-1, self.num_classes], name=name) l_mask = [] l_label = [] l_dyn_hw = [] label_img = tf.identity(label_img, name='label_img_0') n_label_scales = self.n_pools + 1 if not self.do_scale_feat_to_label else 1 for pi in range(n_label_scales): l_mask.append(nonvoid_mask(label_img, 'eval_mask_{}'.format(pi))) l_label.append(flatten_label(label_img, 'label_{}'.format(pi))) img_shape = tf.shape(label_img) l_dyn_hw.append([img_shape[1], img_shape[2]]) if pi == self.n_pools: break label_img = AvgPooling('label_img_{}'.format(pi+1), label_img, 2, \ padding='same', data_format='channels_last') return image, [l_label, l_mask, l_dyn_hw]
Example #19
Source File: augmentation.py From tf-image-segmentation with MIT License | 5 votes |
def flip_randomly_left_right_image_with_annotation(image_tensor, annotation_tensor): """Accepts image tensor and annotation tensor and returns randomly flipped tensors of both. The function performs random flip of image and annotation tensors with probability of 1/2 The flip is performed or not performed for image and annotation consistently, so that annotation matches the image. Parameters ---------- image_tensor : Tensor of size (width, height, 3) Tensor with image annotation_tensor : Tensor of size (width, height, 1) Tensor with annotation Returns ------- randomly_flipped_img : Tensor of size (width, height, 3) of type tf.float. Randomly flipped image tensor randomly_flipped_annotation : Tensor of size (width, height, 1) Randomly flipped annotation tensor """ # Random variable: two possible outcomes (0 or 1) # with a 1 in 2 chance random_var = tf.random_uniform(maxval=2, dtype=tf.int32, shape=[]) randomly_flipped_img = control_flow_ops.cond(pred=tf.equal(random_var, 0), fn1=lambda: tf.image.flip_left_right(image_tensor), fn2=lambda: image_tensor) randomly_flipped_annotation = control_flow_ops.cond(pred=tf.equal(random_var, 0), fn1=lambda: tf.image.flip_left_right(annotation_tensor), fn2=lambda: annotation_tensor) return randomly_flipped_img, randomly_flipped_annotation
Example #20
Source File: multiple_grid_anchor_generator.py From MBMD with MIT License | 4 votes |
def __init__(self, box_specs_list, base_anchor_size=None, clip_window=None): """Constructs a MultipleGridAnchorGenerator. To construct anchors, at multiple grid resolutions, one must provide a list of feature_map_shape_list (e.g., [(8, 8), (4, 4)]), and for each grid size, a corresponding list of (scale, aspect ratio) box specifications. For example: box_specs_list = [[(.1, 1.0), (.1, 2.0)], # for 8x8 grid [(.2, 1.0), (.3, 1.0), (.2, 2.0)]] # for 4x4 grid To support the fully convolutional setting, we pass grid sizes in at generation time, while scale and aspect ratios are fixed at construction time. Args: box_specs_list: list of list of (scale, aspect ratio) pairs with the outside list having the same number of entries as feature_map_shape_list (which is passed in at generation time). base_anchor_size: base anchor size as [height, width] (length-2 float tensor, default=[256, 256]). clip_window: a tensor of shape [4] specifying a window to which all anchors should be clipped. If clip_window is None, then no clipping is performed. Raises: ValueError: if box_specs_list is not a list of list of pairs ValueError: if clip_window is not either None or a tensor of shape [4] """ if isinstance(box_specs_list, list) and all( [isinstance(list_item, list) for list_item in box_specs_list]): self._box_specs = box_specs_list else: raise ValueError('box_specs_list is expected to be a ' 'list of lists of pairs') if base_anchor_size is None: base_anchor_size = tf.constant([256, 256], dtype=tf.float32) self._base_anchor_size = base_anchor_size if clip_window is not None and clip_window.get_shape().as_list() != [4]: raise ValueError('clip_window must either be None or a shape [4] tensor') self._clip_window = clip_window self._scales = [] self._aspect_ratios = [] for box_spec in self._box_specs: if not all([isinstance(entry, tuple) and len(entry) == 2 for entry in box_spec]): raise ValueError('box_specs_list is expected to be a ' 'list of lists of pairs') scales, aspect_ratios = zip(*box_spec) self._scales.append(scales) self._aspect_ratios.append(aspect_ratios)
Example #21
Source File: training.py From tf-image-segmentation with MIT License | 4 votes |
def get_labels_from_annotation(annotation_tensor, class_labels): """Returns tensor of size (width, height, num_classes) derived from annotation tensor. The function returns tensor that is of a size (width, height, num_classes) which is derived from annotation tensor with sizes (width, height) where value at each position represents a class. The functions requires a list with class values like [0, 1, 2 ,3] -- they are used to derive labels. Derived values will be ordered in the same way as the class numbers were provided in the list. Last value in the aforementioned list represents a value that indicate that the pixel should be masked out. So, the size of num_classes := len(class_labels) - 1. Parameters ---------- annotation_tensor : Tensor of size (width, height) Tensor with class labels for each element class_labels : list of ints List that contains the numbers that represent classes. Last value in the list should represent the number that was used for masking out. Returns ------- labels_2d_stacked : Tensor of size (width, height, num_classes). Tensor with labels for each pixel. """ # Last value in the classes list should show # which number was used in the annotation to mask out # the ambigious regions or regions that should not be # used for training. # TODO: probably replace class_labels list with some custom object valid_entries_class_labels = class_labels[:-1] # Stack the binary masks for each class labels_2d = map(lambda x: tf.equal(annotation_tensor, x), valid_entries_class_labels) # Perform the merging of all of the binary masks into one matrix labels_2d_stacked = tf.stack(labels_2d, axis=2) # Convert tf.bool to tf.float # Later on in the labels and logits will be used # in tf.softmax_cross_entropy_with_logits() function # where they have to be of the float type. labels_2d_stacked_float = tf.to_float(labels_2d_stacked) return labels_2d_stacked_float
Example #22
Source File: multiple_grid_anchor_generator.py From moveo_ros with MIT License | 4 votes |
def __init__(self, box_specs_list, base_anchor_size=None, clip_window=None): """Constructs a MultipleGridAnchorGenerator. To construct anchors, at multiple grid resolutions, one must provide a list of feature_map_shape_list (e.g., [(8, 8), (4, 4)]), and for each grid size, a corresponding list of (scale, aspect ratio) box specifications. For example: box_specs_list = [[(.1, 1.0), (.1, 2.0)], # for 8x8 grid [(.2, 1.0), (.3, 1.0), (.2, 2.0)]] # for 4x4 grid To support the fully convolutional setting, we pass grid sizes in at generation time, while scale and aspect ratios are fixed at construction time. Args: box_specs_list: list of list of (scale, aspect ratio) pairs with the outside list having the same number of entries as feature_map_shape_list (which is passed in at generation time). base_anchor_size: base anchor size as [height, width] (length-2 float tensor, default=[256, 256]). clip_window: a tensor of shape [4] specifying a window to which all anchors should be clipped. If clip_window is None, then no clipping is performed. Raises: ValueError: if box_specs_list is not a list of list of pairs ValueError: if clip_window is not either None or a tensor of shape [4] """ if isinstance(box_specs_list, list) and all( [isinstance(list_item, list) for list_item in box_specs_list]): self._box_specs = box_specs_list else: raise ValueError('box_specs_list is expected to be a ' 'list of lists of pairs') if base_anchor_size is None: base_anchor_size = tf.constant([256, 256], dtype=tf.float32) self._base_anchor_size = base_anchor_size if clip_window is not None and clip_window.get_shape().as_list() != [4]: raise ValueError('clip_window must either be None or a shape [4] tensor') self._clip_window = clip_window self._scales = [] self._aspect_ratios = [] for box_spec in self._box_specs: if not all([isinstance(entry, tuple) and len(entry) == 2 for entry in box_spec]): raise ValueError('box_specs_list is expected to be a ' 'list of lists of pairs') scales, aspect_ratios = zip(*box_spec) self._scales.append(scales) self._aspect_ratios.append(aspect_ratios)
Example #23
Source File: multiple_grid_anchor_generator.py From moveo_ros with MIT License | 4 votes |
def create_ssd_anchors(num_layers=6, min_scale=0.2, max_scale=0.95, aspect_ratios=(1.0, 2.0, 3.0, 1.0/2, 1.0/3), base_anchor_size=None, reduce_boxes_in_lowest_layer=True): """Creates MultipleGridAnchorGenerator for SSD anchors. This function instantiates a MultipleGridAnchorGenerator that reproduces ``default box`` construction proposed by Liu et al in the SSD paper. See Section 2.2 for details. Grid sizes are assumed to be passed in at generation time from finest resolution to coarsest resolution --- this is used to (linearly) interpolate scales of anchor boxes corresponding to the intermediate grid sizes. Anchors that are returned by calling the `generate` method on the returned MultipleGridAnchorGenerator object are always in normalized coordinates and clipped to the unit square: (i.e. all coordinates lie in [0, 1]x[0, 1]). Args: num_layers: integer number of grid layers to create anchors for (actual grid sizes passed in at generation time) min_scale: scale of anchors corresponding to finest resolution (float) max_scale: scale of anchors corresponding to coarsest resolution (float) aspect_ratios: list or tuple of (float) aspect ratios to place on each grid point. base_anchor_size: base anchor size as [height, width]. reduce_boxes_in_lowest_layer: a boolean to indicate whether the fixed 3 boxes per location is used in the lowest layer. Returns: a MultipleGridAnchorGenerator """ if base_anchor_size is None: base_anchor_size = [1.0, 1.0] base_anchor_size = tf.constant(base_anchor_size, dtype=tf.float32) box_specs_list = [] scales = [min_scale + (max_scale - min_scale) * i / (num_layers - 1) for i in range(num_layers)] + [1.0] for layer, scale, scale_next in zip( range(num_layers), scales[:-1], scales[1:]): layer_box_specs = [] if layer == 0 and reduce_boxes_in_lowest_layer: layer_box_specs = [(0.1, 1.0), (scale, 2.0), (scale, 0.5)] else: for aspect_ratio in aspect_ratios: layer_box_specs.append((scale, aspect_ratio)) if aspect_ratio == 1.0: layer_box_specs.append((np.sqrt(scale*scale_next), 1.0)) box_specs_list.append(layer_box_specs) return MultipleGridAnchorGenerator(box_specs_list, base_anchor_size)
Example #24
Source File: multiple_grid_anchor_generator.py From hands-detection with MIT License | 4 votes |
def __init__(self, box_specs_list, base_anchor_size=None, clip_window=None): """Constructs a MultipleGridAnchorGenerator. To construct anchors, at multiple grid resolutions, one must provide a list of feature_map_shape_list (e.g., [(8, 8), (4, 4)]), and for each grid size, a corresponding list of (scale, aspect ratio) box specifications. For example: box_specs_list = [[(.1, 1.0), (.1, 2.0)], # for 8x8 grid [(.2, 1.0), (.3, 1.0), (.2, 2.0)]] # for 4x4 grid To support the fully convolutional setting, we pass grid sizes in at generation time, while scale and aspect ratios are fixed at construction time. Args: box_specs_list: list of list of (scale, aspect ratio) pairs with the outside list having the same number of entries as feature_map_shape_list (which is passed in at generation time). base_anchor_size: base anchor size as [height, width] (length-2 float tensor, default=[256, 256]). clip_window: a tensor of shape [4] specifying a window to which all anchors should be clipped. If clip_window is None, then no clipping is performed. Raises: ValueError: if box_specs_list is not a list of list of pairs ValueError: if clip_window is not either None or a tensor of shape [4] """ if isinstance(box_specs_list, list) and all( [isinstance(list_item, list) for list_item in box_specs_list]): self._box_specs = box_specs_list else: raise ValueError('box_specs_list is expected to be a ' 'list of lists of pairs') if base_anchor_size is None: base_anchor_size = tf.constant([256, 256], dtype=tf.float32) self._base_anchor_size = base_anchor_size if clip_window is not None and clip_window.get_shape().as_list() != [4]: raise ValueError('clip_window must either be None or a shape [4] tensor') self._clip_window = clip_window self._scales = [] self._aspect_ratios = [] for box_spec in self._box_specs: if not all([isinstance(entry, tuple) and len(entry) == 2 for entry in box_spec]): raise ValueError('box_specs_list is expected to be a ' 'list of lists of pairs') scales, aspect_ratios = zip(*box_spec) self._scales.append(scales) self._aspect_ratios.append(aspect_ratios)
Example #25
Source File: multiple_grid_anchor_generator.py From hands-detection with MIT License | 4 votes |
def create_ssd_anchors(num_layers=6, min_scale=0.2, max_scale=0.95, aspect_ratios=(1.0, 2.0, 3.0, 1.0/2, 1.0/3), base_anchor_size=None, reduce_boxes_in_lowest_layer=True): """Creates MultipleGridAnchorGenerator for SSD anchors. This function instantiates a MultipleGridAnchorGenerator that reproduces ``default box`` construction proposed by Liu et al in the SSD paper. See Section 2.2 for details. Grid sizes are assumed to be passed in at generation time from finest resolution to coarsest resolution --- this is used to (linearly) interpolate scales of anchor boxes corresponding to the intermediate grid sizes. Anchors that are returned by calling the `generate` method on the returned MultipleGridAnchorGenerator object are always in normalized coordinates and clipped to the unit square: (i.e. all coordinates lie in [0, 1]x[0, 1]). Args: num_layers: integer number of grid layers to create anchors for (actual grid sizes passed in at generation time) min_scale: scale of anchors corresponding to finest resolution (float) max_scale: scale of anchors corresponding to coarsest resolution (float) aspect_ratios: list or tuple of (float) aspect ratios to place on each grid point. base_anchor_size: base anchor size as [height, width]. reduce_boxes_in_lowest_layer: a boolean to indicate whether the fixed 3 boxes per location is used in the lowest layer. Returns: a MultipleGridAnchorGenerator """ if base_anchor_size is None: base_anchor_size = [1.0, 1.0] base_anchor_size = tf.constant(base_anchor_size, dtype=tf.float32) box_specs_list = [] scales = [min_scale + (max_scale - min_scale) * i / (num_layers - 1) for i in range(num_layers)] + [1.0] for layer, scale, scale_next in zip( range(num_layers), scales[:-1], scales[1:]): layer_box_specs = [] if layer == 0 and reduce_boxes_in_lowest_layer: layer_box_specs = [(0.1, 1.0), (scale, 2.0), (scale, 0.5)] else: for aspect_ratio in aspect_ratios: layer_box_specs.append((scale, aspect_ratio)) if aspect_ratio == 1.0: layer_box_specs.append((np.sqrt(scale*scale_next), 1.0)) box_specs_list.append(layer_box_specs) return MultipleGridAnchorGenerator(box_specs_list, base_anchor_size)
Example #26
Source File: fast_disa.py From DiSAN with Apache License 2.0 | 4 votes |
def stacking_fast_directional_self_attention( rep_tensor, rep_mask, hn, head_num=8, is_train=None, residual_keep_prob=.8, attn_keep_prob=.8, dense_keep_prob=.9, wd=0., # dropout and L2 use_direction=True, attn_self=False, activation_func_name='relu', dot_activation_name='exp', layer_num=10, scope=None ): """ stacked Fast-DiSA :param rep_tensor: same as that in Fast-DiSA; :param rep_mask: same as that in Fast-DiSA; :param hn: same as that in Fast-DiSA; :param head_num: same as that in Fast-DiSA; :param is_train: same as that in Fast-DiSA; :param residual_keep_prob: float-[], dropout keep probability for residual connection; :param attn_keep_prob: same as that in Fast-DiSA; :param dense_keep_prob: same as that in Fast-DiSA; :param wd: same as that in Fast-DiSA; :param use_direction: same as that in Fast-DiSA; :param attn_self: same as that in Fast-DiSA; :param activation_func_name: same as that in Fast-DiSA; :param dot_activation_name: same as that in Fast-DiSA; :param layer_num: int-[], the number of layer stacked; :param scope: soc :return: """ with tf.variable_scope(scope or 'stacking_fast_disa'): final_mask_ft = mask_ft_generation(rep_mask, head_num, use_direction, attn_self) x = rep_tensor for layer_idx in range(layer_num): with tf.variable_scope('layer_%d' % layer_idx): # ffn y = bn_dense_layer( x, hn, True, 0., 'ffn', activation_func_name, False, wd, dense_keep_prob, is_train) x = residual_connection(x, y, is_train, residual_keep_prob, 'res_con_1') # self-attn y = fast_directional_self_attention( x, rep_mask, hn, head_num, is_train, attn_keep_prob, dense_keep_prob, wd, use_direction, attn_self=attn_self, use_fusion_gate=False, final_mask_ft=final_mask_ft, dot_activation_name=dot_activation_name, use_input_for_attn=True, add_layer_for_multi=False, activation_func_name=activation_func_name, apply_act_for_v=False, input_hn=None, output_hn=hn, accelerate=True, merge_var=False, scope='fast_disa' ) x = residual_connection(x, y, is_train, residual_keep_prob, 'res_con_2') return x
Example #27
Source File: multiple_grid_anchor_generator.py From object_detection_kitti with Apache License 2.0 | 4 votes |
def __init__(self, box_specs_list, base_anchor_size=None, clip_window=None): """Constructs a MultipleGridAnchorGenerator. To construct anchors, at multiple grid resolutions, one must provide a list of feature_map_shape_list (e.g., [(8, 8), (4, 4)]), and for each grid size, a corresponding list of (scale, aspect ratio) box specifications. For example: box_specs_list = [[(.1, 1.0), (.1, 2.0)], # for 8x8 grid [(.2, 1.0), (.3, 1.0), (.2, 2.0)]] # for 4x4 grid To support the fully convolutional setting, we pass grid sizes in at generation time, while scale and aspect ratios are fixed at construction time. Args: box_specs_list: list of list of (scale, aspect ratio) pairs with the outside list having the same number of entries as feature_map_shape_list (which is passed in at generation time). base_anchor_size: base anchor size as [height, width] (length-2 float tensor, default=[256, 256]). clip_window: a tensor of shape [4] specifying a window to which all anchors should be clipped. If clip_window is None, then no clipping is performed. Raises: ValueError: if box_specs_list is not a list of list of pairs ValueError: if clip_window is not either None or a tensor of shape [4] """ if isinstance(box_specs_list, list) and all( [isinstance(list_item, list) for list_item in box_specs_list]): self._box_specs = box_specs_list else: raise ValueError('box_specs_list is expected to be a ' 'list of lists of pairs') if base_anchor_size is None: base_anchor_size = tf.constant([256, 256], dtype=tf.float32) self._base_anchor_size = base_anchor_size if clip_window is not None and clip_window.get_shape().as_list() != [4]: raise ValueError('clip_window must either be None or a shape [4] tensor') self._clip_window = clip_window self._scales = [] self._aspect_ratios = [] for box_spec in self._box_specs: if not all([isinstance(entry, tuple) and len(entry) == 2 for entry in box_spec]): raise ValueError('box_specs_list is expected to be a ' 'list of lists of pairs') scales, aspect_ratios = zip(*box_spec) self._scales.append(scales) self._aspect_ratios.append(aspect_ratios)
Example #28
Source File: multiple_grid_anchor_generator.py From object_detection_kitti with Apache License 2.0 | 4 votes |
def create_ssd_anchors(num_layers=6, min_scale=0.2, max_scale=0.95, aspect_ratios=(1.0, 2.0, 3.0, 1.0/2, 1.0/3), base_anchor_size=None, reduce_boxes_in_lowest_layer=True): """Creates MultipleGridAnchorGenerator for SSD anchors. This function instantiates a MultipleGridAnchorGenerator that reproduces ``default box`` construction proposed by Liu et al in the SSD paper. See Section 2.2 for details. Grid sizes are assumed to be passed in at generation time from finest resolution to coarsest resolution --- this is used to (linearly) interpolate scales of anchor boxes corresponding to the intermediate grid sizes. Anchors that are returned by calling the `generate` method on the returned MultipleGridAnchorGenerator object are always in normalized coordinates and clipped to the unit square: (i.e. all coordinates lie in [0, 1]x[0, 1]). Args: num_layers: integer number of grid layers to create anchors for (actual grid sizes passed in at generation time) min_scale: scale of anchors corresponding to finest resolution (float) max_scale: scale of anchors corresponding to coarsest resolution (float) aspect_ratios: list or tuple of (float) aspect ratios to place on each grid point. base_anchor_size: base anchor size as [height, width]. reduce_boxes_in_lowest_layer: a boolean to indicate whether the fixed 3 boxes per location is used in the lowest layer. Returns: a MultipleGridAnchorGenerator """ if base_anchor_size is None: base_anchor_size = [1.0, 1.0] base_anchor_size = tf.constant(base_anchor_size, dtype=tf.float32) box_specs_list = [] scales = [min_scale + (max_scale - min_scale) * i / (num_layers - 1) for i in range(num_layers)] + [1.0] for layer, scale, scale_next in zip( range(num_layers), scales[:-1], scales[1:]): layer_box_specs = [] if layer == 0 and reduce_boxes_in_lowest_layer: layer_box_specs = [(0.1, 1.0), (scale, 2.0), (scale, 0.5)] else: for aspect_ratio in aspect_ratios: layer_box_specs.append((scale, aspect_ratio)) if aspect_ratio == 1.0: layer_box_specs.append((np.sqrt(scale*scale_next), 1.0)) box_specs_list.append(layer_box_specs) return MultipleGridAnchorGenerator(box_specs_list, base_anchor_size)
Example #29
Source File: modules.py From PlaneNet with MIT License | 4 votes |
def meanfieldModule(planeSegmentations, planeDepths, planesY, imageDiff, numOutputPlanes = 20, coef = [1, 1, 1], beta = 1, iteration = 0, maxDepthDiff = 0.2, varDepthDiff = 0.5, kernel_size = 9): batchSize = int(planeSegmentations.shape[0]) height = int(planeSegmentations.shape[1]) width = int(planeSegmentations.shape[2]) P = planeSegmentations #minDepthDiff = 0.1 #normalDotThreshold = np.cos(np.deg2rad(30)) #N_diff = tf.matmul(planeNormals, planeNormals, transpose_b=True) #N_diff_mask = tf.cast((N_diff < normalDotThreshold), tf.float) + tf.diag(tf.ones(numOutputPlanes)) #N_diff = tf.clip(N_diff, minDepthDiff, 1) #N_diff_mask = tf.expand_dims(tf.expand_dims(N_diff_mask, 1), 1) #D_diff = (D_diff - minDepthDiff) * N_diff_mask + minDepthDiff #confidenceThreshold = 0.00 #P_truncated = P * (P >= confidenceThreshold).astype(tf.float) S = tf.one_hot(tf.argmax(planeSegmentations, 3), depth=numOutputPlanes) # D = tf.tile(tf.expand_dims(planeDepths, -1), [1, 1, 1, 1, numOutputPlanes]) # D_transpose = tf.tile(tf.expand_dims(planeDepths, 3), [1, 1, 1, numOutputPlanes, 1]) # D_diff = tf.abs(D - D_transpose) # DS_weight = tf.exp(-tf.pow(tf.clip_by_value(1 - D_diff / maxDepthDiff, 0, 1), 2) / sigmaDepthDiff) # DS_diff = tf.reduce_sum(DS_weight * tf.expand_dims(S, 3), axis=4) - tf.exp(-1 / sigmaDepthDiff) * S depthWeight = 50.0 colorWeight = 50.0 normalY = tf.reduce_sum(S * tf.reshape(planesY, [-1, 1, 1, numOutputPlanes]), axis=3, keep_dims=True) depth_diff = (planeDepths - tf.reduce_sum(planeDepths * S, 3, keep_dims=True)) * normalY depth_diff = tf.concat([depth_diff[:, :, :, :numOutputPlanes - 1], (1 - S[:, :, :, numOutputPlanes - 1:numOutputPlanes])], axis=3) DS_diff = (1 - tf.exp(-tf.pow(tf.minimum(depth_diff, maxDepthDiff), 2) / varDepthDiff)) + (1 - S) * (1 / depthWeight + (colorWeight / depthWeight) * imageDiff) #DS_diff = tf.exp(-tf.pow(1 - tf.clip_by_value(tf.abs(planeDepths - tf.reduce_sum(planeDepths * S, 3, keep_dims=True)), 0, 1), 2) / 0.5) - tf.exp(-1 / 0.5) * S neighbor_kernel_array = gaussian(kernel_size) neighbor_kernel_array[(kernel_size - 1) / 2][(kernel_size - 1) / 2] = 0 neighbor_kernel_array /= neighbor_kernel_array.sum() neighbor_kernel = tf.constant(neighbor_kernel_array.reshape(-1), shape=neighbor_kernel_array.shape, dtype=tf.float32) neighbor_kernel = tf.reshape(neighbor_kernel, [kernel_size, kernel_size, 1, 1]) DS = tf.nn.depthwise_conv2d(DS_diff, tf.tile(neighbor_kernel, [1, 1, numOutputPlanes, 1]), strides=[1, 1, 1, 1], padding='SAME') P = tf.clip_by_value(P, 1e-4, 1) confidence = P * tf.exp(-coef[1] * DS) #confidence = coef[0] * P + tf.exp(-coef[1] * DS) + tf.exp(-coef[2] * S_diff) #confidence[:, :, :, numOutputPlanes] = 1e-4 #confidence = tf.clip(confidence, 1e-4, 1) refined_segmentation = tf.nn.softmax(tf.log(confidence)) return refined_segmentation, {'diff': DS}
Example #30
Source File: multiple_grid_anchor_generator.py From MBMD with MIT License | 4 votes |
def create_ssd_anchors(num_layers=6, min_scale=0.2, max_scale=0.95, aspect_ratios=(1.0, 2.0, 3.0, 1.0/2, 1.0/3), base_anchor_size=None, reduce_boxes_in_lowest_layer=True): """Creates MultipleGridAnchorGenerator for SSD anchors. This function instantiates a MultipleGridAnchorGenerator that reproduces ``default box`` construction proposed by Liu et al in the SSD paper. See Section 2.2 for details. Grid sizes are assumed to be passed in at generation time from finest resolution to coarsest resolution --- this is used to (linearly) interpolate scales of anchor boxes corresponding to the intermediate grid sizes. Anchors that are returned by calling the `generate` method on the returned MultipleGridAnchorGenerator object are always in normalized coordinates and clipped to the unit square: (i.e. all coordinates lie in [0, 1]x[0, 1]). Args: num_layers: integer number of grid layers to create anchors for (actual grid sizes passed in at generation time) min_scale: scale of anchors corresponding to finest resolution (float) max_scale: scale of anchors corresponding to coarsest resolution (float) aspect_ratios: list or tuple of (float) aspect ratios to place on each grid point. base_anchor_size: base anchor size as [height, width]. reduce_boxes_in_lowest_layer: a boolean to indicate whether the fixed 3 boxes per location is used in the lowest layer. Returns: a MultipleGridAnchorGenerator """ if base_anchor_size is None: base_anchor_size = [1.0, 1.0] base_anchor_size = tf.constant(base_anchor_size, dtype=tf.float32) box_specs_list = [] scales = [min_scale + (max_scale - min_scale) * i / (num_layers - 1) for i in range(num_layers)] + [1.0] for layer, scale, scale_next in zip( range(num_layers), scales[:-1], scales[1:]): layer_box_specs = [] if layer == 0 and reduce_boxes_in_lowest_layer: layer_box_specs = [(0.1, 1.0), (scale, 2.0), (scale, 0.5)] else: for aspect_ratio in aspect_ratios: layer_box_specs.append((scale, aspect_ratio)) if aspect_ratio == 1.0: layer_box_specs.append((np.sqrt(scale*scale_next), 1.0)) box_specs_list.append(layer_box_specs) return MultipleGridAnchorGenerator(box_specs_list, base_anchor_size)