https://github.com/pycroscopy/atomai

Deep and Machine Learning for Microscopy

https://github.com/pycroscopy/atomai

Science Score: 49.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 3 DOI reference(s) in README
  • Academic publication links
    Links to: arxiv.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (17.0%) to scientific vocabulary

Keywords

deep-kernel-learning deep-learning electron-microscopy ensemble-learning fully-convolutional-networks google-colaboratory imaging jupyter-notebook machine-learning materials materials-science microscopy multivariate-analysis pytorch scanning-probe-microscopy semantic-segmentation variational-autoencoders
Last synced: 6 months ago · JSON representation

Repository

Deep and Machine Learning for Microscopy

Basic Info
Statistics
  • Stars: 213
  • Watchers: 8
  • Forks: 41
  • Open Issues: 11
  • Releases: 14
Topics
deep-kernel-learning deep-learning electron-microscopy ensemble-learning fully-convolutional-networks google-colaboratory imaging jupyter-notebook machine-learning materials materials-science microscopy multivariate-analysis pytorch scanning-probe-microscopy semantic-segmentation variational-autoencoders
Created over 5 years ago · Last pushed 8 months ago
Metadata Files
Readme License

README.md

PyPI version build codecov Documentation Status

Downloads Colab Gitpod ready-to-code

AtomAI

What is AtomAI

AtomAI is a Pytorch-based package for deep and machine learning analysis of microscopy data that doesn't require any advanced knowledge of Python or machine learning. The intended audience is domain scientists with a basic understanding of how to use NumPy and Matplotlib. It was developed by Maxim Ziatdinov at Oak Ridge National Lab.

Why AtomAI

The purpose of the AtomAI is to provide an environment that bridges the instrument specific libraries and general physical analysis by enabling the seamless deployment of machine learning algorithms including deep convolutional neural networks, invariant variational autoencoders, and decomposition/unmixing techniques for image and hyperspectral data analysis. Ultimately, it aims to combine the power and flexibility of the PyTorch deep learning framework and simplicity and intuitive nature of packages such as scikit-learn, with a focus on scientific data.

Scientific papers that used AtomAI:

  • Exploring causal physical mechanisms via non-Gaussian linear models and deep kernel learning: applications for ferroelectric domain structures. ACS Nano 16, 1250-1259 (2022). DOI: 10.1021/acsnano.1c09059
  • Exploring order parameters and dynamic processes in disordered systems via variational autoencoders. Science Advances 7, eabd5084 (2021). DOI: 10.1126/sciadv.abd5084
  • Tracking atomic structure evolution during directed electron beam induced Si-atom motion in graphene via deep machine learning. Nanotechnology 32, 035703 (2020). DOI: 10.1088/1361-6528/abb8a6

Show full list

Chat with us about DL/ML in Microscopy (beta)

Have some specific questions about applications of deep/machine learning (DL/ML) in microscopy? Feel free to ask them on Gitter! Gitter

How to use AtomAI

Quickstart: AtomAI in the Cloud

The easiest way to start using AtomAI is via Google Colab:

More examples ->

Semantic segmentation

If your goal is to train and/or apply deep learning models for semantic segmentation of your experimental images, it is recommended to start with atomai.models.Segmentor, which provides an easy way to train neural networks (with just two lines of code) and to make a prediction with trained models (with just one line of code). Here is an example of how one can train a neural network for atom/particle/defect finding with essentially two lines of code: ```python import atomai as aoi

Initialize model

model = aoi.models.Segmentor(nb_classes=3) # uses UNet by default

Train

model.fit(images, labels, imagestest, labelstest, # training data (numpy arrays) trainingcycles=300, computeaccuracy=True, swa=True # training parameters ) Hereswastands for [stochastic weight averaging](https://arxiv.org/abs/1803.05407), which usually allows improving the model's accuracy and leads to better generalization. The trained model can be used to find atoms/particles/defects in new, previously unseen (by a model) data: python nn_output, coordinates = model.predict(expdata) ```

