MrDivakaruni commited on
Commit
aeb90d9
·
1 Parent(s): 860bbb8

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +129 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from scipy import sparse
3
+ from scipy import ndimage
4
+ from sklearn.linear_model import Lasso
5
+ from sklearn.linear_model import Ridge
6
+ import matplotlib.pyplot as plt
7
+ import gradio as gr
8
+
9
+
10
+ def _weights(x, dx=1, orig=0):
11
+ x = np.ravel(x)
12
+ floor_x = np.floor((x - orig) / dx).astype(np.int64)
13
+ alpha = (x - orig - floor_x * dx) / dx
14
+ return np.hstack((floor_x, floor_x + 1)), np.hstack((1 - alpha, alpha))
15
+
16
+
17
+ def _generate_center_coordinates(l_x):
18
+ X, Y = np.mgrid[:l_x, :l_x].astype(np.float64)
19
+ center = l_x / 2.0
20
+ X += 0.5 - center
21
+ Y += 0.5 - center
22
+ return X, Y
23
+
24
+
25
+ def build_projection_operator(l_x, n_dir):
26
+ """Compute the tomography design matrix.
27
+
28
+ Parameters
29
+ ----------
30
+
31
+ l_x : int
32
+ linear size of image array
33
+
34
+ n_dir : int
35
+ number of angles at which projections are acquired.
36
+
37
+ Returns
38
+ -------
39
+ p : sparse matrix of shape (n_dir l_x, l_x**2)
40
+ """
41
+ X, Y = _generate_center_coordinates(l_x)
42
+ angles = np.linspace(0, np.pi, n_dir, endpoint=False)
43
+ data_inds, weights, camera_inds = [], [], []
44
+ data_unravel_indices = np.arange(l_x**2)
45
+ data_unravel_indices = np.hstack((data_unravel_indices, data_unravel_indices))
46
+ for i, angle in enumerate(angles):
47
+ Xrot = np.cos(angle) * X - np.sin(angle) * Y
48
+ inds, w = _weights(Xrot, dx=1, orig=X.min())
49
+ mask = np.logical_and(inds >= 0, inds < l_x)
50
+ weights += list(w[mask])
51
+ camera_inds += list(inds[mask] + i * l_x)
52
+ data_inds += list(data_unravel_indices[mask])
53
+ proj_operator = sparse.coo_matrix((weights, (camera_inds, data_inds)))
54
+ return proj_operator
55
+
56
+
57
+ def generate_synthetic_data(l):
58
+ """Synthetic binary data"""
59
+ rs = np.random.RandomState(0)
60
+ n_pts = 36
61
+ x, y = np.ogrid[0:l, 0:l]
62
+ mask_outer = (x - l / 2.0) ** 2 + (y - l / 2.0) ** 2 < (l / 2.0) ** 2
63
+ mask = np.zeros((l, l))
64
+ points = l * rs.rand(2, n_pts)
65
+ mask[(points[0]).astype(int), (points[1]).astype(int)] = 1
66
+ mask = ndimage.gaussian_filter(mask, sigma=l / n_pts)
67
+ res = np.logical_and(mask > mask.mean(), mask_outer)
68
+ return np.logical_xor(res, ndimage.binary_erosion(res))
69
+
70
+ def Generate_synthetic_images_and_projections(l,alpha_l2,alpha_l1):
71
+
72
+ # Generate synthetic images, and projections
73
+ proj_operator = build_projection_operator(l, l // 7)
74
+ data = generate_synthetic_data(l)
75
+ proj = proj_operator @ data.ravel()[:, np.newaxis]
76
+ proj += 0.15 * np.random.randn(*proj.shape)
77
+
78
+ # Reconstruction with L2 (Ridge) penalization
79
+ rgr_ridge = Ridge(alpha=alpha_l2)
80
+ rgr_ridge.fit(proj_operator, proj.ravel())
81
+ rec_l2 = rgr_ridge.coef_.reshape(l, l)
82
+
83
+ # Reconstruction with L1 (Lasso) penalization
84
+ # the best value of alpha was determined using cross validation
85
+ # with LassoCV
86
+ rgr_lasso = Lasso(alpha=alpha_l1)
87
+ rgr_lasso.fit(proj_operator, proj.ravel())
88
+ rec_l1 = rgr_lasso.coef_.reshape(l, l)
89
+
90
+ fig = plt.figure(figsize=(8, 3.3))
91
+ plt.subplot(131)
92
+ plt.imshow(data, cmap=plt.cm.gray, interpolation="nearest")
93
+ plt.axis("off")
94
+ plt.title("original image")
95
+ plt.subplot(132)
96
+ plt.imshow(rec_l2, cmap=plt.cm.gray, interpolation="nearest")
97
+ plt.title("L2 penalization")
98
+ plt.axis("off")
99
+ plt.subplot(133)
100
+ plt.imshow(rec_l1, cmap=plt.cm.gray, interpolation="nearest")
101
+ plt.title("L1 penalization")
102
+ plt.axis("off")
103
+ plt.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, right=1)
104
+
105
+ fig.canvas.draw()
106
+ image = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
107
+ image = image.reshape(fig.canvas.get_width_height()[::-1] + (3,))
108
+ plt.close(fig)
109
+ return image
110
+
111
+ title = "Compressive sensing: Tomography reconstruction with L1 prior (Lasso)"
112
+
113
+ des="This example shows the reconstruction of an image from a set of parallel projections, acquired along different angles. Such a dataset is acquired in computed tomography (CT)."
114
+ with gr.Blocks(title=title) as demo:
115
+ gr.Markdown(f" {title}")
116
+ gr.Markdown(f"{des}")
117
+ gr.Markdown("[This demo is based on Scikit-Learn example](https://scikit-learn.org/stable/auto_examples/applications/plot_tomography_l1_reconstruction.html#sphx-glr-auto-examples-applications-plot-tomography-l1-reconstruction-py)")
118
+
119
+ with gr.Row():
120
+ l=gr.Slider(minimum=100, maximum=500, step=1, value = 128, label = "Linear size")
121
+ alpha_l2=gr.Slider(minimum=0, maximum=1, step=0.001, value = 0.2, label = "alpha l2")
122
+ alpha_l1=gr.Slider(minimum=0, maximum=1, step=0.001, value = 0.001, label = "alpha l1")
123
+
124
+
125
+ output = gr.Image()
126
+ btn = gr.Button(value="Submit")
127
+ btn.click(fn=Generate_synthetic_images_and_projections, inputs = [l,alpha_l2,alpha_l1], outputs=output)
128
+
129
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ numpy
2
+ scipy
3
+ matplotlib
4
+ scikit-learn