{"cells":[{"metadata":{},"cell_type":"markdown","source":"The notebook uses DataLoaders in fastai2 to help manage the datasets, and use CNN and resnet to create a classification model.\n\nTo use TPU resources, the notebook uses xla extension from https://github.com/butchland/fastai_xla_extensions.\n\nFor the part of installing xla extension, this notebook refers from https://www.kaggle.com/johnyquest/tpu-fastai-notebook.\n\nIf you have any questions, ask away!"},{"metadata":{"papermill":{"duration":0.026006,"end_time":"2020-12-14T09:12:47.881463","exception":false,"start_time":"2020-12-14T09:12:47.855457","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Preparation"},{"metadata":{"papermill":{"duration":0.024006,"end_time":"2020-12-14T09:12:47.929698","exception":false,"start_time":"2020-12-14T09:12:47.905692","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Environment Check"},{"metadata":{"papermill":{"duration":0.023831,"end_time":"2020-12-14T09:12:47.978415","exception":false,"start_time":"2020-12-14T09:12:47.954584","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Check datasets"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-14T09:12:48.036701Z","iopub.status.busy":"2020-12-14T09:12:48.035882Z","iopub.status.idle":"2020-12-14T09:12:48.047107Z","shell.execute_reply":"2020-12-14T09:12:48.0478Z"},"papermill":{"duration":0.044527,"end_time":"2020-12-14T09:12:48.048023","exception":false,"start_time":"2020-12-14T09:12:48.003496","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Install Packages"},{"metadata":{},"cell_type":"markdown","source":"***The order of installation is CRUCIAL!***"},{"metadata":{"trusted":true},"cell_type":"code","source":"# %%capture\n!pip install -Uqq fastcore --upgrade\n!pip install -Uqq fastai --upgrade","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Install the TPU client"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -Uqq cloud-tpu-client==0.10 https://storage.googleapis.com/tpu-pytorch/wheels/torch_xla-1.7-cp37-cp37m-linux_x86_64.whl","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Install fastai_xla_extensions"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -Uqq git+https://github.com/butchland/fastai_xla_extensions.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import fastai_xla_extensions.core","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.024883,"end_time":"2020-12-14T09:12:48.098818","exception":false,"start_time":"2020-12-14T09:12:48.073935","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Check fastai version"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.execute_input":"2020-12-14T09:12:48.153163Z","iopub.status.busy":"2020-12-14T09:12:48.152365Z","iopub.status.idle":"2020-12-14T09:12:48.161535Z","shell.execute_reply":"2020-12-14T09:12:48.160979Z"},"papermill":{"duration":0.038331,"end_time":"2020-12-14T09:12:48.161637","exception":false,"start_time":"2020-12-14T09:12:48.123306","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import fastai\nfastai.__version__","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.025479,"end_time":"2020-12-14T09:12:50.154485","exception":false,"start_time":"2020-12-14T09:12:50.129006","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Import library"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-14T09:12:50.212632Z","iopub.status.busy":"2020-12-14T09:12:50.211754Z","iopub.status.idle":"2020-12-14T09:12:51.777663Z","shell.execute_reply":"2020-12-14T09:12:51.776987Z"},"papermill":{"duration":1.597289,"end_time":"2020-12-14T09:12:51.77779","exception":false,"start_time":"2020-12-14T09:12:50.180501","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"%%capture\nfrom fastai.vision.all import *\nimport fastai_xla_extensions.core","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.142211,"end_time":"2020-12-14T09:12:51.974684","exception":false,"start_time":"2020-12-14T09:12:51.832473","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Datasets Handling"},{"metadata":{},"cell_type":"markdown","source":"## Transform TFRecord Format to Image files"},{"metadata":{"trusted":true},"cell_type":"code","source":"path_tmp = Path(\"/kaggle/temp\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### tfrecord to image"},{"metadata":{},"cell_type":"markdown","source":"Start with one file."},{"metadata":{"trusted":true},"cell_type":"code","source":"filenames = ['/kaggle/input/tpu-getting-started/tfrecords-jpeg-224x224/val/09-224x224-232.tfrec']\nraw_dataset = tf.data.TFRecordDataset(filenames)\nraw_dataset\n\nfor raw_record in raw_dataset.take(-1):\n    example = tf.train.Example()\n    example.ParseFromString(raw_record.numpy())\n    print(example)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_id = example.features.feature[\"id\"].bytes_list.value[0].decode(\"utf-8\")\nimage_id","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_class = str(example.features.feature[\"class\"].int64_list.value[0])\nimage_class","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image = example.features.feature[\"image\"].bytes_list.value[0]\nimage","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image = Image.open(io.BytesIO(image))\nimage","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Batch processing..."},{"metadata":{"trusted":true},"cell_type":"code","source":"fns = {'train':[], 'val':[], 'test':[]}\nfor dirname, _, filenames in os.walk('/kaggle/input/tpu-getting-started/tfrecords-jpeg-224x224'):\n    for fn in filenames:\n        data_type = os.path.basename(dirname)\n        fns[data_type].append(os.path.join(dirname, fn))\nfns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for data_type, path_list in fns.items():\n    raw_dataset = tf.data.TFRecordDataset(path_list)\n\n    for raw_record in raw_dataset.take(-1):\n        example = tf.train.Example()\n        example.ParseFromString(raw_record.numpy())\n\n        image_id = example.features.feature[\"id\"].bytes_list.value[0].decode(\"utf-8\")\n        image = example.features.feature[\"image\"].bytes_list.value[0]\n        image = Image.open(io.BytesIO(image))\n\n        if data_type != 'test':\n            image_class = str(example.features.feature[\"class\"].int64_list.value[0])\n\n            foldername = f'{path_tmp}/{data_type}/{image_class}'\n        else:\n            foldername = f'{path_tmp}/{data_type}'\n    \n        try:\n            os.makedirs(foldername)\n        except:\n            pass\n        image.save(f'{foldername}/{image_id}.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imgs = get_image_files(path_tmp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Image.open(imgs[1])","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.040966,"end_time":"2020-12-14T09:12:52.905814","exception":false,"start_time":"2020-12-14T09:12:52.864848","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Create DataLoaders"},{"metadata":{"trusted":true},"cell_type":"code","source":"db = DataBlock(blocks = (ImageBlock, CategoryBlock),\n                 get_items=get_image_files, \n                 splitter=GrandparentSplitter(train_name='train', valid_name='val'),\n                 get_y=parent_label,\n                 batch_tfms=aug_transforms(size=224, min_scale=0.75))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = db.dataloaders(path_tmp, bs=64)\ndls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-14T09:12:53.502941Z","iopub.status.busy":"2020-12-14T09:12:53.502019Z","iopub.status.idle":"2020-12-14T09:12:53.576641Z","shell.execute_reply":"2020-12-14T09:12:53.576101Z"},"papermill":{"duration":0.113321,"end_time":"2020-12-14T09:12:53.576753","exception":false,"start_time":"2020-12-14T09:12:53.463432","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"xb,yb = dls.one_batch()\nxb.shape,yb.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Create and Train Model"},{"metadata":{"papermill":{"duration":0.030985,"end_time":"2020-12-14T09:12:53.985443","exception":false,"start_time":"2020-12-14T09:12:53.954458","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Use fp16 and Restnet50 Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.bs = 32","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.callback.fp16 import *\nlearn = cnn_learner(dls, resnet50, metrics=accuracy).to_fp16()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Transfer the model to TPU and check if success"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.to_xla()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"assert one_param(learn.model).device.type == 'xla'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fine_tune(10, freeze_epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.recorder.plot_loss()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Validate"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-14T09:12:54.691894Z","iopub.status.busy":"2020-12-14T09:12:54.689828Z","iopub.status.idle":"2020-12-14T09:12:54.692626Z","shell.execute_reply":"2020-12-14T09:12:54.693107Z"},"papermill":{"duration":0.043026,"end_time":"2020-12-14T09:12:54.693232","exception":false,"start_time":"2020-12-14T09:12:54.650206","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"learn.show_results()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.validate()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.034312,"end_time":"2020-12-14T09:12:55.020142","exception":false,"start_time":"2020-12-14T09:12:54.98583","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Test Dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dl = dls.test_dl(get_image_files(f'{path_tmp}/test')) \nclass_score, y = learn.get_preds(dl=test_dl) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_score = np.argmax(class_score, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted_classes = [dls.vocab[i] for i in class_score]\npredicted_classes[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_id_list = [x[:-4] for x in os.listdir(path_tmp/'test')]\nimage_id_list","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-14T09:12:55.432779Z","iopub.status.busy":"2020-12-14T09:12:55.43196Z","iopub.status.idle":"2020-12-14T09:12:55.740842Z","shell.execute_reply":"2020-12-14T09:12:55.742547Z"},"papermill":{"duration":0.352001,"end_time":"2020-12-14T09:12:55.742739","exception":false,"start_time":"2020-12-14T09:12:55.390738","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"output = pd.DataFrame({'id': image_id_list, 'label': predicted_classes})\noutput.to_csv('submission.csv', index=False)\noutput.head()","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}