ImSpec models

AtomAI also provides models that can be used for predicting spectra from image data and vice versa. These models can be used for predicting property (functionality) from structure. An example can be predicting approximate scanning tulleling spectroscopy or electron energy loss spectroscopy spectra from structural images of local sample regions (the assumption is of course that there is only a small variability of spectral behaviour within each (sub)-image). The training/prediction routines are very similar to those of the semantic segmentation model, with the main difference being that one has to specify the dimensions of input and output data: ```python indim = (16, 16) # Input dimensions (image height and width) outdim = (64,) # Output dimensions (spectra length)

Initialize and train model

model = aoi.models.ImSpec(indim, outdim, latentdim=10) model.fit(imgstrain, spectratrain, imgstest, spectratest, # training data (numpy arrays) fullepoch=True, trainingcycles=120, swa=True # training parameters ) Make a prediction with the trained ImSpec model by running python prediction = model.predict(imgsval, norm=False) ```

Deep ensembles

One can also use AtomAI to train an ensemble of models instead of just a single model. The average ensemble prediction is usually more accurate and reliable than that of the single model. In addition, we also get the information about the uncertainty in our prediction for each pixel/point.

```python

Ititialize and compile ensemble trainer

etrainer = aoi.trainers.EnsembleTrainer("Unet", nbclasses=3) etrainer.compileensembletrainer(trainingcycles=500, compute_accuracy=True, swa=True)

Train ensemble of 10 models starting every time with new randomly initialized weights

smodel, ensemble = etrainer.trainensemblefromscratch( images, labels, imagestest, labelstest, nmodels=10) ``` The ensemble of models can be then used to make a prediction with uncertainty estimates for each point (e.g. each pixel in the image):

python predictor = aoi.predictors.EnsemblePredictor(smodel, ensemble, nb_classes=3) nn_out_mean, nn_out_var = predictor.predict(expdata)

Variational autoencoders (VAE)

AtomAI has built-in variational autoencoders (VAEs) for finding in the unsupervised fashion the most effective reduced representation of system's local descriptors. The available VAEs are regular VAE, rotationally and/or translationally invariant VAE (rVAE), class-conditined VAE/rVAE, and joint VAE/rVAE. The VAEs can be applied to both raw data and NN output, but typically work better with the latter. Here's a simple example: ```python

Get a stack of subimages from experimental data (e.g. a semantically segmented atomic movie)

imstack, com, frames = aoi.utils.extractsubimages(nnoutput, coords, window_size=32)

Intitialize rVAE model

inputdim = (32, 32) rvae = aoi.models.rVAE(inputdim, latent_dim=2)

Train

rvae.fit( imstacktrain, rotationprior=np.pi/3, trainingcycles=100, batchsize=100)

Visualize the learned manifold

rvae.manifold2d(); One can also use the trained VAE to view the data distribution in the latent space. In this example the first 3 latent variables are associated with rotations and xy-translations (they are automatically added in rVAE to whatever number of latent dimensions is specified), whereas the last 2 latent variables are associated with images content. python encodedmean, encodedsd = rvae.encode(imstack) z1, z2, z3 = encodedmean[:,0], encodedmean[:, 1:3], encoded_mean[:, 3:] ```

Deep kernel learning

