Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950511 unmodified line53545556575859601 unmodified line626364656667686970import numpy as npimport osimport cv2import gradio as grimport onnximport onnxruntime as ortimport tensorflow as tfonnx_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'# Load ONNX models#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)class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]def classify_image(img, model_choice):if model_choice == "ResNet":img = tf.image.resize(img, (150, 150))img = resnet_preprocess_input(img)elif model_choice == "EfficientNet":img = tf.image.resize(img, (150, 150))img = efficientnet_preprocess_input(img)elif model_choice == "VGGNet":img = tf.image.resize(img, (150, 150))img = vgg_preprocess_input(img)elif model_choice == "InceptionV3":img = tf.image.resize(img, (150, 150))img = inception_preprocess_input(img)else:return "Invalid model choice"img = tf.expand_dims(img, 0)# Use gr.to_onnx to convert ONNX models to Gradio-compatible formatonnx_models = {"ResNet": gr.to_onnx(onnx_resnet_model),"EfficientNet": gr.to_onnx(onnx_efficientnet_model),"VGGNet": gr.to_onnx(onnx_vggnet_model),"InceptionV3": gr.to_onnx(onnx_inception_model),}onnx_model = onnx_models.get(model_choice)1 unmodified linereturn "Invalid model choice"# Perform inference using the ONNX modelpredictions = gr.run(onnx_model, {"input": img})class_idx = np.argmax(predictions["output"])# Map class index to class nameclass_name = class_names[class_idx]1 unmodified line# Return the predicted class name as a stringreturn f"Predicted class using {model_choice}: {class_name}"image_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")],12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152531 unmodified line55565758596061626364651 unmodified line676869707172737475import torchimport torchvision.transforms as transformsimport gradio as grfrom PIL import Image# 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 imgdef classify_image(img, model_choice):# Convert Gradio Image type to PIL Imagepil_img = Image.fromarray(img.astype('uint8'), 'RGB')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"# Load the ONNX model based on the user's choiceonnx_models = {"ResNet": onnx_resnet_model,"EfficientNet": onnx_efficientnet_model,"VGGNet": onnx_vggnet_model,"InceptionV3": onnx_inception_model,}onnx_model = onnx_models.get(model_choice)1 unmodified linereturn "Invalid model choice"# Perform inference using the ONNX modelsession = torch.jit.prepare(onnx_model)predictions = session.run(img)# Get the predicted class indexclass_idx = torch.argmax(predictions).item()# Map class index to class nameclass_name = class_names[class_idx]1 unmodified line# Return the predicted class name as a stringreturn f"Predicted class using {model_choice}: {class_name}"# Create Gradio interfaceimage_input = gr.Image()iface = gr.Interface(fn=classify_image,inputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],
app.py+38-33