Spaces:
Sleeping
Sleeping
added logreg
Browse files- .gradio/flagged/Upload Digit Image/7f4b9bd6aad14867077e/sample_6_0.jpg +0 -0
- .gradio/flagged/dataset1.csv +3 -0
- GNB/__pycache__/gaussian_naive_bayes.cpython-313.pyc +0 -0
- LogisticRegresssion/LogisticRegression.py +165 -0
- LogisticRegresssion/__pycache__/LogisticRegression.cpython-312.pyc +0 -0
- LogisticRegresssion/__pycache__/LogisticRegression.cpython-313.pyc +0 -0
- app.py +64 -7
- custom_logreg_hog.pkl +3 -0
- requirements.txt +2 -1
.gradio/flagged/Upload Digit Image/7f4b9bd6aad14867077e/sample_6_0.jpg
ADDED
|
.gradio/flagged/dataset1.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Upload Digit Image,Prediction,timestamp
|
| 2 |
+
,,2026-05-01 03:58:08.315134
|
| 3 |
+
.gradio\flagged\Upload Digit Image\7f4b9bd6aad14867077e\sample_6_0.jpg,Is 6,2026-05-01 04:00:35.641165
|
GNB/__pycache__/gaussian_naive_bayes.cpython-313.pyc
CHANGED
|
Binary files a/GNB/__pycache__/gaussian_naive_bayes.cpython-313.pyc and b/GNB/__pycache__/gaussian_naive_bayes.cpython-313.pyc differ
|
|
|
LogisticRegresssion/LogisticRegression.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
class LogReg():
|
| 4 |
+
def __init__(self, max_iterations=100, threshold=0.5, learning_rate=0.01, class_weight=None, reg_eqn=None, reg_param=0, random_state=42):
|
| 5 |
+
self.threshold = threshold
|
| 6 |
+
self.max_iterations = max_iterations
|
| 7 |
+
self.learning_rate = learning_rate
|
| 8 |
+
self.class_weight = class_weight
|
| 9 |
+
self.reg_eqn = reg_eqn
|
| 10 |
+
self.reg_param=reg_param
|
| 11 |
+
self.random_state=random_state
|
| 12 |
+
self.weights = None
|
| 13 |
+
self.bias = 0.0
|
| 14 |
+
|
| 15 |
+
def __linear(self, x):
|
| 16 |
+
return np.dot(x, self.weights) + self.bias
|
| 17 |
+
|
| 18 |
+
def __sigmoid(self, z):
|
| 19 |
+
z = np.clip(z, -500, 500)
|
| 20 |
+
return 1.0 / (1.0 + np.exp(-z))
|
| 21 |
+
|
| 22 |
+
def __loss(self, preds, y):
|
| 23 |
+
m = len(y)
|
| 24 |
+
eps = 1e-9
|
| 25 |
+
preds = np.clip(preds, eps, 1 - eps)
|
| 26 |
+
return - (1/m) * np.sum(y * np.log(preds) + (1 - y) * np.log(1 - preds))
|
| 27 |
+
|
| 28 |
+
def fit(self, x, y):
|
| 29 |
+
np.random.seed(42)
|
| 30 |
+
num_samples, num_features = x.shape
|
| 31 |
+
classes = np.unique(y)
|
| 32 |
+
self.weights = np.random.randn(num_features) * 0.01
|
| 33 |
+
self.bias = 0.0
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
classes_weights = {}
|
| 37 |
+
if self.class_weight is None:
|
| 38 |
+
classes_weights = {label: 1.0 for label in classes}
|
| 39 |
+
elif self.class_weight == 'balanced':
|
| 40 |
+
for label in classes:
|
| 41 |
+
classes_weights[label] = num_samples / (len(classes) * np.sum(y == label))
|
| 42 |
+
else:
|
| 43 |
+
classes_weights = self.class_weight
|
| 44 |
+
|
| 45 |
+
loss_weights = np.ones_like(y, dtype=float)
|
| 46 |
+
for label in classes:
|
| 47 |
+
loss_weights[y == label] = classes_weights[label]
|
| 48 |
+
|
| 49 |
+
weight_sum = np.sum(loss_weights)
|
| 50 |
+
|
| 51 |
+
for i in range(self.max_iterations):
|
| 52 |
+
linear_predictions = self.__linear(x)
|
| 53 |
+
probas = self.__sigmoid(linear_predictions)
|
| 54 |
+
cost = self.__loss(probas, y)
|
| 55 |
+
|
| 56 |
+
if self.reg_eqn == 'L2':
|
| 57 |
+
cost += self.reg_param * np.sum(self.weights ** 2)
|
| 58 |
+
elif self.reg_eqn == 'L1':
|
| 59 |
+
cost += self.reg_param * np.sum(np.abs(self.weights))
|
| 60 |
+
|
| 61 |
+
#if i % 10 == 0:
|
| 62 |
+
#print(f"Iteration {i}, Loss: {cost}")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
dw = np.dot(x.T, loss_weights*(probas-y)) / weight_sum
|
| 66 |
+
db = np.sum(loss_weights*(probas-y)) / weight_sum
|
| 67 |
+
|
| 68 |
+
if self.reg_eqn == 'L2':
|
| 69 |
+
dw += (self.reg_param / weight_sum) * self.weights
|
| 70 |
+
elif self.reg_eqn == 'L1':
|
| 71 |
+
dw += (self.reg_param / weight_sum) * np.sign(self.weights)
|
| 72 |
+
|
| 73 |
+
self.weights -= self.learning_rate * dw
|
| 74 |
+
self.bias -= self.learning_rate * db
|
| 75 |
+
|
| 76 |
+
def predict(self, test):
|
| 77 |
+
linear_predictions = self.__linear(test)
|
| 78 |
+
probas = self.__sigmoid(linear_predictions)
|
| 79 |
+
return (probas >= self.threshold).astype(int)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
class MultiLogReg():
|
| 85 |
+
def __init__(self, max_iterations=100, learning_rate=0.01, class_weight=None, reg_eqn=None, reg_param=0, random_state=42):
|
| 86 |
+
self.max_iterations = max_iterations
|
| 87 |
+
self.learning_rate = learning_rate
|
| 88 |
+
self.class_weight = class_weight
|
| 89 |
+
self.reg_eqn = reg_eqn
|
| 90 |
+
self.reg_param = reg_param
|
| 91 |
+
self.random_state = random_state
|
| 92 |
+
self.classes = None
|
| 93 |
+
self.weights = None
|
| 94 |
+
self.bias = None
|
| 95 |
+
|
| 96 |
+
def __linear(self, x):
|
| 97 |
+
return np.dot(x, self.weights) + self.bias
|
| 98 |
+
|
| 99 |
+
def __softmax(self, z):
|
| 100 |
+
e_z = np.exp(z - np.max(z, axis=-1, keepdims=True))
|
| 101 |
+
return e_z / np.sum(e_z, axis=-1, keepdims=True)
|
| 102 |
+
|
| 103 |
+
def __one_hot(self, y):
|
| 104 |
+
n_samples = len(y)
|
| 105 |
+
classes = np.unique(y)
|
| 106 |
+
one_hot = np.zeros((n_samples, len(classes)))
|
| 107 |
+
one_hot[np.arange(n_samples), y] = 1
|
| 108 |
+
return one_hot
|
| 109 |
+
|
| 110 |
+
def __loss(self, preds, y_one_hot):
|
| 111 |
+
m = len(y_one_hot)
|
| 112 |
+
eps = 1e-9
|
| 113 |
+
preds = np.clip(preds, eps, 1 - eps)
|
| 114 |
+
return -np.sum(y_one_hot * np.log(preds)) / m
|
| 115 |
+
|
| 116 |
+
def fit(self, x, y):
|
| 117 |
+
np.random.seed(self.random_state)
|
| 118 |
+
num_samples, num_features = x.shape
|
| 119 |
+
self.classes = np.unique(y)
|
| 120 |
+
num_classes = len(self.classes)
|
| 121 |
+
self.weights = np.random.randn(num_features, num_classes) * 0.01
|
| 122 |
+
self.bias = np.zeros(num_classes)
|
| 123 |
+
|
| 124 |
+
classes_weights = {}
|
| 125 |
+
if self.class_weight is None:
|
| 126 |
+
classes_weights = {label: 1.0 for label in self.classes}
|
| 127 |
+
elif self.class_weight == 'balanced':
|
| 128 |
+
for label in self.classes:
|
| 129 |
+
classes_weights[label] = num_samples / (len(self.classes) * np.sum(y == label))
|
| 130 |
+
else:
|
| 131 |
+
classes_weights = self.class_weight
|
| 132 |
+
|
| 133 |
+
loss_weights = np.ones_like(y, dtype=float)
|
| 134 |
+
for label in self.classes:
|
| 135 |
+
loss_weights[y == label] = classes_weights[label]
|
| 136 |
+
|
| 137 |
+
weight_sum = np.sum(loss_weights)
|
| 138 |
+
|
| 139 |
+
loss_weights = loss_weights.reshape(-1,1)
|
| 140 |
+
y_one_hot = self.__one_hot(y)
|
| 141 |
+
|
| 142 |
+
for i in range(self.max_iterations):
|
| 143 |
+
linear_predictions = self.__linear(x)
|
| 144 |
+
probas = self.__softmax(linear_predictions)
|
| 145 |
+
cost = self.__loss(probas, y_one_hot)
|
| 146 |
+
|
| 147 |
+
#if i % 10 == 0:
|
| 148 |
+
#print(f"Iteration {i}, Loss: {cost}")
|
| 149 |
+
|
| 150 |
+
dw = np.dot(x.T, loss_weights*(probas-y_one_hot)) / weight_sum
|
| 151 |
+
db = np.sum(loss_weights*(probas-y_one_hot), axis=0) / weight_sum
|
| 152 |
+
|
| 153 |
+
if self.reg_eqn == 'L2':
|
| 154 |
+
dw += (2 * self.reg_param / weight_sum) * self.weights
|
| 155 |
+
elif self.reg_eqn == 'L1':
|
| 156 |
+
dw += (self.reg_param / weight_sum) * np.sign(self.weights)
|
| 157 |
+
|
| 158 |
+
self.weights -= self.learning_rate * dw
|
| 159 |
+
self.bias -= self.learning_rate * db
|
| 160 |
+
|
| 161 |
+
def predict(self, test):
|
| 162 |
+
linear_predictions = self.__linear(test)
|
| 163 |
+
probas = self.__softmax(linear_predictions)
|
| 164 |
+
preds_indices = np.argmax(probas, axis=1)
|
| 165 |
+
return self.classes[preds_indices]
|
LogisticRegresssion/__pycache__/LogisticRegression.cpython-312.pyc
ADDED
|
Binary file (9.95 kB). View file
|
|
|
LogisticRegresssion/__pycache__/LogisticRegression.cpython-313.pyc
ADDED
|
Binary file (10.1 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
from skimage.feature import hog
|
|
|
|
| 4 |
from GNB.gaussian_naive_bayes import GNB
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
|
|
@@ -64,18 +66,73 @@ def classify_image(image):
|
|
| 64 |
return "Is 6"
|
| 65 |
else:
|
| 66 |
return "Not 6"
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
demo = gr.Interface(
|
| 70 |
-
fn=
|
| 71 |
-
inputs=
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
outputs=gr.Textbox(label="Prediction"),
|
| 77 |
title="MNIST Digit 6 Classifier",
|
| 78 |
-
description="Upload an image and the model will classify whether it is digit 6 or not."
|
| 79 |
)
|
|
|
|
| 80 |
if __name__ == "__main__":
|
| 81 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
from skimage.feature import hog
|
| 4 |
+
import cv2
|
| 5 |
from GNB.gaussian_naive_bayes import GNB
|
| 6 |
+
from LogisticRegresssion.LogisticRegression import LogReg
|
| 7 |
|
| 8 |
|
| 9 |
|
|
|
|
| 66 |
return "Is 6"
|
| 67 |
else:
|
| 68 |
return "Not 6"
|
| 69 |
+
|
| 70 |
|
| 71 |
+
with open("custom_logreg_hog.pkl", "rb") as f:
|
| 72 |
+
logreg_trained_model = pickle.load(f)
|
| 73 |
+
|
| 74 |
+
def classify_with_logreg(image):
|
| 75 |
+
"""
|
| 76 |
+
Takes an uploaded image from Gradio, preprocesses it to match the MNIST
|
| 77 |
+
training format, extracts HOG features, and predicts if it is a '6'.
|
| 78 |
+
"""
|
| 79 |
+
if len(image.shape) == 3:
|
| 80 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 81 |
+
|
| 82 |
+
image = cv2.resize(image, (28, 28))
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if np.mean(image) > 127:
|
| 86 |
+
image = cv2.bitwise_not(image)
|
| 87 |
+
|
| 88 |
+
image = image / 255.0
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
features = hog(
|
| 92 |
+
image,
|
| 93 |
+
orientations=9,
|
| 94 |
+
pixels_per_cell=(4, 4),
|
| 95 |
+
cells_per_block=(2, 2),
|
| 96 |
+
block_norm='L2-Hys',
|
| 97 |
+
feature_vector=True
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
processed_image = features.reshape(1, -1)
|
| 102 |
+
|
| 103 |
+
prediction = logreg_trained_model.predict(processed_image)
|
| 104 |
+
|
| 105 |
+
return "Target Detected: This is a 6!" if prediction[0] == 1 else "Not a 6."
|
| 106 |
+
|
| 107 |
+
def predict_with_selected_model(image, model_choice):
|
| 108 |
+
if image is None:
|
| 109 |
+
return "Please upload an image."
|
| 110 |
+
|
| 111 |
+
if model_choice == "Gaussian Naive Bayes":
|
| 112 |
+
return classify_image(image)
|
| 113 |
+
elif model_choice == "Logistic Regression":
|
| 114 |
+
return classify_with_logreg(image)
|
| 115 |
+
else:
|
| 116 |
+
return "Unknown model selected."
|
| 117 |
|
| 118 |
demo = gr.Interface(
|
| 119 |
+
fn=predict_with_selected_model,
|
| 120 |
+
inputs=[
|
| 121 |
+
gr.Image(
|
| 122 |
+
image_mode="L",
|
| 123 |
+
type="numpy",
|
| 124 |
+
label="Upload Digit Image"
|
| 125 |
+
),
|
| 126 |
+
gr.Radio(
|
| 127 |
+
choices=["Gaussian Naive Bayes", "Logistic Regression"],
|
| 128 |
+
value="Gaussian Naive Bayes",
|
| 129 |
+
label="Choose Classifier"
|
| 130 |
+
)
|
| 131 |
+
],
|
| 132 |
outputs=gr.Textbox(label="Prediction"),
|
| 133 |
title="MNIST Digit 6 Classifier",
|
| 134 |
+
description="Upload an image, select which algorithm to use, and the model will classify whether it is digit 6 or not."
|
| 135 |
)
|
| 136 |
+
|
| 137 |
if __name__ == "__main__":
|
| 138 |
demo.launch()
|
custom_logreg_hog.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:39a5c86359cdf7fcc3b08031a187ded596e0882c40d9cb005f88fa131259fb4c
|
| 3 |
+
size 10762
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
gradio
|
| 2 |
numpy
|
| 3 |
scikit-image
|
| 4 |
-
scikit-learn
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
numpy
|
| 3 |
scikit-image
|
| 4 |
+
scikit-learn
|
| 5 |
+
opencv-python-headless
|