AdityaPatwa commited on
Commit
9191424
·
verified ·
1 Parent(s): 837f5fa

Upload 4 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ models/solar_panel_defect_model.onnx.data filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import onnxruntime as ort
2
+ import numpy as np
3
+ from PIL import Image
4
+ from torchvision import transforms
5
+ import gradio as gr
6
+
7
+
8
+ # Path to ONNX model
9
+ ONNX_PATH = "models/solar_panel_defect_model.onnx"
10
+
11
+ # Load ONNX runtime session
12
+ ort_session = ort.InferenceSession(ONNX_PATH, providers=["CPUExecutionProvider"])
13
+
14
+ print("✅ ONNX model loaded")
15
+
16
+ # Class labels (VERY IMPORTANT: order must match training)
17
+ CLASS_NAMES = [
18
+ 'Bird-drop',
19
+ 'Clean',
20
+ 'Dusty',
21
+ 'Electrical-damage',
22
+ 'Physical-Damage',
23
+ 'Snow-Covered'
24
+ ]
25
+
26
+ # Same preprocessing as training
27
+ IMG_SIZE = 224
28
+
29
+ preprocess = transforms.Compose([
30
+ transforms.Resize((IMG_SIZE, IMG_SIZE)),
31
+ transforms.Grayscale(num_output_channels=3),
32
+ transforms.ToTensor(),
33
+ transforms.Normalize(
34
+ mean=[0.485, 0.456, 0.406],
35
+ std=[0.229, 0.224, 0.225]
36
+ )
37
+ ])
38
+
39
+ def predict_image(pil_image):
40
+ """
41
+ Input: PIL Image
42
+ Output: (predicted_class, confidence, full_probs_dict)
43
+ """
44
+ img = preprocess(pil_image).unsqueeze(0).numpy().astype(np.float32)
45
+
46
+ # ONNX inference
47
+ outputs = ort_session.run(
48
+ None,
49
+ {"input_image": img}
50
+ )[0]
51
+
52
+ # Softmax
53
+ exp_scores = np.exp(outputs)
54
+ probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
55
+
56
+ probs = probs[0]
57
+ pred_idx = int(np.argmax(probs))
58
+
59
+ predicted_class = CLASS_NAMES[pred_idx]
60
+ confidence = float(probs[pred_idx])
61
+
62
+ prob_dict = {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}
63
+
64
+ return predicted_class, confidence, prob_dict
65
+
66
+
67
+ print("✅ Inference pipeline ready")
68
+
69
+
70
+ # STEP G3: Gradio Interface
71
+
72
+ def gradio_predict(image):
73
+ pred_class, confidence, prob_dict = predict_image(image)
74
+
75
+ confidence_percent = f"{confidence * 100:.2f}%"
76
+
77
+ return pred_class, confidence_percent, prob_dict
78
+
79
+
80
+ iface = gr.Interface(
81
+ fn=gradio_predict,
82
+ inputs=gr.Image(type="pil", label="Upload Solar Panel Image (Thermal / IR / RGB)"),
83
+ outputs=[
84
+ gr.Textbox(label="Predicted Defect Class"),
85
+ gr.Textbox(label="Confidence"),
86
+ gr.Label(label="All Class Probabilities")
87
+ ],
88
+ title="AI-Powered Solar Panel Defect Detection",
89
+ description=(
90
+ "Upload any solar panel image (thermal, infrared, or RGB). "
91
+ "The AI model classifies defects such as soiling, electrical damage, "
92
+ "physical damage, snow coverage, or clean panels."
93
+ ),
94
+ examples=None
95
+ )
96
+
97
+ iface.launch(debug=True)
models/solar_panel_defect_model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ab00b00767429a687a12fbf9b41ada718c8a3457313c9f788c7e437126981187
3
+ size 603578
models/solar_panel_defect_model.onnx.data ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:76814cf9528fda3433229d1ed5422635ce29e15ae19c14db49eefd70596fe0f5
3
+ size 16056320
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ onnxruntime
2
+ torch
3
+ torchvision
4
+ gradio
5
+ Pillow
6
+ numpy
7
+ torchaudio