{"cells":[{"metadata":{"_uuid":"158e6796da20c1d7fff00036ac9a6cf062d7a06a"},"cell_type":"markdown","source":"This is a simple kernel you can use as a base experiment for your first submission, implemented with the fastAI v1 library, by [fast.ai](https://www.fast.ai/).  \nIt is based on the [first lesson](https://nbviewer.jupyter.org/github/fastai/course-v3/blob/master/nbs/dl1/lesson1-pets.ipynb) of the course-v3, which is explained in [this](https://course.fast.ai/videos/?lesson=1) video.  \nIf you are interested in other edited FastAI ipynb, you can find another one here:\n* [fastaiv1 Collaborative Filtering](https://www.kaggle.com/gianfa/fastaiv1-collaborative-filtering/)"},{"metadata":{"trusted":true,"_uuid":"0187296932c48947292be14d31ce8b8181b2a959"},"cell_type":"code","source":"# %reload_ext autoreload\n# %autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3b5bd2f3d855aa514fa8c0aaa7055ecdea0f9da4"},"cell_type":"markdown","source":"## Main imports"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import torch\nimport cv2\nimport matplotlib.pyplot as plt\nimport PIL\nprint(PIL.PILLOW_VERSION)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49c7a0fb70b5019089e85a13ff34c42b7269a611"},"cell_type":"code","source":"from fastai.vision import *\nfrom fastai.metrics import error_rate\nimport fastai\nfastai.__version__","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"472fa64c9eeb722815578fd29866637941afc046"},"cell_type":"code","source":"import os\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f81866aad2ee1231ef04f997e287abc628e776d0"},"cell_type":"markdown","source":"## Data Retrieving"},{"metadata":{"trusted":true,"_uuid":"7f530c6d1df139810b830abf3e663690b8add326"},"cell_type":"code","source":"os.listdir('../input/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1068f7482b08360e8d2e6887c9e413ae81d62eb"},"cell_type":"code","source":"train_folder = '../input/train/train/'\ntest_folder = '../input/test1/test1/'\nprint(os.listdir(train_folder)[:10])\nprint(os.listdir(test_folder)[:10])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"71646782ebc596fd4a869b2b6e80bccef68bc4a4"},"cell_type":"markdown","source":"## Data Loading\nIt is very fast to do with fastAI"},{"metadata":{"_uuid":"5033e6e4ab0fabea5583f94223a26b2b164f8b6a"},"cell_type":"markdown","source":"Take the image names and split them by regex"},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(2)\nfnames = get_image_files(train_folder)\nprint(fnames[:5])\npat = re.compile(r'(cat|dog)\\.\\d+\\.jpg') # we specify a regex for finding cat or dog images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sz = 64\nbs = 64\ndata = ImageDataBunch.from_name_re(\n                                train_folder,\n                                fnames,\n                                pat,\n                                ds_tfms=get_transforms(),\n                                size=sz, bs=bs,\n                                valid_pct = 0.25,\n                                num_workers = 0, # for code safety on kaggle\n).normalize(imagenet_stats)\ndata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(rows=4, figsize=(7,6))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b57245e4810b62434261455c58fc990018534fb7"},"cell_type":"markdown","source":"## Modeling and Training"},{"metadata":{"trusted":true,"_uuid":"712c99f10368616bd7559ebe5b589d6e7545e7c7"},"cell_type":"code","source":"learn = cnn_learner(\n    data,\n    models.resnet34,\n    metrics=error_rate,\n    model_dir=\"/tmp/model/\"\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf4f236e2d0826864682cb7063c384957cc452c6"},"cell_type":"code","source":"learn.data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da51b116abb70a3f66a7b5230e6b3cc2fa362505"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"95a22301d72755d8fcd1c602e03f2fcac2fe4ecb"},"cell_type":"code","source":"learn.fit_one_cycle(1, max_lr=slice(2e-3))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"069b9e78845d32c094704c0c8b17907241d64696"},"cell_type":"markdown","source":"You can see that after a single cycle it's able to reach around 0.03 of valid_loss, great job fasta.ai!"},{"metadata":{"trusted":true,"_uuid":"64a38bc6a63d59f38b449a332e34670fe9d50b84"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"28438c998f25646771001443cedbcfad4e51fbcf"},"cell_type":"markdown","source":"## Results"},{"metadata":{"trusted":true,"_uuid":"b05384d07d3adb467887ed8d83d05d7ee125db19"},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\nlosses,idxs = interp.top_losses()\nprint(len(data.valid_ds)==len(losses)==len(idxs))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a13e55257374b3c371e6b38172d891272eec198f"},"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa8a610e168acaa56a49534ee73bde29ac73dfcc"},"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(5,5), dpi=100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mc = interp.most_confused(min_val=2)\nmcc = [x[0] for x in mc[:5]]\nmcc","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.DataFrame(os.listdir(train_folder))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"a = ['0']\ntrain.sample(n=10, random_state=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"aff978b2cbe9a7f88bd0e1b4f5dacf07c43ec64e"},"cell_type":"markdown","source":"## Prepare submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5af9472aba6820252aafe1178069e877a238221"},"cell_type":"code","source":"item1_path = data.items[100]\nprint(item1_path)\nitem1 = data.open( item1_path )\nitem1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7eeb74589b4f6397e7903b24f8857042e4fbd94f"},"cell_type":"code","source":"pred_class, pred_idx, outputs = learn.predict(item1)\nprobs = torch.nn.functional.softmax(np.log(outputs), dim=0)\nprint(pred_class)\nprint(probs)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a10d1e05c981781155b7627ecdca3aa285a4644c"},"cell_type":"markdown","source":"Now the **Test Set**"},{"metadata":{"trusted":true},"cell_type":"code","source":"data_test = ImageList.from_folder(test_folder).split_none().label_empty()\ndata_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dst = data_test.train.x[:20]\ndst","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now add _dst_ to the learner dataBunch"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.add_test(items=dst)\ndata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.data = data\nlearn.data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_probs, pred_class = learn.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(pred_probs)\nprint(pred_class)\nprint((pred_probs.numpy()[:,0]>0.5)+0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(os.listdir(test_folder))\nprint(len(df))\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_idx = 8\nprint(pred_class[img_idx])\ndata_test.train.x[img_idx]\nplt.imshow(plt.imread(test_folder+df[0].iloc[img_idx]))\n# test_folder+df[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5407e3180bd9ade2fc45ee984f0b6b6f2c737a36"},"cell_type":"code","source":"\nsubmission_data = [ids, pred_class]\n\ndf = pd.DataFrame(submission_data).T\ndf.columns = ['id','label']\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"082a246a76f15e7628e011cdf5c776394c221c83"},"cell_type":"code","source":"df.to_csv('./kaggle_catsdogs.csv', index=False)\nprint( os.path.exists('./kaggle_catsdogs.csv') )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49616fe568b2aa460d1bc9fc9d7f0092d6f8a061"},"cell_type":"code","source":"!rm -rf dogscats","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3159e4617e40be26f82f296d3b9e9260113fe480"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}