{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\n%matplotlib inline\n\nnp.random.seed(2)\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\n\nsns.set(style='white', context='notebook', palette='deep')\n\nfrom PIL import Image\n\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n #   for filename in filenames:\n  #      print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:27:43.263885Z","iopub.execute_input":"2023-05-12T11:27:43.264229Z","iopub.status.idle":"2023-05-12T11:27:51.006369Z","shell.execute_reply.started":"2023-05-12T11:27:43.264200Z","shell.execute_reply":"2023-05-12T11:27:51.005387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = '/kaggle/input/diabetic-retinopathy-resized/resized_train/resized_train'\n#print('Number of training images:', len(os.listdir(data_dir)))\n\ncropped_data_dir = '/kaggle/input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped/'\n#print('Number of training images:', len(os.listdir(cropped_data_dir)))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:27:51.008597Z","iopub.execute_input":"2023-05-12T11:27:51.009601Z","iopub.status.idle":"2023-05-12T11:27:51.014941Z","shell.execute_reply.started":"2023-05-12T11:27:51.009564Z","shell.execute_reply":"2023-05-12T11:27:51.013862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv(\"../input/diabetic-retinopathy-resized/trainLabels.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:27:51.016504Z","iopub.execute_input":"2023-05-12T11:27:51.017111Z","iopub.status.idle":"2023-05-12T11:27:51.063855Z","shell.execute_reply.started":"2023-05-12T11:27:51.017078Z","shell.execute_reply":"2023-05-12T11:27:51.062890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:27:51.066929Z","iopub.execute_input":"2023-05-12T11:27:51.067262Z","iopub.status.idle":"2023-05-12T11:27:51.074149Z","shell.execute_reply.started":"2023-05-12T11:27:51.067232Z","shell.execute_reply":"2023-05-12T11:27:51.072895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check the data\ntrain_labels.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:27:51.075787Z","iopub.execute_input":"2023-05-12T11:27:51.076144Z","iopub.status.idle":"2023-05-12T11:27:51.107836Z","shell.execute_reply.started":"2023-05-12T11:27:51.076113Z","shell.execute_reply":"2023-05-12T11:27:51.107030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:27:51.110675Z","iopub.execute_input":"2023-05-12T11:27:51.110962Z","iopub.status.idle":"2023-05-12T11:27:51.123593Z","shell.execute_reply.started":"2023-05-12T11:27:51.110939Z","shell.execute_reply":"2023-05-12T11:27:51.122367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.tail()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:27:51.125420Z","iopub.execute_input":"2023-05-12T11:27:51.125857Z","iopub.status.idle":"2023-05-12T11:27:51.135133Z","shell.execute_reply.started":"2023-05-12T11:27:51.125825Z","shell.execute_reply":"2023-05-12T11:27:51.134070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img = Image.open(os.path.join(data_dir, os.listdir(data_dir)[0]))\nprint('Image size:', sample_img.size)\n\nsample_img = Image.open(os.path.join(cropped_data_dir, os.listdir(cropped_data_dir)[0]))\nprint('Image size:', sample_img.size)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-12T11:27:51.136967Z","iopub.execute_input":"2023-05-12T11:27:51.137858Z","iopub.status.idle":"2023-05-12T11:27:54.784190Z","shell.execute_reply.started":"2023-05-12T11:27:51.137700Z","shell.execute_reply":"2023-05-12T11:27:54.783041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, axarr = plt.subplots(2,2)\naxarr[0,0].imshow(Image.open(os.path.join(data_dir, os.listdir(data_dir)[0])))\naxarr[0,1].imshow(Image.open(os.path.join(cropped_data_dir, os.listdir(cropped_data_dir)[0])))\naxarr[1,0].imshow(Image.open(os.path.join(data_dir, os.listdir(data_dir)[1])))\naxarr[1,1].imshow(Image.open(os.path.join(cropped_data_dir, os.listdir(cropped_data_dir)[1])))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:27:54.785796Z","iopub.execute_input":"2023-05-12T11:27:54.786536Z","iopub.status.idle":"2023-05-12T11:27:56.182493Z","shell.execute_reply.started":"2023-05-12T11:27:54.786500Z","shell.execute_reply":"2023-05-12T11:27:56.181569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check Data Distribution**","metadata":{}},{"cell_type":"code","source":"widths = []\nheights = []\n\nfor img_file in os.listdir(data_dir):\n    img = Image.open(os.path.join(data_dir, img_file))\n    width, height = img.size\n    widths.append(width)\n    heights.append(height)\n\nprint('Average image size:', np.mean(widths), 'x', np.mean(heights))\n\n# Check the distribution of image sizes\nfig, axs = plt.subplots(1, 2, figsize=(15, 6))\naxs[0].hist(widths, bins=50)\naxs[0].set_xlabel('Image width')\naxs[0].set_ylabel('Frequency')\naxs[1].hist(heights, bins=50)\naxs[1].set_xlabel('Image height')\naxs[1].set_ylabel('Frequency')\nplt.show()\n\n# Check the distribution of image modes\nmodes = []\n\nfor img_file in os.listdir(data_dir):\n    img = Image.open(os.path.join(data_dir, img_file))\n    modes.append(img.mode)\n\nprint('Image modes:', set(modes))\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:27:56.187579Z","iopub.execute_input":"2023-05-12T11:27:56.188163Z","iopub.status.idle":"2023-05-12T11:33:44.956678Z","shell.execute_reply.started":"2023-05-12T11:27:56.188131Z","shell.execute_reply":"2023-05-12T11:33:44.955781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check for class distribution**","metadata":{}},{"cell_type":"code","source":"#labels_df = pd.read_csv('../input/diabetic-retinopathy-resized/trainLabels.csv')\ntrain_labels['level'].hist(bins=5)\nplt.xlabel('Class')\nplt.ylabel('Frequency')\nplt.title('Class Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:33:44.958121Z","iopub.execute_input":"2023-05-12T11:33:44.958716Z","iopub.status.idle":"2023-05-12T11:33:45.282934Z","shell.execute_reply.started":"2023-05-12T11:33:44.958682Z","shell.execute_reply":"2023-05-12T11:33:45.282003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Converting images into their pixel values as 1D array in CSV file**","metadata":{}},{"cell_type":"code","source":"import cv2\n\nIMG_DIR = data_dir\n\n# Read train_labels.csv to obtain the image name sequence\ndf_train = train_labels\nimage_sequence = df_train['image'].values\n\nwith open('eye_train.csv', 'wb') as f:\n    for img_name in image_sequence:\n        img_path = os.path.join(IMG_DIR, img_name+ '.jpeg')\n\n        # Process the image\n        img_array = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)\n#        if img_array is not None:\n        \n        img_pil = Image.fromarray(img_array)\n        img_28x28 = np.array(img_pil.resize((28, 28), Image.ANTIALIAS))\n        img_array = img_28x28.flatten()\n        #Normalize the images\n        img_array = img_array / 255.0\n        # Save the image pixel values to the CSV file\n        np.savetxt(f, img_array.reshape(1, -1), delimiter=\",\", header='')\n#        else:\n#            print(f\"Failed to load image: {img_name}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:40:00.115955Z","iopub.execute_input":"2023-05-12T11:40:00.116629Z","iopub.status.idle":"2023-05-12T11:44:37.803350Z","shell.execute_reply.started":"2023-05-12T11:40:00.116595Z","shell.execute_reply":"2023-05-12T11:44:37.802292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import os\n#os.remove(\"/#kaggle/working/eye_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:45:19.711077Z","iopub.execute_input":"2023-05-12T11:45:19.711435Z","iopub.status.idle":"2023-05-12T11:45:19.716792Z","shell.execute_reply.started":"2023-05-12T11:45:19.711409Z","shell.execute_reply":"2023-05-12T11:45:19.715752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.image.unique().shape","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:45:21.798029Z","iopub.execute_input":"2023-05-12T11:45:21.798707Z","iopub.status.idle":"2023-05-12T11:45:21.811273Z","shell.execute_reply.started":"2023-05-12T11:45:21.798675Z","shell.execute_reply":"2023-05-12T11:45:21.810153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_eye_train = pd.read_csv('eye_train.csv', header=None, skiprows=1) #not reading the column names\n\n#retrieving column names as a list\ncolumn_names = pd.read_csv('eye_train.csv', nrows=1, header=None).values[0]\ndf_eye_train.loc[-1] = column_names           #adding at index -1\ndf_eye_train.index = df_eye_train.index + 1   #resetting index\ndf_eye_train = df_eye_train.sort_index().reset_index(drop=True)   #resetting index to start from 0\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:45:23.369786Z","iopub.execute_input":"2023-05-12T11:45:23.370811Z","iopub.status.idle":"2023-05-12T11:45:29.842854Z","shell.execute_reply.started":"2023-05-12T11:45:23.370765Z","shell.execute_reply":"2023-05-12T11:45:29.841896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_eye_train.tail()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:45:31.630216Z","iopub.execute_input":"2023-05-12T11:45:31.630569Z","iopub.status.idle":"2023-05-12T11:45:31.656538Z","shell.execute_reply.started":"2023-05-12T11:45:31.630540Z","shell.execute_reply":"2023-05-12T11:45:31.655466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_eye_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:45:36.474483Z","iopub.execute_input":"2023-05-12T11:45:36.474857Z","iopub.status.idle":"2023-05-12T11:45:36.480664Z","shell.execute_reply.started":"2023-05-12T11:45:36.474827Z","shell.execute_reply":"2023-05-12T11:45:36.479800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train_labels[\"level\"]\nY_train[135:145]","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:45:59.333878Z","iopub.execute_input":"2023-05-12T11:45:59.334217Z","iopub.status.idle":"2023-05-12T11:45:59.342597Z","shell.execute_reply.started":"2023-05-12T11:45:59.334192Z","shell.execute_reply":"2023-05-12T11:45:59.341413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Performing Classification through CNN**","metadata":{}},{"cell_type":"markdown","source":"**Label Encoding**","metadata":{}},{"cell_type":"code","source":"Y_train = to_categorical(Y_train, num_classes = 5)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:01.739538Z","iopub.execute_input":"2023-05-12T11:46:01.741021Z","iopub.status.idle":"2023-05-12T11:46:01.749518Z","shell.execute_reply.started":"2023-05-12T11:46:01.740978Z","shell.execute_reply":"2023-05-12T11:46:01.748004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train[135:145]","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:03.073565Z","iopub.execute_input":"2023-05-12T11:46:03.073945Z","iopub.status.idle":"2023-05-12T11:46:03.081249Z","shell.execute_reply.started":"2023-05-12T11:46:03.073912Z","shell.execute_reply":"2023-05-12T11:46:03.080335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Split into training and validation set**","metadata":{}},{"cell_type":"code","source":"# Set the random seed\nrandom_seed = 2","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:08.118401Z","iopub.execute_input":"2023-05-12T11:46:08.118765Z","iopub.status.idle":"2023-05-12T11:46:08.125093Z","shell.execute_reply.started":"2023-05-12T11:46:08.118719Z","shell.execute_reply":"2023-05-12T11:46:08.124169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape image i 3 dimensions (height = 28px, width = 28px , channal = 1)\ndf_eye_train = df_eye_train.values.reshape(-1,28,28,1)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:09.183406Z","iopub.execute_input":"2023-05-12T11:46:09.183766Z","iopub.status.idle":"2023-05-12T11:46:09.188518Z","shell.execute_reply.started":"2023-05-12T11:46:09.183717Z","shell.execute_reply":"2023-05-12T11:46:09.187552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_eye_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:10.256065Z","iopub.execute_input":"2023-05-12T11:46:10.256966Z","iopub.status.idle":"2023-05-12T11:46:10.263509Z","shell.execute_reply.started":"2023-05-12T11:46:10.256926Z","shell.execute_reply":"2023-05-12T11:46:10.262310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:11.575942Z","iopub.execute_input":"2023-05-12T11:46:11.576280Z","iopub.status.idle":"2023-05-12T11:46:11.583003Z","shell.execute_reply.started":"2023-05-12T11:46:11.576252Z","shell.execute_reply":"2023-05-12T11:46:11.581920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the train and the validation set for the fitting (80:20% split)\nX_train, X_val, Y_train, Y_val = train_test_split(df_eye_train, Y_train, test_size = 0.2, random_state=random_seed)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:12.684776Z","iopub.execute_input":"2023-05-12T11:46:12.685133Z","iopub.status.idle":"2023-05-12T11:46:13.031638Z","shell.execute_reply.started":"2023-05-12T11:46:12.685105Z","shell.execute_reply":"2023-05-12T11:46:13.030683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:14.575882Z","iopub.execute_input":"2023-05-12T11:46:14.576530Z","iopub.status.idle":"2023-05-12T11:46:14.582712Z","shell.execute_reply.started":"2023-05-12T11:46:14.576492Z","shell.execute_reply":"2023-05-12T11:46:14.581730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:16.113751Z","iopub.execute_input":"2023-05-12T11:46:16.114104Z","iopub.status.idle":"2023-05-12T11:46:16.120081Z","shell.execute_reply.started":"2023-05-12T11:46:16.114077Z","shell.execute_reply":"2023-05-12T11:46:16.119171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **CNN**","metadata":{}},{"cell_type":"code","source":"# Set the CNN model \n# my CNN architechture is In -> [[Conv2D->relu]*2 -> MaxPool2D -> Dropout]*2 -> Flatten -> Dense -> Dropout -> Out\n\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(5, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:20.229643Z","iopub.execute_input":"2023-05-12T11:46:20.230677Z","iopub.status.idle":"2023-05-12T11:46:22.668317Z","shell.execute_reply.started":"2023-05-12T11:46:20.230637Z","shell.execute_reply":"2023-05-12T11:46:22.667387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Defining Optimiser and Annealer**","metadata":{}},{"cell_type":"code","source":"# Define the optimizer\noptimizer = RMSprop(learning_rate=0.001, rho=0.9, epsilon=1e-08, decay=0.0)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:26.391218Z","iopub.execute_input":"2023-05-12T11:46:26.391578Z","iopub.status.idle":"2023-05-12T11:46:26.396054Z","shell.execute_reply.started":"2023-05-12T11:46:26.391547Z","shell.execute_reply":"2023-05-12T11:46:26.395149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:29.465198Z","iopub.execute_input":"2023-05-12T11:46:29.465890Z","iopub.status.idle":"2023-05-12T11:46:29.483225Z","shell.execute_reply.started":"2023-05-12T11:46:29.465855Z","shell.execute_reply":"2023-05-12T11:46:29.482332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set a learning rate annealer\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:31.310055Z","iopub.execute_input":"2023-05-12T11:46:31.310415Z","iopub.status.idle":"2023-05-12T11:46:31.315273Z","shell.execute_reply.started":"2023-05-12T11:46:31.310384Z","shell.execute_reply":"2023-05-12T11:46:31.314091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 30 \nbatch_size = 100","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:31.467920Z","iopub.execute_input":"2023-05-12T11:46:31.468504Z","iopub.status.idle":"2023-05-12T11:46:31.473646Z","shell.execute_reply.started":"2023-05-12T11:46:31.468475Z","shell.execute_reply":"2023-05-12T11:46:31.472700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory = model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_val, Y_val))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:46:32.658456Z","iopub.execute_input":"2023-05-12T11:46:32.658801Z","iopub.status.idle":"2023-05-12T11:47:48.627082Z","shell.execute_reply.started":"2023-05-12T11:46:32.658773Z","shell.execute_reply":"2023-05-12T11:47:48.626132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Evaluating the Model**","metadata":{}},{"cell_type":"markdown","source":"**Training and Validation curves**","metadata":{}},{"cell_type":"code","source":"# Retrieve the training and validation loss values\ntrain_loss = history.history['loss']\nval_loss = history.history['val_loss']\n\n# Retrieve the training and validation accuracy values\ntrain_acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\n# Plot the loss curves\nplt.figure(figsize=(12, 6))\nplt.subplot(1, 2, 1)\nplt.plot(train_loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Loss Curves')\nplt.legend()\n\n# Plot the accuracy curves\nplt.subplot(1, 2, 2)\nplt.plot(train_acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Accuracy Curves')\nplt.legend()\n\n# Display the plot\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:48:09.571646Z","iopub.execute_input":"2023-05-12T11:48:09.572639Z","iopub.status.idle":"2023-05-12T11:48:10.242580Z","shell.execute_reply.started":"2023-05-12T11:48:09.572594Z","shell.execute_reply":"2023-05-12T11:48:10.241702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Look at confusion matrix \n\ndef plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n\n# Predict the values from the validation dataset\nY_pred = model.predict(X_val)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(Y_val,axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n# plot the confusion matrix\nplot_confusion_matrix(confusion_mtx, classes = range(10)) \n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:48:12.206015Z","iopub.execute_input":"2023-05-12T11:48:12.206386Z","iopub.status.idle":"2023-05-12T11:48:13.652694Z","shell.execute_reply.started":"2023-05-12T11:48:12.206357Z","shell.execute_reply":"2023-05-12T11:48:13.651813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display some error results \n\n# Errors are difference between predicted labels and true labels\nerrors = (Y_pred_classes - Y_true != 0)\n\nY_pred_classes_errors = Y_pred_classes[errors]\nY_pred_errors = Y_pred[errors]\nY_true_errors = Y_true[errors]\nX_val_errors = X_val[errors]\n\ndef display_errors(errors_index,img_errors,pred_errors, obs_errors):\n    \"\"\" This function shows 6 images with their predicted and real labels\"\"\"\n    n = 0\n    nrows = 2\n    ncols = 3\n    fig, ax = plt.subplots(nrows,ncols,sharex=True,sharey=True)\n    for row in range(nrows):\n        for col in range(ncols):\n            error = errors_index[n]\n            ax[row,col].imshow((img_errors[error]).reshape((28,28)))\n            ax[row,col].set_title(\"Predicted label :{}\\nTrue label :{}\".format(pred_errors[error],obs_errors[error]))\n            n += 1\n\n# Probabilities of the wrong predicted numbers\nY_pred_errors_prob = np.max(Y_pred_errors,axis = 1)\n\n# Predicted probabilities of the true values in the error set\ntrue_prob_errors = np.diagonal(np.take(Y_pred_errors, Y_true_errors, axis=1))\n\n# Difference between the probability of the predicted label and the true label\ndelta_pred_true_errors = Y_pred_errors_prob - true_prob_errors\n\n# Sorted list of the delta prob errors\nsorted_dela_errors = np.argsort(delta_pred_true_errors)\n\n# Top 6 errors \nmost_important_errors = sorted_dela_errors[-6:]\n\n# Show the top 6 errors\ndisplay_errors(most_important_errors, X_val_errors, Y_pred_classes_errors, Y_true_errors)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:48:16.940363Z","iopub.execute_input":"2023-05-12T11:48:16.940720Z","iopub.status.idle":"2023-05-12T11:48:17.862829Z","shell.execute_reply.started":"2023-05-12T11:48:16.940692Z","shell.execute_reply":"2023-05-12T11:48:17.861762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Performing Classification through AlexNet**","metadata":{}},{"cell_type":"markdown","source":"# AlexNet","metadata":{}},{"cell_type":"markdown","source":"## For AlexNet, input size should be 227x227x3. For this, another csv should be created and passed to the model. It is extremely time-consuming. And time is running out!!!!","metadata":{}},{"cell_type":"code","source":"# Define the AlexNet model\nmodel = Sequential()\n\n# Layer 1: Convolutional Layer\nmodel.add(Conv2D(filters=96, kernel_size=(11, 11), strides=(4, 4), activation='relu', input_shape=(227, 227, 3)))\nmodel.add(MaxPool2D(pool_size=(3, 3), strides=(2, 2)))\n\n# Layer 2: Convolutional Layer\nmodel.add(Conv2D(filters=256, kernel_size=(5, 5), strides=(1, 1), activation='relu'))\nmodel.add(MaxPool2D(pool_size=(3, 3), strides=(2, 2)))\n\n# Layer 3: Convolutional Layer\nmodel.add(Conv2D(filters=384, kernel_size=(3, 3), strides=(1, 1), activation='relu'))\n\n# Layer 4: Convolutional Layer\nmodel.add(Conv2D(filters=384, kernel_size=(3, 3), strides=(1, 1), activation='relu'))\n\n# Layer 5: Convolutional Layer\nmodel.add(Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1), activation='relu'))\nmodel.add(MaxPool2D(pool_size=(3, 3), strides=(2, 2)))\n\n# Flatten the output from the previous layer\nmodel.add(Flatten())\n\n# Layer 6: Fully Connected Layer\nmodel.add(Dense(units=4096, activation='relu'))\nmodel.add(Dropout(0.5))\n\n# Layer 7: Fully Connected Layer\nmodel.add(Dense(units=4096, activation='relu'))\nmodel.add(Dropout(0.5))\n\n# Layer 8: Output Layer\nmodel.add(Dense(units=5, activation='softmax'))\n\n# Compile the model\nmodel.compile(optimizer=Adam(learning_rate=0.001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nmodel.fit(X_train, Y_train, batch_size=128, epochs=10, validation_data=(X_val, Y_val))\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T11:51:35.849569Z","iopub.execute_input":"2023-05-12T11:51:35.849983Z","iopub.status.idle":"2023-05-12T11:51:35.960181Z","shell.execute_reply.started":"2023-05-12T11:51:35.849951Z","shell.execute_reply":"2023-05-12T11:51:35.958696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}