this A.i program is doing image classification using ANN model .
ANN mainly learns statistical correlations between pixel values without understanding the spatial structure
CNN learns both statistical patterns and visual structures (edges, shapes, curves),
🔹 ANN (MLP – Multilayer Perceptron)
-
Best for:
-
Tabular data (Excel sheets, databases, financial records, medical measurements).
-
Problems where features are independent values (age, income, temperature, etc.).
-
-
Why:
-
ANN just takes numbers as input and finds correlations between them.
-
There’s no spatial or sequential structure that it needs to preserve.
-
🔹 CNN (Convolutional Neural Network)
-
Best for:
-
Images (clothes, faces, traffic signs).
-
Video frames (action recognition, surveillance).
-
Sound (often converted to spectrogram images, so CNN can analyze patterns).
-
Handwriting / documents (OCR, sign recognition).
-
-
Why:
-
CNNs detect spatial patterns: edges, shapes, curves, textures, objects.
-
They preserve the structure of the data (2D for images, 1D for sound waves).
-
It learns how to look at a picture of clothing (like a shirt, shoe, or bag) and then decide which category it belongs to
-
Input (the picture):
-
The AI gets a 28×28 grayscale image of clothing.
-
-
Processing (hidden layers):
-
Inside the AI (the neural network), there are many “neurons.”
-
Each layer transforms the image data into more useful patterns:
-
First layer finds basic shapes (edges, curves).
-
Next layer combines them into bigger features (like sleeves, soles).
-
Last layer uses these features to make a decision.
-
-
-
Output (the guess):
-
The final layer produces 10 probabilities (one for each clothing type).
-
Example output:
-
1. Training Loss
-
What it is:
How well the model is doing on the training data it sees during learning. -
How it behaves:
-
Should go down steadily as the model learns.
-
If it stays high, the model is not learning well.
-
2. Validation Loss
-
What it is:
How well the model is doing on new data it has never seen before (the validation set). -
Why important:
It shows if the model is generalizing or just memorizing.
3. Comparing Training vs Validation
-
Both decrease together:
→ Model is learning well and generalizing. -
Training loss keeps going down, but validation loss goes up:
→ Model is overfitting (memorizing training data, not generalizing). -
Both losses are high and don’t improve much:
→ Model is underfitting (too simple or not enough training).
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 |
# -*- coding: utf-8 -*- """ Fashion-MNIST classifier with progress messages, category samples, and model saving """ import numpy as np import matplotlib.pyplot as plt from tensorflow import keras from tensorflow.keras import layers # >>> Start loading dataset print("=== Start loading Fashion-MNIST dataset ===") # 1) Load Fashion-MNIST dataset (x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data() # >>> Done loading + summary print("=== Done loading dataset ===") print("Training set:", x_train.shape, y_train.shape) print("Test set:", x_test.shape, y_test.shape) # Clothing class names class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"] # Show one example of each category print("=== Showing 1 example from each category ===") plt.figure(figsize=(12,3)) for i in range(10): idx = np.where(y_train == i)[0][0] # find first index of this class plt.subplot(2,5,i+1) plt.imshow(x_train[idx], cmap="gray") plt.title(class_names[i]) plt.axis("off") plt.suptitle("Fashion-MNIST categories (1 sample each)", fontsize=14) plt.tight_layout() plt.show() # 2) Normalize images to [0,1] print("=== Normalizing images ===") x_train = x_train.astype("float32") / 255.0 x_test = x_test.astype("float32") / 255.0 # 3) Flatten 28x28 → 784 x_train = x_train.reshape((-1, 28*28)) x_test = x_test.reshape((-1, 28*28)) # 4) One-hot encode labels y_train_cat = keras.utils.to_categorical(y_train, 10) y_test_cat = keras.utils.to_categorical(y_test, 10) # 5) Build MLP model print("=== Building model ===") model = keras.Sequential([ layers.Dense(256, activation="relu", input_shape=(784,)), layers.Dropout(0.5), layers.Dense(128, activation="relu"), layers.Dropout(0.5), layers.Dense(10, activation="softmax") ]) # 6) Compile model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) model.summary() # 7) Train model print("=== Start training ===") history = model.fit(x_train, y_train_cat, validation_data=(x_test, y_test_cat), epochs=30, batch_size=128) # 8) Evaluate model print("=== Evaluating model ===") loss, acc = model.evaluate(x_test, y_test_cat) print(f"Test Accuracy: {acc:.4f}") # 9) Save the model model_filename = "fashion_mnist_mlp.h5" model.save(model_filename) print(f"=== Model saved as {model_filename} ===") # 10) Predict first test sample print("=== Predicting sample image ===") sample_idx = 0 sample = x_test[sample_idx].reshape(1, -1) pred = model.predict(sample) pred_label = np.argmax(pred) print("Predicted:", class_names[pred_label], "True:", class_names[y_test[sample_idx]]) # 11) Plot training history plt.figure(figsize=(12,4)) plt.subplot(1,2,1) plt.plot(history.history["loss"], label="train loss") plt.plot(history.history["val_loss"], label="val loss") plt.title("Loss") plt.legend() plt.subplot(1,2,2) plt.plot(history.history["accuracy"], label="train acc") plt.plot(history.history["val_accuracy"], label="val acc") plt.title("Accuracy") plt.legend() plt.show() |
Problem:
A big neural network can memorize the training data instead of learning patterns. This is called overfittin
-
Solution (Dropout):
During training, Dropout randomly turns off some neurons.
Example: In a layer with 10 neurons, maybe only 5 work each step. -
Effect:
-
The network must learn backup detectors.
If neuron A is off, neuron B must still catch the feature (like detecting an edge).
-
It stops the model from depending on a few strong neurons only.
-
Feels like you’re training many smaller networks and combining them.
-
👉 Result: The model works better on new, unseen data (not just training data).
Prediction :
It loads your trained Fashion-MNIST model, opens a new image (like a shirt or shoe), resizes it to 28×28, preprocesses it so it matches the training format, and then asks the model to predict which clothing category it belongs to.
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 |
# -*- coding: utf-8 -*- """ Load trained Fashion-MNIST model and predict category for a new image """ import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.models import load_model from PIL import Image import os # Category names (same order as training) class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"] # 1) Load trained model model_filename = "fashion_mnist_mlp.h5" print(f"=== Loading model: {model_filename} ===") model = load_model(model_filename) print("=== Model loaded successfully ===") # 2) Load new image from fixed path image_path = r"D:\temp\test1.jpg" print(f"=== Loading new image: {image_path} ===") if not os.path.exists(image_path): print(f"ERROR: File not found at {image_path}") else: # Open image, convert to grayscale, resize to 28x28 img = Image.open(image_path).convert("L") # "L" = grayscale img_resized = img.resize((28, 28)) # >>> Invert colors: background → black, object → white img_array = 255 - np.array(img_resized) # Show the processed (inverted) image plt.imshow(img_array, cmap="gray") plt.title("Processed input (inverted, 28x28)") plt.axis("off") plt.show() # 3) Prepare image for model img_array = img_array.astype("float32") / 255.0 # normalize [0,1] img_flat = img_array.reshape(1, 28*28) # flatten into 784 values # 4) Predict with the model pred = model.predict(img_flat) pred_label = np.argmax(pred) pred_prob = np.max(pred) # 5) Print result print("=== Prediction Result ===") print(f"Category number: {pred_label}") print(f"Category name: {class_names[pred_label]}") print(f"Confidence: {pred_prob:.4f}") |
:CNN
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 |
# -*- coding: utf-8 -*- """ Fashion-MNIST CNN classifier with progress messages, category samples, and model saving """ import numpy as np import matplotlib.pyplot as plt from tensorflow import keras from tensorflow.keras import layers # >>> Start loading dataset print("=== Start loading Fashion-MNIST dataset ===") # 1) Load Fashion-MNIST dataset (28x28 grayscale clothing images) (x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data() # >>> Done loading + summary print("=== Done loading dataset ===") print("Training set:", x_train.shape, y_train.shape) # (60000, 28, 28), (60000,) print("Test set:", x_test.shape, y_test.shape) # (10000, 28, 28), (10000,) # Clothing class names for labels 0–9 class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"] # 2) Show one example image of each category print("=== Showing 1 example from each category ===") plt.figure(figsize=(12,3)) for i in range(10): idx = np.where(y_train == i)[0][0] # find first index of class i plt.subplot(2,5,i+1) plt.imshow(x_train[idx], cmap="gray") plt.title(class_names[i]) plt.axis("off") plt.suptitle("Fashion-MNIST categories (1 sample each)", fontsize=14) plt.tight_layout() plt.show() # 3) Normalize images to [0,1] (important for stable training) print("=== Normalizing images ===") x_train = x_train.astype("float32") / 255.0 x_test = x_test.astype("float32") / 255.0 # 4) Reshape to CNN format: (samples, height, width, channels) # Channels=1 because grayscale; CNN expects 4D input x_train = x_train.reshape((-1, 28, 28, 1)) x_test = x_test.reshape((-1, 28, 28, 1)) # 5) One-hot encode labels (e.g., 3 → [0,0,0,1,0,0,0,0,0,0]) y_train_cat = keras.utils.to_categorical(y_train, 10) y_test_cat = keras.utils.to_categorical(y_test, 10) # 6) Build CNN model print("=== Building CNN model ===") model = keras.Sequential([ # First convolutional block layers.Conv2D(32, (3,3), activation="relu", input_shape=(28,28,1)), # Learn 32 filters of size 3x3 layers.MaxPooling2D((2,2)), # Downsample: reduces 28x28 → 14x14 # Second convolutional block layers.Conv2D(64, (3,3), activation="relu"), # Learn 64 filters layers.MaxPooling2D((2,2)), # Downsample: 14x14 → 7x7 # Flatten to prepare for fully connected layers layers.Flatten(), # Fully connected (dense) layer layers.Dense(128, activation="relu"), # Dropout: randomly turn off 50% neurons → reduce overfitting layers.Dropout(0.5), # Output layer: 10 classes with softmax probabilities layers.Dense(10, activation="softmax") ]) # 7) Compile model (choose optimizer, loss, metrics) model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) # Print model summary model.summary() # 8) Train the model print("=== Start training ===") history = model.fit( x_train, y_train_cat, validation_data=(x_test, y_test_cat), # check generalization epochs=15, # CNN converges faster → fewer epochs batch_size=128, # update after 128 samples verbose=1 ) # 9) Evaluate model on test data print("=== Evaluating model ===") loss, acc = model.evaluate(x_test, y_test_cat) print(f"Test Accuracy: {acc:.4f}") # 10) Save trained model model_filename = "fashion_mnist_cnn.h5" model.save(model_filename) print(f"=== Model saved as {model_filename} ===") # 11) Predict one test sample print("=== Predicting sample image ===") sample_idx = 0 sample = x_test[sample_idx].reshape(1, 28, 28, 1) # single test image pred = model.predict(sample) pred_label = np.argmax(pred) print("Predicted:", class_names[pred_label], "True:", class_names[y_test[sample_idx]]) # Show predicted image plt.imshow(x_test[sample_idx].reshape(28,28), cmap="gray") plt.title(f"Predicted: {class_names[pred_label]} | True: {class_names[y_test[sample_idx]]}") plt.axis("off") plt.show() # 12) Plot training history (loss & accuracy) plt.figure(figsize=(12,4)) plt.subplot(1,2,1) plt.plot(history.history["loss"], label="train loss") plt.plot(history.history["val_loss"], label="val loss") plt.title("Loss") plt.legend() plt.subplot(1,2,2) plt.plot(history.history["accuracy"], label="train acc") plt.plot(history.history["val_accuracy"], label="val acc") plt.title("Accuracy") plt.legend() plt.show() |
Prediction cnn :
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 |
# -*- coding: utf-8 -*- """ Load trained CNN Fashion-MNIST model and predict category for a new image """ import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.models import load_model from PIL import Image import os # Class names (Fashion-MNIST) class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"] # 1) Load trained CNN model model_filename = "fashion_mnist_cnn.h5" print(f"=== Loading model: {model_filename} ===") model = load_model(model_filename) print("=== Model loaded successfully ===") # 2) Path to image image_path = r"D:\temp\test1.jpg" print(f"=== Loading new image: {image_path} ===") if not os.path.exists(image_path): print(f"ERROR: File not found at {image_path}") else: # 3) Open image, convert to grayscale, resize to 28x28 img = Image.open(image_path).convert("L") # grayscale img_resized = img.resize((28, 28)) # 4) Invert colors → black background, white object img_array = 255 - np.array(img_resized) # Show processed image plt.imshow(img_array, cmap="gray") plt.title("Processed Input (28x28, black background)") plt.axis("off") plt.show() # 5) Normalize to [0,1] and reshape for CNN img_array = img_array.astype("float32") / 255.0 img_ready = img_array.reshape(1, 28, 28, 1) # 6) Predict with CNN model pred = model.predict(img_ready) pred_label = np.argmax(pred) pred_prob = np.max(pred) # 7) Print result print("=== Prediction Result ===") print(f"Category number: {pred_label}") print(f"Category name: {class_names[pred_label]}") print(f"Confidence: {pred_prob:.4f}") |
=== Prediction Result ===
Category number: 0
Category name: T-shirt/top
Confidence: 0.8358