{"metadata":{"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.12"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport pydicom\nimport numpy as np\nimport os\nimport glob\nfrom tqdm import tqdm\nimport gc\n\nimport torchvision\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset\nfrom fastai.vision.all import *\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:32.843343Z","iopub.status.busy":"2024-07-13T12:40:32.842946Z","iopub.status.idle":"2024-07-13T12:40:43.943592Z","shell.execute_reply":"2024-07-13T12:40:43.941945Z","shell.execute_reply.started":"2024-07-13T12:40:32.843311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CV = 5\nBS = 64\nEPOCHS = 5\nLR = 5e-4\nSEED = 777\nfold = 1","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:43.946386Z","iopub.status.busy":"2024-07-13T12:40:43.946011Z","iopub.status.idle":"2024-07-13T12:40:43.952033Z","shell.execute_reply":"2024-07-13T12:40:43.950475Z","shell.execute_reply.started":"2024-07-13T12:40:43.946354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('C:/Users/Angel/kaggle/train.csv')\ntrain.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:43.953802Z","iopub.status.busy":"2024-07-13T12:40:43.953433Z","iopub.status.idle":"2024-07-13T12:40:44.027593Z","shell.execute_reply":"2024-07-13T12:40:44.026450Z","shell.execute_reply.started":"2024-07-13T12:40:43.953772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagnosis = list(filter(lambda x: x.find('foraminal') > -1, train.columns))\ntrain = train[train[diagnosis].isnull().values.sum(1)==0].reset_index(drop=True)\ntrain.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.029275Z","iopub.status.busy":"2024-07-13T12:40:44.028944Z","iopub.status.idle":"2024-07-13T12:40:44.062572Z","shell.execute_reply":"2024-07-13T12:40:44.061358Z","shell.execute_reply.started":"2024-07-13T12:40:44.029247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta_f = pd.read_csv('C:/Users/Angel/kaggle/train_series_descriptions.csv')\ndf_meta_f.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.066800Z","iopub.status.busy":"2024-07-13T12:40:44.065831Z","iopub.status.idle":"2024-07-13T12:40:44.087089Z","shell.execute_reply":"2024-07-13T12:40:44.085896Z","shell.execute_reply.started":"2024-07-13T12:40:44.066748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta_f['series_description'].groupby(df_meta_f['series_description']).count()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.088724Z","iopub.status.busy":"2024-07-13T12:40:44.088364Z","iopub.status.idle":"2024-07-13T12:40:44.103190Z","shell.execute_reply":"2024-07-13T12:40:44.101828Z","shell.execute_reply.started":"2024-07-13T12:40:44.088695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_coor = pd.read_csv('C:/Users/Angel/kaggle/train_label_coordinates.csv')\ndf_coor.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.106397Z","iopub.status.busy":"2024-07-13T12:40:44.104895Z","iopub.status.idle":"2024-07-13T12:40:44.245029Z","shell.execute_reply":"2024-07-13T12:40:44.242024Z","shell.execute_reply.started":"2024-07-13T12:40:44.106339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_coor['condition'].groupby(df_coor['condition']).count()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.248072Z","iopub.status.busy":"2024-07-13T12:40:44.246687Z","iopub.status.idle":"2024-07-13T12:40:44.271492Z","shell.execute_reply":"2024-07-13T12:40:44.267640Z","shell.execute_reply.started":"2024-07-13T12:40:44.248012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LF = df_coor[df_coor['condition']=='Left Neural Foraminal Narrowing'][[\n    'study_id',\n    'series_id',\n    'instance_number',\n    'level',\n    'x',\n    'y'\n]].sort_values([\n    'study_id',\n    'series_id',\n    'level'\n])[[\n    'study_id',\n    'series_id',\n    'level',\n    'instance_number',\n    'x',\n    'y'    \n]]\nLF.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.273789Z","iopub.status.busy":"2024-07-13T12:40:44.273050Z","iopub.status.idle":"2024-07-13T12:40:44.298919Z","shell.execute_reply":"2024-07-13T12:40:44.297652Z","shell.execute_reply.started":"2024-07-13T12:40:44.273742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(['L1/L2','L2/L3','L3/L4','L4/L5','L5/S1']*(len(LF)//5) == LF['level']).sum() == len(LF)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LF = LF[[\n    'study_id',\n    'series_id',\n    'instance_number',\n    'x',\n    'y'    \n]]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LF[[\n    'x_L1L2',\n    'y_L1L2',\n    'x_L2L3',\n    'y_L2L3',\n    'x_L3L4',\n    'y_L3L4',\n    'x_L4L5',\n    'y_L4L5',\n    'x_L5S1',\n    'y_L5S1',    \n]] = np.tile(LF[['x','y']].values.reshape(-1,1,5,2),(1,5,1,1)).reshape(-1,10)\nLF = LF.drop(columns=['x','y']).drop_duplicates().reset_index(drop=True)\nLF.tail()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagnosis = list(filter(lambda x: x.find('left_neural_foraminal') > -1, train.columns))\nLF = LF.merge(train[['study_id']+diagnosis], left_on='study_id', right_on='study_id')\nLF.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.300953Z","iopub.status.busy":"2024-07-13T12:40:44.300454Z","iopub.status.idle":"2024-07-13T12:40:44.333485Z","shell.execute_reply":"2024-07-13T12:40:44.331864Z","shell.execute_reply.started":"2024-07-13T12:40:44.300910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagnosis = {x:x[5:] for x in diagnosis}\ndiagnosis","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.335761Z","iopub.status.busy":"2024-07-13T12:40:44.335291Z","iopub.status.idle":"2024-07-13T12:40:44.348226Z","shell.execute_reply":"2024-07-13T12:40:44.346298Z","shell.execute_reply.started":"2024-07-13T12:40:44.335728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LF = LF.rename(columns=diagnosis)\nLF.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.351505Z","iopub.status.busy":"2024-07-13T12:40:44.350452Z","iopub.status.idle":"2024-07-13T12:40:44.377111Z","shell.execute_reply":"2024-07-13T12:40:44.375753Z","shell.execute_reply.started":"2024-07-13T12:40:44.351456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RF = df_coor[df_coor['condition']=='Right Neural Foraminal Narrowing'][[\n    'study_id',\n    'series_id',\n    'instance_number',\n    'level',\n    'x',\n    'y'\n]].sort_values([\n    'study_id',\n    'series_id',\n    'level'\n])[[\n    'study_id',\n    'series_id',\n    'instance_number',\n    'level',\n    'x',\n    'y'    \n]].drop_duplicates()\nRF.tail()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"centers = {}\nfor i in range(len(RF)):\n    row = RF.iloc[i]\n    centers[row['study_id']]={}\nfor i in range(len(RF)):\n    row = RF.iloc[i]\n    centers[row['study_id']][row['series_id']]={'L1/L2':[],'L2/L3':[],'L3/L4':[],'L4/L5':[],'L5/S1':[]}\nfor i in range(len(RF)):\n    row = RF.iloc[i]\n    centers[row['study_id']][row['series_id']][row['level']].append([row['x'],row['y']])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coordinates = np.zeros((len(RF),10))\ncoordinates[:] = np.nan\nfor i in range(len(RF)):\n    row = RF.iloc[i]\n    for level in centers[row['study_id']][row['series_id']]:\n        if len(centers[row['study_id']][row['series_id']][level]) > 0:\n            center = np.array(centers[row['study_id']][row['series_id']][level]).mean(0)\n            coordinates[\n                i,\n                {'L1/L2':0, 'L2/L3':2, 'L3/L4':4, 'L4/L5':6, 'L5/S1':8}[level]:{'L1/L2':0, 'L2/L3':2, 'L3/L4':4, 'L4/L5':6, 'L5/S1':8}[level]+2\n            ] = center","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RF = RF[[\n    'study_id',\n    'series_id',\n    'instance_number',\n    'x',\n    'y'    \n]]\nRF[[\n    'x_L1L2',\n    'y_L1L2',\n    'x_L2L3',\n    'y_L2L3',\n    'x_L3L4',\n    'y_L3L4',\n    'x_L4L5',\n    'y_L4L5',\n    'x_L5S1',\n    'y_L5S1',    \n]] = coordinates\nRF = RF.drop(columns=['x','y']).drop_duplicates().reset_index(drop=True)\nRF.tail()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RF = RF[RF[[\n    'x_L1L2',\n    'y_L1L2',\n    'x_L2L3',\n    'y_L2L3',\n    'x_L3L4',\n    'y_L3L4',\n    'x_L4L5',\n    'y_L4L5',\n    'x_L5S1',\n    'y_L5S1',    \n]].isnull().values.sum(1)==0].reset_index(drop=True)\nRF.tail()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagnosis = list(filter(lambda x: x.find('right_neural_foraminal') > -1, train.columns))\nRF = RF.merge(train[['study_id']+diagnosis], left_on='study_id', right_on='study_id')\nRF.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.418790Z","iopub.status.busy":"2024-07-13T12:40:44.417737Z","iopub.status.idle":"2024-07-13T12:40:44.442676Z","shell.execute_reply":"2024-07-13T12:40:44.440923Z","shell.execute_reply.started":"2024-07-13T12:40:44.418746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagnosis = {x:x[6:] for x in diagnosis}\ndiagnosis","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.444493Z","iopub.status.busy":"2024-07-13T12:40:44.443982Z","iopub.status.idle":"2024-07-13T12:40:44.453917Z","shell.execute_reply":"2024-07-13T12:40:44.451678Z","shell.execute_reply.started":"2024-07-13T12:40:44.444437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RF = RF.rename(columns=diagnosis)\nRF.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.455900Z","iopub.status.busy":"2024-07-13T12:40:44.455432Z","iopub.status.idle":"2024-07-13T12:40:44.474221Z","shell.execute_reply":"2024-07-13T12:40:44.472842Z","shell.execute_reply.started":"2024-07-13T12:40:44.455864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"F = pd.concat([LF,RF],axis=0,ignore_index=True)\nF.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.476368Z","iopub.status.busy":"2024-07-13T12:40:44.475721Z","iopub.status.idle":"2024-07-13T12:40:44.498951Z","shell.execute_reply":"2024-07-13T12:40:44.497665Z","shell.execute_reply.started":"2024-07-13T12:40:44.476335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"F = F.merge(df_meta_f[['series_id','series_description']], left_on='series_id', right_on='series_id')\nF.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.501516Z","iopub.status.busy":"2024-07-13T12:40:44.500803Z","iopub.status.idle":"2024-07-13T12:40:44.529545Z","shell.execute_reply":"2024-07-13T12:40:44.528233Z","shell.execute_reply.started":"2024-07-13T12:40:44.501425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for c in F.columns[-6:]:\n    print(F[c].groupby(F[c]).count())","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.531266Z","iopub.status.busy":"2024-07-13T12:40:44.530911Z","iopub.status.idle":"2024-07-13T12:40:44.552864Z","shell.execute_reply":"2024-07-13T12:40:44.551595Z","shell.execute_reply.started":"2024-07-13T12:40:44.531237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"v,c = np.unique(F['study_id'],return_counts=True)","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.554569Z","iopub.status.busy":"2024-07-13T12:40:44.554225Z","iopub.status.idle":"2024-07-13T12:40:44.561679Z","shell.execute_reply":"2024-07-13T12:40:44.559834Z","shell.execute_reply.started":"2024-07-13T12:40:44.554539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(v,c,'.')","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.563480Z","iopub.status.busy":"2024-07-13T12:40:44.563104Z","iopub.status.idle":"2024-07-13T12:40:44.862743Z","shell.execute_reply":"2024-07-13T12:40:44.861218Z","shell.execute_reply.started":"2024-07-13T12:40:44.563424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"L = len(v)\nS = L/CV\nfold_indices = list(np.rint(np.arange(CV)*S).astype(int))+[L]","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.864623Z","iopub.status.busy":"2024-07-13T12:40:44.864224Z","iopub.status.idle":"2024-07-13T12:40:44.871309Z","shell.execute_reply":"2024-07-13T12:40:44.869922Z","shell.execute_reply.started":"2024-07-13T12:40:44.864576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    print(len(v[fold_indices[i]:fold_indices[i+1]]))\n    F.loc[F['study_id'].isin(v[fold_indices[i]:fold_indices[i+1]]),'series_description'] = i+1\nF.tail()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.873863Z","iopub.status.busy":"2024-07-13T12:40:44.873354Z","iopub.status.idle":"2024-07-13T12:40:44.901121Z","shell.execute_reply":"2024-07-13T12:40:44.899720Z","shell.execute_reply.started":"2024-07-13T12:40:44.873792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for f in range(CV):\n    print('fold: ',f+1)\n    for c in F.columns[-6:]:\n        print(F[F['series_description']==f+1][c].groupby(F[c]).count())\n    print('\\n')","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:44.903179Z","iopub.status.busy":"2024-07-13T12:40:44.902798Z","iopub.status.idle":"2024-07-13T12:40:44.998407Z","shell.execute_reply":"2024-07-13T12:40:44.997191Z","shell.execute_reply.started":"2024-07-13T12:40:44.903150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = F.columns[-6:-1]\ntarget","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:45.000489Z","iopub.status.busy":"2024-07-13T12:40:45.000122Z","iopub.status.idle":"2024-07-13T12:40:45.008285Z","shell.execute_reply":"2024-07-13T12:40:45.007039Z","shell.execute_reply.started":"2024-07-13T12:40:45.000459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = {\n    'Normal/Mild':0,\n    'Moderate':1,\n    'Severe':2\n}","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:45.010243Z","iopub.status.busy":"2024-07-13T12:40:45.009833Z","iopub.status.idle":"2024-07-13T12:40:45.020980Z","shell.execute_reply":"2024-07-13T12:40:45.019755Z","shell.execute_reply.started":"2024-07-13T12:40:45.010210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Gemini\ndef augment_image(image):\n  # Randomly flip the image horizontally.\n  if random.random() > 0.5:\n    image = cv2.flip(image, 1)\n  # Randomly flip the image vertically.\n  if random.random() > 0.5:\n    image = cv2.flip(image, 0)\n\n  # Randomly rotate the image.\n  angle = random.uniform(-45, 45)\n  (h, w) = image.shape[:2]\n  center = (w // 2, h // 2)\n  M = cv2.getRotationMatrix2D(center, angle, 1.0)\n  image = cv2.warpAffine(image, M, (w, h))\n\n  return image","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class T1Dataset(Dataset):\n    def __init__(self, df, VALID=False):\n        self.data = df\n        self.VALID = VALID\n\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, index):\n        row = self.data.iloc[index]\n        \n        sample = 'C:/Users/Angel/kaggle/train/'\n        sample = sample+str(row['study_id'])+'/'+str(row['series_id'])+'/'+str(row['instance_number'])+'.dcm'\n        \n        image = cv2.resize(pydicom.dcmread(sample).pixel_array,(256,256))\n#       if not self.VALID: image = augment_image(image)\n        image = torch.as_tensor(image/np.max(image)).unsqueeze(0).float()\n        \n        label = torch.as_tensor([labels[x] for x in row[target]])\n\n        return image.to(device),label.to(device)","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:45.022874Z","iopub.status.busy":"2024-07-13T12:40:45.022481Z","iopub.status.idle":"2024-07-13T12:40:45.035948Z","shell.execute_reply":"2024-07-13T12:40:45.034076Z","shell.execute_reply.started":"2024-07-13T12:40:45.022843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:45.038155Z","iopub.status.busy":"2024-07-13T12:40:45.037737Z","iopub.status.idle":"2024-07-13T12:40:45.052053Z","shell.execute_reply":"2024-07-13T12:40:45.050755Z","shell.execute_reply.started":"2024-07-13T12:40:45.038125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def myLoss(y_pred,y_true):\n    y_pred = nn.Softmax(-1)(y_pred.view(-1,5,3))\n    Loss = 0\n    for j in range(5):\n                Loss += nn.CrossEntropyLoss()(y_pred[:,j],y_true[:,j])\n    return Loss/5","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:46.867255Z","iopub.status.busy":"2024-07-13T12:40:46.866913Z","iopub.status.idle":"2024-07-13T12:40:46.873416Z","shell.execute_reply":"2024-07-13T12:40:46.872192Z","shell.execute_reply.started":"2024-07-13T12:40:46.867226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def acc(y_pred,y_true):\n    y_pred = torch.argmax(y_pred.view(-1,3),-1)\n    y_true = y_true.flatten()\n    return (y_pred == y_true).sum().item()*100/len(y_pred)\ndef L1L2(y_pred,y_true):\n    y_pred = torch.argmax(y_pred.view(-1,5,3)[:,0],-1)\n    y_true = y_true[:,0]\n    return (y_pred == y_true).sum().item()*100/len(y_pred)\ndef L2L3(y_pred,y_true):\n    y_pred = torch.argmax(y_pred.view(-1,5,3)[:,1],-1)\n    y_true = y_true[:,1]\n    return (y_pred == y_true).sum().item()*100/len(y_pred)\ndef L3L4(y_pred,y_true):\n    y_pred = torch.argmax(y_pred.view(-1,5,3)[:,2],-1)\n    y_true = y_true[:,2]\n    return (y_pred == y_true).sum().item()*100/len(y_pred)\ndef L4L5(y_pred,y_true):\n    y_pred = torch.argmax(y_pred.view(-1,5,3)[:,3],-1)\n    y_true = y_true[:,3]\n    return (y_pred == y_true).sum().item()*100/len(y_pred)\ndef L5S1(y_pred,y_true):\n    y_pred = torch.argmax(y_pred.view(-1,5,3)[:,4],-1)\n    y_true = y_true[:,4]\n    return (y_pred == y_true).sum().item()*100/len(y_pred)","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:46.875596Z","iopub.status.busy":"2024-07-13T12:40:46.875106Z","iopub.status.idle":"2024-07-13T12:40:46.888038Z","shell.execute_reply":"2024-07-13T12:40:46.886129Z","shell.execute_reply.started":"2024-07-13T12:40:46.875555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for fold in [1,2,3,4,5]:\n    seed_everything(SEED)\n\n    tdf = F[F['series_description'] != fold]\n    vdf = F[F['series_description'] == fold]\n\n    for t in target:\n        total = len(vdf)\n        n_normal = (vdf[t] == 'Normal/Mild').sum()\n        n_moderate = (vdf[t] == 'Moderate').sum()\n        n_severe = (vdf[t] == 'Severe').sum()\n        print(t)\n        print(n_normal*100/total,'%, ',n_moderate*100/total,'%, ',n_severe*100/total,'%')\n    \n    tds = T1Dataset(tdf)\n    vds = T1Dataset(vdf,VALID=False)\n    tdl = torch.utils.data.DataLoader(tds, batch_size=BS, shuffle=True, drop_last=True)\n    vdl = torch.utils.data.DataLoader(vds, batch_size=BS, shuffle=False)\n\n    CNN = torchvision.models.resnet18(weights='DEFAULT')\n    W = nn.Parameter(CNN.conv1.weight.sum(1).unsqueeze(1))\n    CNN.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n    CNN.conv1.weight = W\n    CNN.fc = nn.Linear(in_features=512, out_features=5*3, bias=True)\n    CNN = CNN.to(device)\n\n    dls = DataLoaders(tdl,vdl)\n\n    learn = Learner(\n        dls,\n        CNN,\n        lr=LR,\n        loss_func=myLoss,\n        metrics=[\n            acc,\n            L1L2,\n            L2L3,\n            L3L4,\n            L4L5,\n            L5S1\n        ],\n        cbs=[\n            ShowGraphCallback()\n        ]\n    )\n\n    learn.fit_one_cycle(EPOCHS)\n    del tds,tdl,vds,vdl,CNN,W,dls,learn\n    gc.collect()","metadata":{"execution":{"iopub.execute_input":"2024-07-13T12:40:46.890640Z","iopub.status.busy":"2024-07-13T12:40:46.890128Z","iopub.status.idle":"2024-07-13T14:32:40.080628Z","shell.execute_reply":"2024-07-13T14:32:40.078796Z","shell.execute_reply.started":"2024-07-13T12:40:46.890580Z"},"trusted":true},"execution_count":null,"outputs":[]}]}