原子解析数据的深度和机器学习
atomai的Python项目详细描述
阿托迈
什么是AtomAI
AtomAI是一个基于Pytorch的软件包,用于显微镜数据的深度/机器学习分析,不需要任何Python(或机器学习)的高级知识。它是AICrystallographer project的下一个迭代。目标读者是具有如何使用NumPy和Matplotlib基本知识的领域科学家。在
如何使用它
快速启动:云中的AtomAI
开始使用AtomAI最简单的方法是通过Google Colab
- 在
Train a deep fully convolutional neural network for atom finding
在 - 在
Multivariate statistical analysis of distortion domains in a single atomic image
在 - 在
Variational autoencoders for analysis of structural transformations
在 - 在
Prepare training data from experimental image with atomic coordinates
在
语义分割
如果您的目标是训练和/或应用深度学习模型对实验图像进行语义分割,建议从atomai.models.Segmentor
开始,这提供了一种简单的方法来训练神经网络(只需两行代码)并使用经过训练的模型(仅使用一行代码)进行预测。下面是一个例子,说明了如何用两行代码训练神经网络来发现原子/粒子/缺陷:
importatomaiasaoi# Initialize modelmodel=aoi.models.Segmentor(nb_classes=3)# uses UNet by default# Trainmodel.fit(images,labels,images_test,labels_test,# training data (numpy arrays)training_cycles=300,compute_accuracy=True,swa=True# training parameters)
这里swa
代表stochastic weight averaging,它通常可以提高模型的精度,并导致更好的泛化。经过训练的模型可用于在新的、以前未被模型看到的数据中发现原子/粒子/缺陷:
ImSpec型号
AtomAI还提供了可用于将图像数据转换为光谱的模型,反之亦然。这些模型可用于预测结构物性。例如,可以从局部样品区域的结构图像预测近似扫描tulleling光谱或电子能量损失光谱(当然,假设每个(子)图像中的光谱行为只有很小的可变性)。训练/预测例程与语义分割的相同:
in_dim=(16,16)# Input dimensions (image height and width)out_dim=(64,)# Output dimensions (spectra length)# Initialize and train modelmodel=aoi.models.ImSpec(in_dim,out_dim,latent_dim=10)model.fit(imgs_train,spectra_train,imgs_test,spectra_test,# trainig data (numpy arrays)full_epoch=True,training_cycles=120,swa=True# training parameters)
利用训练好的ImSpec模型通过运行
prediction=model.predict(imgs_val,norm=False)
深度合成
我们也可以使用AtomAI来训练一个模型的集合,而不仅仅是一个模型。平均集合预报通常比单一模式预报更准确可靠。此外,我们还获得每个像素/点的uncertainty in our prediction的信息。在
# Ititialize and compile ensemble traineretrainer=aoi.trainers.EnsembleTrainer("Unet",batch_norm=True,nb_classes=3,with_dilation=False)etrainer.compile_ensemble_trainer(training_cycles=500,compute_accuracy=True,swa=True)# Train ensemble of models starting every time with new randomly initialized weightssmodel,ensemble=etrainer.train_ensemble_from_scratch(images,labels,images_test,labels_test,n_models=10)
然后,可以使用模型集合对每个点(例如图像中的每个像素)进行不确定性估计的预测:
predictor=aoi.predictors.EnsemblePredictor(smodel,ensemble,nb_classes=3)nn_out_mean,nn_out_var=predictor.predict(expdata)
变分自动编码器(VAE)
AtomAI还内置了variational autoencoders (VAEs),用于在无监督的方式下找到系统局部描述符的最有效简化表示。可用的VAE有正则VAE、旋转不变和/或平移不变VAE(rVAE)和类条件VAE/rVAE。VAE可以应用于原始数据和NN输出,但通常后者的工作效果更好。下面是一个简单的例子:
# Get a stack of subimages from experimental data (e.g. a semantically segmented atomic movie)imstack,com,frames=utils.extract_subimages(nn_output,coords,window_size=32)# Intitialize rVAE modelinput_dim=(32,32)rvae=aoi.models.rVAE(input_dim)# Trainrvae.fit(imstack_train,latent_dim=2,rotation_prior=np.pi/3,training_cycles=100,batch_size=100)# Visualize the learned manifoldrvae.manifold2d()
还可以利用训练好的VAE来观察潜在空间中的数据分布。在本例中,前3个潜在变量与旋转和xy平移相关(无论指定了多少个潜在维度,它们都会自动添加到rVAE中),而最后2个潜在变量与图像内容关联。在
encoded_mean,encoded_sd=rvae.encode(imstack)z1,z2,z3=encoded_mean[:,0],encoded_mean[:,1:3],encoded_mean[:,3:]
定制型号
最后,可以使用AtomAI训练器和预测器来轻松处理定制Pythorch模型。假设我们定义一个定制的Pythorch神经网络为
# Here ConvBlock and UpsampleBlock are from atomai.nets moduletorch_encoder=torch.nn.Sequential(ConvBlock(ndim=2,nb_layers=1,input_channels=1,output_channels=8,batch_norm=True),torch.nn.MaxPool2d(2,2),ConvBlock(2,2,8,16,batch_norm=False),torch.nn.MaxPool2d(2,2),ConvBlock(2,2,16,32,batch_norm=False),torch.nn.MaxPool2d(2,2),ConvBlock(2,2,32,64,batch_norm=False))torch_decoder=torch.nn.Sequential(UpsampleBlock(ndim=2,input_channels=64,output_channels=64,mode="nearest"),ConvBlock(2,2,64,32,batch_norm=False),UpsampleBlock(2,32,32,mode="nearest"),ConvBlock(2,2,32,16,batch_norm=False),UpsampleBlock(2,16,16,mode="nearest"),ConvBlock(2,1,16,8,batch_norm=False),torch.nn.Conv2d(8,1,1))torch_DAE=torch.nn.Sequential(torch_encoder,torch_decoder)
我们可以使用AtomAI的培训师轻松培训该车型:
# Initialize trainer and pass our model to ittrainer=aoi.trainers.BaseTrainer()trainer.set_model(torch_DAE)# Fix the initialization parameters (for reproducibility)set_train_rng(1)trainer._reset_weights()# start each time with the same initializationtrainer._reset_training_history()# Compile trainertrainer.compile_trainer((imgdata_noisy,imgdata,imgdata_noisy_test,imgdata_test),# training dataloss="mse",training_cycles=500,swa=True# training parameters)# Traintrained_model=trainer.run()
经过训练的模型可用于使用AtomAI的预测因子对新数据进行预测:
p=aoi.predictors.BasePredictor(trained_model,use_gpu=True)prediction=p.predict(imgdata_noisy_test)
不仅仅是深度学习
深度神经网络提取的信息可以进一步用于原始数据和“解码”数据的统计分析。例如,对于铁电材料的单原子分辨图像,可以识别不同铁电体的畴扭曲:
# Get local descriptorsimstack=aoi.stat.imlocal(nn_output,coords,window_size=32,coord_class=1)# Compute distortion "eigenvectors" with associated loading maps and plot results:pca_results=imstack.imblock_pca(n_components=4,plot_results=True)
对于电影,可以提取单个缺陷的轨迹并计算不同类之间的转移概率:
# Get local descriptors (such as subimages centered around impurities)imstack=aoi.stat.imlocal(nn_output,coordinates,window_size=32,coord_class=1)# Calculate Gaussian mixture model (GMM) componentscomponents,imgs,coords=imstack.gmm(n_components=10,plot_results=True)# Calculate GMM components and transition probabilities for different trajectoriestransitions_dict=imstack.transition_matrix(n_components=10,rmax=10)# and more
安装
首先,安装PyTorch。然后,通过
pip install atomai
- 项目
标签: