def update_state(self, inputs, outputs): """Function that updates the metric state at each example. Args: inputs: A dictionary containing input tensors. outputs: A dictionary containing output tensors. Returns: Update op. """ # Prepare logits and labels logits = outputs[ standard_fields.DetectionResultFields.object_semantic_points] labels = inputs[standard_fields.InputDataFields.object_class_points] weights = inputs[standard_fields.InputDataFields.point_loss_weights] num_valid_points = inputs[standard_fields.InputDataFields.num_valid_points] if len(logits.get_shape().as_list()) == 3: batch_size = logits.get_shape().as_list()[0] logits_list = [] labels_list = [] weights_list = [] for i in range(batch_size): num_valid_points_i = num_valid_points[i] logits_list.append(logits[i, 0:num_valid_points_i, :]) labels_list.append(labels[i, 0:num_valid_points_i, :]) weights_list.append(weights[i, 0:num_valid_points_i, :]) logits = tf.concat(logits_list, axis=0) labels = tf.concat(labels_list, axis=0) weights = tf.concat(weights_list, axis=0) if self.num_classes is None: num_classes = logits.get_shape().as_list()[-1] else: num_classes = self.num_classes if num_classes != logits.get_shape().as_list()[-1]: raise ValueError('num_classes do not match the logits dimensions.') class_labels, class_predictions = _get_class_labels_and_predictions( labels=labels, logits=logits, num_classes=self.num_classes, multi_label=self.multi_label) update_ops = [] for c in self.class_range: update_op_tp_c = self.true_positive_metrics[c].update_state( y_true=class_labels[c], y_pred=class_predictions[c], sample_weight=weights) update_ops.append(update_op_tp_c) update_op_fp_c = self.false_positive_metrics[c].update_state( y_true=class_labels[c], y_pred=class_predictions[c], sample_weight=weights) update_ops.append(update_op_fp_c) update_op_fn_c = self.false_negative_metrics[c].update_state( y_true=class_labels[c], y_pred=class_predictions[c], sample_weight=weights) update_ops.append(update_op_fn_c) return tf.group(update_ops)
def slice_to_max_num_distractors_fn(inputs): """Reduces the number of distractors to the max number.""" label_for_ex, scores_for_ex = inputs scores_nocorrect = tf.concat([ scores_for_ex[0:label_for_ex], scores_for_ex[(label_for_ex + 1):] ], axis=0) random_start_index = tf.random.uniform( shape=[], minval=0, maxval=scores_for_ex.shape[0] - max_num_dist, dtype=tf.int32) new_scores = scores_nocorrect[ random_start_index:random_start_index + max_num_dist] # Put the groundtruth embedding in position 0 to make labels easy. new_scores = tf.concat([ tf.expand_dims(scores_for_ex[label_for_ex], 0), new_scores ], axis=0) return new_scores
def update_state(self, inputs, outputs): """Function that updates the metric state at each example. Args: inputs: A dictionary containing input tensors. outputs: A dictionary containing output tensors. Returns: Update op. """ detections_score = tf.reshape( outputs[standard_fields.DetectionResultFields.objects_score], [-1]) detections_class = tf.reshape( outputs[standard_fields.DetectionResultFields.objects_class], [-1]) num_detections = tf.shape(detections_score)[0] detections_instance_mask = tf.reshape( outputs[ standard_fields.DetectionResultFields.instance_segments_voxel_mask], [num_detections, -1]) gt_class = tf.reshape(inputs[standard_fields.InputDataFields.objects_class], [-1]) num_gt = tf.shape(gt_class)[0] gt_voxel_instance_ids = tf.reshape( inputs[standard_fields.InputDataFields.object_instance_id_voxels], [-1]) gt_instance_masks = tf.transpose( tf.one_hot(gt_voxel_instance_ids - 1, depth=num_gt, dtype=tf.float32)) for c in self.class_range: gt_mask_c = tf.equal(gt_class, c) num_gt_c = tf.math.reduce_sum(tf.cast(gt_mask_c, dtype=tf.int32)) gt_instance_masks_c = tf.boolean_mask(gt_instance_masks, gt_mask_c) detections_mask_c = tf.equal(detections_class, c) num_detections_c = tf.math.reduce_sum( tf.cast(detections_mask_c, dtype=tf.int32)) if num_detections_c == 0: continue det_scores_c = tf.boolean_mask(detections_score, detections_mask_c) det_instance_mask_c = tf.boolean_mask(detections_instance_mask, detections_mask_c) det_scores_c, sorted_indices = tf.math.top_k( det_scores_c, k=num_detections_c) det_instance_mask_c = tf.gather(det_instance_mask_c, sorted_indices) tp_c = tf.zeros([num_detections_c], dtype=tf.int32) if num_gt_c > 0: ious_c = instance_segmentation_utils.points_mask_iou( masks1=gt_instance_masks_c, masks2=det_instance_mask_c) max_overlap_gt_ids = tf.cast( tf.math.argmax(ious_c, axis=0), dtype=tf.int32) is_gt_box_detected = tf.zeros([num_gt_c], dtype=tf.int32) for i in tf.range(num_detections_c): gt_id = max_overlap_gt_ids[i] if (ious_c[gt_id, i] > self.iou_threshold and is_gt_box_detected[gt_id] == 0): tp_c = tf.maximum( tf.one_hot(i, num_detections_c, dtype=tf.int32), tp_c) is_gt_box_detected = tf.maximum( tf.one_hot(gt_id, num_gt_c, dtype=tf.int32), is_gt_box_detected) self.tp[c] = tf.concat([self.tp[c], tp_c], axis=0) self.scores[c] = tf.concat([self.scores[c], det_scores_c], axis=0) self.num_gt[c] += num_gt_c return tf.no_op()
def _gif_and_image_summary(name, images, fps, saturate=False, step=None): images = tf.image.convert_image_dtype(images, tf.uint8, saturate=saturate) output = tf.concat(tf.unstack(images), axis=2)[None] gif_utils.gif_summary_v2(name, output, 1, fps, step=step) output = tf.concat(tf.unstack(images), axis=2) output = tf.concat(tf.unstack(output), axis=0)[None] tf.contrib.summary.image(name, output, step=step)
def spatial_loss(truth_features, predicted_features, space_desc): feature_losses = [] for truth, prediction, spec in zip(truth_features, predicted_features, space_desc.features): if spec.type == FeatureType.CATEGORICAL: truth = tf.transpose(truth, (0, 2, 3, 1)) prediction = tf.transpose(prediction, (0, 2, 3, 1)) feature_losses.append( tf.losses.softmax_cross_entropy(truth, prediction)) summary_image = tf.argmax( tf.concat([truth, prediction], 2), 3) summary_image = tf.gather( palette[space_desc.index][spec.index], summary_image) tf.summary.image(spec.name, summary_image) else: feature_losses.append( tf.losses.mean_squared_error(truth, prediction)) summary_image = tf.concat([truth, prediction], 3) tf.summary.image(spec.name, tf.transpose(summary_image, (0, 2, 3, 1))) tf.summary.scalar(spec.name, feature_losses[-1]) return tf.reduce_mean(tf.stack(feature_losses))
def train_step(self, time_step: ActionTimeStep, state, calc_intrinsic_reward=True): """ Args: time_step (ActionTimeStep): input time_step data state (tuple): state for MISC (previous observation, previous previous action) calc_intrinsic_reward (bool): if False, only return the losses Returns: TrainStep: outputs: empty tuple () state: tuple of observation and previous action info: (MISCInfo): """ feature = time_step.observation prev_action = time_step.prev_action feature = tf.concat([feature_state, prev_action], axis=-1) prev_feature = tf.concat(state, axis=-1) feature_reshaped = tf.expand_dims(feature, axis=1) prev_feature_reshaped = tf.expand_dims(prev_feature, axis=1) feature_pair = tf.concat([prev_feature_reshaped, feature_reshaped], 1) feature_reshaped_tran = transpose2(feature_reshaped, 1, 0) def add_batch(): self._buffer.add_batch(feature_reshaped_tran) if calc_intrinsic_reward: add_batch() if self._n_objects < 2: obs_tau_excludes_goal, obs_tau_achieved_goal = \ self._split_observation_fn(feature_pair) loss = self._mine(obs_tau_excludes_goal, obs_tau_achieved_goal) elif self._n_objects == 2: obs_tau_excludes_goal, obs_tau_achieved_goal_1, obs_tau_achieved_goal_2 \ = self._split_observation_fn( feature_pair) loss_1 = self._mine(obs_tau_excludes_goal, obs_tau_achieved_goal_1) loss_2 = self._mine(obs_tau_excludes_goal, obs_tau_achieved_goal_2) loss = loss_1 + loss_2 intrinsic_reward = () if calc_intrinsic_reward: # scale/normalize the MISC intrinsic reward if self._n_objects < 2: intrinsic_reward = tf.clip_by_value(self._mi_r_scale * loss, 0, 1) elif self._n_objects == 2: intrinsic_reward = tf.clip_by_value( self._mi_r_scale * loss_1, 0, 1) + 1 * tf.clip_by_value(self._mi_r_scale * loss_2, 0, 1) return AlgorithmStep( outputs=(), state=[feature_state, prev_action], \ info=MISCInfo(reward=intrinsic_reward))
def loop_body(idx, qq_grad, qv_grad, sk_grad, sv_grad): """Compute gradients for a single query.""" qq = query_queries[idx:idx + 1] qv = query_values[idx:idx + 1] x = self._get_dist(qq, qv, support_keys_id, support_values_id, labels) grads = tf.gradients( x, [qq, qv, support_keys_id, support_values_id], grad_ys=dy[:, idx:idx + 1]) qq_grad = tf.concat([qq_grad, grads[0]], axis=0) qv_grad = tf.concat([qv_grad, grads[1]], axis=0) sk_grad += grads[2] sv_grad += grads[3] return (idx + 1, qq_grad, qv_grad, sk_grad, sv_grad)
def _network_adapter(self, states, scope): self._validate_states(states) with tf.compat.v1.name_scope('network'): q_value_list = [] for slate in self._all_possible_slates: user = tf.squeeze(states[:, 0, :, :], axis=2) docs = [] for i in slate: docs.append(tf.squeeze(states[:, i + 1, :, :], axis=2)) q_value_list.append(self.network(user, tf.concat(docs, axis=1), scope)) q_values = tf.concat(q_value_list, axis=1) return dqn_agent.DQNNetworkType(q_values)
def get_similarity_matrix(self): if self.model_config.use_relations_outputs: return tf.concat( [self.embeddings_layer.entity_embeddings, self.embeddings_layer.relation_embeddings], axis=0, ) return self.embeddings_layer.entity_embeddings
def compute_class_distances(self, support_embeddings, onehot_support_labels, query_embeddings): """Return the relation score of each query example to each prototype.""" # `query_embeddings` is [num_examples, 21, 21, num_features]. out_shape = query_embeddings.shape.as_list()[1:] num_features = out_shape[-1] num_query_examples = tf.shape(input=query_embeddings)[0] # [num_classes, 19, 19, num_features]. prototypes = compute_prototypes(support_embeddings, onehot_support_labels) # [num_classes, 19, 19, num_features]. prototype_extended = tf.tile( tf.expand_dims(prototypes, 0), [num_query_examples] + [1] * (1 + len(out_shape))) # [num_query_examples, 19, 19, num_features]. way = onehot_support_labels.shape.as_list()[-1] query_extended = tf.tile( tf.expand_dims(query_embeddings, 1), [1, way] + [1] * len(out_shape)) relation_pairs = tf.concat((prototype_extended, query_extended), len(out_shape) + 1) # relation_pairs.shape.as_list()[-3:] == [-1] + out_shape + [num_features*2] relation_pairs = tf.reshape(relation_pairs, [-1] + out_shape[:-1] + [num_features * 2]) return tf.reshape(self.relation_module_fn(relation_pairs), [-1, way])
def loop_body(idx, dist): dist_new = self._get_dist(query_queries_fwd[idx:idx + 1], query_values_fwd[idx:idx + 1], support_keys_fwd, support_values_fwd, labels_fwd) dist = tf.concat([dist, dist_new], axis=1) return (idx + 1, dist)
def flatten(input, axis=1, end_axis=-1): """ Caffe-style flatten. Args: inputs: An N-D tensor. axis: The first axis to flatten: all preceding axes are retained in the output. May be negative to index from the end (e.g., -1 for the last axis). end_axis: The last axis to flatten: all following axes are retained in the output. May be negative to index from the end (e.g., the default -1 for the last axis) Returns: A M-D tensor where M = N - (end_axis - axis) """ input_shape = tf.shape(input) input_rank = tf.shape(input_shape)[0] if axis < 0: axis = input_rank + axis if end_axis < 0: end_axis = input_rank + end_axis output_shape = [] if axis != 0: output_shape.append(input_shape[:axis]) output_shape.append([tf.reduce_prod(input_shape[axis:end_axis + 1])]) if end_axis + 1 != input_rank: output_shape.append(input_shape[end_axis + 1:]) output_shape = tf.concat(output_shape, axis=0) output = tf.reshape(input, output_shape) return output
def build_graph(self): """Builds the neural network graph.""" # define graph self.g = tf.Graph() with self.g.as_default(): # create and store a new session for the graph self.sess = tf.Session() # define placeholders self.x = tf.placeholder(shape=[None, self.dim_input], dtype=tf.float32) self.y = tf.placeholder(shape=[None, self.num_classes], dtype=tf.float32) # define simple model with tf.variable_scope('last_layer'): self.z = tf.layers.dense(inputs=self.x, units=self.num_classes) self.loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(labels=self.y, logits=self.z)) self.output_probs = tf.nn.softmax(self.z) # Variables of the last layer self.ll_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) self.ll_vars_concat = tf.concat( [self.ll_vars[0], tf.expand_dims(self.ll_vars[1], axis=0)], 0) # Summary _variable_summaries(self.ll_vars_concat) # saving the weights of last layer when running bootstrap algorithm self.saver = tf.train.Saver(var_list=self.ll_vars) self.gd_opt = tf.train.GradientDescentOptimizer(self.step_size) # SGD optimizer for the last layer grads_vars_sgd = self.gd_opt.compute_gradients(self.loss) self.train_op = self.gd_opt.apply_gradients(grads_vars_sgd) for g, v in grads_vars_sgd: if g is not None: s = list(v.name) s[v.name.rindex(':')] = '_' tf.summary.histogram(''.join(s) + '/grad_hist_boot_sgd', g) # Merge all the summaries and write them out self.all_summaries = tf.summary.merge_all() location = os.path.join(self.working_dir, 'logs') self.writer = tf.summary.FileWriter(location, graph=self.g) saver_network = tf.train.Saver(var_list=self.ll_vars) print('Loading the network...') # Restores from checkpoint saver_network.restore(self.sess, self.model_dir) print('Graph successfully loaded.')
def compute_logits(self, support_embeddings, query_embeddings, onehot_support_labels): """Computes the relation score of each query example to each prototype.""" # [n_test, 21, 21, n_features]. query_embed_shape = query_embeddings.shape.as_list() n_feature = query_embed_shape[3] out_shape = query_embed_shape[1:3] n_test = tf.shape(query_embeddings)[0] # [n_test, num_clases, 21, 21, n_feature]. # It is okay one of the elements in the list to be tensor. prototypes = compute_prototypes(support_embeddings, onehot_support_labels) prototype_extended = tf.tile(tf.expand_dims(prototypes, 0), [n_test, 1, 1, 1, 1]) # [num_clases, n_test, 21, 21, n_feature]. query_f_extended = tf.tile( tf.expand_dims(query_embeddings, 1), [1, tf.shape(onehot_support_labels)[-1], 1, 1, 1]) relation_pairs = tf.concat((prototype_extended, query_f_extended), 4) # relation_pairs.shape.as_list()[-3:] == [-1] + out_shape + [n_feature*2] relation_pairs = tf.reshape(relation_pairs, [-1] + out_shape + [n_feature * 2]) relationnet_dict = functional_backbones.relation_module( relation_pairs, 'relationnet') way = tf.shape(onehot_support_labels)[-1] relations = tf.reshape(relationnet_dict['output'], [-1, way]) return relations
def _build_target_distribution(self): self._reshape_networks() batch_size = tf.shape(self._replay.rewards)[0] # size of rewards: batch_size x 1 rewards = self._replay.rewards[:, None] # size of tiled_support: batch_size x num_atoms tiled_support = tf.tile(self.support, [batch_size]) tiled_support = tf.reshape(tiled_support, [batch_size, self.num_atoms]) # size of target_support: batch_size x num_atoms is_terminal_multiplier = 1. - tf.cast(self._replay.terminals, tf.float32) # Incorporate terminal state to discount factor. # size of gamma_with_terminal: batch_size x 1 gamma_with_terminal = self.cumulative_gamma * is_terminal_multiplier gamma_with_terminal = gamma_with_terminal[:, None] target_support = rewards + gamma_with_terminal * tiled_support # size of next_probabilities: batch_size x num_actions x num_atoms next_probabilities = tf.contrib.layers.softmax( self._replay_next_logits) # size of next_qt: 1 x num_actions next_qt = tf.reduce_sum(self.support * next_probabilities, 2) # size of next_qt_argmax: 1 x batch_size next_qt_argmax = tf.argmax( next_qt + self._replay.next_legal_actions, axis=1)[:, None] batch_indices = tf.range(tf.to_int64(batch_size))[:, None] # size of next_qt_argmax: batch_size x 2 next_qt_argmax = tf.concat([batch_indices, next_qt_argmax], axis=1) # size of next_probabilities: batch_size x num_atoms next_probabilities = tf.gather_nd(next_probabilities, next_qt_argmax) return project_distribution(target_support, next_probabilities, self.support)
def _build_train_op(self): """Builds the training op for Rainbow. Returns: train_op: An op performing one step of training. """ target_distribution = tf.stop_gradient(self._build_target_distribution()) # size of indices: batch_size x 1. indices = tf.range(tf.shape(self._replay_logits)[0])[:, None] # size of reshaped_actions: batch_size x 2. reshaped_actions = tf.concat([indices, self._replay.actions[:, None]], 1) # For each element of the batch, fetch the logits for its selected action. chosen_action_logits = tf.gather_nd(self._replay_logits, reshaped_actions) loss = tf.nn.softmax_cross_entropy_with_logits( labels=target_distribution, logits=chosen_action_logits) optimizer = tf.train.AdamOptimizer( learning_rate=self.learning_rate, epsilon=self.optimizer_epsilon) update_priorities_op = self._replay.tf_set_priority( self._replay.indices, tf.sqrt(loss + 1e-10)) target_priorities = self._replay.tf_get_priority(self._replay.indices) target_priorities = tf.math.add(target_priorities, 1e-10) target_priorities = 1.0 / tf.sqrt(target_priorities) target_priorities /= tf.reduce_max(target_priorities) weighted_loss = target_priorities * loss with tf.control_dependencies([update_priorities_op]): return optimizer.minimize(tf.reduce_mean(weighted_loss)), weighted_loss
def recsim_dqn_network(user, doc, scope): inputs = tf.concat([user, doc], axis=1) with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): hidden = tf.keras.layers.Dense(256, activation=tf.nn.relu)(inputs) hidden = tf.keras.layers.Dense(32, activation=tf.nn.relu)(hidden) q_value = tf.keras.layers.Dense(1, name='output')(hidden) return q_value
def score_documents_tf(user_obs, doc_obs, no_click_mass=1.0, is_mnl=False, min_normalizer=-1.0): """Computes unnormalized scores given both user and document observations. This implements both multinomial proportional model and multinormial logit model given some parameters. We also assume scores are based on inner products of user_obs and doc_obs. Args: user_obs: An instance of AbstractUserState. doc_obs: A numpy array that represents the observation of all documents in the candidate set. no_click_mass: a float indicating the mass given to a no click option is_mnl: whether to use a multinomial logit model instead of a multinomial proportional model. min_normalizer: A float (<= 0) used to offset the scores to be positive when using multinomial proportional model. Returns: A float tensor that stores unnormalzied scores of documents and a float tensor that represents the score for the action of picking no document. """ user_obs = tf.reshape(user_obs, [1, -1]) scores = tf.reduce_sum(input_tensor=tf.multiply(user_obs, doc_obs), axis=1) all_scores = tf.concat([scores, tf.constant([no_click_mass])], axis=0) if is_mnl: all_scores = tf.nn.softmax(all_scores) else: all_scores = all_scores - min_normalizer return all_scores[:-1], all_scores[-1]
def fn(): """Loss function for when number of input and output boxes is positive.""" if is_balanced: weights = loss_utils.get_balanced_loss_weights_multiclass( labels=input_boxes_instance_id) else: weights = tf.ones([tf.shape(input_boxes_instance_id)[0], 1], dtype=tf.float32) gt_length = tf.reshape(input_boxes_length, [-1, 1]) gt_height = tf.reshape(input_boxes_height, [-1, 1]) gt_width = tf.reshape(input_boxes_width, [-1, 1]) predicted_length = tf.reshape(output_boxes_length, [-1, 1]) predicted_height = tf.reshape(output_boxes_height, [-1, 1]) predicted_width = tf.reshape(output_boxes_width, [-1, 1]) predicted_length /= gt_length predicted_height /= gt_height predicted_width /= gt_width predicted_size = tf.concat( [predicted_length, predicted_height, predicted_width], axis=1) gt_size = tf.ones_like(predicted_size) if loss_type == 'huber': loss_fn = tf.keras.losses.Huber( delta=delta, reduction=tf.keras.losses.Reduction.NONE) elif loss_type == 'absolute_difference': loss_fn = tf.keras.losses.MeanAbsoluteError( reduction=tf.keras.losses.Reduction.NONE) else: raise ValueError(('Unknown loss type %s.' % loss_type)) size_losses = loss_fn(y_true=gt_size, y_pred=predicted_size) return tf.reduce_mean(size_losses * tf.reshape(weights, [-1]))
def recsim_dqn_network(user, doc, scope): inputs = tf.concat([user, doc], axis=1) with tf.compat.v1.variable_scope(scope, reuse=tf.compat.v1.AUTO_REUSE): hidden = tf.compat.v1.layers.dense(inputs, 256, activation=tf.nn.relu) hidden = tf.compat.v1.layers.dense(hidden, 32, activation=tf.nn.relu) q_value = tf.compat.v1.layers.dense(hidden, 1, name='output') return q_value
def classification_loss_fn(logits, labels, num_valid_voxels=None, weights=1.0): """Semantic segmentation cross entropy loss.""" logits_rank = len(logits.get_shape().as_list()) labels_rank = len(labels.get_shape().as_list()) if logits_rank != labels_rank: raise ValueError('Logits and labels should have the same rank.') if logits_rank != 2 and logits_rank != 3: raise ValueError( 'Logits and labels should have either 2 or 3 dimensions.') if logits_rank == 2: if num_valid_voxels is not None: raise ValueError( '`num_valid_voxels` should be None if not using batched logits.' ) elif logits_rank == 3: if num_valid_voxels is None: raise ValueError( '`num_valid_voxels` cannot be None if using batched logits.') if logits_rank == 3: if (isinstance(weights, tf.Tensor) and len(weights.get_shape().as_list()) == 3): use_weights = True else: use_weights = False batch_size = logits.get_shape().as_list()[0] logits_list = [] labels_list = [] weights_list = [] for i in range(batch_size): num_valid_voxels_i = num_valid_voxels[i] logits_list.append(logits[i, 0:num_valid_voxels_i, :]) labels_list.append(labels[i, 0:num_valid_voxels_i, :]) if use_weights: weights_list.append(weights[i, 0:num_valid_voxels_i, :]) logits = tf.concat(logits_list, axis=0) labels = tf.concat(labels_list, axis=0) if use_weights: weights = tf.concat(weights_list, axis=0) weights = tf.convert_to_tensor(weights, dtype=tf.float32) if labels.get_shape().as_list()[-1] == 1: num_classes = logits.get_shape().as_list()[-1] labels = tf.one_hot(tf.reshape(labels, shape=[-1]), num_classes) losses = tf.nn.softmax_cross_entropy_with_logits( labels=tf.stop_gradient(labels), logits=logits) return tf.reduce_mean(losses * tf.reshape(weights, [-1]))
def _concat_states(self, states, transpose=False): """Concatenate all pairs of states in a batch. Args: states: Tensor, batch of states from which we will concatenate batch_size^2 pairs of states. transpose: bool, whether to concatenate states in transpose order. Returns: A batch_size^2 Tensor containing the concatenation of all elements in `states`. """ # tiled_states will have shape # [batch_size, batch_size, representation_dimension] and will be of the # following form (where \phi_1 is the representation of the state of the # first batch_element): # [ \phi_1 \phi_2 ... \phi_batch_size ] # [ \phi_1 \phi_2 ... \phi_batch_size ] # ... # [ \phi_1 \phi_2 ... \phi_batch_size ] batch_size = tf.shape(states)[0] tiled_states = tf.tile([states], [batch_size, 1, 1]) # transpose_tiled_states will have shape # [batch_size, batch_size, representation_dimension] and will be of the # following form (where \phi_1 is the representation of the state of the # first batch_element): # [ \phi_1 \phi_1 ... \phi_1 ] # [ \phi_2 \phi_2 ... \phi_2 ] # ... # [ \phi_batch_size \phi_batch_size ... \phi_batch_size ] transpose_tiled_states = tf.keras.backend.repeat(states, batch_size) # concat_states will be a # [batch_size, batch_size, representation_dimension*2] matrix containing the # concatenation of all pairs of states in the batch. if transpose: concat_states = tf.concat([transpose_tiled_states, tiled_states], 2) else: concat_states = tf.concat([tiled_states, transpose_tiled_states], 2) # We return a reshaped matrix which results in a new batch of size # batch_size ** 2. Resulting matrix will have shape # [batch_size**2, representation_dimension]. return tf.reshape(concat_states, (batch_size**2, 4))
def _box_classification_loss_unbatched(inputs_1, outputs_1, is_intermediate, is_balanced, mine_hard_negatives, hard_negative_score_threshold): """Loss function for input and outputs of batch size 1.""" valid_mask = _get_voxels_valid_mask(inputs_1=inputs_1) if is_intermediate: logits = outputs_1[standard_fields.DetectionResultFields. intermediate_object_semantic_voxels] else: logits = outputs_1[ standard_fields.DetectionResultFields.object_semantic_voxels] num_classes = logits.get_shape().as_list()[-1] if num_classes is None: raise ValueError('Number of classes is unknown.') logits = tf.boolean_mask(tf.reshape(logits, [-1, num_classes]), valid_mask) labels = tf.boolean_mask( tf.reshape( inputs_1[standard_fields.InputDataFields.object_class_voxels], [-1, 1]), valid_mask) if mine_hard_negatives or is_balanced: instances = tf.boolean_mask( tf.reshape( inputs_1[ standard_fields.InputDataFields.object_instance_id_voxels], [-1]), valid_mask) params = {} if mine_hard_negatives: negative_scores = tf.reshape(tf.nn.softmax(logits)[:, 0], [-1]) hard_negative_mask = tf.logical_and( tf.less(negative_scores, hard_negative_score_threshold), tf.equal(tf.reshape(labels, [-1]), 0)) hard_negative_labels = tf.boolean_mask(labels, hard_negative_mask) hard_negative_logits = tf.boolean_mask(logits, hard_negative_mask) hard_negative_instances = tf.boolean_mask( tf.ones_like(instances) * (tf.reduce_max(instances) + 1), hard_negative_mask) logits = tf.concat([logits, hard_negative_logits], axis=0) instances = tf.concat([instances, hard_negative_instances], axis=0) labels = tf.concat([labels, hard_negative_labels], axis=0) if is_balanced: weights = loss_utils.get_balanced_loss_weights_multiclass( labels=tf.expand_dims(instances, axis=1)) params['weights'] = weights return classification_loss_fn(logits=logits, labels=labels, **params)
def _build_op(self): h = FullyConnected(output_shape=self._output_shape, scope=self._scope)(self.x, self.keep_prob) h = tf.layers.dense(tf.concat([h, self.y_features], axis=2), units=128, activation=tf.nn.tanh) h = tf.layers.dense(h, units=1, activation=None) self.h = self._project_output(h)
def classification_loss_using_mask_iou_func_unbatched( embeddings, instance_ids, sampled_embeddings, sampled_instance_ids, sampled_class_labels, sampled_logits, similarity_strategy, is_balanced): """Classification loss using mask iou. Args: embeddings: A tf.float32 tensor of size [n, f]. instance_ids: A tf.int32 tensor of size [n]. sampled_embeddings: A tf.float32 tensor of size [num_samples, f]. sampled_instance_ids: A tf.int32 tensor of size [num_samples]. sampled_class_labels: A tf.int32 tensor of size [num_samples, 1]. sampled_logits: A tf.float32 tensor of size [num_samples, num_classes]. similarity_strategy: Defines the method for computing similarity between embedding vectors. Possible values are 'dotproduct' and 'distance'. is_balanced: If True, the per-voxel losses are re-weighted to have equal total weight for foreground vs. background voxels. Returns: A tf.float32 loss scalar tensor. """ predicted_soft_masks = metric_learning_utils.embedding_centers_to_soft_masks( embedding=embeddings, centers=sampled_embeddings, similarity_strategy=similarity_strategy) predicted_masks = tf.cast(tf.greater(predicted_soft_masks, 0.5), dtype=tf.float32) gt_masks = tf.cast(tf.equal(tf.expand_dims(sampled_instance_ids, axis=1), tf.expand_dims(instance_ids, axis=0)), dtype=tf.float32) pairwise_iou = instance_segmentation_utils.points_mask_pairwise_iou( masks1=predicted_masks, masks2=gt_masks) num_classes = sampled_logits.get_shape().as_list()[1] sampled_class_labels_one_hot = tf.one_hot(indices=tf.reshape( sampled_class_labels, [-1]), depth=num_classes) sampled_class_labels_one_hot_fg = sampled_class_labels_one_hot[:, 1:] iou_coefs = tf.tile(tf.reshape(pairwise_iou, [-1, 1]), [1, num_classes - 1]) sampled_class_labels_one_hot_fg *= iou_coefs sampled_class_labels_one_hot_bg = tf.maximum( 1.0 - tf.math.reduce_sum( sampled_class_labels_one_hot_fg, axis=1, keepdims=True), 0.0) sampled_class_labels_one_hot = tf.concat( [sampled_class_labels_one_hot_bg, sampled_class_labels_one_hot_fg], axis=1) params = {} if is_balanced: weights = loss_utils.get_balanced_loss_weights_multiclass( labels=tf.expand_dims(sampled_instance_ids, axis=1)) params['weights'] = weights return classification_loss_fn(logits=sampled_logits, labels=sampled_class_labels_one_hot, **params)
def _build_op(self): batch_size = tf.shape(self.y)[0] seq_len = self.y.get_shape()[1] y_shape = self.y.get_shape()[2] y = self.y shape = [batch_size, seq_len, self.latent] z = tf.distributions.Normal(loc=0., scale=1.).sample(sample_shape=shape) z = tf.concat([z, self.y_features], axis=2) g = RGenerator(output_shape=y_shape, scope="generator") x_fake, _, _ = g(z, keep_prob=self.keep_prob) x_fake = tf.nn.tanh( x_fake) # i guess this is to bound the output of the lstm d = RDiscriminator(output_shape=1, scope="discriminator") self._true_d, _, _ = d(tf.concat([y, self.y_features], axis=2)) self._fake_d, _, _ = d(tf.concat([x_fake, self.y_features], axis=2)) self.h = x_fake[:, :, :1] self.x_fake = x_fake
def _build_op(self): h = self.embedding(self.x, 128) encoder_output, encoder_state, states = Recurrent( output_shape=self._output_shape)(h, keep_prob=self.keep_prob) encoder_output = tf.layers.dense(tf.concat( [encoder_output, self.y_features], axis=2), units=self._output_shape, activation=None) self.h = self._project_output(encoder_output) self._reg = self.reg_op([states])
def _network_adapter(self, states, scope): self._validate_states(states) with tf.name_scope('network'): # Since we decompose the slate optimization into an item-level # optimization problem, the observation space is the user state # observation plus all documents' observations. In the Dopamine DQN agent # implementation, there is one head for each possible action value, which # is designed for computing the argmax operation in the action space. # In our implementation, we generate one output for each document. q_value_list = [] for i in range(self._num_candidates): user = tf.squeeze(states[:, 0, :, :], axis=2) doc = tf.squeeze(states[:, i + 1, :, :], axis=2) q_value_list.append(self.network(user, doc, scope)) q_values = tf.concat(q_value_list, axis=1) return dqn_agent.DQNNetworkType(q_values)
def _prepare_lidar_points(inputs, lidar_names): """Integrates and returns the lidar points in vehicle coordinate frame.""" points_position = [] points_intensity = [] points_elongation = [] points_normal = [] points_in_image_frame_xy = [] points_in_image_frame_id = [] for lidar_name in lidar_names: lidar_location = tf.reshape( inputs[('lidars/%s/extrinsics/t') % lidar_name], [-1, 3]) inside_no_label_zone = tf.reshape( inputs[('lidars/%s/pointcloud/inside_nlz' % lidar_name)], [-1]) valid_points_mask = tf.math.logical_not(inside_no_label_zone) points_position_current_lidar = tf.boolean_mask( inputs[('lidars/%s/pointcloud/positions' % lidar_name)], valid_points_mask) points_position.append(points_position_current_lidar) points_intensity.append( tf.boolean_mask( inputs[('lidars/%s/pointcloud/intensity' % lidar_name)], valid_points_mask)) points_elongation.append( tf.boolean_mask( inputs[('lidars/%s/pointcloud/elongation' % lidar_name)], valid_points_mask)) points_to_lidar_vectors = lidar_location - points_position_current_lidar points_normal_direction = points_to_lidar_vectors / tf.expand_dims( tf.norm(points_to_lidar_vectors, axis=1), axis=1) points_normal.append(points_normal_direction) points_in_image_frame_xy.append( tf.boolean_mask( inputs['lidars/%s/camera_projections/positions' % lidar_name], valid_points_mask)) points_in_image_frame_id.append( tf.boolean_mask( inputs['lidars/%s/camera_projections/ids' % lidar_name], valid_points_mask)) points_position = tf.concat(points_position, axis=0) points_intensity = tf.concat(points_intensity, axis=0) points_elongation = tf.concat(points_elongation, axis=0) points_normal = tf.concat(points_normal, axis=0) points_in_image_frame_xy = tf.concat(points_in_image_frame_xy, axis=0) points_in_image_frame_id = tf.cast(tf.concat(points_in_image_frame_id, axis=0), dtype=tf.int32) points_in_image_frame_yx = tf.cast(tf.reverse(points_in_image_frame_xy, axis=[-1]), dtype=tf.int32) return (points_position, points_intensity, points_elongation, points_normal, points_in_image_frame_yx, points_in_image_frame_id)
def prepare_dataset(dataset_name=gin.REQUIRED, shuffle_input_sentences=False, num_eval_examples=2000, batch_size=32): """Create batched, properly-formatted datasets from the TFDS datasets. Args: dataset_name: Name of TFDS dataset. shuffle_input_sentences: Not used during evaluation, but arg still needed for gin compatibility. num_eval_examples: Number of examples to use during evaluation. For the nolabel evaluation, this is also the number of distractors we choose between. batch_size: Batch size. Returns: The validation dataset, the story identifiers for each story in the embedding matrix, and the embedding matrix. """ del num_eval_examples del shuffle_input_sentences splits_to_load = [ tfds.Split.TRAIN, rocstories_sentence_embeddings.VALIDATION_2018, ] tfds_train, tfds_valid = tfds.load(dataset_name, data_dir=FLAGS.data_dir, split=splits_to_load) _, train_embs, train_story_ids = utils.build_train_style_dataset( tfds_train, batch_size, shuffle_input_sentences=False, return_ids=True, is_training=False) out = build_all_distractor_valid_dataset(tfds_valid, batch_size=batch_size) valid_dataset, valid_embs, valid_story_ids = out all_story_ids = valid_story_ids + train_story_ids all_emb_matrix = tf.concat([valid_embs, train_embs], axis=0) return valid_dataset, all_story_ids, all_emb_matrix