Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556571 unmodified line5960616263646566672 unmodified lines70717273import numpy as npimport osimport cv2import gradio as grimport 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'# Uncomment or remove the following line based on your needsonnx_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)) # ResNet50 input sizeimg = image.img_to_array(img)img = img.copy() # Make a copy of the image arrayimg = resnet_preprocess_input(img)elif model_choice == "EfficientNet":img = tf.image.resize(img, (150, 150))img = image.img_to_array(img)img = preprocess_input(img)elif model_choice == "VGGNet":img = tf.image.resize(img, (150, 150))img = image.img_to_array(img)img = preprocess_input(img)elif model_choice == "InceptionV3":img = tf.image.resize(img, (150, 150))img = image.img_to_array(img)img = preprocess_input(img)else:return "Invalid model choice"img = tf.expand_dims(img, 0) # Add batch dimension# Make predictions based on the selected modelif model_choice == "ResNet":predictions = onnx_resnet_model.predict(img)elif model_choice == "EfficientNet":predictions = onnx_efficientnet_model.predict(img)elif model_choice == "VGGNet":predictions = onnx_vggnet_model.predict(img)elif model_choice == "InceptionV3":predictions = onnx_inception_model.predict(img)class_idx = tf.argmax(predictions[0])# 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 selection2 unmodified linesinputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],outputs=["json"],live=True).launch(share=True)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647481 unmodified line5051525354552 unmodified lines58596061import torchimport torchvision.transforms as transformsimport gradio as grfrom PIL import Image# Assuming you have the PyTorch models saved in the specified pathspytorch_efficientnet_path = './Models_pytorch/efficientnet.pth'pytorch_inception_path = './Models_pytorch/inceptionv3.pth'pytorch_vggnet_path = './Models_pytorch/vgg_16.pth'pytorch_resnet_path = './Models_pytorch/resnet50.pth'# Load PyTorch modelspytorch_efficientnet_model = torch.load(pytorch_efficientnet_path)pytorch_inception_model = torch.load(pytorch_inception_path)pytorch_vggnet_model = torch.load(pytorch_vggnet_path)pytorch_resnet_model = torch.load(pytorch_resnet_path)class_names = ["Ajloun Castle", "Jerash", "Petra", "Roman amphitheater", "Umm Qais", "Wadi Rum"]def preprocess_image_pil(img):img = transforms.Resize((150, 150))(img)img = transforms.ToTensor()(img)img = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(img)img = img.unsqueeze(0) # Add batch dimensionreturn imgdef classify_image(img, model_choice):img = Image.fromarray(img) # Convert to PIL Imageif model_choice == "ResNet":img = preprocess_image_pil(img)predictions = pytorch_resnet_model(img)elif model_choice == "EfficientNet":img = preprocess_image_pil(img)predictions = pytorch_efficientnet_model(img)elif model_choice == "VGGNet":img = preprocess_image_pil(img)predictions = pytorch_vggnet_model(img)elif model_choice == "InceptionV3":img = preprocess_image_pil(img)predictions = pytorch_inception_model(img)else:return "Invalid model choice"class_idx = torch.argmax(predictions[0])# 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 selection2 unmodified linesinputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],outputs=["json"],live=True).launch(share=True)
app.py+33-45