File size: 7,861 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
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
"""
Phase 3: Interpretability & Analysis
Generates Grad-CAM heatmaps and runs NDVI correlation analysis
using real DeepGlobe images through the EcoPulse pipeline.
"""
import argparse
import os
import yaml
import numpy as np
import cv2
import torch
from PIL import Image
from tqdm import tqdm

from src.cnn_model import load_model
from src.transforms import EUROSAT_TRANSFORM
from src.visualization import apply_grad_cam, save_grad_cam_overlay, plot_greenery_overlay
from src.pipeline import EcoPulsePipeline
from src.data_loader import DeepGlobeDataset
from src.metrics import ndvi_correlation


def generate_grad_cam_samples(config_path, num_samples=5):
    """
    Generates Grad-CAM heatmap overlays for a set of DeepGlobe images
    using the trained ResNet-50 classifier.
    """
    with open(config_path, "r") as f:
        config = yaml.safe_load(f)

    output_dir = os.path.join(config['paths']['output_figures'], 'grad_cam')
    os.makedirs(output_dir, exist_ok=True)

    classes = config['classes']
    greenery_classes = config['greenery_classes']

    # Load the trained CNN
    device = "cuda" if torch.cuda.is_available() else "cpu"
    print(f"Using device: {device}")
    model = load_model(
        weights_path=os.path.join(config['paths']['output_models'], 'resnet50_eurosat.pth'),
        num_classes=config['model']['num_classes'],
        device=device
    )
    model.eval()

    transform = EUROSAT_TRANSFORM

    # Load DeepGlobe images
    deepglobe_dir = config['paths']['deepglobe_dir']
    dataset = DeepGlobeDataset(deepglobe_dir)
    num_samples = min(num_samples, len(dataset))

    print(f"\n{'='*60}")
    print(f"  GRAD-CAM HEATMAP GENERATION")
    print(f"  Processing {num_samples} DeepGlobe images...")
    print(f"{'='*60}\n")

    for i in tqdm(range(num_samples), desc="Generating Grad-CAMs"):
        img_name = dataset.image_files[i]
        img_path = os.path.join(dataset.images_dir, img_name)

        # Load the raw image for the overlay
        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")
        raw_img_rgb = cv2.cvtColor(raw_img, cv2.COLOR_BGR2RGB)

        # Create a center crop (64x64-like patch) for the CNN input
        h, w = raw_img_rgb.shape[:2]
        cx, cy = w // 2, h // 2
        patch_size = min(h, w) // 3
        crop = raw_img_rgb[cy - patch_size:cy + patch_size, cx - patch_size:cx + patch_size]

        crop_pil = Image.fromarray(crop)
        input_tensor = transform(crop_pil).unsqueeze(0)

        # Generate Grad-CAM
        heatmap, pred_idx = apply_grad_cam(model, input_tensor, target_class=None)
        pred_class = classes[pred_idx]
        is_green = pred_class in greenery_classes

        # Save the overlay
        base_name = os.path.splitext(img_name)[0]
        out_path = os.path.join(output_dir, f"{base_name}_gradcam.png")
        save_grad_cam_overlay(crop, heatmap, out_path)

        label = "[GREENERY]" if is_green else "[NON-GREEN]"
        print(f"  [{i+1}/{num_samples}] {img_name} -> Predicted: {pred_class} ({label})")

    print(f"\nAll Grad-CAM overlays saved to: {output_dir}")
    return output_dir


