File size: 2,041 Bytes
d48a2df
 
 
 
 
 
 
 
 
 
 
 
8e56f6b
d48a2df
 
 
 
 
 
 
 
 
 
 
 
 
 
615bb38
0985b5f
d48a2df
b1b90d7
615bb38
d48a2df
 
 
 
8fa3fab
d48a2df
 
 
 
 
 
 
 
 
 
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
import pickle
import numpy as np
import os
os.system("pip install scikit-learn")
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
os.system("pip install gradio")
import gradio as gr

class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]

def fashion_images(image, model):
    print("image: ",image.shape)
    numpy_image = image.reshape(1, 28*28)
    def select_model(model):
      match (model):
        case 'Softmax Regression':
          return pickle.load(open('random_forest_model_best.pkl', 'rb'))
        case 'Neural Network (sklearn) 1':
          return pickle.load(open('random_forest_model_best.pkl', 'rb'))
        case 'Neural Network (sklearn) 2':
          return pickle.load(open('random_forest_model_best.pkl', 'rb'))
        case 'Neural Network (keras) three-layer':
          return pickle.load(open('keras_sequential', 'rb'))
        case _:
          raise BaseException('Model not valid. Please pick a valid model.')
    user_model = select_model(model)
    predicted_class = user_model.predict(numpy_image)[0]
    print("predicted_class: ",predicted_class)
    predicted_class = class_names[predicted_class]
    predicted_proba = user_model.predict_proba(numpy_image)[0]
    print("predicted_proba: ",predicted_proba)
    predicted_proba = {class_names: float(prob) for class_names,prob in zip(class_names, predicted_proba) }
    return predicted_class, predicted_proba


input_module1 = gr.Image(label = "test_image", image_mode='L', shape=(28, 28))
input_module2 = gr.Dropdown(choices=['Softmax Regression', 'Neural Network (sklearn) 1', 'Neural Network (sklearn) 2','Neural Network (keras) three-layer'], label = "Select Algorithm")

output_module1 = gr.Textbox(label = "Predicted Class")
output_module2 = gr.Label(label = "Predict Probability")

gr.Interface(
    fn=fashion_images,
    inputs=[input_module1, input_module2],
    outputs=[output_module1, output_module2]
).launch(debug=True)