AtomAI has an easy-to-use deep kernel learning module for performing automated experiments. The DKL, originally introduced by Andrew Gordon Wilson, can be understood as a hybrid of classical deep neural network (DNN) and Gaussian process (GP). The DNN serves as a feature extractor that allows reducing the complex high-dimensional features to low-dimensional descriptors on which a standard GP kernel operates. The parameters of DNN and of GP kernel are optimized jointly by performing a gradient ascent on marginal log-likelihood. Practically, the DKL training inputs are a small number of patches from an easy-to-acquire structural image (e.g., topography in STM), and training targets are usually a physical property (e.g. size of superconducting gap) derived from the spectra measured in those patches. The DKL output on the new inputs (image patches for which there are no measured spectra) is the expected property value and associated uncertainty, which are used to derive the next measurement point. python exploration_steps = 50 data_dim = X_measured.shape[-1] # here the image height and width are 'flattened' for e in range(exploration_steps): # obtain/update DKL posterior dklgp = aoi.models.dklGPR(data_dim, embedim=2, precision="single") dklgp.fit(X_measured, y_measured, training_cycles=200) # Thompson sampling for getting the next measurement point obj, next_point_idx = dklgp.thompson(X_unmeasured) # Select the next point to measure (assumes a discrete grid of points) next_point = indices_unmeasured[next_point_idx] # Do measurement and update sets measured/unmeasured points ...

Custom models

Finally, it is possible to use AtomAI trainers and predictors for easy work with custom PyTorch models. Suppose we define a custom denoising autoencoder in Pytorch as ```python import torch from atomai.nets import ConvBlock, UpsampleBlock

torchencoder = torch.nn.Sequential( ConvBlock(ndim=2, nblayers=1, inputchannels=1, outputchannels=8, batchnorm=False), torch.nn.MaxPool2d(2, 2), ConvBlock(2, 2, 8, 16, batchnorm=False), torch.nn.MaxPool2d(2, 2), ConvBlock(2, 2, 16, 32, batchnorm=False), torch.nn.MaxPool2d(2, 2), ConvBlock(2, 2, 32, 64, batchnorm=False) ) torchdecoder = torch.nn.Sequential( UpsampleBlock(ndim=2, inputchannels=64, outputchannels=64, mode="nearest"), ConvBlock(2, 2, 64, 32, batchnorm=False), UpsampleBlock(2, 32, 32, mode="nearest"), ConvBlock(2, 2, 32, 16, batchnorm=False), UpsampleBlock(2, 16, 16, mode="nearest"), ConvBlock(2, 1, 16, 8, batchnorm=False), torch.nn.Conv2d(8, 1, 1) ) torchDAE = torch.nn.Sequential(torchencoder, torch_decoder) We can easily train this model using AtomAI's trainers: python

Initialize trainer and pass our model to it

trainer = aoi.trainers.BaseTrainer() trainer.setmodel(torchDAE)

Fix the initialization parameters (for reproducibility)

aoi.utils.settrainrng(1) trainer.resetweights() trainer.resettraining_history()

Compile trainer

trainer.compiletrainer( (imgdatanoisy, imgdata, imgdatanoisytest, imgdatatest), # training data loss="mse", trainingcycles=500, swa=True # training parameters )

Train

trainedmodel = trainer.run() The trained model can be used to make predictions on new data using AtomAI's predictors: python p = aoi.predictors.BasePredictor(trainedmodel, usegpu=True) prediction = p.predict(imgdatanoisy_test) ```

Not just deep learning

The information extracted by deep neural networks can be further used for statistical analysis of raw and "decoded" data. For example, for a single atom-resolved image of ferroelectric material, one can identify domains with different ferroic distortions:

```python

Get local descriptors

imstack = aoi.stat.imlocal(nnoutput, coords, windowsize=32, coord_class=1)

Compute distortion "eigenvectors" with associated loading maps and plot results:

pcaresults = imstack.imblockpca(ncomponents=4, plotresults=True) ```

For movies, one can extract trajectories of individual defects and calculate the transition probabilities between different classes:

```python

Get local descriptors (such as subimages centered around impurities)

imstack = aoi.stat.imlocal(nnoutput, coordinates, windowsize=32, coord_class=1)

Calculate Gaussian mixture model (GMM) components

components, imgs, coords = imstack.gmm(ncomponents=10, plotresults=True)

Calculate GMM components and transition probabilities for different trajectories

transitionsdict = imstack.transitionmatrix(n_components=10, rmax=10)

and more

```

