File size: 3,908 Bytes
22cb36f
 
 
 
cf2fa8a
22cb36f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
647af28
22cb36f
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
#Web application (sample codes from student)
import matplotlib as mpl
import matplotlib.pyplot as plt
import gradio as gr
import numpy as np
import pickle
import tensorflow as tf
from tensorflow import keras
from sklearn.neighbors import KNeighborsClassifier

#Step 1
fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()

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


# recommended way to save data into image
from PIL import Image
for i in range(6):
  idx = np.random.randint(0,len(X_train_full))
  some_image = X_train_full[idx] # select one image sample
  some_image_label = class_names[y_train_full[idx]] # select one image sample
  some_image = some_image.reshape(28, 28) # reshape from rank-1 tensor (784,) to rank-2 tensor (28,28) 
  im = Image.fromarray(some_image)
  im.save('image'+str(i)+'.jpg')

input_module1 = gr.inputs.Image(label = "test_image", image_mode='L', shape = (28,28))

input_module2 = gr.inputs.Dropdown(choices=["Random Forest", "Decision Tree", "AdaBoost", "Gradient Tree Boosting", "KNN"], label = "Select Algorithm")

output_module1 = gr.outputs.Textbox(label = "Predicted Class")

output_module2 = gr.outputs.Label(label = "Predict Probability")


def fashion_images(input1, input2):
    image = input1.reshape(1, 28*28)/255.0

    import pickle
    with open('knn_model_best.pkl', 'rb') as file:
        best_knn_model = pickle.load(file)

    with open('gradientboost_model_best.pkl', 'rb') as file:
        best_gbt_model = pickle.load(file)

    with open('adaboost_model_best.pkl', 'rb') as file:
        best_adaboost_model = pickle.load(file)

    with open('decision_tree_model_best.pkl', 'rb') as file:
        best_tree_model = pickle.load(file)
        
    with open('random_forest_model_best.pkl', 'rb') as file:
        best_RF_model = pickle.load(file)


    if input2 == 'Random Forest':
        y_test_predicted_proba = best_RF_model.predict_proba(image)
        y_test_predicted_label = best_RF_model.predict(image)
        output = class_names[y_test_predicted_label[0]]

    elif input2 == 'Gradient Tree Boosting':
        y_test_predicted_proba = best_gbt_model.predict_proba(image)
        y_test_predicted_label = best_gbt_model.predict(image)
        output = class_names[y_test_predicted_label[0]]
        
    elif input2 == 'AdaBoost':
        y_test_predicted_proba = best_adaboost_model.predict_proba(image)
        y_test_predicted_label = best_adaboost_model.predict(image)
        output = class_names[y_test_predicted_label[0]]
        
    elif input2 == 'Decision tree':
        y_test_predicted_proba = best_tree_model.predict_proba(image)
        y_test_predicted_label = best_tree_model.predict(image)
        output = class_names[y_test_predicted_label[0]]
        
    elif input2 == 'Random Forest':
        y_test_predicted_proba = best_RF_model.predict_proba(image)
        y_test_predicted_label = best_RF_model.predict(image)
        output = class_names[y_test_predicted_label[0]]

    elif input2 == 'KNN': 
        y_test_predicted_proba = best_knn_model.predict_proba(image)
        y_test_predicted_label = best_knn_model.predict(image)
        output = class_names[y_test_predicted_label[0]]
    
    output_prob = {}
    for name, prob in zip(class_names, y_test_predicted_proba[0]):
        output_prob[name] = prob

    return output, output_prob

# Step 6.4: Put all three component together into the gradio's interface function 
interface = gr.Interface(fn=fashion_images, 
             inputs=[input_module1, input_module2], 
             outputs=[output_module1,output_module2],
             examples=[["image0.jpg", "Random Forest"], 
                       ["image1.jpg", "Decision tree"], 
                       ["image2.jpg", "KNN"]])
interface.launch()