-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
52 lines (43 loc) · 1.41 KB
/
train.py
File metadata and controls
52 lines (43 loc) · 1.41 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
import pandas as pd
from src.models.cnn_model import build_model
from sklearn.utils.class_weight import compute_class_weight
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# 1. load the pre-split data
data = np.load("data/dataset.npz")
X_train, X_test = data["X_train"], data["X_test"]
y_train, y_test = data["y_train"], data["y_test"]
# 2. compute class weights
# this will give higher weight to the minority class
classes = np.unique(y_train)
weights = compute_class_weight(
class_weight="balanced",
classes=classes,
y=y_train
)
class_weights = dict(zip(classes, weights))
print("Class weights:", class_weights)
# 3. build and train the model
model = build_model(input_shape=(64, 64, 1))
datagen = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
horizontal_flip=True
)
datagen.fit(X_train)
history = model.fit(
datagen.flow(X_train, y_train, batch_size=32),
epochs=10,
validation_data=(X_test, y_test),
class_weight=class_weights
)
# 4. save the trained model
model.save("src/models/best_model.h5")
# 5. save training history for plotting
pd.DataFrame(history.history).to_csv("results/history.csv", index=False)
print("Model and history saved in results/")
# 6. evaluate on test set
loss, acc = model.evaluate(X_test, y_test)
print(f"Test Loss: {loss:.4f}, Test Accuracy: {acc:.4f}")