Spaces:
Running
Running
Upload app/modelutil.py with huggingface_hub
Browse files- app/modelutil.py +44 -0
app/modelutil.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import os
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras.layers import (Activation, Bidirectional, Conv3D, Dense,
|
| 5 |
+
Dropout, LSTM, MaxPool3D, Reshape)
|
| 6 |
+
from tensorflow.keras.models import Sequential
|
| 7 |
+
|
| 8 |
+
# Disable all GPUS
|
| 9 |
+
tf.config.set_visible_devices([], 'GPU')
|
| 10 |
+
|
| 11 |
+
def load_model() -> Sequential:
|
| 12 |
+
model = Sequential()
|
| 13 |
+
model.add(Conv3D(128, 3, input_shape=(75, 46, 140, 1), padding='same'))
|
| 14 |
+
model.add(Activation('relu'))
|
| 15 |
+
model.add(MaxPool3D((1, 2, 2)))
|
| 16 |
+
|
| 17 |
+
model.add(Conv3D(256, 3, padding='same'))
|
| 18 |
+
model.add(Activation('relu'))
|
| 19 |
+
model.add(MaxPool3D((1, 2, 2)))
|
| 20 |
+
|
| 21 |
+
model.add(Conv3D(75, 3, padding='same'))
|
| 22 |
+
model.add(Activation('relu'))
|
| 23 |
+
model.add(MaxPool3D((1, 2, 2)))
|
| 24 |
+
|
| 25 |
+
# Reshape instead of TimeDistributed(Flatten) — matches your trained weights
|
| 26 |
+
model.add(Reshape((75, 5 * 17 * 75)))
|
| 27 |
+
|
| 28 |
+
model.add(Bidirectional(LSTM(128, kernel_initializer='Orthogonal', return_sequences=True)))
|
| 29 |
+
model.add(Dropout(.5))
|
| 30 |
+
|
| 31 |
+
model.add(Bidirectional(LSTM(128, kernel_initializer='Orthogonal', return_sequences=True)))
|
| 32 |
+
model.add(Dropout(.5))
|
| 33 |
+
|
| 34 |
+
model.add(Dense(41, kernel_initializer='he_normal', activation='softmax'))
|
| 35 |
+
|
| 36 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
| 37 |
+
weights_path = os.path.abspath(
|
| 38 |
+
os.path.join(base_dir, '..', 'models', 'checkpoint.weights.h5')
|
| 39 |
+
)
|
| 40 |
+
if not os.path.exists(weights_path):
|
| 41 |
+
raise FileNotFoundError(f"Model weights not found at: {weights_path}")
|
| 42 |
+
|
| 43 |
+
model.load_weights(weights_path)
|
| 44 |
+
return model
|