Spaces:
Sleeping
Sleeping
File size: 10,202 Bytes
c6b938d 99590b1 bd8d998 c6b938d 63eadff c6b938d b587a85 e395d17 99590b1 8b4d0d5 e395d17 99590b1 8b4d0d5 e395d17 8b4d0d5 afb6160 8b4d0d5 afb6160 8b4d0d5 afb6160 b587a85 99590b1 4ccb3e8 20a56b5 339e819 8b4d0d5 0e9040f 99590b1 2a8cb04 99590b1 b587a85 99590b1 2a8cb04 99590b1 f779568 99590b1 f779568 99590b1 f779568 339e819 99590b1 14483b4 afb6160 99590b1 dfc71bd 8b4d0d5 beb5958 8b4d0d5 ffbf3ef 03658fe 99590b1 d85c7af 0d19feb 99590b1 ffbf3ef c6b938d d85c7af 99590b1 d85c7af 99590b1 f779568 99590b1 2a8cb04 99590b1 d85c7af 0d19feb d85c7af 196dfda 99590b1 0c783ea 4ccb3e8 0c783ea ffbf3ef beb5958 dc3e797 4b0fd5b 99c3135 4b0fd5b f779568 c6b938d 99590b1 c6b938d 99590b1 c6b938d 99590b1 c6b938d 99590b1 c77d957 99590b1 c6b938d 99590b1 b587a85 c77d957 99590b1 d85c7af 3d97c90 99590b1 01c0965 b587a85 99590b1 0e9040f 14483b4 99590b1 14483b4 99590b1 8b4d0d5 99590b1 8b4d0d5 99590b1 b587a85 99590b1 0aa2ff0 c77d957 99590b1 93188fe 99590b1 dc3e797 99590b1 339e819 8b4d0d5 99590b1 4b0fd5b 99590b1 8e9a321 ed82302 8e9a321 99590b1 c77d957 99590b1 0e9040f 99590b1 e219644 8e9a321 99590b1 c77d957 99590b1 c6b938d c77d957 ed82302 99590b1 c77d957 c6b938d 99590b1 c6b938d c77d957 99590b1 c6b938d 99590b1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | import gradio as gr
import numpy as np
from skimage.feature import hog
import pickle
import cv2
loading_errors = {}
def load_pickle_file(file_name):
try:
with open(file_name, "rb") as f:
loaded_file = pickle.load(f)
return loaded_file
except pickle.UnpicklingError:
error_msg = f"{file_name} is not a valid pickle. Run `git lfs pull`."
print(error_msg, flush=True)
loading_errors[file_name] = error_msg
return None
except Exception as exc:
error_msg = f"Could not load {file_name}: {exc}"
print(error_msg, flush=True)
loading_errors[file_name] = error_msg
return None
# def build_knn_from_pca_package(package):
# if package is None:
# return None
# required_keys = {"best_k", "weights", "X_train", "y_train", "pca_mean", "pca_components"}
# missing_keys = required_keys.difference(package)
# if missing_keys:
# print(f"knn_pca.pkl is missing keys: {sorted(missing_keys)}")
# return None
# knn = MultimodeKNN(k=package["best_k"], weights=package["weights"])
# knn.fit(package["X_train"], package["y_train"])
# return knn
gnb_package = load_pickle_file("gnb_digit6_package.pkl")
pca_multi = load_pickle_file("pcaMulti.pkl")
rf_model = load_pickle_file("rf_hog_model.pkl")
#pca_KNN = load_pickle_file("knn_pca.pkl")
if pca_multi is not None:
pca_multi = pca_multi['pca']
if gnb_package is not None:
gnb_binary_model = gnb_package["model"]
pca = gnb_package["pca"]
best_weight = gnb_package["best_weight"]
hog_params = gnb_package["hog_params"]
else:
gnb_binary_model = None
pca = None
best_weight = None
hog_params = None
print("Loading Decision Tree...")
dt_package = load_pickle_file("dt.pkl")
if dt_package is not None:
dt_binary_model = dt_package["model"]
else:
dt_binary_model = None
print("Loading Logistic Regression...")
logreg_binary_model = load_pickle_file("custom_logreg_hog.pkl")
svm_package = load_pickle_file("svm2.pkl")
if svm_package is not None:
svm_binary_model = svm_package["model"]
else:
svm_binary_model = None
svm_scaler_dict = load_pickle_file("svmScaler.pkl")
svm_scaler = svm_scaler_dict.get('scaler', svm_scaler_dict.get('model'))
knn_model_binary = load_pickle_file('knn_final_model.pkl')
knn_multi = load_pickle_file("knn_hog_pca.pkl")
gnb_multimode_model = load_pickle_file('gnbMulti.pkl')
if gnb_multimode_model is not None:
gnb_multimode_model = gnb_multimode_model["model"]
dt_multimode_model = load_pickle_file('dt_Multi.pkl')
if dt_multimode_model is not None:
dt_multimode_model = dt_multimode_model['model']
svm_multimode_model = load_pickle_file('svm_hog.pkl')
svm_multimode_scaler = load_pickle_file('scaler_hog.pkl')
logreg_multimode_model = load_pickle_file('multiLog2_hog.pkl')
def preprocess_image(image, model_choice, mode_choice):
#image = cv2.resize(image, (28, 28))
image = np.array(image)
image = image / 255.0
image_hog = hog(
image,
**hog_params
)
if mode_choice == "Binary":
if model_choice == "GNB" or model_choice == "KNN":
image_hog = image_hog.reshape(1, -1)
image_pca = pca.transform(image_hog)
return image_pca
elif model_choice == "SVM":
image_r = image.reshape(1,-1)
scaled_img = svm_scaler.transform(image_r)
scaled_img = scaled_img.reshape(28,28)
svm_img_hog = hog(scaled_img, **hog_params)
return svm_img_hog.reshape(1, -1)
else:
image_hog = image_hog.reshape(1, -1)
return image_hog
else:
if model_choice == "GNB":
image_hog = image_hog.reshape(1, -1)
image_pca = pca_multi.transform(image_hog)
return image_pca
elif model_choice == "DT" or model_choice == "Logistic Regression" or model_choice == "SVM":
image_hog = image_hog.reshape(1, -1)
return image_hog
elif model_choice == "KNN":
image_hog = image_hog.reshape(1, -1)
image_pca = pca_multi.transform(image_hog)
return image_pca
def classify_binary_gnb(image):
processed_image = preprocess_image(image, "GNB", "Binary")
prediction = gnb_binary_model.predict(
processed_image,
class_weights={0: 1.0, 1: best_weight}
)
if prediction[0] == 1:
return "GNB Binary: Is 6"
else:
return "GNB Binary: Not 6"
def classify_binary_dt(image):
processed_image = preprocess_image(image, "DT", "Binary")
prediction = dt_binary_model.predict(processed_image)
if prediction[0] == 1:
return "Decision Tree Binary: Is 6"
else:
return "Decision Tree Binary: Not 6"
def classify_binary_svm(image):
processed_image = preprocess_image(image, "SVM", "Binary")
#scaled = svm_scaler.transform(processed_image)
prediction = svm_binary_model.predict(processed_image)
if prediction[0] == 1:
return "SVM Binary: Is 6"
else:
return "SVM Binary: Not 6"
def classify_binary_logreg(image):
processed_image = preprocess_image(image, "Logistic Regression", "Binary")
prediction = logreg_binary_model.predict(processed_image)
if prediction[0] == 1:
return "Logistic Regression Binary: Is 6"
else:
return "Logistic Regression Binary: Not 6"
def classify_knn(image):
if knn_model_binary is None:
return "KNN Binary model placeholder: model is not inserted yet."
processed_image = preprocess_image(image, "KNN", "Binary")
prediction = knn_model_binary.predict(processed_image)
if prediction[0] == 1:
return "KNN Binary: Is 6"
else:
return "KNN Binary: Not 6"
def classify_multimode_gnb(image):
if gnb_multimode_model is None or pca_multi is None:
return "GNB Multimode model could not be loaded. If this is a Git LFS file, run `git lfs pull` first."
processed_image = preprocess_image(image, "GNB", "Multimode")
prediction = gnb_multimode_model.predict(processed_image)
return "GNB Multimode prediction: " + str(prediction[0])
def classify_multimode_dt(image):
if dt_multimode_model is None:
return "Decision Tree Multimode model could not be loaded. If this is a Git LFS file, run `git lfs pull` first."
processed_image = preprocess_image(image, "DT", "Multimode")
prediction = dt_multimode_model.predict(processed_image)
return "Decision Tree Multimode prediction: " + str(prediction[0])
def classify_multimode_svm(image):
if svm_multimode_model is None:
if 'svm_hog.pkl' in loading_errors:
return f"ERROR: {loading_errors['svm_hog.pkl']}"
else:
return "SVM Multimode model placeholder: model is not inserted yet."
processed_image = preprocess_image(image, "SVM", "Multimode")
scaled = svm_multimode_scaler.transform(processed_image)
prediction = svm_multimode_model.predict(scaled)
return "SVM Multimode prediction: " + str(prediction[0])
def classify_multimode_logreg(image):
if logreg_multimode_model is None:
return "Logistic Regression Multimode model placeholder: model is not inserted yet."
processed_image = preprocess_image(image, "Logistic Regression", "Multimode")
image_scaled = svm_multimode_scaler.transform(processed_image)
prediction = logreg_multimode_model.predict(image_scaled)
return "Logistic Regression Multimode prediction: " + str(prediction[0])
def classify_multimode_knn(image):
if knn_multi is None:
return "KNN Multimode model could not be loaded. If this is a Git LFS file, run `git lfs pull` first."
processed_image = preprocess_image(image, "KNN", "Multimode")
prediction = knn_multi.predict(processed_image)
return "KNN Multimode prediction: " + str(prediction[0])
def classify_rf(image):
processed_image = preprocess_image(image , "DT" , "Multimode")
prediction = rf_model.predict(processed_image)
return "RF Prediction: " + str(prediction[0])
def predict_with_selected_model(image, model_choice, mode_choice):
if image is None:
return "Please upload an image."
if mode_choice == "Binary":
if model_choice == "GNB":
return classify_binary_gnb(image)
elif model_choice == "DT":
return classify_binary_dt(image)
elif model_choice == "SVM":
return classify_binary_svm(image)
elif model_choice == "Logistic Regression":
return classify_binary_logreg(image)
elif model_choice == "KNN":
return classify_knn(image)
else:
return "Unknown binary model selected."
elif mode_choice == "Multimode":
if model_choice == "GNB":
return classify_multimode_gnb(image)
elif model_choice == "DT":
return classify_multimode_dt(image)
elif model_choice == "SVM":
return classify_multimode_svm(image)
elif model_choice == "Logistic Regression":
return classify_multimode_logreg(image)
elif model_choice == "KNN":
return classify_multimode_knn(image)
elif model_choice == "Random Forest":
return classify_rf(image)
else:
return "Unknown multimode model selected."
else:
return "Unknown mode selected."
demo = gr.Interface(
fn=predict_with_selected_model,
inputs=[
gr.Image(
image_mode="L",
type="numpy",
label="Upload Digit Image"
),
gr.Radio(
choices=["GNB", "DT", "SVM", "Logistic Regression", "KNN" , "Random Forest"],
value="GNB",
label="Choose Model Type"
),
gr.Radio(
choices=["Binary", "Multimode"],
value="Binary",
label="Choose Classification Mode"
)
],
outputs=gr.Textbox(label="Prediction"),
title="MNIST Classifier",
description="Upload an image, select the model type, choose binary or multimode, and get a prediction."
)
if __name__ == "__main__":
demo.launch()
|