{"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":"import numpy as np \nimport pandas as pd \nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import StratifiedKFold\nimport matplotlib.pyplot as plt\nimport os\nimport tensorflow as tf\nfrom tensorflow.keras import Model\nfrom tqdm import notebook","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-17T17:56:10.210044Z","iopub.execute_input":"2022-03-17T17:56:10.210582Z","iopub.status.idle":"2022-03-17T17:56:15.998125Z","shell.execute_reply.started":"2022-03-17T17:56:10.210489Z","shell.execute_reply":"2022-03-17T17:56:15.997393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport cv2\nfrom PIL import Image \nimport pathlib\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nfrom tensorflow.keras.applications.vgg19 import VGG19","metadata":{"execution":{"iopub.status.busy":"2022-03-17T18:05:50.5038Z","iopub.execute_input":"2022-03-17T18:05:50.504061Z","iopub.status.idle":"2022-03-17T18:05:50.897463Z","shell.execute_reply.started":"2022-03-17T18:05:50.504031Z","shell.execute_reply":"2022-03-17T18:05:50.896726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print(f'Running on TPU {tpu.master()}')\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy()\n\nAUTO = tf.data.experimental.AUTOTUNE\nREPLICAS = strategy.num_replicas_in_sync\nprint(f'REPLICAS: {REPLICAS}')","metadata":{"execution":{"iopub.status.busy":"2022-03-17T17:56:19.385938Z","iopub.execute_input":"2022-03-17T17:56:19.386729Z","iopub.status.idle":"2022-03-17T17:56:19.401898Z","shell.execute_reply.started":"2022-03-17T17:56:19.386687Z","shell.execute_reply":"2022-03-17T17:56:19.401165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train  =  pd.read_csv(\"../input/vinbigdata-chest-xray-abnormalities-detection/train.csv\")\ntest = pd.read_csv('../input/vinbigdata-chest-xray-abnormalities-detection/sample_submission.csv')\n\ntrain_dir256 = \"../input/vinbigdata-chest-xray-resized-png-256x256/train\"\ntest_dir256 = \"../input/vinbigdata-chest-xray-resized-png-256x256/test\"\n\n\ntrain['image_png'] = train.image_id+'.png'\ntest['image_png'] = test.image_id+'.png'\n\n   \n","metadata":{"execution":{"iopub.status.busy":"2022-03-17T17:59:05.846547Z","iopub.execute_input":"2022-03-17T17:59:05.84681Z","iopub.status.idle":"2022-03-17T17:59:06.008468Z","shell.execute_reply.started":"2022-03-17T17:59:05.846782Z","shell.execute_reply":"2022-03-17T17:59:06.007728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.imshow() \nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-17T18:19:21.875903Z","iopub.execute_input":"2022-03-17T18:19:21.876148Z","iopub.status.idle":"2022-03-17T18:19:21.888995Z","shell.execute_reply.started":"2022-03-17T18:19:21.87612Z","shell.execute_reply":"2022-03-17T18:19:21.888382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-17T18:20:52.411979Z","iopub.execute_input":"2022-03-17T18:20:52.412255Z","iopub.status.idle":"2022-03-17T18:20:52.424818Z","shell.execute_reply.started":"2022-03-17T18:20:52.412227Z","shell.execute_reply":"2022-03-17T18:20:52.424146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE256 = [256, 256] \nBATCH_SIZE = 32  \nEPOCHS = 2\nOPTIMIZER = tf.keras.optimizers.Adam(learning_rate=0.001) ","metadata":{"execution":{"iopub.status.busy":"2022-03-17T18:07:05.657934Z","iopub.execute_input":"2022-03-17T18:07:05.658634Z","iopub.status.idle":"2022-03-17T18:07:05.663564Z","shell.execute_reply.started":"2022-03-17T18:07:05.658594Z","shell.execute_reply":"2022-03-17T18:07:05.6626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg19 import preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Specify the values for all arguments to data_generator_with_aug.\n# add zoom and vertical flip -> dogs are dogs no matter it is position and facing\ndata_generator_with_aug = ImageDataGenerator(preprocessing_function=preprocess_input,\n                                              horizontal_flip = True,\n                                              width_shift_range = 0.2,\n                                              height_shift_range = 0.2,\n                                                zoom_range = 0.3, \n                                             vertical_flip = True\n                                                )\n            \ndata_generator_no_aug = ImageDataGenerator(preprocessing_function=preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2022-03-17T18:19:48.57317Z","iopub.execute_input":"2022-03-17T18:19:48.573815Z","iopub.status.idle":"2022-03-17T18:19:48.579633Z","shell.execute_reply.started":"2022-03-17T18:19:48.573773Z","shell.execute_reply":"2022-03-17T18:19:48.578755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = data_generator_with_aug.flow(\n                                        #is_fold_train, is_fold_train.target,\n                                        train.image_id, train.class_id,\n                                        batch_size=BATCH_SIZE)\n\n\nvalidation_generator = data_generator_no_aug.flow(\n                                        test.image_id, test.PredictionString,\n                                        batch_size=BATCH_SIZE)\n\n#datagen_test =  ImageDataGenerator(validation_split = 0.2) ","metadata":{"execution":{"iopub.status.busy":"2022-03-17T18:28:48.362908Z","iopub.execute_input":"2022-03-17T18:28:48.36316Z","iopub.status.idle":"2022-03-17T18:28:48.393233Z","shell.execute_reply.started":"2022-03-17T18:28:48.363131Z","shell.execute_reply":"2022-03-17T18:28:48.392048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.info())\ntrain.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_fold_train = train.groupby(\"image_png\")[\"class_id\"].agg(lambda s: \n(s == 14).sum()).reset_index().rename({\n    \"class_id\": \"num_normal_annotations\"}, axis=1)\nis_fold_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-17T18:12:48.042659Z","iopub.execute_input":"2022-03-17T18:12:48.043247Z","iopub.status.idle":"2022-03-17T18:12:49.932205Z","shell.execute_reply.started":"2022-03-17T18:12:48.043199Z","shell.execute_reply":"2022-03-17T18:12:49.931374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def change(x):\n    if (x==3):\n        x=1\n    return x\n\nis_fold_train['target'] = is_fold_train['num_normal_annotations'].apply(lambda x: change(x))\nis_fold_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-17T18:23:05.012147Z","iopub.execute_input":"2022-03-17T18:23:05.01289Z","iopub.status.idle":"2022-03-17T18:23:05.035912Z","shell.execute_reply.started":"2022-03-17T18:23:05.012849Z","shell.execute_reply":"2022-03-17T18:23:05.035245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skfolds = StratifiedKFold(n_splits=5, \n                          random_state=42, \n                          shuffle = True)\n    \nfor num_fold, (train_index, val_index) in enumerate(skfolds.split(is_fold_train, is_fold_train.target)):\n    is_fold_train.loc[val_index, 'fold'] = int(num_fold)\n    \nis_fold_train['target'] = is_fold_train.target.astype('str')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(is_fold_train.info())\nis_fold_train.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen_train = ImageDataGenerator(\n                        rotation_range=40,          \n                        width_shift_range=0.2,   \n                        height_shift_range=0.2,  \n                        zoom_range=0.2,           \n                        horizontal_flip=True,     \n                        vertical_flip=False      \n                                   )     \n\ndatagen_test =  ImageDataGenerator(validation_split = 0.2) ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_augmentation = tf.keras.Sequential([\n  tf.keras.layers.experimental.preprocessing.RandomFlip(\"horizontal_and_vertical\"),\n  tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),\n])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_history = [] \nval_loss_history = []\n\nbinary_accuracy_history = []\nval_binary_accuracy_history = []","metadata":{},"execution_count":null,"outputs":[]}]}