{"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":"!pip install -qU python-gdcm pydicom pylibjpeg\n!mkdir cancer\n!mkdir nocancer\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport tensorflow as tf\nimport cv2\nimport pydicom\nimport pylibjpeg\nimport gdcm\nimport random\nimport matplotlib as plt\n\nfrom joblib import Parallel, delayed","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-04T11:16:38.652103Z","iopub.execute_input":"2023-01-04T11:16:38.652567Z","iopub.status.idle":"2023-01-04T11:17:06.155748Z","shell.execute_reply.started":"2023-01-04T11:16:38.652477Z","shell.execute_reply":"2023-01-04T11:17:06.15459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\nprint(df.groupby(['cancer'])['image_id'].count())\nprint('\\n')\nprint(df.groupby(['cancer', 'laterality'])['image_id'].count())\nprint('\\n')\nprint(df.groupby(['cancer', 'laterality', 'implant'])['image_id'].count())","metadata":{"execution":{"iopub.status.busy":"2023-01-04T11:17:06.158346Z","iopub.execute_input":"2023-01-04T11:17:06.159664Z","iopub.status.idle":"2023-01-04T11:17:06.356501Z","shell.execute_reply.started":"2023-01-04T11:17:06.159613Z","shell.execute_reply":"2023-01-04T11:17:06.355072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancerDF = df[(df['cancer'] == 1) & (df['implant'] == 0)].reset_index()\nprint(len(cancerDF))","metadata":{"execution":{"iopub.status.busy":"2023-01-04T11:17:06.486828Z","iopub.execute_input":"2023-01-04T11:17:06.487326Z","iopub.status.idle":"2023-01-04T11:17:06.500398Z","shell.execute_reply.started":"2023-01-04T11:17:06.487278Z","shell.execute_reply":"2023-01-04T11:17:06.498857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT = 1280\nWIDTH = 720\nTRAIN_FOLDER = '/kaggle/input/rsna-breast-cancer-detection/train_images/'\nCANCER_FOLDER = '/kaggle/working/cancer/'\nUPSCALE_COUNT = 25000\n\ndata_augmentation = tf.keras.Sequential([\n    tf.keras.layers.RandomRotation(0.33),\n    tf.keras.layers.RandomContrast(0.33),\n])\n\ndef upscale(i):\n\n    index = random.randint(0, len(cancerDF) - 1)\n\n    patientID = str(cancerDF.loc[index, 'patient_id'])\n    imageID = str(cancerDF.loc[index, 'image_id'])\n\n    dcm = pydicom.dcmread(TRAIN_FOLDER + patientID + '/' + imageID + '.dcm')\n    pixelArray = dcm.pixel_array\n\n    equal_columns = np.all(pixelArray == pixelArray[0], axis=0)\n    image = pixelArray[:, ~equal_columns]\n\n    if dcm.PhotometricInterpretation == 'MONOCHROME1':\n        image = 1- image\n\n    image = np.reshape(image, (image.shape[0], image.shape[1], 1))\n    image = tf.convert_to_tensor(image)\n    image = tf.image.resize_with_pad(image, HEIGHT, WIDTH)\n        \n    if cancerDF.loc[index, 'laterality'] == \"R\":\n        image = tf.image.flip_left_right(image)\n        \n    image = data_augmentation(image)\n    tf.keras.utils.save_img(CANCER_FOLDER + patientID + '_' + imageID + '_' + str(i) + '.jpg', image)\n    \nParallel()(delayed(upscale)(i) for i in range(UPSCALE_COUNT))","metadata":{"execution":{"iopub.status.busy":"2023-01-04T01:34:36.459889Z","iopub.execute_input":"2023-01-04T01:34:36.460294Z","iopub.status.idle":"2023-01-04T01:34:36.941991Z","shell.execute_reply.started":"2023-01-04T01:34:36.460262Z","shell.execute_reply":"2023-01-04T01:34:36.940592Z"},"trusted":true},"execution_count":null,"outputs":[]}]}