{"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":"\nimport os\nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom PIL import Image\nimport warnings\nwarnings.filterwarnings('ignore')\nimport cv2\nfrom openslide import OpenSlide\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense\nfrom tensorflow.keras.utils import to_categorical\n\n\ntrain_csv= pd.read_csv('../input/mayo-clinic-strip-ai/train.csv')\ntest_csv= pd.read_csv('../input/mayo-clinic-strip-ai/test.csv')\nother_csv= pd.read_csv('../input/mayo-clinic-strip-ai/other.csv')\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-31T07:53:12.365426Z","iopub.execute_input":"2022-08-31T07:53:12.365855Z","iopub.status.idle":"2022-08-31T07:53:19.486152Z","shell.execute_reply.started":"2022-08-31T07:53:12.365771Z","shell.execute_reply":"2022-08-31T07:53:19.484985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-31T07:53:19.48804Z","iopub.execute_input":"2022-08-31T07:53:19.488431Z","iopub.status.idle":"2022-08-31T07:53:19.511757Z","shell.execute_reply.started":"2022-08-31T07:53:19.488395Z","shell.execute_reply":"2022-08-31T07:53:19.51062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.describe(include='object')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T07:53:19.513427Z","iopub.execute_input":"2022-08-31T07:53:19.513802Z","iopub.status.idle":"2022-08-31T07:53:19.541228Z","shell.execute_reply.started":"2022-08-31T07:53:19.513765Z","shell.execute_reply":"2022-08-31T07:53:19.540216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other_csv.describe(include='object')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T07:53:19.543929Z","iopub.execute_input":"2022-08-31T07:53:19.544425Z","iopub.status.idle":"2022-08-31T07:53:19.565207Z","shell.execute_reply.started":"2022-08-31T07:53:19.544388Z","shell.execute_reply":"2022-08-31T07:53:19.564318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['path']= train_csv['image_id'].apply(lambda x:'../input/mayo-clinic-strip-ai/train/'+x+'.tif' )\ntest_csv['path']= test_csv['image_id'].apply(lambda x:'../input/mayo-clinic-strip-ai/test/'+x+'.tif' )\ntrain_csv['label']= train_csv['label'].apply(lambda x: 0 if x=='CE' else 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T07:53:19.566532Z","iopub.execute_input":"2022-08-31T07:53:19.566986Z","iopub.status.idle":"2022-08-31T07:53:19.576807Z","shell.execute_reply.started":"2022-08-31T07:53:19.566949Z","shell.execute_reply":"2022-08-31T07:53:19.575838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-31T07:53:19.578609Z","iopub.execute_input":"2022-08-31T07:53:19.578884Z","iopub.status.idle":"2022-08-31T07:53:19.59318Z","shell.execute_reply.started":"2022-08-31T07:53:19.578859Z","shell.execute_reply":"2022-08-31T07:53:19.591958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nslide = OpenSlide(train_csv.loc[1, \"path\"])\nregion = (0, 0)\nsize = (10000, 10000)\nregion = slide.read_region(region, 0, size)\nplt.figure(figsize=(8, 8))\nplt.imshow(region)\nplt.show() ","metadata":{"execution":{"iopub.status.busy":"2022-08-31T07:53:19.595446Z","iopub.execute_input":"2022-08-31T07:53:19.59605Z","iopub.status.idle":"2022-08-31T07:53:34.617466Z","shell.execute_reply.started":"2022-08-31T07:53:19.596011Z","shell.execute_reply":"2022-08-31T07:53:34.616292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain=[]\ntest=[]\n# getting training images\nfor img_path in train_csv['path']:\n    slide=OpenSlide(img_path)\n    region= (1000,1000)    \n    size  = (5000, 5000)\n    image = slide.read_region(region, 0, size)\n    image = tf.image.resize(image, (512, 512))\n    image = np.array(image) \n    img_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    grayed = cv2.cvtColor(img_RGB, cv2.COLOR_BGR2GRAY)\n    grayed = np.uint8(grayed)\n    edged = cv2.Canny(grayed,100,200)\n    train.append(edged)\n    \n# getting testing images                      \nfor img_path in test_csv['path']:\n    slide=OpenSlide(img_path)\n    region= (1000,1000)    \n    size  = (5000, 5000)\n    image = slide.read_region(region, 0, size)\n    image = tf.image.resize(image, (512, 512))\n    image = np.array(image) \n    img_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    grayed = cv2.cvtColor(img_RGB, cv2.COLOR_BGR2GRAY)\n    grayed= np.uint8(grayed)\n    edged = cv2.Canny(grayed,100,200)\n    test.append(edged)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T07:53:34.618749Z","iopub.execute_input":"2022-08-31T07:53:34.620001Z","iopub.status.idle":"2022-08-31T08:36:54.693049Z","shell.execute_reply.started":"2022-08-31T07:53:34.619966Z","shell.execute_reply":"2022-08-31T08:36:54.691893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train[1])","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:36:54.694833Z","iopub.execute_input":"2022-08-31T08:36:54.695217Z","iopub.status.idle":"2022-08-31T08:36:54.913145Z","shell.execute_reply.started":"2022-08-31T08:36:54.695175Z","shell.execute_reply":"2022-08-31T08:36:54.912241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=np.reshape(train,(len(train),512,512,1))\ntest=np.reshape(test,(len(test),512,512,1))\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:36:54.9168Z","iopub.execute_input":"2022-08-31T08:36:54.917142Z","iopub.status.idle":"2022-08-31T08:36:54.985642Z","shell.execute_reply.started":"2022-08-31T08:36:54.917113Z","shell.execute_reply":"2022-08-31T08:36:54.984517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model= Sequential()\n\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),strides=(2,2),padding='same',activation='relu',input_shape=(512,512,1)))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(Dropout(.25))\n\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),strides=(2,2),padding='same',activation='relu'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(Dropout(.25))\n\nmodel.add(Conv2D(filters=32,kernel_size=(3,3),strides=(2,2),padding='same',activation='relu'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(Dropout(.25))\n\nmodel.add(Flatten())\n\n\nmodel.add(Dense(2, activation='softmax'))\nmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:36:54.98723Z","iopub.execute_input":"2022-08-31T08:36:54.987712Z","iopub.status.idle":"2022-08-31T08:36:55.126012Z","shell.execute_reply.started":"2022-08-31T08:36:54.987675Z","shell.execute_reply":"2022-08-31T08:36:55.12487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x= train\ny=to_categorical(train_csv['label'])\n\nhistory= model.fit(x,y,batch_size=64,epochs=20,validation_split=.2,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:36:55.127234Z","iopub.execute_input":"2022-08-31T08:36:55.127597Z","iopub.status.idle":"2022-08-31T08:37:37.073389Z","shell.execute_reply.started":"2022-08-31T08:36:55.12756Z","shell.execute_reply":"2022-08-31T08:37:37.07228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=model.predict(test)\npred","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:37.075149Z","iopub.execute_input":"2022-08-31T08:37:37.075554Z","iopub.status.idle":"2022-08-31T08:37:37.230705Z","shell.execute_reply.started":"2022-08-31T08:37:37.075515Z","shell.execute_reply":"2022-08-31T08:37:37.229694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss= pd.read_csv('../input/mayo-clinic-strip-ai/sample_submission.csv')\nss","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:37.234201Z","iopub.execute_input":"2022-08-31T08:37:37.235321Z","iopub.status.idle":"2022-08-31T08:37:37.253825Z","shell.execute_reply.started":"2022-08-31T08:37:37.235264Z","shell.execute_reply":"2022-08-31T08:37:37.252972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission= pd.DataFrame(pred,columns=['CE','LAA'])\nsubmission['patient_id']=test_csv['patient_id']\n\nfirst_column = submission.pop('patient_id')\nsubmission.insert(0, 'patient_id', first_column)\nsubmission.groupby('patient_id').mean()\n\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:37.256463Z","iopub.execute_input":"2022-08-31T08:37:37.256747Z","iopub.status.idle":"2022-08-31T08:37:37.289256Z","shell.execute_reply.started":"2022-08-31T08:37:37.25672Z","shell.execute_reply":"2022-08-31T08:37:37.288322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:37.29049Z","iopub.execute_input":"2022-08-31T08:37:37.290844Z","iopub.status.idle":"2022-08-31T08:37:37.303238Z","shell.execute_reply.started":"2022-08-31T08:37:37.290809Z","shell.execute_reply":"2022-08-31T08:37:37.301889Z"},"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":[]}]}