{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Introduction: Dog Breed Identification\n\nHow well can you distinguish different dog breeds?\n\nThe task of this competition is to do fine-grained image classification on images of different dog breeds.\n\nThis kernel shows you how to use the [fast.ai library](https://docs.fast.ai/) to create a baseline model for the [Dog Breed Identification\n](https://www.kaggle.com/c/dog-breed-identification) competition.\n\nI already did the setup, evaluation and prediction, so that you can get right into the training of the CNN.\n\nTo learn more about the fast.ai library check out the [documentation](https://docs.fast.ai) and the [fast.ai course 'Practical Deep Learning for Coders'](https://course.fast.ai/)"},{"metadata":{},"cell_type":"markdown","source":"## Setup"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nimport os\nimport pandas as pd\nfrom pathlib import Path","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Check out the inout folder"},{"metadata":{"trusted":true},"cell_type":"code","source":"input_dir = Path(\"../input\")\nprint(os.listdir(input_dir))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Save the train and test directories"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir = input_dir/'train'\ntest_dir = input_dir/'test'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Check out train- and test-folder"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir(train_dir)[:5])\nprint(os.listdir(test_dir)[:5])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Take a look at labels.csv"},{"metadata":{"trusted":true},"cell_type":"code","source":"labels_path = \"../input/labels.csv\"\nlabels_csv = pd.read_csv(labels_path)\nlabels_csv.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Load in the data"},{"metadata":{"trusted":true},"cell_type":"code","source":"tfms = get_transforms()\ndata = ImageDataBunch.from_csv(\n    path = \"../input\",\n    folder = \"train\",\n    suffix = \".jpg\",\n    test = \"test/test\",\n    bs = 16,\n    size = 224,\n    ds_tfms = tfms,\n    num_workers = 0\n).normalize(imagenet_stats)\nprint(data.classes[:10])\ndata.show_batch(rows=2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Training"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(data, models.resnet18, metrics=accuracy, model_dir=\"/tmp/models\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Evaluation\nCreate a ClassificationInterpretation object to generate confusion matrices and visualizations of the most incorrect images"},{"metadata":{"trusted":true},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Plot the four images with the highest loss"},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_multi_top_losses(4, figsize=(10, 10))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Typically, you would now create a confusion matrix, but there are 120 classes in this task, so the confusion matrix would be a mess. Fast.ai has this neat function that only shows the highest values of the confusion matrix:\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.most_confused(min_val=5)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Prediction\nLet the neural network predict. It predicts a probability for every possible class. The class with the highest probability is taken as the predicted class"},{"metadata":{"trusted":true},"cell_type":"code","source":"class_score, y = learn.get_preds(DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Create a submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"# let's first check the sample submission file to understand the format of the submission file\nsample_submission =  pd.read_csv(input_dir/\"sample_submission.csv\")\ndisplay(sample_submission.head(3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes_series = pd.Series(os.listdir(test_dir))\nclasses_series = classes_series.str[:-4]\nclasses_df = pd.DataFrame({'id':classes_series})\npredictions_df = pd.DataFrame(class_score.numpy(), columns=data.classes)\nsubmission = pd.concat([classes_df, predictions_df], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\nsubmission[:5]","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}