{"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\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\nPATH = '/kaggle/input/bengaliai-cv19/'\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":"import fastai\nfrom fastai.vision import *\nfrom fastai.callbacks import SaveModelCallback\n#from csvlogger import *\n#from radam import *\n#from mish_activation import *\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfastai.__version__","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimport zipfile\nfrom tqdm import tqdm_notebook as tqdm\nimport random\nimport torchvision\n\nSEED = 42\nLABELS = 'train.csv'\n\ndef seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = True\n\nseed_everything(SEED)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"HEIGHT = 137\nWIDTH = 236\nSIZE = (128,128)\nBATCH = 128\n\nTRAIN = [PATH+'train_image_data_0.parquet',\n         PATH+'train_image_data_1.parquet',\n         PATH+'train_image_data_2.parquet',\n         PATH+'train_image_data_3.parquet']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_label = pd.read_csv(PATH+LABELS)\nnunique = list(df_label.nunique())[1:-1]\nprint(nunique)\ndf_label['components'] = 'r_'+df_label['grapheme_root'].astype(str)+','\\\n                         +'v_'+df_label['vowel_diacritic'].astype(str)+','\\\n                         +'c_'+df_label['consonant_diacritic'].astype(str)\ndf_label.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stats128, stats137, fold, nfolds = ([0.08547], [0.22490]), ([0.06922], [0.20514]), 0, 4\nFOLDER = '../input/bengali-grapheme'\n\nsrc = (ImageList.from_df(df_label, path='.', folder=FOLDER, suffix='.png', cols='image_id')\n       .split_by_idx(range(fold*len(df_label)//nfolds,(fold+1)*len(df_label)//nfolds))\n        #.split_from_df(col='is_valid')\n        .label_from_df(cols=['components'],label_delim=','))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = (src.transform(get_transforms(do_flip=False,max_warp=0.1), size=SIZE, padding_mode='zeros')\n        .databunch(bs=BATCH)\n        .normalize(imagenet_stats))\n\ndata.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model \narch = models.resnet34\n\nacc_02 = partial(accuracy_thresh)\nf_score = partial(fbeta)\nlearn = cnn_learner(data, arch, metrics=[acc_02, f_score])","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":"lr = 0.03","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(6, slice(lr))","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(5, slice(1e-5,lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.export(Path('/kaggle/working')/'try3-rn34-im128.pkl')","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}