{"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 numpy as np\nimport pandas as pd\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-10T06:59:57.345044Z","iopub.execute_input":"2022-05-10T06:59:57.345361Z","iopub.status.idle":"2022-05-10T06:59:57.35854Z","shell.execute_reply.started":"2022-05-10T06:59:57.345329Z","shell.execute_reply":"2022-05-10T06:59:57.357607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow as tf\nfrom tensorflow.keras.applications import *\nfrom tensorflow.keras.optimizers import *\nfrom tensorflow.keras.losses import *\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.models import *\nfrom tensorflow.keras.callbacks import *\nfrom tensorflow.keras.preprocessing.image import *\nfrom tensorflow.keras.utils import *\nfrom sklearn.metrics import *\nfrom sklearn.model_selection import *\nimport tensorflow.keras.backend as K\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom glob import glob\nfrom skimage.io import *\n%config Completer.use_jedi = False\nimport warnings\nwarnings.filterwarnings('ignore')\nprint(\"All modules have been imported\")","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:59:57.367839Z","iopub.execute_input":"2022-05-10T06:59:57.368082Z","iopub.status.idle":"2022-05-10T06:59:57.390214Z","shell.execute_reply.started":"2022-05-10T06:59:57.368058Z","shell.execute_reply":"2022-05-10T06:59:57.389254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import itertools\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.figure(figsize = (6,6))\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=90)\n    plt.yticks(tick_marks, classes)\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    cm = np.round(cm,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    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:59:57.391843Z","iopub.execute_input":"2022-05-10T06:59:57.392349Z","iopub.status.idle":"2022-05-10T06:59:57.402528Z","shell.execute_reply.started":"2022-05-10T06:59:57.392314Z","shell.execute_reply":"2022-05-10T06:59:57.401571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"info=pd.read_csv(\"../input/prepossessed-arrays-of-binary-data/1000_Binary Dataframe\")\ninfo=info.drop('Unnamed: 0',axis=1)\ninfo.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:59:57.407576Z","iopub.execute_input":"2022-05-10T06:59:57.407868Z","iopub.status.idle":"2022-05-10T06:59:57.429975Z","shell.execute_reply.started":"2022-05-10T06:59:57.407839Z","shell.execute_reply":"2022-05-10T06:59:57.429137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style('darkgrid')\nfig, ax = plt.subplots(figsize=(10,5))\nsns.barplot(x=info.level.unique(),y=info.level.value_counts(),palette='Blues_r',ax=ax)","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:59:57.431392Z","iopub.execute_input":"2022-05-10T06:59:57.431884Z","iopub.status.idle":"2022-05-10T06:59:57.556776Z","shell.execute_reply.started":"2022-05-10T06:59:57.431849Z","shell.execute_reply":"2022-05-10T06:59:57.555927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sizes = info['level'].values\nsns.distplot(sizes, kde=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:59:57.558384Z","iopub.execute_input":"2022-05-10T06:59:57.558898Z","iopub.status.idle":"2022-05-10T06:59:57.730221Z","shell.execute_reply.started":"2022-05-10T06:59:57.558861Z","shell.execute_reply":"2022-05-10T06:59:57.72943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Binary_90 = np.load('../input/prepossessed-arrays-of-binary-data/1000_Binary_images_data_90.npz')\nX_90=Binary_90['a']\nBinary_128 = np.load('../input/prepossessed-arrays-of-binary-data/1000_Binary_images_data_128.npz')\nX_128=Binary_128['a']\nBinary_264 = np.load('../input/prepossessed-arrays-of-binary-data/1000_Binary_images_data_264.npz')\nX_264=Binary_264['a']\ny=info['level'].values\n\n\nprint(X_90.shape)\nprint(X_128.shape)\nprint(X_264.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-10T06:59:57.731815Z","iopub.execute_input":"2022-05-10T06:59:57.732299Z","iopub.status.idle":"2022-05-10T07:00:07.071098Z","shell.execute_reply.started":"2022-05-10T06:59:57.732263Z","shell.execute_reply":"2022-05-10T07:00:07.070141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape before reshaping X_90\" +str(X_90.shape))\nX_90=X_90.reshape(1000,90,90,3)\nprint(\"Shape after reshaping X_90\" +str(X_90.shape))\nprint(\"\\n\\n\")\n\nprint(\"Shape before reshaping X_128\" +str(X_128.shape))\nX_128=X_128.reshape(1000,128,128,3)\nprint(\"Shape after reshaping X_128\" +str(X_128.shape))\nprint(\"\\n\\n\")\n\nprint(\"Shape before reshaping X_264\" +str(X_264.shape))\nX_264=X_264.reshape(1000,264,264,3)\nprint(\"Shape after reshaping X_264\" +str(X_264.shape))","metadata":{"execution":{"iopub.status.busy":"2022-05-10T07:00:07.072709Z","iopub.execute_input":"2022-05-10T07:00:07.073083Z","iopub.status.idle":"2022-05-10T07:00:07.080885Z","shell.execute_reply.started":"2022-05-10T07:00:07.073042Z","shell.execute_reply":"2022-05-10T07:00:07.079547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title(\"90*90*3 Image\")\nplt.imshow(X_90[1])\nplt.show()\n\nplt.title(\"128*128*3 Image\")\nplt.imshow(X_128[1])\nplt.show()\n\nplt.title(\"264*264*3 Image\")\nplt.imshow(X_264[1])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-10T07:00:07.082984Z","iopub.execute_input":"2022-05-10T07:00:07.083382Z","iopub.status.idle":"2022-05-10T07:00:07.655361Z","shell.execute_reply.started":"2022-05-10T07:00:07.083335Z","shell.execute_reply":"2022-05-10T07:00:07.654481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-10T07:00:07.656758Z","iopub.execute_input":"2022-05-10T07:00:07.657135Z","iopub.status.idle":"2022-05-10T07:00:07.662937Z","shell.execute_reply.started":"2022-05-10T07:00:07.657097Z","shell.execute_reply":"2022-05-10T07:00:07.661784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=np.array(X_264)\nY=np.array(y)\nY=to_categorical(Y,5)\nx_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42, stratify = Y)\n#x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.1, random_state=42)\nprint(len(x_train),len(x_test))","metadata":{"execution":{"iopub.status.busy":"2022-05-10T07:00:07.665843Z","iopub.execute_input":"2022-05-10T07:00:07.666247Z","iopub.status.idle":"2022-05-10T07:00:08.783015Z","shell.execute_reply.started":"2022-05-10T07:00:07.666207Z","shell.execute_reply":"2022-05-10T07:00:08.782158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet50 (7 Dense Layers)","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(ResNet50(input_shape=(264,264,3),include_top=False,weights=None))\nfor layer in model.layers:\n    layer.trainable = False\nmodel.add(Flatten())\nmodel.add(Dense(16,kernel_initializer='he_uniform'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(units= 5, activation='softmax'))\n\nc3=tf.keras.callbacks.ReduceLROnPlateau(\n    monitor=\"val_loss\",\n    factor=0.1,\n    patience=2,\n    mode=\"auto\",\n    min_delta=0.0001,\n    cooldown=0,\n    min_lr=0.001\n)\nmodel.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy','AUC'])\nhistory=model.fit(x_train,y_train,epochs=50,batch_size=16,validation_split=0.2)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-10T07:00:08.784658Z","iopub.execute_input":"2022-05-10T07:00:08.785021Z","iopub.status.idle":"2022-05-10T07:02:18.283135Z","shell.execute_reply.started":"2022-05-10T07:00:08.784984Z","shell.execute_reply":"2022-05-10T07:02:18.282438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=np.argmax(y_test, axis=1)\npred=np.argmax(model.predict(x_test),axis=-1)\ncm=confusion_matrix(y_test,pred)\ncm_plot=plot_confusion_matrix(cm,classes=['0','1'])","metadata":{"execution":{"iopub.status.busy":"2022-05-10T07:02:18.284928Z","iopub.execute_input":"2022-05-10T07:02:18.285258Z","iopub.status.idle":"2022-05-10T07:02:19.96401Z","shell.execute_reply.started":"2022-05-10T07:02:18.285223Z","shell.execute_reply":"2022-05-10T07:02:19.96313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Performance Report:\")\ny_pred6=np.argmax(model.predict(x_test),axis=-1)\nY_test=to_categorical(y_test,5)\ny_pred_prb6=model.predict(x_test)\n#target=['0','1']\nfrom sklearn import metrics\nprint('Accuracy score is :', metrics.accuracy_score(y_test, y_pred6))\nprint('Precision score is :', metrics.precision_score(y_test, y_pred6, average='weighted'))\nprint('Recall score is :',metrics.recall_score(y_test,y_pred6, average='weighted'))\nprint('F1 Score is :', metrics.f1_score(y_test, y_pred6,average='weighted'))\nprint('Cohen Kappa Score:', metrics.cohen_kappa_score(y_test, y_pred6))","metadata":{"execution":{"iopub.status.busy":"2022-05-10T07:02:19.965313Z","iopub.execute_input":"2022-05-10T07:02:19.965837Z","iopub.status.idle":"2022-05-10T07:02:21.44899Z","shell.execute_reply.started":"2022-05-10T07:02:19.965796Z","shell.execute_reply":"2022-05-10T07:02:21.448125Z"},"trusted":true},"execution_count":null,"outputs":[]}]}