{"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":"%env JOBLIB_TEMP_FOLDER=/tmp","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:03.916708Z","iopub.execute_input":"2023-03-01T11:52:03.917357Z","iopub.status.idle":"2023-03-01T11:52:03.948509Z","shell.execute_reply.started":"2023-03-01T11:52:03.917266Z","shell.execute_reply":"2023-03-01T11:52:03.946611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    import pylibjpeg\nexcept:\n   !pip install /kaggle/input/rsna-2022-whl/{pylibjpeg-1.4.0-py3-none-any.whl,python_gdcm-3.0.15-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl}\n","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:03.951763Z","iopub.execute_input":"2023-03-01T11:52:03.95272Z","iopub.status.idle":"2023-03-01T11:52:35.557108Z","shell.execute_reply.started":"2023-03-01T11:52:03.952654Z","shell.execute_reply":"2023-03-01T11:52:35.555412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:35.558839Z","iopub.execute_input":"2023-03-01T11:52:35.55926Z","iopub.status.idle":"2023-03-01T11:52:40.868294Z","shell.execute_reply.started":"2023-03-01T11:52:35.55922Z","shell.execute_reply":"2023-03-01T11:52:40.866816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pfbeta_fast(labels, predictions, beta=1.3):\n    labels = tf.cast(labels, dtype=tf.float32)\n    #predictions = tf.cast(predictions, dtype=tf.float32)\n\n    pTP = tf.reduce_sum(labels * predictions)\n    pFP = tf.reduce_sum((1 - labels) * predictions)\n    num_positives = tf.reduce_sum(labels)\n\n    pPrecision = pTP / (pTP + pFP)\n    pRecall = pTP / num_positives\n\n    beta_squared = beta ** 2\n\n    condition = tf.logical_and(pPrecision > 0, pRecall > 0)\n    true_fn = lambda: (1 + beta_squared) * pPrecision * pRecall / (beta_squared * pPrecision + pRecall)\n    false_fn = lambda: 0.0\n    pF1 = tf.cond(condition, true_fn, false_fn)\n\n    return pF1","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:40.87104Z","iopub.execute_input":"2023-03-01T11:52:40.871762Z","iopub.status.idle":"2023-03-01T11:52:40.880348Z","shell.execute_reply.started":"2023-03-01T11:52:40.871726Z","shell.execute_reply":"2023-03-01T11:52:40.878899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\n# returns a compiled model\n# identical to the previous one\nmodel = load_model('/kaggle/input/7epochsrsna/RSNA.h5',custom_objects={\"pfbeta_fast\":pfbeta_fast})","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:40.88174Z","iopub.execute_input":"2023-03-01T11:52:40.882197Z","iopub.status.idle":"2023-03-01T11:52:53.705434Z","shell.execute_reply.started":"2023-03-01T11:52:40.882164Z","shell.execute_reply":"2023-03-01T11:52:53.70424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n                0.001, \n                decay_steps=20, \n                decay_rate=0.96, \n                staircase=True\n            ) ","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:53.707727Z","iopub.execute_input":"2023-03-01T11:52:53.708174Z","iopub.status.idle":"2023-03-01T11:52:54.134235Z","shell.execute_reply.started":"2023-03-01T11:52:53.708146Z","shell.execute_reply":"2023-03-01T11:52:54.132805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n     optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),\n     loss='binary_crossentropy',\n     metrics=[pfbeta_fast]\n     )","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:54.136042Z","iopub.execute_input":"2023-03-01T11:52:54.13648Z","iopub.status.idle":"2023-03-01T11:52:54.16011Z","shell.execute_reply.started":"2023-03-01T11:52:54.136442Z","shell.execute_reply":"2023-03-01T11:52:54.158769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_HEIGHT=224\nIMG_WIDTH=224","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:54.162381Z","iopub.execute_input":"2023-03-01T11:52:54.162826Z","iopub.status.idle":"2023-03-01T11:52:54.170901Z","shell.execute_reply.started":"2023-03-01T11:52:54.1628Z","shell.execute_reply":"2023-03-01T11:52:54.168972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_io as tfio\nfrom PIL import Image\nfrom keras.preprocessing import image","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:54.174594Z","iopub.execute_input":"2023-03-01T11:52:54.175342Z","iopub.status.idle":"2023-03-01T11:52:54.425247Z","shell.execute_reply.started":"2023-03-01T11:52:54.175302Z","shell.execute_reply":"2023-03-01T11:52:54.424057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pydicom\nimport cv2\nfrom skimage.transform import resize","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:54.426494Z","iopub.execute_input":"2023-03-01T11:52:54.42688Z","iopub.status.idle":"2023-03-01T11:52:55.284455Z","shell.execute_reply.started":"2023-03-01T11:52:54.426849Z","shell.execute_reply":"2023-03-01T11:52:55.283381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Set the directory path for test images\ndir_path = '/kaggle/input/rsna-breast-cancer-detection/test_images/10008'\n\n# Get the list of files in the directory\nfile_names = os.listdir(dir_path)\nresults = []\nfilenames = []\n# Print the file names\nfor file_name in file_names:\n    file_path = dir_path + '/' + file_name\n    ds = pydicom.read_file(file_path)\n    image1 = ds.pixel_array.astype(float)\n    image1 = (np.maximum(image1,0) / image1.max()) * 255.0\n    image1 = image1.astype(np.uint8)\n    image1 = cv2.cvtColor(image1, cv2.COLOR_GRAY2RGB) \n    image1 = resize(image1, (IMG_WIDTH, IMG_HEIGHT), anti_aliasing=True)\n    test_image = image.img_to_array(image1)*255\n    test_image = np.expand_dims(test_image, axis = 0)\n    result = model.predict(test_image)\n    result = str(result[0].reshape(1,-1).flatten().tolist()).replace('[', '').replace(']', '')\n    results.append(result)\n    filenames.append(file_name[:-4])\nprint(filenames,results)","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:52:55.285809Z","iopub.execute_input":"2023-03-01T11:52:55.286189Z","iopub.status.idle":"2023-03-01T11:53:06.399752Z","shell.execute_reply.started":"2023-03-01T11:52:55.286153Z","shell.execute_reply":"2023-03-01T11:53:06.397731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.401882Z","iopub.execute_input":"2023-03-01T11:53:06.402349Z","iopub.status.idle":"2023-03-01T11:53:06.408861Z","shell.execute_reply.started":"2023-03-01T11:53:06.402313Z","shell.execute_reply":"2023-03-01T11:53:06.407804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = pd.DataFrame(\n    {'image_id': filenames,\n     'cancer': results\n    })","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.410288Z","iopub.execute_input":"2023-03-01T11:53:06.412019Z","iopub.status.idle":"2023-03-01T11:53:06.48143Z","shell.execute_reply.started":"2023-03-01T11:53:06.411978Z","shell.execute_reply":"2023-03-01T11:53:06.480319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.4824Z","iopub.execute_input":"2023-03-01T11:53:06.482654Z","iopub.status.idle":"2023-03-01T11:53:06.502906Z","shell.execute_reply.started":"2023-03-01T11:53:06.48263Z","shell.execute_reply":"2023-03-01T11:53:06.501752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = predictions.copy()\npredictions.loc[:, 'image_id'] = predictions['image_id'].astype('int64')\npredictions.loc[:, 'cancer'] = predictions['cancer'].astype('float64')","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.50453Z","iopub.execute_input":"2023-03-01T11:53:06.505139Z","iopub.status.idle":"2023-03-01T11:53:06.525105Z","shell.execute_reply.started":"2023-03-01T11:53:06.505101Z","shell.execute_reply":"2023-03-01T11:53:06.523716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.526932Z","iopub.execute_input":"2023-03-01T11:53:06.527428Z","iopub.status.idle":"2023-03-01T11:53:06.542083Z","shell.execute_reply.started":"2023-03-01T11:53:06.527387Z","shell.execute_reply":"2023-03-01T11:53:06.54061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.543749Z","iopub.execute_input":"2023-03-01T11:53:06.544159Z","iopub.status.idle":"2023-03-01T11:53:06.56489Z","shell.execute_reply.started":"2023-03-01T11:53:06.544124Z","shell.execute_reply":"2023-03-01T11:53:06.56338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\ntest_csv","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.566476Z","iopub.execute_input":"2023-03-01T11:53:06.566854Z","iopub.status.idle":"2023-03-01T11:53:06.595539Z","shell.execute_reply.started":"2023-03-01T11:53:06.566793Z","shell.execute_reply":"2023-03-01T11:53:06.59386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.59725Z","iopub.execute_input":"2023-03-01T11:53:06.598317Z","iopub.status.idle":"2023-03-01T11:53:06.607951Z","shell.execute_reply.started":"2023-03-01T11:53:06.598274Z","shell.execute_reply":"2023-03-01T11:53:06.606453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.merge(test_csv, predictions, on=['image_id'])\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.609612Z","iopub.execute_input":"2023-03-01T11:53:06.610088Z","iopub.status.idle":"2023-03-01T11:53:06.643402Z","shell.execute_reply.started":"2023-03-01T11:53:06.610061Z","shell.execute_reply":"2023-03-01T11:53:06.642654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=submission.groupby('prediction_id')['cancer'].max().reset_index().rename(columns={'max':'cancer','prediction_id' : 'prediction_id'})","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.644543Z","iopub.execute_input":"2023-03-01T11:53:06.645103Z","iopub.status.idle":"2023-03-01T11:53:06.662427Z","shell.execute_reply.started":"2023-03-01T11:53:06.645076Z","shell.execute_reply":"2023-03-01T11:53:06.66128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.663769Z","iopub.execute_input":"2023-03-01T11:53:06.664144Z","iopub.status.idle":"2023-03-01T11:53:06.680875Z","shell.execute_reply.started":"2023-03-01T11:53:06.664115Z","shell.execute_reply":"2023-03-01T11:53:06.679746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.682973Z","iopub.execute_input":"2023-03-01T11:53:06.683449Z","iopub.status.idle":"2023-03-01T11:53:06.701973Z","shell.execute_reply.started":"2023-03-01T11:53:06.683412Z","shell.execute_reply":"2023-03-01T11:53:06.701088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1 = pd.DataFrame().reindex_like(sample)","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.705997Z","iopub.execute_input":"2023-03-01T11:53:06.706853Z","iopub.status.idle":"2023-03-01T11:53:06.713589Z","shell.execute_reply.started":"2023-03-01T11:53:06.706811Z","shell.execute_reply":"2023-03-01T11:53:06.712541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1['prediction_id'] = submission['prediction_id'].values.tolist()\nsubmission1['cancer'] = submission['cancer'].values.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.714856Z","iopub.execute_input":"2023-03-01T11:53:06.715235Z","iopub.status.idle":"2023-03-01T11:53:06.727507Z","shell.execute_reply.started":"2023-03-01T11:53:06.715202Z","shell.execute_reply":"2023-03-01T11:53:06.726606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.728799Z","iopub.execute_input":"2023-03-01T11:53:06.730139Z","iopub.status.idle":"2023-03-01T11:53:06.74327Z","shell.execute_reply.started":"2023-03-01T11:53:06.730096Z","shell.execute_reply":"2023-03-01T11:53:06.742323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.744969Z","iopub.execute_input":"2023-03-01T11:53:06.745613Z","iopub.status.idle":"2023-03-01T11:53:06.762009Z","shell.execute_reply.started":"2023-03-01T11:53:06.745578Z","shell.execute_reply":"2023-03-01T11:53:06.760454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.764223Z","iopub.execute_input":"2023-03-01T11:53:06.764907Z","iopub.status.idle":"2023-03-01T11:53:06.773565Z","shell.execute_reply.started":"2023-03-01T11:53:06.764869Z","shell.execute_reply":"2023-03-01T11:53:06.772282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(pd.read_csv('submission.csv').head())","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.775692Z","iopub.execute_input":"2023-03-01T11:53:06.776386Z","iopub.status.idle":"2023-03-01T11:53:06.796143Z","shell.execute_reply.started":"2023-03-01T11:53:06.776347Z","shell.execute_reply":"2023-03-01T11:53:06.79405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(pd.read_csv('submission.csv').info())","metadata":{"execution":{"iopub.status.busy":"2023-03-01T11:53:06.797739Z","iopub.execute_input":"2023-03-01T11:53:06.798116Z","iopub.status.idle":"2023-03-01T11:53:06.822056Z","shell.execute_reply.started":"2023-03-01T11:53:06.798085Z","shell.execute_reply":"2023-03-01T11:53:06.820769Z"},"trusted":true},"execution_count":null,"outputs":[]}]}