How Wadhwani AI Uses PyTorch To Empower Cotton Farmers

Image for post
An image of a cotton boll infested by bollworms

Cotton is a major fibre crop across the world, cultivated in over 80 countries with nearly 100 million families across the world rely on cotton farming for their livelihood. With such importance placed on many farmers’ crops, cotton’s particular vulnerability to pest infestations has been troubling to many. However, pest infestation is also simultaneously one of the most significant and preventable problems that farmers face with 55% of all pesticide usage in India being devoted to cotton farming.

AI for Pest Monitoring

Image for post

An image of a pheromone trap

At Wadhwani AI, we are dedicated to applying AI to solve problems for social good. When it comes to combating and preventing crop attacks, pest traps can be used as an early warning system, with the number of pests captured used as a proxy for infestation. By applying deep learning on trap images, Wadhwani AI is able to provide detailed spray recommendations to farmers in order to proactively address specific pest issues.

By using PyTorch, Wadhwani AI researchers have been able to create a model that is able to accurately predict the location of pests within cotton crops. Once an image is captured, it passes through a multi-task network which verifies whether the image is valid. If it is, the detection branch provides the potential locations of the detected pests. The final recommendation is given based on the number of pests detected, and rules laid out by entomologists.

The overall process is outlined with further detail below:

Image for post
Figure 1: The entire process flow for our pest monitoring solution

Another advantage PyTorch is able to offer is through its ability to use offline inference. As many smallholder farms are located in rural areas, many farmers’ access to network coverage is poor and limited. To help improve user experience, Wadhwani AI is able to deploy their models by using model compression algorithms to reduce their model size by 98% from 250MB to less than 5MB. A recent paper by our team published in KDD 2020 talks about our journey, the challenges we faced and what we learnt.

Building the Machine Learning Solution

Image for post

Figure 2: The list of various tools that we use for different components of our pipeline

Our work is enabled by a host of tools, particularly, PyTorch for developing and deploying our models, and Weights & Biases for experiment tracking and communicating results to relevant stakeholders. A comprehensive summary of all the tools we use has been shown below.

The Multi-task Model

The core of our solution is object detection. Broadly, object detection models can be divided into two categories, two-stage and single-stage detectors. Two-stage architectures are generally more accurate than single-stage approaches, albeit slower. As inference speed is paramount to our user experience, we use a single-stage approach.

Single Shot MultiBox Detector (SSD) was one of the first single-stage detectors to give comparable performance to its two-stage counterparts while being fast. We faced a significant challenge as our dataset is very different from standard object detection datasets. Our images are of poorer quality as they are captured from smartphones in remote villages. The pests often tend to cluster together, often making it hard to identify boundaries. These boundaries — like limbs and wings — which are the key features for classification, end up not being present in the input image.

Image for post

Figure 3: A few of the challenges when dealing with real world data are highlighted in the image above

The objective function used to train SSD is called MultiBox loss. It consists of a localization and a classification component to account for the tightness of the bounding box and the accuracy of the predicted class respectively. This is where experiment tracking comes in very handy to understand how different aspects of the loss functions are getting affected by changes in optimization, data augmentation, network architecture and input size. For example, it can be seen from the image below that the overall loss (train/loss) is always dominated by the classification loss (train/loss_c) and not the localization loss (train/loss_l).

Image for post

Figure 4: SSD Loss function curves on Weights & Biases dashboard

Another way to observe the evolution of model performance is by visualizing how the predictions evolve over time. Weights & Biases helps us do this as we can track how the predicted bounding boxes change over time as shown below:

Image for post

Figure 5: Visualizing the evolution of bounding box predictions on Weights & Biases

Upon deploying our model in the field, we found that it was common for users to submit images that did not contain any pest traps. Deep learning models are known to perform poorly on out-of-distribution images, often being overly confident about its mispredictions. To explicitly tackle this issue — and after utilizing various different approaches — a separate classifier was trained to reject outliers for a wide variety of out-of-distribution images. Further, a VGGNet model was trained to use images from the COCO dataset as the invalid class and images from our pest dataset as the valid class.

