trs's picture
Update app.py
5cc607b verified
Raw History Blame
1.4 kB
import torch
import torchvision.transforms as transforms
from PIL import Image
import gradio as gr
# Load your PyTorch InceptionV3 model
model_path = "./inceptionv3_-saved-model-59-val_accuracy-0.94.hdf5" # Update with your model path
model = torch.load(model_path, map_location=torch.device('cpu'))
model.eval()
# Load the image transformer
transform = transforms.Compose([
transforms.Resize((150, 150)),
transforms.ToTensor(),
])
# Define class names corresponding to your dataset
class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]
# Define the classify_image function
def classify_image(img):
# Preprocess the image
img = Image.fromarray((img * 255).astype('uint8')) # Assuming img is a NumPy array
img = transform(img)
img = img.unsqueeze(0) # Add batch dimension
# Make predictions using the PyTorch model
with torch.no_grad():
logits = model(img)
# Get predicted class index
class_idx = torch.argmax(logits[0]).item()
# Map class index to class name
class_name = class_names[class_idx]
# Return the predicted class name as a string
return f"Predicted class: {class_name}"
# Create a Gradio interface
iface = gr.Interface(
fn=classify_image,
inputs=["image"],
outputs="text",
live=True
)
# Launch the Gradio interface
iface.launch(share=True,debug=True)