DeepGuard_Ai / src /model_optimized.py
abesh-meena's picture
Upload model_optimized.py
943a15e verified
"""
src/model_optimized.py
Memory-optimized model loading for Hugging Face Spaces
Reduces memory usage by 50-70%
"""
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
import gc
# Set memory growth to prevent OOM
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(f"GPU memory setup error: {e}")
# Limit CPU threads for better memory management
tf.config.threading.set_intra_op_parallelism_threads(1)
tf.config.threading.set_inter_op_parallelism_threads(1)
def load_model_optimized(model_path):
"""Load model with memory optimization"""
try:
print(f"Loading model: {model_path}")
# Clear memory before loading
gc.collect()
tf.keras.backend.clear_session()
# Load with custom objects and memory optimization
model = tf.keras.models.load_model(
model_path,
compile=False, # Don't compile to save memory
custom_objects={
'tf': tf,
'keras': keras
}
)
# Optimize model for inference
model.trainable = False
for layer in model.layers:
layer.trainable = False
print(f"Model loaded successfully! Memory optimized.")
return model
except Exception as e:
print(f"Error loading model: {e}")
return None
def predict_memory_efficient(model, input_array):
"""Memory-efficient prediction"""
try:
# Clear memory before prediction
gc.collect()
# Predict with batch size 1
if len(input_array.shape) == 3:
input_array = np.expand_dims(input_array, axis=0)
prediction = model.predict(input_array, batch_size=1, verbose=0)
# Clear memory after prediction
gc.collect()
return prediction
except Exception as e:
print(f"Prediction error: {e}")
return None