Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ziatdinovmax/gpim
Gaussian processes and Bayesian optimization for images and hyperspectral data
https://github.com/ziatdinovmax/gpim
bayesian-optimization colab-notebook gaussian-processes hyperspectral-images image-processing lattice-models microscopy
Last synced: 2 months ago
JSON representation
Gaussian processes and Bayesian optimization for images and hyperspectral data
- Host: GitHub
- URL: https://github.com/ziatdinovmax/gpim
- Owner: ziatdinovmax
- License: mit
- Created: 2020-02-18T19:28:27.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-11-24T06:29:59.000Z (about 1 year ago)
- Last Synced: 2024-05-08T05:35:43.873Z (8 months ago)
- Topics: bayesian-optimization, colab-notebook, gaussian-processes, hyperspectral-images, image-processing, lattice-models, microscopy
- Language: Python
- Homepage:
- Size: 53.3 MB
- Stars: 55
- Watchers: 6
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Downloads](https://pepy.tech/badge/gpim/month)](https://pepy.tech/project/gpim/month)
[![PyPI version](https://badge.fury.io/py/gpim.svg)](https://badge.fury.io/py/gpim)[![build](https://github.com/pycroscopy/atomai/actions/workflows/actions.yml/badge.svg)](https://github.com/ziatdinovmax/GPim/actions/workflows/actions.yml)
[![Documentation Status](https://readthedocs.org/projects/gpim/badge/?version=latest)](https://gpim.readthedocs.io/en/latest/?badge=latest)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/07ee1606a88b48d1bc46453f3ae1b1c8)](https://app.codacy.com/manual/ziatdinovmax/GPim?utm_source=github.com&utm_medium=referral&utm_content=ziatdinovmax/GPim&utm_campaign=Badge_Grade_Dashboard)[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ziatdinovmax/GPim/blob/master/examples/notebooks/Quickstart_GPim.ipynb)
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/ziatdinovmax/GPim)## What is GPim
GPim is a python package that provides an easy way to apply Gaussian processes (GP) in [Pyro](https://pyro.ai/) and [Gpytorch](https://gpytorch.ai/) to images and hyperspectral data and to perform GP-based Bayesian optimization on grid data. The intended audience are domain scientists (for example, microscopists) with a basic knowledge of how to work with numpy arrays in Python.
Scientific papers that use GPim:- GP for 3D hyperspectral microscopy data: [npj Comput Mater 6, 21 (2020)](https://www.nature.com/articles/s41524-020-0289-6)
- GP for 4D hyperspectral microscopy data: [Journal of Applied Physics 128, 055101 (2020)](https://aip.scitation.org/doi/10.1063/5.0013847)
- GP and GP-based BO for Ising model: [Journal of Applied Physics 128, 164304 (2020)](https://aip.scitation.org/doi/10.1063/5.0021762)
- GP-based BO for hysteresis loop engineering in ferroelectrics: [Journal of Applied Physics 128, 024102 (2020)](https://aip.scitation.org/doi/10.1063/5.0011917)
- GP-based BO for automated experiments in microscopy: [ACS Nano 15, 11253–11262 (2021)](https://doi.org/10.1021/acsnano.0c10239)
## Installation
First install [PyTorch](https://pytorch.org/). Then install GPim using
```bash
pip install gpim
```## How to use
### GP reconstructionBelow is a simple example of applying GPim to reconstructing a sparse 2D image. It can be similarly applied to 3D and 4D hyperspectral data. The missing data points in sparse data must be represented as [NaNs](https://docs.scipy.org/doc/numpy/reference/constants.html?highlight=numpy%20nan#numpy.nan). In the absense of missing observation GPim can be used for image and spectroscopic data cleaning/smoothing in all the dimensions simultaneously, as well as for the resolution enhancement.
```python
import gpim
import numpy as np# # Load dataset
R = np.load('sparse_exp_data.npy')# Get full (ideal) grid indices
X_full = gpim.utils.get_full_grid(R, dense_x=1)
# Get sparse grid indices
X_sparse = gpim.utils.get_sparse_grid(R)
# Kernel lengthscale constraints (optional)
lmin, lmax = 1., 4.
lscale = [[lmin, lmin], [lmax, lmax]]# Run GP reconstruction to obtain mean prediction and uncertainty for each predictied point
mean, sd, hyperparams = gpim.reconstructor(
X_sparse, R, X_full, lengthscale=lscale,
learning_rate=0.1, iterations=250,
use_gpu=True, verbose=False).run()# Plot reconstruction results
gpim.utils.plot_reconstructed_data2d(R, mean, cmap='jet')
# Plot evolution of kernel hyperparameters during training
gpim.utils.plot_kernel_hyperparams(hyperparams)
```### GP-based Bayesian optimization
When performing measurements (real or simulated), one can use the information about the expected function value and uncertainty in GP reconstruction to select the next measurement point. This is usually referred to as exploration-exploitation approach in the context of Bayesian optimization. A simple example with a "dummy" function is shown below.```python
import gpim
import numpy as np
np.random.seed(42)# Create a dummy 2D function
def trial_func(idx):
"""
Takes a list of indices as input and returns function value at these indices
"""
def func(x0, y0, a, b, fwhm):
return np.exp(-4*np.log(2) * (a*(idx[0]-x0)**2 + b*(idx[1]-y0)**2) / fwhm**2)
Z1 = func(5, 10, 1, 1, 4.5)
Z2 = func(10, 8, 0.75, 1.5, 7)
Z3 = func(18, 18, 1, 1.5, 10)
return Z1 + Z2 + Z3# Create an empty observation matrix
grid_size = 25
Z_sparse = np.ones((grid_size, grid_size)) * np.nan
# Seed it with several random observations
idx = np.random.randint(0, grid_size, size=(4, 2))
for i in idx:
Z_sparse[tuple(i)] = trial_func(i)# Get full and sparse grid indices for GP
X_full = gpim.utils.get_full_grid(Z_sparse)
X_sparse= gpim.utils.get_sparse_grid(Z_sparse)
# Initialize Bayesian optimizer with an 'expected improvement' acquisition function
boptim = gpim.boptimizer(
X_sparse, Z_sparse, X_full,
trial_func, acquisition_function='ei',
exploration_steps=30,
use_gpu=False, verbose=1)
# Run Bayesian optimization
boptim.run()# Plot exploration history
gpim.utils.plot_query_points(boptim.indices_all, plot_lines=True)
```## Running GPim notebooks in the cloud
1) Executable Google Colab [notebook](https://colab.research.google.com/github/ziatdinovmax/GPim/blob/master/examples/notebooks/GP_2D3D_images.ipynb) with the examples of applying GP to sparse spiral 2D scans in piezoresponse force microscopy (PFM), simulated 2D atomic image in electron microscopy, and hyperspectral 3D data in Band Excitation PFM.
2) Executable Google Colab [notebook](https://colab.research.google.com/github/ziatdinovmax/GPim/blob/master/examples/notebooks/GP_EELS.ipynb) with the example of applying "parallel" GP method to analysis of EELS data.
3) Executable Google Colab [notebook](https://colab.research.google.com/github/ziatdinovmax/GPim/blob/master/examples/notebooks/GP_TD_cKPFM.ipynb) with the example of applying GP to 4D spectroscopic dataset for smoothing and resolution enhancement in contact Kelvin Probe Force Microscopy (cKPFM).
4) Executable Google Colab [notebook](https://colab.research.google.com/github/ziatdinovmax/GPim/blob/master/examples/notebooks/GP_based_exploration_exploitation.ipynb) with a simple example of performing GP-based exploration-exploitation on a toy dataset.## Requirements
It is strongly recommended to run the codes with a GPU hardware accelerator (such as NVIDIA's P100 or V100 GPU). If you don't have a GPU on your local machine, you may rent a cloud GPU from [Google Cloud AI Platform](https://cloud.google.com/deep-learning-vm/). Running the [example notebooks](https://colab.research.google.com/github/ziatdinovmax/GPim/blob/master/examples/notebooks/Quickstart_GPim.ipynb) one time from top to bottom will cost about 1 USD with a standard deep learning VM instance (one P100 GPU and 15 GB of RAM).