{"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":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport pydicom\nimport os\nimport gc\nfrom tqdm import tqdm\nimport time\nimport random","metadata":{"execution":{"iopub.status.busy":"2023-02-13T05:45:07.536017Z","iopub.execute_input":"2023-02-13T05:45:07.537013Z","iopub.status.idle":"2023-02-13T05:45:07.726364Z","shell.execute_reply.started":"2023-02-13T05:45:07.53691Z","shell.execute_reply":"2023-02-13T05:45:07.724668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.optim as optim\nimport torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2023-02-13T05:45:07.728878Z","iopub.execute_input":"2023-02-13T05:45:07.72939Z","iopub.status.idle":"2023-02-13T05:45:09.91523Z","shell.execute_reply.started":"2023-02-13T05:45:07.729339Z","shell.execute_reply":"2023-02-13T05:45:09.913404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-02-13T05:45:09.917024Z","iopub.execute_input":"2023-02-13T05:45:09.91878Z","iopub.status.idle":"2023-02-13T05:45:09.926976Z","shell.execute_reply.started":"2023-02-13T05:45:09.918726Z","shell.execute_reply":"2023-02-13T05:45:09.925402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Extract and return list of data packs**","metadata":{}},{"cell_type":"code","source":"def mid(s, offset, amount):\n    return s[offset:offset+amount]","metadata":{"execution":{"iopub.status.busy":"2023-02-13T05:45:09.930686Z","iopub.execute_input":"2023-02-13T05:45:09.932288Z","iopub.status.idle":"2023-02-13T05:45:09.941391Z","shell.execute_reply.started":"2023-02-13T05:45:09.932229Z","shell.execute_reply":"2023-02-13T05:45:09.939658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def datainpath(path):\n    files=os.listdir(path)\n    datas=list()\n    imgs=list()\n    avail=list()\n    for file in files:\n        if (mid(file,len(file)-3,3))=='.pt' :\n            if (mid(file,0,6))=='cc_img' :\n                imgs.append(mid(file,7,len(file)-10))\n            elif (mid(file,0,7))=='cc_data':\n                datas.append(mid(file,8,len(file)-11))\n    for item in imgs:\n        if item in datas :\n            avail.append(['%scc_img_%s.pt' %(path,item),'%scc_data_%s.pt' %(path,item),])\n    return avail","metadata":{"execution":{"iopub.status.busy":"2023-02-13T05:45:09.943934Z","iopub.execute_input":"2023-02-13T05:45:09.945335Z","iopub.status.idle":"2023-02-13T05:45:09.957813Z","shell.execute_reply.started":"2023-02-13T05:45:09.945271Z","shell.execute_reply":"2023-02-13T05:45:09.956425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Loader**","metadata":{}},{"cell_type":"code","source":"def data_loader_offline(data_packs):\n    \n    batch_size_big=256\n    batch_size_small=4\n\n    for i in range(0,len(data_packs)):\n        x=torch.load(data_packs[i][0])\n        y=torch.load(data_packs[i][1])\n        for k in range(0,batch_size_big,batch_size_small):\n            if (k+batch_size_small)>batch_size_big :\n                break\n            newx=x[k:k+batch_size_small]\n            newy=y[k:k+batch_size_small]\n            newx=torch.cat([newx, newx, newx], dim=1)\n            yield torch.as_tensor(newx,dtype=torch.float) , torch.as_tensor(newy,dtype=torch.float)\n        del x,y\n        gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-13T05:45:09.95972Z","iopub.execute_input":"2023-02-13T05:45:09.960604Z","iopub.status.idle":"2023-02-13T05:45:09.973141Z","shell.execute_reply.started":"2023-02-13T05:45:09.960551Z","shell.execute_reply":"2023-02-13T05:45:09.971075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Load model and data**","metadata":{}},{"cell_type":"code","source":"device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\nmodel=torch.load('/kaggle/input/newmodel-16384/model_e7.pt',map_location=device)\nprint(device)\ncriterion = nn.BCEWithLogitsLoss()\n#optimizer = optim.AdamW(model.parameters(), lr=0.00000001)\noptimizer = optim.Adam(model.parameters())","metadata":{"execution":{"iopub.status.busy":"2023-02-13T05:45:09.976019Z","iopub.execute_input":"2023-02-13T05:45:09.977171Z","iopub.status.idle":"2023-02-13T05:45:13.599795Z","shell.execute_reply.started":"2023-02-13T05:45:09.976913Z","shell.execute_reply":"2023-02-13T05:45:13.598633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_folders=['/kaggle/input/rsna-data-2/','/kaggle/input/rsna-data-1/','/kaggle/input/rsna-data-4/','/kaggle/input/rsna-data-3/']\npaths=list()\ntry :\n    for main_folder in main_folders:\n        for item in os.listdir(main_folder):\n            path=main_folder+item+'/'\n            paths.append(path)\nexcept KeyError as e:\n        print(e)\nprint(paths)\nrandom.shuffle(paths)\nprint(paths)","metadata":{"execution":{"iopub.status.busy":"2023-02-13T05:45:13.601092Z","iopub.execute_input":"2023-02-13T05:45:13.602258Z","iopub.status.idle":"2023-02-13T05:45:13.662593Z","shell.execute_reply.started":"2023-02-13T05:45:13.602201Z","shell.execute_reply":"2023-02-13T05:45:13.66112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Run**","metadata":{}},{"cell_type":"code","source":"t1=time.perf_counter()\nfor path in paths :\n    data_packs=datainpath(path)\n    print(path,' : ',len(data_packs))\n    random.shuffle(data_packs)\n    gen=data_loader_offline(data_packs)\n    try : \n        t1=time.perf_counter()\n        epoch=1\n        running_loss=0\n        #for i in tqdm(range(0,len(data_packs)*64)):\n        for i in range(0,len(data_packs)*64):\n            gen_res=next(gen)\n            inputs,labels=gen_res[0].to(device),gen_res[1].to(device)\n            del gen_res\n            optimizer.zero_grad()\n            outputs=torch.squeeze(model(inputs))\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n            running_loss += loss.item()\n            if i%64==0 and i!=0 :\n                print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 64:.3f}')\n                print('time = ',str(time.perf_counter()-t1))\n                print('Example : ',(outputs,labels))\n                running_loss = 0.0\n            del inputs,outputs,labels\n            gc.collect()\n            if torch.cuda.is_available() : torch.cuda.empty_cache()\n    except KeyError as e:\n        print(e)\nprint('Finished Training')","metadata":{"execution":{"iopub.status.busy":"2023-02-11T05:31:23.965698Z","iopub.execute_input":"2023-02-11T05:31:23.966181Z","iopub.status.idle":"2023-02-11T06:01:34.044494Z","shell.execute_reply.started":"2023-02-11T05:31:23.966141Z","shell.execute_reply":"2023-02-11T06:01:34.04258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model,'model_e%i.pt' %(8))","metadata":{"execution":{"iopub.status.busy":"2023-02-11T06:07:17.206619Z","iopub.execute_input":"2023-02-11T06:07:17.207017Z","iopub.status.idle":"2023-02-11T06:07:17.477284Z","shell.execute_reply.started":"2023-02-11T06:07:17.206962Z","shell.execute_reply":"2023-02-11T06:07:17.476309Z"},"trusted":true},"execution_count":null,"outputs":[]}]}