{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":23870,"databundleVersionId":1781260,"sourceType":"competition"}],"dockerImageVersionId":30132,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport tensorflow as tf\nimport tensorflow.keras.layers as tfl\nfrom tensorflow.keras import losses\nfrom tensorflow.keras.layers import Flatten ,Dense, Dropout\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nfrom tensorflow.keras.layers.experimental.preprocessing import RandomFlip, RandomRotation,RandomCrop,RandomContrast,Normalization\n\n\n#load data\nimport pandas as pd\ntrain_df = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/train.csv')\n#sample_df.shape\ndef append_ext(fn):\n    return fn+\".jpg\"\n\ntrain_df[\"StudyInstanceUID\"]=train_df[\"StudyInstanceUID\"].apply(append_ext)\n\ntrain_df.shape\n\nBATCH_SIZE = 8\nIMG_SIZE = (650, 650)\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nlabel=['ETT - Abnormal', 'ETT - Borderline',\n       'ETT - Normal', 'NGT - Abnormal', 'NGT - Borderline',\n       'NGT - Incompletely Imaged', 'NGT - Normal', 'CVC - Abnormal',\n       'CVC - Borderline', 'CVC - Normal', 'Swan Ganz Catheter Present']\n\ndatagen=ImageDataGenerator(validation_split=0.20,\n                           rotation_range=0.30,\n                           vertical_flip= True,\n\n                          #Normalization = True,\n                          rescale=1./255.)\n                          \n\ntrain_dataset=datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=\"../input/ranzcr-clip-catheter-line-classification/train/\",\n    x_col=\"StudyInstanceUID\",\n    y_col=label,\n    subset=\"training\",\n    batch_size=BATCH_SIZE,\n    color_mode='rgb',\n    labels_mode ='binary',\n    class_mode='raw',\n    target_size=IMG_SIZE,\n    #rotation_range=0.30,\n    #vertical_flip= True,\n    shuffle=1024,\n    seed=42,\n    interpolation=\"bilinear\")\n\nvalidation_dataset=datagen.flow_from_dataframe(\ndataframe=train_df,\ndirectory=\"../input/ranzcr-clip-catheter-line-classification/train\",\nx_col=\"StudyInstanceUID\",\ny_col=label,\nsubset=\"validation\",\nbatch_size=BATCH_SIZE,\ncolor_mode='rgb',\nlabels_mode ='binary',\nclass_mode='raw',\ntarget_size=IMG_SIZE,\n#shuffle=1024,\nshuffle=False,\nseed=42,\ninterpolation=\"bilinear\")\n\n\nimage_shape = (650,650)\ninput_shape = image_shape + (3,)\nim_size =650\nfrom tensorflow.keras import Model, initializers, regularizers\n\nfrom tensorflow.keras.applications.densenet import DenseNet169\n\n\nmodelB7 = tf.keras.Sequential([DenseNet169(input_shape=(im_size, im_size, 3),\n                                                weights='imagenet',\n                                                include_top=False\n                                                ),\n                             tf.keras.layers.GlobalAveragePooling2D()])\n    \n\n    \n    \ninputs = tf.keras.Input(shape=input_shape) \n\n#x = data_augmenter()(inputs)    \nx = modelB7(inputs) \n#x =  Flatten()(x)\nx = Dropout(0.6)(x)\n#x = tfl.GlobalAveragePooling2D()(x)\noutputs = tfl.Dense(11,activation='sigmoid')(x)\n    \n\nmodel = tf.keras.Model(inputs, outputs)  \ntf.compat.v1.reset_default_graph()\nmodel.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n    loss='binary_crossentropy',\n    metrics=[tf.keras.metrics.AUC(multi_label=True)])\nmodel.summary()\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-08T21:42:10.714522Z","iopub.execute_input":"2024-02-08T21:42:10.715148Z","iopub.status.idle":"2024-02-08T21:43:09.079220Z","shell.execute_reply.started":"2024-02-08T21:42:10.715044Z","shell.execute_reply":"2024-02-08T21:43:09.078478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"steps_per_epoch = 24067 // BATCH_SIZE\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(\n    './model.h5', save_best_only=True, monitor='val_auc', mode='max')\nlr_reducer = tf.keras.callbacks.ReduceLROnPlateau(\n    monitor='val_auc', patience=3, min_lr=1e-6, mode='max')\n\n\ninitial_learning_rate = 1e-4\ndef lr_exp_decay(epoch, lr):\n    k = 0.4\n    return initial_learning_rate * tf.math.exp(-k*epoch)\n\nhistory = model.fit(\n    train_dataset,\n    verbose=True,\n    epochs=7,\n    #initial_epoch=history.epoch[-1],\n    #callbacks=[checkpoint],\n    callbacks=[checkpoint, tf.keras.callbacks.LearningRateScheduler(lr_exp_decay, verbose=1)],\n    #steps_per_epoch=steps_per_epoch,\n    validation_data=validation_dataset)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-08T21:43:09.081077Z","iopub.execute_input":"2024-02-08T21:43:09.081382Z","iopub.status.idle":"2024-02-09T06:22:28.772722Z","shell.execute_reply.started":"2024-02-08T21:43:09.081343Z","shell.execute_reply":"2024-02-09T06:22:28.772028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_df = pd.DataFrame(history.history) \n# or save to csv: \nhist_csv_file = /kaggle/working/+'DenseNet169_2024V1.csv'\nwith open(hist_csv_file, mode='w') as f:\n   hist_df.to_csv(f)\n\nmodel.save(\"/kaggle/working/DenseNet169_2024V1.h5\") ","metadata":{"execution":{"iopub.status.busy":"2024-02-09T06:22:28.774133Z","iopub.execute_input":"2024-02-09T06:22:28.774382Z","iopub.status.idle":"2024-02-09T06:22:28.780441Z","shell.execute_reply.started":"2024-02-09T06:22:28.774354Z","shell.execute_reply":"2024-02-09T06:22:28.779517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}