However, during inference, the image had to pass through two networks, significantly increasing the inference time. Since both the image validation and object detection models used the same base network (VGGNet) which accounted for the majority of the inference time, we combined them together by adding a new branch from the last layer of the base network in SSD for image validation so that we have a single model. The network was jointly trained in a multi-task learning setup with the detection and validation heads being trained by the MultiBox loss and cross-entropy respectively.

Image for post
Figure 6: Illustration of our machine learning model

Model Evaluation

The most common approach to evaluating object detection networks is by using Average Precision (AP) per class. However, the relationship between AP and the goal of this solution is not always straightforward. AP is defined using a range of confidence thresholds for each class whereas, during deployment, we need to select a single operating point. We treat the final pesticide spray recommendation task as a binary classification problem where the classes indicate whether to spray. Consequently, we redefine false positives and false negatives to enable clearer communication with the relevant stakeholders, who, generally, find it hard to interpret precision and recall, as the following:

  • Missed alarm rate (MAR): percentage of cases where the recommendation should have been to spray, but the system suggested otherwise.
  • False alarm rate (FAR): percentage of cases where no action should be taken, but the system suggested spraying.

We set a goal of achieving < 5% MAR and FAR as both false positives and false negatives are harmful to the farmer. Hyperparameter tuning and the optimal confidence threshold selection for deployment was done towards this joint objective.

We specifically found the DataFrame plot within Weights & Biases to be extremely helpful in understanding the predictions at an image level during inference.

Image for post

Figure 7: Comparing ground truth vs prediction on an image using “dataframe” plot on Weights and Biases

It helps us visually compare the predicted and ground truth bounding boxes for any image, while also allowing us to sort, group and filter data as per our requirement. For example, the predictions can be sorted based on the confidence score to qualitatively evaluate low confidence input data.

The Parameter Importance plot in Weights & Biases helps to identify the most impactful hyperparameters with respect to the val loss.

Image for post

Figure 8: The Parameter Importance plot within Weights & Biases shows the correlation of different parameters with a given metric

Model Compression

Even though the primary motivation for model compression is deployment on-phone, it proves beneficial for deployment on-cloud as well since it runs significantly faster. We use iterative pruning (as outlined in this paper) and quantization to reduce our model size from 265MB to 5MB. The paper introduces the following technique for classification networks.

Image for post

Figure 9: Illustration of pruning convolutional layers

The figure above shows how one filter is pruned. Let’s look at two consecutive layers, L and L+1. The number of input and output channels for layer L is K and respectivelySince the output of layer L is the input to layer L+1, the number of input channels for layer L+1 is also N, while the number of output channels is M. Pruning the filter at index i from layer L would reduce the number of output channels of layer L, and hence, the number of input channels in layer L+1, by one. Thus, for pruning the chosen filter, we also need to update all the layers with layer as their input. We modify this technique to work with our multi-task architecture, reducing 1024 filters at each pruning iteration, for 15 iterations. Refer to this blog post for more context and details.

Model Deployment

To deploy models in production, we first convert the PyTorch model to a TorchScript module. Torchscript serializes the model and makes it independent of any Python dependency. We mix scripting and tracing for our model since certain parts of the model have a control flow which is data dependent.

Image for post

Figure 10: Illustration of our workflow to prepare a PyTorch model ready for serving

Deployment On-Cloud

We use TorchServe for serving our model. It has a built-in web server which takes a model and serves it as a REST API request. It also allows us to configure the model for batched predictions easily. The torchscript module is packaged in a model archive (.mar) file, required for TorchServe, which contains the entry point and workflow for executing an inference request. Finally, the TorchScript module and the mar file, both, are uploaded to an s3 bucket. We built an internal framework for maintaining model registry and serving. Each model, ready for production, is added to the model registry with all metadata associated with it. The framework uses these artifacts to then serve the model in an AWS EC2 instance with TorchServe.

