{"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":"%matplotlib inline\n#這是juoyter notebook的magic word˙\n\nimport matplotlib\nimport matplotlib.pyplot as plt\nfrom IPython import display","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-06T07:51:05.741433Z","iopub.execute_input":"2021-07-06T07:51:05.742258Z","iopub.status.idle":"2021-07-06T07:51:05.758038Z","shell.execute_reply.started":"2021-07-06T07:51:05.742103Z","shell.execute_reply":"2021-07-06T07:51:05.756914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n#判斷是否在jupyter notebook上\ndef is_in_ipython():\n    \"Is the code running in the ipython environment (jupyter including)\"\n    program_name = os.path.basename(os.getenv('_', ''))\n\n    if ('jupyter-notebook' in program_name or # jupyter-notebook\n        'ipython'          in program_name or # ipython\n        'jupyter' in program_name or  # jupyter\n        'JPY_PARENT_PID'   in os.environ):    # ipython-notebook\n        return True\n    else:\n        return False\n\n\n#判斷是否在colab上\ndef is_in_colab():\n    if not is_in_ipython(): return False\n    try:\n        from google import colab\n        return True\n    except: return False\n\n#判斷是否在kaggke_kernal上\ndef is_in_kaggle_kernal():\n    if 'kaggle' in os.environ['PYTHONPATH']:\n        return True\n    else:\n        return False\n\nif is_in_colab():\n    from google.colab import drive\n    drive.mount('/content/gdrive')","metadata":{"execution":{"iopub.status.busy":"2021-07-06T07:51:05.760203Z","iopub.execute_input":"2021-07-06T07:51:05.760843Z","iopub.status.idle":"2021-07-06T07:51:05.779267Z","shell.execute_reply.started":"2021-07-06T07:51:05.760788Z","shell.execute_reply":"2021-07-06T07:51:05.778009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ['TRIDENT_BACKEND'] = 'pytorch'\n\nif is_in_kaggle_kernal():\n    os.environ['TRIDENT_HOME'] = './trident'\n    \nelif is_in_colab():\n    os.environ['TRIDENT_HOME'] = '/content/gdrive/My Drive/trident'\n\n#為確保安裝最新版 \n!pip uninstall tridentx -y\n!pip install tridentx --upgrade\n!pip install pydicom --upgrade\n!pip install python-gdcm  --upgrade\n\nimport json\nimport copy\nimport numpy as np\n#調用trident api\nimport trident as T\nfrom trident import *\nfrom trident.models import resnet,efficientnet\nimport random\nfrom tqdm import tqdm\nimport pydicom\nimport gdcm\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"execution":{"iopub.status.busy":"2021-07-06T07:51:05.781566Z","iopub.execute_input":"2021-07-06T07:51:05.781909Z","iopub.status.idle":"2021-07-06T07:51:38.521215Z","shell.execute_reply.started":"2021-07-06T07:51:05.781879Z","shell.execute_reply":"2021-07-06T07:51:38.519793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dicom2array(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)\n    # VOI LUT (if available by DICOM device) is used to\n    # transform raw DICOM data to \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\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    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data","metadata":{"execution":{"iopub.status.busy":"2021-07-06T07:51:38.52327Z","iopub.execute_input":"2021-07-06T07:51:38.523607Z","iopub.status.idle":"2021-07-06T07:51:38.531576Z","shell.execute_reply.started":"2021-07-06T07:51:38.523576Z","shell.execute_reply":"2021-07-06T07:51:38.530288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_images=glob.glob('../input/siim-covid19-detection/train/*/*/*.dcm')\n\nprint(len(dicom_images))","metadata":{"execution":{"iopub.status.busy":"2021-07-06T08:00:56.070168Z","iopub.execute_input":"2021-07-06T08:00:56.070551Z","iopub.status.idle":"2021-07-06T08:01:03.549876Z","shell.execute_reply.started":"2021-07-06T08:00:56.070517Z","shell.execute_reply":"2021-07-06T08:01:03.548452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nresize_fn=Resize((608,608))\nmake_dir_if_need('./train_images')\nimages_dict=OrderedDict()\nfor i in tqdm(range(len(dicom_images))):\n    img=dicom_images[i]\n    img_path=img.replace('../input/siim-covid19-detection/train/','')\n    #將路徑分離成資料夾、檔案名稱以及附檔名\n    folder,filename,ext=split_path(img_path)\n    #將資料夾字串分割\n    img_folders=folder.split('/')\n    #基於第一層資料夾以及圖檔名作為key\n    images_dict[img_folders[0]+'-'+filename]=OrderedDict()\n    \n    #取出dicom圖檔\n    pixels=dicom2array(img)\n    #縮放為608x608\n    resize_pixels=resize_fn(pixels)\n    #將縮放後檔案\n    array2image(resize_pixels).save('./train_images/{0}.png'.format(img_folders[0]+'-'+filename))\n    images_dict[img_folders[0]+'-'+filename]['img_path']='./train_images/{0}.png'.format(img_folders[0]+'-'+filename)\n    images_dict[img_folders[0]+'-'+filename]['shape']=pixels.shape\n    del pixels\n    del resize_pixels\n    \nprint(len(images_dict))\nprint(list(images_dict.items())[:5])","metadata":{"execution":{"iopub.status.busy":"2021-07-06T08:02:45.671286Z","iopub.execute_input":"2021-07-06T08:02:45.671666Z","iopub.status.idle":"2021-07-06T08:02:45.782405Z","shell.execute_reply.started":"2021-07-06T08:02:45.671609Z","shell.execute_reply":"2021-07-06T08:02:45.78087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nf=open('./train_images/image.json','w',encoding='utf-8-sig')\nf.write(json.dumps(images_dict))\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-06T07:52:18.177457Z","iopub.execute_input":"2021-07-06T07:52:18.177826Z","iopub.status.idle":"2021-07-06T07:52:18.192829Z","shell.execute_reply.started":"2021-07-06T07:52:18.177796Z","shell.execute_reply":"2021-07-06T07:52:18.191562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nf=open('./train_images/image.json','r',encoding='utf-8-sig')\nimages_dict=json.loads(f.read())\nprint(len(images_dict))","metadata":{"execution":{"iopub.status.busy":"2021-07-06T07:52:28.69281Z","iopub.execute_input":"2021-07-06T07:52:28.693221Z","iopub.status.idle":"2021-07-06T07:52:28.699988Z","shell.execute_reply.started":"2021-07-06T07:52:28.693186Z","shell.execute_reply":"2021-07-06T07:52:28.698994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#這裡的id是圖片的資料夾第一層\nimport pandas as pd\ndf_train_study=pd.read_csv('../input/siim-covid19-detection/train_study_level.csv', index_col='id')\ndf_train_study = df_train_study.idxmax(axis=1).to_frame(name='case')\ndf_train_study.index = df_train_study.index.str.replace('_study', '')\ndf_train_study","metadata":{"execution":{"iopub.status.busy":"2021-07-06T08:38:13.032746Z","iopub.execute_input":"2021-07-06T08:38:13.033164Z","iopub.status.idle":"2021-07-06T08:38:13.085972Z","shell.execute_reply.started":"2021-07-06T08:38:13.033132Z","shell.execute_reply":"2021-07-06T08:38:13.084714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_image=pd.read_csv('../input/siim-covid19-detection/train_image_level.csv', index_col='id')\n#df_train_image = df_train_image.merge(df_train_study, left_on='StudyInstanceUID', right_index=True)\ndf_train_image","metadata":{"execution":{"iopub.status.busy":"2021-07-06T08:38:16.049361Z","iopub.execute_input":"2021-07-06T08:38:16.049840Z","iopub.status.idle":"2021-07-06T08:38:16.109730Z","shell.execute_reply.started":"2021-07-06T08:38:16.049785Z","shell.execute_reply":"2021-07-06T08:38:16.108553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print([img for img in dicom_images if '000a312787f2' in img])","metadata":{"execution":{"iopub.status.busy":"2021-07-06T08:38:23.633056Z","iopub.execute_input":"2021-07-06T08:38:23.633409Z","iopub.status.idle":"2021-07-06T08:38:23.641532Z","shell.execute_reply.started":"2021-07-06T08:38:23.633379Z","shell.execute_reply":"2021-07-06T08:38:23.640492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_image = df_train_image.merge(df_train_study, left_on='StudyInstanceUID', right_index=True)\ndf_train_image","metadata":{"execution":{"iopub.status.busy":"2021-07-06T08:38:28.827656Z","iopub.execute_input":"2021-07-06T08:38:28.828047Z","iopub.status.idle":"2021-07-06T08:38:28.856641Z","shell.execute_reply.started":"2021-07-06T08:38:28.828015Z","shell.execute_reply":"2021-07-06T08:38:28.855430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_dict={'Negative for Pneumonia':0, 'Typical Appearance':1,'Indeterminate Appearance':2,'Atypical Appearance':3}\n\nfor i, row in df_train_image.iterrows():\n    image_id = row['StudyInstanceUID']+'-'+ row.name.replace('_image','')\n    #if image_id in images_dict:\n    #    print(image_id,'not in images_dict')\n\n\n    value = row['boxes']\n    scale=608/builtins.max(images_dict[image_id]['shape'][:2])\n    if isinstance(value, float): # nan\n        images_dict[image_id]['orig_boxes']=[]\n        images_dict[image_id]['boxes']=[]\n        images_dict[image_id]['scale']=scale\n        continue\n \n    else:\n        values = eval(value)\n        current_boxes=[]\n        adj_boxes=[]\n        \n        for val in values:\n            current_boxes.append([float(val['x']), float(val['y']),float(val['x'] + val['width']),float(val['y'] + val['height']),float(label_dict[row['case']])])\n            adj_boxes.append([float(val['x'])*scale, float(val['y'])*scale,float(val['x'] + val['width'])*scale,float(val['y'] + val['height'])*scale,float(label_dict[row['case']])])\n        images_dict[image_id]['orig_boxes']=current_boxes\n        images_dict[image_id]['boxes']=adj_boxes\n        images_dict[image_id]['scale']=scale\n\n\nf=open('./train_images/image.json','w',encoding='utf-8-sig')\nf.write(json.dumps(images_dict))\nprint(list(images_dict.items())[:5])\nprint(len(images_dict))","metadata":{"execution":{"iopub.status.busy":"2021-07-06T08:42:00.795971Z","iopub.execute_input":"2021-07-06T08:42:00.796404Z","iopub.status.idle":"2021-07-06T08:42:01.952751Z","shell.execute_reply.started":"2021-07-06T08:42:00.796367Z","shell.execute_reply":"2021-07-06T08:42:01.951851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r output.zip ./train_images/\n","metadata":{"execution":{"iopub.status.busy":"2021-07-06T08:42:51.374738Z","iopub.execute_input":"2021-07-06T08:42:51.375203Z","iopub.status.idle":"2021-07-06T08:43:29.317939Z","shell.execute_reply.started":"2021-07-06T08:42:51.375164Z","shell.execute_reply":"2021-07-06T08:43:29.316598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#取回你的模型的最簡單方法\nfrom IPython.display import FileLink\nFileLink('./output.zip')","metadata":{"execution":{"iopub.status.busy":"2021-07-06T08:44:38.908714Z","iopub.execute_input":"2021-07-06T08:44:38.909148Z","iopub.status.idle":"2021-07-06T08:44:38.916932Z","shell.execute_reply.started":"2021-07-06T08:44:38.909109Z","shell.execute_reply":"2021-07-06T08:44:38.915702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf  ./train_images/*","metadata":{"execution":{"iopub.status.busy":"2021-07-06T05:08:12.11647Z","iopub.execute_input":"2021-07-06T05:08:12.116948Z","iopub.status.idle":"2021-07-06T05:08:13.179322Z","shell.execute_reply.started":"2021-07-06T05:08:12.11691Z","shell.execute_reply":"2021-07-06T05:08:13.177733Z"},"trusted":true},"execution_count":null,"outputs":[]}]}