{"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":"from google.colab import drive\ndrive.mount('/content/drive')","metadata":{"id":"YV62ndgs8IJU","outputId":"5284d8d6-6a2f-4fe2-faab-6afd67459162"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras","metadata":{"id":"pp6HucSULsa3"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install pydicom","metadata":{"id":"kNZng8othuP9","outputId":"86f5311f-7265-4fa3-8535-3235fd5b7c4c","execution":{"iopub.status.busy":"2022-04-05T14:30:19.976206Z","iopub.execute_input":"2022-04-05T14:30:19.976843Z","iopub.status.idle":"2022-04-05T14:30:32.334520Z","shell.execute_reply.started":"2022-04-05T14:30:19.976796Z","shell.execute_reply":"2022-04-05T14:30:32.333593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install patool","metadata":{"id":"r0abl448xlKM","outputId":"a00a8fde-8934-42d8-a16f-a148efe43534","execution":{"iopub.status.busy":"2022-04-05T14:30:32.336519Z","iopub.execute_input":"2022-04-05T14:30:32.337379Z","iopub.status.idle":"2022-04-05T14:30:32.826157Z","shell.execute_reply.started":"2022-04-05T14:30:32.337338Z","shell.execute_reply":"2022-04-05T14:30:32.822218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import patoolib\npatoolib.extract_archive(\"/content/drive/MyDrive/train_subset.rar\", outdir=\"/content/sample_data\")","metadata":{"id":"jwzOmJe2wy6v","outputId":"f5db3062-321d-418d-faa3-078aee235702","execution":{"iopub.status.busy":"2022-04-05T14:30:32.830797Z","iopub.status.idle":"2022-04-05T14:30:32.833514Z","shell.execute_reply.started":"2022-04-05T14:30:32.833173Z","shell.execute_reply":"2022-04-05T14:30:32.833209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import patoolib\npatoolib.extract_archive(\"/content/drive/MyDrive/test.zip\", outdir=\"/content/sample_data\")","metadata":{"id":"Kr97soaZ-qyn","outputId":"9f9b2489-60bd-41f7-bb62-5846cf0628c5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cp -av /content/sample_data/train_set/00000 /content/sample_data/test","metadata":{"id":"l11v4kveDsH0","outputId":"46b57439-9762-420d-c59f-3efb4060cf33"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport os\nimport glob\nimport random\nimport time\n\nimport numpy as np\nimport pandas as pd\n\nimport cv2\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nfrom sklearn.model_selection import train_test_split\n\nfrom torch.utils.data import Dataset","metadata":{"id":"K15XdCi3yE6D","execution":{"iopub.status.busy":"2022-04-05T14:30:49.362095Z","iopub.execute_input":"2022-04-05T14:30:49.362381Z","iopub.status.idle":"2022-04-05T14:30:51.941928Z","shell.execute_reply.started":"2022-04-05T14:30:49.362349Z","shell.execute_reply":"2022-04-05T14:30:51.941128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import roc_auc_score\nimport glob","metadata":{"id":"bWIQ9nOWfowx","execution":{"iopub.status.busy":"2022-04-05T14:30:55.155553Z","iopub.execute_input":"2022-04-05T14:30:55.155823Z","iopub.status.idle":"2022-04-05T14:30:55.243749Z","shell.execute_reply.started":"2022-04-05T14:30:55.155793Z","shell.execute_reply":"2022-04-05T14:30:55.242977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lables = os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train')","metadata":{"id":"2VImVXqgHyqB","execution":{"iopub.status.busy":"2022-04-05T14:30:57.705588Z","iopub.execute_input":"2022-04-05T14:30:57.706135Z","iopub.status.idle":"2022-04-05T14:30:57.772221Z","shell.execute_reply.started":"2022-04-05T14:30:57.706086Z","shell.execute_reply":"2022-04-05T14:30:57.771452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l =[] \nfor x in lables:\n  y = x.lstrip('0')\n  if y == '':\n    l.append(0)\n  else:  \n    l.append(int(y))\n","metadata":{"id":"BAHgGEFHH3dy","execution":{"iopub.status.busy":"2022-04-05T14:30:59.750233Z","iopub.execute_input":"2022-04-05T14:30:59.751022Z","iopub.status.idle":"2022-04-05T14:30:59.758087Z","shell.execute_reply.started":"2022-04-05T14:30:59.750982Z","shell.execute_reply":"2022-04-05T14:30:59.757209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lables_t = os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/test')","metadata":{"id":"GHZXoEQlM8Es","execution":{"iopub.status.busy":"2022-04-05T14:31:01.614983Z","iopub.execute_input":"2022-04-05T14:31:01.615273Z","iopub.status.idle":"2022-04-05T14:31:01.632537Z","shell.execute_reply.started":"2022-04-05T14:31:01.615229Z","shell.execute_reply":"2022-04-05T14:31:01.631835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l1 =[] \nfor x in lables_t:\n  y = x.lstrip('0')\n  if y == '':\n    l1.append(0)\n  else:  \n    l1.append(int(y))","metadata":{"id":"8CZprS5rM6Yf","execution":{"iopub.status.busy":"2022-04-05T14:31:02.875410Z","iopub.execute_input":"2022-04-05T14:31:02.875681Z","iopub.status.idle":"2022-04-05T14:31:02.880207Z","shell.execute_reply.started":"2022-04-05T14:31:02.875651Z","shell.execute_reply":"2022-04-05T14:31:02.879339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\n\n","metadata":{"id":"R7sbLcCuyKzJ","execution":{"iopub.status.busy":"2022-04-05T14:31:04.795080Z","iopub.execute_input":"2022-04-05T14:31:04.797410Z","iopub.status.idle":"2022-04-05T14:31:04.811867Z","shell.execute_reply.started":"2022-04-05T14:31:04.797370Z","shell.execute_reply":"2022-04-05T14:31:04.811138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_train = []\nfor x in l:\n  target_train.append(train_label[train_label['BraTS21ID']==x]['MGMT_value'])","metadata":{"id":"Bnr3DLelKbLb","execution":{"iopub.status.busy":"2022-04-05T14:31:08.000195Z","iopub.execute_input":"2022-04-05T14:31:08.000532Z","iopub.status.idle":"2022-04-05T14:31:08.394211Z","shell.execute_reply.started":"2022-04-05T14:31:08.000497Z","shell.execute_reply":"2022-04-05T14:31:08.393315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_test = []\nfor x in l1:\n  target_test.append(train_label[train_label['BraTS21ID']==x]['MGMT_value'])","metadata":{"id":"1Pph9_-xNJDJ","execution":{"iopub.status.busy":"2022-04-05T14:31:09.594948Z","iopub.execute_input":"2022-04-05T14:31:09.595222Z","iopub.status.idle":"2022-04-05T14:31:09.643619Z","shell.execute_reply.started":"2022-04-05T14:31:09.595191Z","shell.execute_reply":"2022-04-05T14:31:09.642781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_train = np.array(target_train)\ntarget_test  = np.array(target_test )","metadata":{"id":"JszrxxDpPBFE","outputId":"62adc1ef-85be-4929-e80f-fb8fafa71bac","execution":{"iopub.status.busy":"2022-04-05T14:31:11.085369Z","iopub.execute_input":"2022-04-05T14:31:11.085731Z","iopub.status.idle":"2022-04-05T14:31:11.097761Z","shell.execute_reply.started":"2022-04-05T14:31:11.085693Z","shell.execute_reply":"2022-04-05T14:31:11.096893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tain_set_org_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train'\nl = os.listdir(tain_set_org_path)\npath5=[]\nfor x in l:\n  path1 = os.path.join(tain_set_org_path,x)\n  path2 = os.listdir(path1)\n  for z in path2:\n    path3 = os.path.join(path1,z)\n    path4 = os.listdir(path3)\n    for y in path4:\n      path5.append(os.path.join(path3,y))\n\n\n","metadata":{"id":"3N289E2ZPuS_","execution":{"iopub.status.busy":"2022-04-05T14:31:12.410254Z","iopub.execute_input":"2022-04-05T14:31:12.410929Z","iopub.status.idle":"2022-04-05T14:32:34.188133Z","shell.execute_reply.started":"2022-04-05T14:31:12.410890Z","shell.execute_reply":"2022-04-05T14:32:34.187327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set_org_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/test'\nl = os.listdir(test_set_org_path)\npath_t=[]\nfor x in l:\n  path1 = os.path.join(test_set_org_path,x)\n  path2 = os.listdir(path1)\n  for z in path2:\n    path3 = os.path.join(path1,z)\n    path4 = os.listdir(path3)\n    for y in path4:\n      path_t.append(os.path.join(path3,y))\n","metadata":{"id":"eP_YWMHe-490","execution":{"iopub.status.busy":"2022-04-05T14:32:34.191187Z","iopub.execute_input":"2022-04-05T14:32:34.191650Z","iopub.status.idle":"2022-04-05T14:32:46.584184Z","shell.execute_reply.started":"2022-04-05T14:32:34.191609Z","shell.execute_reply":"2022-04-05T14:32:46.583391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_IMAGES_3D = 64\nTRAINING_BATCH_SIZE = 8\nTEST_BATCH_SIZE = 8\nIMAGE_SIZE = 64\nN_EPOCHS = 15\ndo_valid = True\nn_workers = 4\n","metadata":{"id":"G0LUFIOZivSQ","execution":{"iopub.status.busy":"2022-04-05T14:32:46.587587Z","iopub.execute_input":"2022-04-05T14:32:46.587797Z","iopub.status.idle":"2022-04-05T14:32:46.594428Z","shell.execute_reply.started":"2022-04-05T14:32:46.587772Z","shell.execute_reply":"2022-04-05T14:32:46.593628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom_image(path, img_size=IMAGE_SIZE, voi_lut=True, rotate=0):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n\n    if rotate > 0:\n        rot_choices = [\n            0,\n            cv2.ROTATE_90_CLOCKWISE,\n            cv2.ROTATE_90_COUNTERCLOCKWISE,\n            cv2.ROTATE_180,\n        ]\n        data = cv2.rotate(data, rot_choices[rotate])\n\n    data = cv2.resize(data, (img_size, img_size))\n    return data","metadata":{"id":"XI6UGFpbhssq","execution":{"iopub.status.busy":"2022-04-05T14:32:50.640097Z","iopub.execute_input":"2022-04-05T14:32:50.640828Z","iopub.status.idle":"2022-04-05T14:32:50.648007Z","shell.execute_reply.started":"2022-04-05T14:32:50.640783Z","shell.execute_reply":"2022-04-05T14:32:50.647190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\ndef subset_of_data_set(path5):\n  data = []\n  for x in path5:\n    num_imgs = NUM_IMAGES_3D\n    img_size=IMAGE_SIZE\n    out = [load_dicom_image(x)]\n    img3d = np.stack(out).T\n    if img3d.shape[-1] < num_imgs:\n      n_zero = np.zeros((img_size, img_size, num_imgs - img3d.shape[-1]))\n      img3d = np.concatenate((img3d, n_zero), axis=-1)\n    if np.min(img3d) < np.max(img3d):\n      img3d = img3d - np.min(img3d)\n      img3d = img3d / np.max(img3d)\n    data.append(out)    \n  return np.array(data)\n\n","metadata":{"id":"2H6XQewXi2S_","execution":{"iopub.status.busy":"2022-04-05T14:33:08.316021Z","iopub.execute_input":"2022-04-05T14:33:08.316321Z","iopub.status.idle":"2022-04-05T14:33:08.323518Z","shell.execute_reply.started":"2022-04-05T14:33:08.316276Z","shell.execute_reply":"2022-04-05T14:33:08.322334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def subset_of_data_set1(path5):\n  data = []\n  for x in path5:\n    out = [load_dicom_image(x)]\n    data.append(out)    \n  return np.array(data)","metadata":{"id":"p3_2BWWpGR8x","execution":{"iopub.status.busy":"2022-04-05T14:33:14.596574Z","iopub.execute_input":"2022-04-05T14:33:14.597190Z","iopub.status.idle":"2022-04-05T14:33:14.601549Z","shell.execute_reply.started":"2022-04-05T14:33:14.597148Z","shell.execute_reply":"2022-04-05T14:33:14.600779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = subset_of_data_set1(path5[0:-1:3])\ndata_test = subset_of_data_set1(path_t)","metadata":{"id":"enzErIeytNy9","execution":{"iopub.status.busy":"2022-04-05T14:33:16.635120Z","iopub.execute_input":"2022-04-05T14:33:16.635839Z","iopub.status.idle":"2022-04-05T15:01:11.039000Z","shell.execute_reply.started":"2022-04-05T14:33:16.635794Z","shell.execute_reply":"2022-04-05T15:01:11.037760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"id":"rqhdq34s5WZt","outputId":"a40fa698-b8e4-42c0-b6e2-78c67faadd8a","execution":{"iopub.status.busy":"2022-04-05T15:01:25.108853Z","iopub.execute_input":"2022-04-05T15:01:25.109124Z","iopub.status.idle":"2022-04-05T15:01:25.115036Z","shell.execute_reply.started":"2022-04-05T15:01:25.109093Z","shell.execute_reply":"2022-04-05T15:01:25.114335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test.shape","metadata":{"id":"9wvO6iYTB42s","outputId":"15c92e3f-a38c-4a9e-a392-26746143b5f6","execution":{"iopub.status.busy":"2022-04-05T15:01:19.636796Z","iopub.execute_input":"2022-04-05T15:01:19.637478Z","iopub.status.idle":"2022-04-05T15:01:19.644893Z","shell.execute_reply.started":"2022-04-05T15:01:19.637435Z","shell.execute_reply":"2022-04-05T15:01:19.643990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data1 = data.reshape(data.shape[0],data.shape[-1],data.shape[-2],1)","metadata":{"id":"R0-_DpaOIMku","execution":{"iopub.status.busy":"2022-04-05T15:01:28.525434Z","iopub.execute_input":"2022-04-05T15:01:28.526001Z","iopub.status.idle":"2022-04-05T15:01:28.530512Z","shell.execute_reply.started":"2022-04-05T15:01:28.525964Z","shell.execute_reply":"2022-04-05T15:01:28.529637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data2 = data_test.reshape(data_test.shape[0],data_test.shape[-1],data_test.shape[-2],1)","metadata":{"id":"gFvIg8BbPKnU","execution":{"iopub.status.busy":"2022-04-05T15:01:31.505014Z","iopub.execute_input":"2022-04-05T15:01:31.507149Z","iopub.status.idle":"2022-04-05T15:01:31.510761Z","shell.execute_reply.started":"2022-04-05T15:01:31.507113Z","shell.execute_reply":"2022-04-05T15:01:31.510092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data1[15].reshape(64,64))\n","metadata":{"id":"5t2A7LlnqAI2","outputId":"9e9288f1-6f03-4d71-d469-d74f50cdd21c","execution":{"iopub.status.busy":"2022-04-05T15:01:45.006180Z","iopub.execute_input":"2022-04-05T15:01:45.006942Z","iopub.status.idle":"2022-04-05T15:01:45.198800Z","shell.execute_reply.started":"2022-04-05T15:01:45.006903Z","shell.execute_reply":"2022-04-05T15:01:45.198114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1 =[]\nfor i  in range(len(path5[0:-1:3])):\n  y = path5[i].split('/')[-3].lstrip('0')\n  if y == '':\n    y1.append(0)\n  else:  \n    y1.append(int(y))\n\n","metadata":{"id":"vlpIQxh6UXih","execution":{"iopub.status.busy":"2022-04-05T15:01:50.585451Z","iopub.execute_input":"2022-04-05T15:01:50.586035Z","iopub.status.idle":"2022-04-05T15:01:50.719942Z","shell.execute_reply.started":"2022-04-05T15:01:50.585977Z","shell.execute_reply":"2022-04-05T15:01:50.719243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y2 =[]\nfor i  in range(len(path_t)):\n  y = path5[i].split('/')[-3].lstrip('0')\n  if y == '':\n    y2.append(0)\n  else:  \n    y2.append(int(y))\n\n","metadata":{"id":"yW9plq5hOVLm","execution":{"iopub.status.busy":"2022-04-05T15:01:53.207946Z","iopub.execute_input":"2022-04-05T15:01:53.208512Z","iopub.status.idle":"2022-04-05T15:01:53.270935Z","shell.execute_reply.started":"2022-04-05T15:01:53.208472Z","shell.execute_reply":"2022-04-05T15:01:53.270268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_train1 = []\nfor x in y1:\n  target_train1.append(train_label[train_label['BraTS21ID']==x]['MGMT_value'].values)","metadata":{"id":"CHV5fhuNURPv","execution":{"iopub.status.busy":"2022-04-05T15:01:55.985264Z","iopub.execute_input":"2022-04-05T15:01:55.985947Z","iopub.status.idle":"2022-04-05T15:02:39.061145Z","shell.execute_reply.started":"2022-04-05T15:01:55.985904Z","shell.execute_reply":"2022-04-05T15:02:39.060333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_test1 = []\nfor x in y2:\n  target_test1.append(train_label[train_label['BraTS21ID']==x]['MGMT_value'].values)","metadata":{"id":"4VIma_VCOgW5","execution":{"iopub.status.busy":"2022-04-05T15:02:39.062693Z","iopub.execute_input":"2022-04-05T15:02:39.063199Z","iopub.status.idle":"2022-04-05T15:02:57.010860Z","shell.execute_reply.started":"2022-04-05T15:02:39.063160Z","shell.execute_reply":"2022-04-05T15:02:57.010106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_train2 = np.array(target_train1)","metadata":{"id":"6RP8B0HYXfqK","execution":{"iopub.status.busy":"2022-04-05T15:02:57.012168Z","iopub.execute_input":"2022-04-05T15:02:57.012431Z","iopub.status.idle":"2022-04-05T15:02:57.070057Z","shell.execute_reply.started":"2022-04-05T15:02:57.012398Z","shell.execute_reply":"2022-04-05T15:02:57.069431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_test2 = np.array(target_test1)\n","metadata":{"id":"RcbxKddqOsD-","execution":{"iopub.status.busy":"2022-04-05T15:02:57.072056Z","iopub.execute_input":"2022-04-05T15:02:57.072345Z","iopub.status.idle":"2022-04-05T15:02:57.100540Z","shell.execute_reply.started":"2022-04-05T15:02:57.072311Z","shell.execute_reply":"2022-04-05T15:02:57.099888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(target_train2.shape,target_test2.shape)","metadata":{"id":"OFV1CXbzOzHL","outputId":"64ecb59c-e7cc-4e1b-d34a-7550e1aeb0ee","execution":{"iopub.status.busy":"2022-04-05T15:02:57.101904Z","iopub.execute_input":"2022-04-05T15:02:57.102158Z","iopub.status.idle":"2022-04-05T15:02:57.107059Z","shell.execute_reply.started":"2022-04-05T15:02:57.102125Z","shell.execute_reply":"2022-04-05T15:02:57.106107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_hot_r=pd.get_dummies(target_train2.ravel(), prefix_sep='_', drop_first=False)\ntarget1=np.array(one_hot_r)\ntarget1.shape","metadata":{"id":"h2CAH-u7YB8e","outputId":"9c918949-c932-4173-a2c2-a8273335a295","execution":{"iopub.status.busy":"2022-04-05T15:02:57.108326Z","iopub.execute_input":"2022-04-05T15:02:57.108758Z","iopub.status.idle":"2022-04-05T15:02:57.135044Z","shell.execute_reply.started":"2022-04-05T15:02:57.108721Z","shell.execute_reply":"2022-04-05T15:02:57.134423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_hot_r1=pd.get_dummies(target_test2.ravel(), prefix_sep='_', drop_first=False)\ntarget2=np.array(one_hot_r1)\ntarget2.shape","metadata":{"id":"HhaGUPzoO9wp","outputId":"74094d72-955f-4165-b164-d7980b19b257","execution":{"iopub.status.busy":"2022-04-05T15:02:57.136091Z","iopub.execute_input":"2022-04-05T15:02:57.136481Z","iopub.status.idle":"2022-04-05T15:02:57.146068Z","shell.execute_reply.started":"2022-04-05T15:02:57.136444Z","shell.execute_reply":"2022-04-05T15:02:57.145317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_set,test_set,target_train,target_test= train_test_split(np.array(pd.concat([pd.DataFrame(data1.reshape(116214,64*64*1)),pd.DataFrame(data2.reshape(51473,64*64*1))])), np.array(pd.concat([pd.DataFrame(target1),pd.DataFrame(target2)])), test_size=0.3, random_state=42)","metadata":{"id":"BoyqEljxZdji","execution":{"iopub.status.busy":"2022-04-05T15:05:03.026289Z","iopub.execute_input":"2022-04-05T15:05:03.027017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = train_set.reshape(train_set.shape[0],64,64,1)\ntest_set = test_set.reshape(test_set.shape[0],64,64,1)","metadata":{"id":"JbwSMy1r_oHg"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(target_train.shape,target_test.shape)\nprint(test_set.shape)\nprint(train_set.shape)","metadata":{"id":"DRJ1HN7oAIGe","outputId":"0be4946e-50c4-4f2d-e1d7-2f1a195a3f8b"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nimport keras_resnet.models\nfrom keras.layers import Input\nfrom keras.layers import Flatten\nfrom keras.layers import Dropout\nfrom keras.layers import Dense\nfrom keras import Model","metadata":{"id":"ZSjTQkpdlWN1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shape, classes = (train_set.shape[1], train_set.shape[2], 1), 2\ninput = Input(shape=(train_set.shape[1], train_set.shape[2], 1))\nmodel = keras_resnet.models.ResNet50(input, classes=classes)\nlast_layer = model.get_layer('pool5').output\nlast_layer1 = Dropout(0.40)(last_layer)\nx= Flatten(name='flatten')(last_layer1)\nx = Dropout(0.40)(x)\nout = Dense(2, activation='softmax', name='output_layer')(x)\ncustom_resnet_model = Model(inputs=input,outputs= out)\n\ncustom_resnet_model.compile(\"adam\", \"binary_crossentropy\", [\"accuracy\"])","metadata":{"id":"v-Kb8XF-w9rH"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"custom_resnet_model.summary()","metadata":{"id":"TW6Ci2VvxAZL","outputId":"f55ba8f0-d4c4-41d5-a694-b26a45624cbc"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shape, classes = (train_set.shape[1], train_set.shape[2], 1), 2\ninput = Input(shape=(train_set.shape[1], train_set.shape[2], 1))\nmodel = keras_resnet.models.ResNet50(input, classes=classes)\nlast_layer = model.get_layer('pool5').output\nlast_layer1 = Dropout(0.25)(last_layer)\nx= Flatten(name='flatten')(last_layer1)\nx = Dropout(0.25)(x)\nout = Dense(2, activation='softmax', name='output_layer')(x)\ncustom_resnet_model = Model(inputs=input,outputs= out)\n\ncustom_resnet_model.compile(\"adam\", \"binary_crossentropy\", [\"accuracy\"])\nhist = custom_resnet_model.fit(train_set, target_train, batch_size=1024,validation_batch_size=1024,epochs=400, verbose=1, validation_split=0.2)\ncustom_resnet_model.save(\"ResNet.h5\")","metadata":{"id":"zUryzIC0lbQt","outputId":"417e39b0-bc07-4133-86af-53d7a5a9b695"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\npd.DataFrame(hist.history).plot(figsize=(10, 5))\nplt.grid(True)\nplt.gca().set_ylim(0, 2) # set the vertical range to [0-1]\nplt.xlabel('number of epochs', fontsize=14)\nplt.ylabel('accuracy and loss', fontsize=14)\nplt.title('result of classification with Resnet',fontsize=16)\nplt.show()","metadata":{"id":"_-I-GForyFCK","outputId":"a80ae854-0c89-4e0e-8d6e-456d92c8e29b"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"custom_resnet_model.evaluate(test_set,target_test)","metadata":{"id":"Iwcus-kQySJY","outputId":"e99df63a-1294-4f50-ac04-92f0849cf387"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scikit-plot\n\nimport scikitplot as skplt\nimport matplotlib.pyplot as plt","metadata":{"id":"yNnkyZR_EUkr","outputId":"a07f4830-d012-4daf-f3de-d25a2dafc65c"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.rcParams[\"figure.figsize\"] = (20,20)\nY_pred = model.predict(test_set)\n#y_pred = np.argmax(Y_pred, axis=1)\ny_test1 =np.argmax(target_test, axis=1)\nskplt.metrics.plot_roc_curve(y_test1,Y_pred,title=\"ROC Curves with Resnet Classifier\", cmap='BuGn', figsize=[5,5])\nplt.show()","metadata":{"id":"-N38keGQDUKw","outputId":"34fc6c1d-d379-4f8c-ffb4-3d9086466c8f"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.metrics import classification_report, confusion_matrix\nplt.rcParams[\"figure.figsize\"] = (20,20)\nY_pred = model.predict(test_set)\ny_pred = np.argmax(Y_pred, axis=1)\nprint('Confusion Matrix')\nprint(confusion_matrix(np.argmax(target_test, axis=1), y_pred))\nprint('Classification Report')\nskplt.metrics.plot_confusion_matrix(np.argmax(target_test, axis=1), y_pred, normalize=True,cmap='RdBu',figsize=[5,5])","metadata":{"id":"LwLww7qqFFLl","outputId":"7a975394-e335-426d-d212-f43cdaa31e51"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = custom_resnet_model.predict(test_set)","metadata":{"id":"Ur86Hgq0jV4U"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predd = custom_resnet_model.predict(train_set)","metadata":{"id":"0wHdnzhmMuZ1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_total = pd.concat([pd.DataFrame(predd),pd.DataFrame(pred)])\npred_total = np.array(pred_total)","metadata":{"id":"JPvxGZgvM5F7"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_total1=[]\nfor x in pred_total:\n  pred_total1.append(max(x))\n","metadata":{"id":"_19E83tWN_Tb"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(pred_total).value_counts()","metadata":{"id":"NpIgBoTLOnvx","outputId":"08746898-1e8a-43b3-e3d4-a20f4536cf49"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_total1 = pd.DataFrame(pred_total1)","metadata":{"id":"N-DSsXZNOWy1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label['pred_total'] = pred_total1","metadata":{"id":"pKRrui0ZNkX2"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label","metadata":{"id":"_tR1S-AhOdqv","outputId":"9e2d106c-8577-4f2f-b9eb-6c684601b9ab"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(target_test).value_counts()","metadata":{"id":"LRJ3sCBokhBr","outputId":"0082f91c-f145-4d48-b96c-35c6ca6da99d"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred1=[]\nfor x in pred:\n  pred1.append(max(x))","metadata":{"id":"ajq_OTmcLsLj"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out = []\nfor x in pred:\n  out.append(np.argmax(x))\nout = np.array(out)","metadata":{"id":"PeV821mWjygS"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(out).value_counts()","metadata":{"id":"dzG3h0j6KKax","outputId":"78cd6d9a-8f1d-4960-a359-d86e6db18ed0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.rcParams[\"figure.figsize\"] = (5,5)\nplt.bar(np.arange(0,2) ,pd.DataFrame(out).value_counts())","metadata":{"id":"X5RYUOzeKPk7","outputId":"7b1f47d8-97f6-4d61-a407-22c9d12bbdc4"},"execution_count":null,"outputs":[]}]}