{"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":"# 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":"2022-09-26T04:31:02.892454Z","iopub.execute_input":"2022-09-26T04:31:02.892895Z","iopub.status.idle":"2022-09-26T04:31:22.721976Z","shell.execute_reply.started":"2022-09-26T04:31:02.892809Z","shell.execute_reply":"2022-09-26T04:31:22.720758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense,GlobalAveragePooling2D,BatchNormalization,Dropout\nfrom keras.models import Model\nfrom keras import backend as K\nfrom keras.models import load_model\nfrom keras.callbacks import ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:22.724433Z","iopub.execute_input":"2022-09-26T04:31:22.724764Z","iopub.status.idle":"2022-09-26T04:31:28.732084Z","shell.execute_reply.started":"2022-09-26T04:31:22.724733Z","shell.execute_reply":"2022-09-26T04:31:28.731113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")\nlen(x)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:28.733380Z","iopub.execute_input":"2022-09-26T04:31:28.734058Z","iopub.status.idle":"2022-09-26T04:31:28.771767Z","shell.execute_reply.started":"2022-09-26T04:31:28.734018Z","shell.execute_reply":"2022-09-26T04:31:28.770847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:28.774979Z","iopub.execute_input":"2022-09-26T04:31:28.775248Z","iopub.status.idle":"2022-09-26T04:31:28.788829Z","shell.execute_reply.started":"2022-09-26T04:31:28.775223Z","shell.execute_reply":"2022-09-26T04:31:28.787715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fractions=np.array([0.9,0.1])\nx=x.sample(frac=1)\ntrain_df,test_df=np.array_split(x,(fractions[:-1].cumsum()*len(x)).astype(int))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:28.790562Z","iopub.execute_input":"2022-09-26T04:31:28.790924Z","iopub.status.idle":"2022-09-26T04:31:28.800988Z","shell.execute_reply.started":"2022-09-26T04:31:28.790889Z","shell.execute_reply":"2022-09-26T04:31:28.800086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:28.802527Z","iopub.execute_input":"2022-09-26T04:31:28.802915Z","iopub.status.idle":"2022-09-26T04:31:28.812386Z","shell.execute_reply.started":"2022-09-26T04:31:28.802882Z","shell.execute_reply":"2022-09-26T04:31:28.811305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_validate, y_train, y_validate = train_test_split(train_df['image_id'], train_df['label'],\n                                                    stratify=train_df['label'], \n                                                    test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:28.814236Z","iopub.execute_input":"2022-09-26T04:31:28.814738Z","iopub.status.idle":"2022-09-26T04:31:28.977196Z","shell.execute_reply.started":"2022-09-26T04:31:28.814704Z","shell.execute_reply":"2022-09-26T04:31:28.976323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=pd.DataFrame(X_train)\nX_validate=pd.DataFrame(X_validate)\ny_train=pd.DataFrame(y_train)\ny_validate=pd.DataFrame(y_validate)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:28.979523Z","iopub.execute_input":"2022-09-26T04:31:28.980178Z","iopub.status.idle":"2022-09-26T04:31:28.986916Z","shell.execute_reply.started":"2022-09-26T04:31:28.980143Z","shell.execute_reply":"2022-09-26T04:31:28.985983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_validate['label']=y_validate","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:28.988491Z","iopub.execute_input":"2022-09-26T04:31:28.988849Z","iopub.status.idle":"2022-09-26T04:31:28.999446Z","shell.execute_reply.started":"2022-09-26T04:31:28.988816Z","shell.execute_reply":"2022-09-26T04:31:28.998361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_validate.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.004025Z","iopub.execute_input":"2022-09-26T04:31:29.004313Z","iopub.status.idle":"2022-09-26T04:31:29.015823Z","shell.execute_reply.started":"2022-09-26T04:31:29.004281Z","shell.execute_reply":"2022-09-26T04:31:29.014744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train['label']=y_train","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.017589Z","iopub.execute_input":"2022-09-26T04:31:29.018021Z","iopub.status.idle":"2022-09-26T04:31:29.024353Z","shell.execute_reply.started":"2022-09-26T04:31:29.017987Z","shell.execute_reply":"2022-09-26T04:31:29.023336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.025959Z","iopub.execute_input":"2022-09-26T04:31:29.026402Z","iopub.status.idle":"2022-09-26T04:31:29.037990Z","shell.execute_reply.started":"2022-09-26T04:31:29.026261Z","shell.execute_reply":"2022-09-26T04:31:29.036678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=pd.get_dummies(X_train['label'])\ny\nX_train=X_train.drop('label',axis=1)\ntrain_df=pd.concat([X_train,y],axis=1)\ntrain_df.columns=['image_id','Cassava Bacterial Blight (CBB)','Cassava Brown Streak Disease (CBSD)','Cassava Green Mottle (CGM)','Cassava Mosaic Disease (CMD)','Healthy']\n\ntrain_df = train_df.astype({\"image_id\": object, \"Cassava Bacterial Blight (CBB)\": np.float32,\"Cassava Brown Streak Disease (CBSD)\": np.float32,\"Cassava Green Mottle (CGM)\": np.float32,\"Cassava Mosaic Disease (CMD)\": np.float32,\"Healthy\": np.float32})","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.039105Z","iopub.execute_input":"2022-09-26T04:31:29.039361Z","iopub.status.idle":"2022-09-26T04:31:29.054960Z","shell.execute_reply.started":"2022-09-26T04:31:29.039337Z","shell.execute_reply":"2022-09-26T04:31:29.053965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=pd.get_dummies(X_validate['label'])\ny\nX_validate=X_validate.drop('label',axis=1)\nvalidate_df=pd.concat([X_validate,y],axis=1)\nvalidate_df.columns=['image_id','Cassava Bacterial Blight (CBB)','Cassava Brown Streak Disease (CBSD)','Cassava Green Mottle (CGM)','Cassava Mosaic Disease (CMD)','Healthy']\n\nvalidate_df = validate_df.astype({\"image_id\": object, \"Cassava Bacterial Blight (CBB)\": np.float32,\"Cassava Brown Streak Disease (CBSD)\": np.float32,\"Cassava Green Mottle (CGM)\": np.float32,\"Cassava Mosaic Disease (CMD)\": np.float32,\"Healthy\": np.float32})","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.056604Z","iopub.execute_input":"2022-09-26T04:31:29.056997Z","iopub.status.idle":"2022-09-26T04:31:29.068129Z","shell.execute_reply.started":"2022-09-26T04:31:29.056964Z","shell.execute_reply":"2022-09-26T04:31:29.067164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=pd.get_dummies(test_df['label'])\ny\ntest_df=test_df.drop('label',axis=1)\ntest_dff=pd.concat([test_df,y],axis=1)\ntest_dff.columns=['image_id','Cassava Bacterial Blight (CBB)','Cassava Brown Streak Disease (CBSD)','Cassava Green Mottle (CGM)','Cassava Mosaic Disease (CMD)','Healthy']\n\ntest_dff = test_dff.astype({\"image_id\": object, \"Cassava Bacterial Blight (CBB)\": np.float32,\"Cassava Brown Streak Disease (CBSD)\": np.float32,\"Cassava Green Mottle (CGM)\": np.float32,\"Cassava Mosaic Disease (CMD)\": np.float32,\"Healthy\": np.float32})","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.069352Z","iopub.execute_input":"2022-09-26T04:31:29.070267Z","iopub.status.idle":"2022-09-26T04:31:29.080971Z","shell.execute_reply.started":"2022-09-26T04:31:29.070230Z","shell.execute_reply":"2022-09-26T04:31:29.080075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_df),len(validate_df),len(test_dff))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.082261Z","iopub.execute_input":"2022-09-26T04:31:29.083117Z","iopub.status.idle":"2022-09-26T04:31:29.089708Z","shell.execute_reply.started":"2022-09-26T04:31:29.083084Z","shell.execute_reply":"2022-09-26T04:31:29.088708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [\n          'Cassava Bacterial Blight (CBB)',\n          'Cassava Brown Streak Disease (CBSD)', 'Cassava Green Mottle (CGM)',\n          'Cassava Mosaic Disease (CMD)',\n          'Healthy'\n]","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.090927Z","iopub.execute_input":"2022-09-26T04:31:29.091921Z","iopub.status.idle":"2022-09-26T04:31:29.099768Z","shell.execute_reply.started":"2022-09-26T04:31:29.091888Z","shell.execute_reply":"2022-09-26T04:31:29.098937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_leakage_check(df1,df2,col):\n    \n    df1_unique=set(df1[col])\n    df2_unique=set(df2[col])\n    \n    same_images_in_both_groups=list(df1_unique.intersection(df2_unique))\n    \n    leakage=len(same_images_in_both_groups)!=0\n    \n    return leakage","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.100855Z","iopub.execute_input":"2022-09-26T04:31:29.101887Z","iopub.status.idle":"2022-09-26T04:31:29.109849Z","shell.execute_reply.started":"2022-09-26T04:31:29.101855Z","shell.execute_reply":"2022-09-26T04:31:29.109033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Leakage between train and valid\",data_leakage_check(train_df,validate_df,'image_id'))\nprint(\"Leakage between train and test\",data_leakage_check(train_df,test_dff,'image_id'))\nprint(\"Leakage between test and valid\",data_leakage_check(test_dff,validate_df,'image_id'))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.111138Z","iopub.execute_input":"2022-09-26T04:31:29.112088Z","iopub.status.idle":"2022-09-26T04:31:29.132966Z","shell.execute_reply.started":"2022-09-26T04:31:29.112054Z","shell.execute_reply":"2022-09-26T04:31:29.131747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_generator(df, image_dir, x_col, y_cols, shuffle=True, batch_size=32, target_w = 320, target_h = 320):\n    \n           \n    print(\"getting train generator...\") \n    \n    image_generator = ImageDataGenerator(\n        rotation_range=90,\n        fill_mode='nearest',\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True,\n        vertical_flip=True,\n        brightness_range=[0.5,1.5],\n        samplewise_center=True,\n        samplewise_std_normalization= True)\n    \n    \n    generator = image_generator.flow_from_dataframe(\n            dataframe=df,\n            directory=image_dir,\n            x_col=x_col,\n            y_col=y_cols,\n            class_mode=\"raw\",\n            batch_size=batch_size,\n            shuffle=shuffle,\n            \n            target_size=(target_w,target_h))\n    \n    return generator","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.134392Z","iopub.execute_input":"2022-09-26T04:31:29.135207Z","iopub.status.idle":"2022-09-26T04:31:29.144550Z","shell.execute_reply.started":"2022-09-26T04:31:29.135172Z","shell.execute_reply":"2022-09-26T04:31:29.143535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_test_and_valid_generator(valid_df, test_df, train_df, image_dir, x_col, y_cols, sample_size=200, batch_size=32, target_w = 320, target_h = 320):\n    \n    print(\"getting train and valid generators...\")\n    \n    raw_train_generator = ImageDataGenerator().flow_from_dataframe(\n        dataframe=train_df, \n        directory=IMAGE_DIR, \n        x_col=\"image_id\", \n        y_col=labels, \n        class_mode=\"raw\", \n        batch_size=sample_size, \n        shuffle=True, \n        target_size=(target_w, target_h))\n    \n    \n    batch = raw_train_generator.next()\n    data_sample = batch[0]\n\n    \n    image_generator = ImageDataGenerator(\n        \n        featurewise_center=True,\n        featurewise_std_normalization= True)\n    \n    \n    image_generator.fit(data_sample)\n\n    \n    valid_generator = image_generator.flow_from_dataframe(\n            dataframe=validate_df,\n            directory=image_dir,\n            x_col=x_col,\n            y_col=y_cols,\n            class_mode=\"raw\",\n            batch_size=batch_size,\n            shuffle=False,\n            \n            target_size=(target_w,target_h))\n\n    test_generator = image_generator.flow_from_dataframe(\n            dataframe=test_df,\n            directory=image_dir,\n            x_col=x_col,\n            y_col=y_cols,\n            class_mode=\"raw\",\n            batch_size=batch_size,\n            shuffle=False,\n            \n            target_size=(target_w,target_h))\n    return valid_generator, test_generator","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.145930Z","iopub.execute_input":"2022-09-26T04:31:29.146851Z","iopub.status.idle":"2022-09-26T04:31:29.159506Z","shell.execute_reply.started":"2022-09-26T04:31:29.146817Z","shell.execute_reply":"2022-09-26T04:31:29.158841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_DIR='../input/cassava-leaf-disease-classification/train_images'\ntrain_generator=get_train_generator(train_df,IMAGE_DIR,\"image_id\",labels)\nvalid_generator,test_generator=get_test_and_valid_generator(validate_df,test_dff,train_df,IMAGE_DIR,\"image_id\",labels)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:29.161520Z","iopub.execute_input":"2022-09-26T04:31:29.161814Z","iopub.status.idle":"2022-09-26T04:31:43.772445Z","shell.execute_reply.started":"2022-09-26T04:31:29.161761Z","shell.execute_reply":"2022-09-26T04:31:43.771415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.xticks(rotation=90)\nplt.bar(x=labels, height=np.mean(train_generator.labels, axis=0),color='cadetblue')\nplt.ylabel(\"Frequency\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:43.774323Z","iopub.execute_input":"2022-09-26T04:31:43.774715Z","iopub.status.idle":"2022-09-26T04:31:43.984205Z","shell.execute_reply.started":"2022-09-26T04:31:43.774660Z","shell.execute_reply":"2022-09-26T04:31:43.983327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def compute_class_freqs(labels):\n    \n    N=len(labels)\n    \n    pos_f=np.sum(labels,axis=0)/N\n    neg_f=1-np.sum(labels,axis=0)/N\n    \n    return pos_f,neg_f","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:43.985603Z","iopub.execute_input":"2022-09-26T04:31:43.985950Z","iopub.status.idle":"2022-09-26T04:31:43.991644Z","shell.execute_reply.started":"2022-09-26T04:31:43.985924Z","shell.execute_reply":"2022-09-26T04:31:43.990664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f_pos,f_neg=compute_class_freqs(train_generator.labels)\nf_pos","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:43.993361Z","iopub.execute_input":"2022-09-26T04:31:43.994093Z","iopub.status.idle":"2022-09-26T04:31:44.002140Z","shell.execute_reply.started":"2022-09-26T04:31:43.994058Z","shell.execute_reply":"2022-09-26T04:31:44.001404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.DataFrame({\"Class\": labels, \"Label\": \"Positive\", \"Value\": f_pos})\ndata = data.append([{\"Class\": labels[l], \"Label\": \"Negative\", \"Value\": v} for l,v in enumerate(f_neg)], ignore_index=True)\nplt.xticks(rotation=90)\nf = sns.barplot(x=\"Class\", y=\"Value\", hue=\"Label\" ,data=data,palette=['#5f9ea0','#87CEEB'])","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:44.003437Z","iopub.execute_input":"2022-09-26T04:31:44.004004Z","iopub.status.idle":"2022-09-26T04:31:44.292541Z","shell.execute_reply.started":"2022-09-26T04:31:44.003968Z","shell.execute_reply":"2022-09-26T04:31:44.291604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pos_weights=f_neg\nneg_weights=f_pos\npos_c=f_pos*pos_weights\nneg_c=f_neg*neg_weights","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:44.294029Z","iopub.execute_input":"2022-09-26T04:31:44.295950Z","iopub.status.idle":"2022-09-26T04:31:44.301265Z","shell.execute_reply.started":"2022-09-26T04:31:44.295912Z","shell.execute_reply":"2022-09-26T04:31:44.300134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.DataFrame({\"Class\": labels, \"Label\": \"Positive\", \"Value\": pos_c})\ndata = data.append([{\"Class\": labels[l], \"Label\": \"Negative\", \"Value\": v} \n                        for l,v in enumerate(neg_c)], ignore_index=True)\nplt.xticks(rotation=90)\nsns.barplot(x=\"Class\", y=\"Value\", hue=\"Label\" ,data=data,palette=['#5f9ea0','#87CEEB']);","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:44.308047Z","iopub.execute_input":"2022-09-26T04:31:44.308342Z","iopub.status.idle":"2022-09-26T04:31:44.571532Z","shell.execute_reply.started":"2022-09-26T04:31:44.308303Z","shell.execute_reply":"2022-09-26T04:31:44.570582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef get_weighted_loss(pos_weights, neg_weights, epsilon=1e-7):\n    \n    def weighted_loss(y_true, y_pred):\n        \n        loss = 0.0\n        \n        for i in range(len(pos_weights)):\n            \n            loss += -(K.mean((pos_weights[i] * y_true[:,i] * K.log(y_pred[:,i] + epsilon)) + (neg_weights[i] * (1-y_true[:,i]) * K.log(1-y_pred[:,i] + epsilon)),axis = 0)) #complete this line\n        return loss\n    \n        \n    return weighted_loss","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:44.572957Z","iopub.execute_input":"2022-09-26T04:31:44.573967Z","iopub.status.idle":"2022-09-26T04:31:44.581670Z","shell.execute_reply.started":"2022-09-26T04:31:44.573930Z","shell.execute_reply":"2022-09-26T04:31:44.580679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sensitivity(y_true, y_pred): \n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    return true_positives / (possible_positives + K.epsilon())","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:44.606875Z","iopub.execute_input":"2022-09-26T04:31:44.607345Z","iopub.status.idle":"2022-09-26T04:31:44.616134Z","shell.execute_reply.started":"2022-09-26T04:31:44.607312Z","shell.execute_reply":"2022-09-26T04:31:44.615076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def specificity(y_true, y_pred):\n    true_negatives = K.sum(K.round(K.clip((1 - y_true) * (1 - y_pred), 0, 1)))\n    possible_negatives = K.sum(K.round(K.clip(1 - y_true, 0, 1)))\n    return true_negatives / (possible_negatives + K.epsilon())","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:44.617762Z","iopub.execute_input":"2022-09-26T04:31:44.618206Z","iopub.status.idle":"2022-09-26T04:31:44.626224Z","shell.execute_reply.started":"2022-09-26T04:31:44.618172Z","shell.execute_reply":"2022-09-26T04:31:44.625242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import DenseNet169\nbase_model = DenseNet169(weights='imagenet', include_top=False,input_shape=(320,320,3))\n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:44.628729Z","iopub.execute_input":"2022-09-26T04:31:44.629485Z","iopub.status.idle":"2022-09-26T04:31:51.533167Z","shell.execute_reply.started":"2022-09-26T04:31:44.629451Z","shell.execute_reply":"2022-09-26T04:31:51.532174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_addons as tfa","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:51.534461Z","iopub.execute_input":"2022-09-26T04:31:51.534828Z","iopub.status.idle":"2022-09-26T04:31:51.661532Z","shell.execute_reply.started":"2022-09-26T04:31:51.534794Z","shell.execute_reply":"2022-09-26T04:31:51.660611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\t\nx=base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dropout(0.6)(x)\n\npredictions=Dense(5,activation='sigmoid')(x)\nimport keras\nmodel=Model(inputs=base_model.input,outputs=predictions)\noptimiser=tf.keras.optimizers.Adam(lr=0.0001)\n\nes = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10, restore_best_weights = True)\n\nmodel.compile(optimizer=tf.keras.optimizers.Adam(\n    learning_rate=0.00001),loss=get_weighted_loss(pos_weights,neg_weights),metrics=['accuracy',specificity,sensitivity])","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:51.662864Z","iopub.execute_input":"2022-09-26T04:31:51.663320Z","iopub.status.idle":"2022-09-26T04:31:51.731632Z","shell.execute_reply.started":"2022-09-26T04:31:51.663284Z","shell.execute_reply":"2022-09-26T04:31:51.730675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator, \n                              validation_data=valid_generator,\n                              steps_per_epoch=int( np.ceil(len(train_df)/ 32) ),\n                    validation_steps=int( np.ceil(len(validate_df)/ 32) ),\n                              epochs = 90,\n                              callbacks=[es]\n                   )\n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T04:31:51.733164Z","iopub.execute_input":"2022-09-26T04:31:51.733814Z","iopub.status.idle":"2022-09-26T09:19:35.636413Z","shell.execute_reply.started":"2022-09-26T04:31:51.733777Z","shell.execute_reply":"2022-09-26T09:19:35.635403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_vals = model.predict_generator(test_generator, steps = len(test_generator))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T09:19:35.637836Z","iopub.execute_input":"2022-09-26T09:19:35.638330Z","iopub.status.idle":"2022-09-26T09:20:25.763412Z","shell.execute_reply.started":"2022-09-26T09:19:35.638291Z","shell.execute_reply":"2022-09-26T09:20:25.762426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from shutil import copyfile","metadata":{"execution":{"iopub.status.busy":"2022-09-26T09:20:25.764909Z","iopub.execute_input":"2022-09-26T09:20:25.765260Z","iopub.status.idle":"2022-09-26T09:20:25.770759Z","shell.execute_reply.started":"2022-09-26T09:20:25.765221Z","shell.execute_reply":"2022-09-26T09:20:25.769746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\ntf.compat.v1.enable_eager_execution()\nimport cv2\nfrom keras.preprocessing import image\nfrom sklearn.metrics import roc_auc_score, roc_curve\nfrom tensorflow.compat.v1.logging import INFO, set_verbosity\n\nrandom.seed(a=None, version=2)\n\nset_verbosity(INFO)\n\n\ndef get_mean_std_per_batch(image_path, df, H=256, W=256):\n    sample_data = []\n    for idx, img in enumerate(df.sample(100)[\"image_id\"].values):\n        \n        sample_data.append(\n            np.array(image.load_img(image_path, target_size=(H, W))))\n\n    mean = np.mean(sample_data[0])\n    std = np.std(sample_data[0])\n    return mean, std\n\n\ndef load_image(img, image_dir, df, preprocess=True, H=256, W=256):\n    \n    img_path = image_dir + img\n    mean, std = get_mean_std_per_batch(img_path, df, H=H, W=W)\n    x = image.load_img(img_path, target_size=(H, W))\n    if preprocess:\n        x -= mean\n        x /= std\n        x = np.expand_dims(x, axis=0)\n    return x\n\nfrom tensorflow import GradientTape\ndef grad_cam(input_model, image, cls, layer_name, H=256, W=256):\n    \n    y_c = input_model.output[0, cls]\n    conv_output = input_model.get_layer(layer_name).output\n    grads = K.gradients(y_c, conv_output)[0]\n\n    gradient_function = K.function([input_model.input], [conv_output, grads])\n\n    output, grads_val = gradient_function([image])\n    output, grads_val = output[0, :], grads_val[0, :, :, :]\n\n    weights = np.mean(grads_val, axis=(0, 1))\n    cam = np.dot(output, weights)\n\n    \n    cam = cv2.resize(cam, (W, H), cv2.INTER_LINEAR)\n    cam = np.maximum(cam, 0)\n    cam = cam / cam.max()\n    return cam\n\n\ndef compute_gradcam(model, img, image_dir, df, labels, selected_labels,\n                    layer_name='bn'):\n    preprocessed_input = load_image(img, image_dir, df)\n    predictions = model.predict(preprocessed_input)\n\n    print(\"Loading original image\")\n    plt.figure(figsize=(15, 10))\n    plt.subplot(151)\n    plt.title(\"Original\")\n    plt.axis('off')\n    plt.imshow(load_image(img, image_dir, df, preprocess=False), cmap='gray')\n\n    j = 1\n    for i in range(len(labels)):\n        if labels[i] in selected_labels:\n            print(f\"Generating gradcam for class {labels[i]}\")\n            gradcam = grad_cam(model, preprocessed_input, i, layer_name)\n            plt.subplot(151 + j)\n            plt.title(f\"{labels[i]}: p={predictions[0][i]:.3f}\")\n            plt.axis('off')\n            plt.imshow(load_image(img, image_dir, df, preprocess=False),\n                       cmap='gray')\n            plt.imshow(gradcam, cmap='jet', alpha=min(0.5, predictions[0][i]))\n            j += 1\n\n\ndef get_roc_curve(labels, predicted_vals, generator):\n    auc_roc_vals = []\n    for i in range(len(labels)):\n        try:\n            gt = generator.labels[:, i]\n            pred = predicted_vals[:, i]\n            auc_roc = roc_auc_score(gt, pred)\n            auc_roc_vals.append(auc_roc)\n            fpr_rf, tpr_rf, _ = roc_curve(gt, pred)\n            plt.figure(1, figsize=(10, 10))\n            plt.plot([0, 1], [0, 1], 'k--')\n            plt.plot(fpr_rf, tpr_rf,\n                     label=labels[i] + \" (\" + str(round(auc_roc, 3)) + \")\")\n            plt.xlabel('False positive rate')\n            plt.ylabel('True positive rate')\n            plt.title('ROC curve')\n            plt.legend(loc='best')\n        except:\n            print(\n                f\"Error in generating ROC curve for {labels[i]}. \"\n                f\"Dataset lacks enough examples.\"\n            )\n    plt.show()\n    return auc_roc_vals","metadata":{"execution":{"iopub.status.busy":"2022-09-26T09:20:25.772577Z","iopub.execute_input":"2022-09-26T09:20:25.773319Z","iopub.status.idle":"2022-09-26T09:20:26.251641Z","shell.execute_reply.started":"2022-09-26T09:20:25.773284Z","shell.execute_reply":"2022-09-26T09:20:26.250680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"auc_rocs =get_roc_curve(labels, predicted_vals, test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T09:20:26.253897Z","iopub.execute_input":"2022-09-26T09:20:26.254588Z","iopub.status.idle":"2022-09-26T09:20:26.538544Z","shell.execute_reply.started":"2022-09-26T09:20:26.254549Z","shell.execute_reply":"2022-09-26T09:20:26.537622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'],color='indianred')\nplt.plot(history.history['val_loss'],color='goldenrod')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T09:20:26.540266Z","iopub.execute_input":"2022-09-26T09:20:26.540968Z","iopub.status.idle":"2022-09-26T09:20:26.734543Z","shell.execute_reply.started":"2022-09-26T09:20:26.540930Z","shell.execute_reply":"2022-09-26T09:20:26.733594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['sensitivity'],color='indianred')\nplt.plot(history.history['val_sensitivity'],color='goldenrod')\nplt.ylabel('Sensitivity')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T09:20:26.736191Z","iopub.execute_input":"2022-09-26T09:20:26.736567Z","iopub.status.idle":"2022-09-26T09:20:26.932544Z","shell.execute_reply.started":"2022-09-26T09:20:26.736530Z","shell.execute_reply":"2022-09-26T09:20:26.931635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['specificity'],color='indianred')\nplt.plot(history.history['val_specificity'],color='goldenrod')\nplt.ylabel('Specificity')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T09:20:26.934050Z","iopub.execute_input":"2022-09-26T09:20:26.934399Z","iopub.status.idle":"2022-09-26T09:20:27.123569Z","shell.execute_reply.started":"2022-09-26T09:20:26.934363Z","shell.execute_reply":"2022-09-26T09:20:27.122615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sensitivity(test_generator.labels,predicted_vals)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T09:20:27.125151Z","iopub.execute_input":"2022-09-26T09:20:27.125839Z","iopub.status.idle":"2022-09-26T09:20:27.135825Z","shell.execute_reply.started":"2022-09-26T09:20:27.125785Z","shell.execute_reply":"2022-09-26T09:20:27.134826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"specificity(test_generator.labels,predicted_vals)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T09:20:27.137516Z","iopub.execute_input":"2022-09-26T09:20:27.137925Z","iopub.status.idle":"2022-09-26T09:20:27.148241Z","shell.execute_reply.started":"2022-09-26T09:20:27.137886Z","shell.execute_reply":"2022-09-26T09:20:27.147173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}