{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# For the CSVs\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Importing Libraries"},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"!pip install smote_variants","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# For the Model\nfrom fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# For acessing the Files\nimport os\nimport imblearn\nfrom imblearn.pipeline import Pipeline\nfrom fastai.callback.tracker import SaveModelCallback\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom imblearn.under_sampling import RandomUnderSampler\nfrom imblearn.over_sampling import *\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Defining the Path:\npath = Path('../input/cassava-leaf-disease-classification')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Lets take a look at the CSV.\ndata = pd.read_csv(path/'train.csv')\n\n\nsampler = RandomUnderSampler(random_state=2)\nimages = list(data.image_id)\nlabels = list(data.label)\nimages, labels = sampler.fit_resample(np.array(images).reshape(-1, 1),labels)\nimages = images.reshape(-1)\ndata = pd.DataFrame(data={'image_id':images, 'label':labels})\n#dls = ImageDataLoaders.from_df(data, path=\"./train_images/\", item_tfms=Resize(224))\ntrain = data[~data['image_id'].isin(['1562043567.jpg', '3551135685.jpg', '2252529694.jpg'])]\ndata.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/cassava-leaf-disease-classification')\n\ndef get_x(r):\n    return path/'train_images'/r['image_id']\n\ndef get_y(r):\n    return r['label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_data(size=256, bs=64, data_df=train):\n    block = DataBlock(blocks=(ImageBlock, CategoryBlock), \n                      splitter=RandomSplitter(seed=42), \n                      get_x=get_x,\n                      get_y=get_y, \n                      item_tfms = RandomResizedCrop(256),\n                      batch_tfms = [*aug_transforms(size=size),\n                                    Normalize.from_stats(*imagenet_stats)])\n\n    \n    return block.dataloaders(data_df, bs=bs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataloader = get_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataloader.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dataloader, resnet18, metrics=accuracy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fine_tune(3,0.00831763744354248)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.export(\"/kaggle/working/models/export.pth\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = load_learner(\"/kaggle/working/models/export.pth\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Submissions"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv(path/'sample_submission.csv')\n\ntest_data_path = submission_df['image_id'].apply(lambda x: path/'test_images'/x)\ntst_dl = learn.dls.test_dl(test_data_path)\npredictions = learn.tta(dl = tst_dl, n=10)\n\nsubmission_df['label'] = np.argmax(predictions[0],axis=1)\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.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":4}