{"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":"2022-09-14T04:29:02.269028Z","iopub.execute_input":"2022-09-14T04:29:02.269699Z","iopub.status.idle":"2022-09-14T04:29:02.895632Z","shell.execute_reply.started":"2022-09-14T04:29:02.269607Z","shell.execute_reply":"2022-09-14T04:29:02.89425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/mayo-clinic-strip-ai/train.csv\")\ntest = pd.read_csv(\"../input/mayo-clinic-strip-ai/test.csv\")\ntrain.head(),test.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:02.897522Z","iopub.execute_input":"2022-09-14T04:29:02.897974Z","iopub.status.idle":"2022-09-14T04:29:02.931109Z","shell.execute_reply.started":"2022-09-14T04:29:02.897931Z","shell.execute_reply":"2022-09-14T04:29:02.930129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop([\"center_id\",\"image_num\"],axis=1)\ntest = test.drop([\"center_id\",\"image_num\"],axis=1)\ntrain.head(),test.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:02.932393Z","iopub.execute_input":"2022-09-14T04:29:02.933194Z","iopub.status.idle":"2022-09-14T04:29:02.95218Z","shell.execute_reply.started":"2022-09-14T04:29:02.933157Z","shell.execute_reply":"2022-09-14T04:29:02.950967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/stroke-blood-clot-origin-1k-scale-bg-crop/train_images/'\ntest_path = '/kaggle/input/test-images-resized-1024/test/'\ntrain['path'] = train_path + train['image_id'] +\".png\"\ntest['path'] = test_path + test['image_id']+\".png\"","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:02.95447Z","iopub.execute_input":"2022-09-14T04:29:02.955357Z","iopub.status.idle":"2022-09-14T04:29:02.967024Z","shell.execute_reply.started":"2022-09-14T04:29:02.95532Z","shell.execute_reply":"2022-09-14T04:29:02.965854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(), test.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:02.968736Z","iopub.execute_input":"2022-09-14T04:29:02.969542Z","iopub.status.idle":"2022-09-14T04:29:02.983425Z","shell.execute_reply.started":"2022-09-14T04:29:02.969494Z","shell.execute_reply":"2022-09-14T04:29:02.982173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(['image_id'],axis =1)\ntest = test.drop([\"image_id\"],axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:02.985045Z","iopub.execute_input":"2022-09-14T04:29:02.985691Z","iopub.status.idle":"2022-09-14T04:29:02.998199Z","shell.execute_reply.started":"2022-09-14T04:29:02.985655Z","shell.execute_reply":"2022-09-14T04:29:02.996864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(), test.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:02.999878Z","iopub.execute_input":"2022-09-14T04:29:03.000556Z","iopub.status.idle":"2022-09-14T04:29:03.01428Z","shell.execute_reply.started":"2022-09-14T04:29:03.000519Z","shell.execute_reply":"2022-09-14T04:29:03.012993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test = train_test_split(train,test_size=0.2,stratify = train['label'], random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:03.015723Z","iopub.execute_input":"2022-09-14T04:29:03.016349Z","iopub.status.idle":"2022-09-14T04:29:04.286019Z","shell.execute_reply.started":"2022-09-14T04:29:03.016306Z","shell.execute_reply":"2022-09-14T04:29:04.284742Z"},"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                                rotation_range = 30,\n                                shear_range=0.5,\n                                zoom_range=[0.5,0.7],\n                                horizontal_flip=True,\n                                vertical_flip=True\n                                )\nval1_gen = ImageDataGenerator(rescale = 1./255,\n                                rotation_range = 30,\n                                shear_range=0.5,\n                                zoom_range=[0.5,0.7],\n                                horizontal_flip=True,\n                                vertical_flip=True\n                              )\n\ntrain_set = train1_gen.flow_from_dataframe(\n                                           dataframe=x_train,\n                                          directory = '/kaggle/input/stroke-blood-clot-origin-1k-scale-bg-crop/train_images',\n                                          x_col = \"path\",\n                                          y_col=\"label\",\n                                          batch_size=32,\n                                          class_mode = 'binary',\n                                          shuffle= True,\n                                           target_size = (1024,1024)\n                                           \n                                         )\nval_set = val1_gen.flow_from_dataframe(\n                                     dataframe = x_test,\n                                     directory = '/kaggle/input/stroke-blood-clot-origin-1k-scale-bg-crop/train_images',\n                                          x_col = \"path\",\n                                          y_col=\"label\",\n                                          batch_size=32,\n                                          class_mode = 'binary',\n                                          shuffle= True,\n                                         target_size = (1024,1024))","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:04.287549Z","iopub.execute_input":"2022-09-14T04:29:04.288023Z","iopub.status.idle":"2022-09-14T04:29:11.598327Z","shell.execute_reply.started":"2022-09-14T04:29:04.287976Z","shell.execute_reply":"2022-09-14T04:29:11.596903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:11.602883Z","iopub.execute_input":"2022-09-14T04:29:11.604058Z","iopub.status.idle":"2022-09-14T04:29:11.610957Z","shell.execute_reply.started":"2022-09-14T04:29:11.604014Z","shell.execute_reply":"2022-09-14T04:29:11.609787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model = Sequential()\n#layer 1\nModel.add(Conv2D(filters=16,kernel_size=(3,3),activation='relu', input_shape=(1024,1024,3)))\nModel.add(MaxPooling2D(pool_size=(2,2)))\n\n#layer 2\nModel.add(Conv2D(filters=32,kernel_size=(3,3),activation='relu', input_shape=(1024,1024,3)))\nModel.add(MaxPooling2D(pool_size=(2,2)))\n\n#layer 3\nModel.add(Conv2D(filters=32,kernel_size=(3,3),activation='relu'))\nModel.add(MaxPooling2D(pool_size=(2,2)))\n\nModel.add(Flatten())\n\n#Dense Layer\nModel.add(Dense(units=32,activation='relu'))\nModel.add(Dense(units=1,activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:11.612272Z","iopub.execute_input":"2022-09-14T04:29:11.612987Z","iopub.status.idle":"2022-09-14T04:29:11.927625Z","shell.execute_reply.started":"2022-09-14T04:29:11.61294Z","shell.execute_reply":"2022-09-14T04:29:11.926656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:11.929078Z","iopub.execute_input":"2022-09-14T04:29:11.929522Z","iopub.status.idle":"2022-09-14T04:29:11.937259Z","shell.execute_reply.started":"2022-09-14T04:29:11.929477Z","shell.execute_reply":"2022-09-14T04:29:11.936197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.compile( loss=\"binary_crossentropy\", optimizer='rmsprop', metrics=[\"accuracy\"],)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:11.938631Z","iopub.execute_input":"2022-09-14T04:29:11.939032Z","iopub.status.idle":"2022-09-14T04:29:11.95895Z","shell.execute_reply.started":"2022-09-14T04:29:11.938995Z","shell.execute_reply":"2022-09-14T04:29:11.957684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping, CSVLogger\n\n#filepath = '/content/drive/MyDrive/ML Projects/Covid X-Ray Image Classification/best-model-{epoch:02d}.hdf5'\n\n#chk_point = ModelCheckpoint(filepath, monitor='val_loss', mode='min',save_best_only = True, verbose=1)\n\n#Early_stop = EarlyStopping(monitor='val_loss',patience=3,verbose=1)\n\ncsvlog = CSVLogger('logs.csv', separator=\",\", append=False)\n\ncall_backs_list = [csvlog]","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:11.960244Z","iopub.execute_input":"2022-09-14T04:29:11.960575Z","iopub.status.idle":"2022-09-14T04:29:11.96658Z","shell.execute_reply.started":"2022-09-14T04:29:11.960544Z","shell.execute_reply":"2022-09-14T04:29:11.96563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = Model.fit_generator(train_set,\n                                   steps_per_epoch = 601//32,\n                                   #7831//32,\n                                   epochs =50,\n                                   validation_data=val_set,\n                                   validation_steps = 151//32,\n                                   #1990//32,\n                                   callbacks=call_backs_list\n                                   )","metadata":{"execution":{"iopub.status.busy":"2022-09-14T04:29:11.967822Z","iopub.execute_input":"2022-09-14T04:29:11.968632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\ncnn1_log = pd.read_csv('./logs.csv')\nplt.figure(figsize=(5,5))\nplt.title('train_loss vs val_loss')\nplt.plot(cnn1_log.loss,label='train')\nplt.plot(cnn1_log.val_loss,label='val')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn1_log = pd.read_csv('./logs.csv')\nplt.figure(figsize=(5,5))\nplt.title('train_accuracy vs val_accuracy')\nplt.plot(cnn1_log.accuracy,label='train')\nplt.plot(cnn1_log.val_accuracy,label='val')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale = 1./255,\n                                rotation_range = 30,\n                                shear_range=0.5,\n                                zoom_range=[0.5,0.7],\n                                horizontal_flip=True,\n                                vertical_flip=True\n                                )\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n  \n#Model = load_model('model_saved.h5')\n#for i in range:  \nimage = load_img('../input/test-images-resized-1024/test/006388_0.png', target_size=(1024,1024))\nimg = np.array(image)\nimg = img / 255.0\nimg = img.reshape(1,1024,1024,3)\nlabel = Model.predict(img)\nprint(\"Predicted Class (0 - CEE , 1- LAA): \", label[0][0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#image = load_img('../input/test-images-resized-1024/test/008e5c_0.png', target_size=(1024,1024))\n#img = np.array(image)\n#img = img / 255.0\n#img = img.reshape(1,1024,1024,3)\n#label = Model.predict(img)\n#print(\"Predicted Class (0 - CEE , 1- LAA): \", label[0][0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#image = load_img('../input/test-images-resized-1024/test/00c058_0.png', target_size=(1024,1024))\n#img = np.array(image)\n#img = img / 255.0\n#img = img.reshape(1,1024,1024,3)\n#label = Model.predict(img)\n#print(\"Predicted Class (0 - CEE , 1- LAA): \", label[0][0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#image = load_img('../input/test-images-resized-1024/test/01adc5_0.png', target_size=(1024,1024))\n#img = np.array(image)\n#img = img / 255.0\n#img = img.reshape(1,1024,1024,3)\n#label = Model.predict(img)\n#print(\"Predicted Class (0 - CEE , 1- LAA): \", label[0][0])\n#print(label)\n#import numpy as np\n#y_pred = np.argmax(label, axis=1)\n#print(y_pred)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir =\"../input/test-images-resized-1024/test/\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_gen = test_gen.flow_from_directory(test_dir,\n        target_size=(1024,1024),\n        batch_size= 4 ,shuffle=False,\n        class_mode= 'binary',classes=['.'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = Model.predict(test_data_gen)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(test[\"patient_id\"].copy())\nsubmission[\"LAA\"] = y_pred\nsubmission[\"LAA\"] = submission[\"LAA\"].apply(lambda x : 0 if x<0 else x)\nsubmission[\"LAA\"] = submission[\"LAA\"].apply(lambda x : 1 if x>1 else x)\nsubmission[\"CE\"] = 1- submission[\"LAA\"]\n\nsubmission = submission.groupby(\"patient_id\").mean()\nsubmission = submission[[\"CE\", \"LAA\"]].round(6).reset_index()\nsubmission","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"./submission.csv\", index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}