File size: 2,137 Bytes
943a15e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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