Spaces:
Runtime error
Runtime error
File size: 1,398 Bytes
b8c40ee 224c243 36e402d b8c40ee 5cc607b b8c40ee 36e402d b8c40ee 7da5886 b8c40ee 224c243 b8c40ee 13f8e7b b8c40ee 224c243 b8c40ee 7da5886 b8c40ee 7da5886 b8c40ee 7da5886 b8c40ee 7da5886 b8c40ee 7da5886 b8c40ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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)
|