{"cells":[{"metadata":{},"cell_type":"markdown","source":"<h2><center>So what are we cooking?</center></h2>"},{"metadata":{},"cell_type":"markdown","source":"<div style=\"background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Forestryimages_cassava.jpg/1280px-Forestryimages_cassava.jpg'); border-radius:8px\">\n    <br>\n    <center><h1 style=\"font-size: 40px\">Story Time</h1></center>    \n    <br>\n</div>"},{"metadata":{},"cell_type":"markdown","source":"<p style=\"font-size:20px\">This is not a story but more like, trying to understand what exactly is it that we are talking about?\n    \n<h2 style=\"font-size:27px\">The Cassava Plant</h2>\n\n<p style=\"font-size:20px\">Cassava originated in South America and was introduced to Africa in relatively recent times. It is known to be a very drought-tolerant crop with the ability to yield even when planted in poor soils. When cassava was first grown in Africa, it was used for subsidiary purposes though it is now considered to be one of the most important food staple crops on the continent. Its production is moving toward an industrialized system in which plant material is used for a variety of products including starch, flour, and animal feed.</p>\n\n<h2 style=\"font-size:27px\">Cassava Mosaic Disease</h2>\n    \n<p style=\"font-size:20px\">It has appeared on major news outlets and is devastating for the farmers in the East African Region</p>\n<div>\n<img src=\"https://www.pestnet.org/fact_sheets/assets/image/cassava_bacterial_blight_173/cassavabb.jpg\" style=\"width: 40%; border-radius: 8px; float:left;\">\n<br><br>\n\n<img src=\"https://videos.files.wordpress.com/0l5aq83P/cassava_bbc_world_tv_gmt_18_sept_2017_hd.original.jpg\" style=\"width: 60%;margin:10px; border-radius: 8px; float:center;\">\n    \n</div>\n\n<br>\n\n<p style=\"font-size:20px;\">Initially following infection of a cassava geminivirus in cassava, systemic symptoms develop. These symptoms include chlorotic mosaic of the leaves, leaf distortion, and stunted growth. Leaf stalks have a characteristic S-shape. Infection can be overcome by the plant especially when a rapid onset of symptoms occurs. A slow onset of disease development usually correlates with death of the plant.</p>\n\n<h2 style=\"font-size:27px\">Cassava Mosaic Disease</h2>\n\n<p style=\"font-size:20px;\">Control strategies for cassava mosaic disease include sanitation and plant resistance. In this case, sanitation means using cuttings from healthy plants to start with a healthy plot and maintaining that healthy plot by identifying unhealthy plants and immediately removing them. This strategy does not protect them from being inoculated by whiteflies, but research shows that the virus is more aggressive in plants infected from contaminated cuttings than by insect vectors. There are also specific varieties that fare better against some viruses than others, so plant resistance is possible.</p>"},{"metadata":{},"cell_type":"markdown","source":"<h1 style=\"font-size: 40px; text-align:center\">How Can We Help?</h1>\n\n<p style=\"font-size:20px;\">The problem has been discussed above which leaves us with a very important question: How can we help these people?</p>\n    \n<p style=\"font-size:20px;\">Folks at Tensorflow have a great idea. <a href=\"https://www.youtube.com/watch?v=NlpS-DhayQA\" style=\"font-size:20px; text-align:left\">Check out this video.</a> </p>\n    \n<img src=\"https://bigdata.cgiar.org/wp-content/uploads/2019/06/Pic-4-1-1024x785-1024x785.jpg\" style=\"width:54%; border-radius:8px; float:left; margin: 15px\">\n\n<p style=\"font-size: 20px; margin-left:20px\">Mostly grown as a food source in Africa, cassava is the third largest source of carbohydrates in the world. In recent times, cassava production has turned from subsistence to commercial production. CMD was first described in 1894 and is now considered one of the most damaging crop viruses in the world.\nThe Ministry of Agriculture Food Security and Cooperatives of Tanzania recommend uprooting diseased plants once every week by pulling them out by hand. Plants should be carried away from the field and exposed to the sunlight for drying and then burned to kill the viruses.</p>"},{"metadata":{},"cell_type":"markdown","source":"<p style=\"font-size:20px\">Finally, lets solve this problem by training a model.</p>"},{"metadata":{},"cell_type":"markdown","source":"# Importing Libraries"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Installing EfficientNet\n!pip install -q efficientnet_pytorch","execution_count":null,"outputs":[]},{"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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# For the Model\nfrom fastai.vision.all import *\n\nfrom efficientnet_pytorch import EfficientNet","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# For acessing the Files\nimport os","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')\ntrain = data[~data['image_id'].isin(['1562043567.jpg', '3551135685.jpg', '2252529694.jpg'])]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<p style=\"font-size:20px\">Defining some functions in order to access the files.</p>"},{"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":{},"cell_type":"markdown","source":"<p style=\"font-size:20px\">Lets finally make a function for a dataloader</p>"},{"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":{},"cell_type":"markdown","source":"<p style=\"font-size:20px\">Let's take a look these images once again:</p>"},{"metadata":{"trusted":true},"cell_type":"code","source":"dataloader.show_batch()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## The Model"},{"metadata":{},"cell_type":"markdown","source":"<p style=\"font-size:20px\">ITS TIME TO GRAB THE MODEL AND TRAIN IT!</p>"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = EfficientNet.from_pretrained('efficientnet-b2')\nlearn = Learner(dataloader, model, metrics=accuracy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"# learn.lr_find()"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fine_tune(5,0.019054606556892395)\n# The learnering rate above comes from the lr_find function.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.export(\"./export.pth\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = load_learner(\"../input/submission10cassava/export (3).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":{},"cell_type":"markdown","source":"<p style=\"font-size:20px\">That's it folks! Thank you for going through the whole thing. I shouldn't be shamelessly asking for an upvote but it might get me a job and save me from defaulting on my student loan. Hahaha. No I am kidding I go to Delhi University, we don't have student debt here.<br><br> But still. <br> Maybe? 😏😏</p>"}],"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}