{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-30T07:06:04.049656Z","iopub.execute_input":"2023-10-30T07:06:04.050025Z","iopub.status.idle":"2023-10-30T07:06:07.706314Z","shell.execute_reply.started":"2023-10-30T07:06:04.049946Z","shell.execute_reply":"2023-10-30T07:06:07.705102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\nimport numpy as np\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Conv2D,MaxPool2D,Flatten,Dropout,BatchNormalization,Activation\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt\nfrom keras.preprocessing.image import load_img,img_to_array\nfrom sklearn.model_selection import KFold\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:07.708615Z","iopub.execute_input":"2023-10-30T07:06:07.709057Z","iopub.status.idle":"2023-10-30T07:06:13.501793Z","shell.execute_reply.started":"2023-10-30T07:06:07.709016Z","shell.execute_reply":"2023-10-30T07:06:13.500644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"../input/preprocessed59/RVDSFS.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/mayo-clinic-strip-ai/test.csv\")\nother = pd.read_csv(\"/kaggle/input/mayo-clinic-strip-ai/other.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:13.503203Z","iopub.execute_input":"2023-10-30T07:06:13.503784Z","iopub.status.idle":"2023-10-30T07:06:13.547164Z","shell.execute_reply.started":"2023-10-30T07:06:13.50375Z","shell.execute_reply":"2023-10-30T07:06:13.546221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"../input/preprocessedimages/CE_LAA\"\n\ntrain_data.head","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:13.549571Z","iopub.execute_input":"2023-10-30T07:06:13.549874Z","iopub.status.idle":"2023-10-30T07:06:13.568043Z","shell.execute_reply.started":"2023-10-30T07:06:13.549846Z","shell.execute_reply":"2023-10-30T07:06:13.567043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ce_1 = train_data[train_data.TYPE.eq('CE')].sample(2181)\nlaa_1 = train_data[train_data.TYPE.eq('LAA')].sample(2814)\ntrain_1 = ce_1.append(laa_1)\ntrain_1.TYPE.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:13.569002Z","iopub.execute_input":"2023-10-30T07:06:13.569364Z","iopub.status.idle":"2023-10-30T07:06:13.591439Z","shell.execute_reply.started":"2023-10-30T07:06:13.569339Z","shell.execute_reply":"2023-10-30T07:06:13.59059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_1.head","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:13.592517Z","iopub.execute_input":"2023-10-30T07:06:13.592791Z","iopub.status.idle":"2023-10-30T07:06:13.602765Z","shell.execute_reply.started":"2023-10-30T07:06:13.592765Z","shell.execute_reply":"2023-10-30T07:06:13.601802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train1, val1 = train_test_split(train_1,test_size=0.1,stratify = train_1['TYPE'], random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:13.603931Z","iopub.execute_input":"2023-10-30T07:06:13.604225Z","iopub.status.idle":"2023-10-30T07:06:13.621167Z","shell.execute_reply.started":"2023-10-30T07:06:13.604199Z","shell.execute_reply":"2023-10-30T07:06:13.62028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain1_gen = ImageDataGenerator(rescale = 1./255)\n\nval1_gen = ImageDataGenerator(rescale = 1./255)\n\ntrain_dataSet = train1_gen.flow_from_dataframe(dataframe = x_train1,\n                                               directory = \"../input/preprocessedimages/CE_LAA\",\n                                               x_col = \"PATIENTID\",\n                                               y_col = \"TYPE\",\n                                               batch_size = 32,\n                                               class_mode = 'binary',\n                                               shuffle = True,\n                                               target_size = (224,224)\n                                               )\n\nval_set = val1_gen.flow_from_dataframe(dataframe = val1,\n                                       directory = \"../input/preprocessedimages/CE_LAA\",\n                                       x_col = \"PATIENTID\",\n                                       y_col = \"TYPE\",\n                                       batch_size = 32,\n                                       class_mode = 'binary',\n                                       shuffle = True,\n                                       target_size = (224,224)\n                                      \n                                      )","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:13.622392Z","iopub.execute_input":"2023-10-30T07:06:13.622689Z","iopub.status.idle":"2023-10-30T07:06:15.883282Z","shell.execute_reply.started":"2023-10-30T07:06:13.622662Z","shell.execute_reply":"2023-10-30T07:06:15.882282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataSet.class_indices","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:15.884435Z","iopub.execute_input":"2023-10-30T07:06:15.884701Z","iopub.status.idle":"2023-10-30T07:06:15.891038Z","shell.execute_reply.started":"2023-10-30T07:06:15.884676Z","shell.execute_reply":"2023-10-30T07:06:15.890118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convlayer=tf.keras.applications.resnet_v2.ResNet101V2(input_shape=(224,224,3),weights=None,include_top=False)\nfor layer in convlayer.layers:\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:15.892196Z","iopub.execute_input":"2023-10-30T07:06:15.892587Z","iopub.status.idle":"2023-10-30T07:06:20.414125Z","shell.execute_reply.started":"2023-10-30T07:06:15.892535Z","shell.execute_reply":"2023-10-30T07:06:20.413125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(convlayer)\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(BatchNormalization())\nmodel.add(Dense(2048,kernel_initializer='he_uniform'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1024,kernel_initializer='he_uniform'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('softmax'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1,activation='sigmoid'))\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:20.417637Z","iopub.execute_input":"2023-10-30T07:06:20.418428Z","iopub.status.idle":"2023-10-30T07:06:21.257616Z","shell.execute_reply.started":"2023-10-30T07:06:20.418382Z","shell.execute_reply":"2023-10-30T07:06:21.256655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import ImageChops\nimport glob\nimport os\ndirectory=\"/kaggle/input/preprocessedimages/CE_LAA\"\nmyfiles=glob.glob(directory)\nmyfiles.sort()\nfor j in myfiles:\n    file=''+j\n    if(os.path.getsize(file)<50*1024):\n        os.remove(file)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:06:53.010771Z","iopub.execute_input":"2023-10-30T07:06:53.011209Z","iopub.status.idle":"2023-10-30T07:06:53.038965Z","shell.execute_reply.started":"2023-10-30T07:06:53.011173Z","shell.execute_reply":"2023-10-30T07:06:53.037716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt=tf.keras.optimizers.RMSprop(lr=0.009)\nmodel.compile(loss='binary_crossentropy',metrics=['accuracy','log_loss'],optimizer='adam')  \nhistory_1=model.fit(train_dataSet,validation_data=val_set,epochs=100)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:25:19.525143Z","iopub.execute_input":"2023-10-30T07:25:19.526125Z","iopub.status.idle":"2023-10-30T07:25:22.167427Z","shell.execute_reply.started":"2023-10-30T07:25:19.526083Z","shell.execute_reply":"2023-10-30T07:25:22.166038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\n\ndef f1_score(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    print(precision)\n    recall = true_positives / (possible_positives + K.epsilon())\n    print(recall)\n    f1 = 2 * (precision * recall) / (precision + recall + K.epsilon())\n    return f1,precision,recall\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:30:35.837514Z","iopub.execute_input":"2023-10-30T07:30:35.837909Z","iopub.status.idle":"2023-10-30T07:30:35.846117Z","shell.execute_reply.started":"2023-10-30T07:30:35.837876Z","shell.execute_reply":"2023-10-30T07:30:35.845133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt=tf.keras.optimizers.RMSprop(lr=0.009)\nmodel.compile(loss='binary_crossentropy',metrics=['accuracy',f1_score],optimizer='adam')  \nhistory_1=model.fit(train_dataSet,validation_data=val_set,epochs=100)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:30:40.656988Z","iopub.execute_input":"2023-10-30T07:30:40.657883Z","iopub.status.idle":"2023-10-30T07:35:29.270583Z","shell.execute_reply.started":"2023-10-30T07:30:40.657843Z","shell.execute_reply":"2023-10-30T07:35:29.269166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}