forked from ravin-d-27/PyDeepFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn_runner.py
More file actions
96 lines (75 loc) · 3.11 KB
/
cnn_runner.py
File metadata and controls
96 lines (75 loc) · 3.11 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# cnn_runner.py - Digits Image Classification Demo (using sklearn, no TensorFlow)
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn.datasets import load_digits
from sklearn.metrics import accuracy_score
# Import the new integrated class
from pydeepflow.model import Multi_Layer_ANN, ConvLayer, Flatten, Plotting_Utils, Multi_Layer_CNN
from pydeepflow.activations import activation
# --- Data Loading Function ---
def load_and_preprocess_digits():
digits = load_digits()
X = digits.images # (1797, 8, 8)
y = digits.target.reshape(-1, 1)
# Normalize pixels 0-16 -> 0-1
X = X.astype("float32") / 16.0
# Reshape to CNN format (N, H, W, C)
X = X.reshape((-1, 8, 8, 1))
# One-hot encode labels
encoder = OneHotEncoder(sparse_output=False)
y = encoder.fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
return X_train, y_train, X_test, y_test
# --- CNN Model Demo (Uses Multi_Layer_CNN) ---
if __name__ == "__main__":
print("Loading and preparing Digits dataset...")
X_train, y_train, X_test, y_test = load_and_preprocess_digits()
# Split for validation
X_val = X_train[-100:]
y_val = y_train[-100:]
X_train = X_train[:-100]
y_train = y_train[:-100]
# --- DEFINE THE FULL SEQUENTIAL ARCHITECTURE ---
# Conv -> Conv -> Flatten -> Dense -> Dense
cnn_layers_config = [
# First convolutional layer: 8x8x1 -> 6x6x16
{'type': 'conv', 'out_channels': 16, 'kernel_size': 3, 'stride': 1, 'padding': 0},
# Second convolutional layer: 6x6x16 -> 4x4x32
{'type': 'conv', 'out_channels': 32, 'kernel_size': 3, 'stride': 1, 'padding': 0},
# Flatten: 4x4x32 -> 512
{'type': 'flatten'},
# First dense layer: 512 -> 64
{'type': 'dense', 'neurons': 64, 'activation': 'relu'},
# Output layer: 64 -> 10 (digits 0-9)
{'type': 'dense', 'neurons': 10, 'activation': 'softmax'}
]
print("\nInitializing Multi_Layer_CNN Model...")
# Instantiate the new integrated model
model = Multi_Layer_CNN(
layers_list=cnn_layers_config,
X_train=X_train,
Y_train=y_train,
loss='categorical_crossentropy',
optimizer='adam'
)
# Train the network end-to-end
print("\nStarting integrated training for 50 epochs...")
model.fit(
epochs=50,
learning_rate=0.01,
verbose=True,
X_val=X_val,
y_val=y_val
)
# Final test set prediction (The model handles the Conv/Flatten/Dense chain internally)
y_pred = model.predict(X_test)
y_pred_classes = np.argmax(y_pred, axis=1)
y_true_classes = np.argmax(y_test, axis=1)
test_accuracy = accuracy_score(y_true_classes, y_pred_classes) * 100
print(f"\n✅ Final Test Accuracy: {test_accuracy:.2f}%")
# Optional: plot training history
plot_util = Plotting_Utils()
plot_util.plot_training_history(model.history, metrics=('loss', 'accuracy'), figure='training_history.png')