{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport os\nimport pandas as pd\nfrom fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset_path = Path('../input/codeml-challenge5')\nos.listdir(dataset_path)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(dataset_path/'train_label.txt', names=['classes'])\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['id'] =  range(1, len(train_df) + 1, )\ntrain_df=train_df.reindex(columns=['id', 'classes'])\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#def get_x(row): \n #   rowID = str(row['id']) + '.png'\n #   return dataset_path/'train_images'/'train_images'/rowID","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#print(get_x(train_df.iloc[1]))\n#PILImage.create(get_x('Image_'+str(train_df.iloc[0])))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_x(row): \n    #rowID = 'Image_'+str(row['id'])+'.png'\n    rowID = row['id']\n    return dataset_path/str(rowID)\ndef get_y(row): return row['classes']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#train_df['id'] = 'Image_'+train_df['id']\n#train_df['id'] = train_df['id'].apply(lambda x: f'train_images/{x}')\n#df['id'] =  'Image_' + df['id'].astype(str) + '.png'\ntrain_df['id'] = 'Image_'+train_df['id'].astype(str) + '.png'\ntrain_df['id'] = '/kaggle/input/codeml-challenge5/train_images/train_images/' + train_df['id'].astype(str)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dblock = DataBlock(\n    blocks = (ImageBlock, CategoryBlock),\n    get_x = get_x,\n    get_y = get_y,\n    item_tfms=Resize(460),\n    batch_tfms = [RandomResizedCropGPU(224), *aug_transforms(), Normalize.from_stats(*imagenet_stats)],    \n    splitter = RandomSplitter(valid_pct=0.2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = dblock.dataloaders(train_df, bs=128)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dls, resnet50, opt_func=ranger, loss_func=LabelSmoothingCrossEntropy(), metrics=accuracy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = learn.to_fp16()\nlearn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = learn.to_fp16()\n#learn.fine_tune(5, freeze_epochs=1)\n#learn.fine_tune(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#learn.fit_one_cycle(3, lr_max=1e-3)\n#learn.unfreeze()\n#learn.fit_one_cycle(4, lr_max=slice(1e-5, 1e-3))\nlearn.fine_tune(8, freeze_epochs=3)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.recorder.plot_loss()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.export('manual_fine_tune_export.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission='../input/codeml-challenge5/all_five_submission.csv'\nsample_df = pd.read_csv(sample_submission)\nsample_copy = sample_df.copy()\nsample_copy['id'] += 1\nsample_copy['id'] = 'Image_'+sample_copy['id'].astype(str) + '.png'\ntesting_path = '/kaggle/input/codeml-challenge5/test_images/content/data/test_imagess/'\nsample_copy['id'] =  testing_path + sample_copy['id'].astype(str)\nsample_copy = sample_copy.drop('classes', axis=1)\n\nsample_copy.head()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dl = learn.dls.test_dl(sample_copy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dl.show_batch()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds, _ = learn.tta(dl=test_dl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df['classes'] = preds.argmax(dim=-1).numpy()\nsample_df = sample_df.drop('testLabels', axis=1)\nsample_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_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":4}