Deployment On-Phone

To deploy our model on smartphones, we use PyTorch Mobile. We build an SDK around it which fetches the artifacts from the model registry and enables us to efficiently deploy multiple models on-device. The application is able to do inference without requiring any internet connection. It queues all the data required for logging and syncs it back to the server in background when the mobile is connected to the network.

Best Practices

To ensure such experiments are replicable, please take into consideration the following:

  • Containerization: Docker has been widely used to ensure that all the dependencies required for replicating an experiment are packaged within a single contained environment.
  • Data versioning: As datasets continue to evolve over time, train/val/test splits cannot be static. Explicitly version data to take snapshots of samples used to train any given experiment. For now, we store the splits explicitly in a data version file. In the future, we are looking forward to migrating to Artifacts by Weights & Biases.
  • Experiment versioning: We store all our hyperparameters for each experiment in a config file along with the Git commit ID and the specific seed.

Our solution is going to be used by more than 10,000 farmers across India in the coming season and we hope that this encourages more people to creatively apply AI to solve societal problems.


Image for post

Aman Dalmia

Aman is a Research Fellow at Wadhwani AI. At Wadhwani AI, he has been working on AI for early detection of pest infestation, identifying malnutrition in neonates using a smartphone video and using cough sounds to screen for COVID-19. Aman graduated with a bachelor’s degree in Electronics and Communication engineering from the Indian Institute of Technology, Guwahati.

Image for post

Vishal Agarwal

Vishal is an ML Engineer at Wadhwani AI. He has been working on AI for early detection of pest infestation and screening tools for Tuberculosis. Currently he is focusing on MLOps with developing and adapting best practices that enable us to build scalable machine learning systems. Vishal has an interest in Systems and ML and has an academic background in Electronics and Electrical Engineering.

About Wadhwani AI
The Wadhwani Institute for Artificial Intelligence is an independent, nonprofit research institute and global hub, developing AI solutions for social good. We harness the power of modern artificial intelligence to meet global challenges. Our mission is to develop and apply AI-based innovations and solutions to a broad range of societal domains including healthcare, agriculture, education, infrastructure, and financial inclusion.

