vumichien's picture
Update app.py
489c177
import gradio as gr
import numpy as np
from matplotlib import pyplot as plt
from sklearn import linear_model, datasets
theme = gr.themes.Monochrome(
primary_hue="indigo",
secondary_hue="blue",
neutral_hue="slate",
)
model_card = f"""
## Description
**Random sample consensus (RANSAC)** is a method to estimate a mathematical model from a set of observed data that may have some wrong information.
The number of times it tries affects how likely it is to get a good answer. **RANSAC** is commonly used in photogrammetry to solve problems with linear or non-linear regression.
It works by separating the input data into two groups: inliers (which may have some noise) and outliers (which are wrong data). It estimates the model only using the inliers.
In this demo, a simulation regression dataset with noise is created, and then compare the results of fitting data in **Linear model** and **RANSAC**.
You can play around with different ``number of samples`` and ``number of outliers`` to see the effect
## Dataset
Simulation dataset
"""
def do_train(n_samples, n_outliers):
X, y, coef = datasets.make_regression(
n_samples=n_samples,
n_features=1,
n_informative=1,
noise=10,
coef=True,
random_state=0,
)
# Add outlier data
np.random.seed(0)
X[:n_outliers] = 3 + 0.5 * np.random.normal(size=(n_outliers, 1))
y[:n_outliers] = -3 + 10 * np.random.normal(size=n_outliers)
# Fit line using all data
lr = linear_model.LinearRegression()
lr.fit(X, y)
# Robustly fit linear model with RANSAC algorithm
ransac = linear_model.RANSACRegressor()
ransac.fit(X, y)
inlier_mask = ransac.inlier_mask_
outlier_mask = np.logical_not(inlier_mask)
# Predict data of estimated models
line_X = np.arange(X.min(), X.max())[:, np.newaxis]
line_y = lr.predict(line_X)
line_y_ransac = ransac.predict(line_X)
text = f"True coefficients: {coef:.4f}.\nLinear regression coefficients: {lr.coef_[0]:.4f}.\nRANSAC coefficients: {ransac.estimator_.coef_[0]:.4f}."
fig, axes = plt.subplots()
axes.scatter(
X[inlier_mask], y[inlier_mask], color="yellowgreen", marker=".", label="Inliers"
)
axes.scatter(
X[outlier_mask], y[outlier_mask], color="gold", marker=".", label="Outliers"
)
axes.plot(line_X, line_y, color="navy", linewidth=2, label="Linear regressor")
axes.plot(
line_X,
line_y_ransac,
color="cornflowerblue",
linewidth=2,
label="RANSAC regressor",
)
axes.legend(loc="lower right")
axes.set_xlabel("Input")
axes.set_ylabel("Response")
return fig, text
with gr.Blocks(theme=theme) as demo:
gr.Markdown('''
<div>
<h1 style='text-align: center'>Robust linear model estimation using RANSAC</h1>
</div>
''')
gr.Markdown(model_card)
gr.Markdown("Author: <a href=\"https://huggingface.co/vumichien\">Vu Minh Chien</a>. Based on the example from <a href=\"https://scikit-learn.org/stable/auto_examples/linear_model/plot_ransac.html#sphx-glr-auto-examples-linear-model-plot-ransac-py\">scikit-learn</a>")
n_samples = gr.Slider(minimum=500, maximum=5000, step=500, value=500, label="Number of samples")
n_outliers = gr.Slider(minimum=25, maximum=250, step=25, value=25, label="Number of outliers")
with gr.Row():
with gr.Column():
plot = gr.Plot(label="Compare Linear regressor and RANSAC")
with gr.Column():
results = gr.Textbox(label="Results")
n_samples.change(fn=do_train, inputs=[n_samples, n_outliers], outputs=[plot, results])
n_outliers.change(fn=do_train, inputs=[n_samples, n_outliers], outputs=[plot, results])
demo.launch()