{"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":"# https://www.kaggle.com/code/deannahedges/mammography-submission","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/dicomsdl-offline-installer/dicomsdl-0.109.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2023-04-06T03:46:15.363119Z","iopub.execute_input":"2023-04-06T03:46:15.363532Z","iopub.status.idle":"2023-04-06T03:46:28.744884Z","shell.execute_reply.started":"2023-04-06T03:46:15.363496Z","shell.execute_reply":"2023-04-06T03:46:28.743399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport cv2\nimport os\nfrom joblib import Parallel, delayed\nfrom tqdm.notebook import tqdm\nfrom pathlib import Path\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport dicomsdl\nimport sys\nimport time\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\n\nRESIZE_TO = (256, 256)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:00:43.489152Z","iopub.execute_input":"2023-04-06T04:00:43.489575Z","iopub.status.idle":"2023-04-06T04:00:53.228986Z","shell.execute_reply.started":"2023-04-06T04:00:43.48953Z","shell.execute_reply":"2023-04-06T04:00:53.227457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /kaggle/working/test_images_processed_cv2_dicomsdl_{RESIZE_TO[0]}/\n\n# https://www.kaggle.com/code/tanlikesmath/brain-tumor-radiogenomic-classification-eda/notebook\ndef dicom_file_to_ary(path):\n    dcm_file = dicomsdl.open(str(path))\n    data = dcm_file.pixelData()\n\n    data = (data - data.min()) / (data.max() - data.min())\n\n    if dcm_file.getPixelDataInfo()['PhotometricInterpretation'] == \"MONOCHROME1\":\n        data = 1 - data\n\n    data = cv2.resize(data, RESIZE_TO)\n    data = (data * 255).astype(np.uint8)\n    return data\n\n\nimage_directories = []\nfor img in Path('/kaggle/input/rsna-breast-cancer-detection/test_images/10008/').iterdir():\n    image_directories.append(img)\nprint(len(image_directories))\n\ndef process_directory(directory_path):\n    \n    processed_ary = dicom_file_to_ary(directory_path)\n        \n    cv2.imwrite(\n        f'test_images_processed_cv2_dicomsdl_{RESIZE_TO[0]}/{directory_path.stem}.png',\n        processed_ary\n    )\ntest_dir = Path(\"/kaggle/working/test_images_processed_cv2_dicomsdl_256/\")\n\nimport multiprocessing as mp\n\nwith mp.Pool(64) as p:\n    p.map(process_directory, image_directories)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-06T03:47:41.064737Z","iopub.execute_input":"2023-04-06T03:47:41.065402Z","iopub.status.idle":"2023-04-06T03:47:44.156193Z","shell.execute_reply.started":"2023-04-06T03:47:41.065354Z","shell.execute_reply":"2023-04-06T03:47:44.154863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_count = len(list(test_dir.glob('*.png')))\nprint(image_count)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T03:47:52.598941Z","iopub.execute_input":"2023-04-06T03:47:52.599481Z","iopub.status.idle":"2023-04-06T03:47:52.606275Z","shell.execute_reply.started":"2023-04-06T03:47:52.599428Z","shell.execute_reply":"2023-04-06T03:47:52.604907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/test.csv\")\ndf['cancer']=0\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:48:05.120517Z","iopub.execute_input":"2023-04-06T04:48:05.120987Z","iopub.status.idle":"2023-04-06T04:48:05.141286Z","shell.execute_reply.started":"2023-04-06T04:48:05.120945Z","shell.execute_reply":"2023-04-06T04:48:05.139924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model('/kaggle/input/mammography-model-v1/model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:00:53.231422Z","iopub.execute_input":"2023-04-06T04:00:53.23215Z","iopub.status.idle":"2023-04-06T04:00:54.964538Z","shell.execute_reply.started":"2023-04-06T04:00:53.232105Z","shell.execute_reply":"2023-04-06T04:00:54.963201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_cancer(img_path):\n    img = tf.keras.utils.load_img(img_path,target_size=RESIZE_TO)\n    img_array = tf.keras.utils.img_to_array(img)\n    img_array = tf.expand_dims(img_array,0)\n    predictions = model.predict(img_array)\n    confidence = np.where(predictions > 0.5,1,0).squeeze().item()\n    print(str(predictions).strip('[]'))\n    return float(str(predictions).strip('[]'))","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:48:07.489692Z","iopub.execute_input":"2023-04-06T04:48:07.490686Z","iopub.status.idle":"2023-04-06T04:48:07.497986Z","shell.execute_reply.started":"2023-04-06T04:48:07.490605Z","shell.execute_reply":"2023-04-06T04:48:07.496506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_dir in test_dir.iterdir():\n    print(str(img_dir.stem))\n    df.loc[(df.image_id==int(img_dir.stem)),'cancer'] = predict_cancer(img_dir)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:48:07.925243Z","iopub.execute_input":"2023-04-06T04:48:07.925672Z","iopub.status.idle":"2023-04-06T04:48:09.929725Z","shell.execute_reply.started":"2023-04-06T04:48:07.925634Z","shell.execute_reply":"2023-04-06T04:48:09.928504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['prediction_id','cancer']]\n\nfinal = df\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:48:35.36865Z","iopub.execute_input":"2023-04-06T04:48:35.369056Z","iopub.status.idle":"2023-04-06T04:48:35.382553Z","shell.execute_reply.started":"2023-04-06T04:48:35.369021Z","shell.execute_reply":"2023-04-06T04:48:35.381529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final = final.groupby('prediction_id').max().reset_index()\nfinal.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:48:36.053299Z","iopub.execute_input":"2023-04-06T04:48:36.054158Z","iopub.status.idle":"2023-04-06T04:48:36.077624Z","shell.execute_reply.started":"2023-04-06T04:48:36.054118Z","shell.execute_reply":"2023-04-06T04:48:36.076263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T04:49:16.10233Z","iopub.execute_input":"2023-04-06T04:49:16.102762Z","iopub.status.idle":"2023-04-06T04:49:16.113578Z","shell.execute_reply.started":"2023-04-06T04:49:16.102721Z","shell.execute_reply":"2023-04-06T04:49:16.112229Z"},"trusted":true},"execution_count":null,"outputs":[]}]}