{"cells":[{"metadata":{},"cell_type":"markdown","source":"__Competition Goal__\n\nAutomatically localize and classify 14 types of thoracic abnormalities from chest radiographs"},{"metadata":{},"cell_type":"markdown","source":"__Competition Metric__\n\nStandard PASCAL VOC 2010 _mean Average Precision (mAP)_ at IoU > 0.4"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%matplotlib inline\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n\nfrom tqdm import tqdm\nfrom glob import glob\nimport gc\n\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport seaborn as sns\nfrom IPython.display import display\n\nplt.rcParams[\"figure.figsize\"] = (12,8)\nplt.rcParams['axes.titlesize'] = 16\n\nimport pydicom as dicom\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nprint(os.listdir('/kaggle/input/'))\n\nfrom time import time, strftime, gmtime\nstart = time()\nimport datetime\nprint(str(datetime.datetime.now()))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"base_dir = '/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/'\nos.listdir(base_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(base_dir + 'train.csv')\ntrain","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv(base_dir + 'sample_submission.csv')\nsub","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Number of train image ids: {}'.format(len(train)))\nprint('Number of test image ids: {}'.format(len(sub)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Number of unique train image ids: {}'.format(train['image_id'].nunique()))\nprint('Number of unique test image ids: {}'.format(sub['image_id'].nunique()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Number of unique rad id in train: {}'.format(train['rad_id'].nunique()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Number of targets: {}'.format(train['class_name'].nunique()))\ntargets = np.sort(train['class_name'].unique())\nprint(targets)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['class_name'].value_counts(normalize = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = sns.countplot(train['class_name'])\nplt.xticks(rotation = 70)\nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"- Clearly imbalanced dataset!"},{"metadata":{"trusted":true},"cell_type":"code","source":"target_map = {'Aortic enlargement': 0, 'Atelectasis': 1, 'Calcification': 2, 'Cardiomegaly': 3, \n              'Consolidation': 4,  'ILD': 5, 'Infiltration': 6, 'Lung Opacity': 7, 'Nodule/Mass': 8, \n              'Other lesion': 9, 'Pleural effusion': 10, 'Pleural thickening': 11, 'Pneumothorax': 12, \n              'Pulmonary fibrosis': 13, ' No finding': 14}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Read and display a dicom file...')\n\nimg_id = np.random.choice(train['image_id'], 1)[0]\ndicom_path = base_dir + 'train/' + img_id + '.dicom'\ndicom_img = dicom.dcmread(dicom_path)\nprint(dicom_img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"idx = np.random.choice(train['image_id'], 1)[0]\ncls = train.loc[train['image_id'] == idx, 'class_name']\nprint('Number of classes for image id: {} is {}'.format(idx, len(cls)))\nprint('Number of unique classes for image id: {} is {}'.format(idx, len(np.unique(cls))))\nprint(cls)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"__Visualization__"},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_images(idx, lbl):\n    f, ax = plt.subplots(1, 3, figsize = (15, 10))\n    f.subplots_adjust(hspace = .1, wspace = .1)\n\n    for i in range(3):\n        dicom_path = base_dir + 'train/' + train.loc[idx[i], 'image_id'] + '.dicom'\n        dicom_file = dicom.dcmread(dicom_path)\n        img = dicom_file.pixel_array\n        ax[i].imshow(img, cmap = 'gray')\n        ax[i].set_xticklabels([])\n        ax[i].set_yticklabels([])\n        ax[i].set_title(f'{lbl}', fontsize = 10)\n        if lbl != 'No finding':\n            bbox = [train.loc[idx[i], 'x_min'],\n                    train.loc[idx[i], 'y_min'],\n                    train.loc[idx[i], 'x_max'],\n                    train.loc[idx[i], 'y_max']]\n            p = matplotlib.patches.Rectangle((bbox[0], bbox[1]),\n                                             bbox[2] - bbox[0],\n                                             bbox[3] - bbox[1],\n                                             color = 'red', fc = 'none')\n            ax[i].add_patch(p)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, lbl in enumerate(targets):\n    indices = train.loc[train['class_name'] == lbl][:3].index.values\n    display_images(indices, lbl)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"__Visualize with all Bounding Box, number of diagnosis  and unqiue diagnosis__"},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_all_class(idx, lbl):\n    f, ax = plt.subplots(1, 3, figsize = (15, 10))\n    f.subplots_adjust(hspace = .1, wspace = .1)\n\n    for i in range(3):\n        dicom_path = base_dir + 'train/' + idx[i] + '.dicom'\n        temp = train.loc[train['image_id'] == idx[i]]\n        n_diag = len(temp)\n        n_udiag = temp['class_name'].unique()\n        #cmap = plt.cm.get_cmap(\"hsv\", n_diag + 1)\n        dicom_file = dicom.dcmread(dicom_path)\n        img = dicom_file.pixel_array\n        ax[i].imshow(img, cmap = 'gray')\n        ax[i].set_xticklabels([])\n        ax[i].set_yticklabels([])\n        ax[i].set_title(f'{lbl}, Votes: {n_diag}, Majority: {len(n_udiag)}', fontsize = 10)\n        if lbl != 'No finding':\n            for j in temp.index.values:\n                bbox = [temp.loc[j, 'x_min'],\n                        temp.loc[j, 'y_min'],\n                        temp.loc[j, 'x_max'],\n                        temp.loc[j, 'y_max']]\n                p = matplotlib.patches.Rectangle((bbox[0], bbox[1]),\n                                                 bbox[2] - bbox[0],\n                                                 bbox[3] - bbox[1],\n                                                 ec = np.random.random(3), fc = 'none')\n                ax[i].add_patch(p)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, lbl in enumerate(targets):\n    ids = train.loc[train['class_name'] == lbl]['image_id'].unique()[:3]\n    display_all_class(ids, lbl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"finish = time()\nprint(strftime(\"%H:%M:%S\", gmtime(finish - start)))","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}