{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('/kaggle/input/cassava-leaf-disease-classification/')\npath.ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(path/'train.csv')\ntrain_df.head()\nwith open(path/'label_num_to_disease_map.json') as f:\n    label_dict = json.load(f)\n\nlabel_dict = {int(k):v for k,v in label_dict.items()}\ndf = train_df.set_index('image_id')\nlabels = df.to_dict()['label']\n\ndef get_label(labels, x):\n    x = Path(x)\n    return labels[x.name]\n\ndef get_data(labels, bs=64, presize=500, resize=384):\n    tfms = [Rotate(90), Warp(magnitude=0.4, p=1.),Zoom(min_zoom=0.9, max_zoom=1.3), Brightness(max_lighting=0.5), Flip(), Contrast(), Resize(resize)]\n    comp = setup_aug_tfms(tfms)\n    return DataBlock(blocks=(ImageBlock, CategoryBlock),\n        get_items=lambda p: get_image_files(p),\n        get_y=partial(get_label, labels),\n        splitter=RandomSplitter(),\n        item_tfms=[Resize(presize)],\n        batch_tfms=[*comp, Normalize.from_stats(*imagenet_stats)]).dataloaders(path/'train_images',bs=bs,num_workers=8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = get_data(labels,bs=128, presize=384)\nlearn = cnn_learner(dls, models.resnet50, metrics=[accuracy], pretrained=False)\ntest_files = get_image_files(path/'test_images')\npredictions = []\nfor fold in range(3):\n    learn.load(f'/kaggle/input/resnet50/models/resnet50-full-fold_{fold}')\n    test_dl = dls.test_dl(test_files)\n    preds = learn.tta(dl=test_dl, n=6)\n    predictions.append(preds[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = torch.argmax(torch.mean(torch.stack(predictions), dim=0), dim=1)\ntest_fn = map(lambda x: x.name, test_files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'image_id': test_fn, 'label': preds})\nsubmission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission","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}