deep learning image classification
DEEP LEARNING, AAN, IMAGE CLASSIFICATION
Step 1: Import Libraries
import numpy as np import matplotlib.pyplot as plt import seaborn as sns from tensorflow.keras.datasets import mnist from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Flatten, Dense, Dropout from tensorflow.keras.utils import to_categorical from sklearn.metrics import confusion_matrix, classification_report
Step 2: Load the Data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
Step 3: Preprocess the Data
X_train = X_train / 255.0 # Normalize pixel values to [0,1] X_test = X_test / 255.0 y_train_cat = to_categorical(y_train, 10) # One-hot encode labels y_test_cat = to_categorical(y_test, 10)
Plot some digits from dataset
selected_indices = [10, 25, 75, 300, 501, 999, 1234, 1500, 1999] # Choose which image indices to display
plt.figure(figsize=(8, 8))
for i, idx in enumerate(selected_indices): plt.subplot(3, 3, i + 1) plt.imshow(X_train[idx], cmap=’gray’) plt.title(f”Label: {y_train[idx]} (Index: {idx})”) plt.axis(‘off’)
plt.tight_layout() plt.show()
Step 4: Build the ANN Model
model = Sequential([ Flatten(input_shape=(28, 28)), Dense(128, activation=’relu’), Dropout(0.3), Dense(64, activation=’relu’), Dropout(0.3), Dense(10, activation=’softmax’) ])
Step 5: Compile the Model
model.compile(optimizer=’adam’, loss=’categorical_crossentropy’, metrics=[‘accuracy’])
Step 6: Train the Model
history = model.fit(X_train, y_train_cat, epochs=10, batch_size=128, validation_split=0.1)
Step 7: Evaluate on Test Set
test_loss, test_accuracy = model.evaluate(X_test, y_test_cat) print(f”Test Accuracy: {test_accuracy:.4f}”)
Step 8: Visualize Training History
plt.plot(history.history[‘accuracy’], label=’Train Acc’) plt.plot(history.history[‘val_accuracy’], label=’Val Acc’) plt.title(‘Model Accuracy’) plt.xlabel(‘Epoch’) plt.ylabel(‘Accuracy’) plt.legend() plt.show()
Step 9: Confusion Matrix
y_pred = model.predict(X_test) y_pred_classes = np.argmax(y_pred, axis=1)
cm = confusion_matrix(y_test, y_pred_classes) plt.figure(figsize=(8, 6)) sns.heatmap(cm, annot=True, fmt=’d’, cmap=’Blues’) plt.xlabel(‘Predicted’) plt.ylabel(‘True’) plt.title(‘Confusion Matrix’) plt.show()
Step 10: Classification Report
print(classification_report(y_test, y_pred_classes))
Step 11: Save and Reload the Model
model.save(“mnist_ann_model.h5”)
from tensorflow.keras.models import load_model reloaded_model = load_model(“mnist_ann_model.h5”) reloaded_model.evaluate(X_test, y_test_cat)