Assignment_1_YagnamSukhadia_COMP_263_301239519

docx

School

Centennial College *

*We aren’t endorsed by this school

Course

263

Subject

Information Systems

Date

Feb 20, 2024

Type

docx

Pages

15

Uploaded by DeaconDonkeyMaster471

Report
# Import necessary libraries import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, LSTM # a. Get the data (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.fashion_mnist.load_data() train_yagnam = {'images': train_images, 'labels': train_labels} test_yagnam = {'images': test_images, 'labels': test_labels} # b. Initial Exploration # 1. Display the size of the training and testing dataset print("Size of the training dataset:", len(train_yagnam['images'])) print("Size of the testing dataset:", len(test_yagnam['images'])) # 2. Display the image resolution (dimension) of the input images image_resolution = train_yagnam['images'].shape[1:] print("Image Resolution (Dimension):", image_resolution) # 3. Display the largest pixel value in the dataset using numpy.amax() largest_pixel_value = np.amax(train_yagnam['images']) print("Largest Pixel Value in the Dataset:", largest_pixel_value)
# c. Data Pre-preprocessing # 1. Normalize pixel values to a range between 0-1 train_yagnam['images'] = train_yagnam['images'] / 255.0 test_yagnam['images'] = test_yagnam['images'] / 255.0 # 2. One-hot encode the labels train_yagnam['labels'] = tf.keras.utils.to_categorical(train_yagnam['labels']) test_yagnam['labels'] = tf.keras.utils.to_categorical(test_yagnam['labels']) # 3. Display the shape of train_yagnam['labels'] and test_yagnam['labels'] print("Shape of train_yagnam['labels']:", train_yagnam['labels'].shape) print("Shape of test_yagnam['labels']:", test_yagnam['labels'].shape) # Number of possible labels num_possible_labels = train_yagnam['labels'].shape[1] print("Number of Possible Labels in the Dataset:", num_possible_labels) # d. Visualization # 1. Create a function to display an image with its true label def display_image(image, true_label): plt.imshow(image, cmap='gray') plt.title(f'True Label: {true_label}') plt.axis('off') plt.show() # 2. Plot the first 12 data samples in the training dataset fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(8, 8)) for i in range(12):
ax = axes[i // 3, i % 3] display_image(train_yagnam['images'][i], true_label=np.argmax(train_yagnam['labels'][i])) ax.axis('off') plt.tight_layout() plt.show() # e. Training Data Preparation # 1. Split the training dataset using Sklearn's train_test_split x_train_yagnam, x_val_yagnam, y_train_yagnam, y_val_yagnam = train_test_split( train_yagnam['images'], train_yagnam['labels'], test_size=0.2, random_state=19 ) # f. Build, Train, and Validate CNN Model # Assuming your original image shape is (28, 28) original_image_shape = (28, 28) # Reshape the input data to add a channel dimension x_train_yagnam = x_train_yagnam.reshape(-1, original_image_shape[0], original_image_shape[1], 1) x_val_yagnam = x_val_yagnam.reshape(-1, original_image_shape[0], original_image_shape[1], 1) test_yagnam['images'] = test_yagnam['images'].reshape(-1, original_image_shape[0], original_image_shape[1], 1) # Assuming your labels are already one-hot encoded num_possible_labels = y_train_yagnam.shape[1]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
# Build the CNN model cnn_model_yagnam = Sequential([ Conv2D(32, (3, 3), activation='relu', input_shape=(original_image_shape[0], original_image_shape[1], 1)), MaxPooling2D(pool_size=(2, 2)), Conv2D(32, (3, 3), activation='relu'), MaxPooling2D(pool_size=(2, 2)), Flatten(), Dense(100, activation='relu'), Dense(num_possible_labels, activation='softmax') ]) # Compile the model cnn_model_yagnam.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # Display a summary of the model cnn_model_yagnam.summary() # Train and validate the model cnn_history_yagnam = cnn_model_yagnam.fit( x_train_yagnam, y_train_yagnam, validation_data=(x_val_yagnam, y_val_yagnam), epochs=8, batch_size=256 ) # Display the Training Vs Validation Accuracy as a line graph plt.plot(cnn_history_yagnam.history['accuracy'], label='Training Accuracy', color='blue')
plt.plot(cnn_history_yagnam.history['val_accuracy'], label='Validation Accuracy', color='green') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.title('Training Vs Validation Accuracy') plt.legend() plt.show() # Evaluate the cnn model with the test dataset test_loss_yagnam, test_accuracy_yagnam = cnn_model_yagnam.evaluate(test_yagnam['images'], test_yagnam['labels']) print("Test Accuracy:", test_accuracy_yagnam) # 3. Create predictions on the test dataset cnn_predictions_yagnam = cnn_model_yagnam.predict(test_yagnam['images']) # 4. Create a function to plot the probability distribution of predictions def plot_prediction_distribution(true_label, prediction_probs): plt.bar(range(num_possible_labels), prediction_probs, color='blue', label='Predicted') plt.bar(true_label, prediction_probs[true_label], color='green', label='True') plt.xlabel('Label') plt.ylabel('Probability') plt.title('Prediction Probability Distribution') plt.legend() plt.show() # 5. Display the first 4 images from the test dataset with their prediction probability distribution for i in range(4): true_label = np.argmax(test_yagnam['labels'][i]) prediction_probs = cnn_predictions_yagnam[i]
plot_prediction_distribution(true_label, prediction_probs) # 6. Analyze and discuss the prediction probability distribution # 7. Display the confusion matrix cnn_predictions_labels = np.argmax(cnn_predictions_yagnam, axis=1) true_labels = np.argmax(test_yagnam['labels'], axis=1) conf_matrix = confusion_matrix(true_labels, cnn_predictions_labels) # 8. Analyze and discuss the confusion matrix sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues') plt.xlabel('Predicted Label') plt.ylabel('True Label') plt.title('Confusion Matrix') plt.show() # h. Build, Train, Validate, Test, and Analyze RNN Model # Repeat Steps f and g for an RNN model rnn_model_yagnam = Sequential([ # i. Input layer tf.keras.layers.Input(shape=(image_resolution[0], image_resolution[1])), # ii. 1st Layer - LSTM LSTM(128), # iii. Output layer Dense(num_possible_labels, activation='softmax') ]) # 2. Compile the RNN model rnn_model_yagnam.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
# 3. Display a summary of the RNN model rnn_model_yagnam.summary() # 4. Train and validate the RNN model rnn_history_yagnam = rnn_model_yagnam.fit( x_train_yagnam, y_train_yagnam, validation_data=(x_val_yagnam, y_val_yagnam), epochs=8, batch_size=256 ) # 5. Display the Training Vs Validation Accuracy as a line graph for the RNN model plt.plot(rnn_history_yagnam.history['accuracy'], label='Training Accuracy', color='blue') plt.plot(rnn_history_yagnam.history['val_accuracy'], label='Validation Accuracy', color='green') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.title('Training Vs Validation Accuracy (RNN Model)') plt.legend() plt.show() # 6. Evaluate the RNN model with the test dataset rnn_test_loss, rnn_test_accuracy = rnn_model_yagnam.evaluate(test_yagnam['images'], test_yagnam['labels']) print("RNN Test Accuracy:", rnn_test_accuracy) # 7. Create predictions on the test dataset with the RNN model rnn_predictions_yagnam = rnn_model_yagnam.predict(test_yagnam['images'])
# 8. Display the first 4 images from the test dataset with their prediction probability distribution for the RNN model for i in range(4): true_label_rnn = np.argmax(test_yagnam['labels'][i]) prediction_probs_rnn = rnn_predictions_yagnam[i] plot_prediction_distribution(true_label_rnn, prediction_probs_rnn) # 9. Analyze and discuss the prediction probability distribution for the RNN model # Function to plot the probability distribution of RNN predictions def plot_rnn_prediction_distribution(true_label, prediction_probs_rnn): plt.bar(range(num_possible_labels), prediction_probs_rnn, color='blue', label='Predicted') plt.bar(true_label, prediction_probs_rnn[true_label], color='green', label='True') plt.xlabel('Label') plt.ylabel('Probability') plt.title('RNN Prediction Probability Distribution') plt.legend() plt.show() # Display the first few examples from the RNN predictions for i in range(4): true_label_rnn = np.argmax(test_yagnam['labels'][i]) prediction_probs_rnn = rnn_predictions_yagnam[i] plot_rnn_prediction_distribution(true_label_rnn, prediction_probs_rnn) # 10. Display the confusion matrix for the RNN model rnn_predictions_labels = np.argmax(rnn_predictions_yagnam, axis=1) conf_matrix_rnn = confusion_matrix(true_labels, rnn_predictions_labels)
# 11. Analyze and discuss the confusion matrix for the RNN model sns.heatmap(conf_matrix_rnn, annot=True, fmt='d', cmap='Blues') plt.xlabel('Predicted Label') plt.ylabel('True Label') plt.title('Confusion Matrix (RNN Model)') plt.show()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help