481 thoughts on “How Wadhwani AI Uses PyTorch To Empower Cotton Farmers”

  1. I’m loving this app. Fun and pretty challenging to figure out how to keep the date going. My only problem I have is that some of the options don’t even work or when you use it at the right time you still lose. I see that this app was not updated since November 2016, but if they can actually do an update to fix all these things, then for sure it gets 5 stars from me Its like my candy love with for life? Kaleidoscope dating sim. Find a new episodes regularly and again and again. I want to know some games, 239, flirt with dating games like my candy love life? Saying virtual girlfriend – rich man looking for online dating sim dating and again and meet? Things to return any other dating experts. Lady popular in workspace design. Scramble Words is a free online word scramble game, with many exciting twists and turns!\r\n\r\nScramble Words is a word-making game. The goal is to unscramble the list of letters found at the bottom of the screen, using your keyboard or mouse to spell out words. As you’re unscrambling, keep in mind that scoring —the goal of the game — is based on the length of the words you spell, the difficulty of the letters used, and how quickly you find and spell each word. Up the difficulty and you’ll be rewarded handsomely!

  2. This is some agreeable stuff. It took me a while to find this internet site but it was worth the time. I noticed this content was buried in yahoo and not the first spot. This site has a ton of helpful material and it doesnt deserve to be burried in the searches like that. By the way I am going to add this site to my favorites.

  3. Pingback: black dating for free login

  4. Кыргызстан Добрый день! Подскажите пожалуйста,у вас в клинике есть такая процедура как лазерная терапия при аденоидах? Если да,то какова цена процедуры?! Как выбрать самый лучший женский возбудитель. Найти действительно эффективный препарат, который поможет в конкретной ситуации, не так уж и просто. Его пары и приятный запах позволяют расслабиться и настроиться на интимный лад. Цена на женский возбудитель в каплях в аптеках Москвы — 990 руб. по акции, без скидки возможно купить за 1 980 руб. \nГрудные дети при очень тяжелом течении заболевания могут быть неспособны принимать пищу или питье, у них могут наблюдаться также потеря сознания, гипотермия и судороги.–39183076 3. Иммуновет 1 Ин расфасовывают по 2,0 см3 в стерильные ампулы или по 2,0, 6,0, 10,0 см3 во флаконы. Ампулы герметично запаивают, а флаконы закрывают резиновыми пробками и обкатывают алюминиевыми колпачками. Просто добавьте все нужные товары в корзину. На втором шаге оформления вы сможете увидеть все ближайшие аптеки на карте, где они будут в наличии по самым выгодням ценам. Заполните форму для консультации и заказа УльтраДжи возбуждающие капсулы для женщин. Оператор уточнит у вас все детали и мы отправим ваш заказ. Через 1-10 дней вы получите посылку и оплатите её при получении

  5. SQL is the language made use of to develop, upgrade, as well as change a data source– pronounced both as ‘Se-quell’ and also’S-Q-L’. Before beginning with SQL, let us have a brief understanding of DBMS. In easy terms, it is software application that is used to produce as well as handle databases. We are going to stick to RDBMS in this article. There are likewise non-relational DBMS like MongoDB used for huge data evaluation. There are various accounts like information analyst, database manager, and also data architect that call for the knowledge of SQL. Besides guiding you in your interviews, this post will likewise give a basic understanding of SQL.
    I can likewise recommend “TOP 30 SQL Interview Coding Tasks” by Matthew Urban, actually terrific book when it pertains to one of the most typical SQL coding interview inquiries. This error generally shows up because of syntax mistakes on-call a column name in Oracle database, observe the ORA identifier in the error code. Make certain you key in the proper column name. Also, take special note on the pen names as they are the one being referenced in the mistake as the invalid identifier. Hibernate is Object Relational Mapping device in Java.

  6. Paypal on jo varsin tunnettu verkkolompakko, joka tunnetaan useissa maissa. Se on melko laajalle alalle levinnyt, ja joissain tapauksissa sinne voi ottaa vaikka palkkansa vastaan tai asettaa suoraveloituksella maksuja menemään. Myös luonnollisesti Paypalista toiseen on helppo siirtää rahaa ja ne siirtyvätkin lähes välittömästi. Omalle pankkitilillekin rahan siirtäminen sujuu vaivattomasti vain muutamalla näpäytyksellä. Voimmekin siis varmasti yhdessa todeta PayPal Casinon olevan turvallinen maksutapa, jopa turvallisempi kuin luottokortin kayttaminen. Kunhan varmistat nettikasinon laadun esimerkiksi sen kasinolisenssin perusteella, vot huoletta kayttaa PayPalia myos nettikasinoiden rahaliikenteen suorittamiseen. Plage Terrasses Soleil is 500 metres from the accommodation. The nearest airport is Beziers Cap d’Agde Airport, 24 km from CAMPING NARBONNE PLAGE 4*: MOBIL HOME PREMIUM 33 M? A 200M DE LA PLAGE ET CENTRE VILLE. Netticasinot?? » Parhaat uudet bonukset | Loyda Netticasino Parhaat ja uudet kasinotarjoukset & kasinoarvostelut | Casino. Poker, ruletti, Blackjack? Ehka laheisella kasinolla: Des d’Activitat Sant Cugat us oferim la possibilitat de poder consultar, comparar i triar les activitats que mes s’ajusten a les vostres necessitats.

  7. Your place is valueble for me. Thanks!?This web page is known as a stroll-by means of for all the information you needed about this and didn’t know who to ask. Glimpse here, and you’ll definitely uncover it.Have you considered promoting your blog? add it to SEO Directory right now 🙂

  8. I was looking over the internet for some information since yesterday night and I at last found what i was looking for! This is a wonderful website by the way, although it seems a little bit off place on my smart phone.

  9. [url=]robaxin 1000[/url] [url=]viagra usa pharmacy[/url] [url=]buy plaquenil 100mg[/url] [url=]vermox price in south africa[/url]

  10. [url=]viagra 500mg price in india[/url] [url=]cialis 1[/url] [url=]buy viagra in canada[/url] [url=]generic flomax price[/url] [url=]no rx avana[/url]

  11. [url=]sildenafil 100g[/url] [url=]canadian viagra online[/url] [url=]tadalafil 2.5 mg tablets[/url] [url=]ordering viagra from india[/url] [url=]best viagra pills in india[/url] [url=]cialis 20mg professional[/url]

  12. [url=]order tadacip 20 mg[/url] [url=]sildenafil 100mg tab[/url] [url=]cialis professional[/url] [url=]stromectol 3mg tablets[/url]

  13. [url=]how to buy cialis cheap[/url] [url=]generic cialis 20mg uk[/url] [url=]wellbutrin 30 mg[/url] [url=]sildenafil pills buy[/url] [url=]buy sildenafil india online[/url] [url=]sildenafil online australia[/url] [url=]where can i buy kamagra in south africa[/url]

  14. [url=]tadalafil prescription price[/url] [url=]where can i buy cialis online in canada[/url]

  15. [url=]cialis 20mg canada[/url] [url=]tadalafil 2.5 mg online india[/url] [url=]viagra from canada online[/url] [url=]how to order sildenafil online[/url] [url=]stromectol uk[/url] [url=]generic daily cialis online[/url] [url=]tadalafil buy canada[/url] [url=]wellbutrin 65 mg[/url] [url=]dapoxetine 60 mg india[/url] [url=]cheap cialis 5 mg[/url]

  16. I admire the beneficial data you offer in your articles or blog posts. Ill bookmark your blog and have my kids check up right here usually. Im very sure they will learn lots of new things here than anybody else!

  17. Conrad Schormann, who has covered the cheating crisis as news editor of, notes that Petrosian did not appear to get help on every move, making the suspicious behaviour even harder to spot. “In his games there were abnormalities, sequences that he played godlike, but there were blunders as well,” he said. Search They have plenty of passion for chess and they now have a way to encourage that passion in others. The trio recently opened the Manasota Chess Center, a venue for scholastic and competitive chess games, in the Crossings at Siesta Key  on June 6. For more information please visit our Privacy Policy and Cookie Policy. For more ?? visit the gallery. Add a whiteboard alongside any call in seconds – Hangouts, FaceTime, or a phone call. Fellow scribblers won’t need an account or subscription. Thanks for the suggestion though. If you use the whiteboard within Teams, some of your options are limited. But for the purposes of Pictionary they should be more than adequate. If you choose to open whiteboard in the native app, you’ll have more variety in terms of colors, line widths and other drawing tools. Some more artistically inclined players may realize they can use the whiteboard app to draw on their tablet using a stylus. Such players may enjoy this method because they can showcase their talents. Most, however, will be happy with using their mouse – or touchscreen monitor, if available – to attempt to produce crude likenesses of people, places or things.  

  18. [url=]sildenafil us pharmacy[/url] [url=]tadalafil tablets sale[/url] [url=]cialis 20mg price in india[/url] [url=]female viagra[/url] [url=]tadalafil 20mg[/url] [url=]cialis online pharmacy[/url] [url=]where can i buy viagra pills online[/url] [url=]buy real cialis[/url] [url=]sildenafil 50mg price pharmacy[/url] [url=]cialis daily best price[/url]

  19. Williamvon