File size: 1,451 Bytes
cde7851
 
 
 
 
 
 
 
 
 
 
 
 
 
6bda815
cde7851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bda815
cde7851
 
 
 
 
 
 
 
 
 
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
import torch
import torchvision.models as models
from PIL import Image
from efficientnet_pytorch import EfficientNet
import torchvision.transforms as transforms
import gradio as gr
from gradio import components
import numpy as np

def predict(image):
    image = Image.fromarray(np.uint8(image)).convert('RGB')

    model = EfficientNet.from_name('efficientnet-b7', num_classes=2)
    model_weights_path = 'efficientnetb7_tyrequality_classifier.pth'
    model.load_state_dict(torch.load(model_weights_path, map_location=torch.device('cpu')))
    model.eval()

    transform = transforms.Compose([
        transforms.Resize(224),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406],
                            [0.229, 0.224, 0.225])
    ])

    input_data = transform(image).unsqueeze(0)

    class_to_label = {0: 'defective', 1: 'good'}

    with torch.no_grad():
        output = model(input_data)

    # Get the predicted class label
    _, predicted_class = torch.max(output, 1)

    probs = torch.nn.functional.softmax(output, dim=1)
    # print(probs, "probs")
    conf, _ = torch.max(probs, 1)

    result = "Tire status is {} with confidence level in {}%".format(class_to_label[predicted_class.item()], conf.item()*100)
    return result

iface = gr.Interface(fn=predict,
                     inputs=gr.Image(),
                     outputs="textbox")

iface.launch(share=True)