{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"dockerImageVersionId":30579,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow_io as tfio\nimport matplotlib.image as mpimg\n\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator\n\nfrom keras.models import Sequential\n\nfrom keras.layers import Conv2D, MaxPooling2D, Activation, Flatten, Dropout, Dense\nfrom keras.optimizers import Adam\nfrom pathlib import Path  \nfrom PIL import Image\nimport glob","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-16T03:58:13.162061Z","iopub.execute_input":"2023-11-16T03:58:13.162404Z","iopub.status.idle":"2023-11-16T03:58:16.714421Z","shell.execute_reply.started":"2023-11-16T03:58:13.162377Z","shell.execute_reply":"2023-11-16T03:58:16.713437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the training data into a DataFrame. \n# Print the shape of the resulting DataFrame.\n\nhcd = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv')\nprint(hcd.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:58:16.715915Z","iopub.execute_input":"2023-11-16T03:58:16.716424Z","iopub.status.idle":"2023-11-16T03:58:16.925024Z","shell.execute_reply.started":"2023-11-16T03:58:16.716397Z","shell.execute_reply":"2023-11-16T03:58:16.924019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the head of the train DataFrame. \nhcd.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:58:16.926478Z","iopub.execute_input":"2023-11-16T03:58:16.926834Z","iopub.status.idle":"2023-11-16T03:58:16.938860Z","shell.execute_reply.started":"2023-11-16T03:58:16.926806Z","shell.execute_reply":"2023-11-16T03:58:16.938106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#label distrobution\n(hcd.label.value_counts() / len(hcd)).to_frame()","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:58:16.940709Z","iopub.execute_input":"2023-11-16T03:58:16.941640Z","iopub.status.idle":"2023-11-16T03:58:16.955947Z","shell.execute_reply.started":"2023-11-16T03:58:16.941614Z","shell.execute_reply":"2023-11-16T03:58:16.954918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adding a variable for the image directory\nimg_dir = '/kaggle/input/histopathologic-cancer-detection/train'\n","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:58:16.957200Z","iopub.execute_input":"2023-11-16T03:58:16.957571Z","iopub.status.idle":"2023-11-16T03:58:16.968202Z","shell.execute_reply.started":"2023-11-16T03:58:16.957541Z","shell.execute_reply":"2023-11-16T03:58:16.966839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tiff_imgs = list(Path(img_dir).glob(\"*.tiff\"))\ndecoded_images = []\n\nfor tiff_img in tiff_imgs: \n    with tf.io.gfile.GFile(tiff_img, 'rb') as f:\n        contents = f.read()\n    decoded_image = tfio_image.decode_tiff(contents)\n\n    decoded_images.append(decoded_image)","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:58:16.970353Z","iopub.execute_input":"2023-11-16T03:58:16.970846Z","iopub.status.idle":"2023-11-16T03:58:20.047096Z","shell.execute_reply.started":"2023-11-16T03:58:16.970810Z","shell.execute_reply":"2023-11-16T03:58:20.045968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nimg_height = 180\nimg_width = 180","metadata":{"execution":{"iopub.status.busy":"2023-11-16T03:58:20.048106Z","iopub.execute_input":"2023-11-16T03:58:20.048953Z","iopub.status.idle":"2023-11-16T03:58:20.055119Z","shell.execute_reply.started":"2023-11-16T03:58:20.048882Z","shell.execute_reply":"2023-11-16T03:58:20.053725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = hcd.sample(n=16).reset_index()\n\nplt.figure(figsize=(4,4))\n\nfor i, row in sample.iterrows():\n\n    img = mpimg.imread(f'{img_dir}/{row.id}.tif')    \n    label = row.label\n\n    plt.subplot(4,4,i+1)\n    plt.imshow(img)\n    plt.text(0, -5, f'Class {label}', color='k')\n        \n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-16T04:01:48.840389Z","iopub.execute_input":"2023-11-16T04:01:48.840750Z","iopub.status.idle":"2023-11-16T04:01:49.608506Z","shell.execute_reply.started":"2023-11-16T04:01:48.840722Z","shell.execute_reply":"2023-11-16T04:01:49.607711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}