Update the RPI model and clean up the examples section

This commit is contained in:
Abe Levitan committed 2021-07-06 13:58:00 -04:00
1 parent 8ddcc7683f
commit 53cd966c33
25 files changed
+121 -165

No files matched your search

+7 -9
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
import CDTools
from matplotlib import pyplot as plt
@@ -23,7 +21,7 @@ dataset.mask = dataset.mask[70:-70,70:-70]
model = CDTools.models.FancyPtycho.from_dataset(dataset,
translation_scale = 4,
n_modes=2,
propagation_distance=-73e-6)
propagation_distance=73e-6)
# Move to the GPU
model.to(device='cuda')
@@ -32,20 +30,20 @@ dataset.get_as(device='cuda')
# We turn off position reconstruction for the first phase
model.translation_offsets.requires_grad = False
for i, loss in enumerate(model.Adam_optimize(10, dataset, batch_size=15)):
for loss in model.Adam_optimize(10, dataset, batch_size=15):
model.inspect(dataset)
print(i,loss)
print(model.report())
# And we turn it on for the second phase
model.translation_offsets.requires_grad = True
for i, loss in enumerate(model.Adam_optimize(20, dataset, batch_size=15)):
for loss in model.Adam_optimize(20, dataset, batch_size=15):
model.inspect(dataset)
print(i,loss)
print(model.report())
# The third phase lowers the rate further
for i, loss in enumerate(model.Adam_optimize(10, dataset, batch_size=15, lr=0.0005)):
for loss in model.Adam_optimize(10, dataset, batch_size=15, lr=0.0005):
model.inspect(dataset)
print(i,loss)
print(model.report())
model.inspect(dataset)
-17
View File
@@ -1,17 +0,0 @@
from __future__ import division, print_function, absolute_import
import CDTools
from matplotlib import pyplot as plt
# First, we load an example dataset from a .cxi file
filename = '/media/Data Bank/APS_HXN_07_19/CXIs/scan_94513_cxi.h5'
#filename = 'data/scan_94361_cxi.h5'
dataset = CDTools.datasets.Ptycho2DDataset.from_cxi(filename)
# Next, we create a ptychography model from the dataset
model = CDTools.models.Simple3DBPP.from_dataset(dataset)
#import pdb; pdb.set_trace()
model.inspect(dataset)
plt.show()
# # Now, we run a short reconstruction from the dataset!
# for i, loss in enumerate(model.Adam_optimize(1, dataset)):
# print(i, loss)
+2 -12
View File
@@ -1,21 +1,12 @@
from __future__ import division, print_function, absolute_import
from matplotlib import pyplot as plt
import pickle
from scipy import io
from CDTools.tools import plotting
from CDTools.tools import analysis
with open('example_reconstructions/gold_balls_ensemble.pickle', 'rb') as f:
dataset = pickle.load(f)
# This converts from a list of dictionaries to a dictionary of lists
# It's safe to assume that all elements have the same set of keys
if type(dataset) == type([]):
dataset = {key: [element[key] for element in dataset]
for key in dataset[0]}
dataset = io.loadmat('example_reconstructions/gold_balls_ensemble.mat')
# Now we synthesize an average reconstruction
@@ -25,7 +16,6 @@ synth_probe, synth_obj, aligned_objs = analysis.synthesize_reconstructions(
# And then we calculate the consistency PRTF from this
freqs, prtf = analysis.calc_consistency_prtf(synth_obj, aligned_objs, dataset['basis'][0])
# Plot the first mode in detail
plotting.plot_phase(synth_probe[0],basis=dataset['basis'][0])
plotting.plot_amplitude(synth_probe[0],basis=dataset['basis'][0])
+19 -12
View File
@@ -1,16 +1,16 @@
from __future__ import division, print_function, absolute_import
import CDTools
import pickle
import numpy as np
from scipy import io
# Load the data
filename = 'example_data/AuBalls_700ms_30nmStep_3_6SS_filter.cxi'
dataset = CDTools.datasets.Ptycho2DDataset.from_cxi(filename)
results = []
n = 25
for idx in range(25):
print('Starting Reconstruction', idx)
for idx in range(n):
print('Starting Reconstruction', idx+1, 'of',n)
# Create a new model each time
model = CDTools.models.FancyPtycho.from_dataset(dataset,n_modes=3,
@@ -21,14 +21,21 @@ for idx in range(25):
dataset.get_as(device='cuda')
# Run the reconstruction
for i, loss in enumerate(model.Adam_optimize(30, dataset, batch_size=100)):
print(i,loss)
for loss in model.Adam_optimize(30, dataset, batch_size=100):
print(model.report(), end='\r')
# And add the results to the ensemble
# Print a summary that won't be overwritten by the next line
print('Finished:',model.report())
# And add the results to the ensemble dictionary
results.append(model.save_results(dataset))
# This converts from a list of dictionaries to a dictionary of lists
# It's safe to assume that all elements have the same set of keys
# After this, e.g. dataset['probe'] will return a list of all probes.
results = {key: np.array([element[key] for element in results])
for key in results[0].keys()}
print(results['probe'].shape)
# Save out the ensemble
with open('example_reconstructions/gold_balls_ensemble.pickle', 'wb') as f:
pickle.dump(results,f)
io.savemat('example_reconstructions/gold_balls_ensemble.mat', results)
Binary file not shown.
Binary file not shown.
Binary file not shown.
-2
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
import CDTools
from matplotlib import pyplot as plt
+13 -17
View File
@@ -1,36 +1,32 @@
from __future__ import division, print_function, absolute_import
import CDTools
from matplotlib import pyplot as plt
import pickle
from scipy import io
#
# This is the testing file for the Bragg ptychography code that includes
# corrections for probe propagation and for off-axis far field diffraction
# I'm starting to build it up using the lab ptychography forward data so I
# can initially test that it works on "softball" data before moving to the
# actual Bragg geometry data
# This is the testing dataset for the Bragg ptychography code, that was
# collected with our optical setup using a low NA zone plate illuminating
# a reflective diffraction grating with writing on it.
#
#filename = 'example_data/lab_ptycho_data.cxi'
# This file is too large to be distributed via Github.
# Please contact Abe Levitan (alevitan@mit) if you would like access
filename = '/media/Data Bank/Lab Ptycho/Zone Plate Bragg 633.cxi'
dataset = CDTools.datasets.Ptycho2DDataset.from_cxi(filename)
#dataset.inspect()
model = CDTools.models.Bragg2DPtycho.from_dataset(dataset,probe_support_radius=60,correct_tilt=False)#propagate_probe=False)#, n_modes=8)
model = CDTools.models.Bragg2DPtycho.from_dataset(dataset,probe_support_radius=60,correct_tilt=False)
model.to(device='cuda')
dataset.get_as(device='cuda')
model.translation_offsets.requires_grad = False
for i, loss in enumerate(model.Adam_optimize(100, dataset)):
for loss in model.Adam_optimize(100, dataset):
model.inspect(dataset)
print(i,loss)
#with open('example_reconstructions/lab_bragg_2d_ptycho.pickle', 'wb') as f:
# pickle.dump(model.save_results(dataset),f)
print(model.report())
#model.compare(dataset)
io.savemat('example_reconstructions/lab_bragg_2d_ptycho.mat',
model.save_results(dataset))
model.compare(dataset)
plt.show()
+8 -8
View File
@@ -1,9 +1,9 @@
from __future__ import division, print_function, absolute_import
import CDTools
from matplotlib import pyplot as plt
import pickle
from scipy import io
# A simple dataset collected from our optical setup
filename = 'example_data/lab_ptycho_data.cxi'
dataset = CDTools.datasets.Ptycho2DDataset.from_cxi(filename)
@@ -19,14 +19,14 @@ dataset.get_as(device='cuda')
model.translation_offsets.requires_grad = False
for i, loss in enumerate(model.Adam_optimize(50, dataset)):
for loss in model.Adam_optimize(50, dataset):
model.inspect(dataset)
print(i,loss)
print(model.report())
model.tidy_probes()
exit()
with open('example_reconstructions/lab_ptycho.pickle', 'wb') as f:
pickle.dump(model.save_results(dataset),f)
io.savemat('example_reconstructions/lab_ptycho.mat',
model.save_results(dataset))
model.compare(dataset)
plt.show()
-1
View File
@@ -1,4 +1,3 @@
from __future__ import division, print_function, absolute_import
import numpy as np
import torch as t
from CDTools.models import CDIModel
-2
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
import CDTools
from matplotlib import pyplot as plt
import numpy as np
+9 -15
View File
@@ -1,15 +1,10 @@
from __future__ import division, print_function, absolute_import
import CDTools
from matplotlib import pyplot as plt
import pickle
from torch.utils.data import Subset
from matplotlib import pyplot as plt
# First, we load an example dataset from a .cxi file
ss_filename = 'example_data/Optical_Data_ss.cxi'
#with open('example_data/Optical_ptycho.pickle', 'rb') as f:
with open('example_data/Optical_ptycho_incoherent.pickle', 'rb') as f:
ptycho_results = pickle.load(f)
@@ -19,9 +14,9 @@ background = ptycho_results['background']
dataset = CDTools.datasets.Ptycho2DDataset.from_cxi(ss_filename)
# Next, we create a ptychography model from the dataset
# Next, we create an RPI model from the dataset
# Note that we explicitly as for two incoherent probe modes
model = CDTools.models.RPI.from_dataset(dataset, probe, [900,900],
model = CDTools.models.RPI.from_dataset(dataset, probe, [500,500],
background=background, n_modes=2,
initialization='random')
@@ -33,20 +28,19 @@ dataset.get_as(device='cuda')
# Note that the inspect step takes the vast majority of the time
# The regularization is an L2 regularizer that empirically helps accelerate
# convergence
for i, loss in enumerate(model.LBFGS_optimize(30, dataset, lr=0.4, regularization_factor=[0.05,0.05])):#0.1)):
for loss in model.LBFGS_optimize(30, dataset, lr=0.4, regularization_factor=[0.05,0.05]):
model.inspect(dataset)
print(i,loss)
#model.inspect(dataset)
print(model.report())
# Now we use the regularizer to damp all but the top modes
for i, loss in enumerate(model.LBFGS_optimize(50, dataset, lr=0.4, regularization_factor=[0.001,0.1])):
for loss in model.LBFGS_optimize(50, dataset, lr=0.4, regularization_factor=[0.001,0.1]):
#model.inspect(dataset)
print(i,loss)
print(model.report())
# Save results to a python dictionary
results = model.save_results()
# Finally, we plot the results
model.inspect(dataset)
model.compare(dataset)
+8 -12
View File
@@ -1,11 +1,7 @@
from __future__ import division, print_function, absolute_import
import CDTools
from CDTools.tools import plotting as p
from matplotlib import pyplot as plt
import pickle
import torch as t
from CDTools.tools import cmath
from scipy import io
filename = 'example_data/lab_ptycho_data.cxi'
dataset = CDTools.datasets.Ptycho2DDataset.from_cxi(filename)
@@ -21,21 +17,21 @@ model.to(device='cuda')
dataset.get_as(device='cuda')
model.translation_offsets.requires_grad = False
for i, loss in enumerate(model.Adam_optimize(200, dataset)):
for loss in model.Adam_optimize(200, dataset):
model.inspect(dataset)
print(i,loss)
print(model.report())
model.tidy_probes()
for i, loss in enumerate(model.Adam_optimize(200, dataset, lr=0.0001)):
for loss in model.Adam_optimize(200, dataset, lr=0.0001):
model.inspect(dataset)
print(i,loss)
print(model.report())
model.tidy_probes(normalize=True)
model.inspect(dataset)
with open('example_reconstructions/unified_modes.pickle', 'wb') as f:
pickle.dump(model.save_results(dataset),f)
io.savemat('example_reconstructions/unified_modes.mat',
model.save_results(dataset))
model.compare(dataset)
plt.show()