def run_ndvi_correlation(config_path, num_samples=5):
    """
    Runs the EcoPulse pipeline on DeepGlobe images, then computes a
    pseudo-NDVI from the RGB channels and correlates it with the
    pipeline's predicted greenery percentage.

    Note: DeepGlobe provides standard RGB imagery, not multispectral.
    We approximate NDVI using the Normalized Green-Red Difference Index (NGRDI):
        NGRDI = (Green - Red) / (Green + Red)
    This is a well-known vegetation proxy for RGB-only data.
    """
    print(f"\n{'='*60}")
    print(f"  NDVI CORRELATION ANALYSIS")
    print(f"{'='*60}\n")

    pipeline = EcoPulsePipeline(config_path)
    config = pipeline.config

    deepglobe_dir = config['paths']['deepglobe_dir']
    dataset = DeepGlobeDataset(deepglobe_dir)
    num_samples = min(num_samples, len(dataset))

    predicted_greenery = []
    ngrdi_values = []

    print(f"Processing {num_samples} images through the full pipeline...\n")

    for i in tqdm(range(num_samples), desc="Pipeline + NGRDI"):
        img_name = dataset.image_files[i]
        img_path = os.path.join(dataset.images_dir, img_name)

        # Run the full EcoPulse pipeline
        image_np, results = pipeline.process_image(img_path)
        predicted_greenery.append(results['greenery_percentage'])

        # Compute NGRDI (RGB-based vegetation proxy)
        green_band = image_np[:, :, 1].astype(float)
        red_band = image_np[:, :, 0].astype(float)
        denominator = green_band + red_band
        denominator[denominator == 0] = 1e-6
        ngrdi = (green_band - red_band) / denominator
        mean_ngrdi = np.mean(ngrdi)
        ngrdi_values.append(mean_ngrdi)

        print(f"  [{i+1}/{num_samples}] {img_name}: "
              f"EcoPulse Greenery = {results['greenery_percentage']:.1f}% | "
              f"Mean NGRDI = {mean_ngrdi:.4f}")

    # Compute correlation
    corr = ndvi_correlation(predicted_greenery, ngrdi_values)

    print(f"\n{'='*60}")
    print(f"  RESULTS")
    print(f"{'='*60}")
    print(f"  Pearson Correlation (EcoPulse vs NGRDI): {corr:.4f}")

    if corr > 0.7:
        print("  [OK] Strong positive correlation -- EcoPulse's deep learning approach")
        print("       closely aligns with traditional vegetation indices.")
    elif corr > 0.4:
        print("  [!!] Moderate correlation -- partial alignment with vegetation indices.")
    else:
        print("  [ii] Weak correlation -- expected when comparing learned features")
        print("       against a simple spectral proxy on RGB-only imagery.")

    # Save results
    output_dir = config['paths']['output_figures']
    os.makedirs(output_dir, exist_ok=True)

    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots(figsize=(8, 6))
    ax.scatter(ngrdi_values, predicted_greenery, c='forestgreen', s=100, edgecolors='black', zorder=5)

    # Trend line
    if len(ngrdi_values) >= 2:
        z = np.polyfit(ngrdi_values, predicted_greenery, 1)
        p = np.poly1d(z)
        x_line = np.linspace(min(ngrdi_values), max(ngrdi_values), 100)
        ax.plot(x_line, p(x_line), '--', color='darkgreen', alpha=0.7, label=f'Trend (r={corr:.3f})')

    ax.set_xlabel('Mean NGRDI (RGB Vegetation Proxy)', fontsize=12)
    ax.set_ylabel('EcoPulse Predicted Greenery (%)', fontsize=12)
    ax.set_title('NDVI Correlation: EcoPulse vs NGRDI', fontsize=14)
    ax.legend(fontsize=11)
    ax.grid(True, alpha=0.3)
    plt.tight_layout()

    scatter_path = os.path.join(output_dir, 'ndvi_correlation_scatter.png')
    plt.savefig(scatter_path, dpi=150)
    plt.close()
    print(f"\n  Scatter plot saved to: {scatter_path}")

    return corr


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="EcoPulse Phase 3: Interpretability & Analysis")
    parser.add_argument("--config", default="config/config.yaml", help="Path to config file")
    parser.add_argument("--samples", type=int, default=5, help="Number of images to process")
    args = parser.parse_args()

    print("=" * 60)
    print("  EcoPulse: Interpretability & Analysis")
    print("=" * 60)

    # Part 1: Grad-CAM Heatmaps
    generate_grad_cam_samples(args.config, num_samples=args.samples)

    # Part 2: NDVI Correlation
    corr = run_ndvi_correlation(args.config, num_samples=args.samples)

    print(f"\n{'='*60}")
    print(f"  INTERPRETABILITY ANALYSIS COMPLETE")
    print(f"  Grad-CAM heatmaps: outputs/figures/grad_cam/")
    print(f"  NDVI scatter plot:  outputs/figures/ndvi_correlation_scatter.png")
    print(f"  Pearson Correlation: {corr:.4f}")
    print(f"{'='*60}")