Skip to main content

Gaussian processes for image analysis

Project description

Downloads PyPI version Build Status Documentation Status Codacy Badge

Colab Gitpod ready-to-code

Under active development (expect some breaking changes)

What is GPim?

GPim is a python package that provides an easy way to apply Gaussian processes (GP) in Pyro and Gpytorch 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.

For the examples, see the papers where GPim was used:

GP for 3D hyperspectral microscopy data: https://arxiv.org/abs/1911.11348

GP for 4D hyperspectral microscopy data: https://arxiv.org/abs/2002.03591

GP and GP-based BO for Ising model: https://arxiv.org/abs/2004.04832

GP-based BO for hysteresis loop engineering in ferroelectrics: https://arxiv.org/abs/2004.12512

Installation

First install PyTorch. Then install GPim using

pip install gpim

How to use

GP reconstruction

Below 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. 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.

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.

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"""
    return np.exp(-4*np.log(2) * ((idx[0]-5)**2 + (idx[1]-10)**2) / 4.5**2) 

# 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 with the example of applying GP to sparse spiral 2D scans in piezoresponse force microscopy (PFM) and hyperspectral 3D data in Band Excitation PFM.
  2. Executable Google Colab notebook with the example of applying GP to 4D spectroscopic dataset for smoothing and resolution enhancement in contact Kelvin Probe Force Microscopy (cKPFM).
  3. Executable Google Colab notebook 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. Running the example notebooks 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).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gpim-0.3.2.tar.gz (28.8 kB view details)

Uploaded Source

Built Distribution

gpim-0.3.2-py3-none-any.whl (35.1 kB view details)

Uploaded Python 3

File details

Details for the file gpim-0.3.2.tar.gz.

File metadata

  • Download URL: gpim-0.3.2.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for gpim-0.3.2.tar.gz
Algorithm Hash digest
SHA256 8d01a34d2032908adb8885ac53214cf2c2ff31c34b2d0b778d4fa5c36b552f6f
MD5 024de7e1f388a79e1883cd5c8cd43ee1
BLAKE2b-256 7a60acabcbf05ef259a517098dd042f38bfd3261edcc229368769af0c46d4758

See more details on using hashes here.

File details

Details for the file gpim-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: gpim-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 35.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for gpim-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8afe3aa00b0db31b54d17f6352b4b288fb8130f992f90b932cf88593f425af5c
MD5 67cc359681ef5dcc6c9f4740b8ca8d72
BLAKE2b-256 31bf227566be547e7789f52b744f65c35dbc527cc8dd5dfde14c90d7850a8c02

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page