File size: 8,227 Bytes
e38de99 |
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# ViT Model in DCRM Pipeline - Complete Explanation
## What is `vitResult`?
The `vitResult` is the output from a **Vision Transformer (ViT) + Gemini AI Ensemble Model** that analyzes the DCRM resistance plot image to classify circuit breaker defects.
---
## π Complete Flow (Step-by-Step)
### **Step 1: Generate Resistance Plot**
**File**: `core/models/vit_classifier.py` β `plot_resistance_for_vit()`
```python
# Creates a plot with 3 lines:
# - Green line: Resistance profile
# - Blue line: Current profile
# - Red line: Travel profile
# Saves as temporary PNG file: temp_vit_plot_{phase}_{uuid}.png
```
**Example**: `temp_vit_plot_r_a3f8d2b1.png`
---
### **Step 2: ViT Model Analysis** (Remote API)
**File**: `core/models/vit_classifier.py` β `get_remote_vit_probabilities()`
```python
# Sends image to deployed ViT model API
DEPLOYED_VIT_URL = "http://143.110.244.235/predict"
# ViT is trained on DCRM images to detect 5 defect classes:
CLASSES = [
"Healthy",
"Arcing_Contact_Misalignment",
"Arcing_Contact_Wear",
"Main Contact Misalignment",
"main_contact_wear"
]
# Returns probability distribution for each class
vit_probs = {
"Healthy": 0.507,
"Arcing_Contact_Misalignment": 0.120,
"Arcing_Contact_Wear": 0.044,
"Main Contact Misalignment": 0.142,
"main_contact_wear": 0.186
}
```
**How ViT Works**:
- ViT (Vision Transformer) is a deep learning model trained on DCRM plot images
- It learned visual patterns from thousands of circuit breaker test plots
- Analyzes waveform shapes, spikes, plateaus, and transitions
- Outputs probability for each defect type
---
### **Step 3: Gemini AI Analysis**
**File**: `core/models/vit_classifier.py` β `get_gemini_prediction()`
```python
# Sends same image to Google Gemini 2.0 Flash
# Uses expert prompt with diagnostic heuristics:
Diagnostic Rules:
1. "The Significant Grass" β Main Contact Corrosion
- Jagged, irregular resistance plateau (> 15-20ΞΌΞ© variance)
2. "Big Spikes & Short Wipe" β Arcing Contact Wear
- Large amplitude spikes, shortened arcing zone
3. "The Struggle to Settle" β Main Misalignment
- High-amplitude peaks before plateau (> 3-5ms)
4. "Rough Entry" β Arcing Misalignment
- Erratic spikes during initial entry
5. "Stretched Time" β Slow Mechanism
- Elongated resistance profile on X-axis
# Returns probability distribution
gemini_probs = {
"Healthy": 0.05,
"Arcing_Contact_Misalignment": 0.02,
"Arcing_Contact_Wear": 0.01,
"Main Contact Misalignment": 0.02,
"main_contact_wear": 0.90 # High confidence!
}
```
---
### **Step 4: Ensemble Prediction**
**File**: `core/models/vit_classifier.py` β `predict_dcrm_image()`
```python
# Combines ViT + Gemini predictions
# ensemble_score = vit_prob + gemini_prob
ensemble_scores = {
"Healthy": 0.507 + 0.05 = 0.557,
"Arcing_Contact_Misalignment": 0.120 + 0.02 = 0.140,
"Arcing_Contact_Wear": 0.044 + 0.01 = 0.054,
"Main Contact Misalignment": 0.142 + 0.02 = 0.162,
"main_contact_wear": 0.186 + 0.90 = 1.086 # β
HIGHEST!
}
# Selects class with highest ensemble score
predicted_class = "main_contact_wear"
confidence = 0.543 # Normalized confidence
```
---
### **Step 5: Integration into Pipeline**
**File**: `apps/flask_server.py` β `process_single_phase_csv()`
```python
# Lines 155-183
vit_result = None
vit_plot_path = f"temp_vit_plot_{phase_name}_{uuid.uuid4().hex[:8]}.png"
# Generate plot
if plot_resistance_for_vit(df, vit_plot_path):
# Get prediction
vit_class, vit_conf, vit_details = predict_dcrm_image(vit_plot_path, api_key=api_key)
vit_result = {
"class": vit_class, # "main_contact_wear"
"confidence": vit_conf, # 0.5429375439882278
"details": vit_details # Full breakdown below
}
# Cleanup temp file
os.remove(vit_plot_path)
```
---
## π¦ vitResult Structure Breakdown
```json
{
"class": "main_contact_wear", // β
FINAL PREDICTION
"confidence": 0.5429375439882278, // β
NORMALIZED CONFIDENCE
"details": {
"vit_probs": { // π€ Vision Transformer probabilities
"Healthy": 0.5076556205749512,
"Arcing_Contact_Misalignment": 0.12034504860639572,
"Arcing_Contact_Wear": 0.04370640590786934,
"Main Contact Misalignment": 0.1424178034067154,
"main_contact_wear": 0.1858750879764557
},
"gemini_probs": { // π§ Gemini AI probabilities
"Healthy": 0.05,
"Arcing_Contact_Misalignment": 0.02,
"Arcing_Contact_Wear": 0.01,
"Main Contact Misalignment": 0.02,
"main_contact_wear": 0.9 // Gemini is very confident!
},
"ensemble_scores": { // π― COMBINED SCORES
"Healthy": 0.5576556205749512,
"Arcing_Contact_Misalignment": 0.1403450486063957,
"Arcing_Contact_Wear": 0.05370640590786934,
"Main Contact Misalignment": 0.16241780340671538,
"main_contact_wear": 1.0858750879764556 // β
HIGHEST β WINNER
}
}
}
```
---
## π Why Two Models?
| Model | Strengths | Weaknesses |
|-------|-----------|------------|
| **ViT** | - Trained on real DCRM data<br>- Fast inference<br>- Consistent | - May overfit to training data<br>- Limited to visual patterns |
| **Gemini** | - Expert reasoning<br>- Contextual understanding<br>- Adapts to new cases | - May hallucinate<br>- Slower<br>- Requires API calls |
| **Ensemble** | β
**Best of both worlds**<br>- ViT provides baseline<br>- Gemini adds expertise | - Slightly higher computational cost |
---
## π― How It's Used in the Pipeline
The `vitResult` is:
1. **Generated** in `flask_server.py` (lines 155-183)
2. **Passed to** `report_generator.py`
3. **Included in** final JSON output under each phase (r, y, b)
4. **Referenced** in fault summaries for LLM context
**Example Usage**:
```python
# In report_generator.py
if vit_result:
faults_summary += f"\nViT Model Prediction:\n- Class: {vit_result.get('class', 'Unknown')}\n- Confidence: {vit_result.get('confidence', 0)*100:.2f}%\n"
```
---
## π Visual Flow Diagram
```
Input CSV Data
β
Extract Resistance, Current, Travel
β
Generate Plot (matplotlib)
β
temp_vit_plot.png
β
ββββ [ViT API] β vit_probs
ββββ [Gemini AI] β gemini_probs
β
Ensemble Combination
β
ensemble_scores
β
Select MAX score β predicted_class
β
vitResult JSON
β
Included in final report
```
---
## π οΈ Configuration
**ViT API Endpoint**:
```python
DEPLOYED_VIT_URL = "http://143.110.244.235/predict"
```
**Gemini Model**:
```python
model = genai.GenerativeModel('gemini-2.0-flash')
```
**API Key** (from environment):
```python
GOOGLE_API_KEY # Main key
GOOGLE_API_KEY_1 # For R phase
GOOGLE_API_KEY_2 # For Y phase
GOOGLE_API_KEY_3 # For B phase
```
---
## π¨ Error Handling
If ViT or Gemini fails:
```python
if not vit_result:
# Pipeline continues without ViT analysis
# Other components (Rule Engine, AI Agent) still work
print("ViT prediction unavailable, continuing with other analyses...")
```
The pipeline is **resilient** - if ViT fails, analysis still completes using Rule Engine + AI Agent.
---
## π Summary
**vitResult provides**:
- β
Image-based defect classification
- β
Visual pattern recognition (ViT)
- β
Expert reasoning (Gemini)
- β
Ensemble confidence scoring
- β
Detailed probability breakdown
- β
Complements KPI-based and time-series analysis
It's a **3rd independent diagnostic method** alongside:
1. Rule Engine (deterministic thresholds)
2. AI Agent (LLM-based fault detection)
3. **ViT Model (image classification)** β This one!
All three methods are combined to provide comprehensive, multi-faceted circuit breaker diagnostics.
|