File size: 2,869 Bytes
d32d74c
 
1f5d32e
d32d74c
bc3d6c8
90946bd
e1abfbb
 
 
10a0153
 
 
 
9e1a7b8
bc3d6c8
 
 
 
 
387a331
e1abfbb
 
 
 
 
 
bc3d6c8
e1abfbb
 
 
 
 
 
 
d32d74c
 
 
 
 
 
 
 
9e1a7b8
1f5d32e
d32d74c
 
14a39e9
d32d74c
 
14a39e9
d32d74c
 
14a39e9
d32d74c
 
14a39e9
d32d74c
 
14a39e9
 
fb7ebe0
d32d74c
cf37736
14a39e9
 
4d3a820
14a39e9
 
4d3a820
90946bd
fb7ebe0
 
685fa67
 
90946bd
14a39e9
cf37736
d32d74c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import torch
import torchvision.transforms as transforms
import gradio as gr
from PIL import Image
import onnx

import torch
import onnx

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'

# Load ONNX models
efficientnet_model = onnx.load(onnx_efficientnet_path)
inceptionv3_model = onnx.load(onnx_inception_path)
vggnet_model = onnx.load(onnx_vggnet_path)
resnet_model = onnx.load(onnx_resnet_path)

# Export ONNX models to PyTorch models
pytorch_efficientnet_model = torch.onnx.export(efficientnet_model, torch.randn(1, 3, 150, 150), verbose=True)
pytorch_inceptionv3_model = torch.onnx.export(inceptionv3_model, torch.randn(1, 3, 150, 150), verbose=True)
pytorch_vggnet_model = torch.onnx.export(vggnet_model, torch.randn(1, 3, 150, 150), verbose=True)
pytorch_resnet_model = torch.onnx.export(resnet_model, torch.randn(1, 3, 150, 150), verbose=True)

# Load PyTorch models
pytorch_efficientnet_model = torch.jit.load(pytorch_efficientnet_model)
pytorch_inceptionv3_model = torch.jit.load(pytorch_inceptionv3_model)
pytorch_vggnet_model = torch.jit.load(pytorch_vggnet_model)
pytorch_resnet_model = torch.jit.load(pytorch_resnet_model)



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 = 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 name
    class_name = class_names[class_idx]

    # 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 selection
iface = gr.Interface(
    fn=classify_image,
    inputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],
    outputs=["json"],
    live=True
).launch(share=True)