{"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":"#Using VGG19 and Fune-tuning  for the dataset","metadata":{"id":"boCebrTALugH"}},{"cell_type":"markdown","source":"#Imports and Data loading","metadata":{"id":"F3MBwZOQL6oZ"}},{"cell_type":"code","source":"import os\nimport gc\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential","metadata":{"id":"bum3T3pDKNRv","outputId":"00a48918-aed5-4ad8-a37a-9fe3c93ebfc3","execution":{"iopub.status.busy":"2023-01-12T20:53:35.89469Z","iopub.execute_input":"2023-01-12T20:53:35.895097Z","iopub.status.idle":"2023-01-12T20:53:46.172679Z","shell.execute_reply.started":"2023-01-12T20:53:35.895007Z","shell.execute_reply":"2023-01-12T20:53:46.171914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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":{"id":"ATKLqIhFKNRz","execution":{"iopub.status.busy":"2023-01-12T20:53:46.177246Z","iopub.execute_input":"2023-01-12T20:53:46.17907Z","iopub.status.idle":"2023-01-12T20:53:46.184232Z","shell.execute_reply.started":"2023-01-12T20:53:46.179031Z","shell.execute_reply":"2023-01-12T20:53:46.183671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We are going to normalise the pixel value between -1 and 1 (i may have an importance or not)","metadata":{"id":"uxI7M9uzL6pb"}},{"cell_type":"code","source":"def reshape_and_normalize(images):\n    \"\"\"Input : image array \n    Resize & normalize pixel value\n    Output : image array \n    \"\"\"\n    # If we wanted to reshape we would add the value here\n    # images = images[..., np.newaxis]\n    # Normalize pixel values\n    images = images  / 255.0\n    return images","metadata":{"id":"_IxCOv6TKNR0","execution":{"iopub.status.busy":"2023-01-12T20:53:46.187964Z","iopub.execute_input":"2023-01-12T20:53:46.190089Z","iopub.status.idle":"2023-01-12T20:53:46.196731Z","shell.execute_reply.started":"2023-01-12T20:53:46.190053Z","shell.execute_reply":"2023-01-12T20:53:46.19586Z"},"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":{"id":"1l-s3VAOKNR1","execution":{"iopub.status.busy":"2023-01-12T20:53:46.200954Z","iopub.execute_input":"2023-01-12T20:53:46.203203Z","iopub.status.idle":"2023-01-12T20:53:46.349457Z","shell.execute_reply.started":"2023-01-12T20:53:46.203169Z","shell.execute_reply":"2023-01-12T20:53:46.348734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Quick Data exploration","metadata":{"id":"RV7cW-EuMfnk"}},{"cell_type":"code","source":"train_csv","metadata":{"id":"mN2w7WjFKNR2","outputId":"82e5953b-b672-4bf3-cef5-f84140a535a8","execution":{"iopub.status.busy":"2023-01-12T20:53:46.350681Z","iopub.execute_input":"2023-01-12T20:53:46.352644Z","iopub.status.idle":"2023-01-12T20:53:46.396013Z","shell.execute_reply.started":"2023-01-12T20:53:46.352614Z","shell.execute_reply":"2023-01-12T20:53:46.395238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"A few nan value but  nothing special beyond that","metadata":{"id":"NzVQMgxiMsXE"}},{"cell_type":"code","source":"test_csv","metadata":{"id":"WIhXX-DUKNR3","outputId":"0cb804bb-0667-435a-a505-be609719c5b1","execution":{"iopub.status.busy":"2023-01-12T20:53:46.397486Z","iopub.execute_input":"2023-01-12T20:53:46.39773Z","iopub.status.idle":"2023-01-12T20:53:46.409925Z","shell.execute_reply.started":"2023-01-12T20:53:46.397697Z","shell.execute_reply":"2023-01-12T20:53:46.409212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"A single patient as a test , it shouldn't require much preprocessing, let's watch the columns closer","metadata":{"id":"s9zb1vlDODZf"}},{"cell_type":"code","source":"train_csv.columns","metadata":{"id":"f0Jf6IyqKNR3","outputId":"c2075617-6a9f-498e-ea6b-454775f8cb97","execution":{"iopub.status.busy":"2023-01-12T20:53:46.41109Z","iopub.execute_input":"2023-01-12T20:53:46.412099Z","iopub.status.idle":"2023-01-12T20:53:46.420549Z","shell.execute_reply.started":"2023-01-12T20:53:46.412065Z","shell.execute_reply":"2023-01-12T20:53:46.41978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Our target value is cancer, since it might be binary , let's watch the distribution","metadata":{"id":"bJ2grrFQOxxr"}},{"cell_type":"code","source":"train_csv.cancer.value_counts()","metadata":{"id":"IbNGAVIBKNR4","outputId":"ef1506fd-fb05-46fe-8eec-d9e5ebc15d1c","execution":{"iopub.status.busy":"2023-01-12T20:53:46.421775Z","iopub.execute_input":"2023-01-12T20:53:46.422128Z","iopub.status.idle":"2023-01-12T20:53:46.437521Z","shell.execute_reply.started":"2023-01-12T20:53:46.422092Z","shell.execute_reply":"2023-01-12T20:53:46.436756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Very imbalanced dataset, This often requires oversampling and undersampling , we will explore the undersampling route.\n\n","metadata":{"id":"bmMXl5DeOL0j"}},{"cell_type":"code","source":"len(set(train_csv.patient_id))","metadata":{"id":"ilESruW-KNR4","outputId":"c2376bc7-2a13-4630-c0f2-ff0fda6c3253","execution":{"iopub.status.busy":"2023-01-12T20:53:46.439246Z","iopub.execute_input":"2023-01-12T20:53:46.43967Z","iopub.status.idle":"2023-01-12T20:53:46.453051Z","shell.execute_reply.started":"2023-01-12T20:53:46.439636Z","shell.execute_reply":"2023-01-12T20:53:46.452354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"11913 different people, it might be a good idea to futher explore by separating by individual person in the future","metadata":{"id":"xQvI0bntOpV2"}},{"cell_type":"code","source":"train_csv.age.hist()","metadata":{"id":"niTlCYzjKNR5","outputId":"4260613b-9e60-4fc1-fd8e-e166b44c9504","execution":{"iopub.status.busy":"2023-01-12T20:53:46.456052Z","iopub.execute_input":"2023-01-12T20:53:46.456416Z","iopub.status.idle":"2023-01-12T20:53:46.765946Z","shell.execute_reply.started":"2023-01-12T20:53:46.456364Z","shell.execute_reply":"2023-01-12T20:53:46.76517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The average age is centered around 60, but given that many women get a mamograph around that age and not earlier we don't know if this data is relevant of biased, but it may be an dinteresting data to include.","metadata":{"id":"Iess8VN3PQQJ"}},{"cell_type":"code","source":"train_csv.laterality.value_counts()","metadata":{"id":"r28OyBvXKNR6","outputId":"c3ed37f9-f14e-4a82-a730-dd48362fac6d","execution":{"iopub.status.busy":"2023-01-12T20:53:46.767212Z","iopub.execute_input":"2023-01-12T20:53:46.768527Z","iopub.status.idle":"2023-01-12T20:53:46.780259Z","shell.execute_reply.started":"2023-01-12T20:53:46.768488Z","shell.execute_reply":"2023-01-12T20:53:46.779604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have about the same amount of left view and right view.","metadata":{"id":"5Nw3IbHLPoFv"}},{"cell_type":"markdown","source":"#Dataset preparation","metadata":{"id":"xyjKGMA5Pu91"}},{"cell_type":"markdown","source":"##Undersampling","metadata":{"id":"QwoIbjjkRRws"}},{"cell_type":"code","source":"#Creating subsets for each outcome\ntrain_subset_0 = train_csv[train_csv.cancer == 0]\ntrain_subset_1 = train_csv[train_csv.cancer == 1]\n\nprint(train_subset_0.shape, train_subset_1.shape)\nprint(train_subset_0.laterality.value_counts())\nprint(train_subset_1.laterality.value_counts())","metadata":{"id":"8ikc7kheKNR6","outputId":"c6a189e3-4898-419a-c7b0-8a35900b5896","execution":{"iopub.status.busy":"2023-01-12T20:53:46.782227Z","iopub.execute_input":"2023-01-12T20:53:46.782845Z","iopub.status.idle":"2023-01-12T20:53:46.837915Z","shell.execute_reply.started":"2023-01-12T20:53:46.782672Z","shell.execute_reply":"2023-01-12T20:53:46.837077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Undersampling the training dataset by creating a subset (no rendomization)\ntrain_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":{"id":"jMMBBH_-KNR6","execution":{"iopub.status.busy":"2023-01-12T20:53:46.839118Z","iopub.execute_input":"2023-01-12T20:53:46.839367Z","iopub.status.idle":"2023-01-12T20:53:46.863504Z","shell.execute_reply.started":"2023-01-12T20:53:46.839335Z","shell.execute_reply":"2023-01-12T20:53:46.862863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subset_main","metadata":{"id":"sHoDQ84YKNR7","outputId":"1fbc3555-0c36-49a6-f10d-b63625eddcb4","execution":{"iopub.status.busy":"2023-01-12T20:53:46.865198Z","iopub.execute_input":"2023-01-12T20:53:46.86559Z","iopub.status.idle":"2023-01-12T20:53:46.891752Z","shell.execute_reply.started":"2023-01-12T20:53:46.865559Z","shell.execute_reply":"2023-01-12T20:53:46.89072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subset_main.laterality.value_counts()","metadata":{"id":"-46CGA9hKNR7","outputId":"7f132d14-5ccc-4e96-a33b-5a49f6422e1a","execution":{"iopub.status.busy":"2023-01-12T20:53:46.893113Z","iopub.execute_input":"2023-01-12T20:53:46.893371Z","iopub.status.idle":"2023-01-12T20:53:46.903228Z","shell.execute_reply.started":"2023-01-12T20:53:46.893338Z","shell.execute_reply":"2023-01-12T20:53:46.902538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_subset_main.shape","metadata":{"id":"_xyDBYrDKNR7","outputId":"d08b0e18-8f94-4f92-dd0c-2c66c28d31e4","execution":{"iopub.status.busy":"2023-01-12T20:53:46.904662Z","iopub.execute_input":"2023-01-12T20:53:46.905284Z","iopub.status.idle":"2023-01-12T20:53:46.913288Z","shell.execute_reply.started":"2023-01-12T20:53:46.90525Z","shell.execute_reply":"2023-01-12T20:53:46.91244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Saving new dataset\n","metadata":{"id":"-byXjIqLRYxW"}},{"cell_type":"code","source":"os.mkdir('/kaggle/working/input_transformed/')","metadata":{"id":"RL8ilegnKNR8","execution":{"iopub.status.busy":"2023-01-12T20:53:46.914611Z","iopub.execute_input":"2023-01-12T20:53:46.914917Z","iopub.status.idle":"2023-01-12T20:53:46.923707Z","shell.execute_reply.started":"2023-01-12T20:53:46.914886Z","shell.execute_reply":"2023-01-12T20:53:46.923055Z"},"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":{"id":"TFnD1dAjKNR8","execution":{"iopub.status.busy":"2023-01-12T20:53:46.924996Z","iopub.execute_input":"2023-01-12T20:53:46.925315Z","iopub.status.idle":"2023-01-12T20:53:46.935294Z","shell.execute_reply.started":"2023-01-12T20:53:46.925273Z","shell.execute_reply":"2023-01-12T20:53:46.934662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nfrom tqdm import tqdm\n# shutil.copyfile(src, dst)","metadata":{"id":"iGfRQuwqKNR8","execution":{"iopub.status.busy":"2023-01-12T20:53:46.936155Z","iopub.execute_input":"2023-01-12T20:53:46.936376Z","iopub.status.idle":"2023-01-12T20:53:46.946818Z","shell.execute_reply.started":"2023-01-12T20:53:46.936349Z","shell.execute_reply":"2023-01-12T20:53:46.946007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\npatientid = train_subset_main.patient_id\nimageid = train_subset_main.image_id\nhascancer = train_subset_main.cancer\nfor patient_id, image_id, has_cancer in tqdm(zip(patientid, imageid, hascancer)):\n    tmpFile = str(patient_id) + \"_\" + str(image_id) + \".png\"\n    tmpSrc = \"/kaggle/input/rsna-breast-cancer-512-pngs/\" + tmpFile\n    tmpDst = \"/kaggle/working/input_transformed/\" + str(has_cancer) + \"/\" + tmpFile\n    shutil.copyfile(tmpSrc, tmpDst)\n    #Loading destination file\n    img = Image.open(tmpDst)\n    #Resizing the image to be 255x255 to fit in VGG19, using antialiasing\n    img = img.resize((255,255), Image.ANTIALIAS)\n    #Saving to destination file\n    img.save(tmpDst)","metadata":{"id":"P2JX58TpKNR8","outputId":"e635e7d4-2289-4496-8543-e03d12cf0bb6","execution":{"iopub.status.busy":"2023-01-12T20:53:46.947844Z","iopub.execute_input":"2023-01-12T20:53:46.948036Z","iopub.status.idle":"2023-01-12T20:54:31.804234Z","shell.execute_reply.started":"2023-01-12T20:53:46.948014Z","shell.execute_reply":"2023-01-12T20:54:31.803539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Loading the dataset","metadata":{"id":"vOMD9LSWbHpB"}},{"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=(255, 255),\n    shuffle=True,\n    validation_split=0.2,\n    subset=\"training\",\n    seed=42,\n)","metadata":{"id":"s7BwvPyUKNR9","outputId":"24f1b65e-6974-4c2e-f4a0-a885e988bc83","execution":{"iopub.status.busy":"2023-01-12T20:54:31.805188Z","iopub.execute_input":"2023-01-12T20:54:31.805453Z","iopub.status.idle":"2023-01-12T20:54:35.85977Z","shell.execute_reply.started":"2023-01-12T20:54:31.805416Z","shell.execute_reply":"2023-01-12T20:54:35.858844Z"},"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=(255, 255),\n    shuffle=True,\n    validation_split=0.2,\n    subset=\"validation\",\n    seed=2023,\n)","metadata":{"id":"jZdKHSi9KNR9","outputId":"528e2563-9e09-4833-a62b-68484584a801","execution":{"iopub.status.busy":"2023-01-12T20:54:35.861369Z","iopub.execute_input":"2023-01-12T20:54:35.861661Z","iopub.status.idle":"2023-01-12T20:54:35.983086Z","shell.execute_reply.started":"2023-01-12T20:54:35.861622Z","shell.execute_reply":"2023-01-12T20:54:35.982304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Model Training","metadata":{"id":"9iGZbBgSba3r"}},{"cell_type":"markdown","source":"Importing and crating the model","metadata":{"id":"frlRjws1biT8"}},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense\nnum_classes = 2\n\n#importing VGG19 and loading it into another model\nmodel = VGG19(weights=\"imagenet\", include_top=False, input_shape=(255, 255, 3))\nnew_model = Sequential()\nnew_model.add(model)\n\n#Adding layer on top for adaptation\nnew_model.add(GlobalAveragePooling2D())\nnew_model.add(Dense(256, activation='relu'))\nnew_model.add(Dense(2, activation='softmax'))\n\n\n","metadata":{"id":"aL89rF6RKNR9","outputId":"0d60defb-389f-4b70-8672-b00da555761d","execution":{"iopub.status.busy":"2023-01-12T21:04:29.976652Z","iopub.execute_input":"2023-01-12T21:04:29.977095Z","iopub.status.idle":"2023-01-12T21:04:30.397195Z","shell.execute_reply.started":"2023-01-12T21:04:29.977039Z","shell.execute_reply":"2023-01-12T21:04:30.396484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Assessing ressources","metadata":{"id":"pJ6ZByOlh6-m"}},{"cell_type":"code","source":"#Showing number of GPU\nprint(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))","metadata":{"id":"w8YlNlY9KNR-","outputId":"3a502de5-c0f4-44ef-a8bc-8df8561fd9a9","execution":{"iopub.status.busy":"2023-01-12T21:04:30.399486Z","iopub.execute_input":"2023-01-12T21:04:30.400112Z","iopub.status.idle":"2023-01-12T21:04:30.405271Z","shell.execute_reply.started":"2023-01-12T21:04:30.400072Z","shell.execute_reply":"2023-01-12T21:04:30.404494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Optimization of memory space\ngc.collect()","metadata":{"id":"Qec3_eWvKNR-","outputId":"ea38b209-3b29-4f96-c491-ffdfaeba798c","execution":{"iopub.status.busy":"2023-01-12T21:04:30.40655Z","iopub.execute_input":"2023-01-12T21:04:30.40741Z","iopub.status.idle":"2023-01-12T21:04:30.561301Z","shell.execute_reply.started":"2023-01-12T21:04:30.407362Z","shell.execute_reply":"2023-01-12T21:04:30.560572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Defining hyperparameters","metadata":{"id":"L2-T-VSjio9p"}},{"cell_type":"code","source":"#Adding hyperparameters and metrics\n#Categorical Entropy helps when testing to show independence\nnew_model.compile(\n    optimizer='adam',\n    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    metrics=[\n        'acc',\n        tf.keras.metrics.categorical_accuracy,\n    ]\n)\n","metadata":{"id":"DoTMvsE2KNR-","execution":{"iopub.status.busy":"2023-01-12T21:04:30.563632Z","iopub.execute_input":"2023-01-12T21:04:30.56404Z","iopub.status.idle":"2023-01-12T21:04:30.578475Z","shell.execute_reply.started":"2023-01-12T21:04:30.563999Z","shell.execute_reply":"2023-01-12T21:04:30.577716Z"},"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.995:#Check for overfitting\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":{"id":"TnUJ8KLRKNR-","execution":{"iopub.status.busy":"2023-01-12T21:04:30.579636Z","iopub.execute_input":"2023-01-12T21:04:30.57991Z","iopub.status.idle":"2023-01-12T21:04:30.58712Z","shell.execute_reply.started":"2023-01-12T21:04:30.579857Z","shell.execute_reply":"2023-01-12T21:04:30.586531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Training the model","metadata":{"id":"iaaVPHBci0Fc"}},{"cell_type":"code","source":"epochs=1\nhistory = new_model.fit(\n  train_ds,\n  validation_data=valid_ds,\n  epochs=epochs,\n)","metadata":{"id":"oeo5FrreKNR-","outputId":"47b3501b-1c6e-4f44-a8a3-19de49c933d7","execution":{"iopub.status.busy":"2023-01-12T21:04:30.588242Z","iopub.execute_input":"2023-01-12T21:04:30.588567Z","iopub.status.idle":"2023-01-12T21:05:47.583562Z","shell.execute_reply.started":"2023-01-12T21:04:30.588534Z","shell.execute_reply":"2023-01-12T21:05:47.582798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Displaying Results","metadata":{"id":"8ysBd-gNi6Th"}},{"cell_type":"code","source":"#Gathering Validation And training Accuracy\nacc = history.history['acc']\nval_acc = history.history['val_acc']\n\n#Gathering Loss and validation loss data\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\n#Gathering \ncat_acc = history.history['categorical_accuracy']\ncat_val_acc = history.history['val_categorical_accuracy']\n\n#Gathering the X value\nepochs_range = range(epochs)\n\n\nplt.figure(figsize=(20, 10))\n\n#Displaying Accurraccy\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\n#Displaying Loss\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')\n\n#Diplaying the first batch of results\nplt.show()\n\n#Creating new figures\nplt.figure(figsize=(10, 10))\n\n#Displaying Categorical accuracy\nplt.plot(epochs_range, cat_acc, label='Training Categorical accuracy')\nplt.plot(epochs_range, cat_val_acc, label='Validation Categorical accuracy')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Categorical accuracy')\n\n#Diplaying the final batch of results\nplt.show()\n","metadata":{"id":"izGh6_K-KNR_","execution":{"iopub.status.busy":"2023-01-12T21:05:47.58662Z","iopub.execute_input":"2023-01-12T21:05:47.586852Z","iopub.status.idle":"2023-01-12T21:05:48.182046Z","shell.execute_reply.started":"2023-01-12T21:05:47.586818Z","shell.execute_reply":"2023-01-12T21:05:48.181317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Model usage","metadata":{"id":"g6cKJFyvlc_8"}},{"cell_type":"markdown","source":"##Loading data","metadata":{"id":"h0sOMSkalhzb"}},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\nfrom skimage import transform\nfrom pprint import pprint","metadata":{"id":"8VbJ_JgxKNR_","execution":{"iopub.status.busy":"2023-01-12T21:05:48.183477Z","iopub.execute_input":"2023-01-12T21:05:48.183758Z","iopub.status.idle":"2023-01-12T21:05:49.554251Z","shell.execute_reply.started":"2023-01-12T21:05:48.183707Z","shell.execute_reply":"2023-01-12T21:05:49.553432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load(filename):\n   \"\"\"Load and transform and image from a filename, returns an array of size (255,255,3)\"\"\"\n   np_image = Image.open(filename)\n   np_image = np.array(np_image).astype('float32')/255\n   np_image = transform.resize(np_image, (255, 255, 3))\n   np_image = np.expand_dims(np_image, axis=0)\n   return np_image","metadata":{"id":"hAuz5PXylirw","execution":{"iopub.status.busy":"2023-01-12T21:05:49.555826Z","iopub.execute_input":"2023-01-12T21:05:49.556073Z","iopub.status.idle":"2023-01-12T21:05:49.561095Z","shell.execute_reply.started":"2023-01-12T21:05:49.556041Z","shell.execute_reply":"2023-01-12T21:05:49.559989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Starting predictions","metadata":{"id":"4-KE352dmn7E"}},{"cell_type":"code","source":"testdata_path = \"/kaggle/input/rsna-screen-breast-cancer-detect-testdata-512x512\"\npred_dict = dict()\nfor image_id in os.listdir(testdata_path):\n    tmpPath = testdata_path + \"/\" + image_id\n    image = load(tmpPath)\n    predictions = new_model.predict(image)\n    score = tf.nn.softmax(predictions[0])\n    pred_dict[image_id] = float(max(score))","metadata":{"id":"3rsndmufKNR_","execution":{"iopub.status.busy":"2023-01-12T21:05:49.565085Z","iopub.execute_input":"2023-01-12T21:05:49.565636Z","iopub.status.idle":"2023-01-12T21:05:50.773756Z","shell.execute_reply.started":"2023-01-12T21:05:49.565583Z","shell.execute_reply":"2023-01-12T21:05:50.772966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating a result sample\ntestD = {\"10008_L\":{\"736471439.png\":0, \"1591370361.png\":0}, \n         \"10008_R\":{\"68070693.png\":0,\"361203119.png\":0}}\n\n#Filling the sample\nfor k1 in testD:\n    for k2 in testD[k1]:\n        testD[k1][k2] = pred_dict[k2]\n\n#Pretty priting the results\npprint(testD)","metadata":{"id":"t1ngVwMjKNR_","outputId":"17ec1d0f-7fb2-44e8-f017-845f65c121f8","execution":{"iopub.status.busy":"2023-01-12T21:05:50.774908Z","iopub.execute_input":"2023-01-12T21:05:50.77517Z","iopub.status.idle":"2023-01-12T21:05:50.784006Z","shell.execute_reply.started":"2023-01-12T21:05:50.775137Z","shell.execute_reply":"2023-01-12T21:05:50.783134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Formatting submission file","metadata":{"id":"ipnOKvEPnIQ-"}},{"cell_type":"markdown","source":"We're going to read the submission file in order to understand how do they want it to be displayed.","metadata":{"id":"VPVROJoDnNWJ"}},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv\")\nsubmission","metadata":{"id":"4TGmPDO7KNSA","outputId":"f73f2f7e-e540-4116-87d6-adf91c083a93","execution":{"iopub.status.busy":"2023-01-12T21:05:50.785101Z","iopub.execute_input":"2023-01-12T21:05:50.786839Z","iopub.status.idle":"2023-01-12T21:05:50.81191Z","shell.execute_reply.started":"2023-01-12T21:05:50.786802Z","shell.execute_reply":"2023-01-12T21:05:50.810298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Since we only have 1 result, we will average the results of our predictions for each side of the mammograph","metadata":{"id":"K7zLFLQcnXAz"}},{"cell_type":"code","source":"for iterator in range(submission.shape[0]):\n    tmpKey = submission.prediction_id.iloc[iterator]\n    submission.cancer.iloc[iterator] = np.mean(list(testD[tmpKey].values()))#Averaging result between images","metadata":{"id":"3ijBnFv9KNSA","execution":{"iopub.status.busy":"2023-01-12T21:05:50.81315Z","iopub.execute_input":"2023-01-12T21:05:50.813437Z","iopub.status.idle":"2023-01-12T21:05:50.828701Z","shell.execute_reply.started":"2023-01-12T21:05:50.813402Z","shell.execute_reply":"2023-01-12T21:05:50.828019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can now submit our results.","metadata":{"id":"8O4ffzGwnntD"}},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\")\nsubmission","metadata":{"id":"2eYcvqAyKNSA","execution":{"iopub.status.busy":"2023-01-12T21:05:50.829823Z","iopub.execute_input":"2023-01-12T21:05:50.830288Z","iopub.status.idle":"2023-01-12T21:05:50.847251Z","shell.execute_reply.started":"2023-01-12T21:05:50.830249Z","shell.execute_reply":"2023-01-12T21:05:50.84646Z"},"trusted":true},"execution_count":null,"outputs":[]}]}