Spaces:
Runtime error
Runtime error
Update app.py
Browse files1231235645891011678910111314151617121314151617191819202122232425262728293023242526313233282930343536323334373839363738404142434442434445464748495051525354454647481 unmodified line5051526263645354552 unmodified lines5859607361import numpy as npimport osimport cv2import torchimport torchvision.transforms as transformsimport gradio as grimport onnxruntime as ortimport tensorflow as tffrom PIL import Image
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'# 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'
# 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)# 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", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]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 dimension return img
def classify_image(img, model_choice): img = Image.fromarray(img) # Convert to PIL Image
if model_choice == "ResNet": img = tf.image.resize(img, (150, 150)) # ResNet50 input size img = image.img_to_array(img) img = img.copy() # Make a copy of the image array img = resnet_preprocess_input(img) img = preprocess_image_pil(img) predictions = pytorch_resnet_model(img) elif model_choice == "EfficientNet": img = tf.image.resize(img, (150, 150)) img = image.img_to_array(img) img = preprocess_input(img) img = preprocess_image_pil(img) predictions = pytorch_efficientnet_model(img) elif model_choice == "VGGNet": img = tf.image.resize(img, (150, 150)) img = image.img_to_array(img) img = preprocess_input(img) img = preprocess_image_pil(img) predictions = pytorch_vggnet_model(img) elif model_choice == "InceptionV3": img = tf.image.resize(img, (150, 150)) img = image.img_to_array(img) img = preprocess_input(img) img = preprocess_image_pil(img) predictions = pytorch_inception_model(img) else: return "Invalid model choice"
img = tf.expand_dims(img, 0) # Add batch dimension
# Make predictions based on the selected model if 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]) class_idx = torch.argmax(predictions[0])
# Map class index to class name class_name = class_names[class_idx]1 unmodified line # Return the predicted class name as a string return f"Predicted class using {model_choice}: {class_name}"
image_input = gr.Image()
# Create a Gradio interface with a dropdown for model selection2 unmodified lines inputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")], outputs=["json"], live=True).launch(share=True)).launch(share=True)app.py+33-45