-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassifyDig.py
More file actions
299 lines (253 loc) · 9.66 KB
/
Copy pathClassifyDig.py
File metadata and controls
299 lines (253 loc) · 9.66 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
# Print all files in the input directory
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
# Import TensorFlow with error handling
try:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Set GPU Memory
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 configuration warning: {e}")
print(f"TensorFlow version: {tf.__version__}")
except Exception as e:
print(f"Error importing TensorFlow: {e}")
exit(1)
# Load the dataset
print("\nLoading dataset...")
try:
data = pd.read_csv('mnist_train.csv')
print(f"Dataset loaded successfully: {len(data)} samples")
except FileNotFoundError:
print("ERROR: train.csv not found!")
print("Please download it from: https://www.kaggle.com/datasets/oddrationale/mnist-in-csv")
exit(1)
# Separate features and labels
y = data['label'].values
X = data.drop('label', axis=1).values
# Normalize pixel values to 0-1 range
X = X.astype('float32') / 255.0
# Reshape to 28 x 28 images
X = X.reshape(-1, 28, 28, 1)
print(f"Input shape: {X.shape}")
print(f"Labels shape: {y.shape}")
print(f"Unique labels: {np.unique(y)}")
# Split into training and validation sets
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
print(f"\nTraining samples: {len(X_train)}")
print(f"Validation samples: {len(X_val)}")
# Build the model
print("\nBuilding model...")
model = keras.Sequential([
layers.Input(shape=(28, 28, 1)),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dropout(0.2),
layers.Dense(10, activation='softmax')
], name='MNIST_Classifier')
# Compile the model
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# Display model architecture
print("\nModel Architecture:")
model.summary()
# Train the model
print("\nTraining model...")
try:
history = model.fit(
X_train, y_train,
epochs=5,
batch_size=128,
validation_data=(X_val, y_val),
verbose=1
)
except Exception as e:
print(f"Training error: {e}")
print("\nTrying with smaller batch size...")
history = model.fit(
X_train, y_train,
epochs=5,
batch_size=32,
validation_data=(X_val, y_val),
verbose=1
)
# Evaluate the model
print("\nEvaluating model...")
test_loss, test_accuracy = model.evaluate(X_val, y_val, verbose=0)
print(f"Validation Loss: {test_loss:.4f}")
print(f"Validation Accuracy: {test_accuracy * 100:.2f}%")
# Plot training history
print("\nGenerating training plots...")
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy', marker='o')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy', marker='s')
plt.title('Model Accuracy Over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.grid(True, alpha=0.3)
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss', marker='o')
plt.plot(history.history['val_loss'], label='Validation Loss', marker='s')
plt.title('Model Loss Over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('training_history.png', dpi=150)
print("Training history plot saved as 'training_history.png'")
# Make predictions on sample images
print("\nMaking predictions on sample images...")
sample_indices = np.random.choice(len(X_val), 10, replace=False)
sample_images = X_val[sample_indices]
sample_labels = y_val[sample_indices]
predictions = model.predict(sample_images, verbose=0)
predicted_labels = np.argmax(predictions, axis=1)
confidence_scores = np.max(predictions, axis=1)
# Display sample predictions
plt.figure(figsize=(15, 3))
for i in range(10):
plt.subplot(2, 5, i + 1)
plt.imshow(sample_images[i].reshape(28, 28), cmap='gray')
color = 'green' if sample_labels[i] == predicted_labels[i] else 'red'
plt.title(f"True: {sample_labels[i]} | Pred: {predicted_labels[i]}\n"
f"Confidence: {confidence_scores[i]:.2%}",
color=color, fontsize=9)
plt.axis('off')
plt.tight_layout()
plt.savefig('sample_predictions.png', dpi=150)
print("Sample predictions saved as 'sample_predictions.png'")
# Save the model
print("\nSaving model...")
try:
model.save('mnist_digit_classifier.keras') # Use .keras format (recommended)
print("Model saved as 'mnist_digit_classifier.keras'")
except Exception:
model.save('mnist_digit_classifier.h5') # Fallback to .h5 format
print("Model saved as 'mnist_digit_classifier.h5'")
# Print final summary
print("\n" + "=" * 50)
print("TRAINING COMPLETE")
print("=" * 50)
print(f"Final Validation Accuracy: {test_accuracy * 100:.2f}%")
print(f"Final Validation Loss: {test_loss:.4f}")
print(f"Total Parameters: {model.count_params():,}")
print("=" * 50)
# Show plots
plt.show()
# TEST MODEL
print("\n" + "=" * 50)
print("TESTING MODEL WITH TEST DATASET")
print("=" * 50)
try:
print("\nLoading test dataset...")
print("Download test.csv from: https://www.kaggle.com/datasets/oddrationale/mnist-in-csv")
test_data = pd.read_csv('mnist_test.csv')
print(f"Test dataset loaded: {len(test_data)} samples")
# Check if test data has labels or not
if 'label' in test_data.columns:
# Test data with labels
y_test = test_data['label'].values
X_test = test_data.drop('label', axis=1).values
has_labels = True
else:
# Test data without labels (competition format)
X_test = test_data.values
has_labels = False
# Preprocess test data
X_test = X_test.astype('float32') / 255.0
X_test = X_test.reshape(-1, 28, 28, 1)
print(f"Test data shape: {X_test.shape}")
# Make predictions
print("\nMaking predictions on test data...")
test_predictions = model.predict(X_test, verbose=1)
predicted_test_labels = np.argmax(test_predictions, axis=1)
if has_labels:
# Calculate test accuracy if labels are available
test_acc = np.mean(predicted_test_labels == y_test)
print(f"\nTest Accuracy: {test_acc * 100:.2f}%")
# Show confusion matrix
from sklearn.metrics import confusion_matrix, classification_report
import seaborn as sns
cm = confusion_matrix(y_test, predicted_test_labels)
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=range(10), yticklabels=range(10))
plt.title('Confusion Matrix - Test Data')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.tight_layout()
plt.savefig('confusion_matrix.png', dpi=150)
print("Confusion matrix saved as 'confusion_matrix.png'")
print("\nClassification Report:")
print(classification_report(y_test, predicted_test_labels,
target_names=[str(i) for i in range(10)]))
# Visualize some test predictions
plt.figure(figsize=(15, 6))
sample_test_indices = np.random.choice(len(X_test), 15, replace=False)
for i, idx in enumerate(sample_test_indices):
plt.subplot(3, 5, i + 1)
plt.imshow(X_test[idx].reshape(28, 28), cmap='gray')
color = 'green' if y_test[idx] == predicted_test_labels[idx] else 'red'
plt.title(f"True: {y_test[idx]} | Pred: {predicted_test_labels[idx]}",
color=color, fontsize=8)
plt.axis('off')
plt.suptitle('Test Set Predictions', fontsize=14, y=1.02)
plt.tight_layout()
plt.savefig('test_predictions.png', dpi=150)
print("Test predictions saved as 'test_predictions.png'")
plt.show()
else:
# Save predictions to CSV (competition format)
print("\nNo labels found in test data. Saving predictions to CSV...")
submission = pd.DataFrame({
'ImageId': range(1, len(predicted_test_labels) + 1),
'Label': predicted_test_labels
})
submission.to_csv('predictions.csv', index=False)
print("Predictions saved as 'predictions.csv'")
# Visualize random test predictions
plt.figure(figsize=(15, 6))
sample_test_indices = np.random.choice(len(X_test), 15, replace=False)
for i, idx in enumerate(sample_test_indices):
plt.subplot(3, 5, i + 1)
plt.imshow(X_test[idx].reshape(28, 28), cmap='gray')
confidence = test_predictions[idx][predicted_test_labels[idx]]
plt.title(f"Pred: {predicted_test_labels[idx]}\n"
f"Conf: {confidence:.2%}", fontsize=8)
plt.axis('off')
plt.suptitle('Test Set Predictions (No Labels)', fontsize=14, y=1.02)
plt.tight_layout()
plt.savefig('test_predictions.png', dpi=150)
print("Test predictions saved as 'test_predictions.png'")
plt.show()
print("\n" + "=" * 50)
print("TESTING COMPLETE")
print("=" * 50)
except FileNotFoundError:
print("\ntest.csv not found!")
print("Download it from: https://www.kaggle.com/datasets/oddrationale/mnist-in-csv")
print("Place it in the same directory as this script.")
except Exception as e:
print(f"\nError during testing: {e}")
print("Make sure test.csv is properly formatted.")