{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# !conda install -c conda-forge gdcm -y -q\n!pip install ../input/pytorchcv/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport re\nimport cv2\nfrom tqdm import tqdm\n\nimport pydicom\nfrom sklearn.cluster import KMeans\nfrom skimage import morphology, measure\nfrom scipy.ndimage.interpolation import zoom\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.cuda import amp\n\nimport warnings\n\nimport random\ndef seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n#     torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = True\n\nseed_everything(42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def crop(image):\n    mid_img = image[int(image.shape[0] / 2)]\n\n    same_first_row = (mid_img[0, :] == mid_img[0, 0]).all()\n    same_first_col = (mid_img[:, 0] == mid_img[0, 0]).all()\n    if same_first_col and same_first_row:\n        pass\n    else:\n        return image\n    \n    r_min, r_max = None, None\n    c_min, c_max = None, None\n    for row in range(mid_img.shape[0]):\n        if not (mid_img[row, :] == mid_img[0, 0]).all() and r_min is None:\n            r_min = row\n        if (mid_img[row, :] == mid_img[0, 0]).all() and r_max is None \\\n                and r_min is not None:\n            r_max = row\n            break\n\n    for col in range(mid_img.shape[1]):\n        if not (mid_img[:, col] == mid_img[0, 0]).all() and c_min is None:\n            c_min = col\n        if (mid_img[:, col] == mid_img[0, 0]).all() and c_max is None \\\n                and c_min is not None:\n            c_max = col\n            break\n#     print(r_min, r_max, c_min, c_max)\n    image = image[:, r_min:r_max, c_min:c_max]\n    return image\n\ndef resize(image, shape=(40,256,256)):\n    resize_factor = np.array(shape) / np.array(image.shape)\n    image = zoom(image, resize_factor, mode='nearest')\n    return image\n\ndef window(img, WL=50, WW=350):\n    upper, lower = WL+WW//2, WL-WW//2\n    X = np.clip(img.copy(), lower, upper)\n    X = X - np.min(X)\n    X = X / np.max(X)\n    X = (X*255.0).astype('uint8')\n    return X","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import glob\nimport re\n\ndef load_scan(path):\n    paths = glob.glob(path+'/*.dcm')\n    paths.sort(key=lambda f: int(re.sub('\\D', '', f)))\n    if len(paths) < 51:\n        paths = paths[:50]\n#         paths = paths[10:-10]\n    slices = [pydicom.read_file(p) for p in paths]\n    try:\n        slices.sort(key=lambda x: float(x.ImagePositionPatient[2]))\n    except:\n        pass\n    \n    image = np.stack([s.pixel_array.astype(float) for s in slices])\n    return image, slices[0], slices","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IN_TRAIN = '../input/osic-pulmonary-fibrosis-progression/test/'\npaths = os.listdir(IN_TRAIN)\npaths.sort()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/osic-pulmonary-fibrosis-progression/train.csv')\ntrain.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])\nsub = pd.read_csv('../input/osic-pulmonary-fibrosis-progression/sample_submission.csv')\ntest = pd.read_csv('../input/osic-pulmonary-fibrosis-progression/test.csv')\n\nsub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])\nsub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))\nsub =  sub[['Patient','Weeks','Confidence','Patient_Week']]\nsub = sub.merge(test.drop('Weeks', axis=1), on=\"Patient\")\n# sub.head()\n\nsub['WHERE'] = 'test'\ntest['WHERE'] = 'sub'\ntrain['WHERE'] = 'train'\n\ndata = train.append([test, sub])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# data = sub.copy()\ndata['min_week'] = data['Weeks']\ndata.loc[data.WHERE=='test','min_week'] = np.nan\ndata['min_week'] = data.groupby('Patient')['min_week'].transform('min')\n\nbase = data.loc[data.Weeks == data.min_week]\nbase = base[['Patient','FVC']].copy()\nbase.columns = ['Patient','min_FVC']\nbase['nb'] = 1\nbase['nb'] = base.groupby('Patient')['nb'].transform('cumsum')\nbase = base[base.nb==1]\nbase.drop('nb', axis=1, inplace=True)\n\ndata = data.merge(base, on='Patient', how='left')\ndata['base_week'] = data['Weeks'] - data['min_week']\ndel base\n\ndata['age'] = (data['Age'] - data['Age'].min() ) / ( data['Age'].max() - data['Age'].min() )\ndata['BASE'] = (data['min_FVC'] - data['min_FVC'].min() ) / ( data['min_FVC'].max() - data['min_FVC'].min() )\ndata['week'] = (data['base_week'] - data['base_week'].min() ) / ( data['base_week'].max() - data['base_week'].min() )\ndata['percent'] = (data['Percent'] - data['Percent'].min() ) / ( data['Percent'].max() - data['Percent'].min() )\n\nCOLS = ['Sex','SmokingStatus'] #,'Age'\nFE = []\nFE += ['Patient']\nfor col in COLS:\n    for mod in data[col].unique():\n        FE.append(mod)\n        data[mod] = (data[col] == mod).astype(int)\nFE += ['age','percent','week','BASE', 'WHERE']\n\nmeta_df = data[FE]\nmeta_df['fold'] = 0\nmeta_df = meta_df[meta_df.WHERE=='test']\n# meta_df = meta_df[meta_df.WHERE=='train']\nmeta_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def get_tab(df):\n    vector = [(df.Age.values[0] - 30) / 30] \n    \n    if df.Sex.values[0] == 'male':\n       vector.append(0)\n    else:\n       vector.append(1)\n    \n    if df.SmokingStatus.values[0] == 'Never smoked':\n        vector.extend([0,0])\n    elif df.SmokingStatus.values[0] == 'Ex-smoker':\n        vector.extend([1,1])\n    elif df.SmokingStatus.values[0] == 'Currently smokes':\n        vector.extend([0,1])\n    else:\n        vector.extend([1,0])\n    return np.array(vector) \n\ntargets = []\ntab = []\nP = []\nfor i, p in enumerate(test.Patient.unique()):\n    sub = test.loc[test.Patient == p, :] \n    \n    tab.append(get_tab(sub))\n\npatients = pd.DataFrame({'Patient': test.Patient.unique(), 'target': 0, 'meta': tab, 'fold': 0})\npatients.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['fold'] = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"from pytorchcv.model_provider import get_model\n\nclass FCN(torch.nn.Module):\n  def __init__(self, base, in_f, num_classes, in_meta, dropout=True):\n    super(FCN, self).__init__()\n    self.base = base\n    self.after_model = nn.Sequential(\n        nn.Flatten(),\n        nn.Dropout(0.5)\n    )\n    self.meta = nn.Sequential(\n        nn.Linear(in_meta, 100),\n        # nn.BatchNorm1d(100),\n        nn.ReLU(),\n        # nn.Dropout(0.8),\n        nn.Linear(100, 100),\n        # nn.BatchNorm1d(100),\n        nn.ReLU(),\n        # nn.Dropout(0.5)\n\n#         nn.Linear(in_meta, 1024),\n#         # nn.BatchNorm1d(1024),\n#         nn.ReLU(),\n#         # nn.Dropout(0.8),\n#         nn.Linear(1024, 512),\n#         # nn.BatchNorm1d(512),\n#         nn.ReLU(),\n#         # nn.Dropout(0.5)\n    )\n    self.classification_meta = nn.Sequential(\n        # nn.Linear(in_f+100, 1024),\n        # nn.BatchNorm1d(1024),\n        # nn.ReLU(),\n        # nn.Dropout(0.5),\n        # nn.Linear(1024, num_classes)\n        nn.Linear(in_f+100, num_classes)\n#         nn.Linear(in_f+512, num_classes)\n    )\n    self.classification = nn.Sequential(\n        # nn.Linear(in_f+100, 1024),\n        # nn.BatchNorm1d(1024),\n        # nn.ReLU(),\n        # nn.Dropout(0.5),\n        # nn.Linear(1024, num_classes)\n        nn.Linear(in_f, num_classes)\n    )\n    self.meta_head = nn.Linear(100, num_classes)\n  \n  def forward(self, x, meta):\n    x = self.base(x)\n    x = self.after_model(x)\n    meta = self.meta(meta)\n    features = torch.cat((x,meta),dim=1)\n    x = self.classification_meta(features)\n    # x = self.classification(x)\n    return x\n\ndef create_model(name, path=None):\n    model = get_model(name, pretrained=False)\n    \n    try:\n      features = list(model.children())[-1].in_features\n    except:\n      features = list(model.children())[-1][-1].in_features\n    model = nn.Sequential(*list(model.children())[:-1]) # Remove original output layer\n    model[0].final_pool = nn.Sequential(nn.AdaptiveAvgPool2d(1))\n    # model[-1] = nn.Sequential(nn.AdaptiveAvgPool2d(1))\n    model = FCN(model, features, config.n_seg_classes, config.meta_features, dropout=True)\n\n    if path:\n      print ('loading pretrained model {}'.format(path))\n      pretrain = torch.load(path)['model_state']\n      model.load_state_dict(pretrain)\n\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from pytorchcv.model_provider import get_model\n\nclass FCN2(torch.nn.Module):\n  def __init__(self, base, in_f, num_classes, in_meta, dropout=True):\n    super(FCN2, self).__init__()\n    self.base = base\n    self.after_model = nn.Sequential(\n        nn.Flatten(),\n        nn.Dropout(0.5)\n    )\n    self.meta = nn.Sequential(\n#         nn.Linear(in_meta, 100),\n#         # nn.BatchNorm1d(100),\n#         nn.ReLU(),\n#         # nn.Dropout(0.8),\n#         nn.Linear(100, 100),\n#         # nn.BatchNorm1d(100),\n#         nn.ReLU(),\n#         # nn.Dropout(0.5)\n\n        nn.Linear(in_meta, 1024),\n        # nn.BatchNorm1d(1024),\n        nn.ReLU(),\n        # nn.Dropout(0.8),\n        nn.Linear(1024, 512),\n        # nn.BatchNorm1d(512),\n        nn.ReLU(),\n        # nn.Dropout(0.5)\n    )\n    self.classification_meta = nn.Sequential(\n        # nn.Linear(in_f+100, 1024),\n        # nn.BatchNorm1d(1024),\n        # nn.ReLU(),\n        # nn.Dropout(0.5),\n        # nn.Linear(1024, num_classes)\n#         nn.Linear(in_f+100, num_classes)\n        nn.Linear(in_f+512, num_classes)\n    )\n    self.classification = nn.Sequential(\n        # nn.Linear(in_f+100, 1024),\n        # nn.BatchNorm1d(1024),\n        # nn.ReLU(),\n        # nn.Dropout(0.5),\n        # nn.Linear(1024, num_classes)\n        nn.Linear(in_f, num_classes)\n    )\n    self.meta_head = nn.Linear(100, num_classes)\n  \n  def forward(self, x, meta):\n    x = self.base(x)\n    x = self.after_model(x)\n    meta = self.meta(meta)\n    features = torch.cat((x,meta),dim=1)\n    x = self.classification_meta(features)\n    # x = self.classification(x)\n    return x\n\ndef create_model2(name, path=None):\n    model = get_model(name, pretrained=False)\n    \n    try:\n      features = list(model.children())[-1].in_features\n    except:\n      features = list(model.children())[-1][-1].in_features\n    model = nn.Sequential(*list(model.children())[:-1]) # Remove original output layer\n    model[0].final_pool = nn.Sequential(nn.AdaptiveAvgPool2d(1))\n    # model[-1] = nn.Sequential(nn.AdaptiveAvgPool2d(1))\n    model = FCN2(model, features, config.n_seg_classes, config.meta_features, dropout=True)\n\n    if path:\n      print ('loading pretrained model {}'.format(path))\n      pretrain = torch.load(path)['model_state']\n      model.load_state_dict(pretrain)\n\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class config:\n    input_D = 30\n    input_H = 512\n    input_W = 512\n    n_seg_classes = 3 # previously 1\n#     meta_features = 4\n    meta_features = 9\n#     meta_features = 6\n    seed = 42\n    quantiles = (0.2, 0.5, 0.8)\n\nnets=[]\n\n# 6.88\nmodel_name = 'resnet50'\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld1-2 (2).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld2-2 (2).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld3-2 (2).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld4-2 (2).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld5-2 (2).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\n# 6.890\nmodel_name = 'resnet50'\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld1-2 (4).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld2-2 (4).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld3-2 (4).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld4-2 (4).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld5-2 (4).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\n# 6.9048\nmodel_name = 'resnet50'\nmodel = create_model2(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld1-2 (5).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model2(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld2-2 (5).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model2(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld3-2 (5).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model2(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld4-2 (5).pth') # 6.885\nmodel.cuda()\nnets.append(model)\n\nmodel = create_model2(model_name, \n    path = '../input/osic-5fold-models2/resnet3d-fld5-2 (5).pth') # 6.885\nmodel.cuda()\nnets.append(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# err","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import albumentations as A\n\ntransform = A.Compose([\n    A.HorizontalFlip(p=1.0)\n])\n\ntransform2 = A.Compose([\n    A.VerticalFlip(p=1.0)\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def convertToHU(image, data):\n#     intercept = data.RescaleIntercept\n#     slope = data.RescaleSlope\n#     image = (image * slope + intercept).astype(np.int16)\n    image = (image + data.RescaleIntercept) / (data.RescaleSlope)\n    return image\n\nA_test, B_test, P_test, WEEK = {},{},{},{}\nlast_pat = ''\no1=[]\nmodel.eval()\nwith torch.no_grad():\n    t = tqdm(meta_df.Patient.values)\n    for i,patient in enumerate(t):\n        meta_batch = torch.from_numpy(meta_df.values[i][1:-2][None].astype(np.float32)).cuda().float()\n        if patient != last_pat:\n            last_pat = patient\n\n            patient_dir = '../input/osic-pulmonary-fibrosis-progression/test/' + patient\n            imgs, meta, slices = load_scan(patient_dir)\n\n            for i,img in enumerate(imgs):\n                imgs[i] = convertToHU(img, meta)\n            imgs = crop(imgs)\n            imgs = resize(imgs, shape=(50,512,512))\n            imgs = imgs[10:40]\n            \n            imgs1,imgs2,imgs3 = np.zeros((30,512,512,1)),np.zeros((30,512,512,1)),np.zeros((30,512,512,1))\n            for i,img in enumerate(imgs):\n                imgs1[i] = window(img, -600, 1500)[:,:,None]\n                imgs2[i] = window(img, 100, 700)[:,:,None]\n                imgs3[i] = window(img, 40, 400)[:,:,None]\n\n            imgs_ = np.concatenate([imgs1, imgs2, imgs3], axis=-1)\n            image_batch = []\n            for i in range(1):\n#                 imgs = imgs_[14+i]\n                imgs = imgs_[15]\n                imgs = np.rollaxis(imgs, -1, 0) / 255.\n#                 imgs = imgs[::-1]\n#                 imgs = imgs[None].copy()\n                image_batch.append(imgs)\n            # tta\n#             for img in image_batch:\n#                 image_batch.append(transform(image=img)['image'])\n#                 image_batch.append(transform2(image=img)['image'])\n                break\n            image_batch = np.array(image_batch)\n#             image_batch = torch.from_numpy(image_batch).cuda().float()\n\n        p = p[0]\n        \n        o=[]\n        for model in nets:\n            for img in image_batch:\n                img = img[None]\n                img = torch.from_numpy(img).cuda().float()\n                o.append(model(img, meta_batch).cpu().numpy())\n#         o1=np.mean(np.array(o))\n        for o in np.mean(np.array(o), axis=0):\n            o1.append(o)\n        \no1=np.array(o1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = data[data.WHERE=='test'].copy()\nsub['FVC'] = o1[:, 1]\nsub['Confidence'] = o1[:, 2] - o1[:, 0]\nsubm = sub[['Patient_Week','FVC','Confidence']].copy()\nsubm.head()\n# sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"otest = pd.read_csv('../input/osic-pulmonary-fibrosis-progression/test.csv')\nfor i in range(len(otest)):\n    subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'FVC'] = otest.FVC[i]\n    subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'Confidence'] = 0.1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = subm.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# sub[[\"Patient_Week\",\"FVC\",\"Confidence\"]].to_csv(\"submission.csv\", index=False)\nsub.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}