def rec_error(beta): """ cross-validation """ res, energies = fista_tv(y1, beta, 400, H) yres = H * res[-1].ravel()[:, np.newaxis] return (((yres - y2)**2).mean()), res[-1], energies
from reconstruction.util import generate_synthetic_data from time import time import matplotlib.pyplot as plt # Synthetic data l = 256 x = generate_synthetic_data(l) # Projection operator and projections data, with noise H = build_projection_operator(l, l / 3) y = H * x.ravel()[:, np.newaxis] y += 2 * np.random.randn(*y.shape) # Reconstruction t1 = time() res, energies = fista_tv(y, 50, 100, H) t2 = time() print "reconstruction done in %f s" % (t2 - t1) # Fraction of errors of segmented image wrt ground truth err = [np.abs(x - (resi > 0.5)).mean() for resi in res] # Display results plt.figure() plt.subplot(221) plt.imshow(x, cmap='gray', interpolation='nearest', vmin=0, vmax=1) plt.title('original data (256x256)') plt.axis('off') plt.subplot(222) plt.imshow(res[-1], cmap='gray', interpolation='nearest', vmin=0, vmax=1) plt.title('reconstruction after 100 iterations')
from time import time import matplotlib.pyplot as plt # Synthetic data l = 256 x = generate_synthetic_data(l) # Projection operator and projections data, with noise H = build_projection_operator(l, l / 3) y = H * x.ravel()[:, np.newaxis] y += 2 * np.random.randn(*y.shape) # Reconstruction t1 = time() res, energies = fista_tv(y, 50, 100, H) t2 = time() print "reconstruction done in %f s" % (t2 - t1) # Fraction of errors of segmented image wrt ground truth err = [np.abs(x - (resi > 0.5)).mean() for resi in res] # Display results plt.figure() plt.subplot(221) plt.imshow(x, cmap="gray", interpolation="nearest", vmin=0, vmax=1) plt.title("original data (256x256)") plt.axis("off") plt.subplot(222) plt.imshow(res[-1], cmap="gray", interpolation="nearest", vmin=0, vmax=1) plt.title("reconstruction after 100 iterations")
plt.subplot(2, 3, 1) plt.imshow(x, cmap=plt.cm.gnuplot2, interpolation='nearest', vmin=-.1, vmax=1.2) plt.title('original data (256x256)') plt.axis('off') for idx, (val_min, val_max, name) in enumerate([ (None, None, 'TV'), (0, 1, 'TV + interval'), ]): # Reconstruction t1 = time() res, energies = fista_tv(y, 50, 100, H, val_min=val_min, val_max=val_max) t2 = time() # Fraction of errors of segmented image wrt ground truth err = np.abs(x - (res[-1] > 0.5)).mean() print "%s: reconstruction done in %f s, %.3f%% segmentation error" % ( name, t2 - t1, 100 * err) plt.subplot(2, 3, 2 + idx) plt.imshow(res[-1], cmap=plt.cm.gnuplot2, interpolation='nearest', vmin=-.1, vmax=1.2) plt.title('reconstruction with %s' % name) plt.axis('off')
# Projection operator and projections data, with noise H = build_projection_operator(l, l / 32) y = H * x.ravel()[:, np.newaxis] y += 5 * np.random.randn(*y.shape) # Display original data plt.figure(figsize=(12, 5)) plt.subplot(2, 3, 1) plt.imshow(x, cmap=plt.cm.gnuplot2, interpolation="nearest", vmin=-0.1, vmax=1.2) plt.title("original data (256x256)") plt.axis("off") for idx, (val_min, val_max, name) in enumerate([(None, None, "TV"), (0, 1, "TV + interval")]): # Reconstruction t1 = time() res, energies = fista_tv(y, 50, 100, H, val_min=val_min, val_max=val_max) t2 = time() # Fraction of errors of segmented image wrt ground truth err = np.abs(x - (res[-1] > 0.5)).mean() print "%s: reconstruction done in %f s, %.3f%% segmentation error" % (name, t2 - t1, 100 * err) plt.subplot(2, 3, 2 + idx) plt.imshow(res[-1], cmap=plt.cm.gnuplot2, interpolation="nearest", vmin=-0.1, vmax=1.2) plt.title("reconstruction with %s" % name) plt.axis("off") ax = plt.subplot(2, 3, 5 + idx) ax.yaxis.set_scale("log") plt.hist(res[-1].ravel(), bins=20, normed=True) plt.yticks(()) plt.title("Histogram of pixel intensity")