Artificial Intelligence Mini Project
Artificial Intelligence Mini Project
Project
Project Description: In this mini project, you will learn how to build an image classifier using
Convolutional Neural Networks (CNNs) - a type of neural network commonly used for image
recognition tasks. You will use Python and popular deep-learning libraries like Keras to create a
CNN model that can label handwritten digits correctly.
Steps Involved:
1. Data Preparation: You will need a dataset of labeled images to train and test your
model. In this project, we will use the popular MNIST dataset. You will preprocess the
data by resizing the images, normalizing pixel values, and splitting the data into training
and testing sets.
2. Building the CNN Model: You will build a CNN model using Keras You will define the
architecture of the model, which includes convolutional layers, pooling layers, and fully
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Dropout
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.utils import to_categorical
plt.subplot(1,10,1)
plt.imshow(X_train[0])
plt.subplot(1,10,2)
plt.imshow(X_train[1])
plt.subplot(1,10,3)
plt.imshow(X_train[2])
plt.subplot(1,10,4)
plt.imshow(X_train[3])
plt.subplot(1,10,5)
plt.figure(figsize=(20,20))
plt.subplot(1,10,1)
plt.imshow(X_train[150])
plt.subplot(1,10,2)
plt.imshow(X_train[162])
plt.subplot(1,10,3)
plt.imshow(X_train[178])
plt.subplot(1,10,4)
plt.imshow(X_train[193])
plt.subplot(1,10,5)
plt.imshow(X_train[205])
plt.subplot(1,10,6)
plt.imshow(X_train[3978])
plt.subplot(1,10,7)
plt.imshow(X_train[456])
plt.subplot(1,10,8)
plt.imshow(X_train[7896])
plt.subplot(1,10,9)
plt.imshow(X_train[57])
plt.subplot(1,10,10)
plt.imshow(X_train[31897])
#Data Preprocessing
print("Shape of X_train : ", X_train.shape)
print("Shape of X_test : ", X_test.shape)
input_shape = (28,28,1)
#One-Hot Encoding
y_cat_train = to_categorical(y_train, 10)
y_cat_train[0:11]
X_train /= 255
X_test /= 255
#Model Creation
model = Sequential()
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy'])
model.summary()
#Early Stopping
early_stop = EarlyStopping(monitor='val_loss', patience=2)
#Model Training
model.fit(X_train, y_cat_train, epochs=50, callbacks = [early_stop], validation_data=(X_test,
y_cat_test))
#Model Performance
training_metrics = pd.DataFrame(model.history.history)
training_metrics.head()
training_metrics[['loss','val_loss']].plot()
training_metrics[['accuracy','val_accuracy']].plot()
np.argmax(model.predict(new_img.reshape(1,28,28,1)), axis=-1)
new_img2 = X_test[0]
plt.imshow(new_img2)
Y_test[0]
np.argmax(model.predict(new_img2.reshape(1,28,28,1)), axis=-1)
new_img3 = X_test[397]
plt.imshow(new_img3)
y_test[397]
—----------------------------------------------------------------------------------------------------------------------------
This mini project will give you a hands-on experience with building and training a CNN model for
image classification, one of the most popular applications of artificial intelligence. You can also
extend the project by experimenting with different datasets, architectures, and techniques to
improve the model's performance.