Installation

First, install PyTorch. Then, install AtomAI via

bash pip install atomai

Cite us

If you used AtomAI for your research, consider citing our paper

@article{ziatdinov2021atomai, title={AtomAI: A Deep Learning Framework for Analysis of Image and Spectroscopy Data in (Scanning) Transmission Electron Microscopy and Beyond}, author={Ziatdinov, Maxim and Ghosh, Ayana and Wong, Tommy and Kalinin, Sergei V.}, journal={arXiv preprint arXiv:2105.07485}, year={2021} }

Owner

  • Name: pycroscopy
  • Login: pycroscopy
  • Kind: organization
  • Email: pycroscopy@gmail.com

GitHub Events

Total
  • Release event: 1
  • Watch event: 18
  • Issue comment event: 2
  • Push event: 7
  • Pull request event: 10
  • Fork event: 3
  • Create event: 3
Last Year
  • Release event: 1
  • Watch event: 18
  • Issue comment event: 2
  • Push event: 7
  • Pull request event: 10
  • Fork event: 3
  • Create event: 3

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 1,262
  • Total Committers: 6
  • Avg Commits per committer: 210.333
  • Development Distribution Score (DDS): 0.019
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
ziatdinovmax z****x@g****m 1,238
Ayana Ghosh r****h@g****m 14
ahoust17 8****7 6
Arpan Biswas a****2@g****m 2
The Codacy Badger b****r@c****m 1
Miguel Fuentes-Cabrera m****a@g****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 9 months ago

All Time
  • Total issues: 17
  • Total pull requests: 73
  • Average time to close issues: 10 days
  • Average time to close pull requests: about 11 hours
  • Total issue authors: 10
  • Total pull request authors: 8
  • Average comments per issue: 1.59
  • Average comments per pull request: 0.44
  • Merged pull requests: 68
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 1
  • Pull requests: 3
  • Average time to close issues: N/A
  • Average time to close pull requests: 4 days
  • Issue authors: 1
  • Pull request authors: 2
  • Average comments per issue: 5.0
  • Average comments per pull request: 0.67
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • ziatdinovmax (5)
  • darianSmalley (2)
  • ramav87 (2)
  • utkarshp1161 (2)
  • kevinroccapriore (1)
  • miaoleixin1994 (1)
  • ercius (1)
  • markcoletti (1)
  • tommycwong (1)
  • jamesafranke (1)
Pull Request Authors
  • ziatdinovmax (63)
  • aghosh92 (5)
  • utkarshp1161 (4)
  • ahoust17 (2)
  • gitter-badger (1)
  • arpanbiswas52 (1)
  • tommycwong (1)
  • saimani5 (1)
Top Labels
Issue Labels
help wanted (4) enhancement (4) good first issue (2) bug (2)
Pull Request Labels
enhancement (1)

Packages

  • Total packages: 2
  • Total downloads:
    • pypi 526 last-month
  • Total dependent packages: 0
    (may contain duplicates)
  • Total dependent repositories: 1
    (may contain duplicates)
  • Total versions: 61
  • Total maintainers: 1
proxy.golang.org: github.com/pycroscopy/atomai
  • Versions: 30
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 9.0%
Average: 9.6%
Dependent repos count: 10.2%
Last synced: 6 months ago
pypi.org: atomai

Deep and machine learning for atom-resolved data

  • Versions: 31
  • Dependent Packages: 0
  • Dependent Repositories: 1
  • Downloads: 526 Last month
Rankings
Stargazers count: 5.5%
Forks count: 6.8%
Dependent packages count: 10.1%
Average: 11.4%
Downloads: 12.9%
Dependent repos count: 21.6%
Maintainers (1)
Last synced: 6 months ago

Dependencies

.github/workflows/actions.yml actions
  • actions/checkout v2 composite
  • actions/setup-python v2 composite
  • codecov/codecov-action v1 composite