Spaces:
Runtime error
Runtime error
Download app.py from trs/Image_Classification_with_pre-traind_Models: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/5cc607bcc106192190441660fc4e2de5de7154a4/app.py
- Command line
-
hf download hf://spaces/trs/Image_Classification_with_pre-traind_Models@5cc607bcc106192190441660fc4e2de5de7154a4/app.py
-
curl -L -o app.py https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/5cc607bcc106192190441660fc4e2de5de7154a4/app.py
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) | |