### Clone and Install Repository Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Clone the forked repository and install the project in editable mode with development dependencies. This setup is crucial for local development and testing. ```bash git clone git@github.com:YourLogin/reptrix.git cd reptrix pip install -U pip setuptools -e . ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/barl-ssl/reptrix/blob/main/docs/readme.md Installs pre-commit and sets up the git hooks for the repository. ```bash pip install pre-commit cd reptrix pre-commit install ``` -------------------------------- ### Setup Conda Environment Source: https://github.com/barl-ssl/reptrix/blob/main/docs/readme.md Commands to create and activate a conda environment, then install the library in editable mode. ```bash conda env create -f conda_env.yaml conda activate reptrix pip install -e . ``` -------------------------------- ### Install Development Dependencies Source: https://github.com/barl-ssl/reptrix/blob/main/docs/contributing.md Install the project in editable mode and set up pre-commit hooks for code quality checks. ```bash pip install -U pip setuptools -e . pip install pre-commit pre-commit install ``` -------------------------------- ### Import Libraries and Setup Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Imports necessary PyTorch and Reptrix libraries. Sets up the computation device (GPU or CPU). ```python import torch import torchvision from tqdm import tqdm from reptrix import alpha, rankme, lidar import reptrix import time ``` ```python device = torch.device("cuda" if torch.cuda.is_available() else "cpu") ``` -------------------------------- ### Install Reptrix from GitHub Source: https://github.com/barl-ssl/reptrix/blob/main/README.md Clone the repository and install Reptrix manually using pip. This is useful for development or when needing the latest unreleased version. ```bash pip install git+https://github.com/BARL-SSL/reptrix ``` -------------------------------- ### Install Reptrix using pip Source: https://github.com/barl-ssl/reptrix/blob/main/README.md Use this command to install the latest version of Reptrix directly from the Python Package Index. ```bash pip install reptrix ``` -------------------------------- ### Create and activate a virtual environment for tox Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Set up a dedicated virtual environment, install tox within it, and then run tox commands. This can resolve issues with existing tox installations. ```bash virtualenv .venv source .venv/bin/activate .venv/bin/pip install tox .venv/bin/tox -e all ``` -------------------------------- ### Install and Configure Pre-commit Hooks Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Install pre-commit and its hooks to automatically format and lint code before committing. This ensures code quality and consistency. ```bash pip install pre-commit pre-commit install ``` -------------------------------- ### Check tox version and location Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Verify your tox installation and the Python version it uses. This helps in troubleshooting tox-related errors. ```bash tox --version ``` ```bash which tox ``` -------------------------------- ### Create Virtual Environment with Conda Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Set up an isolated Python virtual environment using Conda. This command installs necessary development packages like pytest. ```bash conda create -n reptrix python=3 six virtualenv pytest pytest-cov conda activate reptrix ``` -------------------------------- ### Load Pretrained ResNet50 Model Source: https://github.com/barl-ssl/reptrix/blob/main/docs/readme.md Loads a pretrained ResNet50 model using torch.hub.load from the facebookresearch/barlowtwins repository. This is a common starting point for evaluating representation quality. ```default encoder = torch.hub.load('facebookresearch/barlowtwins:main', 'resnet50') ``` -------------------------------- ### Get Representation Shape Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Displays the shape of the extracted feature representations. ```python all_representations.shape ``` -------------------------------- ### Get Features with SSL Transformations and Compute LiDAR Metric Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Extracts features using SSL transformations and computes the LiDAR metric. This involves subsampling representations and timing the LiDAR computation. ```python # Get features of all the augmentations for LiDAR metric evaluation num_augmentations = 10 all_representations_ssl = get_features(encoder.forward, testloader, transform=transform_ssl, num_augmentations=num_augmentations) print("Shape of the representations after SSL transformations:", all_representations_ssl.shape) num_samples = all_representations_ssl.shape[0] // num_augmentations # Randomly sample num_samples number of samples from the all_representations_ssl ramdom_sample_index = torch.randperm(all_representations_ssl.shape[0])[:num_samples] subsampled_representations_ssl = all_representations_ssl[ramdom_sample_index] # Get the Lidar metric start_time = time.time() lidar_metric = lidar.get_lidar(subsampled_representations_ssl, num_samples, num_augmentations, del_sigma_augs=0.001) lidar_time = time.time() lidar_compute_time = lidar_time - start_time # For lidar metric evaluation, we need to use the SSL transformations over the test dataset print(f'Values of different metrics: Alpha: {metric_alpha[0]}, Rankme: {metric_rankme}, LiDAR: {lidar_metric}') print(f'Compute time for different metrics: Alpha: {alpha_compute_time:.3f}s, Rankme: {rankme_compute_time:.3f}s, LiDAR: {lidar_compute_time:.3f}s') ``` -------------------------------- ### Set Model to Evaluation Mode and Get Features Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Sets the encoder to evaluation mode and extracts features using a specified dataloader and transformation. This is a prerequisite for metric computation. ```python encoder.eval() all_representations = get_features(encoder.forward, testloader, transform=transform_base, num_augmentations=1) print('Shape of the representations:', all_representations.shape) ``` -------------------------------- ### Compile and Preview Documentation Source: https://github.com/barl-ssl/reptrix/blob/main/docs/contributing.md Use tox to compile documentation and Python's http.server to preview it locally. ```bash tox -e docs ``` ```bash python3 -m http.server --directory 'docs/_build/html' ``` -------------------------------- ### Preview Documentation Locally Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Serve the compiled documentation locally using Python's http.server. This allows for easy previewing of documentation changes before committing. ```bash python3 -m http.server --directory 'docs/_build/html' ``` -------------------------------- ### Recreate tox environment Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md If tox encounters issues with missing dependencies, recreate the environment using the -r flag. This is useful when setup.cfg or docs/requirements.txt have been updated. ```bash tox -r -e docs ``` -------------------------------- ### Create Virtual Environment with virtualenv Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Set up an isolated Python virtual environment using virtualenv. This is a standard practice to manage project dependencies separately. ```bash virtualenv source /bin/activate ``` -------------------------------- ### Run all pre-commit checks Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Execute all pre-commit hooks across all files in the repository to ensure code style and quality standards are met. ```bash pre-commit run --all-files ``` -------------------------------- ### Run unit tests with tox Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Execute the project's unit tests using tox to verify that your changes do not introduce regressions. ```bash tox ``` -------------------------------- ### Compile Documentation with Tox Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Use tox to compile documentation. This command ensures that the documentation is built in an isolated environment, matching the project's build process. ```bash tox -e docs ``` -------------------------------- ### Prepare DINO Encoder for Device Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Moves the loaded DINO ViT encoder model to the specified device. ```python # Move the model to the device encoder = encoder.to(device) ``` -------------------------------- ### Clone Reptrix Repository Source: https://github.com/barl-ssl/reptrix/blob/main/docs/contributing.md Clone the forked Reptrix repository to your local machine and navigate into the directory. ```bash git clone git@github.com:YourLogin/reptrix.git cd reptrix ``` -------------------------------- ### Load and Prepare Encoder for Feature Extraction Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Moves the encoder model to the specified device and sets it to evaluation mode. It then extracts features using the provided dataloader and transformations. ```python # Move the model to the device encoder = encoder.to(device) # Set the model to evaluation mode encoder.eval() all_representations = get_features(encoder, testloader, transform=transform_base, num_augmentations=1) ``` -------------------------------- ### STL-10 Dataset and Transformations Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Loads the STL-10 test dataset and defines necessary transformations, including base normalization and SSL-specific augmentations for metrics like LiDAR. ```python transform_to_tensor = torchvision.transforms.ToTensor() STL_MEAN = (0.4467, 0.4398, 0.4066) STL_STD = (0.2242, 0.2215, 0.2239) transform_base = torchvision.transforms.Normalize(STL_MEAN, STL_STD) # Define additional SSL transformations for the Lidar metric evaluation transform_ssl = torchvision.transforms.Compose([ torchvision.transforms.RandomHorizontalFlip(p=0.5), torchvision.transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1), torchvision.transforms.RandomGrayscale(p=0.2), torchvision.transforms.RandomResizedCrop( 96, scale=(0.8, 1.0), ratio=(0.75, (4/3)), interpolation=torchvision.transforms.InterpolationMode.BICUBIC), torchvision.transforms.Normalize(STL_MEAN, STL_STD) ]) dataset_folder = '/network/datasets/stl10.var/stl10_torchvision' # Get the STL10 test dataset to measure the quality of the representations learned by the model # testset = torchvision.datasets.STL10(root='./data', split='test', download=False, transform=transform) testset = torchvision.datasets.STL10(root=dataset_folder, split='test', download=False, transform=transform_to_tensor) # Define a dataloader to load the test dataset testloader = torch.utils.data.DataLoader(testset, batch_size=256, shuffle=False, num_workers=4) ``` -------------------------------- ### Load ViT Encoder Trained with DINO Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Loads a pre-trained Vision Transformer (ViT) encoder trained using the DINO self-supervised learning method from PyTorch Hub. ```python # Define a vit encoder using pytorch where you can load the weights from a pre-trained model # We will use encoder that is trained using Dino encoder = torch.hub.load('facebookresearch/dino:main', 'dino_vits16') ``` -------------------------------- ### Push local branch to remote Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Once your work is complete and verified, push your local branch to the remote server to prepare for a pull request. ```git git push -u origin my-feature ``` -------------------------------- ### Compute Alpha and RankMe Metrics Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Calculates the Alpha and RankMe metrics from the extracted representations and records the computation time for each. ```python start_time = time.time() metric_alpha = alpha.get_alpha(all_representations) alpha_time = time.time() metric_rankme = rankme.get_rankme(all_representations) rankme_time = time.time() alpha_compute_time = alpha_time - start_time rankme_compute_time = rankme_time - alpha_time ``` -------------------------------- ### Stage and commit changes Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md After completing your edits, stage the modified files and commit them. Ensure code style is checked by pre-commit. ```git git add git commit ``` -------------------------------- ### Create a new branch for changes Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Before making any modifications, create a new branch for your feature. Avoid working directly on the main branch. ```git git checkout -b my-feature ``` -------------------------------- ### Update Pre-commit Hooks Source: https://github.com/barl-ssl/reptrix/blob/main/docs/readme.md Updates the pre-commit hooks to their latest versions. ```bash pre-commit autoupdate ``` -------------------------------- ### Load Pretrained ResNet50 Encoder Source: https://github.com/barl-ssl/reptrix/blob/main/README.md Loads a pretrained ResNet50 model from PyTorch Hub and removes the final fully connected layer to extract feature vectors. Ensure the model is set to evaluation mode. ```python encoder = torch.hub.load('facebookresearch/barlowtwins:main', 'resnet50') # Remove the final fully connected layer so that the model outputs the 2048 feature vector encoder = torch.nn.Sequential(*(list(encoder.children())[:-1])) encoder.eval() ``` -------------------------------- ### Compute LiDAR Metric Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Extracts features with SSL transformations, subsamples them, and then computes the LiDAR metric, recording the computation time. ```python # Get features of all the augmentations for LiDAR metric evaluation num_augmentations = 10 all_representations_ssl = get_features(encoder.forward, testloader, transform=transform_ssl, num_augmentations=num_augmentations) num_samples = all_representations_ssl.shape[0] // num_augmentations # Randomly sample num_samples number of samples from the all_representations_ssl ramdom_sample_index = torch.randperm(all_representations_ssl.shape[0])[:num_samples] subsampled_representations_ssl = all_representations_ssl[ramdom_sample_index] # Get the Lidar metric start_time = time.time() lidar_metric = lidar.get_lidar(subsampled_representations_ssl, num_samples, num_augmentations, del_sigma_augs=0.001) lidar_time = time.time() lidar_compute_time = lidar_time - start_time # For lidar metric evaluation, we need to use the SSL transformations over the test dataset print(f'Values of different metrics: Alpha: {metric_alpha[0]}, Rankme: {metric_rankme}, LiDAR: {lidar_metric}') print(f'Compute time for different metrics: Alpha: {alpha_compute_time:.3f}s, Rankme: {rankme_compute_time:.3f}s, LiDAR: {lidar_compute_time:.3f}s') ``` -------------------------------- ### Run tests with pytest and enable interactive debugger Source: https://github.com/barl-ssl/reptrix/blob/main/CONTRIBUTING.md Execute tests using pytest and enable the interactive debugger (pdb) upon encountering an error by passing the --pdb option. This is useful for diagnosing failing tests. ```bash tox -- -k --pdb ``` -------------------------------- ### Load Barlow Twins ResNet50 Encoder Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Loads a ResNet50 encoder pretrained with the Barlow Twins method using PyTorch Hub. ```python # Define a resnet encoder using pytorch where you can load the weights from a pre-trained model # We will use encoder that is trained using Barlow Twins encoder = torch.hub.load('facebookresearch/barlowtwins:main', 'resnet50') ``` -------------------------------- ### Extract Features from DataLoader Source: https://github.com/barl-ssl/reptrix/blob/main/README.md A utility function to extract features from a PyTorch DataLoader. It supports applying multiple augmentations to each input and concatenates all extracted features. Use `tqdm` for progress visualization. ```python def get_features(encoder_network, dataloader, transform=None, num_augmentations=10): # Loop over the dataset and collect the representations all_features = [] # Loop over the dataset and collect the representations for i, data in enumerate(tqdm(dataloader, 0)): inputs, _ = data if transform: inputs = torch.cat([transform(inputs) for _ in range(num_augmentations)], dim=0) with torch.no_grad(): features = encoder_network(inputs) if transform: # put the augmentations in an additonal dimension features = features.reshape(-1, num_augmentations, features.shape[1]) all_features.append(features) # Concatenate all the features all_features = torch.cat(all_features, dim=0) return all_features all_representations = get_features(encoder, loader) num_augmentations = 10 all_representations_lidar = get_features(encoder, loader, transform=transform_augs, num_augmentations=num_augmentations) num_samples = all_representations_lidar.shape[0] ``` -------------------------------- ### reptrix.utils.mat_sqrt_inv Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Computes the square root of the inverse of a square, positive definite matrix. ```APIDOC ## reptrix.utils.mat_sqrt_inv(matrix: Tensor) -> Tensor ### Description Compute the square root of inverse of a square (positive definite) matrix. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **matrix** (torch.Tensor) - Required - Matrix to be be sqrt inverted ### Request Example None ### Response #### Success Response (200) * **sqrt_inv_matrix** (torch.Tensor) - Square root of inverse of matrix #### Response Example None ``` -------------------------------- ### Compute Representation Metrics Source: https://github.com/barl-ssl/reptrix/blob/main/docs/readme.md Computes representation quality metrics including alpha, RankMe, and LiDAR using the extracted features. ```python from reptrix import alpha, rankme, lidar metric_alpha = alpha.get_alpha(all_representations) metric_rankme = rankme.get_rankme(all_representations) metric_lidar = lidar.get_lidar(all_representations_lidar, num_samples, num_augmentations, del_sigma_augs=0.00001) ``` -------------------------------- ### Plot Eigenspectrum and Powerlaw Fit Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Calculates the eigenspectrum of the representations and plots it along with the powerlaw fit using the computed Alpha metric. ```python # Plot the eigenspectrum and the powerlaw fit eigenspectrum = reptrix.utils.get_eigenspectrum(all_representations.cpu()) # reptrix.utils.plot_eigenspectrum(eigenspectrum) reptrix.alpha.plot_powerlaw(eigenspectrum, metric_alpha) ``` -------------------------------- ### reptrix.utils.get_eigenspectrum Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Computes the eigenspectrum of the activation covariance matrix. ```APIDOC ## reptrix.utils.get_eigenspectrum(activations_np: ndarray, max_eigenvals: int = 2048) -> ndarray ### Description Get eigenspectrum of activation covariance matrix. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **activations_np** (np.ndarray) - Required - Numpy arr of activations, shape (bsz,d1,d2…dn) * **max_eigenvals** (int) - Optional - Maximum #eigenvalues to compute. Defaults to 2048. ### Request Example None ### Response #### Success Response (200) * **eigenspectrum** (np.ndarray) - Returns the eigenspectrum of the activation covariance matrix #### Response Example None ``` -------------------------------- ### reptrix.alpha.plot_powerlaw Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Plots the eigenspectrum along with the calculated powerlaw fit. ```APIDOC ## reptrix.alpha.plot_powerlaw(eigenspectrum: ndarray, alpha_res: tuple) -> None ### Description Plot eigenspectrum and powerlaw fit. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **eigenspectrum** (np.ndarray) - Required - Eigenspectrum of activation covariance matrix * **alpha_res** (tuple) - Required - Tuple containing alpha, powerlaw fit, goodness of powerlaw fit, goodness of powerlaw fit for first 100 eigenvalues. ### Request Example None ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### reptrix.alpha.get_powerlaw Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Fits a powerlaw to the provided eigenspectrum and returns the decay coefficient, the powerlaw fit, and the goodness of fit. ```APIDOC ## reptrix.alpha.get_powerlaw(eigen: ndarray, trange: ndarray) -> tuple ### Description Fit powerlaw and return decay, powerlaw fit and the goodness of fit. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **eigen** (np.ndarray) - Required - Eigenspectrum of activation covariance matrix * **trange** (np.ndarray) - Required - Range to fit the powerlaw. Tip: Ignore the first couple of eigenvalues because we want to fit the tail of the spectrum. ### Request Example None ### Response #### Success Response (200) * **alpha** (float) - powerlaw decay coefficient * **ypred** (tuple) - Powerlaw fit * **fit_R2** (float) - goodness of powerlaw fit * **fit_R2_100** (float) - goodness of powerlaw fit (computed till the 100 eigvals) #### Response Example None ``` -------------------------------- ### Extract Features from Pretrained Network Source: https://github.com/barl-ssl/reptrix/blob/main/docs/readme.md This function extracts features from a given dataloader using a pretrained encoder network. It supports optional transformations and data augmentations. ```python encoder = torch.nn.Sequential(*(list(encoder.children())[:-1])) encoder.eval() ``` ```python def get_features(encoder_network, dataloader, transform=None, num_augmentations=10): # Loop over the dataset and collect the representations all_features = [] # Loop over the dataset and collect the representations for i, data in enumerate(tqdm(dataloader, 0)): inputs, _ = data if transform: inputs = torch.cat([transform(inputs) for _ in range(num_augmentations)], dim=0) with torch.no_grad(): features = encoder_network(inputs) if transform: # put the augmentations in an additonal dimension features = features.reshape(-1, num_augmentations, features.shape[1]) all_features.append(features) # Concatenate all the features all_features = torch.cat(all_features, dim=0) return all_features all_representations = get_features(encoder, loader) num_augmentations = 10 all_representations_lidar = get_features(encoder, loader, transform=transform_augs, num_augmentations=num_augmentations) num_samples = all_representations_lidar.shape[0] ``` -------------------------------- ### reptrix.utils.plot_eigenspectrum Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Plots the eigenspectrum in a log-log scale. This function is useful for visualizing the distribution of eigenvalues. ```APIDOC ## reptrix.utils.plot_eigenspectrum ### Description Plots the eigenspectrum in a log-log scale. This function is useful for visualizing the distribution of eigenvalues. ### Parameters #### Parameters - **eigenspectrum** (np.ndarray) - Required - Eigenspectrum of activation covariance matrix ``` -------------------------------- ### reptrix.rankme.get_rankme Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Calculates the RankMe metric for a given activation tensor. This metric is used to assess the intrinsic dimensionality of the representation. ```APIDOC ## reptrix.rankme.get_rankme(activations: Tensor, max_eigenvals: int = 2048) -> float ### Description Get RankMe metric. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **activations** (np.ndarray) - Required - Activation tensor of shape (bsz,d1,d2…dn) * **max_eigenvals** (int) - Optional - Maximum #eigenvalues to compute. Defaults to 2048. ### Request Example None ### Response #### Success Response (200) * **RankMe metric** (float) - RankMe metric #### Response Example None ``` -------------------------------- ### reptrix.alpha.get_alpha Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Calculates the alpha value and powerlaw fit for a given activation tensor. This function is useful for analyzing the decay characteristics of activation spectra. ```APIDOC ## reptrix.alpha.get_alpha(activations: Tensor, max_eigenvals: int = 2048, fit_range: ndarray = None) -> tuple ### Description Get alpha and powerlaw fit. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **activations** (np.ndarray) - Required - Activation tensor of shape (bsz,d1,d2…dn) * **max_eigenvals** (int) - Optional - Maximum #eigenvalues to compute. Defaults to 2048. * **fit_range** (np.ndarray) - Optional - Range to fit the powerlaw. Defaults to np.arange(10,100). ### Request Example None ### Response #### Success Response (200) * **alpha** (float) - powerlaw decay coefficient * **ypred** (tuple) - Powerlaw fit * **fit_R2** (float) - goodness of powerlaw fit * **fit_R2_100** (float) - goodness of powerlaw fit (computed till the 100 eigvals) #### Response Example None ``` -------------------------------- ### Feature Extraction Function Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Defines a function to extract features from a dataset using a given encoder and optional transformations. It handles multiple augmentations per image. ```python def get_features(encoder_function, dataloader, transform=None, num_augmentations=10): # Loop over the dataset and collect the representations all_features = [] # Loop over the dataset and collect the representations for i, data in enumerate(tqdm(dataloader, 0)): inputs, _ = data # apply multiple augmentations for each image if transform: inputs = torch.cat([transform(inputs) for _ in range(num_augmentations)], dim=0) with torch.no_grad(): features = encoder_function(inputs.to(device)) if transform: # put the augmentations in an additonal dimension features = features.reshape(num_augmentations, -1, features.shape[1]).transpose(1,0) all_features.append(features) # Concatenate all the features all_features = torch.cat(all_features, dim=0) return all_features ``` -------------------------------- ### reptrix.lidar.get_lidar Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Calculates the LiDAR (Latent Information Detection and Representation) metric for a given activation tensor. This metric is used to estimate the dimensionality of the data manifold. ```APIDOC ## reptrix.lidar.get_lidar(activations: Tensor, num_samples: int, num_augs: int, del_sigma_augs: float = 1e-06, max_eigenvals: int = 2048) -> float ### Description Get RankMe metric. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **activations** (torch.Tensor) - Required - Activation tensor of shape either (num_samples, num_augs, d1,d2…dn) or (num_samples * num_augs, d1,d2…dn) * **num_samples** (int) - Required - Number of unique inputs/samples * **num_augs** (int) - Required - Number of augmentations for each input used to estimate object manifold * **del_sigma_augs** (float) - Optional - A small positive constant that is added to make sure matrices are invertible. Defaults to 1e-6. * **max_eigenvals** (int) - Optional - Maximum #eigenvalues to compute. Defaults to 2048. ### Request Example None ### Response #### Success Response (200) * **LiDAR metric** (float) - LiDAR metric #### Response Example None ``` -------------------------------- ### reptrix.lidar.get_rank Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Computes the effective rank of the LDA (Linear Discriminant Analysis) covariance matrix from its eigenspectrum. ```APIDOC ## reptrix.lidar.get_rank(eigen: ndarray) -> float ### Description Get effective rank of the LDA covariance matrix. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **eigen** (np.ndarray) - Required - Eigenspectrum of the LDA covariance matrix ### Request Example None ### Response #### Success Response (200) * **Effective rank** (float) - Effective rank #### Response Example None ``` -------------------------------- ### reptrix.rankme.get_rank Source: https://github.com/barl-ssl/reptrix/blob/main/docs/api/reptrix.md Computes the effective rank of the representation covariance matrix from its eigenspectrum. ```APIDOC ## reptrix.rankme.get_rank(eigen: ndarray) -> float ### Description Get effective rank of the representation covariance matrix. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **eigen** (np.ndarray) - Required - Eigenspectrum of the representation covariance matrix ### Request Example None ### Response #### Success Response (200) * **Effective rank** (float) - Effective rank #### Response Example None ``` -------------------------------- ### Remove Final Layer from Encoder Source: https://github.com/barl-ssl/reptrix/blob/main/tutorial.ipynb Removes the final fully connected layer from a PyTorch encoder model to output a feature vector. ```python encoder = torch.nn.Sequential(*(list(encoder.children())[:-1])) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.