File size: 5,993 Bytes
3c10631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae4112
 
 
 
 
 
3c10631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import torch
import torch.nn.functional as F
from PIL import Image
import pytorch_lightning as pl
import torch.nn as nn
from torchvision import transforms as T
from torchvision import models
import matplotlib.pyplot as plt
import onnxruntime as ort
from glob import glob
import streamlit as st
import numpy as np
from torchmetrics.functional import accuracy
from torchmetrics import Accuracy

#Define the labels
labels = ['Defect', 'Non-Defect']

# Define the sample images
sample_images = {
    "Defect01": "pics/Defect/c1r1e1n24.tif",
    "Defect02": "pics/Defect/c1r1e1n35.tif",
    "Defect03": "pics/Defect/c1r1e2n44.tif",
    "Non-Defect01": "pics/nDefect/c1r1e0n15.tif",
    "Non-Defect02": "pics/nDefect/c1r1e0n25.tif",
    "Non-Defect03": "pics/nDefect/c1r1e0n38.tif"
}

class DefectResNet(pl.LightningModule):
    def __init__(self, n_classes=2):
        super(DefectResNet, self).__init__()
        
        # จำนวนของพันธุ์output (2)
        self.n_classes = n_classes

        #เปลี่ยน layer สุดท้าย
        self.backbone = models.resnet50(pretrained=True)
        # self.backbone = models.resnet152(pretrained=True)
        # self.backbone = models.vgg19(pretrained=True)
        for param in self.backbone.parameters():
            param.requires_grad = False

        # เปลี่ยน fc layer เป็น output ขนาด 2
        self.backbone.fc = torch.nn.Linear(self.backbone.fc.in_features, n_classes) #For ResNet base mdoel
        # self.backbone.classifier[6] = torch.nn.Linear(self.backbone.classifier[6].in_features, n_classes) #For VGG bse model
        
        self.entropy_loss = nn.CrossEntropyLoss()
        self.accuracy = Accuracy(task="multiclass", num_classes=2)

        self.save_hyperparameters(logger=False)

    def forward(self, x):
        preds = self.backbone(x)
        return preds

    def training_step(self, batch, batch_idx):
        x, y = batch
        logits = self.backbone(x)
        loss = self.entropy_loss(logits, y)
        y_pred = torch.argmax(logits, dim=1)
        self.log("train_loss", loss)
        self.log("train_acc", self.accuracy(y_pred, y))
        return loss

    def validation_step(self, batch, batch_idx):
        x, y = batch
        logits = self.backbone(x)
        loss = self.entropy_loss(logits, y)
        y_pred = torch.argmax(logits, dim=1)
        self.log("val_loss", loss)
        self.log("val_acc", self.accuracy(y_pred, y))
        return loss
        
    def configure_optimizers(self):
        self.optimizer = torch.optim.AdamW(self.parameters(), lr=1e-3)
        return {
            "optimizer": self.optimizer,
            "monitor": "val_loss",
        }

    def test_step(self, batch, batch_idx):
        x, y = batch
        logits = self.backbone(x)
        loss = self.entropy_loss(logits, y)
        y_pred = torch.argmax(logits, dim=1)
        self.log("val_loss", loss)
        self.log("val_acc", self.accuracy(y_pred, y))
        return loss
    
    def _shared_eval_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self.model(x)
        logits = self.backbone(x)
        loss = self.entropy_loss(logits, y)
        acc = accuracy(y_hat, y)
        return loss, acc

# Load the model on the appropriate device
loadmodel = DefectResNet()
def load_checkpoint(checkpoint):
    loadmodel.load_state_dict(checkpoint["state_dict"])
load_checkpoint(torch.load("models/model.ckpt", map_location=torch.device('cpu')))
loadmodel.eval()

transform = T.Compose([
    T.Resize((224, 224)),
    T.ToTensor()
])

def predict(image):
    image = transform(image).unsqueeze(0)

    # Perform the prediction
    with torch.no_grad():
        logits = loadmodel(image)
        probs = F.softmax(logits, dim=1)
        return probs

# Define the Streamlit app
def app():
    predictions = None
    st.title("Textile Defect Detection")
    uploaded_file = st.file_uploader("Upload your image...", type=["jpg"])

    with st.expander("Or choose from sample here..."):
        sample = st.selectbox(label = "Select here", options = list(sample_images.keys()), label_visibility="hidden")   
        col1, col2, col3 = st.columns(3)
        with col1:
            st.image(sample_images["Defect01"], caption="Defect01", use_column_width=True)
        with col2:
            st.image(sample_images["Defect02"], caption="Defect02", use_column_width=True)
        with col3:
            st.image(sample_images["Defect03"], caption="Defect03", use_column_width=True)
        col1, col2, col3 = st.columns(3)
        with col1:
            st.image(sample_images["Non-Defect01"], caption="Non-Defect01", use_column_width=True)
        with col2:
            st.image(sample_images["Non-Defect02"], caption="Non-Defect02", use_column_width=True)
        with col3:
            st.image(sample_images["Non-Defect03"], caption="Non-Defect03", use_column_width=True)

    # If an image is uploaded, make a prediction on it
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image", use_column_width=True)
        predictions = predict(image)
    elif sample:    
        image = Image.open(sample_images[sample])
        st.image(image, caption=sample.capitalize() + " Image", use_column_width=True)
        predictions = predict(image)
      
    # Show  predictions with their probabilities
    if predictions is not  None:
        # st.write(predictions)
        st.subheader(f'Predictions : {labels[torch.argmax(predictions[0]).item()]}')
        for pred, prob in zip(labels, predictions[0]):
            st.write(f"{pred}: {prob * 100:.2f}%")
            st.progress(prob.item())
    else:
        st.write("No predictions.")
    st.subheader("Credits")
    st.write("By : Keyush | Tejas | Jenil")
    st.markdown("Source : [Hugging Face](https://huggingface.co/shahkeyush2002/Textile-Defect-Detection)")

# Run the app
if __name__ == "__main__":
    app()