{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport shutil\nimport cv2\nimport re\n\n# Input data files are available in the \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/Kannada-MNIST')\ntrain = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest  =pd.read_csv('../input/Kannada-MNIST/test.csv')\n#sub = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\n#dig = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#check the kaggle directory folder.\noutpath = '/kaggle/'\nos.listdir(outpath)\n#create the output folder for train and test images\nshutil.os.mkdir(outpath + \"output\")\nshutil.os.mkdir(outpath + \"output/train\")\nshutil.os.mkdir(outpath + \"output/test\")\n\n#code to delete a directory and its contents.\n#shutil.rmtree(outpath + \"output\") \n#shutil.rmtree(outpath + \"output\") \n\n#Check if the directory is created properly\noutpath = '/kaggle/output'\nos.listdir(outpath)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def save_images(outpath,df):\n    for index, row in df.iterrows():\n        pixels=np.asarray(row['pixel0':'pixel783'])\n        img=pixels.reshape((28,28))\n        pathname=os.path.join(outpath,'image_'+str(row['label'])+'_'+str(index)+'.jpg')\n        cv2.imwrite(pathname,img)\n        print('image saved ias {}'.format(pathname))\n\noutpath = '/kaggle/output/train'\nsave_images(outpath,train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('/kaggle/output/train')\npath.ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Lets us view couple of sample images\nimg = open_image('/kaggle/output/train/image_7_28127.jpg')\nimg.show(figsize=(5,5))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Creating the data bunch"},{"metadata":{"trusted":true},"cell_type":"code","source":"path_img = Path('/kaggle/output/train')\n\nfnames = get_image_files(path_img)\nfnames[:5]\n\n# tempsrt = '/kaggle/output/train/image_5_39645.jpg'\n# pat = r'/([^/]+)_\\d+_\\d+.jpg$'\n# pat = r'/image_(\\d+)_'\n# m = re.search(pat,tempsrt)\n# print(m.group(1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(42)\n\n#define the batch size\nbs = 64\n\n#define the regex pattern to get the label from the file name.\n#pat = r'/([^/]+)_\\d+_\\d+.jpg$'\npat = r'/image_(\\d+)_'\n\n#lets do some data augmentation\n#tfms = get_transforms(do_flip=False)\n\ndata = ImageDataBunch.from_name_re(path_img, fnames, pat, valid_pct=0.2,\n        ds_tfms=get_transforms(), size=64, bs=bs, num_workers=4).normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(7,8))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.classes","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Building the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(data, models.resnet34, metrics=error_rate, model_dir='/output/model/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.model\nlearn.save('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.unfreeze()","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.fit_one_cycle(8, max_lr=slice(1e-6,1e-4))","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.fit_one_cycle(8, max_lr=slice(1e-6,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr=1e-5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(8, lr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit = pd.DataFrame(columns=['id','label'])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Lets get the predictions on the test images"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Remove the \nsubmit = pd.DataFrame(columns=['id','label'])\nsubmit['id'] = test['id']\n\nfor index, row in test.iterrows():\n#    sub.at[index, 'id'] = row.at[index,'id']\n    pixels=np.asarray(row['pixel0':'pixel783'])\n    arr=pixels.reshape((28,28))\n    arr = np.stack([arr]*3,axis=0)\n    img = Image(FloatTensor(arr))\n    submit.at[index, 'label'] = int(learn.predict(img)[1])\n    \nsubmit.head(30)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit.to_csv('submission.csv',index=False)","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":1}