Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
"""
|
2 |
-
Biomass Prediction Gradio App with
|
3 |
Author: najahpokkiri
|
4 |
Date: 2025-05-17
|
5 |
|
6 |
-
This version ensures
|
7 |
"""
|
8 |
import os
|
9 |
import sys
|
@@ -75,10 +75,15 @@ class BiomassPredictorApp:
|
|
75 |
|
76 |
logger.info(f"Package keys: {list(self.package.keys())}")
|
77 |
logger.info(f"Model expects {n_features} features")
|
|
|
|
|
|
|
|
|
|
|
78 |
except Exception as e:
|
79 |
logger.error(f"Error loading package file: {e}")
|
80 |
# Fallback to default values
|
81 |
-
n_features = 99 #
|
82 |
self.feature_names = [f"feature_{i}" for i in range(n_features)]
|
83 |
|
84 |
# Create a minimal package with essential components
|
@@ -156,28 +161,22 @@ class BiomassPredictorApp:
|
|
156 |
logger.info(f"Processing image: {height}x{width} pixels, {image.shape[0]} bands")
|
157 |
|
158 |
# Validate minimum band count
|
159 |
-
if image.shape[0] <
|
160 |
-
return None, f"Error: Image has
|
161 |
|
162 |
# Generate all features using feature engineering
|
163 |
-
logger.info("Generating features from bands...")
|
164 |
feature_matrix, valid_mask, generated_features = extract_all_features(image)
|
165 |
|
166 |
-
#
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
# If too many features, truncate
|
171 |
-
if feature_matrix.shape[1] > n_expected_features:
|
172 |
-
logger.info(f"Truncating to first {n_expected_features} features")
|
173 |
-
feature_matrix = feature_matrix[:, :n_expected_features]
|
174 |
-
# If too few features, can't proceed
|
175 |
-
else:
|
176 |
-
return None, f"Error: Generated only {feature_matrix.shape[1]} features, but model expects {n_expected_features}."
|
177 |
|
178 |
# Apply feature scaling if available
|
179 |
try:
|
180 |
if 'scaler' in self.package and self.package['scaler'] is not None:
|
|
|
181 |
feature_matrix = self.package['scaler'].transform(feature_matrix)
|
182 |
except Exception as e:
|
183 |
logger.warning(f"Error applying scaler: {e}. Using original features.")
|
@@ -220,7 +219,8 @@ class BiomassPredictorApp:
|
|
220 |
predictions[y_idx, x_idx] = pred
|
221 |
|
222 |
# Log progress
|
223 |
-
|
|
|
224 |
|
225 |
# Create visualization
|
226 |
logger.info("Creating visualization...")
|
@@ -339,9 +339,8 @@ class BiomassPredictorApp:
|
|
339 |
Upload a multi-band satellite image to predict above-ground biomass (AGB) across the landscape.
|
340 |
|
341 |
**Requirements:**
|
342 |
-
- Image must be a GeoTIFF with
|
343 |
-
-
|
344 |
-
- For best results, image bands should match the training data order
|
345 |
""")
|
346 |
|
347 |
with gr.Row():
|
@@ -378,18 +377,17 @@ class BiomassPredictorApp:
|
|
378 |
### Model Details
|
379 |
|
380 |
- Architecture: StableResNet
|
381 |
-
- Input: Multi-spectral satellite imagery
|
382 |
- Output: Above-ground biomass (Mg/ha)
|
383 |
- Creator: najahpokkiri
|
384 |
- Date: 2025-05-17
|
385 |
|
386 |
### How It Works
|
387 |
|
388 |
-
1. The
|
389 |
-
2.
|
390 |
-
3. The
|
391 |
-
4.
|
392 |
-
5. Results are visualized as a heatmap or RGB overlay
|
393 |
""")
|
394 |
|
395 |
# Add a warning if model failed to load
|
@@ -414,7 +412,7 @@ def launch_app():
|
|
414 |
# Create interface
|
415 |
interface = app.create_interface()
|
416 |
|
417 |
-
# Launch interface
|
418 |
interface.launch()
|
419 |
except Exception as e:
|
420 |
logger.error(f"Error launching app: {e}")
|
|
|
1 |
"""
|
2 |
+
Biomass Prediction Gradio App with Exact 99 Features
|
3 |
Author: najahpokkiri
|
4 |
Date: 2025-05-17
|
5 |
|
6 |
+
This updated version ensures the model receives exactly the 99 expected features.
|
7 |
"""
|
8 |
import os
|
9 |
import sys
|
|
|
75 |
|
76 |
logger.info(f"Package keys: {list(self.package.keys())}")
|
77 |
logger.info(f"Model expects {n_features} features")
|
78 |
+
|
79 |
+
# Verify feature count is 99
|
80 |
+
if n_features != 99:
|
81 |
+
logger.warning(f"Warning: Model expects {n_features} features, not the expected 99. This may cause issues.")
|
82 |
+
|
83 |
except Exception as e:
|
84 |
logger.error(f"Error loading package file: {e}")
|
85 |
# Fallback to default values
|
86 |
+
n_features = 99 # We know there are 99 features
|
87 |
self.feature_names = [f"feature_{i}" for i in range(n_features)]
|
88 |
|
89 |
# Create a minimal package with essential components
|
|
|
161 |
logger.info(f"Processing image: {height}x{width} pixels, {image.shape[0]} bands")
|
162 |
|
163 |
# Validate minimum band count
|
164 |
+
if image.shape[0] < 1:
|
165 |
+
return None, f"Error: Image has no bands. Please use multi-band satellite imagery."
|
166 |
|
167 |
# Generate all features using feature engineering
|
168 |
+
logger.info("Generating all 99 features from bands...")
|
169 |
feature_matrix, valid_mask, generated_features = extract_all_features(image)
|
170 |
|
171 |
+
# Verify we have exactly 99 features
|
172 |
+
if feature_matrix.shape[1] != 99:
|
173 |
+
logger.error(f"Error: Generated {feature_matrix.shape[1]} features, but model expects 99.")
|
174 |
+
return None, f"Error: Generated {feature_matrix.shape[1]} features, but model expects 99."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
# Apply feature scaling if available
|
177 |
try:
|
178 |
if 'scaler' in self.package and self.package['scaler'] is not None:
|
179 |
+
logger.info("Applying feature scaling...")
|
180 |
feature_matrix = self.package['scaler'].transform(feature_matrix)
|
181 |
except Exception as e:
|
182 |
logger.warning(f"Error applying scaler: {e}. Using original features.")
|
|
|
219 |
predictions[y_idx, x_idx] = pred
|
220 |
|
221 |
# Log progress
|
222 |
+
if (i // batch_size) % 5 == 0 or end_idx == len(valid_y):
|
223 |
+
logger.info(f"Processed {end_idx}/{len(valid_y)} pixels")
|
224 |
|
225 |
# Create visualization
|
226 |
logger.info("Creating visualization...")
|
|
|
339 |
Upload a multi-band satellite image to predict above-ground biomass (AGB) across the landscape.
|
340 |
|
341 |
**Requirements:**
|
342 |
+
- Image must be a GeoTIFF with spectral bands
|
343 |
+
- For best results, use imagery with at least 59 bands or similar to training data
|
|
|
344 |
""")
|
345 |
|
346 |
with gr.Row():
|
|
|
377 |
### Model Details
|
378 |
|
379 |
- Architecture: StableResNet
|
380 |
+
- Input: Multi-spectral satellite imagery
|
381 |
- Output: Above-ground biomass (Mg/ha)
|
382 |
- Creator: najahpokkiri
|
383 |
- Date: 2025-05-17
|
384 |
|
385 |
### How It Works
|
386 |
|
387 |
+
1. The model extracts features from each pixel in the satellite image
|
388 |
+
2. These features include spectral bands, vegetation indices, texture metrics, and more
|
389 |
+
3. The model outputs a biomass prediction for each pixel
|
390 |
+
4. Results are visualized as a heatmap or RGB overlay
|
|
|
391 |
""")
|
392 |
|
393 |
# Add a warning if model failed to load
|
|
|
412 |
# Create interface
|
413 |
interface = app.create_interface()
|
414 |
|
415 |
+
# Launch interface - Important: no share=True in Hugging Face Spaces
|
416 |
interface.launch()
|
417 |
except Exception as e:
|
418 |
logger.error(f"Error launching app: {e}")
|