def knn_indices_func_approx( rep_pts: FloatTensor, # (N, pts, dim) pts: FloatTensor, # (N, x, dim) K: int, D: int) -> LongTensor: # (N, pts, K) """ Approximate CPU-based Indexing function based on K-Nearest Neighbors search. :param rep_pts: Representative points. :param pts: Point cloud to get indices from. :param K: Number of nearest neighbors to collect. :param D: "Spread" of neighboring points. :return: Array of indices, P_idx, into pts such that pts[n][P_idx[n],:] is the set k-nearest neighbors for the representative points in pts[n]. """ if rep_pts.is_cuda: rep_pts = rep_pts.cpu() if pts.is_cuda: pts = pts.cpu() rep_pts = rep_pts.data.numpy() pts = pts.data.numpy() region_idx = [] for n, p in enumerate(rep_pts): P_particular = pts[n] lshf = LSHForest(n_estimators=20, n_candidates=100, n_neighbors=D * K + 1) lshf.fit(P_particular) indices = lshf.kneighbors(p, return_distance=False) region_idx.append(indices[:, 1::D])
def knn_indices_func_cpu( rep_pts: FloatTensor, # (N, pts, dim) pts: FloatTensor, # (N, x, dim) K: int, D: int) -> LongTensor: # (N, pts, K) """ CPU-based Indexing function based on K-Nearest Neighbors search. :param rep_pts: Representative points. :param pts: Point cloud to get indices from. :param K: Number of nearest neighbors to collect. :param D: dilatation factor :return: Array of indices, P_idx, into pts such that pts[n][P_idx[n],:] is the set k-nearest neighbors for the representative points in pts[n]. """ if rep_pts.is_cuda: rep_pts = rep_pts.cpu() if pts.is_cuda: pts = pts.cpu() rep_pts = rep_pts.data.numpy() pts = pts.data.numpy() region_idx = [] for n, p in enumerate(rep_pts): P_particular = pts[n] nbrs = NearestNeighbors(D * K + 1, algorithm="auto").fit(P_particular) indices = nbrs.kneighbors(p)[1] region_idx.append(indices[:, 1::D]) region_idx = torch.from_numpy(np.stack(region_idx, axis=0)) return region_idx
def compute_accuracy( self, y_pred: torch.FloatTensor, y_target: torch.FloatTensor ) -> float: "Acccuracy metric for this regression problem is R2" y_pred = y_pred.cpu().detach().numpy() y_target = y_target.cpu().detach().numpy() return metrics.r2_score(y_target, y_pred)
def correct_labels(inputs_: torch.FloatTensor, preds: torch.FloatTensor, targets: torch.FloatTensor, dl): """ Returns the number of correct labels from the inputs, the targets and the predicted targets :param inputs_: :type inputs_: shape: (Batch size, BPTT, n_in) :param preds: :type preds: :param targets: the output vectors not labelled :type targets: shape: (Batch size, BPTT, n_out) :param dl: :type dl: :return: :rtype: """ # If `preds` has not the required size, it is resized before being # passed to `dl.label_data` preds = preds.view(targets.size()) # Data are moved on the CPU to avoid to take space on CUDA inputs_, true_labels = dl.label_data(inputs_.cpu(), targets.cpu()) inputs_, pred_labels = dl.label_data(inputs_, preds.cpu()) true_labels = true_labels.view(-1).numpy() pred_labels = pred_labels.view(-1).numpy() return (true_labels == pred_labels).sum()
class Sample(object): def __init__(self, obs, dists, values=[], pos=None, is_pad=False, is_end=False): self.dists = dists self.values = Tensor(values) # direction self.obs = Tensor(obs) # observed features self.is_pad = is_pad self.is_end = is_end if pos is not None: self.pos = Tensor(pos) self.rnn_input = None self.rnn_output = None def cuda(self, device=None): for i in range(len(self.dists)): if 'loc' in self.dists[i].__dict__: self.dists[i].loc = self.dists[i].loc.cuda(device) if 'scale' in self.dists[i].__dict__: self.dists[i].scale = self.dists[i].scale.cuda(device) if 'prob' in self.dists[i].__dict__: self.dists[i].prob = self.dists[i].prob.cuda(device) self.values = self.values.cuda(device) self.obs = self.obs.cuda(device) if self.pos is not None: self.pos = self.pos.cuda(device) def cpu(self): for i in range(len(self.values)): if 'loc' in self.dists[i].__dict__: self.dists[i].loc = self.dists[i].loc.cpu() if 'scale' in self.dists[i].__dict__: self.dists[i].scale = self.dists[i].scale.cpu() if 'prob' in self.dists[i].__dict__: self.dists[i].prob = self.dists[i].prob.cpu() self.values = self.values.cpu() self.obs = self.obs.cpu() if self.pos is not None: self.pos = self.pos.cpu()
def dump_batch_image(self, image: torch.FloatTensor, epoch: int, split: str, name: str): """ Dump image batch into folder (as grid) and tb TODO: something's wrong with the whole BGR2RGB stuff, we shouldn't need it """ assert split in self.sub_dirs assert len(image.shape ) == 4, f'shape {image.shape} differs from BxCxHxW format' assert image.min() >= 0 and image.max( ) <= 1, 'image must be between 0 and 1!' out_image_path = self.output_path[split] / f'{epoch:05d}_{name}.jpg' image_rolled = torchvision.utils.make_grid( image.cpu(), nrow=8, pad_value=1) #, normalize=True, scale_each=True) # Save image file TF.to_pil_image(image_rolled).save(out_image_path) # TensorBoard if self.tb is not None: self.tb.add_image(f'{split}/{name}', image_rolled, epoch) # CometML if self.cml is not None: self.cml.log_image(image_rolled, name=f'{split}/{name}', step=epoch, image_channels='first')
def digitize(self, layer_name: str, activations: torch.FloatTensor, n_bins: int) -> np.ndarray: activations = activations.cpu() bins = np.percentile(activations, q=np.linspace(start=0, stop=100, num=n_bins, endpoint=True), axis=0) digitized = np.empty_like(activations, dtype=np.int32) for dim in range(activations.shape[1]): digitized[:, dim] = np.digitize(activations[:, dim], bins[:, dim], right=True) return digitized
def label_data(input_: torch.FloatTensor, targets: torch.FloatTensor, cuda: bool = True) \ -> (torch.FloatTensor, torch.FloatTensor): """ Returns inputs and labels for the output of the model :param input_: :type input_: shape: (Batch size, BPTT, n_in) :param targets: the output vectors not labelled :type targets: shape: (Batch size, BPTT, n_out) :param cuda: :type cuda: :return: the inputs, and labels of `targets` :rtype: torch.FloatTensor, torch.FloatTensor shape: (Batch size, BPTT, n_in), (Batch size, BPTT) """ # Conversion to numpy targets = targets.cpu().numpy() # Creation of targets from the structure `targets` labelled = [] for s_n in targets: # Labels of the current sequence s_n (the nth sequence) labelled_data_n = [] for s_nt in s_n: # Labels over the time for the current sequence s_n # labelled_data_n += np.where(s_nt == 1)[0].tolist() labelled_data_n += [np.argmax(s_nt)] labelled += [labelled_data_n] labelled = torch.LongTensor(labelled) return input_, labelled
def get_softmax_labels(self, class_probabilities: torch.FloatTensor, mask: torch.Tensor) -> List[List[int]]: ''' This method has copied a large chunck of code from the `SimpleTagger.decode <https://github.com/allenai/allennlp/blob/master/allennlp/models/simple_tagger.py>`_ method. Parameters ---------- class_probabilities : A tensor containing the softmax scores for the tags. mask: A Tensor of 1's and 0's indicating whether a word exists. Returns ------- A List of Lists where each inner list contains integers representing the most likely tag label index based on the softmax scores. Only returns the tag label indexs for words that exist based on the mask provided. ''' all_predictions = class_probabilities.cpu().data.numpy() prediction_mask = mask.cpu().data.numpy() if all_predictions.ndim == 3: predictions_list = [ all_predictions[i] for i in range(all_predictions.shape[0]) ] else: predictions_list = [all_predictions] all_tags_indices = [] for prediction_index, predictions in enumerate(predictions_list): sequence_length = prediction_mask[prediction_index].sum() tag_indices = numpy.argmax(predictions, axis=-1).tolist()[:sequence_length] all_tags_indices.append(tag_indices) return all_tags_indices
def on_batch_end(self, last_output: torch.FloatTensor, last_target: torch.FloatTensor, **kwargs): last_output = self.predict(last_output) self.preds = torch.cat((self.preds, last_output.cpu())) self.targs = torch.cat((self.targs, last_target.cpu()))
def compute_manhattan_dist(tensor: torch.FloatTensor) -> float: l1_dist = pairwise.manhattan_distances(tensor.cpu()) upper_triangle_idx = np.triu_indices_from(l1_dist, k=1) l1_dist = l1_dist[upper_triangle_idx].mean() return l1_dist