Spaces:
Sleeping
Sleeping
ahad_dev
#12
by
ahadhassan - opened
- .gitattributes +1 -0
- app.py +1 -1
- ndvi_best_model/best_model_weights.weights.h5 +3 -0
- ndvi_best_model/model_architecture.json +0 -0
- ndvi_predictor.py +43 -4
- requirements.txt +2 -1
.gitattributes
CHANGED
|
@@ -39,3 +39,4 @@ modified_ultralytics/assets/bus.jpg filter=lfs diff=lfs merge=lfs -text
|
|
| 39 |
ultralytics/assets/bus.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
ultralytics/engine/__pycache__/exporter.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 41 |
ultralytics/data/__pycache__/augment.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 39 |
ultralytics/assets/bus.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
ultralytics/engine/__pycache__/exporter.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 41 |
ultralytics/data/__pycache__/augment.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
ndvi_best_model/best_model_weights.weights.h5 filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -22,7 +22,7 @@ app = FastAPI()
|
|
| 22 |
|
| 23 |
# Load models at startup
|
| 24 |
try:
|
| 25 |
-
ndvi_model = load_model("ndvi_best_model
|
| 26 |
logger.info("NDVI model loaded successfully")
|
| 27 |
except Exception as e:
|
| 28 |
logger.error(f"Failed to load NDVI model: {e}")
|
|
|
|
| 22 |
|
| 23 |
# Load models at startup
|
| 24 |
try:
|
| 25 |
+
ndvi_model = load_model("ndvi_best_model")
|
| 26 |
logger.info("NDVI model loaded successfully")
|
| 27 |
except Exception as e:
|
| 28 |
logger.error(f"Failed to load NDVI model: {e}")
|
ndvi_best_model/best_model_weights.weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:25bd4b4ffc358dc1263432b1c2e562e5920cc227cc28cdff1ad202a9084a95ab
|
| 3 |
+
size 172269912
|
ndvi_best_model/model_architecture.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
ndvi_predictor.py
CHANGED
|
@@ -1,18 +1,57 @@
|
|
| 1 |
# ndvi_predictor.py
|
| 2 |
import os
|
| 3 |
-
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
| 4 |
os.environ["SM_FRAMEWORK"] = "tf.keras"
|
| 5 |
import segmentation_models as sm
|
| 6 |
import tensorflow as tf
|
|
|
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
import rasterio
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
from PIL import Image
|
| 11 |
import io
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def normalize_rgb(rgb):
|
| 18 |
"""Normalize RGB image to [0, 1] range using percentile normalization"""
|
|
|
|
| 1 |
# ndvi_predictor.py
|
| 2 |
import os
|
| 3 |
+
# os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
| 4 |
os.environ["SM_FRAMEWORK"] = "tf.keras"
|
| 5 |
import segmentation_models as sm
|
| 6 |
import tensorflow as tf
|
| 7 |
+
from tensorflow.keras.models import model_from_json
|
| 8 |
+
from efficientnet.tfkeras import EfficientNetB2
|
| 9 |
import numpy as np
|
| 10 |
import rasterio
|
| 11 |
import matplotlib.pyplot as plt
|
| 12 |
from PIL import Image
|
| 13 |
import io
|
| 14 |
|
| 15 |
+
# Custom loss functions and activation functions
|
| 16 |
+
def balanced_mse_loss(y_true, y_pred):
|
| 17 |
+
mse = tf.square(y_true - y_pred)
|
| 18 |
+
negative_weight = tf.where(y_true < -0.2, 1.5, 1.0)
|
| 19 |
+
boundary_weight = tf.where(tf.abs(y_true) > 0.5, 1.5, 1.0)
|
| 20 |
+
weights = negative_weight * boundary_weight
|
| 21 |
+
weighted_mse = weights * mse
|
| 22 |
+
return tf.reduce_mean(mse)
|
| 23 |
+
|
| 24 |
+
def custom_mae(y_true, y_pred):
|
| 25 |
+
mae = tf.abs(y_true - y_pred)
|
| 26 |
+
return tf.reduce_mean(mae)
|
| 27 |
+
|
| 28 |
+
def load_model(models_dir):
|
| 29 |
+
"""Load NDVI prediction model with custom objects"""
|
| 30 |
+
# Define custom objects dictionary
|
| 31 |
+
custom_objects = {
|
| 32 |
+
'balanced_mse_loss': balanced_mse_loss,
|
| 33 |
+
'custom_mae': custom_mae
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Load model architecture
|
| 37 |
+
with open(os.path.join(models_dir, "model_architecture.json"), "r") as json_file:
|
| 38 |
+
model_json = json_file.read()
|
| 39 |
+
|
| 40 |
+
model = model_from_json(model_json, custom_objects=custom_objects)
|
| 41 |
+
|
| 42 |
+
# Load weights
|
| 43 |
+
model.load_weights(os.path.join(models_dir, "best_model_weights.weights.h5"))
|
| 44 |
+
|
| 45 |
+
# Compile model with custom functions
|
| 46 |
+
optimizer = tf.keras.optimizers.AdamW(learning_rate=0.0005, weight_decay=1e-4)
|
| 47 |
+
|
| 48 |
+
model.compile(
|
| 49 |
+
optimizer=optimizer,
|
| 50 |
+
loss=balanced_mse_loss,
|
| 51 |
+
metrics=[custom_mae, 'mse']
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
return model
|
| 55 |
|
| 56 |
def normalize_rgb(rgb):
|
| 57 |
"""Normalize RGB image to [0, 1] range using percentile normalization"""
|
requirements.txt
CHANGED
|
@@ -10,4 +10,5 @@ numpy
|
|
| 10 |
matplotlib
|
| 11 |
torch
|
| 12 |
torchvision
|
| 13 |
-
tifffile
|
|
|
|
|
|
| 10 |
matplotlib
|
| 11 |
torch
|
| 12 |
torchvision
|
| 13 |
+
tifffile
|
| 14 |
+
efficientnet
|