{"cells":[{"metadata":{},"cell_type":"markdown","source":"We trained a ResNet50 model in the [training notebook](https://www.kaggle.com/ankursingh12/fastai-plant2021-training). In this notebook, we will do the inference part. Are you excited? because I am 😁\n\nLets import fastai & set the seed for reproducibility."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from fastai.vision.all import *\n\nseed = 42\nset_seed(seed, reproducible=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets initialize our path variable"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"path = Path('../input/plant-pathology-2021-fgvc8')\nmodels_path = Path('../input/plant-models')\n\nmodels = [models_path/f'resnet50_foldx{i}.pkl' for i in range(1)]\nmodels","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Next, we will need to define `get_x` and `get_y`. \n\n**Explanation:** When we export a fastai model, it saves everything (quite literally). Along with the model, the dataloader and its configurations are also saved. There is only one caveat, we cannot pickle functions. Hence, we will have to define them again. "},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_x(x): return str(path/'test_images') + os.path.sep + x['image']\ndef get_y(y): return y['labels']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"## Data\nsample = pd.read_csv(path/'sample_submission.csv')\nsample","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"time to load the model and assign test set."},{"metadata":{},"cell_type":"markdown","source":"We are all set to make predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = 0\nfor m in models:\n    learner = load_learner(m, cpu=False).to_fp32()\n    test_dl = learner.dls.test_dl(sample)\n    preds, _ = learner.tta(dl=test_dl)\n    predictions += preds\n    \npredictions /= len(models)\npredictions","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"These predictions are not usable. We will have to convert them their corresponding labels. Please check the [competition home page](https://www.kaggle.com/c/plant-pathology-2021-fgvc8/overview/evaluation) for details about submission format. "},{"metadata":{"trusted":true},"cell_type":"code","source":"vocab = learner.dls.vocab\npreds_dec = [vocab[i] for i in predictions.argmax(1)]\npreds_dec","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It worked as expected! We are not done yet. Lets update our submission file "},{"metadata":{},"cell_type":"markdown","source":"### Submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"sample['labels'] = preds_dec\nsample.to_csv('submission.csv',index=False)\nsample","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"WoooWW! Thank you show much for sticking with me till the end. \n\nI wish you learnt something new. Please consider **upvoting** the notebook. Its will encourage me to write more such kernels. See you around, Bye!"}],"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}