{"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":"# Sources:\n    # To go from Dicom -> PNG:\n        # https://www.kaggle.com/code/radek1/how-to-process-dicom-images-to-pngs/notebook?scriptVersionId=113529850\n    # To load the data, configure for performance, and build model in keras:\n        # https://www.tensorflow.org/tutorials/load_data/images#:~:text=This%20tutorial%20shows%20how%20to%20load%20and%20preprocess,from%20the%20large%20catalog%20available%20in%20TensorFlow%20Datasets.\n    # To augment the data:\n        # https://www.tensorflow.org/tutorials/images/data_augmentation\n    # To make the submission notebook:\n        # https://www.kaggle.com/code/radek1/fast-ai-starter-pack-train-inference/notebook\n        \n\nimport numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-31T17:17:08.475719Z","iopub.execute_input":"2023-03-31T17:17:08.475996Z","iopub.status.idle":"2023-03-31T17:17:08.511446Z","shell.execute_reply.started":"2023-03-31T17:17:08.475968Z","shell.execute_reply":"2023-03-31T17:17:08.510235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\")\ntrain_file.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:08.513439Z","iopub.execute_input":"2023-03-31T17:17:08.513829Z","iopub.status.idle":"2023-03-31T17:17:08.636627Z","shell.execute_reply.started":"2023-03-31T17:17:08.513793Z","shell.execute_reply":"2023-03-31T17:17:08.635409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file['cancer'] = train_file['cancer'].astype('float32')\ntrain_file.info()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:08.638606Z","iopub.execute_input":"2023-03-31T17:17:08.639617Z","iopub.status.idle":"2023-03-31T17:17:08.690671Z","shell.execute_reply.started":"2023-03-31T17:17:08.63957Z","shell.execute_reply":"2023-03-31T17:17:08.689529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file['patient_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:08.698289Z","iopub.execute_input":"2023-03-31T17:17:08.699077Z","iopub.status.idle":"2023-03-31T17:17:08.714765Z","shell.execute_reply.started":"2023-03-31T17:17:08.699024Z","shell.execute_reply":"2023-03-31T17:17:08.713319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install dicomsdl","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:08.718293Z","iopub.execute_input":"2023-03-31T17:17:08.721744Z","iopub.status.idle":"2023-03-31T17:17:08.727987Z","shell.execute_reply.started":"2023-03-31T17:17:08.721706Z","shell.execute_reply":"2023-03-31T17:17:08.726409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pydicom\n# import cv2\nimport os\n# from joblib import Parallel, delayed\n# from tqdm.notebook import tqdm\nfrom pathlib import Path\n# from pydicom.pixel_data_handlers.util import apply_voi_lut\n# import dicomsdl\n# import sys\n# import time\nimport glob\n\nRESIZE_TO = (256, 256)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:16.354032Z","iopub.execute_input":"2023-03-31T17:17:16.355018Z","iopub.status.idle":"2023-03-31T17:17:16.361681Z","shell.execute_reply.started":"2023-03-31T17:17:16.354941Z","shell.execute_reply":"2023-03-31T17:17:16.359971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = Path(\"/kaggle/input/mammography-challenge-pngs/train_images_processed_cv2_dicomsdl_256/\")\nimage_count = len(list(data_dir.glob('*/*.png')))\npos_dir = Path(\"/kaggle/input/mammography-challenge-pngs/train_images_processed_cv2_dicomsdl_256/positive/\")\npos_count = len(list(pos_dir.glob('*.png')))\nneg_dir = Path(\"/kaggle/input/mammography-challenge-pngs/train_images_processed_cv2_dicomsdl_256/negative/\")\nneg_count = len(list(neg_dir.glob('*.png')))\nprint(f'All images: {image_count}')\nprint(f'Positive images: {pos_count}')\nprint(f'Negative images: {neg_count}')","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:17.592513Z","iopub.execute_input":"2023-03-31T17:17:17.593421Z","iopub.status.idle":"2023-03-31T17:17:19.081495Z","shell.execute_reply.started":"2023-03-31T17:17:17.593368Z","shell.execute_reply":"2023-03-31T17:17:19.080387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:21.333146Z","iopub.execute_input":"2023-03-31T17:17:21.333537Z","iopub.status.idle":"2023-03-31T17:17:21.338687Z","shell.execute_reply.started":"2023-03-31T17:17:21.333501Z","shell.execute_reply":"2023-03-31T17:17:21.337526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pos_imgs = list(data_dir.glob('positive/*'))\n# PIL.Image.open(str(pos_imgs[0]))","metadata":{"execution":{"iopub.status.busy":"2023-03-31T16:59:25.277604Z","iopub.execute_input":"2023-03-31T16:59:25.278487Z","iopub.status.idle":"2023-03-31T16:59:25.314415Z","shell.execute_reply.started":"2023-03-31T16:59:25.278417Z","shell.execute_reply":"2023-03-31T16:59:25.313143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# neg_imgs = list(data_dir.glob('negative/*'))\n# PIL.Image.open(str(neg_imgs[0]))","metadata":{"execution":{"iopub.status.busy":"2023-03-31T16:59:25.315902Z","iopub.execute_input":"2023-03-31T16:59:25.316336Z","iopub.status.idle":"2023-03-31T16:59:25.629508Z","shell.execute_reply.started":"2023-03-31T16:59:25.316298Z","shell.execute_reply":"2023-03-31T16:59:25.628405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import datasets, layers, models\n# model = models.load_model(model_path, custom_objects= {'f1_score': f1_score})","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:25.990508Z","iopub.execute_input":"2023-03-31T17:17:25.990881Z","iopub.status.idle":"2023-03-31T17:17:34.450779Z","shell.execute_reply.started":"2023-03-31T17:17:25.990848Z","shell.execute_reply":"2023-03-31T17:17:34.449613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nimg_height = 256\nimg_width = 256","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:34.452885Z","iopub.execute_input":"2023-03-31T17:17:34.453751Z","iopub.status.idle":"2023-03-31T17:17:34.460335Z","shell.execute_reply.started":"2023-03-31T17:17:34.453708Z","shell.execute_reply":"2023-03-31T17:17:34.459233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.keras.utils.image_dataset_from_directory(\n    data_dir,\n    validation_split=0.2,\n    subset='training',\n    seed=123,\n    image_size=(img_height,img_width),\n    batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:17:34.461901Z","iopub.execute_input":"2023-03-31T17:17:34.462401Z","iopub.status.idle":"2023-03-31T17:19:52.961214Z","shell.execute_reply.started":"2023-03-31T17:17:34.462364Z","shell.execute_reply":"2023-03-31T17:19:52.96011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = tf.keras.utils.image_dataset_from_directory(\n    data_dir,\n    validation_split=0.2,\n    subset='validation',\n    seed=123,\n    image_size=(img_height,img_width),\n    batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:19:52.963649Z","iopub.execute_input":"2023-03-31T17:19:52.963931Z","iopub.status.idle":"2023-03-31T17:20:21.939273Z","shell.execute_reply.started":"2023-03-31T17:19:52.963904Z","shell.execute_reply":"2023-03-31T17:20:21.938197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:20:21.940737Z","iopub.execute_input":"2023-03-31T17:20:21.942135Z","iopub.status.idle":"2023-03-31T17:20:21.94829Z","shell.execute_reply.started":"2023-03-31T17:20:21.942088Z","shell.execute_reply":"2023-03-31T17:20:21.947098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(class_names[labels[i]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:02:08.007167Z","iopub.execute_input":"2023-03-31T17:02:08.007832Z","iopub.status.idle":"2023-03-31T17:02:09.913204Z","shell.execute_reply.started":"2023-03-31T17:02:08.007793Z","shell.execute_reply":"2023-03-31T17:02:09.908994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_batch, labels_batch in train_ds:\n    print(image_batch.shape)\n    print(labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:20:21.949639Z","iopub.execute_input":"2023-03-31T17:20:21.950562Z","iopub.status.idle":"2023-03-31T17:20:23.029502Z","shell.execute_reply.started":"2023-03-31T17:20:21.95052Z","shell.execute_reply":"2023-03-31T17:20:23.028483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications import VGG19\ncnn_base = VGG19(weights='imagenet', \n                 include_top=False, \n                 input_shape=(img_height, img_width, 3))","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:20:28.507745Z","iopub.execute_input":"2023-03-31T17:20:28.50813Z","iopub.status.idle":"2023-03-31T17:20:29.502427Z","shell.execute_reply.started":"2023-03-31T17:20:28.508094Z","shell.execute_reply":"2023-03-31T17:20:29.501308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\n# def configure_for_performance(ds):\n#     ds = ds.cache()\n#     ds = ds.shuffle(buffer_size=1000)\n#     ds = ds.batch(batch_size)\n#     ds = ds.prefetch(buffer_size=AUTOTUNE)\n#     return ds\n\n# train_ds = configure_for_performance(train_ds)\n# val_ds = configure_for_performance(val_ds)\n\ntrain_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)\nval_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:20:29.504506Z","iopub.execute_input":"2023-03-31T17:20:29.504861Z","iopub.status.idle":"2023-03-31T17:20:29.520656Z","shell.execute_reply.started":"2023-03-31T17:20:29.504831Z","shell.execute_reply":"2023-03-31T17:20:29.5196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess_input = tf.keras.applications.vgg19.preprocess_input","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:23:51.877518Z","iopub.execute_input":"2023-03-31T17:23:51.878427Z","iopub.status.idle":"2023-03-31T17:23:51.883383Z","shell.execute_reply.started":"2023-03-31T17:23:51.878388Z","shell.execute_reply":"2023-03-31T17:23:51.881944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_shape = (256,256,3)\nbase_model = tf.keras.applications.VGG19(input_shape=img_shape,\n                                        include_top=False,\n                                        weights='imagenet')","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:29:00.01253Z","iopub.execute_input":"2023-03-31T17:29:00.013548Z","iopub.status.idle":"2023-03-31T17:29:00.399624Z","shell.execute_reply.started":"2023-03-31T17:29:00.013488Z","shell.execute_reply":"2023-03-31T17:29:00.3986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch, label_batch = next(iter(train_ds))\nfeature_batch = base_model(image_batch)\nprint(feature_batch.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:29:22.725601Z","iopub.execute_input":"2023-03-31T17:29:22.726554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras import models\n# from keras import layers\n# from keras import optimizers\n\n# model = models.Sequential()\n# model.add(layers.Dense(256, activation='relu', input_dim=2*2*512))\n# model.add(layers.Dense(1, activation='sigmoid'))\n\n# model.compile(optimizer=optimizers.RMSprop(lr=1e-4),\n#               loss='binary_crossentropy',\n#               metrics=['acc'])\n\n# history = model.fit(train_ds,\n#                     epochs=20,\n#                     batch_size=10,\n#                     validation_data=(val_ds))","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:20:37.553139Z","iopub.execute_input":"2023-03-31T17:20:37.553534Z","iopub.status.idle":"2023-03-31T17:20:37.965151Z","shell.execute_reply.started":"2023-03-31T17:20:37.553498Z","shell.execute_reply":"2023-03-31T17:20:37.963533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data_augmentation = tf.keras.Sequential([\n#     tf.keras.layers.RandomFlip('horizontal'),\n#     tf.keras.layers.RandomRotation(0.2),\n# ])","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:02:11.353411Z","iopub.execute_input":"2023-03-31T17:02:11.353822Z","iopub.status.idle":"2023-03-31T17:02:11.366568Z","shell.execute_reply.started":"2023-03-31T17:02:11.353781Z","shell.execute_reply":"2023-03-31T17:02:11.36551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for image, _ in train_ds.take(1):\n#     plt.figure(figsize=(10, 10))\n#     first_image = image[0]\n#     for i in range(9):\n#         ax = plt.subplot(3, 3, i + 1)\n#         augmented_image = data_augmentation(tf.expand_dims(first_image, 0))\n#         plt.imshow(augmented_image[0] / 255)\n#         plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-03-31T17:02:11.368054Z","iopub.execute_input":"2023-03-31T17:02:11.368765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_features, train_labels = extract_features(train_folder, 540) \n# validation_features, validation_labels = extract_features(val_folder, 200) \n# test_features, test_labels = extract_features(test_folder, 180)\n\n# train_features = np.reshape(train_features, (540, 2 * 2 * 512))\n# validation_features = np.reshape(validation_features, (200, 2 * 2 * 512))\n# test_features = np.reshape(test_features, (180, 2 * 2 * 512))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = models.Sequential()\n# model.add(layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)))\n# model.add(layers.RandomFlip(\"horizontal_and_vertical\"))\n# model.add(layers.RandomRotation(0.2))\n# model.add(layers.Conv2D(filters=32,\n#                         kernel_size=3,\n#                         activation='relu'))\n# model.add(layers.MaxPooling2D())\n# model.add(layers.Conv2D(64, 3, activation='relu'))\n# model.add(layers.MaxPooling2D())\n# model.add(layers.Conv2D(64, 3, activation='relu'))\n# model.add(layers.Flatten())\n# model.add(layers.Dense(64, activation='relu'))\n# model.add(layers.Dense(2))","metadata":{"execution":{"iopub.status.busy":"2023-02-27T21:38:41.040637Z","iopub.execute_input":"2023-02-27T21:38:41.041482Z","iopub.status.idle":"2023-02-27T21:38:41.728858Z","shell.execute_reply.started":"2023-02-27T21:38:41.04144Z","shell.execute_reply":"2023-02-27T21:38:41.727767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.build(input_shape=(img_height, img_width, 3))\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-27T21:38:43.466913Z","iopub.execute_input":"2023-02-27T21:38:43.46789Z","iopub.status.idle":"2023-02-27T21:38:43.505499Z","shell.execute_reply.started":"2023-02-27T21:38:43.467836Z","shell.execute_reply":"2023-02-27T21:38:43.504678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(optimizer='adam',\n#               loss='binary_crossentropy',\n#               metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-02-27T21:38:46.351788Z","iopub.execute_input":"2023-02-27T21:38:46.352374Z","iopub.status.idle":"2023-02-27T21:38:46.373264Z","shell.execute_reply.started":"2023-02-27T21:38:46.352334Z","shell.execute_reply":"2023-02-27T21:38:46.372268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history = model.fit(\n#     train_ds,\n#     validation_data=val_ds,\n#     epochs=3,\n#     batch_size=1000\n# )","metadata":{"execution":{"iopub.status.busy":"2023-02-27T21:38:48.057757Z","iopub.execute_input":"2023-02-27T21:38:48.058966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# acc = history.history['accuracy']\n# val_acc = history.history['val_accuracy']\n\n# loss = history.history['loss']\n# val_loss = history.history['val_loss']\n\n# epochs_range = range(epochs)\n\n# plt.figure(figsize=(8, 8))\n# plt.subplot(1, 2, 1)\n# plt.plot(epochs_range, acc, label='Training Accuracy')\n# plt.plot(epochs_range, val_acc, label='Validation Accuracy')\n# plt.legend(loc='lower right')\n# plt.title('Training and Validation Accuracy')\n\n# plt.subplot(1, 2, 2)\n# plt.plot(epochs_range, loss, label='Training Loss')\n# plt.plot(epochs_range, val_loss, label='Validation Loss')\n# plt.legend(loc='upper right')\n# plt.title('Training and Validation Loss')\n# plt.show()","metadata":{},"execution_count":null,"outputs":[]}]}