{"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)\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":"from fastai.vision import *\nimport fastai\nimport imageio\nfrom sklearn.model_selection import train_test_split\n\ndefaults.device = torch.device('cuda')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv('../input/Kannada-MNIST/test.csv')\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp_df = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\ntmp_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dig_df = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')\ndig_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_df = train_df.append(dig_df, ignore_index=True)###\nnew_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def save_imgs(path:Path, imgs, labels):\n    imgs =  np.array(imgs).reshape(-1,28,28)\n    imgs = np.stack((imgs,)*3, axis=-1)\n    if len(labels) > 0:\n        labels = np.array(labels)\n        \n    for i in range(len(imgs)):\n        if len(labels) > 0:\n            imageio.imsave(str(path/str(labels[i])/(str(i)+'.jpg') ), imgs[i])\n        else:\n            imageio.imsave(str(path/(str(i)+'.jpg') ), imgs[i]) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_imgs_labels(path:Path, df):\n    labels = df['label']\n    path.mkdir(parents=True,exist_ok=True)\n    imgs = df.loc[:, 'pixel0':'pixel783']\n    return imgs, labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path1 = Path('../input1/train')\nvalid_path1 = Path('../input1/valid')\ntest_path1 = Path('../input1/test')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imgs1, labels1 = get_imgs_labels(train_path1, new_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train1, X_test1, y_train1, y_test1 \\\n= train_test_split(imgs1, labels1, test_size=0.08, random_state=17, stratify=labels1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(X_train1), len(y_train1), len(X_test1), len(y_test1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_subdirs(path:Path, num_dirs):\n    for i in range(num_dirs):\n        Path(path/str(i)).mkdir(parents=True,exist_ok=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"create_subdirs(train_path1, labels1.nunique())\ncreate_subdirs(valid_path1, labels1.nunique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir ../input1/test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"save_imgs(train_path1, X_train1, y_train1)\nsave_imgs(valid_path1, X_test1, y_test1)\nsave_imgs(test_path1, np.array(test_df.drop(['id'], axis=1)), [])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tfms = get_transforms(do_flip=False, flip_vert=False, max_rotate= 10.0, \n                      max_zoom=1.1, max_lighting=0.15, max_warp=0.15,\n                      p_affine=0.75, p_lighting=0.75)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"db1 = (ImageList.from_folder('../input1/') \n        .split_by_folder()          \n        .label_from_folder()        \n        .add_test_folder(test_path1)\n        .transform(tfms, size=64)\n        .databunch(bs=512)\n        .normalize(mnist_stats))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"db1.show_batch(rows=4, figsize=(6,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn1 = cnn_learner(db1, models.densenet121, metrics=[error_rate, accuracy], model_dir=\"/tmp/model/\", pretrained=False)\nlearn1.unfreeze()\nlearn1.lr_find()\nlearn1.recorder.plot(suggestion=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn1.recorder.plot(suggestion=True, skip_end=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn1.fit_one_cycle(50, max_lr=slice(1e-3, 1e-2, 1e-1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn1.save('unfreeze_densenet121_learn1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn1.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_confusion_matrix()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(6, 6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.drop('id',axis = 'columns',inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp_df = tmp_df[0:0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_arr = np.array(test_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(len(img_arr)):\n    img_data = img_arr[i].reshape(28,28)/255.\n    img_data = np.stack((img_data,)*3, axis=0)\n    img = Image(FloatTensor(img_data))\n    tmp_df.loc[i]=[i+1,int(learn1.predict(img)[1])]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp_df['id'] = tmp_df['id'].apply(lambda k: k-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp_df.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":1}