{"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 os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:00:16.146097Z","iopub.execute_input":"2023-01-26T15:00:16.146371Z","iopub.status.idle":"2023-01-26T15:00:20.863613Z","shell.execute_reply.started":"2023-01-26T15:00:16.146297Z","shell.execute_reply":"2023-01-26T15:00:20.862834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:14:09.454609Z","iopub.execute_input":"2023-01-26T15:14:09.454889Z","iopub.status.idle":"2023-01-26T15:14:09.460274Z","shell.execute_reply.started":"2023-01-26T15:14:09.45486Z","shell.execute_reply":"2023-01-26T15:14:09.459378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the data\n\n# Get current working directory\ncurrent_dir = os.getcwd() \n\n# Append data/mnist.npz to the previous path to get the full path\ndata_path = \"/kaggle/input/rsna-breast-cancer-detection/train_images\"","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:00:46.833564Z","iopub.execute_input":"2023-01-26T15:00:46.83385Z","iopub.status.idle":"2023-01-26T15:00:46.838624Z","shell.execute_reply.started":"2023-01-26T15:00:46.833821Z","shell.execute_reply":"2023-01-26T15:00:46.837729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reshape_and_normalize(images):\n    \n    # Reshape the images to add an extra dimension\n    # images = images[..., np.newaxis]\n    \n    # Normalize pixel values\n    images = images / 255.0\n    \n    ### END CODE HERE\n    return images","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:00:57.72957Z","iopub.execute_input":"2023-01-26T15:00:57.730411Z","iopub.status.idle":"2023-01-26T15:00:57.734766Z","shell.execute_reply.started":"2023-01-26T15:00:57.730363Z","shell.execute_reply":"2023-01-26T15:00:57.733727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\")\ntest_csv = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:01:05.656554Z","iopub.execute_input":"2023-01-26T15:01:05.657337Z","iopub.status.idle":"2023-01-26T15:01:05.759249Z","shell.execute_reply.started":"2023-01-26T15:01:05.657301Z","shell.execute_reply":"2023-01-26T15:01:05.758476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:01:08.90057Z","iopub.execute_input":"2023-01-26T15:01:08.900845Z","iopub.status.idle":"2023-01-26T15:01:08.943555Z","shell.execute_reply.started":"2023-01-26T15:01:08.900814Z","shell.execute_reply":"2023-01-26T15:01:08.942806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.site_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:07:03.026928Z","iopub.execute_input":"2023-01-26T15:07:03.027207Z","iopub.status.idle":"2023-01-26T15:07:03.037086Z","shell.execute_reply.started":"2023-01-26T15:07:03.027177Z","shell.execute_reply":"2023-01-26T15:07:03.036314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.machine_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:07:31.159633Z","iopub.execute_input":"2023-01-26T15:07:31.159908Z","iopub.status.idle":"2023-01-26T15:07:31.169793Z","shell.execute_reply.started":"2023-01-26T15:07:31.15988Z","shell.execute_reply":"2023-01-26T15:07:31.16906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:01:17.968493Z","iopub.execute_input":"2023-01-26T15:01:17.968767Z","iopub.status.idle":"2023-01-26T15:01:17.980623Z","shell.execute_reply.started":"2023-01-26T15:01:17.968738Z","shell.execute_reply":"2023-01-26T15:01:17.979958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.columns","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:01:25.851931Z","iopub.execute_input":"2023-01-26T15:01:25.852522Z","iopub.status.idle":"2023-01-26T15:01:25.85864Z","shell.execute_reply.started":"2023-01-26T15:01:25.852483Z","shell.execute_reply":"2023-01-26T15:01:25.857932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.cancer.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:01:30.944449Z","iopub.execute_input":"2023-01-26T15:01:30.944727Z","iopub.status.idle":"2023-01-26T15:01:30.956781Z","shell.execute_reply.started":"2023-01-26T15:01:30.944697Z","shell.execute_reply":"2023-01-26T15:01:30.955988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set(train_csv.patient_id))","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:01:38.893667Z","iopub.execute_input":"2023-01-26T15:01:38.89443Z","iopub.status.idle":"2023-01-26T15:01:38.908322Z","shell.execute_reply.started":"2023-01-26T15:01:38.894388Z","shell.execute_reply":"2023-01-26T15:01:38.907638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.age.hist()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:01:43.764328Z","iopub.execute_input":"2023-01-26T15:01:43.765056Z","iopub.status.idle":"2023-01-26T15:01:44.033444Z","shell.execute_reply.started":"2023-01-26T15:01:43.765021Z","shell.execute_reply":"2023-01-26T15:01:44.032732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.kdeplot(data=train_csv.age, shade=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:05:02.441758Z","iopub.execute_input":"2023-01-26T15:05:02.442051Z","iopub.status.idle":"2023-01-26T15:05:03.051399Z","shell.execute_reply.started":"2023-01-26T15:05:02.442021Z","shell.execute_reply":"2023-01-26T15:05:03.050674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 2D KDE plot\nsns.jointplot(x=train_csv['cancer'], y=train_csv['machine_id'], kind=\"kde\")","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:08:51.098833Z","iopub.execute_input":"2023-01-26T15:08:51.099143Z","iopub.status.idle":"2023-01-26T15:09:47.408643Z","shell.execute_reply.started":"2023-01-26T15:08:51.099093Z","shell.execute_reply":"2023-01-26T15:09:47.407965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 2D KDE plot\nsns.jointplot(x=train_csv['cancer'], y=train_csv['age'], kind=\"kde\")","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:10:27.679652Z","iopub.execute_input":"2023-01-26T15:10:27.67997Z","iopub.status.idle":"2023-01-26T15:11:24.02294Z","shell.execute_reply.started":"2023-01-26T15:10:27.679936Z","shell.execute_reply":"2023-01-26T15:11:24.021311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Histograms for each species\nsns.histplot(data=train_csv, x='age', hue='cancer')\n\n# Add title\nplt.title(\"Histogram of age, by cancer\")","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:14:17.24764Z","iopub.execute_input":"2023-01-26T15:14:17.248244Z","iopub.status.idle":"2023-01-26T15:14:17.895624Z","shell.execute_reply.started":"2023-01-26T15:14:17.248209Z","shell.execute_reply":"2023-01-26T15:14:17.89493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# KDE plots for each species\nsns.kdeplot(data=train_csv, x='age', hue='cancer', shade=True,common_norm=False)\n\n# Add title\nplt.title(\"Distribution Normalized of age, by cancer\")","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:34:55.298893Z","iopub.execute_input":"2023-01-26T15:34:55.299196Z","iopub.status.idle":"2023-01-26T15:34:55.894376Z","shell.execute_reply.started":"2023-01-26T15:34:55.299166Z","shell.execute_reply":"2023-01-26T15:34:55.893667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.laterality.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:36:00.654879Z","iopub.execute_input":"2023-01-26T15:36:00.65518Z","iopub.status.idle":"2023-01-26T15:36:00.665838Z","shell.execute_reply.started":"2023-01-26T15:36:00.65515Z","shell.execute_reply":"2023-01-26T15:36:00.665177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subset_0 = train_csv[train_csv.cancer == 0]\ntrain_subset_1 = train_csv[train_csv.cancer == 1]\nprint(train_subset_0.shape, train_subset_1.shape)\nprint(train_subset_0.laterality.value_counts())\nprint(train_subset_1.laterality.value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:36:40.972086Z","iopub.execute_input":"2023-01-26T15:36:40.972781Z","iopub.status.idle":"2023-01-26T15:36:40.994691Z","shell.execute_reply.started":"2023-01-26T15:36:40.972748Z","shell.execute_reply":"2023-01-26T15:36:40.993813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"take data subset","metadata":{}},{"cell_type":"code","source":"train_subset_0_L = train_subset_0[train_subset_0.laterality == \"L\"].iloc[:588,]\ntrain_subset_0_R = train_subset_0[train_subset_0.laterality == \"R\"].iloc[:570,]\ntrain_subset_main = pd.concat([train_subset_0_L, train_subset_0_R, train_subset_1])","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:43:49.706274Z","iopub.execute_input":"2023-01-26T15:43:49.706551Z","iopub.status.idle":"2023-01-26T15:43:49.734243Z","shell.execute_reply.started":"2023-01-26T15:43:49.706524Z","shell.execute_reply":"2023-01-26T15:43:49.733485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subset_main","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:44:14.766783Z","iopub.execute_input":"2023-01-26T15:44:14.767503Z","iopub.status.idle":"2023-01-26T15:44:14.796393Z","shell.execute_reply.started":"2023-01-26T15:44:14.767466Z","shell.execute_reply":"2023-01-26T15:44:14.795505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subset_main.laterality.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:44:23.569255Z","iopub.execute_input":"2023-01-26T15:44:23.569892Z","iopub.status.idle":"2023-01-26T15:44:23.578071Z","shell.execute_reply.started":"2023-01-26T15:44:23.569855Z","shell.execute_reply":"2023-01-26T15:44:23.57699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subset_main.cancer.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T15:44:48.088417Z","iopub.execute_input":"2023-01-26T15:44:48.088682Z","iopub.status.idle":"2023-01-26T15:44:48.097698Z","shell.execute_reply.started":"2023-01-26T15:44:48.088655Z","shell.execute_reply":"2023-01-26T15:44:48.096981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subset_main.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:01:50.223382Z","iopub.execute_input":"2023-01-26T16:01:50.223674Z","iopub.status.idle":"2023-01-26T16:01:50.22918Z","shell.execute_reply.started":"2023-01-26T16:01:50.223644Z","shell.execute_reply":"2023-01-26T16:01:50.228177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/working/input_transformed/')","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:01:57.277075Z","iopub.execute_input":"2023-01-26T16:01:57.277369Z","iopub.status.idle":"2023-01-26T16:01:57.282178Z","shell.execute_reply.started":"2023-01-26T16:01:57.27734Z","shell.execute_reply":"2023-01-26T16:01:57.281318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/working/input_transformed/0/')\nos.mkdir('/kaggle/working/input_transformed/1/')","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:06:33.963174Z","iopub.execute_input":"2023-01-26T16:06:33.963474Z","iopub.status.idle":"2023-01-26T16:06:33.984893Z","shell.execute_reply.started":"2023-01-26T16:06:33.963433Z","shell.execute_reply":"2023-01-26T16:06:33.9837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nfrom tqdm import tqdm\n# shutil.copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:06:51.495992Z","iopub.execute_input":"2023-01-26T16:06:51.496618Z","iopub.status.idle":"2023-01-26T16:06:51.500457Z","shell.execute_reply.started":"2023-01-26T16:06:51.496583Z","shell.execute_reply":"2023-01-26T16:06:51.499527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p_id = train_subset_main.patient_id\ni_id = train_subset_main.image_id\ncncr = train_subset_main.cancer\nfor pp, ii, cc in tqdm(zip(p_id, i_id, cncr)):\n    tmpFile = str(pp) + \"_\" + str(ii) + \".png\"\n    tmpSrc = \"/kaggle/input/rsna-breast-cancer-512-pngs/\" + tmpFile\n    tmpDst = \"/kaggle/working/input_transformed/\" + str(cc) + \"/\" + tmpFile\n    shutil.copyfile(tmpSrc, tmpDst)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:08:48.694646Z","iopub.execute_input":"2023-01-26T16:08:48.694951Z","iopub.status.idle":"2023-01-26T16:08:58.726451Z","shell.execute_reply.started":"2023-01-26T16:08:48.694919Z","shell.execute_reply":"2023-01-26T16:08:58.725588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.keras.preprocessing.image_dataset_from_directory(\n    \"/kaggle/working/input_transformed/\",\n    color_mode='rgb',\n    image_size=(512, 512),\n    shuffle=True,\n    validation_split=0.2,\n    subset=\"training\",\n    seed=2023)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:17:34.115552Z","iopub.execute_input":"2023-01-26T16:17:34.115839Z","iopub.status.idle":"2023-01-26T16:17:36.285158Z","shell.execute_reply.started":"2023-01-26T16:17:34.11581Z","shell.execute_reply":"2023-01-26T16:17:36.284385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_ds = tf.keras.preprocessing.image_dataset_from_directory(\n    \"/kaggle/working/input_transformed/\",\n    color_mode='rgb',\n    image_size=(512, 512),\n    shuffle=True,\n    validation_split=0.2,\n    subset=\"validation\",\n    seed=2023)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:18:09.291904Z","iopub.execute_input":"2023-01-26T16:18:09.292195Z","iopub.status.idle":"2023-01-26T16:18:09.415506Z","shell.execute_reply.started":"2023-01-26T16:18:09.292164Z","shell.execute_reply":"2023-01-26T16:18:09.414688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Dropout","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:05:32.663243Z","iopub.execute_input":"2023-01-26T17:05:32.663835Z","iopub.status.idle":"2023-01-26T17:05:32.667636Z","shell.execute_reply.started":"2023-01-26T17:05:32.663796Z","shell.execute_reply":"2023-01-26T17:05:32.666945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 2\n\nmodel = Sequential([\n  layers.experimental.preprocessing.Rescaling(1./255, input_shape=(512, 512, 3)),\n  layers.Conv2D(16, 3, padding='same', activation='relu'),  \n  layers.MaxPooling2D(),   \n  layers.Conv2D(32, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Dropout(0.2),\n  layers.Conv2D(64, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(128, activation='relu'),\n  layers.Dense(num_classes)\n])\n","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:12:51.628201Z","iopub.execute_input":"2023-01-26T17:12:51.62849Z","iopub.status.idle":"2023-01-26T17:12:51.692107Z","shell.execute_reply.started":"2023-01-26T17:12:51.628461Z","shell.execute_reply":"2023-01-26T17:12:51.691424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:12:55.537501Z","iopub.execute_input":"2023-01-26T17:12:55.537777Z","iopub.status.idle":"2023-01-26T17:12:55.550779Z","shell.execute_reply.started":"2023-01-26T17:12:55.537749Z","shell.execute_reply":"2023-01-26T17:12:55.549993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:13:27.976515Z","iopub.execute_input":"2023-01-26T17:13:27.97687Z","iopub.status.idle":"2023-01-26T17:13:27.990042Z","shell.execute_reply.started":"2023-01-26T17:13:27.976829Z","shell.execute_reply":"2023-01-26T17:13:27.989269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class myCallback(tf.keras.callbacks.Callback):\n    # Define the method that checks the accuracy at the end of each epoch\n    def on_epoch_end(self, epoch, logs={}):\n        if logs.get('accuracy') is not None and logs.get('accuracy') >= 0.95:\n            print(\"\\nReached 99.5% accuracy so cancelling training!\") \n            # Stop training once the above condition is met\n            self.model.stop_training = True","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:20:42.217783Z","iopub.execute_input":"2023-01-26T16:20:42.218051Z","iopub.status.idle":"2023-01-26T16:20:42.225649Z","shell.execute_reply.started":"2023-01-26T16:20:42.218021Z","shell.execute_reply":"2023-01-26T16:20:42.224796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=5\nhistory = model.fit(\n  train_ds,\n  validation_data=valid_ds,\n  epochs=epochs\n)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:14:31.228073Z","iopub.execute_input":"2023-01-26T17:14:31.228824Z","iopub.status.idle":"2023-01-26T17:16:03.44812Z","shell.execute_reply.started":"2023-01-26T17:14:31.228794Z","shell.execute_reply":"2023-01-26T17:16:03.447388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:16:18.250009Z","iopub.execute_input":"2023-01-26T17:16:18.250642Z","iopub.status.idle":"2023-01-26T17:16:18.596317Z","shell.execute_reply.started":"2023-01-26T17:16:18.250609Z","shell.execute_reply":"2023-01-26T17:16:18.595594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np\nfrom skimage import transform\ndef load(filename):\n   np_image = Image.open(filename)\n   np_image = np.array(np_image).astype('float32')/255\n   np_image = transform.resize(np_image, (512, 512, 3))\n   np_image = np.expand_dims(np_image, axis=0)\n   return np_image","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:16:26.27393Z","iopub.execute_input":"2023-01-26T17:16:26.274537Z","iopub.status.idle":"2023-01-26T17:16:26.279813Z","shell.execute_reply.started":"2023-01-26T17:16:26.274499Z","shell.execute_reply":"2023-01-26T17:16:26.279134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdata_path = \"/kaggle/input/rsna-screen-breast-cancer-detect-testdata-512x512\"\npred_dict = dict()\nfor ii in os.listdir(testdata_path):\n    tmpPath = testdata_path + \"/\" + ii\n    image = load(tmpPath)\n    predictions = model.predict(image)\n    score = tf.nn.softmax(predictions[0])\n    pred_dict[ii] = float(max(score))","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:16:28.620316Z","iopub.execute_input":"2023-01-26T17:16:28.620598Z","iopub.status.idle":"2023-01-26T17:16:29.249481Z","shell.execute_reply.started":"2023-01-26T17:16:28.620569Z","shell.execute_reply":"2023-01-26T17:16:29.248695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pprint import pprint\ntestD = {\"10008_L\":{\"736471439.png\":0, \"1591370361.png\":0}, \n         \"10008_R\":{\"68070693.png\":0,\"361203119.png\":0}}\n\nfor k1 in testD:\n    for k2 in testD[k1]:\n        testD[k1][k2] = pred_dict[k2]\n\npprint(testD)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:16:31.184317Z","iopub.execute_input":"2023-01-26T17:16:31.184591Z","iopub.status.idle":"2023-01-26T17:16:31.192886Z","shell.execute_reply.started":"2023-01-26T17:16:31.184564Z","shell.execute_reply":"2023-01-26T17:16:31.191894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv\")\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:16:33.910898Z","iopub.execute_input":"2023-01-26T17:16:33.911832Z","iopub.status.idle":"2023-01-26T17:16:33.927404Z","shell.execute_reply.started":"2023-01-26T17:16:33.911785Z","shell.execute_reply":"2023-01-26T17:16:33.926752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for jj in range(submission.shape[0]):\n    tmpKey = submission.prediction_id.iloc[jj]\n    submission.cancer.iloc[jj] = np.mean(list(testD[tmpKey].values()))","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:16:36.763231Z","iopub.execute_input":"2023-01-26T17:16:36.763496Z","iopub.status.idle":"2023-01-26T17:16:36.772174Z","shell.execute_reply.started":"2023-01-26T17:16:36.763469Z","shell.execute_reply":"2023-01-26T17:16:36.770427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\")\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-01-26T17:16:43.59622Z","iopub.execute_input":"2023-01-26T17:16:43.596493Z","iopub.status.idle":"2023-01-26T17:16:43.608367Z","shell.execute_reply.started":"2023-01-26T17:16:43.596465Z","shell.execute_reply":"2023-01-26T17:16:43.607669Z"},"trusted":true},"execution_count":null,"outputs":[]}]}