{"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-11-29T11:53:13.578675Z","iopub.execute_input":"2023-11-29T11:53:13.579524Z","iopub.status.idle":"2023-11-29T11:53:19.560562Z","shell.execute_reply.started":"2023-11-29T11:53:13.579465Z","shell.execute_reply":"2023-11-29T11:53:19.559385Z"},"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-11-29T11:53:19.562997Z","iopub.execute_input":"2023-11-29T11:53:19.563926Z","iopub.status.idle":"2023-11-29T11:53:28.757005Z","shell.execute_reply.started":"2023-11-29T11:53:19.563880Z","shell.execute_reply":"2023-11-29T11:53:28.755689Z"},"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-11-29T11:53:28.758657Z","iopub.execute_input":"2023-11-29T11:53:28.759548Z","iopub.status.idle":"2023-11-29T11:53:28.792222Z","shell.execute_reply.started":"2023-11-29T11:53:28.759501Z","shell.execute_reply":"2023-11-29T11:53:28.791162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[\"diagnosis\"].unique()","metadata":{"execution":{"iopub.status.busy":"2023-11-29T11:53:28.794683Z","iopub.execute_input":"2023-11-29T11:53:28.795506Z","iopub.status.idle":"2023-11-29T11:53:28.807463Z","shell.execute_reply.started":"2023-11-29T11:53:28.795471Z","shell.execute_reply":"2023-11-29T11:53:28.806434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[\"diagnosis\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-11-29T11:53:28.808869Z","iopub.execute_input":"2023-11-29T11:53:28.809309Z","iopub.status.idle":"2023-11-29T11:53:28.820700Z","shell.execute_reply.started":"2023-11-29T11:53:28.809266Z","shell.execute_reply":"2023-11-29T11:53:28.819697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-29T11:53:28.822144Z","iopub.execute_input":"2023-11-29T11:53:28.822482Z","iopub.status.idle":"2023-11-29T11:53:28.832396Z","shell.execute_reply.started":"2023-11-29T11:53:28.822452Z","shell.execute_reply":"2023-11-29T11:53:28.831408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=train_dataset.diagnosis)\npyplot.show","metadata":{"execution":{"iopub.status.busy":"2023-11-29T11:53:28.833653Z","iopub.execute_input":"2023-11-29T11:53:28.834426Z","iopub.status.idle":"2023-11-29T11:53:29.111466Z","shell.execute_reply.started":"2023-11-29T11:53:28.834393Z","shell.execute_reply":"2023-11-29T11:53:29.110298Z"},"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-11-29T11:53:29.112872Z","iopub.execute_input":"2023-11-29T11:53:29.113234Z","iopub.status.idle":"2023-11-29T11:53:29.119840Z","shell.execute_reply.started":"2023-11-29T11:53:29.113201Z","shell.execute_reply":"2023-11-29T11:53:29.118615Z"},"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-11-29T11:53:29.121059Z","iopub.execute_input":"2023-11-29T11:53:29.121355Z","iopub.status.idle":"2023-11-29T11:53:29.133698Z","shell.execute_reply.started":"2023-11-29T11:53:29.121327Z","shell.execute_reply":"2023-11-29T11:53:29.132644Z"},"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-11-29T11:53:29.138418Z","iopub.execute_input":"2023-11-29T11:53:29.139129Z","iopub.status.idle":"2023-11-29T11:53:29.149677Z","shell.execute_reply.started":"2023-11-29T11:53:29.139093Z","shell.execute_reply":"2023-11-29T11:53:29.148622Z"},"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-11-29T11:53:29.151438Z","iopub.execute_input":"2023-11-29T11:53:29.151847Z","iopub.status.idle":"2023-11-29T11:53:29.164418Z","shell.execute_reply.started":"2023-11-29T11:53:29.151811Z","shell.execute_reply":"2023-11-29T11:53:29.163300Z"},"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-11-29T11:53:29.166103Z","iopub.execute_input":"2023-11-29T11:53:29.166649Z","iopub.status.idle":"2023-11-29T11:53:29.179908Z","shell.execute_reply.started":"2023-11-29T11:53:29.166600Z","shell.execute_reply":"2023-11-29T11:53:29.178770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.diagnosis.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-11-29T11:53:29.181177Z","iopub.execute_input":"2023-11-29T11:53:29.181508Z","iopub.status.idle":"2023-11-29T11:53:29.194621Z","shell.execute_reply.started":"2023-11-29T11:53:29.181460Z","shell.execute_reply":"2023-11-29T11:53:29.193626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=dataset.diagnosis)\npyplot.show","metadata":{"execution":{"iopub.status.busy":"2023-11-29T11:53:29.195853Z","iopub.execute_input":"2023-11-29T11:53:29.196219Z","iopub.status.idle":"2023-11-29T11:53:29.444849Z","shell.execute_reply.started":"2023-11-29T11:53:29.196177Z","shell.execute_reply":"2023-11-29T11:53:29.443825Z"},"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-11-29T11:53:29.446356Z","iopub.execute_input":"2023-11-29T11:53:29.446678Z","iopub.status.idle":"2023-11-29T11:53:29.453455Z","shell.execute_reply.started":"2023-11-29T11:53:29.446647Z","shell.execute_reply":"2023-11-29T11:53:29.452188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dataset=[]\ndiagnosis = []","metadata":{"execution":{"iopub.status.busy":"2023-11-29T11:53:29.454633Z","iopub.execute_input":"2023-11-29T11:53:29.454959Z","iopub.status.idle":"2023-11-29T11:53:29.463903Z","shell.execute_reply.started":"2023-11-29T11:53:29.454922Z","shell.execute_reply":"2023-11-29T11:53:29.462701Z"},"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-11-29T11:53:29.465415Z","iopub.execute_input":"2023-11-29T11:53:29.465763Z","iopub.status.idle":"2023-11-29T12:12:27.066822Z","shell.execute_reply.started":"2023-11-29T11:53:29.465731Z","shell.execute_reply":"2023-11-29T12:12:27.065481Z"},"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-11-29T12:12:27.068360Z","iopub.execute_input":"2023-11-29T12:12:27.068729Z","iopub.status.idle":"2023-11-29T12:12:30.581953Z","shell.execute_reply.started":"2023-11-29T12:12:27.068696Z","shell.execute_reply":"2023-11-29T12:12:30.580690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs = np.array(img_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:12:30.583250Z","iopub.execute_input":"2023-11-29T12:12:30.583588Z","iopub.status.idle":"2023-11-29T12:12:30.676826Z","shell.execute_reply.started":"2023-11-29T12:12:30.583555Z","shell.execute_reply":"2023-11-29T12:12:30.675600Z"},"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-11-29T12:12:30.678347Z","iopub.execute_input":"2023-11-29T12:12:30.678711Z","iopub.status.idle":"2023-11-29T12:12:30.757119Z","shell.execute_reply.started":"2023-11-29T12:12:30.678677Z","shell.execute_reply":"2023-11-29T12:12:30.755864Z"},"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-11-29T12:12:30.758669Z","iopub.execute_input":"2023-11-29T12:12:30.759072Z","iopub.status.idle":"2023-11-29T12:12:31.000387Z","shell.execute_reply.started":"2023-11-29T12:12:30.759035Z","shell.execute_reply":"2023-11-29T12:12:30.999076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:12:31.001981Z","iopub.execute_input":"2023-11-29T12:12:31.002336Z","iopub.status.idle":"2023-11-29T12:12:31.009592Z","shell.execute_reply.started":"2023-11-29T12:12:31.002304Z","shell.execute_reply":"2023-11-29T12:12:31.008427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:12:31.010819Z","iopub.execute_input":"2023-11-29T12:12:31.011158Z","iopub.status.idle":"2023-11-29T12:12:31.020374Z","shell.execute_reply.started":"2023-11-29T12:12:31.011117Z","shell.execute_reply":"2023-11-29T12:12:31.019297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_valid.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:12:31.022055Z","iopub.execute_input":"2023-11-29T12:12:31.022343Z","iopub.status.idle":"2023-11-29T12:12:31.030597Z","shell.execute_reply.started":"2023-11-29T12:12:31.022314Z","shell.execute_reply":"2023-11-29T12:12:31.029606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_valid.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:12:31.031915Z","iopub.execute_input":"2023-11-29T12:12:31.032204Z","iopub.status.idle":"2023-11-29T12:12:31.042583Z","shell.execute_reply.started":"2023-11-29T12:12:31.032175Z","shell.execute_reply":"2023-11-29T12:12:31.041649Z"},"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-11-29T12:12:31.043792Z","iopub.execute_input":"2023-11-29T12:12:31.044152Z","iopub.status.idle":"2023-11-29T12:12:31.052604Z","shell.execute_reply.started":"2023-11-29T12:12:31.044121Z","shell.execute_reply":"2023-11-29T12:12:31.051587Z"},"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-11-29T12:12:31.053901Z","iopub.execute_input":"2023-11-29T12:12:31.054255Z","iopub.status.idle":"2023-11-29T12:12:31.067999Z","shell.execute_reply.started":"2023-11-29T12:12:31.054222Z","shell.execute_reply":"2023-11-29T12:12:31.067012Z"},"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-11-29T12:12:31.073979Z","iopub.execute_input":"2023-11-29T12:12:31.074424Z","iopub.status.idle":"2023-11-29T12:12:31.083615Z","shell.execute_reply.started":"2023-11-29T12:12:31.074391Z","shell.execute_reply":"2023-11-29T12:12:31.082529Z"},"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-11-29T12:12:31.084768Z","iopub.execute_input":"2023-11-29T12:12:31.085141Z","iopub.status.idle":"2023-11-29T12:12:34.013249Z","shell.execute_reply.started":"2023-11-29T12:12:31.085097Z","shell.execute_reply":"2023-11-29T12:12:34.012171Z"},"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-11-29T12:12:34.014812Z","iopub.execute_input":"2023-11-29T12:12:34.015281Z","iopub.status.idle":"2023-11-29T12:12:47.285386Z","shell.execute_reply.started":"2023-11-29T12:12:34.015237Z","shell.execute_reply":"2023-11-29T12:12:47.283893Z"},"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-11-29T12:12:47.287156Z","iopub.execute_input":"2023-11-29T12:12:47.287540Z","iopub.status.idle":"2023-11-29T12:15:00.688324Z","shell.execute_reply.started":"2023-11-29T12:12:47.287504Z","shell.execute_reply":"2023-11-29T12:15:00.686841Z"},"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-11-29T12:15:00.689692Z","iopub.execute_input":"2023-11-29T12:15:00.690024Z","iopub.status.idle":"2023-11-29T12:15:01.399261Z","shell.execute_reply.started":"2023-11-29T12:15:00.689992Z","shell.execute_reply":"2023-11-29T12:15:01.398220Z"},"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-11-29T12:15:01.400430Z","iopub.execute_input":"2023-11-29T12:15:01.400721Z","iopub.status.idle":"2023-11-29T12:15:02.456206Z","shell.execute_reply.started":"2023-11-29T12:15:01.400693Z","shell.execute_reply":"2023-11-29T12:15:02.455066Z"},"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-11-29T12:15:02.457657Z","iopub.execute_input":"2023-11-29T12:15:02.457996Z","iopub.status.idle":"2023-11-29T12:15:02.473792Z","shell.execute_reply.started":"2023-11-29T12:15:02.457964Z","shell.execute_reply":"2023-11-29T12:15:02.472666Z"},"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-11-29T12:15:02.475211Z","iopub.execute_input":"2023-11-29T12:15:02.475659Z","iopub.status.idle":"2023-11-29T12:15:02.491382Z","shell.execute_reply.started":"2023-11-29T12:15:02.475611Z","shell.execute_reply":"2023-11-29T12:15:02.490166Z"},"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-11-29T12:15:02.492677Z","iopub.execute_input":"2023-11-29T12:15:02.493014Z","iopub.status.idle":"2023-11-29T12:15:02.539840Z","shell.execute_reply.started":"2023-11-29T12:15:02.492984Z","shell.execute_reply":"2023-11-29T12:15:02.538833Z"},"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-11-29T12:15:02.541260Z","iopub.execute_input":"2023-11-29T12:15:02.541588Z","iopub.status.idle":"2023-11-29T12:22:12.900354Z","shell.execute_reply.started":"2023-11-29T12:15:02.541557Z","shell.execute_reply":"2023-11-29T12:22:12.899372Z"},"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-11-29T12:22:12.902611Z","iopub.execute_input":"2023-11-29T12:22:12.903511Z","iopub.status.idle":"2023-11-29T12:22:13.618394Z","shell.execute_reply.started":"2023-11-29T12:22:12.903465Z","shell.execute_reply":"2023-11-29T12:22:13.617324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"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-11-29T12:22:13.619831Z","iopub.execute_input":"2023-11-29T12:22:13.620243Z","iopub.status.idle":"2023-11-29T12:22:14.575636Z","shell.execute_reply.started":"2023-11-29T12:22:13.620199Z","shell.execute_reply":"2023-11-29T12:22:14.574449Z"},"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-11-29T12:22:14.577354Z","iopub.execute_input":"2023-11-29T12:22:14.577791Z","iopub.status.idle":"2023-11-29T12:22:14.596539Z","shell.execute_reply.started":"2023-11-29T12:22:14.577742Z","shell.execute_reply":"2023-11-29T12:22:14.595256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}