{"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":"!conda install '../input/covidnew/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -c conda-forge -y\n!conda install '../input/covidnew/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -c conda-forge -y\n!conda install '../input/covidnew/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n!conda install '../input/covidnew/conda-4.10.1-py37h89c1867_0.tar.bz2' -c conda-forge -y\n!conda install '../input/covidnew/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -c conda-forge -y\n!conda install '../input/covidnew/openssl-1.1.1k-h7f98852_0.tar.bz2' -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2021-06-19T09:58:59.374177Z","iopub.execute_input":"2021-06-19T09:58:59.374550Z","iopub.status.idle":"2021-06-19T10:00:07.841625Z","shell.execute_reply.started":"2021-06-19T09:58:59.374457Z","shell.execute_reply":"2021-06-19T10:00:07.840723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch import Tensor\nfrom torch.utils.data import Dataset\nfrom torchvision import transforms, models\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data import random_split\nfrom torch.nn import Linear\nfrom torch.nn import ReLU\nfrom torch.nn import Softmax\nfrom torch.nn import Module\nfrom torch.optim import SGD\nfrom torch.nn import CrossEntropyLoss\nimport numpy as np\nimport torch\nfrom torch import nn\nfrom torch import optim\nfrom numpy import asarray\nimport os\n\nfrom PIL import Image\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2021-06-19T10:00:07.845381Z","iopub.execute_input":"2021-06-19T10:00:07.845785Z","iopub.status.idle":"2021-06-19T10:00:09.456006Z","shell.execute_reply.started":"2021-06-19T10:00:07.845744Z","shell.execute_reply":"2021-06-19T10:00:09.455178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport numpy as np\n\ndef read_xray(path, voi_lut = True, fix_monochrome = True):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n        \n    return data","metadata":{"execution":{"iopub.status.busy":"2021-06-19T10:00:09.457427Z","iopub.execute_input":"2021-06-19T10:00:09.457762Z","iopub.status.idle":"2021-06-19T10:00:09.703950Z","shell.execute_reply.started":"2021-06-19T10:00:09.457727Z","shell.execute_reply":"2021-06-19T10:00:09.703186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize(array, size, keep_ratio=False, resample=Image.LANCZOS):\n    # Original from: https://www.kaggle.com/xhlulu/vinbigdata-process-and-resize-to-image\n    im = Image.fromarray(array)\n    \n    if keep_ratio:\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    \n    return im","metadata":{"execution":{"iopub.status.busy":"2021-06-19T10:00:09.705299Z","iopub.execute_input":"2021-06-19T10:00:09.705675Z","iopub.status.idle":"2021-06-19T10:00:09.710830Z","shell.execute_reply.started":"2021-06-19T10:00:09.705640Z","shell.execute_reply":"2021-06-19T10:00:09.709872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel=torch.load('../input/covidnew/model.pth')\nmodel.eval()\ncol=['id','PredictionString']\ntest_transforms = transforms.Compose([transforms.ToTensor(),\n                                     ])\nprediction=pd.DataFrame(columns = col) \nfor dirname, _, filenames in os.walk('/kaggle/input/siim-covid19-detection/test'):\n    for filename in filenames:\n        img_path= os.path.join(dirname, filename)\n        id_=img_path.split('/')[5]+\"_study\"\n        image=read_xray(img_path)\n        image=resize(image, 416)\n        image=asarray(image)\n        image = torch.from_numpy(image).long()\n        image=np.stack((image,)*3, axis=0)\n        image_tensor = test_transforms(image).float()\n        image_tensor = image_tensor.unsqueeze_(0)\n        #inputs = Variable(image_tensor)\n        inputs = image_tensor.to(device)\n        inputs=torch.transpose(inputs, 1, 2)\n        output = model(inputs)\n        predProb = output.detach().cpu().numpy()[0]\n        PredictionString=f'negative {format(predProb[0],\".8f\")} 0 0 1 1 typical {format(predProb[1],\".5f\")} 0 0 1 1 indeterminate {format(predProb[2],\".5f\")} 0 0 1 1 atypical {format(predProb[3],\".5f\")} 0 0 1 1' \n        #print(PredictionString)\n        prediction=prediction.append({\"id\":id_,\"PredictionString\":PredictionString},ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T10:00:09.713244Z","iopub.execute_input":"2021-06-19T10:00:09.713659Z","iopub.status.idle":"2021-06-19T10:02:54.565701Z","shell.execute_reply.started":"2021-06-19T10:00:09.713621Z","shell.execute_reply":"2021-06-19T10:02:54.564235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def CreateSub(test):\n    sub_df = pd.read_csv('../input/siim-covid19-detection/sample_submission.csv')\n    for i in range(len(sub_df)):\n        #print(test[test['id']==sub_df.loc[i,'id']])\n        #negative, typical, indeterminate, atypical = str(tst_preds[i][0]),str(tst_preds[i][1]),str(tst_preds[i][2]),str(tst_preds[i][3]),\n        try:\n            sub_df.loc[i,'PredictionString'] =test[test['id']==sub_df.loc[i,'id']].iloc[0,1]# test[test['id']==sub_df.loc[i,'id']][0][1]#f'negative {negative} 0 0 1 1 typical {typical} 0 0 1 1 indeterminate {indeterminate} 0 0 1 1 atypical {atypical} 0 0 1 1'\n        except:\n            continue\n    return sub_df","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sumfile = CreateSub(prediction)\nsumfile.to_csv('./submission.csv',index=False)","metadata":{},"execution_count":null,"outputs":[]}]}