Download datacollection/generate_masks.py from kabiriitb/rockglacierNET: direct link, hf CLI and curl.
- Browser
- Download file 7.33 kB
-
https://huggingface.co/kabiriitb/rockglacierNET/resolve/main/datacollection/generate_masks.py
- Command line
-
hf download hf://kabiriitb/rockglacierNET/datacollection/generate_masks.py
-
curl -L -o generate_masks.py https://huggingface.co/kabiriitb/rockglacierNET/resolve/main/datacollection/generate_masks.py
7.33 kB
| import os | |
| import glob | |
| import yaml | |
| import numpy as np | |
| import pandas as pd | |
| import geopandas as gpd | |
| from shapely.geometry import box | |
| import rasterio | |
| from rasterio.features import rasterize | |
| from rasterio.transform import from_bounds | |
| import random | |
| import matplotlib | |
| matplotlib.use('Agg') | |
| import matplotlib.pyplot as plt | |
| def load_config(): | |
| config_path = os.path.join(os.path.dirname(__file__), 'config.yaml') | |
| with open(config_path, 'r') as f: | |
| return yaml.safe_load(f) | |
| def generate_masks(): | |
| config = load_config() | |
| boundary_shp_path = os.path.join(os.path.dirname(__file__), config['paths']['rock_glacier_boundary_shp']) | |
| input_dir = os.path.join(os.path.dirname(__file__), config['paths']['output_dir'], 'individual_polygons') | |
| output_mask_dir = os.path.join(os.path.dirname(__file__), config['paths']['output_dir'], 'masks') | |
| report_path = os.path.join(output_mask_dir, 'mask_report.csv') | |
| target_crs = config['preprocessing']['target_crs'] | |
| resolution = config['preprocessing']['resolution_m'] | |
| padding_m = config['preprocessing']['padding_m'] | |
| bg_ratio = config['preprocessing']['background_ratio'] | |
| os.makedirs(output_mask_dir, exist_ok=True) | |
| geojson_files = sorted(glob.glob(os.path.join(input_dir, '*.geojson'))) | |
| if not geojson_files: | |
| print(f"No GeoJSON files found in {input_dir}") | |
| return | |
| print(f"Loading master shapefile from {boundary_shp_path}") | |
| boundary_gdf = gpd.read_file(boundary_shp_path).to_crs(target_crs) | |
| print(f"Generating variable-sized masks with {padding_m}m padding...") | |
| all_masks_info = [] | |
| for filepath in geojson_files: | |
| filename = os.path.basename(filepath) | |
| idx_str = filename.split('_')[1].split('.')[0] | |
| idx = int(idx_str) | |
| gdf = gpd.read_file(filepath) | |
| gdf_proj = gdf.to_crs(target_crs) | |
| if gdf_proj.empty or gdf_proj.geometry.is_empty.all() or gdf_proj.geometry.isna().all(): | |
| continue | |
| # 1. Get true bounding box of the polygon | |
| geom = gdf_proj.geometry.iloc[0] | |
| minx, miny, maxx, maxy = geom.bounds | |
| # 2. Add padding to create the context window | |
| minx -= padding_m | |
| miny -= padding_m | |
| maxx += padding_m | |
| maxy += padding_m | |
| # Ensure dimensions are clean multiples of the resolution (10m) | |
| width = int(np.ceil((maxx - minx) / resolution)) | |
| height = int(np.ceil((maxy - miny) / resolution)) | |
| maxx = minx + width * resolution | |
| maxy = miny + height * resolution | |
| roi_box = box(minx, miny, maxx, maxy) | |
| transform = from_bounds(minx, miny, maxx, maxy, width, height) | |
| # 3. Burn ALL intersecting rock glaciers to prevent label noise | |
| intersecting_glaciers = boundary_gdf[boundary_gdf.intersects(roi_box)] | |
| if intersecting_glaciers.empty: | |
| mask = np.zeros((height, width), dtype='uint8') | |
| else: | |
| shapes = ((g, 1) for g in intersecting_glaciers.geometry) | |
| mask = rasterize( | |
| shapes, | |
| out_shape=(height, width), | |
| transform=transform, | |
| fill=0, | |
| dtype='uint8' | |
| ) | |
| fg_pixels = int(mask.sum()) | |
| all_masks_info.append({ | |
| 'idx': idx, | |
| 'mask': mask, | |
| 'transform': transform, | |
| 'width': width, | |
| 'height': height, | |
| 'fg_pixels': fg_pixels, | |
| 'is_bg': False | |
| }) | |
| num_fg = len(all_masks_info) | |
| target_bg_count = int(num_fg * (bg_ratio / (1 - bg_ratio))) | |
| print(f"Generated {num_fg} foreground masks.") | |
| print(f"Hunting for {target_bg_count} pure background masks...") | |
| # 4. Generate pure background masks | |
| study_area_bounds = boundary_gdf.total_bounds # minx, miny, maxx, maxy | |
| bg_idx = 9000 # Start background indices at 9000 to easily distinguish them | |
| bg_generated = 0 | |
| attempts = 0 | |
| max_attempts = target_bg_count * 50 | |
| random.seed(42) # Reproducibility | |
| # Standard size for background patches (e.g. 640m x 640m) | |
| bg_size_m = 640.0 | |
| bg_px = int(bg_size_m / resolution) | |
| while bg_generated < target_bg_count and attempts < max_attempts: | |
| attempts += 1 | |
| # Pick a random center inside the study area | |
| cx = random.uniform(study_area_bounds[0] + bg_size_m, study_area_bounds[2] - bg_size_m) | |
| cy = random.uniform(study_area_bounds[1] + bg_size_m, study_area_bounds[3] - bg_size_m) | |
| minx = cx - bg_size_m/2 | |
| miny = cy - bg_size_m/2 | |
| maxx = cx + bg_size_m/2 | |
| maxy = cy + bg_size_m/2 | |
| width = bg_px | |
| height = bg_px | |
| roi_box = box(minx, miny, maxx, maxy) | |
| # Check if this random box touches ANY rock glacier | |
| intersecting = boundary_gdf[boundary_gdf.intersects(roi_box)] | |
| if not intersecting.empty: | |
| continue # It touched a glacier, throw it away! | |
| # It's pure background! Save it. | |
| transform = from_bounds(minx, miny, maxx, maxy, width, height) | |
| mask = np.zeros((height, width), dtype='uint8') | |
| all_masks_info.append({ | |
| 'idx': bg_idx, | |
| 'mask': mask, | |
| 'transform': transform, | |
| 'width': width, | |
| 'height': height, | |
| 'fg_pixels': 0, | |
| 'is_bg': True | |
| }) | |
| bg_idx += 1 | |
| bg_generated += 1 | |
| if bg_generated < target_bg_count: | |
| print(f"Warning: Only found {bg_generated} pure background patches after {max_attempts} attempts.") | |
| print(f"Total masks retained: {len(all_masks_info)} ({num_fg} foreground, {bg_generated} background)") | |
| report_data = [] | |
| for m in all_masks_info: | |
| idx = m['idx'] | |
| mask = m['mask'] | |
| transform = m['transform'] | |
| width = m['width'] | |
| height = m['height'] | |
| fg_pixels = m['fg_pixels'] | |
| out_tif = os.path.join(output_mask_dir, f"mask_{idx:04d}.tif") | |
| with rasterio.open( | |
| out_tif, 'w', driver='GTiff', height=height, width=width, | |
| count=1, dtype=mask.dtype, crs=target_crs, transform=transform | |
| ) as dst: | |
| dst.write(mask, 1) | |
| if idx < 5 or (m['is_bg'] and idx < 9005): | |
| plt.figure(figsize=(6, 6)) | |
| plt.imshow(mask, cmap='gray', vmin=0, vmax=1) | |
| title = f"{'Background' if m['is_bg'] else 'Foreground'} Mask {idx:04d} ({width}x{height}px)" | |
| plt.title(title) | |
| plt.axis('off') | |
| plt.savefig(os.path.join(output_mask_dir, f"plot_mask_{idx:04d}.png"), dpi=150, bbox_inches='tight') | |
| plt.close() | |
| report_data.append({ | |
| 'Polygon_ID': idx, | |
| 'Width': width, | |
| 'Height': height, | |
| 'Foreground_Pixels': fg_pixels, | |
| 'Background_Pixels': width * height - fg_pixels, | |
| 'Is_Pure_Background': m['is_bg'] | |
| }) | |
| df_report = pd.DataFrame(report_data) | |
| df_report.to_csv(report_path, index=False) | |
| print(f"Validation report saved to {report_path}") | |
| if __name__ == '__main__': | |
| generate_masks() | |