{"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_minor":4,"nbformat":4,"cells":[{"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\n# import os\n# for 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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-07T17:36:41.473713Z","iopub.execute_input":"2023-03-07T17:36:41.474661Z","iopub.status.idle":"2023-03-07T17:36:41.480919Z","shell.execute_reply.started":"2023-03-07T17:36:41.474601Z","shell.execute_reply":"2023-03-07T17:36:41.479297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install -Uqq fastai timm==0.6.2.dev0\nfrom fastai.vision.all import *\nimport matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:36:43.257346Z","iopub.execute_input":"2023-03-07T17:36:43.257711Z","iopub.status.idle":"2023-03-07T17:37:07.590281Z","shell.execute_reply.started":"2023-03-07T17:36:43.257678Z","shell.execute_reply":"2023-03-07T17:37:07.588914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/tpu-getting-started/tfrecords-jpeg-512x512/train/","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:37:07.593078Z","iopub.execute_input":"2023-03-07T17:37:07.593754Z","iopub.status.idle":"2023-03-07T17:37:08.784177Z","shell.execute_reply.started":"2023-03-07T17:37:07.593710Z","shell.execute_reply":"2023-03-07T17:37:08.782733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the data\nflower_path = '/kaggle/input/104-flowers-garden-of-eden/jpeg-192x192'\nTRAIN_DIR  = flower_path + '/train'\nVAL_DIR  = flower_path + '/val'\nTEST_DIR  = flower_path + '/test/'","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:37:08.786189Z","iopub.execute_input":"2023-03-07T17:37:08.786568Z","iopub.status.idle":"2023-03-07T17:37:08.796960Z","shell.execute_reply.started":"2023-03-07T17:37:08.786528Z","shell.execute_reply":"2023-03-07T17:37:08.795825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_tfms = Resize(192)\nbatch_tfms = [RandomResizedCrop(224), *aug_transforms(mult=1.0, do_flip=True, max_rotate=30.0, max_zoom=1.5,\n                            max_lighting=.8, max_warp=0.3, p_lighting=.9)]","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:37:08.801192Z","iopub.execute_input":"2023-03-07T17:37:08.803135Z","iopub.status.idle":"2023-03-07T17:37:08.823390Z","shell.execute_reply.started":"2023-03-07T17:37:08.803089Z","shell.execute_reply":"2023-03-07T17:37:08.821759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# High level fastai API using fastai\ndata = ImageDataLoaders.from_folder(flower_path, valid='val',batch_tfms=batch_tfms, \n                                   item_tfms=item_tfms)","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:37:15.418840Z","iopub.execute_input":"2023-03-07T17:37:15.419330Z","iopub.status.idle":"2023-03-07T17:37:28.009718Z","shell.execute_reply.started":"2023-03-07T17:37:15.419288Z","shell.execute_reply":"2023-03-07T17:37:28.008542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.show_batch(max_n=9)","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:37:28.015646Z","iopub.execute_input":"2023-03-07T17:37:28.018133Z","iopub.status.idle":"2023-03-07T17:37:29.844421Z","shell.execute_reply.started":"2023-03-07T17:37:28.018091Z","shell.execute_reply":"2023-03-07T17:37:29.842937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.c","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:37:29.862355Z","iopub.execute_input":"2023-03-07T17:37:29.862910Z","iopub.status.idle":"2023-03-07T17:37:29.876699Z","shell.execute_reply.started":"2023-03-07T17:37:29.862850Z","shell.execute_reply":"2023-03-07T17:37:29.875234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.vocab","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:37:29.878205Z","iopub.execute_input":"2023-03-07T17:37:29.878658Z","iopub.status.idle":"2023-03-07T17:37:29.887174Z","shell.execute_reply.started":"2023-03-07T17:37:29.878620Z","shell.execute_reply":"2023-03-07T17:37:29.885822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(TRAIN_DIR)\npath.ls()","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:37:29.888779Z","iopub.execute_input":"2023-03-07T17:37:29.889342Z","iopub.status.idle":"2023-03-07T17:37:29.899758Z","shell.execute_reply.started":"2023-03-07T17:37:29.889302Z","shell.execute_reply":"2023-03-07T17:37:29.898417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flower_blocks = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                   get_items=get_image_files,\n                   splitter=GrandparentSplitter(valid_name='val'),\n                   get_y=[parent_label],\n                   item_tfms=Resize(240),\n                   batch_tfms=aug_transforms(size=224, max_rotate=30, min_scale=0.75))","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:42:48.243080Z","iopub.execute_input":"2023-03-07T17:42:48.243579Z","iopub.status.idle":"2023-03-07T17:42:48.257100Z","shell.execute_reply.started":"2023-03-07T17:42:48.243532Z","shell.execute_reply":"2023-03-07T17:42:48.255940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = flower_blocks.dataloaders(TRAIN_DIR)\ndls.show_batch(max_n=9)","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:42:50.273719Z","iopub.execute_input":"2023-03-07T17:42:50.274176Z","iopub.status.idle":"2023-03-07T17:42:54.280147Z","shell.execute_reply.started":"2023-03-07T17:42:50.274137Z","shell.execute_reply":"2023-03-07T17:42:54.279011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.train.show_batch(max_n=8, nrows=2, unique=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:46:12.628235Z","iopub.execute_input":"2023-03-07T17:46:12.628736Z","iopub.status.idle":"2023-03-07T17:46:14.370132Z","shell.execute_reply.started":"2023-03-07T17:46:12.628691Z","shell.execute_reply":"2023-03-07T17:46:14.365525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dls(size, val_idx, bs=64):\n    dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                   get_items=get_image_files,\n                   get_y=parent_label,\n                   splitter=IndexSplitter(val_idx),\n                   item_tfms = Resize(256),\n                   batch_tfms = [RandomResizedCrop(size), \n                                 *aug_transforms(mult=1.0, do_flip=True, \n                                                 max_rotate=30.0, max_zoom=1.5,\n                                                  max_lighting=.8, max_warp=0.3, \n                                                 p_lighting=.9),\n                                 Normalize.from_stats(*imagenet_stats)])\n    return dblock.dataloaders(path, bs=bs)","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:48:02.630034Z","iopub.execute_input":"2023-03-07T17:48:02.630508Z","iopub.status.idle":"2023-03-07T17:48:02.640493Z","shell.execute_reply.started":"2023-03-07T17:48:02.630464Z","shell.execute_reply":"2023-03-07T17:48:02.639368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\n\nimgs = get_image_files(path)\nrandom.shuffle(imgs)\nlbls = [parent_label(im) for im in imgs]\n\nval_pct = []\nskf = StratifiedKFold(n_splits=2, shuffle=True)\ni = 0\n\nfor _, val_idx in skf.split(np.array(imgs), lbls):\n    dls = get_dls(bs=32, size=128, val_idx=val_idx)\n    learn = vision_learner(dls, resnet18, metrics=error_rate)\n    learn.fine_tune(1)","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:48:04.119188Z","iopub.execute_input":"2023-03-07T17:48:04.119652Z","iopub.status.idle":"2023-03-07T17:53:30.201180Z","shell.execute_reply.started":"2023-03-07T17:48:04.119610Z","shell.execute_reply":"2023-03-07T17:53:30.199921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub= pd.read_csv('../input/tpu-getting-started/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:53:52.481388Z","iopub.execute_input":"2023-03-07T17:53:52.481899Z","iopub.status.idle":"2023-03-07T17:53:52.513010Z","shell.execute_reply.started":"2023-03-07T17:53:52.481835Z","shell.execute_reply":"2023-03-07T17:53:52.511924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:53:56.814254Z","iopub.execute_input":"2023-03-07T17:53:56.814735Z","iopub.status.idle":"2023-03-07T17:53:56.848691Z","shell.execute_reply.started":"2023-03-07T17:53:56.814693Z","shell.execute_reply":"2023-03-07T17:53:56.847162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = sub.shape[0]\ntest_folder = TEST_DIR ","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:54:04.181284Z","iopub.execute_input":"2023-03-07T17:54:04.181832Z","iopub.status.idle":"2023-03-07T17:54:04.188359Z","shell.execute_reply.started":"2023-03-07T17:54:04.181783Z","shell.execute_reply":"2023-03-07T17:54:04.187120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread(TEST_DIR+'416e24d42.jpeg')\nprint(learn.predict(img)[0])","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:54:09.092709Z","iopub.execute_input":"2023-03-07T17:54:09.093217Z","iopub.status.idle":"2023-03-07T17:54:09.213444Z","shell.execute_reply.started":"2023-03-07T17:54:09.093174Z","shell.execute_reply":"2023-03-07T17:54:09.212355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(n):\n  idc = sub.iloc[i][0]\n  k = TEST_DIR + idc + '.jpeg'\n  k = plt.imread(k)\n  ans = learn.predict(k)[0]\n  sub.loc[[i],1:] = str(ans)\n\nprint(\"Done Prediction saved ---> \")","metadata":{"execution":{"iopub.status.busy":"2023-03-07T17:54:15.947321Z","iopub.execute_input":"2023-03-07T17:54:15.947852Z","iopub.status.idle":"2023-03-07T18:03:36.934805Z","shell.execute_reply.started":"2023-03-07T17:54:15.947803Z","shell.execute_reply":"2023-03-07T18:03:36.933596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"sub.to_csv('submission.csv', index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-07T18:06:20.377538Z","iopub.execute_input":"2023-03-07T18:06:20.378215Z","iopub.status.idle":"2023-03-07T18:06:20.411049Z","shell.execute_reply.started":"2023-03-07T18:06:20.378167Z","shell.execute_reply":"2023-03-07T18:06:20.410047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}