-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathani_train.py
More file actions
33 lines (26 loc) · 1.04 KB
/
ani_train.py
File metadata and controls
33 lines (26 loc) · 1.04 KB
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
import numpy as np
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from tensorflow.keras.utils import to_categorical
# Load data
X = np.load("X_animals.npy")
y = np.load("y_animals.npy")
# Encode labels
le = LabelEncoder()
y_encoded = le.fit_transform(y)
y_categorical = to_categorical(y_encoded)
# LSTM Model
model = Sequential()
model.add(LSTM(64, return_sequences=True, activation='relu', input_shape=(30, 126)))
model.add(LSTM(128, return_sequences=False, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(y_categorical.shape[1], activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train
model.fit(X, y_categorical, epochs=40, batch_size=16, validation_split=0.2)
# Save model and labels
model.save("animal_signs_model.h5")
np.save("animal_label_classes.npy", le.classes_)
print("✅ Model Trained and Saved!")