{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30461,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-15T10:48:42.190803Z","iopub.execute_input":"2023-05-15T10:48:42.191091Z","iopub.status.idle":"2023-05-15T10:48:47.891071Z","shell.execute_reply.started":"2023-05-15T10:48:42.191061Z","shell.execute_reply":"2023-05-15T10:48:47.888088Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport seaborn as sns\nfrom matplotlib import pyplot\nimport cv2\nimport os\nfrom tensorflow.keras.utils import img_to_array","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:49:38.366864Z","iopub.execute_input":"2023-05-15T10:49:38.367341Z","iopub.status.idle":"2023-05-15T10:49:47.198360Z","shell.execute_reply.started":"2023-05-15T10:49:38.367300Z","shell.execute_reply":"2023-05-15T10:49:47.197184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = pd.read_csv(\"/kaggle/input/aptos2019-blindness-detection/train.csv\")\ntrain_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:49:51.564816Z","iopub.execute_input":"2023-05-15T10:49:51.566082Z","iopub.status.idle":"2023-05-15T10:49:51.596321Z","shell.execute_reply.started":"2023-05-15T10:49:51.566039Z","shell.execute_reply":"2023-05-15T10:49:51.594576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[\"diagnosis\"].unique()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:49:54.879568Z","iopub.execute_input":"2023-05-15T10:49:54.879979Z","iopub.status.idle":"2023-05-15T10:49:54.893352Z","shell.execute_reply.started":"2023-05-15T10:49:54.879942Z","shell.execute_reply":"2023-05-15T10:49:54.891949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[\"diagnosis\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:49:57.234878Z","iopub.execute_input":"2023-05-15T10:49:57.235286Z","iopub.status.idle":"2023-05-15T10:49:57.249897Z","shell.execute_reply.started":"2023-05-15T10:49:57.235246Z","shell.execute_reply":"2023-05-15T10:49:57.248485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:01.434926Z","iopub.execute_input":"2023-05-15T10:50:01.435308Z","iopub.status.idle":"2023-05-15T10:50:01.442579Z","shell.execute_reply.started":"2023-05-15T10:50:01.435273Z","shell.execute_reply":"2023-05-15T10:50:01.441201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=train_dataset.diagnosis)\npyplot.show","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:01.824704Z","iopub.execute_input":"2023-05-15T10:50:01.825423Z","iopub.status.idle":"2023-05-15T10:50:02.083221Z","shell.execute_reply.started":"2023-05-15T10:50:01.825382Z","shell.execute_reply":"2023-05-15T10:50:02.082139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = pd.DataFrame()\ndataset = dataset.append(train_dataset, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:02.833379Z","iopub.execute_input":"2023-05-15T10:50:02.833808Z","iopub.status.idle":"2023-05-15T10:50:02.840864Z","shell.execute_reply.started":"2023-05-15T10:50:02.833767Z","shell.execute_reply":"2023-05-15T10:50:02.839697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = train_dataset[train_dataset['diagnosis'] == (2)].index.values\ndf_index = train_dataset.iloc[index]\n# df_index = df_index.append([df_index]*1, ignore_index = True)\ndataset = dataset.append(df_index, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:08.142028Z","iopub.execute_input":"2023-05-15T10:50:08.142702Z","iopub.status.idle":"2023-05-15T10:50:08.153541Z","shell.execute_reply.started":"2023-05-15T10:50:08.142654Z","shell.execute_reply":"2023-05-15T10:50:08.152385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = train_dataset[train_dataset['diagnosis'] == (1)].index.values\ndf_index = train_dataset.iloc[index]\ndf_index = df_index.append([df_index]*3, ignore_index = True)\ndataset = dataset.append(df_index, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:09.329309Z","iopub.execute_input":"2023-05-15T10:50:09.329845Z","iopub.status.idle":"2023-05-15T10:50:09.340158Z","shell.execute_reply.started":"2023-05-15T10:50:09.329801Z","shell.execute_reply":"2023-05-15T10:50:09.338893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = train_dataset[train_dataset['diagnosis'] == (4)].index.values\ndf_index = train_dataset.iloc[index]\ndf_index = df_index.append([df_index]*4, ignore_index = True)\ndataset = dataset.append(df_index, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:10.404820Z","iopub.execute_input":"2023-05-15T10:50:10.405758Z","iopub.status.idle":"2023-05-15T10:50:10.416899Z","shell.execute_reply.started":"2023-05-15T10:50:10.405717Z","shell.execute_reply":"2023-05-15T10:50:10.415691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = train_dataset[train_dataset['diagnosis'] == (3)].index.values\ndf_index = train_dataset.iloc[index]\ndf_index = df_index.append([df_index]*7, ignore_index = True)\ndataset = dataset.append(df_index, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:10.992757Z","iopub.execute_input":"2023-05-15T10:50:10.993661Z","iopub.status.idle":"2023-05-15T10:50:11.005998Z","shell.execute_reply.started":"2023-05-15T10:50:10.993592Z","shell.execute_reply":"2023-05-15T10:50:11.004722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.diagnosis.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:11.734313Z","iopub.execute_input":"2023-05-15T10:50:11.735365Z","iopub.status.idle":"2023-05-15T10:50:11.745020Z","shell.execute_reply.started":"2023-05-15T10:50:11.735309Z","shell.execute_reply":"2023-05-15T10:50:11.743462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=dataset.diagnosis)\npyplot.show","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:12.649026Z","iopub.execute_input":"2023-05-15T10:50:12.649839Z","iopub.status.idle":"2023-05-15T10:50:12.877866Z","shell.execute_reply.started":"2023-05-15T10:50:12.649799Z","shell.execute_reply":"2023-05-15T10:50:12.876618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_Images(label,path,ind):\n    img=cv2.imread(path,cv2.IMREAD_COLOR)\n    img_res=cv2.resize(img,(50,50))\n    print(ind)\n    img_array = img_to_array(img_res)\n    img_array = img_array/255.0\n    img_dataset.append(img_array)\n    diagnosis.append(str(label))","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:15.916022Z","iopub.execute_input":"2023-05-15T10:50:15.916573Z","iopub.status.idle":"2023-05-15T10:50:15.923477Z","shell.execute_reply.started":"2023-05-15T10:50:15.916533Z","shell.execute_reply":"2023-05-15T10:50:15.922345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dataset=[]\ndiagnosis = []","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:17.144751Z","iopub.execute_input":"2023-05-15T10:50:17.145232Z","iopub.status.idle":"2023-05-15T10:50:17.151207Z","shell.execute_reply.started":"2023-05-15T10:50:17.145176Z","shell.execute_reply":"2023-05-15T10:50:17.149804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(dataset)):\n    path = os.path.join('/kaggle/input/aptos2019-blindness-detection/train_images','{}.png'.format(dataset.id_code[i]))\n    prepare_Images(dataset.diagnosis[i],path,i)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:18.785438Z","iopub.execute_input":"2023-05-15T10:50:18.786158Z","iopub.status.idle":"2023-05-15T11:08:51.301325Z","shell.execute_reply.started":"2023-05-15T10:50:18.786111Z","shell.execute_reply":"2023-05-15T11:08:51.299921Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_samples(df, columns=3, rows=3):\n    fig=pyplot.figure(figsize=(5*columns, 4*rows))\n\n    for i in range(columns*rows):\n        image_path = os.path.join('/kaggle/input/aptos2019-blindness-detection/train_images','{}.png'.format(df.id_code[i]))\n        image_id = df.diagnosis[i]\n        img = cv2.imread(f'{image_path}')\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        #img = crop_image_from_gray(img)\n        img = cv2.resize(img, (224,224))\n#         img = cv2.addWeighted(img,4,cv2.GaussianBlur(img, (0,0), 224/40) ,-4 ,128)\n        \n        fig.add_subplot(rows, columns, i+1)\n        pyplot.title(image_id)\n        pyplot.imshow(img)\n    \n    pyplot.tight_layout()\n\ndisplay_samples(dataset)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:51.304123Z","iopub.execute_input":"2023-05-15T11:08:51.304534Z","iopub.status.idle":"2023-05-15T11:08:54.732708Z","shell.execute_reply.started":"2023-05-15T11:08:51.304492Z","shell.execute_reply":"2023-05-15T11:08:54.731660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs = np.array(img_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:54.734175Z","iopub.execute_input":"2023-05-15T11:08:54.735269Z","iopub.status.idle":"2023-05-15T11:08:54.841304Z","shell.execute_reply.started":"2023-05-15T11:08:54.735229Z","shell.execute_reply":"2023-05-15T11:08:54.840185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom keras.utils import np_utils\n\nle = LabelEncoder()\ndiag = le.fit_transform(dataset.diagnosis)\ndiag = np_utils.to_categorical(diag)\nprint(diag.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:54.844857Z","iopub.execute_input":"2023-05-15T11:08:54.845261Z","iopub.status.idle":"2023-05-15T11:08:54.909919Z","shell.execute_reply.started":"2023-05-15T11:08:54.845221Z","shell.execute_reply":"2023-05-15T11:08:54.908799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_valid, y_train, y_valid = train_test_split(imgs, diag,\n                                                    shuffle=True, stratify=diag,\n                                                    test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:54.911343Z","iopub.execute_input":"2023-05-15T11:08:54.911890Z","iopub.status.idle":"2023-05-15T11:08:55.524236Z","shell.execute_reply.started":"2023-05-15T11:08:54.911849Z","shell.execute_reply":"2023-05-15T11:08:55.523098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:55.526152Z","iopub.execute_input":"2023-05-15T11:08:55.526587Z","iopub.status.idle":"2023-05-15T11:08:55.534265Z","shell.execute_reply.started":"2023-05-15T11:08:55.526543Z","shell.execute_reply":"2023-05-15T11:08:55.533126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:55.536000Z","iopub.execute_input":"2023-05-15T11:08:55.536807Z","iopub.status.idle":"2023-05-15T11:08:55.546171Z","shell.execute_reply.started":"2023-05-15T11:08:55.536732Z","shell.execute_reply":"2023-05-15T11:08:55.545124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_valid.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:55.548108Z","iopub.execute_input":"2023-05-15T11:08:55.548514Z","iopub.status.idle":"2023-05-15T11:08:55.556910Z","shell.execute_reply.started":"2023-05-15T11:08:55.548474Z","shell.execute_reply":"2023-05-15T11:08:55.555712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_valid.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:55.558300Z","iopub.execute_input":"2023-05-15T11:08:55.559374Z","iopub.status.idle":"2023-05-15T11:08:55.569232Z","shell.execute_reply.started":"2023-05-15T11:08:55.559333Z","shell.execute_reply":"2023-05-15T11:08:55.567461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_width = X_train.shape[1]\nimg_height = X_train.shape[2]\nimg_depth = X_train.shape[3]\nnum_classes = y_train.shape[1]","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:55.574718Z","iopub.execute_input":"2023-05-15T11:08:55.574998Z","iopub.status.idle":"2023-05-15T11:08:55.579885Z","shell.execute_reply.started":"2023-05-15T11:08:55.574971Z","shell.execute_reply":"2023-05-15T11:08:55.578840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_net(optim):\n    net = Sequential(name='DCNN')\n\n    net.add(Conv2D(filters=16,kernel_size=(2,2),input_shape=(50, 50, 3),activation='relu',padding='same',kernel_initializer='he_normal',name='CONV_1'))\n    net.add(MaxPooling2D(pool_size=(2,2), name='MAXPOOL_1'))\n    \n    net.add(Conv2D(filters=32,kernel_size=(2,2),activation='relu',padding='same',kernel_initializer='he_normal',name='CONV_2'))\n    net.add(MaxPooling2D(pool_size=(2,2), name='MAXPOOL_2'))\n    \n    net.add(Conv2D(filters=64,kernel_size=(2,2),activation='relu',padding='same',kernel_initializer='he_normal',name='CONV_3'))\n    net.add(MaxPooling2D(pool_size=(2,2), name='MAXPOOL_3'))\n    \n    net.add(Conv2D(filters=128,kernel_size=(2,2),activation='relu',padding='same',kernel_initializer='he_normal',name='CONV_4'))\n    net.add(MaxPooling2D(pool_size=(2,2), name='MAXPOOL_4'))\n    \n    net.add(Dropout(0.2, name=\"DROPOUT_1\"))\n    net.add(Flatten(name='FLATTEN'))\n        \n    net.add(Dense(128,activation='relu',kernel_initializer='he_normal',name='DENSE_1'))\n    \n    net.add(Dense(5,activation='softmax',name='OUTPUT_LAYER'))\n    \n    net.compile(loss='categorical_crossentropy',optimizer=optim,metrics=['accuracy'])\n    \n    net.summary()\n    \n    return net","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:55.581200Z","iopub.execute_input":"2023-05-15T11:08:55.582089Z","iopub.status.idle":"2023-05-15T11:08:55.596784Z","shell.execute_reply.started":"2023-05-15T11:08:55.582050Z","shell.execute_reply":"2023-05-15T11:08:55.595622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import optimizers\nfrom tensorflow.keras.datasets import mnist\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPooling2D\nfrom tensorflow.keras.layers import Dropout, BatchNormalization, LeakyReLU, Activation\nfrom tensorflow.keras.callbacks import Callback, EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:55.598198Z","iopub.execute_input":"2023-05-15T11:08:55.598922Z","iopub.status.idle":"2023-05-15T11:08:55.609331Z","shell.execute_reply.started":"2023-05-15T11:08:55.598883Z","shell.execute_reply":"2023-05-15T11:08:55.608273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nepochs = 100\noptims = [\n    optimizers.Nadam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, name='Nadam'),\n    optimizers.Adam(),\n]\n\nmodel = build_net(optims[1]) ","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:55.610844Z","iopub.execute_input":"2023-05-15T11:08:55.612057Z","iopub.status.idle":"2023-05-15T11:08:58.416511Z","shell.execute_reply.started":"2023-05-15T11:08:55.612010Z","shell.execute_reply":"2023-05-15T11:08:58.415686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install visualkeras\nimport visualkeras\nvisualkeras.layered_view(model, legend=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:08:58.417656Z","iopub.execute_input":"2023-05-15T11:08:58.418019Z","iopub.status.idle":"2023-05-15T11:09:12.082293Z","shell.execute_reply.started":"2023-05-15T11:08:58.417979Z","shell.execute_reply":"2023-05-15T11:09:12.080966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train, batch_size=batch_size,\n    validation_data=(X_valid, y_valid),\n    steps_per_epoch=len(X_train) / batch_size,\n    epochs=epochs,\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:09:12.085242Z","iopub.execute_input":"2023-05-15T11:09:12.085672Z","iopub.status.idle":"2023-05-15T11:12:35.762060Z","shell.execute_reply.started":"2023-05-15T11:09:12.085602Z","shell.execute_reply":"2023-05-15T11:12:35.760665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set()\nfig = pyplot.figure(0, (12, 4))\n\nax = pyplot.subplot(1, 2, 1)\nsns.lineplot(x=history.epoch, y=history.history['accuracy'], label='train')\nsns.lineplot(x=history.epoch, y=history.history['val_accuracy'], label='valid')\npyplot.title('Accuracy')\npyplot.tight_layout()\n\nax = pyplot.subplot(1, 2, 2)\nsns.lineplot(x=history.epoch, y=history.history['loss'], label='train')\nsns.lineplot(x=history.epoch, y=history.history['val_loss'], label='valid')\npyplot.title('Loss')\npyplot.tight_layout()\n\npyplot.show()\n\nprint(\"Final Accuracy of emotion after 6 epochs will be: \", round(history.history['accuracy'][99]*100,2), \"%\")","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:12:35.764386Z","iopub.execute_input":"2023-05-15T11:12:35.765919Z","iopub.status.idle":"2023-05-15T11:12:36.463804Z","shell.execute_reply.started":"2023-05-15T11:12:35.765876Z","shell.execute_reply":"2023-05-15T11:12:36.462525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import scikitplot\n\npredict_x=model.predict(X_valid) \nyhat_valid=np.argmax(predict_x,axis=1)\nscikitplot.metrics.plot_confusion_matrix(np.argmax(y_valid, axis=1), yhat_valid, figsize=(7,7))\npyplot.savefig(\"confusion_matrix_dcnn.png\")","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:12:36.465440Z","iopub.execute_input":"2023-05-15T11:12:36.466435Z","iopub.status.idle":"2023-05-15T11:12:37.610754Z","shell.execute_reply.started":"2023-05-15T11:12:36.466397Z","shell.execute_reply":"2023-05-15T11:12:37.609593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\n\nprint(f'total wrong validation predictions: {np.sum(np.argmax(y_valid, axis=1) != yhat_valid)}\\n\\n')\nprint(classification_report(np.argmax(y_valid, axis=1), yhat_valid))","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:12:37.612586Z","iopub.execute_input":"2023-05-15T11:12:37.613428Z","iopub.status.idle":"2023-05-15T11:12:37.629491Z","shell.execute_reply.started":"2023-05-15T11:12:37.613383Z","shell.execute_reply":"2023-05-15T11:12:37.628266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_alexnet(optim):\n    net = Sequential(name='AlexNet')\n\n    # 1st convolutional layer\n    net.add(Conv2D(96, (11, 11), strides=(4, 4), padding='same', activation='relu', name='CONV_1'))\n\n    # 2nd max pooling layer\n    net.add(MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='MAXPOOL_1'))\n\n    # 3rd convolutional layer\n    net.add(Conv2D(256, (5, 5), strides=(1, 1), padding='same', activation='relu', name='CONV_2'))\n\n    # 4th max pooling layer\n    net.add(MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='MAXPOOL_2'))\n\n    # 5th convolutional layer\n    net.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', activation='relu', name='CONV_3'))\n\n    # 6th convolutional layer\n    net.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', activation='relu', name='CONV_4'))\n\n    # 7th convolutional layer\n    net.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', name='CONV_5'))\n\n    # 8th max pooling layer\n    net.add(MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='MAXPOOL_3'))\n\n    # 1st fully connected layer\n    net.add(Flatten(name='FLATTEN'))\n    net.add(Dense(4096, activation='relu', name='DENSE_1'))\n\n    # 2nd fully connected layer\n    net.add(Dense(4096, activation='relu', name='DENSE_2'))\n\n    # 3rd fully connected layer\n    net.add(Dense(5, activation='softmax', name='OUTPUT_LAYER'))\n\n    net.compile(loss='categorical_crossentropy',optimizer=optim,metrics=['accuracy'])\n\n#     net.summary()\n\n    return net\n","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:19:34.840204Z","iopub.execute_input":"2023-05-15T11:19:34.840969Z","iopub.status.idle":"2023-05-15T11:19:34.857283Z","shell.execute_reply.started":"2023-05-15T11:19:34.840927Z","shell.execute_reply":"2023-05-15T11:19:34.855767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\nepochs = 100\noptims = [\n    optimizers.Nadam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, name='Nadam'),\n    optimizers.Adam(),\n]\n\nmodel_alex = build_alexnet(optims[1]) ","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:24:48.300471Z","iopub.execute_input":"2023-05-15T11:24:48.301236Z","iopub.status.idle":"2023-05-15T11:24:48.334980Z","shell.execute_reply.started":"2023-05-15T11:24:48.301191Z","shell.execute_reply":"2023-05-15T11:24:48.333769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_2 = model_alex.fit(\n    X_train, y_train, batch_size=batch_size,\n    validation_data=(X_valid, y_valid),\n    steps_per_epoch=len(X_train) / batch_size,\n    epochs=epochs,\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:24:49.734458Z","iopub.execute_input":"2023-05-15T11:24:49.735245Z","iopub.status.idle":"2023-05-15T11:33:13.421597Z","shell.execute_reply.started":"2023-05-15T11:24:49.735202Z","shell.execute_reply":"2023-05-15T11:33:13.420344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set()\nfig = pyplot.figure(0, (12, 4))\n\nax = pyplot.subplot(1, 2, 1)\nsns.lineplot(x=history_2.epoch, y=history_2.history['accuracy'], label='train')\nsns.lineplot(x=history_2.epoch, y=history_2.history['val_accuracy'], label='valid')\npyplot.title('Accuracy')\npyplot.tight_layout()\n\nax = pyplot.subplot(1, 2, 2)\nsns.lineplot(x=history_2.epoch, y=history_2.history['loss'], label='train')\nsns.lineplot(x=history_2.epoch, y=history_2.history['val_loss'], label='valid')\npyplot.title('Loss')\npyplot.tight_layout()\n\npyplot.show()\n\nprint(\"Final Accuracy of emotion after 6 epochs will be: \", round(history_2.history['accuracy'][99]*100,2), \"%\")","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:33:13.425658Z","iopub.execute_input":"2023-05-15T11:33:13.426024Z","iopub.status.idle":"2023-05-15T11:33:14.076423Z","shell.execute_reply.started":"2023-05-15T11:33:13.425990Z","shell.execute_reply":"2023-05-15T11:33:14.075291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import scikitplot\n\npredict_x=model_alex.predict(X_valid) \nyhat_valid=np.argmax(predict_x,axis=1)\nscikitplot.metrics.plot_confusion_matrix(np.argmax(y_valid, axis=1), yhat_valid, figsize=(7,7))\npyplot.savefig(\"confusion_matrix_dcnn.png\")","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:33:14.078437Z","iopub.execute_input":"2023-05-15T11:33:14.079250Z","iopub.status.idle":"2023-05-15T11:33:15.106770Z","shell.execute_reply.started":"2023-05-15T11:33:14.079205Z","shell.execute_reply":"2023-05-15T11:33:15.105590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\n\nprint(f'total wrong validation predictions: {np.sum(np.argmax(y_valid, axis=1) != yhat_valid)}\\n\\n')\nprint(classification_report(np.argmax(y_valid, axis=1), yhat_valid))","metadata":{"execution":{"iopub.status.busy":"2023-05-15T11:33:15.109464Z","iopub.execute_input":"2023-05-15T11:33:15.110161Z","iopub.status.idle":"2023-05-15T11:33:15.126655Z","shell.execute_reply.started":"2023-05-15T11:33:15.110106Z","shell.execute_reply":"2023-05-15T11:33:15.125493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}