{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install fastai --upgrade","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:19.685362Z","iopub.execute_input":"2022-05-14T15:03:19.685687Z","iopub.status.idle":"2022-05-14T15:03:37.678079Z","shell.execute_reply.started":"2022-05-14T15:03:19.685599Z","shell.execute_reply":"2022-05-14T15:03:37.677273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom fastai.vision.all import *\nimport torchvision","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:37.680311Z","iopub.execute_input":"2022-05-14T15:03:37.680569Z","iopub.status.idle":"2022-05-14T15:03:40.150618Z","shell.execute_reply.started":"2022-05-14T15:03:37.680534Z","shell.execute_reply":"2022-05-14T15:03:40.149711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/sorghum-id-fgvc-9')","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.152529Z","iopub.execute_input":"2022-05-14T15:03:40.152976Z","iopub.status.idle":"2022-05-14T15:03:40.156906Z","shell.execute_reply.started":"2022-05-14T15:03:40.152933Z","shell.execute_reply":"2022-05-14T15:03:40.156234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(path/'train_cultivar_mapping.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.157946Z","iopub.execute_input":"2022-05-14T15:03:40.158726Z","iopub.status.idle":"2022-05-14T15:03:40.197549Z","shell.execute_reply.started":"2022-05-14T15:03:40.158671Z","shell.execute_reply":"2022-05-14T15:03:40.196932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.199837Z","iopub.execute_input":"2022-05-14T15:03:40.200081Z","iopub.status.idle":"2022-05-14T15:03:40.24059Z","shell.execute_reply.started":"2022-05-14T15:03:40.200048Z","shell.execute_reply":"2022-05-14T15:03:40.239928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dropping NAN values","metadata":{}},{"cell_type":"code","source":"train_df.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.241982Z","iopub.execute_input":"2022-05-14T15:03:40.242453Z","iopub.status.idle":"2022-05-14T15:03:40.253229Z","shell.execute_reply.started":"2022-05-14T15:03:40.242418Z","shell.execute_reply":"2022-05-14T15:03:40.252445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.254203Z","iopub.execute_input":"2022-05-14T15:03:40.254416Z","iopub.status.idle":"2022-05-14T15:03:40.262953Z","shell.execute_reply.started":"2022-05-14T15:03:40.254389Z","shell.execute_reply":"2022-05-14T15:03:40.26208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.264318Z","iopub.execute_input":"2022-05-14T15:03:40.265173Z","iopub.status.idle":"2022-05-14T15:03:40.293545Z","shell.execute_reply.started":"2022-05-14T15:03:40.265145Z","shell.execute_reply":"2022-05-14T15:03:40.292894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.294784Z","iopub.execute_input":"2022-05-14T15:03:40.295238Z","iopub.status.idle":"2022-05-14T15:03:40.305141Z","shell.execute_reply.started":"2022-05-14T15:03:40.295201Z","shell.execute_reply":"2022-05-14T15:03:40.30445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Getting the unique values of cultivar and their counts","metadata":{}},{"cell_type":"code","source":"train_df.cultivar.unique()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.306331Z","iopub.execute_input":"2022-05-14T15:03:40.306573Z","iopub.status.idle":"2022-05-14T15:03:40.31499Z","shell.execute_reply.started":"2022-05-14T15:03:40.306541Z","shell.execute_reply":"2022-05-14T15:03:40.314344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.cultivar.value_counts().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.316419Z","iopub.execute_input":"2022-05-14T15:03:40.316902Z","iopub.status.idle":"2022-05-14T15:03:40.332806Z","shell.execute_reply.started":"2022-05-14T15:03:40.316853Z","shell.execute_reply":"2022-05-14T15:03:40.332148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.333853Z","iopub.execute_input":"2022-05-14T15:03:40.334832Z","iopub.status.idle":"2022-05-14T15:03:40.341021Z","shell.execute_reply.started":"2022-05-14T15:03:40.334798Z","shell.execute_reply":"2022-05-14T15:03:40.340409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.iloc[3329]","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.342102Z","iopub.execute_input":"2022-05-14T15:03:40.342476Z","iopub.status.idle":"2022-05-14T15:03:40.353028Z","shell.execute_reply.started":"2022-05-14T15:03:40.342334Z","shell.execute_reply":"2022-05-14T15:03:40.352381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.357968Z","iopub.execute_input":"2022-05-14T15:03:40.35846Z","iopub.status.idle":"2022-05-14T15:03:40.367571Z","shell.execute_reply.started":"2022-05-14T15:03:40.358404Z","shell.execute_reply":"2022-05-14T15:03:40.366834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model creation and Training","metadata":{}},{"cell_type":"markdown","source":"Creating DataLoader from dataframe","metadata":{}},{"cell_type":"code","source":"datablock = DataBlock(\n    blocks = (ImageBlock, CategoryBlock),\n    get_x = ColReader('image', path/'train_images'),\n    get_y = ColReader('cultivar'),\n    splitter = RandomSplitter(valid_pct=0.2),#splitter,\n    item_tfms = Resize(225)\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.369094Z","iopub.execute_input":"2022-05-14T15:03:40.369702Z","iopub.status.idle":"2022-05-14T15:03:40.377541Z","shell.execute_reply.started":"2022-05-14T15:03:40.36965Z","shell.execute_reply":"2022-05-14T15:03:40.376821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = datablock.dataloaders(train_df, bs=32)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:40.378968Z","iopub.execute_input":"2022-05-14T15:03:40.379565Z","iopub.status.idle":"2022-05-14T15:03:44.764826Z","shell.execute_reply.started":"2022-05-14T15:03:40.37953Z","shell.execute_reply":"2022-05-14T15:03:44.764071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, torchvision.models.densenet201, metrics = accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:44.766756Z","iopub.execute_input":"2022-05-14T15:03:44.767263Z","iopub.status.idle":"2022-05-14T15:03:48.924738Z","shell.execute_reply.started":"2022-05-14T15:03:44.767225Z","shell.execute_reply":"2022-05-14T15:03:48.923936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Training the model","metadata":{}},{"cell_type":"code","source":"learn.fit(50, cbs=EarlyStoppingCallback(monitor='accuracy', patience=5, min_delta=0.001))","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:03:48.926143Z","iopub.execute_input":"2022-05-14T15:03:48.926398Z","iopub.status.idle":"2022-05-14T15:04:09.969443Z","shell.execute_reply.started":"2022-05-14T15:03:48.926361Z","shell.execute_reply":"2022-05-14T15:04:09.966051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export('./densenet_model.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:09.972129Z","iopub.status.idle":"2022-05-14T15:04:09.972741Z","shell.execute_reply.started":"2022-05-14T15:04:09.972496Z","shell.execute_reply":"2022-05-14T15:04:09.972526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"markdown","source":"Inference for a single image","metadata":{}},{"cell_type":"code","source":"#learn = load_learner('./densenet_model.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:09.977759Z","iopub.status.idle":"2022-05-14T15:04:09.98002Z","shell.execute_reply.started":"2022-05-14T15:04:09.979796Z","shell.execute_reply":"2022-05-14T15:04:09.979821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.predict('../input/sorghum-id-fgvc-9/test/1000005362.png')","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:09.98105Z","iopub.status.idle":"2022-05-14T15:04:09.981935Z","shell.execute_reply.started":"2022-05-14T15:04:09.981717Z","shell.execute_reply":"2022-05-14T15:04:09.981741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Inference on complete data**","metadata":{}},{"cell_type":"code","source":"test_images = get_image_files(path/'test')\ntest_dataloader = learn.dls.test_dl(test_images)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:09.9867Z","iopub.status.idle":"2022-05-14T15:04:09.987159Z","shell.execute_reply.started":"2022-05-14T15:04:09.986938Z","shell.execute_reply":"2022-05-14T15:04:09.986961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images[0]","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:09.988653Z","iopub.status.idle":"2022-05-14T15:04:09.990968Z","shell.execute_reply.started":"2022-05-14T15:04:09.990728Z","shell.execute_reply":"2022-05-14T15:04:09.990754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds, _ = learn.get_preds(dl=test_dataloader)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:09.992125Z","iopub.status.idle":"2022-05-14T15:04:09.992471Z","shell.execute_reply.started":"2022-05-14T15:04:09.992282Z","shell.execute_reply":"2022-05-14T15:04:09.9923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds[0]","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:09.993232Z","iopub.status.idle":"2022-05-14T15:04:09.993579Z","shell.execute_reply.started":"2022-05-14T15:04:09.993375Z","shell.execute_reply":"2022-05-14T15:04:09.993392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(preds[0])","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:09.994373Z","iopub.status.idle":"2022-05-14T15:04:09.994725Z","shell.execute_reply.started":"2022-05-14T15:04:09.994527Z","shell.execute_reply":"2022-05-14T15:04:09.994546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.002241Z","iopub.status.idle":"2022-05-14T15:04:10.002592Z","shell.execute_reply.started":"2022-05-14T15:04:10.002397Z","shell.execute_reply":"2022-05-14T15:04:10.002415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Finding the class having maximum probability","metadata":{}},{"cell_type":"code","source":"class_idxs = torch.argmax(preds, axis=1)\nclass_idxs.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.003599Z","iopub.status.idle":"2022-05-14T15:04:10.004065Z","shell.execute_reply.started":"2022-05-14T15:04:10.003833Z","shell.execute_reply":"2022-05-14T15:04:10.003857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = [dls.vocab[i] for i in class_idxs]","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.004976Z","iopub.status.idle":"2022-05-14T15:04:10.005327Z","shell.execute_reply.started":"2022-05-14T15:04:10.005137Z","shell.execute_reply":"2022-05-14T15:04:10.005155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(class_idxs)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.00611Z","iopub.status.idle":"2022-05-14T15:04:10.006445Z","shell.execute_reply.started":"2022-05-14T15:04:10.006258Z","shell.execute_reply":"2022-05-14T15:04:10.006276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_images)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.009461Z","iopub.status.idle":"2022-05-14T15:04:10.009919Z","shell.execute_reply.started":"2022-05-14T15:04:10.009713Z","shell.execute_reply":"2022-05-14T15:04:10.009735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(results)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.014216Z","iopub.status.idle":"2022-05-14T15:04:10.016042Z","shell.execute_reply.started":"2022-05-14T15:04:10.015779Z","shell.execute_reply":"2022-05-14T15:04:10.01581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_idxs[:5]","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.017083Z","iopub.status.idle":"2022-05-14T15:04:10.017464Z","shell.execute_reply.started":"2022-05-14T15:04:10.017256Z","shell.execute_reply":"2022-05-14T15:04:10.017277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results[:5]","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.019632Z","iopub.status.idle":"2022-05-14T15:04:10.020404Z","shell.execute_reply.started":"2022-05-14T15:04:10.020155Z","shell.execute_reply":"2022-05-14T15:04:10.020183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Creating a dataframe containg predictions for each image","metadata":{}},{"cell_type":"code","source":"images = [img.name for img in (path/'test').ls()]\nsubmissions = pd.DataFrame(list(zip(images, results)), columns = ['filename', 'cultivar'])","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.021653Z","iopub.status.idle":"2022-05-14T15:04:10.022306Z","shell.execute_reply.started":"2022-05-14T15:04:10.022077Z","shell.execute_reply":"2022-05-14T15:04:10.022101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.027251Z","iopub.status.idle":"2022-05-14T15:04:10.028443Z","shell.execute_reply.started":"2022-05-14T15:04:10.028013Z","shell.execute_reply":"2022-05-14T15:04:10.028042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Saving the dataframe in the csv format","metadata":{}},{"cell_type":"code","source":"submissions.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T15:04:10.030437Z","iopub.status.idle":"2022-05-14T15:04:10.031122Z","shell.execute_reply.started":"2022-05-14T15:04:10.030883Z","shell.execute_reply":"2022-05-14T15:04:10.030908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}