{"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":"markdown","source":"#    **Thêm thư viện**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport os,math,random\nimport pandas as pd \nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pydicom as pyd\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom skimage import exposure\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Đọc XRAY**","metadata":{}},{"cell_type":"code","source":"def read_xray(path, voi_lut = True, fix_monochrome = True,equalize_hist=True):\n    dicom = pyd.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to 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               \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    if equalize_hist:\n        data=exposure.equalize_hist(data)\n        \n    return data","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_sample_images(df,directory,n,cmap='gray'):\n    plt.subplots(math.floor(n/2),2,figsize=(16,math.floor(n/2)*8))\n    \n    dfs=df.sample(n)\n    \n    sample_ids=dfs['image_id']\n    sample_class=list(dfs['class_name'])\n    for i,image_id in enumerate(sample_ids):\n        ax=plt.subplot(math.floor(n/2),2,i+1)\n        image=read_xray(os.path.join(directory,f'{image_id}.dicom'))\n        ax.imshow(image,cmap=cmap)\n        plt.title(f'{sample_class[i]}')\n        \n        #bounding boxes:\n        w=dfs.iloc[i]['x_max']-dfs.iloc[i]['x_min']\n        h=dfs.iloc[i]['y_max']-dfs.iloc[i]['y_min']\n        x_min,y_min=dfs.iloc[i]['x_min'],dfs.iloc[i]['y_min']\n        \n        p=mpl.patches.Rectangle((x_min,y_min),w,h,ec='r',lw=1,fc='none')\n        ax.add_patch(p)\n         \n    plt.tight_layout()\n    plt.axis('off')\n    plt.show()  ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Đường dẫn DATA**","metadata":{}},{"cell_type":"code","source":"cwd='./'\ntrain_dir='../input/vinbigdata-chest-xray-abnormalities-detection/train'\ntest_dir='../input/vinbigdata-chest-xray-abnormalities-detection/test'\n\ntrain=pd.read_csv('../input/vinbigdata-chest-xray-abnormalities-detection/train.csv')\nsample_sub=pd.read_csv('../input/vinbigdata-chest-xray-abnormalities-detection/sample_submission.csv')\ntrain.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_sample_images(df=train,directory=train_dir,n=40,cmap='gray')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax=plt.subplots(figsize=(16,8))\nsns.countplot(train['class_name'])\nplt.setp(ax.get_xticklabels(),rotation=90)\nplt.title('Class Balance')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Số hình trong Trainset**","metadata":{}},{"cell_type":"code","source":"print('Tổng số hình trong trainset: {} '.format(len(train)))\nprint('Số hình ảnh đọc nhất: {} '.format(train['image_id'].nunique()))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Số lượng chú thích trung bình trên mỗi hình ảnh: {} '.format(math.ceil(67900/15000)))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Vẽ hình ảnh với tất cả chú thích**","metadata":{}},{"cell_type":"code","source":"def plot_image(img_id,train_dir,df):\n    fig,ax=plt.subplots(figsize=(10,10))\n    img=read_xray(os.path.join(train_dir,f'{img_id}.dicom'))\n    plt.imshow(img,cmap='gray')\n\n    #annotations:\n    \n    dfs=df[df['image_id']==img_id]\n    \n    #all annotations for the image\n    for i in range(len(dfs)):\n        \n         #bounding boxes:\n        #width and height\n        w=dfs.iloc[i]['x_max']-dfs.iloc[i]['x_min']\n        h=dfs.iloc[i]['y_max']-dfs.iloc[i]['y_min']\n        \n        #min,max\n        x_min,y_min=dfs.iloc[i]['x_min'],dfs.iloc[i]['y_min']\n        x_max,y_max=dfs.iloc[i]['x_max'],dfs.iloc[i]['y_max']\n        \n        p=mpl.patches.Rectangle((x_min,y_min),w,h,ec='r',lw=1,fc='none')\n        ax.add_patch(p)\n        ax.annotate('{}'.format(dfs.iloc[i]['class_name']), xy=(x_min+50,y_max+50),\n                    color='blue',horizontalalignment='right')\n    \n    \n    plt.axis('off')\n    plt.show()\n    \nplot_image('9a5094b2563a1ef3ff50dc5c7ff71345',train_dir,train) ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Ngẫu nhiên một số ví dụ**","metadata":{}},{"cell_type":"code","source":"def random_id(df):\n    img_id=random.choice(df['image_id'])\n    return img_id\n\nplot_image(random_id(train),train_dir,train)   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image(random_id(train),train_dir,train)   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image(random_id(train),train_dir,train)   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image(random_id(train),train_dir,train)   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image(random_id(train),train_dir,train)   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image(random_id(train),train_dir,train)   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image(random_id(train),train_dir,train)  ","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}