{"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\n# import os\n# for 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":"2022-08-25T08:20:51.786357Z","iopub.execute_input":"2022-08-25T08:20:51.786848Z","iopub.status.idle":"2022-08-25T08:20:51.795091Z","shell.execute_reply.started":"2022-08-25T08:20:51.786814Z","shell.execute_reply":"2022-08-25T08:20:51.793535Z"},"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","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:20:53.615852Z","iopub.execute_input":"2022-08-25T08:20:53.61702Z","iopub.status.idle":"2022-08-25T08:20:53.624827Z","shell.execute_reply.started":"2022-08-25T08:20:53.616968Z","shell.execute_reply":"2022-08-25T08:20:53.623951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/mayo-clinic-strip-ai/train.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":"2022-08-25T08:20:55.210383Z","iopub.execute_input":"2022-08-25T08:20:55.211286Z","iopub.status.idle":"2022-08-25T08:20:55.229945Z","shell.execute_reply.started":"2022-08-25T08:20:55.211244Z","shell.execute_reply":"2022-08-25T08:20:55.228985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"/kaggle/input/mayoclinicai-224456600-resized-training-images/resized_training_dataset/img_456_folder/\"\ntrain_data['path'] = train_path + train_data.image_id + \".jpg\"\ntrain_data.head","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:20:56.340101Z","iopub.execute_input":"2022-08-25T08:20:56.341012Z","iopub.status.idle":"2022-08-25T08:20:56.35848Z","shell.execute_reply.started":"2022-08-25T08:20:56.340967Z","shell.execute_reply":"2022-08-25T08:20:56.356869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ce_1 = train_data[train_data.label.eq('CE')].sample(207)\nlaa_1 = train_data[train_data.label.eq('LAA')].sample(207)\ntrain_1 = ce_1.append(laa_1)\ntrain_1.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:20:57.455716Z","iopub.execute_input":"2022-08-25T08:20:57.456241Z","iopub.status.idle":"2022-08-25T08:20:57.47546Z","shell.execute_reply.started":"2022-08-25T08:20:57.456198Z","shell.execute_reply":"2022-08-25T08:20:57.473832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_1.head","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:20:59.169313Z","iopub.execute_input":"2022-08-25T08:20:59.169845Z","iopub.status.idle":"2022-08-25T08:20:59.186388Z","shell.execute_reply.started":"2022-08-25T08:20:59.169801Z","shell.execute_reply":"2022-08-25T08:20:59.184998Z"},"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.2,stratify = train_1['label'], random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:00.210017Z","iopub.execute_input":"2022-08-25T08:21:00.211065Z","iopub.status.idle":"2022-08-25T08:21:00.221512Z","shell.execute_reply.started":"2022-08-25T08:21:00.211008Z","shell.execute_reply":"2022-08-25T08:21:00.219824Z"},"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 = \"/kaggle/input/mayoclinicai-224456600-resized-training-images/resized_training_dataset/img_224_folder/\",\n                                               x_col = \"path\",\n                                               y_col = \"label\",\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 = \"/kaggle/input/mayoclinicai-224456600-resized-training-images/resized_training_dataset/img_224_folder/\",\n                                       x_col = \"path\",\n                                       y_col = \"label\",\n                                       batch_size = 32,\n                                       class_mode = 'binary',\n                                       shuffle = True,\n                                       target_size = (224,224)\n                                      \n                                      )\n","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:02.205909Z","iopub.execute_input":"2022-08-25T08:21:02.206516Z","iopub.status.idle":"2022-08-25T08:21:02.623091Z","shell.execute_reply.started":"2022-08-25T08:21:02.206472Z","shell.execute_reply":"2022-08-25T08:21:02.621759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataSet.class_indices","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:04.180461Z","iopub.execute_input":"2022-08-25T08:21:04.180953Z","iopub.status.idle":"2022-08-25T08:21:04.189553Z","shell.execute_reply.started":"2022-08-25T08:21:04.180897Z","shell.execute_reply":"2022-08-25T08:21:04.188505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tf.keras.applications.resnet_v2.ResNet101V2(\n#     include_top=True,\n#     weights='imagenet',\n#     input_tensor=None,\n#     input_shape=None,\n#     pooling=None,\n#     classes=1000,\n#     classifier_activation='softmax'\n# )","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:05.68011Z","iopub.execute_input":"2022-08-25T08:21:05.681342Z","iopub.status.idle":"2022-08-25T08:21:05.686581Z","shell.execute_reply.started":"2022-08-25T08:21:05.681299Z","shell.execute_reply":"2022-08-25T08:21:05.685199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras.applications import ResNet101V2\nconvlayer=tf.keras.applications.resnet_v2.ResNet101V2(input_shape=(224,224,3),weights='imagenet',include_top=False)\nfor layer in convlayer.layers:\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:16.155288Z","iopub.execute_input":"2022-08-25T08:21:16.156541Z","iopub.status.idle":"2022-08-25T08:21:19.784725Z","shell.execute_reply.started":"2022-08-25T08:21:16.156491Z","shell.execute_reply":"2022-08-25T08:21:19.783237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import tensorflow as tf\n# import numpy as np\n# class MyModel(tf.keras.Model):\n\n\n#   def __init__(self):\n#     super(MyModel, self).__init__()\n#     self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)\n#     self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)\n\n\n#   def call(self, inputs):\n#     x = self.dense1(inputs)\n#     return self.dense2(x)\n\n\n# x = np.random.random((4, 3))\n# y = np.random.randint(0, 2, (4, 1))\n\n# # generate random training data\n# x = np.random.random((4, 3))\n# y = np.random.randint(0, 2, (4, 1))\n# # initilize the model\n# model = MyModel()\n# # model.summary()\n# model.build(input_shape=(None,3))","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:21.584868Z","iopub.execute_input":"2022-08-25T08:21:21.585339Z","iopub.status.idle":"2022-08-25T08:21:21.592613Z","shell.execute_reply.started":"2022-08-25T08:21:21.585306Z","shell.execute_reply":"2022-08-25T08:21:21.59078Z"},"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('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1,activation='softmax'))\nprint(model.summary())\n\n\n#  Create a `Sequential` model and add a Dense layer as the first layer.\n# >>> model = tf.keras.models.Sequential()\n# >>> model.add(tf.keras.Input(shape=(16,)))\n# >>> model.add(tf.keras.layers.Dense(32, activation='relu'))\n# >>> # Now the model will take as input arrays of shape (None, 16)\n# >>> # and output arrays of shape (None, 32).\n# >>> # Note that after the first layer, you don't need to specify\n# >>> # the size of the input anymore:\n# >>> model.add(tf.keras.layers.Dense(32))\n# >>> model.output_shape\n# (None, 32)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:22.84502Z","iopub.execute_input":"2022-08-25T08:21:22.845479Z","iopub.status.idle":"2022-08-25T08:21:24.721882Z","shell.execute_reply.started":"2022-08-25T08:21:22.845445Z","shell.execute_reply":"2022-08-25T08:21:24.720905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:26.200511Z","iopub.execute_input":"2022-08-25T08:21:26.202475Z","iopub.status.idle":"2022-08-25T08:21:26.208494Z","shell.execute_reply.started":"2022-08-25T08:21:26.202395Z","shell.execute_reply":"2022-08-25T08:21:26.207185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# opt=tf.keras.optimizers.Adam(lr=0.0001)\n# model.compile(loss='binary_crossentropy',metrics=['accuracy'],optimizer=opt)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:28.384418Z","iopub.execute_input":"2022-08-25T08:21:28.385948Z","iopub.status.idle":"2022-08-25T08:21:28.389769Z","shell.execute_reply.started":"2022-08-25T08:21:28.385864Z","shell.execute_reply":"2022-08-25T08:21:28.388978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history=model.fit(train_dataSet,validation_data=val_set,epochs=5)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:21:28.760335Z","iopub.execute_input":"2022-08-25T08:21:28.761184Z","iopub.status.idle":"2022-08-25T08:21:28.765542Z","shell.execute_reply.started":"2022-08-25T08:21:28.761141Z","shell.execute_reply":"2022-08-25T08:21:28.764521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt=tf.keras.optimizers.RMSprop(lr=0.001)\nmodel.compile(loss='binary_crossentropy',metrics=['accuracy'],optimizer=opt)\nhistory_1=model.fit(train_dataSet,validation_data=val_set,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T08:23:25.570668Z","iopub.execute_input":"2022-08-25T08:23:25.571202Z","iopub.status.idle":"2022-08-25T08:35:09.813949Z","shell.execute_reply.started":"2022-08-25T08:23:25.571147Z","shell.execute_reply":"2022-08-25T08:35:09.812315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}