Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234512378910451213141516678918101112131415202122232425161718192021272829222324313225262728293031323334353637383940343536424344373839404142434445464748464748494950515253545556575152535954555657585964656061626763646566import numpy as npimport osimport cv2import gradio as grimport onnxruntime as ort# -*- coding: utf-8 -*-"""gradio interface.ipynb
onnx_efficientnet_path = './Models_onnx/efficientnet.onnx'#onnx_inception_path = './Models_onnx/inceptionv3.onnx'onnx_vggnet_path = './Models_onnx/vgg_16.onnx'onnx_resnet_path = './Models_onnx/resnet50.onnx'Automatically generated by Colaboratory.
# Uncomment or remove the following line based on your needs# onnx_inception_model = ort.InferenceSession(onnx_inception_path)onnx_vggnet_model = ort.InferenceSession(onnx_vggnet_path)onnx_resnet_model = ort.InferenceSession(onnx_resnet_path)onnx_efficientnet_model = ort.InferenceSession(onnx_efficientnet_path)Original file is located at https://colab.research.google.com/drive/1AxnKS2DJb_EJ2GlZuu7YZekaD0PXQN8O"""
class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]import gradio as grfrom PIL import Imageimport torchfrom torchvision import transformsfrom transformers import AutoModelForImageClassification, AutoTokenizer
def classify_image(img, model_choice): def preprocess_input(img, model): # Placeholder for preprocessing steps # Replace with actual preprocessing steps based on the model processed_img = img / 255.0 return processed_img# Load models from Hugging Face model hubefficientnet_model = AutoModelForImageClassification.from_pretrained("trs/EfficientNet")resnet_model = AutoModelForImageClassification.from_pretrained("trs/ResNet")vggnet_model = AutoModelForImageClassification.from_pretrained("trs/VGGNet")inceptionv3_model = AutoModelForImageClassification.from_pretrained("trs/InceptionV3")
# Keep the input size as (150, 150) since your model expects that size input_size = (150, 150) img_resized = cv2.resize(img, input_size)# Define class names corresponding to your datasetclass_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"] # Replace with your actual class names
# Preprocess the input image img_preprocessed = preprocess_input(img_resized, model_choice)# Define the classify_image functiondef classify_image(img, model_choice): # Resize and preprocess the input image preprocess = transforms.Compose([ transforms.Resize((150, 150)), transforms.ToTensor(), ]) img = preprocess(Image.fromarray(img))
# Run inference based on the selected model model = { "ResNet": onnx_resnet_model, "EfficientNet": onnx_efficientnet_model, "VGGNet": onnx_vggnet_model, #"InceptionV3": onnx_inception_model # Uncomment or remove based on your needs }[model_choice] # Add batch dimension img = img.unsqueeze(0)
predictions = model.run(None, {"input": img_preprocessed}) class_idx = np.argmax(predictions) class_name = class_names[class_idx] # Make predictions based on the selected model if model_choice == "ResNet": predictions = resnet_model(img) elif model_choice == "EfficientNet": predictions = efficientnet_model(img) elif model_choice == "VGGNet": predictions = vggnet_model(img) elif model_choice == "InceptionV3": predictions = inceptionv3_model(img) else: return "Invalid model choice"
# Get the top 3 predictions top_predictions = sorted(enumerate(predictions[0]), key=lambda x: x[1], reverse=True)[:3] top_classes = [class_names[idx] for idx, _ in top_predictions] top_probs = [round(prob * 100, 2) for _, prob in top_predictions] class_idx = torch.argmax(predictions[0])
return { "Original Image": img, "Resized Image": img_resized, "Predicted class using {}: {}".format(model_choice, class_name), "Top 3 Predictions": list(zip(top_classes, top_probs)), "Confidence Scores": predictions[0] } # Map class index to class name class_name = class_names[class_idx]
image_input = gr.Image() # Return the predicted class name as a string return f"Predicted class using {model_choice}: {class_name}"
# Create a Gradio interface with a dropdown for model selectioniface = gr.Interface( fn=classify_image, inputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")], outputs=["image", "image", "text", "text", "text"], inputs=["image", gr.Image()], outputs="text", live=True).launch(debug=True))
# Launch the Gradio interfaceiface.launch(share=True)app.py+50-51