{"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 os\nimport glob\n\nimport shutil\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport scipy.io\nimport numpy as np\nimport cv2\nfrom PIL import Image\nimport pandas as pd\nimport gc\nfrom tqdm import tqdm\n\n\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.models import Sequential, Model\n\nfrom tensorflow.keras.optimizers import Adam, SGD\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D, BatchNormalization, Reshape, Permute, Activation, Input, \\\n    add, multiply,Flatten, ReLU, Dense, AveragePooling2D, MaxPool2D, GlobalMaxPooling2D, GlobalAveragePooling2D, LeakyReLU\nfrom tensorflow.keras.layers import concatenate, Dropout\nfrom tensorflow.keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:38:01.89378Z","iopub.execute_input":"2022-11-28T21:38:01.894544Z","iopub.status.idle":"2022-11-28T21:38:08.092306Z","shell.execute_reply.started":"2022-11-28T21:38:01.894397Z","shell.execute_reply":"2022-11-28T21:38:08.091434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_addons as tfa","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:38:08.094551Z","iopub.execute_input":"2022-11-28T21:38:08.094992Z","iopub.status.idle":"2022-11-28T21:38:08.210531Z","shell.execute_reply.started":"2022-11-28T21:38:08.094954Z","shell.execute_reply":"2022-11-28T21:38:08.209838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import ones_like","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:38:08.211878Z","iopub.execute_input":"2022-11-28T21:38:08.212151Z","iopub.status.idle":"2022-11-28T21:38:08.215969Z","shell.execute_reply.started":"2022-11-28T21:38:08.212101Z","shell.execute_reply":"2022-11-28T21:38:08.215255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"machine = \"gpu\"\n\nif machine == \"gpu\":\n    config = tf.compat.v1.ConfigProto()\n    config.gpu_options.allow_growth = True\n    sess = tf.compat.v1.Session(config=config)\n    tf.compat.v1.keras.backend.set_session(sess)\n    print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))\nelse:\n    strategy = tf.distribute.get_strategy()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:38:08.218214Z","iopub.execute_input":"2022-11-28T21:38:08.218706Z","iopub.status.idle":"2022-11-28T21:38:10.857088Z","shell.execute_reply.started":"2022-11-28T21:38:08.218662Z","shell.execute_reply":"2022-11-28T21:38:10.856387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\nLEARNING_RATE = 3e-5","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:38:10.858426Z","iopub.execute_input":"2022-11-28T21:38:10.859238Z","iopub.status.idle":"2022-11-28T21:38:10.864771Z","shell.execute_reply.started":"2022-11-28T21:38:10.859195Z","shell.execute_reply":"2022-11-28T21:38:10.862255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(80085)\ntf.random.set_seed(80085)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:38:10.866025Z","iopub.execute_input":"2022-11-28T21:38:10.866483Z","iopub.status.idle":"2022-11-28T21:38:10.880254Z","shell.execute_reply.started":"2022-11-28T21:38:10.866437Z","shell.execute_reply":"2022-11-28T21:38:10.879366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DIR = \"../input/pulmonary-dataset/kaggle/Pulmonary_dataset/RSNA\"","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:38:10.881432Z","iopub.execute_input":"2022-11-28T21:38:10.881742Z","iopub.status.idle":"2022-11-28T21:38:10.890374Z","shell.execute_reply.started":"2022-11-28T21:38:10.881709Z","shell.execute_reply":"2022-11-28T21:38:10.889717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n      rescale=1./255,\n      rotation_range=5,\n      shear_range=0.1,\n      zoom_range=0.1,\n    width_shift_range = 0.1,\n    height_shift_range = 0.1,\n    validation_split=0.4)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:38:10.893638Z","iopub.execute_input":"2022-11-28T21:38:10.89412Z","iopub.status.idle":"2022-11-28T21:38:10.900644Z","shell.execute_reply.started":"2022-11-28T21:38:10.894068Z","shell.execute_reply":"2022-11-28T21:38:10.899726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagen.flow_from_directory(\n        DIR,\n        target_size=(256, 256),\n        batch_size=BATCH_SIZE,\n        class_mode='binary',\n        subset='training')","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:38:10.901834Z","iopub.execute_input":"2022-11-28T21:38:10.902256Z","iopub.status.idle":"2022-11-28T21:42:08.22198Z","shell.execute_reply.started":"2022-11-28T21:38:10.902217Z","shell.execute_reply":"2022-11-28T21:42:08.221231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = datagen.flow_from_directory(\n        DIR,\n        target_size=(256, 256),\n        batch_size=BATCH_SIZE,\n        class_mode='binary', \n        subset='validation')","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:42:08.225159Z","iopub.execute_input":"2022-11-28T21:42:08.225418Z","iopub.status.idle":"2022-11-28T21:43:00.88381Z","shell.execute_reply.started":"2022-11-28T21:42:08.225388Z","shell.execute_reply":"2022-11-28T21:43:00.883006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tensorflow.keras.applications import DenseNet121\n\n#conv_base = DenseNet121(weights=None, include_top=False, input_shape=(256,256,3))","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:43:00.884947Z","iopub.execute_input":"2022-11-28T21:43:00.88563Z","iopub.status.idle":"2022-11-28T21:43:00.8897Z","shell.execute_reply.started":"2022-11-28T21:43:00.885585Z","shell.execute_reply":"2022-11-28T21:43:00.888861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = Sequential()\n#model.add(conv_base)\n#model.add(GlobalMaxPooling2D())\n#model.add(Dense(units = 32))\n#model.add(BatchNormalization())\n#model.add(ReLU())\n#model.add(Dropout(0.2))\n\n#model.add(Dense(1, activation=\"sigmoid\"))\n#conv_base.trainable = True","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:43:00.891086Z","iopub.execute_input":"2022-11-28T21:43:00.891401Z","iopub.status.idle":"2022-11-28T21:43:00.901733Z","shell.execute_reply.started":"2022-11-28T21:43:00.891363Z","shell.execute_reply":"2022-11-28T21:43:00.900905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tensorflow.keras.applications import ResNet50V2\n\n#model = ResNet50V2(weights=None, include_top=True, input_shape=(256, 256, 3),classes=1,classifier_activation=\"sigmoid\")","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:43:00.903067Z","iopub.execute_input":"2022-11-28T21:43:00.903358Z","iopub.status.idle":"2022-11-28T21:43:00.911821Z","shell.execute_reply.started":"2022-11-28T21:43:00.903305Z","shell.execute_reply":"2022-11-28T21:43:00.91108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB2\n\nmodel = EfficientNetB2(weights=None, include_top=True, input_shape=(256, 256, 3),classes=1, classifier_activation=\"sigmoid\")","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:43:00.913098Z","iopub.execute_input":"2022-11-28T21:43:00.913391Z","iopub.status.idle":"2022-11-28T21:43:03.09498Z","shell.execute_reply.started":"2022-11-28T21:43:00.913354Z","shell.execute_reply":"2022-11-28T21:43:03.094248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:43:03.096056Z","iopub.execute_input":"2022-11-28T21:43:03.096292Z","iopub.status.idle":"2022-11-28T21:43:03.100783Z","shell.execute_reply.started":"2022-11-28T21:43:03.096259Z","shell.execute_reply":"2022-11-28T21:43:03.099258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/models","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:43:03.102177Z","iopub.execute_input":"2022-11-28T21:43:03.102659Z","iopub.status.idle":"2022-11-28T21:43:04.101756Z","shell.execute_reply.started":"2022-11-28T21:43:03.102614Z","shell.execute_reply":"2022-11-28T21:43:04.100647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = tf.keras.losses.BinaryCrossentropy() #tfa.losses.SigmoidFocalCrossEntropy()\nmodel.compile(loss=loss, \n              optimizer=tfa.optimizers.AdamW(learning_rate=LEARNING_RATE, weight_decay = 0.01), \n              metrics=['binary_accuracy'])\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_binary_accuracy', patience = 3, verbose=1, factor=0.1, min_lr=1e-7)\n\nfilepath = \"/kaggle/models/saved-model-{epoch:02d}-{val_binary_accuracy:.2f}.hdf5\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, \n                             save_best_only=False, save_freq='epoch')\n","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:43:04.105082Z","iopub.execute_input":"2022-11-28T21:43:04.105542Z","iopub.status.idle":"2022-11-28T21:43:04.133847Z","shell.execute_reply.started":"2022-11-28T21:43:04.105504Z","shell.execute_reply":"2022-11-28T21:43:04.133128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:43:04.135241Z","iopub.execute_input":"2022-11-28T21:43:04.135497Z","iopub.status.idle":"2022-11-28T21:43:05.176141Z","shell.execute_reply.started":"2022-11-28T21:43:04.135465Z","shell.execute_reply":"2022-11-28T21:43:05.175337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n      train_generator,\n      epochs=5,\n      validation_data=valid_generator,\n      callbacks=[checkpoint,learning_rate_reduction],\n    class_weight = {0: 3/5, 1: 2/5},\n      verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T21:43:05.177933Z","iopub.execute_input":"2022-11-28T21:43:05.17857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_history():\n    plt.plot(history.history['binary_accuracy'], label='The score of correct predictions on the training set')\n    plt.plot(history.history['val_binary_accuracy'], label='The score of correct predictions on the val set')\n    plt.xlabel('Epoch')\n    plt.ylabel('Score correct answers')\n    plt.legend()\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_acc = 0\nbest_model = \"\"\nfor i in os.listdir(\"/kaggle/models\"):\n    model.load_weights(\"/kaggle/models/\"+i)\n    loss, acc = model.evaluate_generator(valid_generator, steps=3, verbose=0)\n    if acc > best_acc:\n        best_model = i\n        best_acc = acc","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights(\"/kaggle/models/\"+best_model)\nloss, acc = model.evaluate_generator(valid_generator, steps=3, verbose=0)\nacc = acc *100\nprint(f\"accuracy is: {acc:.2f}%\")\nprint(\"best model is \" + best_model)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink(r\"/kaggle/models/\"+best_model)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/working/models","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.move(\"/kaggle/models/\", \"/kaggle/working/models/\")\nshutil.make_archive(\"/kaggle/working/models.zip\", 'zip', \"/kaggle/working/models/\")\n\n\nFileLink(r\"/kaggle/working/models.zip.zip\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}