{"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":"!git clone https://github.com/ultralytics/yolov5\n!cd yolov5\nimport torch\ntorch.cuda.empty_cache()\nimport torch\nimport glob\nimport random\nimport cv2\nimport matplotlib.pyplot as plt\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-20T07:11:11.689488Z","iopub.execute_input":"2022-12-20T07:11:11.690013Z","iopub.status.idle":"2022-12-20T07:11:18.45907Z","shell.execute_reply.started":"2022-12-20T07:11:11.689914Z","shell.execute_reply":"2022-12-20T07:11:18.457696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -qU python-gdcm pydicom pylibjpeg\nimport importlib\nimport pydicom, cv2","metadata":{"execution":{"iopub.status.busy":"2022-12-20T07:11:18.461233Z","iopub.execute_input":"2022-12-20T07:11:18.461927Z","iopub.status.idle":"2022-12-20T07:11:36.002477Z","shell.execute_reply.started":"2022-12-20T07:11:18.46189Z","shell.execute_reply":"2022-12-20T07:11:36.000793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport random, os, pandas as pd, numpy as np\n\n# Set the seed value all over the place to make this reproducible.\nseed_val = 101\n\nos.environ['PYTHONHASHSEED'] = str(seed_val)\nrandom.seed(seed_val)\nnp.random.seed(seed_val)\ntorch.manual_seed(seed_val)\ntorch.cuda.manual_seed_all(seed_val)\ntorch.backends.cudnn.deterministic = True\n\nfrom PIL import Image\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\n\ndef read_xray(path, voi_lut = True, fix_monochrome = True):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n        \n    return data\n\n\n\n\ndef resize(array, size, keep_ratio=False, resample=Image.LANCZOS):\n    # Original from: https://www.kaggle.com/xhlulu/vinbigdata-process-and-resize-to-image\n    im = Image.fromarray(array)\n    \n    if keep_ratio:\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    \n    return im\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-20T07:11:36.004185Z","iopub.execute_input":"2022-12-20T07:11:36.00456Z","iopub.status.idle":"2022-12-20T07:11:36.022011Z","shell.execute_reply.started":"2022-12-20T07:11:36.004523Z","shell.execute_reply":"2022-12-20T07:11:36.020691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/working/test","metadata":{"execution":{"iopub.status.busy":"2022-12-20T07:11:36.024743Z","iopub.execute_input":"2022-12-20T07:11:36.02511Z","iopub.status.idle":"2022-12-20T07:11:37.121196Z","shell.execute_reply.started":"2022-12-20T07:11:36.025076Z","shell.execute_reply":"2022-12-20T07:11:37.119449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndef rtf(r):\n    return f\"/kaggle/input/rsna-breast-cancer-detection/train_images/{r['patient_id']}/{r['image_id']}.dcm\"\ntr = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\") \n#imags = tr.query(\"site_id == 2 and cancer == 0\")[0:30]['image_id']\nimags = [1096308702, 982883275, 1446339465, 1563019139, 551129512]\nfor i in imags:\n    q = tr.query(f\"image_id == {i}\").iloc[0]\n    f = rtf(q)\n    fout = f\"/kaggle/working/test/test_{i}_b.png\"\n    img=read_xray(f)\n    cv2.imwrite(fout, img)\n    print(\"\\n\\n\",fout)","metadata":{"execution":{"iopub.status.busy":"2022-12-20T07:11:37.122738Z","iopub.execute_input":"2022-12-20T07:11:37.12315Z","iopub.status.idle":"2022-12-20T07:11:43.402305Z","shell.execute_reply.started":"2022-12-20T07:11:37.123086Z","shell.execute_reply":"2022-12-20T07:11:43.401318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\"/kaggle/working/yolov5\")\n!python detect.py --source /kaggle/working/test --weights '/kaggle/input/mammogram-mass-analyzer-v00/mammogram-mass-analyzer-v0.0/yolov5/TRAINED_MODEL_FOLDER/exp50_best.pt' '/kaggle/input/mammogram-mass-analyzer-v00/mammogram-mass-analyzer-v0.0/yolov5/TRAINED_MODEL_FOLDER/exp51_best.pt' --conf-thres 0.25 --iou-thres 0.2 --img 2048 --save-txt --save-conf --exist-ok\n","metadata":{"execution":{"iopub.status.busy":"2022-12-20T07:11:43.403698Z","iopub.execute_input":"2022-12-20T07:11:43.404298Z","iopub.status.idle":"2022-12-20T07:13:48.961125Z","shell.execute_reply.started":"2022-12-20T07:11:43.404263Z","shell.execute_reply":"2022-12-20T07:13:48.959857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for f in glob.glob(\"/kaggle/working/yolov5/runs/detect/exp/*.png\"):\n    print(f)\n    imgFile = cv2.imread(f)\n    plt.figure(figsize=(10,10))\n    plt.imshow(imgFile)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-20T07:13:48.962753Z","iopub.execute_input":"2022-12-20T07:13:48.963123Z","iopub.status.idle":"2022-12-20T07:13:59.501202Z","shell.execute_reply.started":"2022-12-20T07:13:48.963088Z","shell.execute_reply":"2022-12-20T07:13:59.500008Z"},"trusted":true},"execution_count":null,"outputs":[]}]}