{"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 matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2022-02-20T00:55:19.475828Z","iopub.execute_input":"2022-02-20T00:55:19.476064Z","iopub.status.idle":"2022-02-20T00:55:20.564036Z","shell.execute_reply.started":"2022-02-20T00:55:19.476001Z","shell.execute_reply":"2022-02-20T00:55:20.563308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv=pd.read_csv('../input/histopathologic-cancer-detection/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:20.565423Z","iopub.execute_input":"2022-02-20T00:55:20.565667Z","iopub.status.idle":"2022-02-20T00:55:20.906110Z","shell.execute_reply.started":"2022-02-20T00:55:20.565618Z","shell.execute_reply":"2022-02-20T00:55:20.905360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:20.956938Z","iopub.execute_input":"2022-02-20T00:55:20.957651Z","iopub.status.idle":"2022-02-20T00:55:20.967546Z","shell.execute_reply.started":"2022-02-20T00:55:20.957614Z","shell.execute_reply":"2022-02-20T00:55:20.966165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:20.968574Z","iopub.execute_input":"2022-02-20T00:55:20.969365Z","iopub.status.idle":"2022-02-20T00:55:20.974865Z","shell.execute_reply.started":"2022-02-20T00:55:20.969327Z","shell.execute_reply":"2022-02-20T00:55:20.974065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian=trian_csv[trian_csv['label']==0]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:20.907506Z","iopub.execute_input":"2022-02-20T00:55:20.907769Z","iopub.status.idle":"2022-02-20T00:55:20.927148Z","shell.execute_reply.started":"2022-02-20T00:55:20.907716Z","shell.execute_reply":"2022-02-20T00:55:20.926501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:20.984434Z","iopub.execute_input":"2022-02-20T00:55:20.984852Z","iopub.status.idle":"2022-02-20T00:55:20.992507Z","shell.execute_reply.started":"2022-02-20T00:55:20.984818Z","shell.execute_reply":"2022-02-20T00:55:20.991753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_1=trian.iloc[:80000,:]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:20.976158Z","iopub.execute_input":"2022-02-20T00:55:20.976611Z","iopub.status.idle":"2022-02-20T00:55:20.983236Z","shell.execute_reply.started":"2022-02-20T00:55:20.976577Z","shell.execute_reply":"2022-02-20T00:55:20.982532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_1.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:29:58.331808Z","iopub.execute_input":"2022-02-20T01:29:58.332076Z","iopub.status.idle":"2022-02-20T01:29:58.337410Z","shell.execute_reply.started":"2022-02-20T01:29:58.332049Z","shell.execute_reply":"2022-02-20T01:29:58.336548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_2=trian.iloc[80000:87000,:]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.024650Z","iopub.execute_input":"2022-02-20T00:55:21.025097Z","iopub.status.idle":"2022-02-20T00:55:21.030126Z","shell.execute_reply.started":"2022-02-20T00:55:21.025057Z","shell.execute_reply":"2022-02-20T00:55:21.029169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_2.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.031184Z","iopub.execute_input":"2022-02-20T00:55:21.032096Z","iopub.status.idle":"2022-02-20T00:55:21.042438Z","shell.execute_reply.started":"2022-02-20T00:55:21.032063Z","shell.execute_reply":"2022-02-20T00:55:21.041765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fog=trian_csv[trian_csv['label']==1]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:20.929104Z","iopub.execute_input":"2022-02-20T00:55:20.929474Z","iopub.status.idle":"2022-02-20T00:55:20.939542Z","shell.execute_reply.started":"2022-02-20T00:55:20.929413Z","shell.execute_reply":"2022-02-20T00:55:20.938748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fog.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:20.942243Z","iopub.execute_input":"2022-02-20T00:55:20.942430Z","iopub.status.idle":"2022-02-20T00:55:20.955819Z","shell.execute_reply.started":"2022-02-20T00:55:20.942408Z","shell.execute_reply":"2022-02-20T00:55:20.955152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fog.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:30:21.413903Z","iopub.execute_input":"2022-02-20T01:30:21.414172Z","iopub.status.idle":"2022-02-20T01:30:21.420001Z","shell.execute_reply.started":"2022-02-20T01:30:21.414141Z","shell.execute_reply":"2022-02-20T01:30:21.419081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fog_1=fog.iloc[:80000,:]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:20.996178Z","iopub.execute_input":"2022-02-20T00:55:20.996366Z","iopub.status.idle":"2022-02-20T00:55:21.002154Z","shell.execute_reply.started":"2022-02-20T00:55:20.996344Z","shell.execute_reply":"2022-02-20T00:55:21.001444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fog_2=fog.iloc[80000:87000,:]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.003137Z","iopub.execute_input":"2022-02-20T00:55:21.003533Z","iopub.status.idle":"2022-02-20T00:55:21.011198Z","shell.execute_reply.started":"2022-02-20T00:55:21.003495Z","shell.execute_reply":"2022-02-20T00:55:21.010489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fog_2.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.012700Z","iopub.execute_input":"2022-02-20T00:55:21.013163Z","iopub.status.idle":"2022-02-20T00:55:21.022545Z","shell.execute_reply.started":"2022-02-20T00:55:21.013129Z","shell.execute_reply":"2022-02-20T00:55:21.020782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv=pd.concat([trian_1,fog_1])","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.043572Z","iopub.execute_input":"2022-02-20T00:55:21.044334Z","iopub.status.idle":"2022-02-20T00:55:21.057037Z","shell.execute_reply.started":"2022-02-20T00:55:21.044297Z","shell.execute_reply":"2022-02-20T00:55:21.056303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.058140Z","iopub.execute_input":"2022-02-20T00:55:21.058836Z","iopub.status.idle":"2022-02-20T00:55:21.064041Z","shell.execute_reply.started":"2022-02-20T00:55:21.058796Z","shell.execute_reply":"2022-02-20T00:55:21.063357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv=pd.concat([trian_2,fog_2])","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.065225Z","iopub.execute_input":"2022-02-20T00:55:21.065816Z","iopub.status.idle":"2022-02-20T00:55:21.073103Z","shell.execute_reply.started":"2022-02-20T00:55:21.065778Z","shell.execute_reply":"2022-02-20T00:55:21.072422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.074291Z","iopub.execute_input":"2022-02-20T00:55:21.074654Z","iopub.status.idle":"2022-02-20T00:55:21.081382Z","shell.execute_reply.started":"2022-02-20T00:55:21.074617Z","shell.execute_reply":"2022-02-20T00:55:21.080642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.082563Z","iopub.execute_input":"2022-02-20T00:55:21.083231Z","iopub.status.idle":"2022-02-20T00:55:21.109281Z","shell.execute_reply.started":"2022-02-20T00:55:21.083192Z","shell.execute_reply":"2022-02-20T00:55:21.108510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='label',data=trian_csv)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.110709Z","iopub.execute_input":"2022-02-20T00:55:21.111136Z","iopub.status.idle":"2022-02-20T00:55:21.323726Z","shell.execute_reply.started":"2022-02-20T00:55:21.111103Z","shell.execute_reply":"2022-02-20T00:55:21.323064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv[\"id\"] = trian_csv[\"id\"].apply(lambda x: x + \".tif\")\ntrian_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.325011Z","iopub.execute_input":"2022-02-20T00:55:21.325397Z","iopub.status.idle":"2022-02-20T00:55:21.387156Z","shell.execute_reply.started":"2022-02-20T00:55:21.325359Z","shell.execute_reply":"2022-02-20T00:55:21.386353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv['label'] = trian_csv['label'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.388597Z","iopub.execute_input":"2022-02-20T00:55:21.388897Z","iopub.status.idle":"2022-02-20T00:55:21.538479Z","shell.execute_reply.started":"2022-02-20T00:55:21.388859Z","shell.execute_reply":"2022-02-20T00:55:21.537654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.539840Z","iopub.execute_input":"2022-02-20T00:55:21.540306Z","iopub.status.idle":"2022-02-20T00:55:21.550077Z","shell.execute_reply.started":"2022-02-20T00:55:21.540269Z","shell.execute_reply":"2022-02-20T00:55:21.549274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='label',data=test_csv)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.551392Z","iopub.execute_input":"2022-02-20T00:55:21.552347Z","iopub.status.idle":"2022-02-20T00:55:21.715496Z","shell.execute_reply.started":"2022-02-20T00:55:21.552312Z","shell.execute_reply":"2022-02-20T00:55:21.714750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv['label'] = test_csv['label'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.716748Z","iopub.execute_input":"2022-02-20T00:55:21.717007Z","iopub.status.idle":"2022-02-20T00:55:21.735939Z","shell.execute_reply.started":"2022-02-20T00:55:21.716963Z","shell.execute_reply":"2022-02-20T00:55:21.735239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv[\"id\"] = test_csv[\"id\"].apply(lambda x: x + \".tif\")\ntest_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.736989Z","iopub.execute_input":"2022-02-20T00:55:21.737441Z","iopub.status.idle":"2022-02-20T00:55:21.756273Z","shell.execute_reply.started":"2022-02-20T00:55:21.737402Z","shell.execute_reply":"2022-02-20T00:55:21.755480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:55:21.757446Z","iopub.execute_input":"2022-02-20T00:55:21.757820Z","iopub.status.idle":"2022-02-20T00:55:21.764522Z","shell.execute_reply.started":"2022-02-20T00:55:21.757786Z","shell.execute_reply":"2022-02-20T00:55:21.763428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import  preprocess_input ,InceptionV3","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.mobilenet import preprocess_input , MobileNet","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:57:24.592307Z","iopub.execute_input":"2022-02-20T00:57:24.592962Z","iopub.status.idle":"2022-02-20T00:57:24.597361Z","shell.execute_reply.started":"2022-02-20T00:57:24.592905Z","shell.execute_reply":"2022-02-20T00:57:24.596620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:57:35.442874Z","iopub.execute_input":"2022-02-20T00:57:35.443420Z","iopub.status.idle":"2022-02-20T00:57:35.447113Z","shell.execute_reply.started":"2022-02-20T00:57:35.443381Z","shell.execute_reply":"2022-02-20T00:57:35.446226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n            rescale=1./255.0,\n           # preprocessing_function=preprocess_input,\n            rotation_range=10,\n            horizontal_flip=True,\n            vertical_flip=True,\n            validation_split = 0.1\n            )","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:57:57.719678Z","iopub.execute_input":"2022-02-20T00:57:57.720010Z","iopub.status.idle":"2022-02-20T00:57:57.726014Z","shell.execute_reply.started":"2022-02-20T00:57:57.719972Z","shell.execute_reply":"2022-02-20T00:57:57.725362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagen.flow_from_dataframe(\n            dataframe=trian_csv,\n            directory=\"../input/histopathologic-cancer-detection/train/\",\n            x_col=\"id\",\n            y_col=\"label\",\n            subset=\"training\",\n            batch_size=128,\n            seed=42,\n            shuffle=True,\n            class_mode=\"binary\",\n            target_size=(64,64))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T00:58:03.364678Z","iopub.execute_input":"2022-02-20T00:58:03.365352Z","iopub.status.idle":"2022-02-20T01:04:36.963141Z","shell.execute_reply.started":"2022-02-20T00:58:03.365317Z","shell.execute_reply":"2022-02-20T01:04:36.962351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=cv2.imread('../input/histopathologic-cancer-detection/train/00001b2b5609af42ab0ab276dd4cd41c3e7745b5.tif')\nimage.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:04:36.964960Z","iopub.execute_input":"2022-02-20T01:04:36.965187Z","iopub.status.idle":"2022-02-20T01:04:37.003103Z","shell.execute_reply.started":"2022-02-20T01:04:36.965160Z","shell.execute_reply":"2022-02-20T01:04:37.002308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes=list(train_generator.class_indices.keys())\nplt.figure(figsize=(20,20))\nfor X_batch, y_batch in train_generator:\n    # create a grid of 3x3 images\n    for i in range(0,36):\n        plt.subplot(6,6,i+1)\n        plt.imshow(X_batch[i])\n    # show the plot\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:04:37.004526Z","iopub.execute_input":"2022-02-20T01:04:37.004811Z","iopub.status.idle":"2022-02-20T01:04:41.766144Z","shell.execute_reply.started":"2022-02-20T01:04:37.004773Z","shell.execute_reply":"2022-02-20T01:04:41.763814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen_valid = tf.keras.preprocessing.image.ImageDataGenerator(\n            rescale=1./255.,\n   # preprocessing_function=preprocess_input,\n            validation_split = 0.1\n            )","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:04:41.767959Z","iopub.execute_input":"2022-02-20T01:04:41.768194Z","iopub.status.idle":"2022-02-20T01:04:41.773313Z","shell.execute_reply.started":"2022-02-20T01:04:41.768165Z","shell.execute_reply":"2022-02-20T01:04:41.772741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = datagen_valid.flow_from_dataframe(\n            dataframe=trian_csv,\n            directory=\"../input/histopathologic-cancer-detection/train\",\n            x_col=\"id\",\n            y_col=\"label\",\n            subset=\"validation\",\n            batch_size=128,\n            seed=42,\n            shuffle=False,\n            class_mode=\"binary\",\n            target_size=(64,64))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:04:41.774680Z","iopub.execute_input":"2022-02-20T01:04:41.775185Z","iopub.status.idle":"2022-02-20T01:05:32.259360Z","shell.execute_reply.started":"2022-02-20T01:04:41.775150Z","shell.execute_reply":"2022-02-20T01:05:32.258434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nfor X_batch, y_batch in valid_generator:\n    # create a grid of 3x3 images\n    for i in range(0,36):\n        plt.subplot(6,6,i+1)\n        plt.imshow(X_batch[i])\n    # show the plot\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:05:32.260714Z","iopub.execute_input":"2022-02-20T01:05:32.261133Z","iopub.status.idle":"2022-02-20T01:05:36.854997Z","shell.execute_reply.started":"2022-02-20T01:05:32.261094Z","shell.execute_reply":"2022-02-20T01:05:36.854296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen_test = tf.keras.preprocessing.image.ImageDataGenerator(\n            rescale=1./255.\n   # preprocessing_function=preprocess_input\n            )","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:05:36.856167Z","iopub.execute_input":"2022-02-20T01:05:36.857915Z","iopub.status.idle":"2022-02-20T01:05:36.862205Z","shell.execute_reply.started":"2022-02-20T01:05:36.857872Z","shell.execute_reply":"2022-02-20T01:05:36.861279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = datagen_test.flow_from_dataframe(\n            dataframe=test_csv,\n            directory=\"../input/histopathologic-cancer-detection/train\",\n            x_col=\"id\",\n            y_col=\"label\",\n            batch_size=128,\n            seed=42,\n            shuffle=False,\n            class_mode=\"binary\",\n            target_size=(64,64))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:05:36.863469Z","iopub.execute_input":"2022-02-20T01:05:36.863968Z","iopub.status.idle":"2022-02-20T01:06:12.217396Z","shell.execute_reply.started":"2022-02-20T01:05:36.863926Z","shell.execute_reply":"2022-02-20T01:06:12.216550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nfor X_batch, y_batch in test_generator:\n    # create a grid of 3x3 images\n    for i in range(0,36):\n        plt.subplot(6,6,i+1)\n        plt.imshow(X_batch[i])\n        plt.grid(visible=None)\n    # show the plot\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:06:12.218832Z","iopub.execute_input":"2022-02-20T01:06:12.219291Z","iopub.status.idle":"2022-02-20T01:06:17.316900Z","shell.execute_reply.started":"2022-02-20T01:06:12.219247Z","shell.execute_reply":"2022-02-20T01:06:17.314806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense ,Flatten ,Conv2D ,MaxPooling2D ,Dropout ,BatchNormalization ,GlobalMaxPool2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping ,ReduceLROnPlateau ,ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:06:17.319354Z","iopub.execute_input":"2022-02-20T01:06:17.319769Z","iopub.status.idle":"2022-02-20T01:06:17.327716Z","shell.execute_reply.started":"2022-02-20T01:06:17.319717Z","shell.execute_reply":"2022-02-20T01:06:17.326764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#optimizer\noptimizer_adam=Adam(learning_rate=0.001,beta_1=0.9,beta_2=0.99)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:06:17.329066Z","iopub.execute_input":"2022-02-20T01:06:17.329391Z","iopub.status.idle":"2022-02-20T01:06:17.348760Z","shell.execute_reply.started":"2022-02-20T01:06:17.329355Z","shell.execute_reply":"2022-02-20T01:06:17.348018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#callbacks\n\nEarlyStop=EarlyStopping(patience=10,restore_best_weights=True)\nReduce_LR=ReduceLROnPlateau(monitor='val_accuracy',verbose=2,factor=0.5,min_lr=0.00001)\nmodel_check=ModelCheckpoint('model.hdf5',monitor='val_loss',verbose=1,save_best_only=True)\ncallback=[EarlyStop , Reduce_LR,model_check]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:06:17.350065Z","iopub.execute_input":"2022-02-20T01:06:17.350325Z","iopub.status.idle":"2022-02-20T01:06:17.357097Z","shell.execute_reply.started":"2022-02-20T01:06:17.350295Z","shell.execute_reply":"2022-02-20T01:06:17.355975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential([\n                  Conv2D(64,3,activation='relu',kernel_initializer='he_normal',input_shape=(64,64,3)),\n                  BatchNormalization(),\n                  Conv2D(128,3,activation='relu',kernel_initializer='he_normal'),\n                  BatchNormalization(),\n                  MaxPooling2D(3),\n                  Conv2D(256,3,activation='relu'),\n                  BatchNormalization(),\n                  MaxPooling2D(3),\n                  Conv2D(512,3,padding='valid',activation='relu'),\n                  BatchNormalization(),\n                  GlobalMaxPool2D(),\n                  Flatten(),\n                  Dense(512,activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001)),\n                  BatchNormalization(),\n                  Dense(1024,activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001)),\n                  BatchNormalization(),\n                  Dense(1,activation='sigmoid',kernel_initializer='glorot_normal')\n                  \n])\n\nmodel.compile(optimizer=optimizer_adam,loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-20T01:06:17.358413Z","iopub.execute_input":"2022-02-20T01:06:17.358986Z","iopub.status.idle":"2022-02-20T01:06:19.720262Z","shell.execute_reply.started":"2022-02-20T01:06:17.358940Z","shell.execute_reply":"2022-02-20T01:06:19.718581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nMb=MobileNet(include_top=False,weights='imagenet',input_shape=(64,64,3))\nMb.trainable=False\nmodel=Sequential([\n                  Mb,\n                  Flatten(),\n                  Dense(512,activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001)),\n                  BatchNormalization(),\n                  Dense(1024,activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001)),\n                  BatchNormalization(),\n                  Dense(1,activation='sigmoid',kernel_initializer='glorot_normal')\n                  \n])\n\nmodel.compile(optimizer=optimizer_adam,loss='binary_crossentropy',metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2022-02-20T02:03:58.174236Z","iopub.execute_input":"2022-02-20T02:03:58.174584Z","iopub.status.idle":"2022-02-20T02:03:59.501297Z","shell.execute_reply.started":"2022-02-20T02:03:58.174539Z","shell.execute_reply":"2022-02-20T02:03:59.500577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T02:04:20.644635Z","iopub.execute_input":"2022-02-20T02:04:20.644922Z","iopub.status.idle":"2022-02-20T02:04:20.659603Z","shell.execute_reply.started":"2022-02-20T02:04:20.644893Z","shell.execute_reply":"2022-02-20T02:04:20.658860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(train_generator,validation_data=valid_generator,epochs=100,\n                  steps_per_epoch=len(train_generator)//64,validation_steps=len(valid_generator)//64,\n                  callbacks=callback, verbose=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-20T02:08:24.934558Z","iopub.execute_input":"2022-02-20T02:08:24.934864Z","iopub.status.idle":"2022-02-20T02:12:17.749510Z","shell.execute_reply.started":"2022-02-20T02:08:24.934819Z","shell.execute_reply":"2022-02-20T02:12:17.748387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T02:13:28.352180Z","iopub.execute_input":"2022-02-20T02:13:28.352465Z","iopub.status.idle":"2022-02-20T02:14:39.534862Z","shell.execute_reply.started":"2022-02-20T02:13:28.352432Z","shell.execute_reply":"2022-02-20T02:14:39.534171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plotting training values\nimport seaborn as sns\nsns.set()\n\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(loss) + 1)\n\n#accuracy plot\nplt.plot(epochs, acc, color='green', label='Training Accuracy')\nplt.plot(epochs, val_acc, color='blue', label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend()\n\nplt.figure()\n#loss plot\nplt.plot(epochs, loss, color='green', label='Training Loss')\nplt.plot(epochs, val_loss, color='red', label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T02:14:39.536475Z","iopub.execute_input":"2022-02-20T02:14:39.537252Z","iopub.status.idle":"2022-02-20T02:14:40.026326Z","shell.execute_reply.started":"2022-02-20T02:14:39.537211Z","shell.execute_reply":"2022-02-20T02:14:40.025606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T02:14:40.027604Z","iopub.execute_input":"2022-02-20T02:14:40.027875Z","iopub.status.idle":"2022-02-20T02:14:56.095506Z","shell.execute_reply.started":"2022-02-20T02:14:40.027840Z","shell.execute_reply":"2022-02-20T02:14:56.094778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0]","metadata":{"execution":{"iopub.status.busy":"2022-02-20T02:14:56.097609Z","iopub.execute_input":"2022-02-20T02:14:56.097872Z","iopub.status.idle":"2022-02-20T02:14:56.103165Z","shell.execute_reply.started":"2022-02-20T02:14:56.097837Z","shell.execute_reply":"2022-02-20T02:14:56.102451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import confusion_matrix\ncm = confusion_matrix(test_generator.classes, predictions.round())\n\nsns.heatmap(cm,  annot=True, fmt=\"d\" ,cmap=\"YlGnBu\")","metadata":{"execution":{"iopub.status.busy":"2022-02-20T02:14:56.104450Z","iopub.execute_input":"2022-02-20T02:14:56.104861Z","iopub.status.idle":"2022-02-20T02:14:56.388483Z","shell.execute_reply.started":"2022-02-20T02:14:56.104827Z","shell.execute_reply":"2022-02-20T02:14:56.387852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(test_generator.classes, predictions.round()))","metadata":{"execution":{"iopub.status.busy":"2022-02-20T02:14:56.389516Z","iopub.execute_input":"2022-02-20T02:14:56.389771Z","iopub.status.idle":"2022-02-20T02:14:56.426270Z","shell.execute_reply.started":"2022-02-20T02:14:56.389714Z","shell.execute_reply":"2022-02-20T02:14:56.425463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}