{"cells":[{"metadata":{"execution":{"iopub.execute_input":"2021-03-12T06:45:04.372485Z","iopub.status.busy":"2021-03-12T06:45:04.371857Z","iopub.status.idle":"2021-03-12T06:45:04.374951Z","shell.execute_reply":"2021-03-12T06:45:04.374474Z"},"papermill":{"duration":0.021039,"end_time":"2021-03-12T06:45:04.375109","exception":false,"start_time":"2021-03-12T06:45:04.35407","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# inspired from\n# https://www.kaggle.com/sinamhd9/keras-models-tensorflow-data-dataset-tpu-part1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install tensorflow_io","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2021-03-12T06:45:04.506391Z","iopub.status.busy":"2021-03-12T06:45:04.467896Z","iopub.status.idle":"2021-03-12T06:45:10.141651Z","shell.execute_reply":"2021-03-12T06:45:10.140525Z"},"papermill":{"duration":5.753388,"end_time":"2021-03-12T06:45:10.141808","exception":false,"start_time":"2021-03-12T06:45:04.38842","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"%reset -f\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# ML tools \nfrom sklearn.model_selection import KFold\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom kaggle_datasets import KaggleDatasets\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Activation, Conv2D, MaxPooling2D, Dropout, Conv2D,MaxPooling2D,GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.applications import Xception\nimport tensorflow_io as tfio\nimport os\nfrom tensorflow.keras import optimizers\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping\nfrom tensorflow.keras import models, layers\nfrom tensorflow.keras.initializers import RandomUniform, lecun_normal, GlorotUniform\nimport gc\nimport cv2\n\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow import data, io, image, reshape, float32\nfrom tensorflow.random import set_seed\nfrom numpy.random import seed\nimport random\nimg_size = 512\nTPU_FLAG = 0\n\n# change\nseeding = 1\n# mse/Kl_divergence","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/vinbigdata-chest-xray-abnormalities-detection/train.csv')\ntrain","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndf_target = pd.DataFrame()\ndf_target['image_id'] = train['image_id'].unique()+ '.dicom'\ndf_target['label'] = 1\ndf_target['height'] = -1\ndf_target['width'] = -1\ndf_target.index = df_target['image_id']\ndf_target","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Convert DICOM to PNG via openCV\n%time\ninputdir = '../input/vinbigdata-chest-xray-abnormalities-detection/train/'\noutdir = 'train/'\nos.mkdir('train')\n# test_list = [os.path.basename(x) for x in glob.glob(inputdir + './*.dcm')]\n\ndef read_dicom(path):\n    image_bytes = tf.io.read_file(path)\n    image = tfio.image.decode_dicom_image(\n        image_bytes, \n        dtype = tf.uint16\n    )\n    image = tf.squeeze(image, axis = 0)\n    im_shape = image.shape\n    image = tf.image.resize(image, (img_size, img_size), )\n    \n    image = image - tf.reduce_min(image)\n    image = image / tf.reduce_max(image)\n    image = tf.cast(image * 255, tf.uint8)\n    return image, im_shape\n\ncount = 0\nfor f in df_target.index:   \n    img, im_shape = read_dicom(inputdir + f) # read dicom image\n    df_target['height'].loc[f] = im_shape[0]\n    df_target['width'].loc[f] = im_shape[1]\n    df_target['image_id'].loc[f]  = f.replace('.dicom','.png')\n    cv2.imwrite(outdir + f.replace('.dicom','.png'),img.numpy()) # write png image\n    count = count+1\n    if count%100 == 0:\n        print(count)\n    \nim_shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target.to_csv('train_df_height_width', index=False)","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}