{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.metrics import *","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"sys.path.insert(0,'../input/dm-nfnet')\nimport timm\n\n\nclass NFNet(nn.Module):\n    def __init__(self, num_classes=11, model_nr=3, pretrained=True):\n        super(NFNet, self).__init__()\n        self.model = timm.create_model(f'dm_nfnet_f{model_nr}', pretrained=pretrained)\n        self.model.head.fc = nn.Linear(self.model.head.fc.in_features, num_classes)\n        \n    def forward(self, x):\n        x = self.model(x)\n        return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset_path = Path('../input/ranzcr-clip-catheter-line-classification')\ntrain_df = pd.read_csv(dataset_path/'train.csv')\ntrain_df['path'] = train_df['StudyInstanceUID'].map(lambda x:str(dataset_path/'train'/x)+'.jpg')\ntrain_df = train_df.drop(columns=['StudyInstanceUID'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Tune model_nr and data augmentation. Batch size and image size are automatically adapted to the capacity of the Kaggle GPU"},{"metadata":{"trusted":true},"cell_type":"code","source":"img_sizes = [192, 224, 256, 320, 384, 416, 448]\nmodel_nr = 0 # Between 0 and 5\npartial_data = .05\nsize = img_sizes[model_nr]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"For test runs use partial_data by uncommenting the commented line and commenting the line above"},{"metadata":{"trusted":true},"cell_type":"code","source":"aug_intensity = 1.1\nitem_tfms = RandomResizedCrop(size * 2, min_scale=.85, ratio=(.75, 1.33333))\nbatch_tfms = [*aug_transforms(mult=aug_intensity, do_flip=True, flip_vert=True, max_rotate=45, size=size, max_warp=0), Normalize.from_stats(*imagenet_stats)]\n\ndb = DataBlock(blocks=(ImageBlock, MultiCategoryBlock(encoded=True, vocab=list(train_df.columns[:11]))),\n               #splitter = RandomSplitter(.01),\n               splitter=RandomSubsetSplitter(partial_data * .85, partial_data * .15),\n               get_x = ColReader(12),\n               get_y = ColReader(list(range(11))),\n               item_tfms = item_tfms,\n               batch_tfms = batch_tfms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bs = [128, 72, 36, 14, 6, 4]\ndls = db.dataloaders(train_df, batch_size=bs[model_nr])\nm = NFNet(model_nr=model_nr)\nl = Learner(dls, m, loss_func=nn.BCEWithLogitsLoss(), metrics=[accuracy_multi]).to_fp16()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"l.fine_tune(4, base_lr=2e-5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df = pd.read_csv(dataset_path/'sample_submission.csv')\nsample_df['PatientID'] = 'None'\nsample_df['path'] = sample_df['StudyInstanceUID'].map(lambda x:str(dataset_path/'test'/x)+'.jpg')\nsample_df = sample_df.drop(columns=['StudyInstanceUID'])\ntest_dl = dls.test_dl(sample_df, batch_size = int(bs[model_nr] / 2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"l = l.to_fp32()\npreds, _ = l.tta(dl=test_dl, n=2, beta=0.25)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = sample_df.copy()\nlabel_names = list(train_df.columns[:11])\nfor i in range(len(submission_df)):\n    for j in range(len(label_names)):\n        submission_df.iloc[i, j+1] = preds[i][j].numpy().astype(np.float32)\n\nsubmission_df.to_csv(f'submission.csv', index=False)\nprint(submission_df.head(10))","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}