{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport seaborn\nfrom sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix\n\nfrom fastai import *\nfrom fastai.vision import *\n\n%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load data\n\nThis [dataset](https://www.kaggle.com/ianmoone0617/flower-goggle-tpu-classification) contains an already processed tfrecords data as csv and jpegs. Will be using it in this notebook."},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/flower-goggle-tpu-classification/flowers_idx.csv\")\nlabel = pd.read_csv(\"../input/flower-goggle-tpu-classification/flowers_label.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('/kaggle/input/flower-goggle-tpu-classification/flower_tpu/flower_tpu/flowers_google/')\npath_test = Path('/kaggle/input/flower-goggle-tpu-classification/flower_tpu/flower_tpu/test/test/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tfms = get_transforms(do_flip=True,max_rotate=0.1,max_lighting=0.15)\ntest = (ImageList.from_folder(path_test,extensions='.jpeg'))\n\ndata = (ImageList.from_df(df,path,folder='flowers_google',suffix='.jpeg',cols='id')\n                .split_by_rand_pct(0.15)\n                .label_from_df(cols='flower_cls')\n                .transform(tfms)\n                .add_test(test)\n                .databunch(bs=32)\n                .normalize(imagenet_stats))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(rows=4)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train"},{"metadata":{"trusted":true},"cell_type":"code","source":"arch = models.resnet50\nlearn = cnn_learner(data, arch, metrics=accuracy, model_dir='/kaggle/working')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 1e-2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(6,lr,moms=(0.9,0.8))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Interpretation"},{"metadata":{"trusted":true},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_top_losses(12,figsize=(20,8))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.most_confused(min_val=3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prediction and submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"img = open_image('/kaggle/input/flower-goggle-tpu-classification/flower_tpu/flower_tpu/test/test/d9cb87ad0.jpeg')\nprint(learn.predict(img)[0])\nimg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"samp = pd.read_csv('/kaggle/input/flower-classification-with-tpus/sample_submission.csv')\nn = samp.shape[0]\npath_alltest = '/kaggle/input/flower-goggle-tpu-classification/flower_tpu/flower_tpu/test/test/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(n):\n    idc = samp.iloc[i][0]\n    k = path_alltest + idc + '.jpeg'\n    k = open_image(k)\n    ans = learn.predict(k)[0]\n    samp.loc[[i],1:] = str(ans)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"samp.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lab = {}\nfor i in range(label.shape[0]):\n  sha = label.iloc[i]\n  lab[sha[1]]=int(sha[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"samp.label.replace(lab,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"samp.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"samp.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}