edns 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- edns/__init__.py +60 -0
- edns/configs/models/edsr_small.yaml +30 -0
- edns/data/__init__.py +33 -0
- edns/data/io.py +124 -0
- edns/data/normalization.py +190 -0
- edns/evaluation/__init__.py +22 -0
- edns/evaluation/metrics.py +432 -0
- edns/hallucination/__init__.py +31 -0
- edns/hallucination/detection.py +732 -0
- edns/inference/__init__.py +14 -0
- edns/inference/app.py +335 -0
- edns/models/__init__.py +13 -0
- edns/models/edsr.py +218 -0
- edns/utils/__init__.py +15 -0
- edns/utils/helpers.py +90 -0
- edns/utils/logging.py +53 -0
- edns-1.0.0.dist-info/METADATA +172 -0
- edns-1.0.0.dist-info/RECORD +22 -0
- edns-1.0.0.dist-info/WHEEL +5 -0
- edns-1.0.0.dist-info/entry_points.txt +2 -0
- edns-1.0.0.dist-info/licenses/LICENSE +21 -0
- edns-1.0.0.dist-info/top_level.txt +1 -0
edns/__init__.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""
|
|
2
|
+
EDN-S: Enhanced Deep Network for Satellite Super-Resolution
|
|
3
|
+
==========================================================
|
|
4
|
+
|
|
5
|
+
A PyTorch implementation of EDSR-small for satellite imagery super-resolution.
|
|
6
|
+
Trained on Sentinel-2 simulated ALSAT-2B ×4 dataset.
|
|
7
|
+
|
|
8
|
+
Author: Aimen Achraff
|
|
9
|
+
License: MIT
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
__version__ = "1.0.0"
|
|
13
|
+
__author__ = "Aimen Achraff"
|
|
14
|
+
|
|
15
|
+
from .models import (
|
|
16
|
+
EDSR,
|
|
17
|
+
EDSRPretrained,
|
|
18
|
+
create_edsr,
|
|
19
|
+
ResBlock,
|
|
20
|
+
Upsampler,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
from .inference import (
|
|
24
|
+
run_sr,
|
|
25
|
+
load_model,
|
|
26
|
+
SRInference,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
from .data import (
|
|
30
|
+
load_sentinel2_band,
|
|
31
|
+
normalize_band,
|
|
32
|
+
denormalize_band,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
from .evaluation import (
|
|
36
|
+
compute_psnr,
|
|
37
|
+
compute_ssim,
|
|
38
|
+
compute_sam,
|
|
39
|
+
compute_ergas,
|
|
40
|
+
compute_all_metrics,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
__all__ = [
|
|
44
|
+
"EDSR",
|
|
45
|
+
"EDSRPretrained",
|
|
46
|
+
"create_edsr",
|
|
47
|
+
"ResBlock",
|
|
48
|
+
"Upsampler",
|
|
49
|
+
"run_sr",
|
|
50
|
+
"load_model",
|
|
51
|
+
"SRInference",
|
|
52
|
+
"load_sentinel2_band",
|
|
53
|
+
"normalize_band",
|
|
54
|
+
"denormalize_band",
|
|
55
|
+
"compute_psnr",
|
|
56
|
+
"compute_ssim",
|
|
57
|
+
"compute_sam",
|
|
58
|
+
"compute_ergas",
|
|
59
|
+
"compute_all_metrics",
|
|
60
|
+
]
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# EDN-S EDSR-small Configuration
|
|
2
|
+
# Protocol: 16 resblocks, 64 feats, scale=4, 4 channels
|
|
3
|
+
|
|
4
|
+
model:
|
|
5
|
+
_target_: edns.models.EDSR
|
|
6
|
+
n_resblocks: 16
|
|
7
|
+
n_feats: 64
|
|
8
|
+
scale: 4
|
|
9
|
+
in_channels: 4
|
|
10
|
+
out_channels: 4
|
|
11
|
+
res_scale: 1.0
|
|
12
|
+
rgb_range: 1.0
|
|
13
|
+
|
|
14
|
+
training:
|
|
15
|
+
batch_size: 16
|
|
16
|
+
patch_size: 48
|
|
17
|
+
lr: 1.0e-4
|
|
18
|
+
epochs: 300
|
|
19
|
+
loss_type: "l1"
|
|
20
|
+
amp: true
|
|
21
|
+
num_workers: 4
|
|
22
|
+
seed: 42
|
|
23
|
+
deterministic: true
|
|
24
|
+
val_interval: 1
|
|
25
|
+
checkpoint_interval: 50
|
|
26
|
+
grad_clip: 1.0
|
|
27
|
+
|
|
28
|
+
# Pretrained weights (optional)
|
|
29
|
+
# pretrained_path: "experiments/checkpoints/edsr_baseline_x4.pth"
|
|
30
|
+
# pretrained_3ch: true # adapt from 3-channel pretrained
|
edns/data/__init__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""
|
|
2
|
+
EDN-S Data Module
|
|
3
|
+
=================
|
|
4
|
+
|
|
5
|
+
Utilities for loading and preprocessing satellite imagery.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .io import (
|
|
9
|
+
load_sentinel2_band,
|
|
10
|
+
load_sentinel2_stack,
|
|
11
|
+
save_geotiff,
|
|
12
|
+
find_sentinel2_bands,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from .normalization import (
|
|
16
|
+
normalize_band,
|
|
17
|
+
denormalize_band,
|
|
18
|
+
normalize_stack,
|
|
19
|
+
denormalize_stack,
|
|
20
|
+
load_normalization_stats,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"load_sentinel2_band",
|
|
25
|
+
"load_sentinel2_stack",
|
|
26
|
+
"save_geotiff",
|
|
27
|
+
"find_sentinel2_bands",
|
|
28
|
+
"normalize_band",
|
|
29
|
+
"denormalize_band",
|
|
30
|
+
"normalize_stack",
|
|
31
|
+
"denormalize_stack",
|
|
32
|
+
"load_normalization_stats",
|
|
33
|
+
]
|
edns/data/io.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""
|
|
2
|
+
EDN-S Data I/O
|
|
3
|
+
==============
|
|
4
|
+
|
|
5
|
+
Reading and writing satellite GeoTIFF files.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import rasterio
|
|
9
|
+
import numpy as np
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import List, Tuple, Optional, Union
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def load_sentinel2_band(
|
|
15
|
+
filepath: Union[str, Path],
|
|
16
|
+
band_idx: int = 1
|
|
17
|
+
) -> Tuple[np.ndarray, dict]:
|
|
18
|
+
"""
|
|
19
|
+
Load a single band from a Sentinel-2 GeoTIFF.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
filepath: Path to GeoTIFF file
|
|
23
|
+
band_idx: 1-based band index (default: 1)
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
Tuple of (array [H, W], profile_dict)
|
|
27
|
+
"""
|
|
28
|
+
with rasterio.open(filepath) as src:
|
|
29
|
+
arr = src.read(band_idx).astype(np.float32)
|
|
30
|
+
profile = src.profile.copy()
|
|
31
|
+
return arr, profile
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def load_sentinel2_stack(
|
|
35
|
+
band_paths: List[Union[str, Path]],
|
|
36
|
+
band_order: List[str] = ['B2', 'B3', 'B4', 'B8']
|
|
37
|
+
) -> Tuple[np.ndarray, dict]:
|
|
38
|
+
"""
|
|
39
|
+
Load multiple bands as a stack [C, H, W].
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
band_paths: List of paths to band GeoTIFFs (order should match band_order)
|
|
43
|
+
band_order: Expected band order (default: B2, B3, B4, B8)
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
Tuple of (array [C, H, W], profile_dict from first band)
|
|
47
|
+
"""
|
|
48
|
+
if len(band_paths) != len(band_order):
|
|
49
|
+
raise ValueError(f"Expected {len(band_order)} bands, got {len(band_paths)}")
|
|
50
|
+
|
|
51
|
+
bands = []
|
|
52
|
+
profile = None
|
|
53
|
+
for i, path in enumerate(band_paths):
|
|
54
|
+
arr, p = load_sentinel2_band(path)
|
|
55
|
+
bands.append(arr)
|
|
56
|
+
if profile is None:
|
|
57
|
+
profile = p
|
|
58
|
+
|
|
59
|
+
# Stack to [C, H, W]
|
|
60
|
+
stack = np.stack(bands, axis=0)
|
|
61
|
+
return stack, profile
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def save_geotiff(
|
|
65
|
+
array: np.ndarray,
|
|
66
|
+
filepath: Union[str, Path],
|
|
67
|
+
profile: dict,
|
|
68
|
+
dtype: str = "float32",
|
|
69
|
+
compress: str = "lzw"
|
|
70
|
+
) -> str:
|
|
71
|
+
"""
|
|
72
|
+
Save array as GeoTIFF with updated profile.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
array: Array of shape [C, H, W] or [H, W]
|
|
76
|
+
filepath: Output path
|
|
77
|
+
profile: Rasterio profile dict (will be updated)
|
|
78
|
+
dtype: Output data type
|
|
79
|
+
compress: Compression method
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
Output filepath
|
|
83
|
+
"""
|
|
84
|
+
if array.ndim == 2:
|
|
85
|
+
array = array[np.newaxis, ...]
|
|
86
|
+
|
|
87
|
+
out_profile = profile.copy()
|
|
88
|
+
out_profile.update(
|
|
89
|
+
dtype=dtype,
|
|
90
|
+
count=array.shape[0],
|
|
91
|
+
height=array.shape[1],
|
|
92
|
+
width=array.shape[2],
|
|
93
|
+
compress=compress
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
with rasterio.open(filepath, 'w', **out_profile) as dst:
|
|
97
|
+
dst.write(array.astype(dtype))
|
|
98
|
+
|
|
99
|
+
return str(filepath)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def find_sentinel2_bands(
|
|
103
|
+
directory: Union[str, Path],
|
|
104
|
+
pattern: str = "*_{band}.tif"
|
|
105
|
+
) -> dict:
|
|
106
|
+
"""
|
|
107
|
+
Find Sentinel-2 band files in a directory.
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
directory: Directory to search
|
|
111
|
+
pattern: Glob pattern with {band} placeholder
|
|
112
|
+
|
|
113
|
+
Returns:
|
|
114
|
+
Dict mapping band names to file paths
|
|
115
|
+
"""
|
|
116
|
+
directory = Path(directory)
|
|
117
|
+
bands = {}
|
|
118
|
+
|
|
119
|
+
for band in ['B2', 'B3', 'B4', 'B8']:
|
|
120
|
+
files = list(directory.glob(pattern.format(band=band)))
|
|
121
|
+
if files:
|
|
122
|
+
bands[band] = files[0]
|
|
123
|
+
|
|
124
|
+
return bands
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""
|
|
2
|
+
EDN-S Normalization
|
|
3
|
+
===================
|
|
4
|
+
|
|
5
|
+
Z-score normalization for Sentinel-2 bands using training statistics.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
import numpy as np
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Dict, List, Optional, Union
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# Default normalization stats path
|
|
15
|
+
DEFAULT_STATS_PATH = Path(__file__).parent.parent.parent.parent / "data" / "metadata" / "sentinel2" / "normalization_stats.json"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def load_normalization_stats(path: Optional[Union[str, Path]] = None) -> Dict:
|
|
19
|
+
"""
|
|
20
|
+
Load normalization statistics from JSON file.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
path: Path to normalization stats JSON (uses default if None)
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
Dict with band statistics (mean, std, p2, p98)
|
|
27
|
+
"""
|
|
28
|
+
if path is None:
|
|
29
|
+
path = DEFAULT_STATS_PATH
|
|
30
|
+
|
|
31
|
+
with open(path) as f:
|
|
32
|
+
stats = json.load(f)
|
|
33
|
+
|
|
34
|
+
return stats
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def normalize_band(
|
|
38
|
+
band: np.ndarray,
|
|
39
|
+
band_name: str,
|
|
40
|
+
stats: Optional[Dict] = None,
|
|
41
|
+
method: str = "zscore"
|
|
42
|
+
) -> np.ndarray:
|
|
43
|
+
"""
|
|
44
|
+
Normalize a single band using training statistics.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
band: Input band array [H, W]
|
|
48
|
+
band_name: Band name (B2, B3, B4, B8)
|
|
49
|
+
stats: Normalization stats dict (loads default if None)
|
|
50
|
+
method: Normalization method ('zscore' or 'percentile')
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
Normalized band array
|
|
54
|
+
"""
|
|
55
|
+
if stats is None:
|
|
56
|
+
stats = load_normalization_stats()
|
|
57
|
+
|
|
58
|
+
if band_name not in stats['bands']:
|
|
59
|
+
raise ValueError(f"Band {band_name} not found in stats")
|
|
60
|
+
|
|
61
|
+
band_stats = stats['bands'][band_name]
|
|
62
|
+
|
|
63
|
+
# Handle NaN values
|
|
64
|
+
nan_mask = np.isnan(band)
|
|
65
|
+
band = np.nan_to_num(band, nan=0.0)
|
|
66
|
+
|
|
67
|
+
if method == "zscore":
|
|
68
|
+
mean = band_stats['mean']
|
|
69
|
+
std = band_stats['std']
|
|
70
|
+
normalized = (band - mean) / (std + 1e-8)
|
|
71
|
+
elif method == "percentile":
|
|
72
|
+
p2 = band_stats.get('p2', band_stats['mean'] - 2 * band_stats['std'])
|
|
73
|
+
p98 = band_stats.get('p98', band_stats['mean'] + 2 * band_stats['std'])
|
|
74
|
+
normalized = np.clip((band - p2) / (p98 - p2 + 1e-8), 0, 1)
|
|
75
|
+
else:
|
|
76
|
+
raise ValueError(f"Unknown method: {method}")
|
|
77
|
+
|
|
78
|
+
# Restore NaN
|
|
79
|
+
normalized[nan_mask] = np.nan
|
|
80
|
+
|
|
81
|
+
return normalized
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def denormalize_band(
|
|
85
|
+
band: np.ndarray,
|
|
86
|
+
band_name: str,
|
|
87
|
+
stats: Optional[Dict] = None,
|
|
88
|
+
method: str = "zscore"
|
|
89
|
+
) -> np.ndarray:
|
|
90
|
+
"""
|
|
91
|
+
Denormalize a band back to original range.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
band: Normalized band array [H, W]
|
|
95
|
+
band_name: Band name (B2, B3, B4, B8)
|
|
96
|
+
stats: Normalization stats dict (loads default if None)
|
|
97
|
+
method: Normalization method ('zscore' or 'percentile')
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
Denormalized band array
|
|
101
|
+
"""
|
|
102
|
+
if stats is None:
|
|
103
|
+
stats = load_normalization_stats()
|
|
104
|
+
|
|
105
|
+
if band_name not in stats['bands']:
|
|
106
|
+
raise ValueError(f"Band {band_name} not found in stats")
|
|
107
|
+
|
|
108
|
+
band_stats = stats['bands'][band_name]
|
|
109
|
+
|
|
110
|
+
# Handle NaN values
|
|
111
|
+
nan_mask = np.isnan(band)
|
|
112
|
+
band = np.nan_to_num(band, nan=0.0)
|
|
113
|
+
|
|
114
|
+
if method == "zscore":
|
|
115
|
+
mean = band_stats['mean']
|
|
116
|
+
std = band_stats['std']
|
|
117
|
+
denormalized = band * std + mean
|
|
118
|
+
elif method == "percentile":
|
|
119
|
+
p2 = band_stats.get('p2', band_stats['mean'] - 2 * band_stats['std'])
|
|
120
|
+
p98 = band_stats.get('p98', band_stats['mean'] + 2 * band_stats['std'])
|
|
121
|
+
denormalized = band * (p98 - p2) + p2
|
|
122
|
+
else:
|
|
123
|
+
raise ValueError(f"Unknown method: {method}")
|
|
124
|
+
|
|
125
|
+
# Restore NaN
|
|
126
|
+
denormalized[nan_mask] = np.nan
|
|
127
|
+
|
|
128
|
+
return denormalized
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def normalize_stack(
|
|
132
|
+
stack: np.ndarray,
|
|
133
|
+
band_order: List[str] = ['B2', 'B3', 'B4', 'B8'],
|
|
134
|
+
stats: Optional[Dict] = None,
|
|
135
|
+
method: str = "zscore"
|
|
136
|
+
) -> np.ndarray:
|
|
137
|
+
"""
|
|
138
|
+
Normalize a multi-band stack.
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
stack: Input stack [C, H, W]
|
|
142
|
+
band_order: List of band names matching stack channels
|
|
143
|
+
stats: Normalization stats dict (loads default if None)
|
|
144
|
+
method: Normalization method
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
Normalized stack [C, H, W]
|
|
148
|
+
"""
|
|
149
|
+
if stats is None:
|
|
150
|
+
stats = load_normalization_stats()
|
|
151
|
+
|
|
152
|
+
if len(band_order) != stack.shape[0]:
|
|
153
|
+
raise ValueError(f"Band order length {len(band_order)} != channels {stack.shape[0]}")
|
|
154
|
+
|
|
155
|
+
normalized = np.zeros_like(stack)
|
|
156
|
+
for i, band_name in enumerate(band_order):
|
|
157
|
+
normalized[i] = normalize_band(stack[i], band_name, stats, method)
|
|
158
|
+
|
|
159
|
+
return normalized
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def denormalize_stack(
|
|
163
|
+
stack: np.ndarray,
|
|
164
|
+
band_order: List[str] = ['B2', 'B3', 'B4', 'B8'],
|
|
165
|
+
stats: Optional[Dict] = None,
|
|
166
|
+
method: str = "zscore"
|
|
167
|
+
) -> np.ndarray:
|
|
168
|
+
"""
|
|
169
|
+
Denormalize a multi-band stack.
|
|
170
|
+
|
|
171
|
+
Args:
|
|
172
|
+
stack: Normalized stack [C, H, W]
|
|
173
|
+
band_order: List of band names matching stack channels
|
|
174
|
+
stats: Normalization stats dict (loads default if None)
|
|
175
|
+
method: Normalization method
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
Denormalized stack [C, H, W]
|
|
179
|
+
"""
|
|
180
|
+
if stats is None:
|
|
181
|
+
stats = load_normalization_stats()
|
|
182
|
+
|
|
183
|
+
if len(band_order) != stack.shape[0]:
|
|
184
|
+
raise ValueError(f"Band order length {len(band_order)} != channels {stack.shape[0]}")
|
|
185
|
+
|
|
186
|
+
denormalized = np.zeros_like(stack)
|
|
187
|
+
for i, band_name in enumerate(band_order):
|
|
188
|
+
denormalized[i] = denormalize_band(stack[i], band_name, stats, method)
|
|
189
|
+
|
|
190
|
+
return denormalized
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""
|
|
2
|
+
EDN-S Evaluation Module
|
|
3
|
+
=======================
|
|
4
|
+
|
|
5
|
+
Metrics for super-resolution quality assessment.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .metrics import (
|
|
9
|
+
compute_psnr,
|
|
10
|
+
compute_ssim,
|
|
11
|
+
compute_sam,
|
|
12
|
+
compute_ergas,
|
|
13
|
+
compute_all_metrics,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"compute_psnr",
|
|
18
|
+
"compute_ssim",
|
|
19
|
+
"compute_sam",
|
|
20
|
+
"compute_ergas",
|
|
21
|
+
"compute_all_metrics",
|
|
22
|
+
]
|