def vertices3d_to_box3d(vertices: Tensor): center = vertices.sum(axis=0) # right upper left lower in xy plane right_index, far_index, high_index = vertices.argmax(axis=0) left_index, _, low_index = vertices.argmin(axis=0) x_right, y_right, _ = vertices[right_index] x_far, y_far, _ = vertices[far_index] x_left, y_left, _ = vertices[left_index] _, _, z_high = vertices[high_index] _, _, z_low = vertices[low_index] far_right_x_delta = np.abs(x_right - x_far) far_right_y_delta = np.abs(y_right - y_far) far_left_x_delta = np.abs(x_left - x_far) far_left_y_delta = np.abs(y_left - y_far) angle = np.arctan(far_right_x_delta / far_right_y_delta) #TODO atan2 y_size = np.sqrt(far_right_x_delta**2 + far_right_y_delta**2) x_size = np.sqrt(far_left_x_delta**2 + far_left_y_delta**2) z_size = z_high - z_low extent = torch.tensor([x_size, y_size, z_size]) angle = torch.tensor([angle]) box = torch.cat((center, extent, angle), dim=0) return box
def _extremity_game_target_function(context: torch.Tensor, function_selectors: torch.Tensor, target_type) -> torch.Tensor: func_idxs = function_selectors.argmax(dim=1) func_min_or_max = func_idxs % 2 param_idxs = func_idxs // 2 min_obj_per_param = context.argmin( dim=1) # index of min property along the row (index of the object) max_obj_per_param = context.argmax(dim=1) targets = [] targets2 = [] # previous targets for batch in range(context.shape[0]): num_object = context.size()[1] if func_min_or_max[batch] == 0: # batch = batch id # min_obj_per_param[batch][param_idxs[batch]]: index of min targets2.append( context[batch, min_obj_per_param[batch][param_idxs[batch]]]) # MY CODE t_id = torch.zeros(num_object).long() o_id = min_obj_per_param[batch][param_idxs[batch]] # object id t_id[o_id] = 1 # one-hot tensor targets.append(o_id) # print("min_obj_per_param[batch][param_idxs[batch]]: ", min_obj_per_param[batch][param_idxs[batch]]) else: targets2.append( context[batch, max_obj_per_param[batch][param_idxs[batch]]]) # MY CODE t_id = torch.zeros(num_object).long() o_id = max_obj_per_param[batch][param_idxs[batch]] # object id t_id[o_id] = 1 # one-hot tensor targets.append(o_id) # print(" max_obj_per_param[batch][param_idxs[batch]]: ", max_obj_per_param[batch][param_idxs[batch]]) # print("context: ", context) # print("targets: ", targets) # print("targets2: ", targets2) # print(" torch.stack(targets)", torch.stack(targets)) # print("torch.stack(targets2) : ", torch.stack(targets2) ) if target_type == "target_properties": return torch.stack(targets2) # elif target_type == "target_id": # target_id return torch.stack(targets) else: print("invalid target type") exit()
def find_bmus(self, distances: torch.Tensor) -> torch.Tensor: """ Find BMU for each batch in `distances`. Parameters ---------- distances : torch.Tensor The data-to-codebook distances. """ # distances shape: [bs, codebook_size] # argmin shape : [bs, 1] min_idxs = distances.argmin(-1) # Distances are flattened, so we need to transform 1d indices into 2d map locations return idxs_1d_to_2d(min_idxs, self.size[1]).to(distances.device)
def _extremity_game_target_function( context: torch.Tensor, function_selectors: torch.Tensor) -> torch.Tensor: func_idxs = function_selectors.argmax(dim=1) func_min_or_max = func_idxs % 2 param_idxs = func_idxs // 2 min_obj_per_param = context.argmin(dim=1) max_obj_per_param = context.argmax(dim=1) targets = [] for batch in range(context.shape[0]): if func_min_or_max[batch] == 0: targets.append( context[batch, min_obj_per_param[batch][param_idxs[batch]]]) else: targets.append( context[batch, max_obj_per_param[batch][param_idxs[batch]]]) return torch.stack(targets)
def update(self, pixel_error: torch.Tensor): if not self.is_fixed: self.optimal_id = pixel_error.argmin()