def calculate_auc_score(y_true: np.array, y_pred: np.array) -> float: if type(y_true) == torch.Tensor: y_true = y_true.detach().numpy() if type(y_pred) == torch.Tensor: y_pred = y_pred.detach().numpy() fpr, tpr, thresholds = roc_curve(y_true, y_pred) return auc(fpr, tpr)
def calculate_model_metrics(y_true: np.array, y_pred: np.array, verbose: bool = True, mode: str = 'Test') -> Tuple[float, float, float]: """ Calculating Accuracy, recall, precision """ if type(y_true) == torch.tensor: y_true = y_true.detach().numpy() y_pred = convert_probs_to_preds(probs=y_pred) y_true, y_pred = y_true.reshape(-1), y_pred.reshape(-1) assert y_true.shape == y_pred.shape accuracy = accuracy_score(y_true=y_true, y_pred=y_pred) recall = recall_score(y_true=y_true, y_pred=y_pred) # , average='samples') precision = precision_score(y_true=y_true, y_pred=y_pred) # , average=='samples') if verbose: print(f'*** {mode} ***') print( f'Num of found targets: {(y_pred[np.where(y_true == 1)[0]] == 1).sum()} / {int(y_true.sum())}' ) print( f'Accuracy: {accuracy * 100:.2f}%; Recall: {recall * 100:.2f}%; Precision: {precision * 100:.2f}%' ) return accuracy, recall, precision
def get_user_items_scores_by_mask_matrix(y_pred_scores: np.array, mask_matrix: np.array) -> np.array: """ From y_pred_scores & mask_matrix, we create (NUM_USERS, 2) array for 2 items for each user with their value ma = mask array :return: array with the items belong to user """ ma = y_pred_scores.detach().numpy() * mask_matrix return ma[ma.nonzero()].reshape(-1, 2)
def rot_to_euler(R: np.array) -> np.array: """ :return: Euler angles in rad in ZYX """ import cv2 as cv if torch.is_tensor(R): R = R.detach().cpu().numpy() angles = np.radians(cv.RQDecomp3x3(R)[0]) angles[0], angles[2] = angles[2], angles[0] return angles
def _to_xarray_dataset(latlons: np.array, data: np.array, var_name: str = "data") -> xr.Dataset: """ create a 2D (single timestep) xr.Dataset """ # convert to numpy array if isinstance(data, Tensor): try: data = data.detach().numpy() except RuntimeError as E: print(E) data.numpy() if isinstance(latlons, Tensor): latlons = latlons.numpy() points = [i for i in range(len(latlons))] _vals = data.flatten() dims = ["point"] coords = {"point": points} return xr.Dataset({var_name: (dims, _vals)}, coords=coords)
def plot_preds(input_image: np.array, prediction: np.array, target: Tensor, predictions_dir: str, n_images: int = 2, save_fig: bool = True) -> None: """ Takes as input PyTorch tensors, plots the input image, predictions and targets Args: input_image : the input image predictions : the predictions thresholded at 0.5 probability target : Tensor of the targets predictions_dir: directory with oof predictions n_images : number of images to tack on the plot save_fig : if true, saves the figure. Default: True """ # Only select the first n images in a batch prediction = prediction[:n_images] # binarise prediction prediction = torch.sigmoid(prediction) prediction.round() target = target[:n_images] input_image = input_image[:n_images] prediction = prediction.detach().cpu().numpy() preds = np.hstack(prediction) preds_rgb = np.repeat(preds[..., None], 3, axis=2) # repeat for 3 channels to plot #thresholded_pred = np.repeat(preds_rgb[..., None] > 0.5, 3, axis=2) input_image = input_image.cpu().numpy().transpose(0,2,3,1) input_im = np.hstack(input_image[2]) # use Exy channel only for visualisation input_rgb = np.repeat(input_im[..., None], 3, axis=2) # repeat channel 3 times to plot overlayed_im = (input_rgb*0.6 + preds_rgb*0.7).clip(0,1) target = target.detach().cpu().numpy() target = np.hstack(target) target_rgb = np.repeat(target[..., None], 3, axis=2) # repeat for 3 channels to plot fig = plt.figure(figsize=(12,26)) plot_im = np.vstack([input_rgb, overlayed_im, preds_rgb, target_rgb]).clip(0,1).astype(np.float32) plt.imshow(plot_im) plt.axis("off") if save_fig: plt.savefig(os.path.join(predictions_dir, f"preds_{checkpoint_name}.png")) plt.show()
def calculate_MSE(y_true: np.array, y_pred: np.array) -> float: return mean_squared_error(y_true=y_true.detach().numpy(), y_pred=y_pred.detach().numpy())
def class_contour(x: np.array, y: np.array, clf: nn.Module, prec: float = 0.05, title: str = "Class Contour", ax: plt.Axes = None): """ Plots the class contour IF the dimnesions is 2 or lower. Otherwise the embeddings are visualized using TSNE. Paramters: x: np.array Feature matrix. y: np.array Labels for the x. clf: nn.Module Downstream classifier. prec: float Precision of the mesh grid (if it is created, i.e. when input dim is <=2) title: str Title of the plot. ax: matplotlib.Axes Axes for the plot. If None a new axis is created. Return: matplotlib.Figure: Figure of the plot IF the axis paramter is None. Otherwise nothing is returned. """ if isinstance(x, torch.Tensor): x = x.detach().cpu().numpy() if isinstance(y, torch.Tensor): y = y.detach().cpu().numpy() if len(x.shape) != 2: x = np.array(list(x)) if len(y.shape) != 2: y = np.array(list(y)) fig = None if ax is None: fig, ax = plt.subplots(figsize=(5, 5)) clf_device = clf.device mode = "" try: h = prec # step size in the mesh x_min, x_max = x[:, 0].min() - 0, x[:, 0].max() + min(h, 0) y_min, y_max = x[:, 1].min() - 0, x[:, 1].max() + min(h, 0) # np.linspace(2.0, 3.0, num=5) xx, yy = np.meshgrid( np.linspace(x_min, x_max, num=int((x_max - x_min) // h)), np.linspace(y_min, y_max, num=int((y_max - y_min) // h))) clf = clf.cpu() Z = clf( torch.tensor( np.c_[xx.ravel(), yy.ravel()], dtype=torch.float32).detach().cpu()).detach().cpu().numpy() Z = np.argmax(Z, axis=1) Z = Z.reshape(xx.shape) Z = ndimage.gaussian_filter(Z, sigma=1.0, order=0) ax.contourf(xx, yy, Z, alpha=0.8, cmap=sns.color_palette("Spectral", as_cmap=True)) except: # mode = " - TSNE" # x = TSNE(n_components=2).fit_transform(x) mode = " - PCA" x = PCA(n_components=2).fit_transform(x) ax.scatter(x[:, 0], x[:, 1], c=y.astype(int), cmap=sns.color_palette("Spectral", as_cmap=True), edgecolors='w') ax.set_title(f"{title} (Sample Shape: {x.shape}) {mode}") clf = clf.to(clf_device) if fig is not None: return fig