Sreenivas98/FashionMIST_Classification
1
1import gradio as gr2import pickle3from sklearn.neighbors import KNeighborsClassifier4from sklearn.linear_model import LogisticRegression5import sklearn6import tensorflow7from tensorflow import keras8from keras.models import Sequential9from keras.layers import Dense10from tensorflow.keras.models import load_model11 12input_1 = gr.Image(shape=(28,28),image_mode='L')13 14input_2 = gr.Dropdown(["KNN", "SoftMax", "NeuralNetwork_Shallow", "NeuralNetwork_Deep"])15 16output = gr.Label(num_top_classes=9)17 18class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",19"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]20 21def predict_knn(test_img):22 Knn_model = pickle.load(open('knn_model.pkl', 'rb'))23 predictions = Knn_model.predict_proba(test_img)24 print(predictions)25 return {class_names[i]: float(predictions[0][i]) for i in range(0,10)}26 27def predict_softmax(test_img):28 Softmax_model = pickle.load(open('softmax_model.pkl', 'rb'))29 predictions = Softmax_model.predict_proba(test_img)30 return {class_names[i]: float(predictions[0][i]) for i in range(0,10)}31 32def predict_neural(test_img):33 Neural_model = load_model("neural_model.h5")34 predictions = Neural_model.predict(test_img)35 return {class_names[i]: float(predictions[0][i]) for i in range(0,10)}36 37def predict_deep_neural(test_img):38 DeepNeural_model = load_model("deep_neural_model.h5")39 predictions = DeepNeural_model.predict(test_img)40 return {class_names[i]: float(predictions[0][i]) for i in range(0,10)}41 42def predictFashionClass(test_img,chosen_model):43 test_img_flatten=test_img.reshape(-1,28*28)44 if chosen_model == "KNN":45 fashionProbs = predict_knn(test_img_flatten)46 return fashionProbs47 elif chosen_model == "SoftMax":48 fashionProbs = predict_softmax(test_img_flatten)49 return fashionProbs50 51 elif chosen_model == "NeuralNetwork_Shallow":52 fashionProbs = predict_neural(test_img_flatten)53 return fashionProbs 54 55 elif chosen_model == "NeuralNetwork_Deep":56 fashionProbs = predict_deep_neural(test_img_flatten)57 return fashionProbs58 59gr.Interface(fn=predictFashionClass,inputs=[input_1,input_2],outputs=output).launch(debug=True)