Spaces:
Sleeping
Sleeping
Commit
·
164569e
1
Parent(s):
2d1c539
Adding circular noise
Browse files- app.py +105 -73
- backup1.py +123 -0
- backup2.py +165 -0
- line_models_sampling.py +106 -76
app.py
CHANGED
|
@@ -1,18 +1,80 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
line_parameters = np.zeros((num_iterations, 2))
|
| 6 |
for i in range(num_iterations):
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# Randomly sample two points from the line
|
| 11 |
-
idx = np.random.choice(num_points, 2, replace=False)
|
| 12 |
-
x1, y1 = x_values[idx[0]], y_values_with_noise[idx[0]]
|
| 13 |
-
x2, y2 = x_values[idx[1]], y_values_with_noise[idx[1]]
|
| 14 |
|
| 15 |
-
# Compute slope and intercept of line passing through sampled points
|
| 16 |
line_slope = (y2 - y1) / (x2 - x1)
|
| 17 |
line_intercept = y1 - line_slope * x1
|
| 18 |
|
|
@@ -21,88 +83,59 @@ def compute_line_parameters(slope, intercept, mean, std, num_points, x_min, x_ma
|
|
| 21 |
|
| 22 |
return line_parameters
|
| 23 |
|
| 24 |
-
from sklearn.cluster import KMeans
|
| 25 |
-
# import matplotlib.pyplot as plt
|
| 26 |
|
| 27 |
-
import gradio as gr
|
| 28 |
-
import matplotlib.pyplot as plt
|
| 29 |
-
import numpy as np
|
| 30 |
-
from sklearn.cluster import KMeans
|
| 31 |
|
| 32 |
-
def cluster_line_parameters(line_parameters, num_clusters):
|
| 33 |
-
# Cluster line parameters using KMeans
|
| 34 |
kmeans = KMeans(n_clusters=num_clusters)
|
| 35 |
kmeans.fit(line_parameters)
|
| 36 |
labels = kmeans.labels_
|
| 37 |
centroids = kmeans.cluster_centers_
|
| 38 |
-
# how many points are in each cluster
|
| 39 |
counts = np.bincount(labels)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
ax.set_xlabel('Slope')
|
| 45 |
-
ax.set_ylabel('Intercept')
|
| 46 |
|
| 47 |
-
|
| 48 |
-
'''
|
| 49 |
-
x = np.linspace(x_min, x_max, num_points)
|
| 50 |
-
for i in range(num_clusters):
|
| 51 |
-
slope, intercept = centroids[i]
|
| 52 |
-
y = slope * x + intercept
|
| 53 |
-
ax.plot(x, y, linewidth=2)
|
| 54 |
-
'''
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 60 |
-
|
| 61 |
-
# Close the figure to free up memory
|
| 62 |
-
plt.close(fig)
|
| 63 |
-
|
| 64 |
-
# Return the labels, centroids, and image
|
| 65 |
-
return labels, centroids, counts, img
|
| 66 |
-
|
| 67 |
-
import gradio as gr
|
| 68 |
-
|
| 69 |
-
def predict_line_with_noise(slope, intercept, mean, std, num_points, x_min, x_max):
|
| 70 |
-
x_values = np.linspace(x_min, x_max, num_points)
|
| 71 |
-
noise = np.random.normal(mean, std, num_points)
|
| 72 |
-
y_values = slope * x_values + intercept + noise
|
| 73 |
-
return x_values, y_values
|
| 74 |
-
|
| 75 |
-
def cluster_line_params(slope, intercept, mean, std, num_points, x_min, x_max, num_iterations, num_clusters):
|
| 76 |
-
num_points = int(num_points)
|
| 77 |
num_iterations = int(num_iterations)
|
| 78 |
num_clusters = int(num_clusters)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
centroids = np.c_[centroids, counts]
|
| 84 |
df = pd.DataFrame(centroids, columns=["Slope", "Intercept", "Count"])
|
| 85 |
-
|
| 86 |
-
return
|
| 87 |
|
| 88 |
# Define input and output components
|
| 89 |
inputs = [
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
]
|
| 100 |
|
| 101 |
outputs = [
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
]
|
| 107 |
|
| 108 |
# Create the Gradio interface
|
|
@@ -113,11 +146,10 @@ interface = gr.Interface(
|
|
| 113 |
outputs=outputs,
|
| 114 |
title="Line Parameter Clustering",
|
| 115 |
description="Cluster line parameters with Gaussian noise",
|
| 116 |
-
allow_flagging=
|
| 117 |
)
|
| 118 |
|
| 119 |
# Launch the interface
|
| 120 |
interface.launch()
|
| 121 |
|
| 122 |
|
| 123 |
-
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from sklearn.cluster import KMeans
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from gradio.components import Slider, Number, Image, Dataframe
|
| 7 |
+
import matplotlib
|
| 8 |
+
matplotlib.use('Agg')
|
| 9 |
+
|
| 10 |
+
def generate_random_points_in_circle(center_x, center_y, radius, num_points):
|
| 11 |
+
angles = np.random.uniform(0, 2 * np.pi, num_points)
|
| 12 |
+
radii = np.sqrt(np.random.uniform(0, radius**2, num_points))
|
| 13 |
+
x_values = center_x + radii * np.cos(angles)
|
| 14 |
+
y_values = center_y + radii * np.sin(angles)
|
| 15 |
+
points = np.column_stack((x_values, y_values))
|
| 16 |
+
return points
|
| 17 |
+
|
| 18 |
+
def generate_random_points_in_line_between_bounds_and_with_noise(slope, intercept, mean, std, num_points, x_min, x_max):
|
| 19 |
+
x_values = np.linspace(x_min, x_max, num_points)
|
| 20 |
+
noise = np.random.normal(mean, std, num_points)
|
| 21 |
+
y_values = slope * x_values + intercept + noise
|
| 22 |
+
points = np.column_stack((x_values, y_values))
|
| 23 |
+
return points
|
| 24 |
+
|
| 25 |
+
def generate_all_points(line_num_points, circle_num_points, center_x, center_y, radius, slope, intercept, mean, std, x_min, x_max):
|
| 26 |
+
circle_points = generate_random_points_in_circle(center_x, center_y, radius, circle_num_points)
|
| 27 |
+
line_points = generate_random_points_in_line_between_bounds_and_with_noise(slope, intercept, mean, std, line_num_points, x_min, x_max)
|
| 28 |
+
all_points = np.concatenate((circle_points, line_points), axis=0)
|
| 29 |
+
return line_points, circle_points, all_points
|
| 30 |
+
|
| 31 |
+
def create_points_plot(line_points, circle_points):
|
| 32 |
+
fig, ax = plt.subplots()
|
| 33 |
+
x_values = line_points[:, 0]
|
| 34 |
+
y_values = line_points[:, 1]
|
| 35 |
+
ax.scatter(x_values, y_values, alpha=0.2, color="blue")
|
| 36 |
+
ax.scatter(circle_points[:, 0], circle_points[:, 1], alpha=0.2, color='red')
|
| 37 |
+
fig.canvas.draw()
|
| 38 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 39 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 40 |
+
plt.close(fig)
|
| 41 |
+
return img
|
| 42 |
+
|
| 43 |
+
def create_points_lines_plot(line_points, line_parameters):
|
| 44 |
+
fig, ax = plt.subplots()
|
| 45 |
+
x_values = line_points[:, 0]
|
| 46 |
+
y_values = line_points[:, 1]
|
| 47 |
+
ax.scatter(x_values, y_values, alpha=0.2)
|
| 48 |
+
for slope, intercept in line_parameters:
|
| 49 |
+
ax.plot(x_values, slope * x_values + intercept, alpha=0.5)
|
| 50 |
+
fig.canvas.draw()
|
| 51 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 52 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 53 |
+
plt.close(fig)
|
| 54 |
+
return img
|
| 55 |
+
|
| 56 |
+
def create_cluster_plot(line_parameters, labels, centroids):
|
| 57 |
+
fig, ax = plt.subplots()
|
| 58 |
+
ax.scatter(line_parameters[:, 0], line_parameters[:, 1], c=labels, cmap='viridis')
|
| 59 |
+
ax.set_xlabel('Slope')
|
| 60 |
+
ax.set_ylabel('Intercept')
|
| 61 |
+
|
| 62 |
+
fig.canvas.draw()
|
| 63 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 64 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 65 |
|
| 66 |
+
plt.close(fig)
|
| 67 |
+
|
| 68 |
+
return img
|
| 69 |
+
|
| 70 |
+
# Modify the compute_line_parameters function
|
| 71 |
+
def compute_line_parameters(num_iterations, all_points):
|
| 72 |
line_parameters = np.zeros((num_iterations, 2))
|
| 73 |
for i in range(num_iterations):
|
| 74 |
+
idx = np.random.choice(all_points.shape[0], 2, replace=False)
|
| 75 |
+
x1, y1 = all_points[idx[0], 0], all_points[idx[0], 1]
|
| 76 |
+
x2, y2 = all_points[idx[1], 0], all_points[idx[1], 1]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
|
|
|
| 78 |
line_slope = (y2 - y1) / (x2 - x1)
|
| 79 |
line_intercept = y1 - line_slope * x1
|
| 80 |
|
|
|
|
| 83 |
|
| 84 |
return line_parameters
|
| 85 |
|
|
|
|
|
|
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
def cluster_line_parameters(line_parameters, num_clusters, all_points, circle_points):
|
|
|
|
| 89 |
kmeans = KMeans(n_clusters=num_clusters)
|
| 90 |
kmeans.fit(line_parameters)
|
| 91 |
labels = kmeans.labels_
|
| 92 |
centroids = kmeans.cluster_centers_
|
|
|
|
| 93 |
counts = np.bincount(labels)
|
| 94 |
|
| 95 |
+
points_img = create_points_plot(all_points, circle_points)
|
| 96 |
+
points_lines_img = create_points_lines_plot(all_points, line_parameters)
|
| 97 |
+
cluster_img = create_cluster_plot(line_parameters, labels, centroids)
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
return labels, centroids, counts, points_img, points_lines_img, cluster_img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
# Update the cluster_line_params function
|
| 102 |
+
def cluster_line_params(slope, intercept, mean, std, line_num_points, x_min, x_max, num_iterations, num_clusters, center_x, center_y, radius, circle_num_points, random_seed):
|
| 103 |
+
np.random.seed(random_seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
num_iterations = int(num_iterations)
|
| 105 |
num_clusters = int(num_clusters)
|
| 106 |
+
|
| 107 |
+
line_points, circle_points, all_points = generate_all_points(line_num_points, circle_num_points, center_x, center_y, radius, slope, intercept, mean, std, x_min, x_max)
|
| 108 |
+
line_parameters = compute_line_parameters(num_iterations, all_points)
|
| 109 |
+
labels, centroids, counts, points_img, points_lines_img, cluster_img = cluster_line_parameters(line_parameters, num_clusters, all_points, circle_points)
|
| 110 |
+
|
| 111 |
centroids = np.c_[centroids, counts]
|
| 112 |
df = pd.DataFrame(centroids, columns=["Slope", "Intercept", "Count"])
|
| 113 |
+
|
| 114 |
+
return points_img, points_lines_img, cluster_img, df
|
| 115 |
|
| 116 |
# Define input and output components
|
| 117 |
inputs = [
|
| 118 |
+
Slider(minimum=-5.0, maximum=5.0, value=1.0, label="Line Slope"),
|
| 119 |
+
Slider(minimum=-10.0, maximum=10.0, value=0.0, label="Line Intercept"),
|
| 120 |
+
Slider(minimum=0.0, maximum=5.0, value=1.0, label="Line Error Mean"),
|
| 121 |
+
Slider(minimum=0.0, maximum=5.0, value=1.0, label="Line Error Standard Deviation"),
|
| 122 |
+
Number(value=100, precision=0, label="Number of Points Sampled from the Line"),
|
| 123 |
+
Number(value=-5, label="Minimum Value of x for Sampling"),
|
| 124 |
+
Number(value=5, label="Maximum Value of x for Sampling"),
|
| 125 |
+
Number(value=100, label="Number of Iterations for RANSAC-like Line Parameter Estimation"),
|
| 126 |
+
Number(value=3, label="Number of Clusters of Line Parameters"),
|
| 127 |
+
Number(value=0, label="X Value Center of the Circle"),
|
| 128 |
+
Number(value=0, label="Y Value Center of the Circle"),
|
| 129 |
+
Number(value=1, label="Radius of the Circle"),
|
| 130 |
+
Number(value=100, precision=0, label="Number of Points Sampled from the Circle"),
|
| 131 |
+
Number(value=0, precision=0, label="Random Seed")
|
| 132 |
]
|
| 133 |
|
| 134 |
outputs = [
|
| 135 |
+
Image(label="Points", type="pil"),
|
| 136 |
+
Image(label="Points and Lines", type="pil"),
|
| 137 |
+
Image(label="Clustering of the line parameters", type="pil"),
|
| 138 |
+
Dataframe(label="Centroids and Counts", type="pandas")
|
| 139 |
]
|
| 140 |
|
| 141 |
# Create the Gradio interface
|
|
|
|
| 146 |
outputs=outputs,
|
| 147 |
title="Line Parameter Clustering",
|
| 148 |
description="Cluster line parameters with Gaussian noise",
|
| 149 |
+
allow_flagging="never"
|
| 150 |
)
|
| 151 |
|
| 152 |
# Launch the interface
|
| 153 |
interface.launch()
|
| 154 |
|
| 155 |
|
|
|
backup1.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
def compute_line_parameters(slope, intercept, mean, std, num_points, x_min, x_max, num_iterations):
|
| 5 |
+
line_parameters = np.zeros((num_iterations, 2))
|
| 6 |
+
for i in range(num_iterations):
|
| 7 |
+
# Generate points on the line with noise
|
| 8 |
+
x_values, y_values_with_noise = predict_line_with_noise(slope, intercept, mean, std, num_points, x_min, x_max)
|
| 9 |
+
|
| 10 |
+
# Randomly sample two points from the line
|
| 11 |
+
idx = np.random.choice(num_points, 2, replace=False)
|
| 12 |
+
x1, y1 = x_values[idx[0]], y_values_with_noise[idx[0]]
|
| 13 |
+
x2, y2 = x_values[idx[1]], y_values_with_noise[idx[1]]
|
| 14 |
+
|
| 15 |
+
# Compute slope and intercept of line passing through sampled points
|
| 16 |
+
line_slope = (y2 - y1) / (x2 - x1)
|
| 17 |
+
line_intercept = y1 - line_slope * x1
|
| 18 |
+
|
| 19 |
+
line_parameters[i, 0] = line_slope
|
| 20 |
+
line_parameters[i, 1] = line_intercept
|
| 21 |
+
|
| 22 |
+
return line_parameters
|
| 23 |
+
|
| 24 |
+
from sklearn.cluster import KMeans
|
| 25 |
+
# import matplotlib.pyplot as plt
|
| 26 |
+
|
| 27 |
+
import gradio as gr
|
| 28 |
+
import matplotlib.pyplot as plt
|
| 29 |
+
import numpy as np
|
| 30 |
+
from sklearn.cluster import KMeans
|
| 31 |
+
|
| 32 |
+
def cluster_line_parameters(line_parameters, num_clusters):
|
| 33 |
+
# Cluster line parameters using KMeans
|
| 34 |
+
kmeans = KMeans(n_clusters=num_clusters)
|
| 35 |
+
kmeans.fit(line_parameters)
|
| 36 |
+
labels = kmeans.labels_
|
| 37 |
+
centroids = kmeans.cluster_centers_
|
| 38 |
+
# how many points are in each cluster
|
| 39 |
+
counts = np.bincount(labels)
|
| 40 |
+
|
| 41 |
+
# Create a scatter plot of the data points
|
| 42 |
+
fig, ax = plt.subplots()
|
| 43 |
+
ax.scatter(line_parameters[:, 0], line_parameters[:, 1], c=labels, cmap='viridis')
|
| 44 |
+
ax.set_xlabel('Slope')
|
| 45 |
+
ax.set_ylabel('Intercept')
|
| 46 |
+
|
| 47 |
+
# Generate some lines using the centroids
|
| 48 |
+
'''
|
| 49 |
+
x = np.linspace(x_min, x_max, num_points)
|
| 50 |
+
for i in range(num_clusters):
|
| 51 |
+
slope, intercept = centroids[i]
|
| 52 |
+
y = slope * x + intercept
|
| 53 |
+
ax.plot(x, y, linewidth=2)
|
| 54 |
+
'''
|
| 55 |
+
|
| 56 |
+
# Convert the figure to a PNG image
|
| 57 |
+
fig.canvas.draw()
|
| 58 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 59 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 60 |
+
|
| 61 |
+
# Close the figure to free up memory
|
| 62 |
+
plt.close(fig)
|
| 63 |
+
|
| 64 |
+
# Return the labels, centroids, and image
|
| 65 |
+
return labels, centroids, counts, img
|
| 66 |
+
|
| 67 |
+
import gradio as gr
|
| 68 |
+
|
| 69 |
+
def predict_line_with_noise(slope, intercept, mean, std, num_points, x_min, x_max):
|
| 70 |
+
x_values = np.linspace(x_min, x_max, num_points)
|
| 71 |
+
noise = np.random.normal(mean, std, num_points)
|
| 72 |
+
y_values = slope * x_values + intercept + noise
|
| 73 |
+
return x_values, y_values
|
| 74 |
+
|
| 75 |
+
def cluster_line_params(slope, intercept, mean, std, num_points, x_min, x_max, num_iterations, num_clusters):
|
| 76 |
+
num_points = int(num_points)
|
| 77 |
+
num_iterations = int(num_iterations)
|
| 78 |
+
num_clusters = int(num_clusters)
|
| 79 |
+
line_parameters = compute_line_parameters(slope, intercept, mean, std, num_points, x_min, x_max, num_iterations)
|
| 80 |
+
labels, centroids, counts, img = cluster_line_parameters(line_parameters, num_clusters)
|
| 81 |
+
# return labels, centroids, img
|
| 82 |
+
# put counts as the third column of centroids
|
| 83 |
+
centroids = np.c_[centroids, counts]
|
| 84 |
+
df = pd.DataFrame(centroids, columns=["Slope", "Intercept", "Count"])
|
| 85 |
+
# return img, centroids, df
|
| 86 |
+
return img, df
|
| 87 |
+
|
| 88 |
+
# Define input and output components
|
| 89 |
+
inputs = [
|
| 90 |
+
gr.inputs.Slider(minimum=-5.0, maximum=5.0, default=1.0, label="Slope"),
|
| 91 |
+
gr.inputs.Slider(minimum=-10.0, maximum=10.0, default=0.0, label="Intercept"),
|
| 92 |
+
gr.inputs.Slider(minimum=0.0, maximum=5.0, default=1.0, label="Mean"),
|
| 93 |
+
gr.inputs.Slider(minimum=0.0, maximum=5.0, default=1.0, label="Standard Deviation"),
|
| 94 |
+
gr.inputs.Number(default=100, label="Number of Points"),
|
| 95 |
+
gr.inputs.Number(default=-5, label="Minimum Value of x"),
|
| 96 |
+
gr.inputs.Number(default=5, label="Maximum Value of x"),
|
| 97 |
+
gr.inputs.Number(default=100, label="Number of Iterations"),
|
| 98 |
+
gr.inputs.Number(default=3, label="Number of Clusters")
|
| 99 |
+
]
|
| 100 |
+
|
| 101 |
+
outputs = [
|
| 102 |
+
#image and numpy
|
| 103 |
+
gr.outputs.Image(label="Image", type="pil"),
|
| 104 |
+
# show the centroids and counts, which is a numpy array
|
| 105 |
+
gr.outputs.Dataframe(label="Centroids and Counts", type="pandas")
|
| 106 |
+
]
|
| 107 |
+
|
| 108 |
+
# Create the Gradio interface
|
| 109 |
+
interface = gr.Interface(
|
| 110 |
+
fn=cluster_line_params,
|
| 111 |
+
inputs=inputs,
|
| 112 |
+
# outputs=["numpy", "numpy", "image"],
|
| 113 |
+
outputs=outputs,
|
| 114 |
+
title="Line Parameter Clustering",
|
| 115 |
+
description="Cluster line parameters with Gaussian noise",
|
| 116 |
+
allow_flagging=False
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Launch the interface
|
| 120 |
+
interface.launch()
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
backup2.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from sklearn.cluster import KMeans
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from gradio.components import Slider, Number, Image, Dataframe
|
| 7 |
+
import matplotlib
|
| 8 |
+
matplotlib.use('Agg')
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
def generate_random_points_in_circle(center_x, center_y, radius, num_points):
|
| 13 |
+
angles = np.random.uniform(0, 2 * np.pi, num_points)
|
| 14 |
+
radii = np.sqrt(np.random.uniform(0, radius**2, num_points))
|
| 15 |
+
|
| 16 |
+
x_values = center_x + radii * np.cos(angles)
|
| 17 |
+
y_values = center_y + radii * np.sin(angles)
|
| 18 |
+
|
| 19 |
+
points = np.column_stack((x_values, y_values))
|
| 20 |
+
|
| 21 |
+
return points
|
| 22 |
+
|
| 23 |
+
def compute_line_parameters(slope, intercept, mean, std, num_points, x_min, x_max, num_iterations):
|
| 24 |
+
line_parameters = np.zeros((num_iterations, 2))
|
| 25 |
+
all_points = []
|
| 26 |
+
for i in range(num_iterations):
|
| 27 |
+
x_values, y_values_with_noise = predict_line_with_noise(slope, intercept, mean, std, num_points, x_min, x_max)
|
| 28 |
+
all_points.append(np.column_stack((x_values, y_values_with_noise)))
|
| 29 |
+
|
| 30 |
+
idx = np.random.choice(num_points, 2, replace=False)
|
| 31 |
+
x1, y1 = x_values[idx[0]], y_values_with_noise[idx[0]]
|
| 32 |
+
x2, y2 = x_values[idx[1]], y_values_with_noise[idx[1]]
|
| 33 |
+
|
| 34 |
+
line_slope = (y2 - y1) / (x2 - x1)
|
| 35 |
+
line_intercept = y1 - line_slope * x1
|
| 36 |
+
|
| 37 |
+
line_parameters[i, 0] = line_slope
|
| 38 |
+
line_parameters[i, 1] = line_intercept
|
| 39 |
+
|
| 40 |
+
return line_parameters, all_points
|
| 41 |
+
|
| 42 |
+
def create_points_plot(all_points, circle_points):
|
| 43 |
+
fig, ax = plt.subplots()
|
| 44 |
+
|
| 45 |
+
for points in all_points:
|
| 46 |
+
x_values = points[:, 0]
|
| 47 |
+
y_values = points[:, 1]
|
| 48 |
+
ax.scatter(x_values, y_values, alpha=0.2, color="blue")
|
| 49 |
+
|
| 50 |
+
ax.scatter(circle_points[:, 0], circle_points[:, 1], alpha=0.2, color='red')
|
| 51 |
+
|
| 52 |
+
fig.canvas.draw()
|
| 53 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 54 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 55 |
+
|
| 56 |
+
plt.close(fig)
|
| 57 |
+
|
| 58 |
+
return img
|
| 59 |
+
|
| 60 |
+
def create_points_lines_plot(all_points, line_parameters):
|
| 61 |
+
fig, ax = plt.subplots()
|
| 62 |
+
|
| 63 |
+
for points, (slope, intercept) in zip(all_points, line_parameters):
|
| 64 |
+
x_values = points[:, 0]
|
| 65 |
+
y_values = points[:, 1]
|
| 66 |
+
ax.scatter(x_values, y_values, alpha=0.2)
|
| 67 |
+
ax.plot(x_values, slope * x_values + intercept, alpha=0.5)
|
| 68 |
+
|
| 69 |
+
fig.canvas.draw()
|
| 70 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 71 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 72 |
+
|
| 73 |
+
plt.close(fig)
|
| 74 |
+
|
| 75 |
+
return img
|
| 76 |
+
|
| 77 |
+
def create_cluster_plot(line_parameters, labels, centroids):
|
| 78 |
+
fig, ax = plt.subplots()
|
| 79 |
+
ax.scatter(line_parameters[:, 0], line_parameters[:, 1], c=labels, cmap='viridis')
|
| 80 |
+
ax.set_xlabel('Slope')
|
| 81 |
+
ax.set_ylabel('Intercept')
|
| 82 |
+
|
| 83 |
+
fig.canvas.draw()
|
| 84 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 85 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 86 |
+
|
| 87 |
+
plt.close(fig)
|
| 88 |
+
|
| 89 |
+
return img
|
| 90 |
+
|
| 91 |
+
def cluster_line_parameters(line_parameters, num_clusters, all_points, circle_points):
|
| 92 |
+
kmeans = KMeans(n_clusters=num_clusters)
|
| 93 |
+
kmeans.fit(line_parameters)
|
| 94 |
+
labels = kmeans.labels_
|
| 95 |
+
centroids = kmeans.cluster_centers_
|
| 96 |
+
counts = np.bincount(labels)
|
| 97 |
+
|
| 98 |
+
points_img = create_points_plot(all_points, circle_points)
|
| 99 |
+
points_lines_img = create_points_lines_plot(all_points, line_parameters)
|
| 100 |
+
cluster_img = create_cluster_plot(line_parameters, labels, centroids)
|
| 101 |
+
|
| 102 |
+
return labels, centroids, counts, points_img, points_lines_img, cluster_img
|
| 103 |
+
|
| 104 |
+
def predict_line_with_noise(slope, intercept, mean, std, num_points, x_min, x_max):
|
| 105 |
+
x_values = np.linspace(x_min, x_max, num_points)
|
| 106 |
+
noise = np.random.normal(mean, std, num_points)
|
| 107 |
+
y_values = slope * x_values + intercept + noise
|
| 108 |
+
return x_values, y_values
|
| 109 |
+
|
| 110 |
+
def cluster_line_params(slope, intercept, mean, std, num_points, x_min, x_max, num_iterations, num_clusters, center_x, center_y, radius, circle_points, random_seed):
|
| 111 |
+
np.random.seed(random_seed)
|
| 112 |
+
num_points = int(num_points)
|
| 113 |
+
num_iterations = int(num_iterations)
|
| 114 |
+
num_clusters = int(num_clusters)
|
| 115 |
+
points_in_circle = generate_random_points_in_circle(center_x, center_y, radius, circle_points)
|
| 116 |
+
line_parameters, all_points = compute_line_parameters(slope, intercept, mean, std, num_points, x_min, x_max, num_iterations)
|
| 117 |
+
labels, centroids, counts, points_img, points_lines_img, cluster_img = cluster_line_parameters(line_parameters, num_clusters, all_points, points_in_circle)
|
| 118 |
+
# return labels, centroids, img
|
| 119 |
+
# put counts as the third column of centroids
|
| 120 |
+
centroids = np.c_[centroids, counts]
|
| 121 |
+
df = pd.DataFrame(centroids, columns=["Slope", "Intercept", "Count"])
|
| 122 |
+
# return img, centroids, df
|
| 123 |
+
return points_img, points_lines_img, cluster_img, df
|
| 124 |
+
|
| 125 |
+
# Define input and output components
|
| 126 |
+
inputs = [
|
| 127 |
+
Slider(minimum=-5.0, maximum=5.0, value=1.0, label="Line Slope"),
|
| 128 |
+
Slider(minimum=-10.0, maximum=10.0, value=0.0, label="Line Intercept"),
|
| 129 |
+
Slider(minimum=0.0, maximum=5.0, value=1.0, label="Line Error Mean"),
|
| 130 |
+
Slider(minimum=0.0, maximum=5.0, value=1.0, label="Line Error Standard Deviation"),
|
| 131 |
+
Number(value=100, label="Number of Points Sampled from the Line"),
|
| 132 |
+
Number(value=-5, label="Minimum Value of x for Sampling"),
|
| 133 |
+
Number(value=5, label="Maximum Value of x for Sampling"),
|
| 134 |
+
Number(value=100, label="Number of Iterations for RANSAC-like Line Parameter Estimation"),
|
| 135 |
+
Number(value=3, label="Number of Clusters of Line Parameters"),
|
| 136 |
+
Number(value=0, label="X Value Center of the Circle"),
|
| 137 |
+
Number(value=0, label="Y Value Center of the Circle"),
|
| 138 |
+
Number(value=1, label="Radius of the Circle"),
|
| 139 |
+
Number(value=100, precision=0, label="Number of Points Sampled from the Circle"),
|
| 140 |
+
Number(value=0, precision=0, label="Random Seed")
|
| 141 |
+
]
|
| 142 |
+
|
| 143 |
+
outputs = [
|
| 144 |
+
Image(label="Points", type="pil"),
|
| 145 |
+
Image(label="Points and Lines", type="pil"),
|
| 146 |
+
Image(label="Clustering of the line parameters", type="pil"),
|
| 147 |
+
Dataframe(label="Centroids and Counts", type="pandas")
|
| 148 |
+
]
|
| 149 |
+
|
| 150 |
+
# Create the Gradio interface
|
| 151 |
+
interface = gr.Interface(
|
| 152 |
+
fn=cluster_line_params,
|
| 153 |
+
inputs=inputs,
|
| 154 |
+
# outputs=["numpy", "numpy", "image"],
|
| 155 |
+
outputs=outputs,
|
| 156 |
+
title="Line Parameter Clustering",
|
| 157 |
+
description="Cluster line parameters with Gaussian noise",
|
| 158 |
+
allow_flagging="never"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
# Launch the interface
|
| 162 |
+
interface.launch()
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
|
line_models_sampling.py
CHANGED
|
@@ -1,18 +1,80 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
line_parameters = np.zeros((num_iterations, 2))
|
| 6 |
for i in range(num_iterations):
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
# Randomly sample two points from the line
|
| 11 |
-
idx = np.random.choice(num_points, 2, replace=False)
|
| 12 |
-
x1, y1 = x_values[idx[0]], y_values_with_noise[idx[0]]
|
| 13 |
-
x2, y2 = x_values[idx[1]], y_values_with_noise[idx[1]]
|
| 14 |
-
|
| 15 |
-
# Compute slope and intercept of line passing through sampled points
|
| 16 |
line_slope = (y2 - y1) / (x2 - x1)
|
| 17 |
line_intercept = y1 - line_slope * x1
|
| 18 |
|
|
@@ -21,88 +83,59 @@ def compute_line_parameters(slope, intercept, mean, std, num_points, x_min, x_ma
|
|
| 21 |
|
| 22 |
return line_parameters
|
| 23 |
|
| 24 |
-
from sklearn.cluster import KMeans
|
| 25 |
-
# import matplotlib.pyplot as plt
|
| 26 |
|
| 27 |
-
import gradio as gr
|
| 28 |
-
import matplotlib.pyplot as plt
|
| 29 |
-
import numpy as np
|
| 30 |
-
from sklearn.cluster import KMeans
|
| 31 |
|
| 32 |
-
def cluster_line_parameters(line_parameters, num_clusters):
|
| 33 |
-
# Cluster line parameters using KMeans
|
| 34 |
kmeans = KMeans(n_clusters=num_clusters)
|
| 35 |
kmeans.fit(line_parameters)
|
| 36 |
labels = kmeans.labels_
|
| 37 |
centroids = kmeans.cluster_centers_
|
| 38 |
-
# how many points are in each cluster
|
| 39 |
counts = np.bincount(labels)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
ax.set_xlabel('Slope')
|
| 45 |
-
ax.set_ylabel('Intercept')
|
| 46 |
-
|
| 47 |
-
# Generate some lines using the centroids
|
| 48 |
-
'''
|
| 49 |
-
x = np.linspace(x_min, x_max, num_points)
|
| 50 |
-
for i in range(num_clusters):
|
| 51 |
-
slope, intercept = centroids[i]
|
| 52 |
-
y = slope * x + intercept
|
| 53 |
-
ax.plot(x, y, linewidth=2)
|
| 54 |
-
'''
|
| 55 |
-
|
| 56 |
-
# Convert the figure to a PNG image
|
| 57 |
-
fig.canvas.draw()
|
| 58 |
-
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 59 |
-
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 60 |
-
|
| 61 |
-
# Close the figure to free up memory
|
| 62 |
-
plt.close(fig)
|
| 63 |
-
|
| 64 |
-
# Return the labels, centroids, and image
|
| 65 |
-
return labels, centroids, counts, img
|
| 66 |
-
|
| 67 |
-
import gradio as gr
|
| 68 |
|
| 69 |
-
|
| 70 |
-
x_values = np.linspace(x_min, x_max, num_points)
|
| 71 |
-
noise = np.random.normal(mean, std, num_points)
|
| 72 |
-
y_values = slope * x_values + intercept + noise
|
| 73 |
-
return x_values, y_values
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
num_iterations = int(num_iterations)
|
| 78 |
num_clusters = int(num_clusters)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
centroids = np.c_[centroids, counts]
|
| 84 |
df = pd.DataFrame(centroids, columns=["Slope", "Intercept", "Count"])
|
| 85 |
-
|
| 86 |
-
return
|
| 87 |
|
| 88 |
# Define input and output components
|
| 89 |
inputs = [
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
]
|
| 100 |
|
| 101 |
outputs = [
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
]
|
| 107 |
|
| 108 |
# Create the Gradio interface
|
|
@@ -113,11 +146,8 @@ interface = gr.Interface(
|
|
| 113 |
outputs=outputs,
|
| 114 |
title="Line Parameter Clustering",
|
| 115 |
description="Cluster line parameters with Gaussian noise",
|
| 116 |
-
allow_flagging=
|
| 117 |
)
|
| 118 |
|
| 119 |
# Launch the interface
|
| 120 |
-
interface.launch(
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from sklearn.cluster import KMeans
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from gradio.components import Slider, Number, Image, Dataframe
|
| 7 |
+
import matplotlib
|
| 8 |
+
matplotlib.use('Agg')
|
| 9 |
+
|
| 10 |
+
def generate_random_points_in_circle(center_x, center_y, radius, num_points):
|
| 11 |
+
angles = np.random.uniform(0, 2 * np.pi, num_points)
|
| 12 |
+
radii = np.sqrt(np.random.uniform(0, radius**2, num_points))
|
| 13 |
+
x_values = center_x + radii * np.cos(angles)
|
| 14 |
+
y_values = center_y + radii * np.sin(angles)
|
| 15 |
+
points = np.column_stack((x_values, y_values))
|
| 16 |
+
return points
|
| 17 |
+
|
| 18 |
+
def generate_random_points_in_line_between_bounds_and_with_noise(slope, intercept, mean, std, num_points, x_min, x_max):
|
| 19 |
+
x_values = np.linspace(x_min, x_max, num_points)
|
| 20 |
+
noise = np.random.normal(mean, std, num_points)
|
| 21 |
+
y_values = slope * x_values + intercept + noise
|
| 22 |
+
points = np.column_stack((x_values, y_values))
|
| 23 |
+
return points
|
| 24 |
|
| 25 |
+
def generate_all_points(line_num_points, circle_num_points, center_x, center_y, radius, slope, intercept, mean, std, x_min, x_max):
|
| 26 |
+
circle_points = generate_random_points_in_circle(center_x, center_y, radius, circle_num_points)
|
| 27 |
+
line_points = generate_random_points_in_line_between_bounds_and_with_noise(slope, intercept, mean, std, line_num_points, x_min, x_max)
|
| 28 |
+
all_points = np.concatenate((circle_points, line_points), axis=0)
|
| 29 |
+
return line_points, circle_points, all_points
|
| 30 |
+
|
| 31 |
+
def create_points_plot(line_points, circle_points):
|
| 32 |
+
fig, ax = plt.subplots()
|
| 33 |
+
x_values = line_points[:, 0]
|
| 34 |
+
y_values = line_points[:, 1]
|
| 35 |
+
ax.scatter(x_values, y_values, alpha=0.2, color="blue")
|
| 36 |
+
ax.scatter(circle_points[:, 0], circle_points[:, 1], alpha=0.2, color='red')
|
| 37 |
+
fig.canvas.draw()
|
| 38 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 39 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 40 |
+
plt.close(fig)
|
| 41 |
+
return img
|
| 42 |
+
|
| 43 |
+
def create_points_lines_plot(line_points, line_parameters):
|
| 44 |
+
fig, ax = plt.subplots()
|
| 45 |
+
x_values = line_points[:, 0]
|
| 46 |
+
y_values = line_points[:, 1]
|
| 47 |
+
ax.scatter(x_values, y_values, alpha=0.2)
|
| 48 |
+
for slope, intercept in line_parameters:
|
| 49 |
+
ax.plot(x_values, slope * x_values + intercept, alpha=0.5)
|
| 50 |
+
fig.canvas.draw()
|
| 51 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 52 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 53 |
+
plt.close(fig)
|
| 54 |
+
return img
|
| 55 |
+
|
| 56 |
+
def create_cluster_plot(line_parameters, labels, centroids):
|
| 57 |
+
fig, ax = plt.subplots()
|
| 58 |
+
ax.scatter(line_parameters[:, 0], line_parameters[:, 1], c=labels, cmap='viridis')
|
| 59 |
+
ax.set_xlabel('Slope')
|
| 60 |
+
ax.set_ylabel('Intercept')
|
| 61 |
+
|
| 62 |
+
fig.canvas.draw()
|
| 63 |
+
img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 64 |
+
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 65 |
+
|
| 66 |
+
plt.close(fig)
|
| 67 |
+
|
| 68 |
+
return img
|
| 69 |
+
|
| 70 |
+
# Modify the compute_line_parameters function
|
| 71 |
+
def compute_line_parameters(num_iterations, all_points):
|
| 72 |
line_parameters = np.zeros((num_iterations, 2))
|
| 73 |
for i in range(num_iterations):
|
| 74 |
+
idx = np.random.choice(all_points.shape[0], 2, replace=False)
|
| 75 |
+
x1, y1 = all_points[idx[0], 0], all_points[idx[0], 1]
|
| 76 |
+
x2, y2 = all_points[idx[1], 0], all_points[idx[1], 1]
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
line_slope = (y2 - y1) / (x2 - x1)
|
| 79 |
line_intercept = y1 - line_slope * x1
|
| 80 |
|
|
|
|
| 83 |
|
| 84 |
return line_parameters
|
| 85 |
|
|
|
|
|
|
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
def cluster_line_parameters(line_parameters, num_clusters, all_points, circle_points):
|
|
|
|
| 89 |
kmeans = KMeans(n_clusters=num_clusters)
|
| 90 |
kmeans.fit(line_parameters)
|
| 91 |
labels = kmeans.labels_
|
| 92 |
centroids = kmeans.cluster_centers_
|
|
|
|
| 93 |
counts = np.bincount(labels)
|
| 94 |
|
| 95 |
+
points_img = create_points_plot(all_points, circle_points)
|
| 96 |
+
points_lines_img = create_points_lines_plot(all_points, line_parameters)
|
| 97 |
+
cluster_img = create_cluster_plot(line_parameters, labels, centroids)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
return labels, centroids, counts, points_img, points_lines_img, cluster_img
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
# Update the cluster_line_params function
|
| 102 |
+
def cluster_line_params(slope, intercept, mean, std, line_num_points, x_min, x_max, num_iterations, num_clusters, center_x, center_y, radius, circle_num_points, random_seed):
|
| 103 |
+
np.random.seed(random_seed)
|
| 104 |
num_iterations = int(num_iterations)
|
| 105 |
num_clusters = int(num_clusters)
|
| 106 |
+
|
| 107 |
+
line_points, circle_points, all_points = generate_all_points(line_num_points, circle_num_points, center_x, center_y, radius, slope, intercept, mean, std, x_min, x_max)
|
| 108 |
+
line_parameters = compute_line_parameters(num_iterations, all_points)
|
| 109 |
+
labels, centroids, counts, points_img, points_lines_img, cluster_img = cluster_line_parameters(line_parameters, num_clusters, all_points, circle_points)
|
| 110 |
+
|
| 111 |
centroids = np.c_[centroids, counts]
|
| 112 |
df = pd.DataFrame(centroids, columns=["Slope", "Intercept", "Count"])
|
| 113 |
+
|
| 114 |
+
return points_img, points_lines_img, cluster_img, df
|
| 115 |
|
| 116 |
# Define input and output components
|
| 117 |
inputs = [
|
| 118 |
+
Slider(minimum=-5.0, maximum=5.0, value=1.0, label="Line Slope"),
|
| 119 |
+
Slider(minimum=-10.0, maximum=10.0, value=0.0, label="Line Intercept"),
|
| 120 |
+
Slider(minimum=0.0, maximum=5.0, value=1.0, label="Line Error Mean"),
|
| 121 |
+
Slider(minimum=0.0, maximum=5.0, value=1.0, label="Line Error Standard Deviation"),
|
| 122 |
+
Number(value=100, precision=0, label="Number of Points Sampled from the Line"),
|
| 123 |
+
Number(value=-5, label="Minimum Value of x for Sampling"),
|
| 124 |
+
Number(value=5, label="Maximum Value of x for Sampling"),
|
| 125 |
+
Number(value=100, label="Number of Iterations for RANSAC-like Line Parameter Estimation"),
|
| 126 |
+
Number(value=3, label="Number of Clusters of Line Parameters"),
|
| 127 |
+
Number(value=0, label="X Value Center of the Circle"),
|
| 128 |
+
Number(value=0, label="Y Value Center of the Circle"),
|
| 129 |
+
Number(value=1, label="Radius of the Circle"),
|
| 130 |
+
Number(value=100, precision=0, label="Number of Points Sampled from the Circle"),
|
| 131 |
+
Number(value=0, precision=0, label="Random Seed")
|
| 132 |
]
|
| 133 |
|
| 134 |
outputs = [
|
| 135 |
+
Image(label="Points", type="pil"),
|
| 136 |
+
Image(label="Points and Lines", type="pil"),
|
| 137 |
+
Image(label="Clustering of the line parameters", type="pil"),
|
| 138 |
+
Dataframe(label="Centroids and Counts", type="pandas")
|
| 139 |
]
|
| 140 |
|
| 141 |
# Create the Gradio interface
|
|
|
|
| 146 |
outputs=outputs,
|
| 147 |
title="Line Parameter Clustering",
|
| 148 |
description="Cluster line parameters with Gaussian noise",
|
| 149 |
+
allow_flagging="never"
|
| 150 |
)
|
| 151 |
|
| 152 |
# Launch the interface
|
| 153 |
+
interface.launch()
|
|
|
|
|
|
|
|
|