{"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":"markdown","source":"# Preparing Dependencies","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-24T14:19:49.750867Z","iopub.execute_input":"2023-05-24T14:19:49.751228Z","iopub.status.idle":"2023-05-24T14:19:50.856999Z","shell.execute_reply.started":"2023-05-24T14:19:49.751199Z","shell.execute_reply":"2023-05-24T14:19:50.855915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Detect hardware, return appropriate distribution strategy\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy() # default distribution strategy in Tensorflow. Works on CPU and single GPU.\n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:05:12.319434Z","iopub.execute_input":"2023-05-24T13:05:12.320420Z","iopub.status.idle":"2023-05-24T13:05:12.336337Z","shell.execute_reply.started":"2023-05-24T13:05:12.320388Z","shell.execute_reply":"2023-05-24T13:05:12.335140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE\n\n# Configuration\nIMAGE_SIZE = 96, 96\nEPOCHS = 100\nSEED = 123\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:59:04.566681Z","iopub.execute_input":"2023-05-24T13:59:04.567141Z","iopub.status.idle":"2023-05-24T13:59:04.575937Z","shell.execute_reply.started":"2023-05-24T13:59:04.567098Z","shell.execute_reply":"2023-05-24T13:59:04.574828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv')\nsample_submission = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:05:30.596006Z","iopub.execute_input":"2023-05-24T13:05:30.596554Z","iopub.status.idle":"2023-05-24T13:05:31.267726Z","shell.execute_reply.started":"2023-05-24T13:05:30.596514Z","shell.execute_reply":"2023-05-24T13:05:31.266696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:06:04.883802Z","iopub.execute_input":"2023-05-24T13:06:04.884361Z","iopub.status.idle":"2023-05-24T13:06:05.003868Z","shell.execute_reply.started":"2023-05-24T13:06:04.884320Z","shell.execute_reply":"2023-05-24T13:06:05.001448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ntrain_img_path = '/kaggle/input/histopathologic-cancer-detection/train'\nimg_extensions = set()\n\nfor img_path in os.listdir(train_img_path):\n    image_extension = img_path.split('.')[-1] # Split at \".\" and take the last string after split\n    img_extensions.update({image_extension})\n\nprint('Image Formats: ')\nprint('-'.join(list(img_extensions)),'\\n')\nprint(f'Number of Unique Image Extension: {len(img_extensions)}')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:11:38.085755Z","iopub.execute_input":"2023-05-24T13:11:38.086117Z","iopub.status.idle":"2023-05-24T13:11:38.429326Z","shell.execute_reply.started":"2023-05-24T13:11:38.086089Z","shell.execute_reply":"2023-05-24T13:11:38.428356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\n\ntrain_images = glob('/kaggle/input/histopathologic-cancer-detection/train/*.tif')\ntest_images = glob('/kaggle/input/histopathologic-cancer-detection/test/*.tif')\n\nprint(f'Total Training Images: {len(train_images)}')\nprint(f'Total Testing Images: {len(test_images)}')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:28:04.895964Z","iopub.execute_input":"2023-05-24T13:28:04.896330Z","iopub.status.idle":"2023-05-24T13:28:05.995425Z","shell.execute_reply.started":"2023-05-24T13:28:04.896300Z","shell.execute_reply":"2023-05-24T13:28:05.994336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images[:3]","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:34:34.969013Z","iopub.execute_input":"2023-05-24T13:34:34.969383Z","iopub.status.idle":"2023-05-24T13:34:34.976537Z","shell.execute_reply.started":"2023-05-24T13:34:34.969339Z","shell.execute_reply":"2023-05-24T13:34:34.975544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train_labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:33:06.104321Z","iopub.execute_input":"2023-05-24T13:33:06.105446Z","iopub.status.idle":"2023-05-24T13:33:06.122351Z","shell.execute_reply.started":"2023-05-24T13:33:06.105407Z","shell.execute_reply":"2023-05-24T13:33:06.121363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_dictionary = {img_path:label for img_path, label in \n                    zip(train_labels['id'].values, \n                        train_labels['label'].values)\n                   }","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:39:41.936374Z","iopub.execute_input":"2023-05-24T13:39:41.937217Z","iopub.status.idle":"2023-05-24T13:39:42.018242Z","shell.execute_reply.started":"2023-05-24T13:39:41.937184Z","shell.execute_reply":"2023-05-24T13:39:42.017221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images[:1]","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:38:58.933434Z","iopub.execute_input":"2023-05-24T13:38:58.934584Z","iopub.status.idle":"2023-05-24T13:38:58.941623Z","shell.execute_reply.started":"2023-05-24T13:38:58.934538Z","shell.execute_reply":"2023-05-24T13:38:58.940561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nimport skimage.io as io\n\nX = np.array([io.imread(img_path) for img_path in train_images[:25000]])\ny = np.array([label_dictionary[img_path.split('/')[-1][:-4]] for img_path in train_images[:25000]])","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:08:58.406537Z","iopub.execute_input":"2023-05-24T15:08:58.406966Z","iopub.status.idle":"2023-05-24T15:10:20.598392Z","shell.execute_reply.started":"2023-05-24T15:08:58.406934Z","shell.execute_reply":"2023-05-24T15:10:20.597280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=y)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T14:47:40.675988Z","iopub.execute_input":"2023-05-24T14:47:40.676703Z","iopub.status.idle":"2023-05-24T14:47:40.903766Z","shell.execute_reply.started":"2023-05-24T14:47:40.676663Z","shell.execute_reply":"2023-05-24T14:47:40.902544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,stratify=y,random_state=123)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T14:48:14.291201Z","iopub.execute_input":"2023-05-24T14:48:14.291615Z","iopub.status.idle":"2023-05-24T14:48:14.370885Z","shell.execute_reply.started":"2023-05-24T14:48:14.291583Z","shell.execute_reply":"2023-05-24T14:48:14.369722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout, Resizing, Rescaling\nfrom keras.losses import BinaryCrossentropy\nfrom keras.optimizers import SGD\nfrom keras.metrics import BinaryAccuracy, Precision, Recall\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:11:49.136494Z","iopub.execute_input":"2023-05-24T15:11:49.137473Z","iopub.status.idle":"2023-05-24T15:11:49.144156Z","shell.execute_reply.started":"2023-05-24T15:11:49.137438Z","shell.execute_reply":"2023-05-24T15:11:49.143026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess = Sequential([])\npreprocess.add(Resizing(196,196,interpolation='bilinear',input_shape=(None,None,3)))\npreprocess.add(Rescaling(1./255))","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:07:43.165791Z","iopub.execute_input":"2023-05-24T15:07:43.166409Z","iopub.status.idle":"2023-05-24T15:07:43.200132Z","shell.execute_reply.started":"2023-05-24T15:07:43.166365Z","shell.execute_reply":"2023-05-24T15:07:43.199007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(\n    monitor='val_accuracy',\n    min_delta=0.05,\n    patience=10\n)\n\nreduce_lr_on_plateau = ReduceLROnPlateau(\n    factor=0.05,\n    min_lr=0.01,\n    patience=5\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:16:11.924701Z","iopub.execute_input":"2023-05-24T15:16:11.925120Z","iopub.status.idle":"2023-05-24T15:16:11.930893Z","shell.execute_reply.started":"2023-05-24T15:16:11.925087Z","shell.execute_reply":"2023-05-24T15:16:11.929833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([])\n\nmodel.add(preprocess)\nmodel.add(Conv2D(16,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(16,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n# model.add(Conv2D(64,(3,3),activation='relu'))\n# model.add(MaxPooling2D(strides=16,pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(256,activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(256,activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(1,activation='sigmoid'))\n\nmodel.compile(\n    optimizer=SGD(0.03),\n    loss=BinaryCrossentropy(),\n    metrics=[BinaryAccuracy('accuracy')]\n)\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:19:07.852918Z","iopub.execute_input":"2023-05-24T15:19:07.853930Z","iopub.status.idle":"2023-05-24T15:19:08.016349Z","shell.execute_reply.started":"2023-05-24T15:19:07.853896Z","shell.execute_reply":"2023-05-24T15:19:08.015548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    X_train,y_train,\n    validation_data=(X_test,y_test),\n    epochs=EPOCHS,\n    verbose=1,\n    callbacks=[early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:19:11.061121Z","iopub.execute_input":"2023-05-24T15:19:11.061547Z","iopub.status.idle":"2023-05-24T15:20:11.928093Z","shell.execute_reply.started":"2023-05-24T15:19:11.061509Z","shell.execute_reply":"2023-05-24T15:20:11.926856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loss, val_acc = model.evaluate(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T15:20:20.086533Z","iopub.execute_input":"2023-05-24T15:20:20.086919Z","iopub.status.idle":"2023-05-24T15:20:20.982109Z","shell.execute_reply.started":"2023-05-24T15:20:20.086887Z","shell.execute_reply":"2023-05-24T15:20:20.980786Z"},"trusted":true},"execution_count":null,"outputs":[]}]}