-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
326 lines (268 loc) · 12.8 KB
/
main.py
File metadata and controls
326 lines (268 loc) · 12.8 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python3
"""
Crop Classification Pipeline - Main Script
This script orchestrates the complete crop classification preprocessing pipeline:
1. Extract labeled patches from multispectral orthoimages
2. Stack temporal patches into 4D arrays
3. Create spatially-aware train/validation/test splits
Usage:
python main.py --help
python main.py extract --vector data/raw/fields.geojson --ortho-dir data/raw/orthoimages/
python main.py stack --patches-dir data/processed/patches
python main.py split --mapping data/processed/stacked/stack_mapping.csv --zone-mask data/raw/zone_mask.tif
python main.py pipeline --vector data/raw/fields.geojson --ortho-dir data/raw/orthoimages/ --zone-mask data/raw/zone_mask.tif
"""
import os
import sys
import argparse
import logging
from pathlib import Path
# Add src directory to path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from libs.patch_extractor import PatchExtractor
from libs.temporal_stacker import TemporalStacker
from libs.spatial_splitter import SpatialSplitter
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('pipeline.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def setup_directories(base_dir: str = ".") -> None:
"""Create necessary directory structure."""
directories = [
"data/raw/orthoimages",
"data/raw/vectors",
"data/processed/patches",
"data/processed/stacked",
"output/models",
"output/predictions",
"output/visualizations"
]
for dir_path in directories:
full_path = os.path.join(base_dir, dir_path)
os.makedirs(full_path, exist_ok=True)
logger.info(f"Created directory: {full_path}")
def extract_patches(args) -> str:
"""
Extract labeled patches from orthoimages.
Args:
args: Parsed command line arguments
Returns:
Path to output directory
"""
logger.info("Starting patch extraction")
extractor = PatchExtractor(
vector_path=args.vector,
ortho_dir=args.ortho_dir,
mask_path=args.mask_path,
output_dir=args.output_dir,
patch_size=args.patch_size,
stride=args.stride,
channel_first=args.channel_first,
min_plots_per_class=args.min_plots_per_class
)
extractor.extract_all_orthos()
# Log statistics
crop_mapping = extractor.get_crop_mapping()
logger.info(f"Extraction complete. Found {len(crop_mapping)} crop classes:")
for label, name in crop_mapping.items():
logger.info(f" Class {label}: {name}")
return args.output_dir
def stack_temporal(args) -> str:
"""
Stack temporal patches into 4D arrays.
Args:
args: Parsed command line arguments
Returns:
Path to mapping CSV file
"""
logger.info("Starting temporal stacking")
stacker = TemporalStacker(
base_dir=args.patches_dir,
output_dir=args.output_dir,
patch_subdir=args.patch_subdir,
date_pattern=args.date_pattern,
expected_bands=args.expected_bands,
min_temporal_samples=args.min_temporal_samples
)
mapping_path = stacker.run()
# Log statistics
stats = stacker.get_statistics()
logger.info(f"Stacking complete. Statistics:")
logger.info(f" Total patches: {stats['total_patches']}")
# Only log additional stats if patches were processed
if stats['total_patches'] > 0:
logger.info(f" Unique classes: {stats['unique_classes']}")
logger.info(f" Temporal samples - Min: {stats['temporal_samples_stats']['min']}, "
f"Max: {stats['temporal_samples_stats']['max']}, "
f"Mean: {stats['temporal_samples_stats']['mean']:.1f}")
else:
logger.warning(" No patches were processed. Check min_temporal_samples requirement.")
return mapping_path
def create_spatial_splits(args) -> tuple:
"""
Create spatially-aware train/validation/test splits.
Args:
args: Parsed command line arguments
Returns:
Tuple of (full_mapping_path, stacked_split_path)
"""
logger.info("Starting spatial splitting")
splitter = SpatialSplitter(
mapping_csv=args.mapping,
zone_mask_path=args.zone_mask,
stacked_folder=args.stacked_folder,
output_csv=args.output_csv,
min_temporal_samples=args.min_temporal_samples,
excluded_classes=args.excluded_classes
)
# Set custom zone mapping if provided
if args.zone_mapping:
custom_mapping = {}
for mapping in args.zone_mapping:
zone_id, split_name = mapping.split(':')
custom_mapping[int(zone_id)] = split_name
splitter.set_custom_zone_mapping(custom_mapping)
full_path, stacked_path = splitter.run()
# Log statistics
stats = splitter.get_split_statistics()
logger.info(f"Spatial splitting complete. Statistics:")
logger.info(f" Total patches: {stats['total_patches']}")
for split, count in stats['split_distribution'].items():
logger.info(f" {split.capitalize()}: {count} patches")
return full_path, stacked_path
def run_full_pipeline(args) -> None:
"""
Run the complete preprocessing pipeline.
Args:
args: Parsed command line arguments
"""
logger.info("Starting full preprocessing pipeline")
# Setup directories
setup_directories()
# Step 1: Extract patches
logger.info("=" * 50)
logger.info("STEP 1: Patch Extraction")
logger.info("=" * 50)
patch_args = argparse.Namespace(
vector=args.vector,
ortho_dir=args.ortho_dir,
mask_path=args.mask_path,
output_dir=args.patches_output or "data/processed/patches",
patch_size=args.patch_size,
stride=args.stride,
channel_first=args.channel_first,
min_plots_per_class=args.min_plots_per_class
)
patches_dir = extract_patches(patch_args)
# Step 2: Stack temporal data
logger.info("=" * 50)
logger.info("STEP 2: Temporal Stacking")
logger.info("=" * 50)
stack_args = argparse.Namespace(
patches_dir=patches_dir,
output_dir=args.stacked_output or "data/processed/stacked",
patch_subdir=args.patch_subdir,
date_pattern=args.date_pattern,
expected_bands=args.expected_bands,
min_temporal_samples=args.min_temporal_samples
)
mapping_path = stack_temporal(stack_args)
# Step 3: Create spatial splits
logger.info("=" * 50)
logger.info("STEP 3: Spatial Splitting")
logger.info("=" * 50)
split_args = argparse.Namespace(
mapping=mapping_path,
zone_mask=args.zone_mask,
stacked_folder=stack_args.output_dir,
output_csv=None,
min_temporal_samples=args.min_temporal_samples,
excluded_classes=args.excluded_classes,
zone_mapping=args.zone_mapping
)
full_path, stacked_path = create_spatial_splits(split_args)
logger.info("=" * 50)
logger.info("PIPELINE COMPLETE")
logger.info("=" * 50)
logger.info(f"Results saved to:")
logger.info(f" Patches: {patches_dir}")
logger.info(f" Stacked arrays: {stack_args.output_dir}")
logger.info(f" Split mapping: {stacked_path}")
logger.info(f" Full mapping: {full_path}")
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Crop Classification Preprocessing Pipeline",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__
)
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Extract patches subcommand
extract_parser = subparsers.add_parser('extract', help='Extract labeled patches from orthoimages')
extract_parser.add_argument('--vector', required=True, help='Path to vector file (GeoJSON/Shapefile)')
extract_parser.add_argument('--ortho-dir', required=True, help='Directory containing orthoimage TIFFs')
extract_parser.add_argument('--mask-path', help='Pre-computed crop mask (optional)')
extract_parser.add_argument('--output-dir', default='data/processed/patches', help='Output directory for patches')
extract_parser.add_argument('--patch-size', type=int, default=64, help='Patch size in pixels')
extract_parser.add_argument('--stride', type=int, default=32, help='Sliding window stride')
extract_parser.add_argument('--channel-first', action='store_true', default=True, help='Store patches as (C,H,W)')
extract_parser.add_argument('--min-plots-per-class', type=int, default=3, help='Minimum plots per crop class')
# Stack temporal subcommand
stack_parser = subparsers.add_parser('stack', help='Stack temporal patches into 4D arrays')
stack_parser.add_argument('--patches-dir', default='data/processed/patches', help='Directory containing patches')
stack_parser.add_argument('--output-dir', default='data/processed/stacked', help='Output directory for stacked arrays')
stack_parser.add_argument('--patch-subdir', default='patch', help='Subdirectory name within date folders')
stack_parser.add_argument('--date-pattern', default=r'\d{6}_reflectance_ortho', help='Regex pattern for date folders')
stack_parser.add_argument('--expected-bands', type=int, default=10, help='Expected number of spectral bands')
stack_parser.add_argument('--min-temporal-samples', type=int, default=3, help='Minimum temporal samples required')
# Split spatial subcommand
split_parser = subparsers.add_parser('split', help='Create spatial train/val/test splits')
split_parser.add_argument('--mapping', required=True, help='Path to stack mapping CSV')
split_parser.add_argument('--zone-mask', required=True, help='Path to zone mask raster')
split_parser.add_argument('--stacked-folder', required=True, help='Directory containing stacked .npy files')
split_parser.add_argument('--output-csv', help='Output path for split mapping')
split_parser.add_argument('--min-temporal-samples', type=int, default=3, help='Minimum temporal samples required')
split_parser.add_argument('--excluded-classes', type=int, nargs='+', default=[1, 2], help='Class IDs to exclude')
split_parser.add_argument('--zone-mapping', nargs='+', help='Custom zone mappings (format: zone:split)')
# Full pipeline subcommand
pipeline_parser = subparsers.add_parser('pipeline', help='Run complete preprocessing pipeline')
pipeline_parser.add_argument('--vector', required=True, help='Path to vector file (GeoJSON/Shapefile)')
pipeline_parser.add_argument('--ortho-dir', required=True, help='Directory containing orthoimage TIFFs')
pipeline_parser.add_argument('--zone-mask', required=True, help='Path to zone mask raster')
pipeline_parser.add_argument('--mask-path', help='Pre-computed crop mask (optional)')
pipeline_parser.add_argument('--patches-output', help='Output directory for patches')
pipeline_parser.add_argument('--stacked-output', help='Output directory for stacked arrays')
pipeline_parser.add_argument('--patch-size', type=int, default=64, help='Patch size in pixels')
pipeline_parser.add_argument('--stride', type=int, default=32, help='Sliding window stride')
pipeline_parser.add_argument('--channel-first', action='store_true', default=True, help='Store patches as (C,H,W)')
pipeline_parser.add_argument('--min-plots-per-class', type=int, default=3, help='Minimum plots per crop class')
pipeline_parser.add_argument('--patch-subdir', default='patch', help='Subdirectory name within date folders')
pipeline_parser.add_argument('--date-pattern', default=r'\d{6}_reflectance_ortho', help='Regex pattern for date folders')
pipeline_parser.add_argument('--expected-bands', type=int, default=10, help='Expected number of spectral bands')
pipeline_parser.add_argument('--min-temporal-samples', type=int, default=3, help='Minimum temporal samples required')
pipeline_parser.add_argument('--excluded-classes', type=int, nargs='+', default=[1, 2], help='Class IDs to exclude')
pipeline_parser.add_argument('--zone-mapping', nargs='+', help='Custom zone mappings (format: zone:split)')
# Setup subcommand
setup_parser = subparsers.add_parser('setup', help='Setup directory structure')
setup_parser.add_argument('--base-dir', default='.', help='Base directory for setup')
args = parser.parse_args()
if args.command == 'extract':
extract_patches(args)
elif args.command == 'stack':
stack_temporal(args)
elif args.command == 'split':
create_spatial_splits(args)
elif args.command == 'pipeline':
run_full_pipeline(args)
elif args.command == 'setup':
setup_directories(args.base_dir)
else:
parser.print_help()
if __name__ == '__main__':
main()