DEV Community

TK Lin
TK Lin

Posted on

๐Ÿง  Ensemble_Strategy

Ensemble Strategy: Two AIs Are Better Than One

Washin Village AI Director Tech Notes #4


๐ŸŽฏ What is Ensemble?

Just like human teamwork, Ensemble Learning makes multiple AI models work together, combining their judgments for more accurate results.

Core concept: Two heads are better than one.


๐Ÿ” Why Do We Need Ensemble?

Single model limitations:

Model Pros Cons
Unified_v18 High accuracy (79.5%) Prone to errors on certain categories
Inc_v201 Different training data Lower accuracy (46%)

However! The probability of both models making the same mistake is very low.


๐Ÿ’ก Ensemble Strategies

Strategy 1: Voting Mechanism

def ensemble_vote(predictions):
    """Majority voting"""
    from collections import Counter
    votes = Counter([p['class'] for p in predictions])
    return votes.most_common(1)[0][0]
Enter fullscreen mode Exit fullscreen mode

Strategy 2: Weighted Confidence

def ensemble_weighted(predictions, weights):
    """Weighted average confidence"""
    combined = {}
    for pred, weight in zip(predictions, weights):
        for cls, conf in pred.items():
            combined[cls] = combined.get(cls, 0) + conf * weight
    return max(combined, key=combined.get)
Enter fullscreen mode Exit fullscreen mode

Strategy 3: Validation Mode

def ensemble_validate(primary, secondary, threshold=0.85):
    """Primary model predicts, secondary validates"""
    if primary['confidence'] >= threshold:
        return primary['class']

    if primary['class'] == secondary['class']:
        return primary['class']

    return "needs_human_review"
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Š Our Configuration

Dual-Model Ensemble

Input Image
    โ”‚
    โ”œโ”€โ†’ Primary Model (Unified_v18) โ”€โ”€โ†’ Prediction + Confidence
    โ”‚
    โ””โ”€โ†’ Validation Model (Inc_v201) โ”€โ”€โ†’ Prediction + Confidence
    โ”‚
    โ†“
Ensemble Decision Engine
    โ”‚
    โ†“
Final Result
Enter fullscreen mode Exit fullscreen mode

Decision Rules

Scenario Action
Primary confidence โ‰ฅ 90% Use primary result directly
Both models agree Use that result
Models disagree Choose higher confidence
Both uncertain Mark for human review

๐Ÿ“ˆ Performance Comparison

Configuration Top-1 Accuracy Human Review Rate
Primary only 79.5% 0%
Ensemble (voting) 81.2% 0%
Ensemble (validation) 82.8% 5%

Conclusion: Ensemble + validation mode works best!


๐Ÿ”ง Actual Code

class EnsembleValidator:
    def __init__(self):
        self.primary = YOLO('Unified_v18.pt')
        self.secondary = YOLO('Inc_v201.pt')

    def predict(self, image):
        # Primary model prediction
        p1 = self.primary.predict(image)

        # High confidence: return directly
        if p1.confidence >= 0.90:
            return p1.class_name, "high_confidence"

        # Secondary model validation
        p2 = self.secondary.predict(image)

        if p1.class_name == p2.class_name:
            return p1.class_name, "validated"

        # Return higher confidence
        if p1.confidence > p2.confidence:
            return p1.class_name, "primary_higher"
        else:
            return p2.class_name, "secondary_higher"
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Lessons Learned

  1. Model diversity matters: Use different architectures or training data
  2. Weight allocation: Higher accuracy models get higher weights
  3. Speed tradeoffs: Ensemble is slower than single model
  4. Threshold tuning: Adjust confidence thresholds based on actual needs

Washin Village ๐Ÿก by AI Director

Top comments (0)