Spaces:
Runtime error
Runtime error
Update app.py
Browse files1212344566789101112131415161718192021222324252627121314151617181931322021222324253435363738394041424326272829454630313233343536375051525354553839405758596061626364414243446745467071474849505152import torchimport torchvision.transforms as transformsimport numpy as npimport osimport cv2import gradio as grfrom PIL import Imageimport onnxruntime as ort
# Load the ONNX modelsonnx_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'
onnx_efficientnet_model = torch.onnx.load(onnx_efficientnet_path)onnx_inception_model = torch.onnx.load(onnx_inception_path)onnx_vggnet_model = torch.onnx.load(onnx_vggnet_path)onnx_resnet_model = torch.onnx.load(onnx_resnet_path)
# Define the class namesclass_names = ["Ajloun Castle", "Jerash", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]
def preprocess_input_pil(img): transform = transforms.Compose([ transforms.Resize((150, 150)), transforms.ToTensor(), ]) img = transform(img).unsqueeze(0) return imgonnx_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)
class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]
def classify_image(img, model_choice): # Convert Gradio Image type to PIL Image pil_img = Image.fromarray(img.astype('uint8'), 'RGB') 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
if model_choice == "ResNet": img = preprocess_input_pil(pil_img) elif model_choice == "EfficientNet": img = preprocess_input_pil(pil_img) elif model_choice == "VGGNet": img = preprocess_input_pil(pil_img) elif model_choice == "InceptionV3": img = preprocess_input_pil(pil_img) else: return "Invalid model choice" # Resize the input image based on the selected model's expected input shape input_size = (224, 224) # Adjust according to the model's input size img = cv2.resize(img, input_size)
# Load the ONNX model based on the user's choice onnx_models = { # Preprocess the input image img = preprocess_input(img, model_choice)
# Run inference based on the selected model model = { "ResNet": onnx_resnet_model, "EfficientNet": onnx_efficientnet_model, "VGGNet": onnx_vggnet_model, "InceptionV3": onnx_inception_model, }
onnx_model = onnx_models.get(model_choice) if onnx_model is None: return "Invalid model choice" "InceptionV3": onnx_inception_model }[model_choice]
# Perform inference using the ONNX model session = torch.jit.prepare(onnx_model) predictions = session.run(img)
# Get the predicted class index class_idx = torch.argmax(predictions).item()
# Map class index to class name predictions = model.run(None, {"input": img}) class_idx = np.argmax(predictions) class_name = class_names[class_idx]
# Return the predicted class name as a string return f"Predicted class using {model_choice}: {class_name}"
# Create Gradio interfaceimage_input = gr.Image()
# 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")],app.py+28-51