{"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":"a7149936-f924-4700-a2ce-aa4cffe91b0a","_cell_guid":"cc56a0bd-4602-4c6b-8a32-b7014af58e08","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T22:57:50.847704Z","iopub.execute_input":"2023-02-07T22:57:50.848070Z","iopub.status.idle":"2023-02-07T23:00:27.038425Z","shell.execute_reply.started":"2023-02-07T22:57:50.848043Z","shell.execute_reply":"2023-02-07T23:00:27.033695Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{"_uuid":"a14423d9-c739-40ab-9f91-99f252e927f3","_cell_guid":"bd00c6a6-b8a0-4b73-b8bf-86ae038412ad","trusted":true}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport random\nimport cv2\nimport shutil\n\nfrom sklearn.utils import shuffle\nfrom sklearn.metrics import classification_report, confusion_matrix\n\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport matplotlib.image as mpimg\n\nfrom skimage.filters import gaussian\nfrom skimage.util import random_noise\nfrom skimage.transform import rotate\nfrom skimage import io\n\n\nimport tensorflow as tf\nfrom tensorflow import keras\n\nfrom tensorflow.keras.models import Sequential\n\nfrom tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization\nfrom tensorflow.keras.layers import RandomFlip, RandomRotation, RandomZoom\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, AveragePooling2D\n\nfrom tensorflow.keras.optimizers import Adam\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"634c9c27-b626-4474-b1db-535214f7efc0","_cell_guid":"eadb742c-a9a3-48f1-87c9-4b9f9de2b33c","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.040358Z","iopub.execute_input":"2023-02-07T23:00:27.040727Z","iopub.status.idle":"2023-02-07T23:00:27.056654Z","shell.execute_reply.started":"2023-02-07T23:00:27.040687Z","shell.execute_reply":"2023-02-07T23:00:27.055647Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"_uuid":"6f7a9853-562e-41bd-a650-6c48386fe077","_cell_guid":"b3c4d1dd-606a-4283-a212-c0f3b378f628","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.057583Z","iopub.execute_input":"2023-02-07T23:00:27.057930Z","iopub.status.idle":"2023-02-07T23:00:27.071949Z","shell.execute_reply.started":"2023-02-07T23:00:27.057906Z","shell.execute_reply":"2023-02-07T23:00:27.070004Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_list = os.listdir('../input/histopathologic-cancer-detection/')\ndir_list","metadata":{"_uuid":"6f0d9e49-240c-43b3-9c96-5a0af4b5e0cc","_cell_guid":"87df97ea-797b-440a-9839-982d5858a05c","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.075322Z","iopub.execute_input":"2023-02-07T23:00:27.076072Z","iopub.status.idle":"2023-02-07T23:00:27.083113Z","shell.execute_reply.started":"2023-02-07T23:00:27.076032Z","shell.execute_reply":"2023-02-07T23:00:27.082253Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = '../input/histopathologic-cancer-detection/train/'\ntest = '../input/histopathologic-cancer-detection/test/'\n\ndata_train = pd.read_csv('../input/histopathologic-cancer-detection/train_labels.csv')","metadata":{"_uuid":"640c52ae-f3fb-4ebd-8510-e102e6d434e9","_cell_guid":"88a9a2a6-e3e0-4763-8176-1793397b17fd","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.084449Z","iopub.execute_input":"2023-02-07T23:00:27.084821Z","iopub.status.idle":"2023-02-07T23:00:27.513853Z","shell.execute_reply.started":"2023-02-07T23:00:27.084796Z","shell.execute_reply":"2023-02-07T23:00:27.512962Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir(train)))\nprint(len(os.listdir(test)))","metadata":{"_uuid":"6abd1adb-85bc-42f4-8a57-883c305805a1","_cell_guid":"1d972402-11e7-4e7b-a905-6ab0574995c6","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.515017Z","iopub.execute_input":"2023-02-07T23:00:27.515243Z","iopub.status.idle":"2023-02-07T23:00:27.706754Z","shell.execute_reply.started":"2023-02-07T23:00:27.515221Z","shell.execute_reply":"2023-02-07T23:00:27.705870Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(train)[:5])\nprint(os.listdir(test)[:5])","metadata":{"_uuid":"500fe22a-66c4-4072-82c8-37836f27f5d5","_cell_guid":"162a93e1-5fb1-4c44-bf40-ac348bcde889","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.707877Z","iopub.execute_input":"2023-02-07T23:00:27.708126Z","iopub.status.idle":"2023-02-07T23:00:27.916342Z","shell.execute_reply.started":"2023-02-07T23:00:27.708103Z","shell.execute_reply":"2023-02-07T23:00:27.915391Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.head(5)","metadata":{"_uuid":"181b08fa-b1fa-45fa-9a41-a2766e08cbbd","_cell_guid":"48c0457c-6757-434f-9843-7af0db247d95","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.917815Z","iopub.execute_input":"2023-02-07T23:00:27.918143Z","iopub.status.idle":"2023-02-07T23:00:27.928194Z","shell.execute_reply.started":"2023-02-07T23:00:27.918111Z","shell.execute_reply":"2023-02-07T23:00:27.926848Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.shape","metadata":{"_uuid":"2cee9e4f-3df6-4c53-9877-f7b266e11de0","_cell_guid":"432263d2-d9ce-445a-9905-c97a23f98872","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.931275Z","iopub.execute_input":"2023-02-07T23:00:27.931564Z","iopub.status.idle":"2023-02-07T23:00:27.938968Z","shell.execute_reply.started":"2023-02-07T23:00:27.931539Z","shell.execute_reply":"2023-02-07T23:00:27.938151Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.info()","metadata":{"_uuid":"06db447b-67c8-4cb4-8ad9-b2e3fa0176a7","_cell_guid":"55790634-4082-465c-9893-18b99298a27d","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.942181Z","iopub.execute_input":"2023-02-07T23:00:27.942701Z","iopub.status.idle":"2023-02-07T23:00:27.965415Z","shell.execute_reply.started":"2023-02-07T23:00:27.942676Z","shell.execute_reply":"2023-02-07T23:00:27.964410Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.describe(include = 'all')","metadata":{"_uuid":"81e6a78d-5ba7-4956-a27a-fb1aba2ef0fe","_cell_guid":"dea09c0f-4dab-4a06-97d5-1852b44731c7","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:27.966786Z","iopub.execute_input":"2023-02-07T23:00:27.967101Z","iopub.status.idle":"2023-02-07T23:00:28.183414Z","shell.execute_reply.started":"2023-02-07T23:00:27.967073Z","shell.execute_reply":"2023-02-07T23:00:28.182328Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.label.value_counts()","metadata":{"_uuid":"862a204f-7f3e-450f-b14d-e43ec6e1635b","_cell_guid":"03dfe485-8405-4ad3-b5ec-b431245a6761","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:28.185111Z","iopub.execute_input":"2023-02-07T23:00:28.185400Z","iopub.status.idle":"2023-02-07T23:00:28.195010Z","shell.execute_reply.started":"2023-02-07T23:00:28.185368Z","shell.execute_reply":"2023-02-07T23:00:28.194079Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(data_train.id.duplicated())","metadata":{"_uuid":"5f6cc040-8e1d-4900-9935-56f2c6616b3a","_cell_guid":"e682d95c-23c1-4635-8142-ad00cb7f42ac","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:28.197159Z","iopub.execute_input":"2023-02-07T23:00:28.197577Z","iopub.status.idle":"2023-02-07T23:00:28.249818Z","shell.execute_reply.started":"2023-02-07T23:00:28.197546Z","shell.execute_reply":"2023-02-07T23:00:28.248869Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.hist(bins = 2);","metadata":{"_uuid":"a181ed04-018e-47ae-be3a-e9ae552719de","_cell_guid":"403530a5-f16c-40b0-99d6-a45268872df9","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:28.251367Z","iopub.execute_input":"2023-02-07T23:00:28.251809Z","iopub.status.idle":"2023-02-07T23:00:28.407367Z","shell.execute_reply.started":"2023-02-07T23:00:28.251774Z","shell.execute_reply":"2023-02-07T23:00:28.406160Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = [20,6])\nlabels = data_train.label.value_counts()\n\nlabels.plot(kind = 'pie',autopct = '%1.2f%%', shadow = False, startangle = 0)\nplt.title('Simple Pie Plot',fontsize = 15)\nplt.ylabel('Labels',fontsize = 15);","metadata":{"_uuid":"baf139ae-bb97-49c3-98dd-f25571a118d1","_cell_guid":"ae6b8908-42e8-4e65-9b5c-3feb03166b87","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:28.409476Z","iopub.execute_input":"2023-02-07T23:00:28.409859Z","iopub.status.idle":"2023-02-07T23:00:28.499872Z","shell.execute_reply.started":"2023-02-07T23:00:28.409829Z","shell.execute_reply":"2023-02-07T23:00:28.498662Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"color = sns.color_palette()[0]\n\ndata_train['label'] = data_train.label.replace(0, 'Non Cancerous').replace(1, 'Cancerous')\n\nsns.countplot(data = data_train, x = 'label', color = color);\nplt.title('Simple Plot');\nplt.ylabel('count');\nplt.xlabel('labels');\n\nvalue_sum = data_train['label'].value_counts().sum()\nvalue = data_train['label'].value_counts()\n\nlocs, labels = plt.xticks(rotation = 0) \n\nfor loc, label in zip(locs, labels):\n\n    count = value[label.get_text()]\n    text = '{:0.2f}%'.format(100 * count/value_sum)\n\n    plt.text(loc, count+3, text, ha = 'center', color = 'black');","metadata":{"_uuid":"7a9f8e2f-04c7-45e4-bb95-d1550b0e59cd","_cell_guid":"07a841a8-29a3-4050-b8b9-516e8246f116","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:28.501225Z","iopub.execute_input":"2023-02-07T23:00:28.501515Z","iopub.status.idle":"2023-02-07T23:00:28.786980Z","shell.execute_reply.started":"2023-02-07T23:00:28.501488Z","shell.execute_reply":"2023-02-07T23:00:28.785872Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = np.random.choice(data_train[data_train.label == 'Cancerous'].index, size = 20, replace = False,)\nnc = np.random.choice(data_train[data_train.label == 'Non Cancerous'].index, size = 20, replace = False,)","metadata":{"_uuid":"a1678434-39bb-4bd3-a236-020a7184bfc4","_cell_guid":"ca168bef-4a55-4aa2-a42b-e9722950314f","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:28.788547Z","iopub.execute_input":"2023-02-07T23:00:28.789201Z","iopub.status.idle":"2023-02-07T23:00:28.856300Z","shell.execute_reply.started":"2023-02-07T23:00:28.789171Z","shell.execute_reply":"2023-02-07T23:00:28.855027Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(4, 5, figsize = (20, 20))\n\n\nfor i in range(4):\n    for j in range(5):\n        axis = c[j + 5*i]\n        path = str(train + data_train.id[axis] + '.tif')\n        image = mpimg.imread(path)\n        ax[i,j].imshow(image)\n        ax[i,j].set_title(data_train.label[axis])","metadata":{"_uuid":"9f5a71fd-a805-4eb6-aa7b-c5791bceca98","_cell_guid":"42e10a08-e174-4f42-be4b-61c714edc576","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:28.857984Z","iopub.execute_input":"2023-02-07T23:00:28.858295Z","iopub.status.idle":"2023-02-07T23:00:30.974717Z","shell.execute_reply.started":"2023-02-07T23:00:28.858268Z","shell.execute_reply":"2023-02-07T23:00:30.973665Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(4, 5, figsize = (20, 20))\n\n\nfor i in range(4):\n    for j in range(5):\n        axis = nc[j + 5*i]\n        path = str(train + data_train.id[axis] + '.tif')\n        image = mpimg.imread(path)\n        ax[i,j].imshow(image)\n        ax[i,j].set_title(data_train.label[axis])","metadata":{"_uuid":"f0e22b70-2d88-4298-bad2-d258558d9ab7","_cell_guid":"bd7be012-1a16-4473-a899-f20917e0887a","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:30.975851Z","iopub.execute_input":"2023-02-07T23:00:30.976124Z","iopub.status.idle":"2023-02-07T23:00:33.659816Z","shell.execute_reply.started":"2023-02-07T23:00:30.976099Z","shell.execute_reply":"2023-02-07T23:00:33.658820Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20, 20))\n\nplt.subplot(1, 4, 1)\npath = str(train + data_train.id[0] + '.tif')\nimage = mpimg.imread(path)\nplt.imshow(image)\nplt.title(data_train.label[0] + ' [Normal]');\n\n\nplt.subplot(1, 4, 2)\ng = gaussian(image)\nplt.imshow(g)\nplt.title(data_train.label[0] + ' [Blur]');\n\n\nplt.subplot(1, 4, 3)\nrn = random_noise(image)\nplt.imshow(rn)\nplt.title(data_train.label[0] + ' [Noise]');\n\n\nplt.subplot(1, 4, 4)\nr = rotate(image, 90)\nplt.imshow(r)\nplt.title(data_train.label[0] + ' [Rotate]');","metadata":{"_uuid":"79efc147-76f6-48c9-a8e2-29cf4c3a6d0b","_cell_guid":"02421440-11ad-4df8-ac2c-51ebf3689b3d","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.661099Z","iopub.execute_input":"2023-02-07T23:00:33.661415Z","iopub.status.idle":"2023-02-07T23:00:33.934418Z","shell.execute_reply.started":"2023-02-07T23:00:33.661384Z","shell.execute_reply":"2023-02-07T23:00:33.931493Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Add(x):\n    return x + '.tif'\n\n\ndata_train['id'] = data_train['id'].apply(Add)\n#data_train['label'] = data_train['label'].astype(str)\n\ndata_train = shuffle(data_train, random_state = 42)\n\ndata_train.head()","metadata":{"_uuid":"6e4088bb-0404-4c6c-83b3-1f172de758c0","_cell_guid":"0015684c-a0ac-49c9-9216-9078ab749370","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.935407Z","iopub.status.idle":"2023-02-07T23:00:33.936256Z","shell.execute_reply.started":"2023-02-07T23:00:33.936066Z","shell.execute_reply":"2023-02-07T23:00:33.936085Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_norm = ImageDataGenerator(rescale = 1.0/255, validation_split = 0.20)","metadata":{"_uuid":"a26386b6-e93e-47cd-93ac-61295b00cd8d","_cell_guid":"7ece19cd-fb9b-49ce-8f13-657110f3382f","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.938533Z","iopub.status.idle":"2023-02-07T23:00:33.939069Z","shell.execute_reply.started":"2023-02-07T23:00:33.938852Z","shell.execute_reply":"2023-02-07T23:00:33.938872Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen_train = data_norm.flow_from_dataframe(data_train,\n                                          directory = train,\n                                          x_col = 'id',\n                                          y_col = 'label',\n                                          subset = 'training',\n                                          batch_size = 256,\n                                          class_mode = 'binary',\n                                          seed = 42,\n                                          target_size = (64, 64))","metadata":{"_uuid":"4674f28f-60ca-4402-b2f4-16a9a28a822e","_cell_guid":"62944239-4c54-4e40-96f5-4e96260f13f0","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.940818Z","iopub.status.idle":"2023-02-07T23:00:33.941195Z","shell.execute_reply.started":"2023-02-07T23:00:33.941023Z","shell.execute_reply":"2023-02-07T23:00:33.941042Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ngen_valid = data_norm.flow_from_dataframe(data_train,\n                                          directory = train,\n                                          x_col = 'id',\n                                          y_col = 'label',\n                                          subset = 'validation',\n                                          batch_size = 256,\n                                          class_mode = 'binary',\n                                          seed = 42,\n                                          target_size = (64, 64))","metadata":{"_uuid":"78ddaf64-eeb8-4cda-86e5-2e59619e6ec9","_cell_guid":"68f689d0-fc48-49ab-9a87-48d3f257bc21","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.943137Z","iopub.status.idle":"2023-02-07T23:00:33.943852Z","shell.execute_reply.started":"2023-02-07T23:00:33.943597Z","shell.execute_reply":"2023-02-07T23:00:33.943638Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mod = Sequential()\n\nmod.add(Conv2D(filters = 32, kernel_size = (3, 3), input_shape = (64, 64, 3)))\nmod.add(Conv2D(filters = 32, kernel_size = (3, 3), input_shape = (64, 64, 3)))\nmod.add(MaxPooling2D(pool_size = (2, 2)))\n\nmod.add(Conv2D(filters = 64, kernel_size = (3, 3), input_shape = (64, 64, 3)))\nmod.add(Conv2D(filters = 64, kernel_size = (3, 3), input_shape = (64, 64, 3)))\nmod.add(AveragePooling2D(pool_size = (2, 2)))\n\nmod.add(Flatten())\n\nmod.add(Dense(1, activation = 'sigmoid'))\n\nmod.compile(loss = 'binary_crossentropy', metrics = ['accuracy'])\n\nmod.summary()","metadata":{"_uuid":"6aeaea16-a6b5-4cd7-afe1-7837bf8208f3","_cell_guid":"a7bebd4c-6f0e-410d-b88c-2bf4eaa273ee","execution":{"iopub.status.busy":"2023-02-07T23:00:33.944942Z","iopub.status.idle":"2023-02-07T23:00:33.946010Z","shell.execute_reply.started":"2023-02-07T23:00:33.945769Z","shell.execute_reply":"2023-02-07T23:00:33.945791Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit = mod.fit_generator(gen_train, epochs = 10, validation_data = gen_valid)\nfit","metadata":{"_uuid":"5fd2d0f0-9648-4ea3-8a5f-090c5b330f8e","_cell_guid":"b1ebd3f0-ed75-433a-b7af-1a76ab550f35","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.947106Z","iopub.status.idle":"2023-02-07T23:00:33.948371Z","shell.execute_reply.started":"2023-02-07T23:00:33.948113Z","shell.execute_reply":"2023-02-07T23:00:33.948141Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit.history","metadata":{"_uuid":"799cde71-463a-4b25-b498-2c0eb0cc8bd3","_cell_guid":"38799d9c-2fdd-4488-933d-3ac26c5d881b","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.949746Z","iopub.status.idle":"2023-02-07T23:00:33.950100Z","shell.execute_reply.started":"2023-02-07T23:00:33.949936Z","shell.execute_reply":"2023-02-07T23:00:33.949952Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(fit.history)\ndf","metadata":{"_uuid":"40cf2717-1d54-4069-a27d-185169b63e3a","_cell_guid":"39fbeba3-e8ef-4130-9ebd-71e9338cff58","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.951207Z","iopub.status.idle":"2023-02-07T23:00:33.951524Z","shell.execute_reply.started":"2023-02-07T23:00:33.951367Z","shell.execute_reply":"2023-02-07T23:00:33.951382Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (14, 7))\n\nplt.subplot(1, 2, 1)\nplt.plot(df['accuracy'])\nplt.plot(df['val_accuracy'])\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(['train', 'validate'], loc = 'lower right');\n\nplt.subplot(1, 2, 2)\nplt.plot(df['loss'])\nplt.plot(df['val_loss'])\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(['train', 'validate'], loc = 'upper right');","metadata":{"_uuid":"e1a57584-4c99-49f4-90b5-88c00640daa2","_cell_guid":"6d49173d-62fd-4579-a516-f99cf8d84151","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.952472Z","iopub.status.idle":"2023-02-07T23:00:33.952864Z","shell.execute_reply.started":"2023-02-07T23:00:33.952709Z","shell.execute_reply":"2023-02-07T23:00:33.952724Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (3, 3), strides = 1, input_shape = (64, 64, 3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3, 3), strides = 1, input_shape = (64, 64, 3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3, 3), strides = 1, input_shape = (64, 64, 3), activation = 'relu'))\nmodel.add(AveragePooling2D(pool_size = (2, 2)))\nmodel.add(BatchNormalization())\n\nmodel.add(Dropout(0.1))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(1, activation = 'sigmoid'))\n\nopt = Adam(learning_rate = 0.001)\n\nmodel.compile(loss = 'binary_crossentropy', metrics = ['accuracy'], optimizer = opt)\n\nmodel.summary()","metadata":{"_uuid":"1bbeb5a6-a191-4669-bd57-28e8007384eb","_cell_guid":"46184ee8-fbba-4ad3-bab8-e517faffad67","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.954248Z","iopub.status.idle":"2023-02-07T23:00:33.954555Z","shell.execute_reply.started":"2023-02-07T23:00:33.954401Z","shell.execute_reply":"2023-02-07T23:00:33.954415Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit_mod = model.fit_generator(gen_train, epochs = 10, validation_data = gen_valid)\nfit_mod","metadata":{"_uuid":"76c50c93-daab-4e44-9422-dc345125e410","_cell_guid":"1efd06ba-7622-4af9-930f-a01af6123be3","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.955631Z","iopub.status.idle":"2023-02-07T23:00:33.955928Z","shell.execute_reply.started":"2023-02-07T23:00:33.955783Z","shell.execute_reply":"2023-02-07T23:00:33.955796Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1 = pd.DataFrame(fit_mod.history)\ndf_1","metadata":{"_uuid":"1328382a-bf58-456d-82ea-15ec78926e83","_cell_guid":"2acfb0ef-8e5e-4ee9-9694-b6d059fab65b","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.956943Z","iopub.status.idle":"2023-02-07T23:00:33.957230Z","shell.execute_reply.started":"2023-02-07T23:00:33.957086Z","shell.execute_reply":"2023-02-07T23:00:33.957100Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10, 5))\n\nplt.subplot(1, 2, 1)\nplt.plot(df_1['accuracy'])\nplt.plot(df_1['val_accuracy'])\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(['train', 'validate']);\n\nplt.subplot(1, 2, 2)\nplt.plot(df_1['loss'])\nplt.plot(df_1['val_loss'])\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(['train', 'validate']);","metadata":{"_uuid":"bbbc7e4b-b26a-4b80-88e0-890f5774a4fe","_cell_guid":"09b6be72-591e-4632-a376-cb92a7476b04","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.958304Z","iopub.status.idle":"2023-02-07T23:00:33.958601Z","shell.execute_reply.started":"2023-02-07T23:00:33.958445Z","shell.execute_reply":"2023-02-07T23:00:33.958458Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = gen_valid.classes\n\ny_pred = model.predict(gen_valid, verbose = 1)\ny_pred = list(map(lambda x: 0 if x < 0.5 else 1, y_pred))","metadata":{"_uuid":"f76f611d-fd1f-413a-ac92-883602f89b8a","_cell_guid":"8cfd211f-91d9-4fa5-b5d8-a5f821e50eb9","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.960008Z","iopub.status.idle":"2023-02-07T23:00:33.960305Z","shell.execute_reply.started":"2023-02-07T23:00:33.960161Z","shell.execute_reply":"2023-02-07T23:00:33.960175Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y, y_pred))","metadata":{"_uuid":"b7fa3a44-028a-497f-8043-cf279f8aa367","_cell_guid":"c91f5c54-9bfd-412b-95d2-f30c14db8dce","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.961328Z","iopub.status.idle":"2023-02-07T23:00:33.961671Z","shell.execute_reply.started":"2023-02-07T23:00:33.961476Z","shell.execute_reply":"2023-02-07T23:00:33.961489Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"con_mat = confusion_matrix(y, y_pred)\nprint(con_mat)\n\nsns.heatmap(con_mat, annot = True);","metadata":{"_uuid":"4af0949b-27c0-48b7-bb38-3062fb58ba5d","_cell_guid":"8091be8b-eb1f-446e-93b8-5d85a209e25f","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.962562Z","iopub.status.idle":"2023-02-07T23:00:33.962901Z","shell.execute_reply.started":"2023-02-07T23:00:33.962751Z","shell.execute_reply":"2023-02-07T23:00:33.962765Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test = pd.DataFrame({'id': os.listdir(test)})\ndata_test","metadata":{"_uuid":"e15dbf5c-1ad1-4ef0-8fc3-3998c9ea3fd8","_cell_guid":"e588b411-bc6f-44f5-97a3-3a5c0a1d32bf","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.964347Z","iopub.status.idle":"2023-02-07T23:00:33.964679Z","shell.execute_reply.started":"2023-02-07T23:00:33.964499Z","shell.execute_reply":"2023-02-07T23:00:33.964514Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_norm = ImageDataGenerator(rescale = 1.0/255)\n\ngen_test = data_norm.flow_from_dataframe(data_test,\n                                          directory = test,\n                                          x_col = 'id',\n                                          batch_size = 1,\n                                          class_mode = None,\n                                          seed = 42,\n                                          target_size = (64, 64))","metadata":{"_uuid":"3c330c2e-9dd9-45f9-958f-d2a92c02675b","_cell_guid":"b65c4901-0aac-404f-9f8f-328ebf59ae73","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.965665Z","iopub.status.idle":"2023-02-07T23:00:33.965950Z","shell.execute_reply.started":"2023-02-07T23:00:33.965806Z","shell.execute_reply":"2023-02-07T23:00:33.965820Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(gen_test, verbose = 1)\npred","metadata":{"_uuid":"80a4a610-5934-4502-8070-d1405e25a812","_cell_guid":"d576c6f5-7d65-443c-af8c-6d3a0db1c4a4","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.967208Z","iopub.status.idle":"2023-02-07T23:00:33.967491Z","shell.execute_reply.started":"2023-02-07T23:00:33.967351Z","shell.execute_reply":"2023-02-07T23:00:33.967364Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pred = np.transpose(pred)\n\ntest_df = data_test.id.apply(lambda x: x.split('.')[0])\ntest_df","metadata":{"_uuid":"a2c08e5b-fd6c-436a-aaa9-96a20018f1b1","_cell_guid":"408e286e-168e-421a-8296-68bea3c5d5cb","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.968443Z","iopub.status.idle":"2023-02-07T23:00:33.968747Z","shell.execute_reply.started":"2023-02-07T23:00:33.968589Z","shell.execute_reply":"2023-02-07T23:00:33.968602Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({'id': test_df, 'label': list(map(lambda x: 0 if x < 0.5 else 1, pred))})\nsubmission_df","metadata":{"_uuid":"e7a6f74d-63cf-4f46-a7b8-4d64df266be5","_cell_guid":"6c97dd0f-52f0-468b-83b7-286b35410087","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.969633Z","iopub.status.idle":"2023-02-07T23:00:33.969912Z","shell.execute_reply.started":"2023-02-07T23:00:33.969777Z","shell.execute_reply":"2023-02-07T23:00:33.969790Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"color = sns.color_palette()[0]\n\nval = submission_df.label.value_counts()\nprint(val)\n\nsns.countplot(data = submission_df, x = 'label', color = color);\nplt.title('Simple Plot');\nplt.ylabel('count');\nplt.xlabel('labels');","metadata":{"_uuid":"893ddc7f-165e-4ef6-98d0-fc5eb40cba39","_cell_guid":"f6328e76-f58a-4e87-aba6-921f05589856","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.971128Z","iopub.status.idle":"2023-02-07T23:00:33.971405Z","shell.execute_reply.started":"2023-02-07T23:00:33.971269Z","shell.execute_reply":"2023-02-07T23:00:33.971282Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index = False)","metadata":{"_uuid":"32b8bae9-4c29-4932-a2fe-1e56560b0700","_cell_guid":"1312b742-efe0-4f02-8cf5-95b30f9efffb","collapsed":false,"execution":{"iopub.status.busy":"2023-02-07T23:00:33.972829Z","iopub.status.idle":"2023-02-07T23:00:33.973112Z","shell.execute_reply.started":"2023-02-07T23:00:33.972970Z","shell.execute_reply":"2023-02-07T23:00:33.972985Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"d2d7ca69-6f10-4fac-bc46-f02f7e3ba331","_cell_guid":"3162642d-6eed-4251-9040-a895091c962c","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}