File size: 2,763 Bytes
43abac3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import yaml
import os
import numpy as np
import cv2
from src.data_loader import DeepGlobeDataset
from src.pipeline import EcoPulsePipeline
from src.metrics import calculate_iou, calculate_dice
from tqdm import tqdm

def evaluate_deepglobe(config_path):
    print("Initializing Pipeline...")
    pipeline = EcoPulsePipeline(config_path)
    config = pipeline.config
    
    deepglobe_dir = config['paths']['deepglobe_dir']
    if not os.path.exists(os.path.join(deepglobe_dir, 'images')):
        print("DeepGlobe dataset not found. Please run download_datasets.py first.")
        return

    dataset = DeepGlobeDataset(deepglobe_dir)
    print(f"Found {len(dataset)} images in DeepGlobe validation set.")
    
    ious = []
    dices = []
    
    # We evaluate on a small subset for demonstration purposes
    subset_size = min(10, len(dataset))
    
    print("Evaluating Segmentation Accuracy on DeepGlobe subset...")
    for i in tqdm(range(subset_size)):
        img_name = dataset.image_files[i]
        img_path = os.path.join(dataset.images_dir, img_name)
        
        _, results = pipeline.process_image(img_path)
        
        # Get image dimensions from the processed image directly
        raw_img = cv2.imread(img_path)
        if raw_img is None:
            raise FileNotFoundError(f"Could not read image at {img_path} — file may be missing or corrupt")
        h, w = raw_img.shape[:2]
        
        # Build predicted green mask
        pred_mask = np.zeros((h, w), dtype=bool)
        for item in results['mask_classifications']:
            if item['is_green']:
                pred_mask = np.logical_or(pred_mask, item['segmentation'])
                
        # Build true green mask from DeepGlobe color-coded annotations
        _, true_mask_rgb = dataset[i]
        true_mask_rgb = np.array(true_mask_rgb)
        
        # Flatten condition for greenery in DeepGlobe
        true_green = ((true_mask_rgb[:,:,1] == 255) & (true_mask_rgb[:,:,0] == 0) & (true_mask_rgb[:,:,2] == 0)) | \
                     ((true_mask_rgb[:,:,0] == 255) & (true_mask_rgb[:,:,1] == 255) & (true_mask_rgb[:,:,2] == 0)) | \
                     ((true_mask_rgb[:,:,0] == 255) & (true_mask_rgb[:,:,1] == 0) & (true_mask_rgb[:,:,2] == 255))
        
        iou = calculate_iou(pred_mask, true_green)
        dice = calculate_dice(pred_mask, true_green)
        
        ious.append(iou)
        dices.append(dice)
        
    print(f"Mean IoU (Greenery): {np.mean(ious):.4f}")
    print(f"Mean Dice Score: {np.mean(dices):.4f}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--config", default="config/config.yaml")
    args = parser.parse_args()
    evaluate_deepglobe(args.config)