<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"><channel><title>Learn Data Science using Python</title><description>This blog is dedicated to all aspiring data scientists. I will share my learning about Data Science, Machine Learning, Deep Learning, OCR, and Computer Vision with my blog posts. So keep reading my posts and be a better version of yourself. Contact me for any AI-related freelance or consultancy work.</description><managingEditor>noreply@blogger.com (Learn Data Science with PrateekG)</managingEditor><pubDate>Mon, 19 Oct 2026 13:17:14 +0530</pubDate><generator>Blogger http://www.blogger.com</generator><openSearch:totalResults xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">66</openSearch:totalResults><openSearch:startIndex xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">1</openSearch:startIndex><openSearch:itemsPerPage xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">25</openSearch:itemsPerPage><link>https://dsbyprateekg.blogspot.com/</link><language>en-us</language><itunes:explicit>no</itunes:explicit><itunes:subtitle>This blog is dedicated to all aspiring data scientists. I will share my learning about Data Science, Machine Learning, Deep Learning, OCR, and Computer Vision with my blog posts. So keep reading my posts and be a better version of yourself. Contact me for</itunes:subtitle><itunes:category text="Technology"><itunes:category text="Tech News"/></itunes:category><itunes:owner><itunes:email>noreply@blogger.com</itunes:email></itunes:owner><item><title>How to CORRECTLY convert Darknet/Yolov4 weights to TFLite format?</title><link>https://dsbyprateekg.blogspot.com/2025/02/how-to-correctly-convert-darknetyolov4.html</link><category>Deployment</category><category>mobile</category><category>tflite</category><category>YOLOv4</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Fri, 7 Feb 2025 16:41:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-7947561243334083545</guid><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgE1Qy3_iJD1bQ7vzt69WURYsngKT96sUCGxmswR4lv3nQF2nnity-3TlySWvidKHRocyTB5lXp_pRccb5CYV1OyI54UvXZVOHCYGgTOjOgHd_OlJizMp4hof11K8UaW0Asw9ijNQ6u5c4NA7sbDKN3FhBtk8Lz9E0SS_E9-s0kRV_bBaF2n8AyKjmQITWa" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="318" data-original-width="950" height="134" src="https://blogger.googleusercontent.com/img/a/AVvXsEgE1Qy3_iJD1bQ7vzt69WURYsngKT96sUCGxmswR4lv3nQF2nnity-3TlySWvidKHRocyTB5lXp_pRccb5CYV1OyI54UvXZVOHCYGgTOjOgHd_OlJizMp4hof11K8UaW0Asw9ijNQ6u5c4NA7sbDKN3FhBtk8Lz9E0SS_E9-s0kRV_bBaF2n8AyKjmQITWa=w400-h134" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;Greetings, computer vision practitioners&#128110;! I hope you're all doing well in your projects. Recently, I received a new requirement from a client regarding an object detection solution that had been successfully deployed as a REST API. The solution was seamlessly integrated into both web and mobile applications. However, the client encountered network reliability issues, particularly in rural areas where poor connectivity occasionally caused the solution to fail on mobile devices&#128148;.&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;As we know, network disruptions are a common challenge, but they become critical when they impact business operations. My client sought a robust solution to address this issue, ensuring uninterrupted functionality regardless of network conditions. In this blog post, we will explore how I tackled this challenge and implemented an effective solution&#128147;.&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;b&gt;The Solution: Enabling Offline Object Detection for Mobile Devices&lt;/b&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;Given that network connectivity issues are beyond our control, the most effective solution I proposed was to implement an offline model directly within the mobile application&#128165;. Instead of relying on a cloud-based or on-premise Rest API, the object detection model would run natively on the device, allowing it to function seamlessly without an internet connection&#128163;.&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;Sounds interesting&#128129;? Absolutely! Since my client was using a Darknet/YOLOv4-based model, the next step was to convert it into a format optimized for offline deployment on mobile devices. For both Android and iOS, the most suitable format is TensorFlow Lite (TFLite), previously known as LiteRT. In the next section, we will dive into the process of converting the YOLOv4 model to TFLite and integrating it into the mobile application&#128170;.&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;b&gt;Converting Darknet/YOLOv4 Weights to TFLite Format:-&amp;nbsp;&lt;/b&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both;"&gt;In this section, we will explore the step-by-step process of successfully converting Darknet/YOLOv4 weights to TFLite format&#128526;. For demonstration purposes, I will be using a Kaggle Notebook, but you are free to use any environment of your choice for the conversion.&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;It is important to note that Darknet/YOLOv4 weights cannot be directly converted to TFLite (formerly LiteRT). The conversion process involves an intermediate step:&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;1. First, we convert the Darknet model to the ONNX format, which is an open standard for machine learning model interoperability.&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;2. Then, we convert the ONNX model to TFLite, making it optimized for mobile deployment.&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;To begin the conversion, we will clone the required &lt;a href="https://github.com/lordofkillz/yolo4_pytorch" rel="nofollow" target="_blank"&gt;GitHub repository&lt;/a&gt; (and if you find it useful, don’t forget to give it a star ⭐ on GitHub!). Let’s get started!-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjaIkEvGtlVXlL8-SQKNcz1cn-mN-G1qGQJrBDt_slO7Q66eDhybZqvjT0PI8UgEbrOg3Lne0k4M-bbWPfpdhtQfvPxKnVO255d2UddjruM2EHRRMwhHOu2HguSOX1zRmH_m-uOYRBUurZMWR02uAP0ugz3q_5HWsonHqefyRrS-_fnGQWbIne1u10p_jjZ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="300" data-original-width="852" src="https://blogger.googleusercontent.com/img/a/AVvXsEjaIkEvGtlVXlL8-SQKNcz1cn-mN-G1qGQJrBDt_slO7Q66eDhybZqvjT0PI8UgEbrOg3Lne0k4M-bbWPfpdhtQfvPxKnVO255d2UddjruM2EHRRMwhHOu2HguSOX1zRmH_m-uOYRBUurZMWR02uAP0ugz3q_5HWsonHqefyRrS-_fnGQWbIne1u10p_jjZ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;Next, we will install the following required package for onnx conversion-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEiO6HYzQJt0io8fwrnObN7y--z81S3YzUB78uaaLE7Gxgdwa6bHr6Xc9v3l5FxtSVE7EkU7zZiCDuh4NANGup6ZstdpjEdbWVokbnsrMRt8CrwbtmPw3avSCnORn2_XLcyXhvVIJj8zfc0DH-F7iSakBs72SLBfuM358p1kD6NkmFfR8V_UNp6qX38SBwnB" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="61" data-original-width="281" height="69" src="https://blogger.googleusercontent.com/img/a/AVvXsEiO6HYzQJt0io8fwrnObN7y--z81S3YzUB78uaaLE7Gxgdwa6bHr6Xc9v3l5FxtSVE7EkU7zZiCDuh4NANGup6ZstdpjEdbWVokbnsrMRt8CrwbtmPw3avSCnORn2_XLcyXhvVIJj8zfc0DH-F7iSakBs72SLBfuM358p1kD6NkmFfR8V_UNp6qX38SBwnB" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;After installing the above package, we will run the following conversion command from the yolov4_pytorch directory-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEg9OtjZVOdysvdKTneMYyyyxN1ZJY0P9dxq-KNDL0Qcay0IPA8YyHR6lbB7CVJtqQgcNs_R5rpWvHFRCF2sjEtM1pTkMcZoeC9TQyUiwo4p5Ch1y5RNM3ADVRSA8CsHhDsPHIAzP6sRnRY2MazIgp1ERFnnOQlMYkD8m2HuRZr1146EiV6B5_TPyEPEEyDn" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="120" data-original-width="1081" src="https://blogger.googleusercontent.com/img/a/AVvXsEg9OtjZVOdysvdKTneMYyyyxN1ZJY0P9dxq-KNDL0Qcay0IPA8YyHR6lbB7CVJtqQgcNs_R5rpWvHFRCF2sjEtM1pTkMcZoeC9TQyUiwo4p5Ch1y5RNM3ADVRSA8CsHhDsPHIAzP6sRnRY2MazIgp1ERFnnOQlMYkD8m2HuRZr1146EiV6B5_TPyEPEEyDn=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;b&gt;Understanding the ONNX Conversion Command:-&lt;/b&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;In the command above, I have specified the paths for the Darknet/YOLOv4 configuration file, object names file, and weight file, along with an example image. This image is used to visualize the object detection results by drawing bounding boxes after converting the model to the ONNX format:&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEi2rilx6Blik7d1_JJ8CufvoGOwcQC1v2dyu5QHbXYFNhr07UJE1huph3QiMppqtMuDVznSFtOt7ZtefkpfGy9GpFVjfX3PbWkfTH5CdOjdCwi8Q-sEO_iOhNzy71yJdDIV1zh89W-ng6S1o29dN8NXnplqrUyMMCGOmDRzjWDRIjC35BBWGMHF_CN0_wDg" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="576" data-original-width="768" height="240" src="https://blogger.googleusercontent.com/img/a/AVvXsEi2rilx6Blik7d1_JJ8CufvoGOwcQC1v2dyu5QHbXYFNhr07UJE1huph3QiMppqtMuDVznSFtOt7ZtefkpfGy9GpFVjfX3PbWkfTH5CdOjdCwi8Q-sEO_iOhNzy71yJdDIV1zh89W-ng6S1o29dN8NXnplqrUyMMCGOmDRzjWDRIjC35BBWGMHF_CN0_wDg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;b&gt;Customizing and Verifying the ONNX Conversion:-&lt;/b&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;You can replace the specified configuration, object names, and weight files with your own as needed. Upon executing the command, the conversion process begins, and the output image is saved with predicted bounding boxes using the ONNX model&#128171;. This allows us to verify whether there are any discrepancies between the original Darknet/YOLOv4 model and the converted ONNX model.&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;At the end of the logs, you will see messages similar to the following:&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgBhZrVf-6ICz43ZR1wTE-X5zp0OOk6wDwFdfxm-GC9t-ITVv8m9_fDXRs_TsTOG5TjwRI0uKi7Ki3oW3OJpN_OEy-kzD6HJJn2aUKNyhL7amN5onkDodeDS_f_lLc7A_smZJaPCcwdthHQDfPPrivLEKdGRRM6rFte3e83EqZzguOUq2yDU2PDLOHTDHav" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="322" data-original-width="520" src="https://blogger.googleusercontent.com/img/a/AVvXsEgBhZrVf-6ICz43ZR1wTE-X5zp0OOk6wDwFdfxm-GC9t-ITVv8m9_fDXRs_TsTOG5TjwRI0uKi7Ki3oW3OJpN_OEy-kzD6HJJn2aUKNyhL7amN5onkDodeDS_f_lLc7A_smZJaPCcwdthHQDfPPrivLEKdGRRM6rFte3e83EqZzguOUq2yDU2PDLOHTDHav=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Proceeding to TFLite Conversion:-&lt;/b&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both;"&gt;As seen in the logs above, the converted ONNX model successfully detects and predicts objects such as the bicycle, truck, and dog, confirming that the conversion process was accurate&#128152;.&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;Now, we can proceed with converting the ONNX model to TFLite format. For this step, we will utilize a highly efficient &lt;a href="https://github.com/PINTO0309/onnx2tf" rel="nofollow" target="_blank"&gt;GitHub repository&lt;/a&gt; specifically designed for ONNX-to-TFLite conversion. I highly recommend giving this repository a ⭐ on GitHub, as it provides a seamless and reliable way to convert almost any ONNX model into TFLite format. Let’s dive into the conversion process!&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEglX6ELor1j-WQhwyPrx236bykcwM3V17LzJVutRfGKx1CVmVS5H19Z6bb2REQ3iHu0PO2nxuhppn8W34pzaiKfZ0TbGqZ1oOInkCbVcKwFXP_kCssDujrxn3Bz_PwN_eJu4B9NJ472SR6eYTWdHsd8MXWnpsBrmqNyfl-cVkmX3MHlNTTP99pZpdb3kPxR" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="298" data-original-width="833" src="https://blogger.googleusercontent.com/img/a/AVvXsEglX6ELor1j-WQhwyPrx236bykcwM3V17LzJVutRfGKx1CVmVS5H19Z6bb2REQ3iHu0PO2nxuhppn8W34pzaiKfZ0TbGqZ1oOInkCbVcKwFXP_kCssDujrxn3Bz_PwN_eJu4B9NJ472SR6eYTWdHsd8MXWnpsBrmqNyfl-cVkmX3MHlNTTP99pZpdb3kPxR=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we need to install also few supportive libraries one by one-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjcehDMSHBudlLOTgP0B0C_5nYc11IRt-aTwk9TuL_7XQdWUZdXXfasIwCgAjaejIE1nofwZkZUamhOsIyauxv5-W83Q_e02L_jVx8kAbL_POMZyjsDYFwHDgMRMRi53Beprg-kbxuFXoCtypFgQgR7X9lbeUQUYEhjJ_wkAcoPwxkgoeMbBv6b2Oa37gLf" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="62" data-original-width="325" height="61" src="https://blogger.googleusercontent.com/img/a/AVvXsEjcehDMSHBudlLOTgP0B0C_5nYc11IRt-aTwk9TuL_7XQdWUZdXXfasIwCgAjaejIE1nofwZkZUamhOsIyauxv5-W83Q_e02L_jVx8kAbL_POMZyjsDYFwHDgMRMRi53Beprg-kbxuFXoCtypFgQgR7X9lbeUQUYEhjJ_wkAcoPwxkgoeMbBv6b2Oa37gLf" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEiFpSt9ZZc6Mz4Zii9ShTmmOK_j-4DfB9vPSAspK9A_6E47N_teDFVGQdTFdguIZ5zzRm7GdDdP_tSQ1jmzVxTL0H2msAgKAT9pOBx4dywFkM5hg6q3k2QkSrXkMb28MwINRNXPrqqGyHdsKvCYvnYiZLcgrYQwwfFlVIzoVJG4B8BlOC1oLp2QZhiavdAP" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="57" data-original-width="262" height="70" src="https://blogger.googleusercontent.com/img/a/AVvXsEiFpSt9ZZc6Mz4Zii9ShTmmOK_j-4DfB9vPSAspK9A_6E47N_teDFVGQdTFdguIZ5zzRm7GdDdP_tSQ1jmzVxTL0H2msAgKAT9pOBx4dywFkM5hg6q3k2QkSrXkMb28MwINRNXPrqqGyHdsKvCYvnYiZLcgrYQwwfFlVIzoVJG4B8BlOC1oLp2QZhiavdAP" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjZ1vir7nIM1sAbCf3vAtVL25uytlAzvtG42RqdEOp3ofBuguho35eFcMpzCG0Bjl2nTXXS8bXBy3cSDLLfZZfXkwBeoxlEJa8eiIrpaAE04pTF7ZZ_tPL-rTSPt8pOoT3zdhtGvgH_dYCXP9Gvhwx8BmRoe4dkhCeiQ-Nxj3J5kWTkBNyP7l_0G8sdWCj6" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="56" data-original-width="218" height="82" src="https://blogger.googleusercontent.com/img/a/AVvXsEjZ1vir7nIM1sAbCf3vAtVL25uytlAzvtG42RqdEOp3ofBuguho35eFcMpzCG0Bjl2nTXXS8bXBy3cSDLLfZZfXkwBeoxlEJa8eiIrpaAE04pTF7ZZ_tPL-rTSPt8pOoT3zdhtGvgH_dYCXP9Gvhwx8BmRoe4dkhCeiQ-Nxj3J5kWTkBNyP7l_0G8sdWCj6" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;Next, we will run the following command to start the tflite conversion-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjkTnp8aXtDKGA8NXIoxzkuLw2GCdd-7NaNVXzhsb_wmXQEnDytncKMsjwxNbSHcf47XvvjSkZuKdDkbvB0ufiDlz30LL9c8xb-xTOw4w3N196ab92UKhl00ZG8m5cbcGmuJ0fviYwsO9qoPjUWj4PyAHo0ldVL3easdt1Ehnc6HVqDomNPsUDYXXQtrFmJ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="55" data-original-width="782" src="https://blogger.googleusercontent.com/img/a/AVvXsEjkTnp8aXtDKGA8NXIoxzkuLw2GCdd-7NaNVXzhsb_wmXQEnDytncKMsjwxNbSHcf47XvvjSkZuKdDkbvB0ufiDlz30LL9c8xb-xTOw4w3N196ab92UKhl00ZG8m5cbcGmuJ0fviYwsO9qoPjUWj4PyAHo0ldVL3easdt1Ehnc6HVqDomNPsUDYXXQtrFmJ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;b&gt;Executing the ONNX to TFLite Conversion:-&lt;/b&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;In the command above, we simply need to specify the path to the ONNX model. Once executed, the script will handle all the complex computations required for the conversion. Upon completion, it will generate Float16 and Float32 TFLite models, which will be stored inside the saved_model folder.&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEg79lCpbLHYOlcMr0CGpvuW95ro8G056iJtvOkLn6ZE01SzxiYaaejrNQx9jKiqJbj8V1pEdfmRLwLpd77X63qyQF3nc5Ux_YZ2v2eecIfxCyVZayDAzYdF3umYrQyjGcdP6WPFts08-4-Pg8JJ1vEFR_vWrIIh4DH58WvJ41L1N1ioM_xpXyxs_a6Jvwtx" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="362" data-original-width="287" height="240" src="https://blogger.googleusercontent.com/img/a/AVvXsEg79lCpbLHYOlcMr0CGpvuW95ro8G056iJtvOkLn6ZE01SzxiYaaejrNQx9jKiqJbj8V1pEdfmRLwLpd77X63qyQF3nc5Ux_YZ2v2eecIfxCyVZayDAzYdF3umYrQyjGcdP6WPFts08-4-Pg8JJ1vEFR_vWrIIh4DH58WvJ41L1N1ioM_xpXyxs_a6Jvwtx" width="190" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;b&gt;Model Size and Verifying Predictions:-&lt;/b&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;If you check the size of the Float16 TFLite model, it is approximately 11 MB, significantly smaller than the original YOLOv4-Tiny model, which is 23 MB&#128165;. This reduction in size is crucial, as it ensures efficient deployment within a mobile application.&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;Next, we need to verify whether the converted TFLite model produces the same predictions as the original model. To do this, we can either write a Python inference script or collaborate with an Android developer to implement it directly within the mobile application&#128119;.&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;Below, I am sharing my Python script for running inference with the converted model.&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;We can use the OpenCV library to load the tflite model directly as below-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEi_1WQsy4whIW0DzNWJBWobC3iRtVOg7iszPAKI68jTKoT-wGlHQJBJKDEaFZ31cZoHllWF9FqLNn3HH0yAsMtQmZjf37bQ4ftdSwJ_2NLouz0FOMC_hnfDGNWyx4FjjBfG9v16iXWZZOCvCdiWQg9Sp3vVKs31tVPVmsgvjy26fB9UUaV8exe8VY-IRVoz" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="593" data-original-width="1087" src="https://blogger.googleusercontent.com/img/a/AVvXsEi_1WQsy4whIW0DzNWJBWobC3iRtVOg7iszPAKI68jTKoT-wGlHQJBJKDEaFZ31cZoHllWF9FqLNn3HH0yAsMtQmZjf37bQ4ftdSwJ_2NLouz0FOMC_hnfDGNWyx4FjjBfG9v16iXWZZOCvCdiWQg9Sp3vVKs31tVPVmsgvjy26fB9UUaV8exe8VY-IRVoz=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we will apply some pre-processing-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhzaRTCkb07dvbxRrXzGgPTgrX8U3aBrGVIXdI3HBgRvc2cri1THCvvmWphL1Lr7yDJoFoHxmxiKbtTRCi9MXCOU67Cpqj3BzrnyjFtmH-fk03JaW05USnr3F9J2IbYA3Mszu1tWjjrB-eNLJ_6GQ8kTTTcQutfLX5n_BsGlCvloje8JarxaFRqzVEJhlZf" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="685" data-original-width="1052" src="https://blogger.googleusercontent.com/img/a/AVvXsEhzaRTCkb07dvbxRrXzGgPTgrX8U3aBrGVIXdI3HBgRvc2cri1THCvvmWphL1Lr7yDJoFoHxmxiKbtTRCi9MXCOU67Cpqj3BzrnyjFtmH-fk03JaW05USnr3F9J2IbYA3Mszu1tWjjrB-eNLJ_6GQ8kTTTcQutfLX5n_BsGlCvloje8JarxaFRqzVEJhlZf=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both;"&gt;&lt;b&gt;Post-Processing and Displaying TFLite Model Output:-&lt;/b&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both;"&gt;After applying essential post-processing techniques, such as Non-Maximum Suppression (NMS) to eliminate redundant bounding boxes, we can visualize the final output of the TFLite model on the given image, as shown below.&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgsex-BcldkJ1Yt9BFS7PIQiBbnM9Q5ErbuVk4Xx9f8rZCu5fRlqrZCDcdmiA4ZznZjbrbSJFgv-xSm-cmD82wJ2_ub5cTgcZqme2gJp5ZjsQvGpTLhXIoVutOHFXomBjMdlJuImo9HQ4QtM3SqEMhKgi2K3FTiN52FVKhJMXi7oEKj8zIJYLt0akoaDEHX" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="310" data-original-width="1077" src="https://blogger.googleusercontent.com/img/a/AVvXsEgsex-BcldkJ1Yt9BFS7PIQiBbnM9Q5ErbuVk4Xx9f8rZCu5fRlqrZCDcdmiA4ZznZjbrbSJFgv-xSm-cmD82wJ2_ub5cTgcZqme2gJp5ZjsQvGpTLhXIoVutOHFXomBjMdlJuImo9HQ4QtM3SqEMhKgi2K3FTiN52FVKhJMXi7oEKj8zIJYLt0akoaDEHX=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The output image will be like below where you can see the predicted objects-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEg1XSzgruz6vjsLEtTnAy2934WyLH7ikazkgWqM85PO5GOhwO32H7vOYkJgyDK6OTrZN5z_YibJoVKtDPyofiodWlvLIwXMu5JZyfKeEdISqDAbcuXN_c1RCLjePZBAmeJTfZxULcCOAySxe4pe6SAHwdtL_0XmWbqQxNtIrILhk4OgnIHtQ0WW3I4_KRto" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img data-original-height="593" data-original-width="753" height="315" src="https://blogger.googleusercontent.com/img/a/AVvXsEg1XSzgruz6vjsLEtTnAy2934WyLH7ikazkgWqM85PO5GOhwO32H7vOYkJgyDK6OTrZN5z_YibJoVKtDPyofiodWlvLIwXMu5JZyfKeEdISqDAbcuXN_c1RCLjePZBAmeJTfZxULcCOAySxe4pe6SAHwdtL_0XmWbqQxNtIrILhk4OgnIHtQ0WW3I4_KRto=w400-h315" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both;"&gt;We can now confirm that the converted TFLite model produces predictions identical to those of the original model&#128074;. Additionally, since this TFLite model is in NHWC format, which is the required format for Android development, it can be seamlessly integrated into mobile applications. With the model ready, we can hand it over to the mobile developer, who can effortlessly import it into Android Studio for further app development and deployment&#128076;.&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;That’s all for today, everyone! In this blog post, we successfully walked through the correct process of converting Darknet/YOLOv4 weights to TFLite format, enabling seamless integration into mobile applications as an offline model. This skill not only enhances your expertise but also strengthens your capabilities as a computer vision practitioner&#128114;.&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;Now, it's your turn! Use my &lt;a href="https://www.kaggle.com/code/iamprateek/darknet-weight-conversion-onnx-tflite/notebook" rel="nofollow" target="_blank"&gt;&lt;b&gt;Kaggle Notebook&lt;/b&gt;&lt;/a&gt; to convert your custom-trained YOLOv4 model to TFLite format and deploy it within a mobile application to demonstrate your computer vision expertise&#128519;.&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;In the next post, we will explore the YOLOv7 model conversion to TFLite. Until then, keep pushing forward, chase your dreams, and make every second count! &#128640; See you in the next one!&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/a/AVvXsEgE1Qy3_iJD1bQ7vzt69WURYsngKT96sUCGxmswR4lv3nQF2nnity-3TlySWvidKHRocyTB5lXp_pRccb5CYV1OyI54UvXZVOHCYGgTOjOgHd_OlJizMp4hof11K8UaW0Asw9ijNQ6u5c4NA7sbDKN3FhBtk8Lz9E0SS_E9-s0kRV_bBaF2n8AyKjmQITWa=s72-w400-h134-c" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">11</thr:total></item><item><title>Generative AI: Retrieval Augmented Generation(RAG)</title><link>https://dsbyprateekg.blogspot.com/2024/01/generative-ai-retrieval-augmented.html</link><category>generative ai</category><category>mistral</category><category>rag</category><category>Retrieval Augmented Generation</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Fri, 19 Jan 2024 17:08:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-3301968955946016139</guid><description>&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&amp;nbsp;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-bmjfYr8lnXZjSIfCGWdDX9Cdu0KVdPtchOVqRZweUS6smV_weeBpY6XqR1ttlW4S0lc4cK2uUBKZmu92-2MBighyphenhyphenH2k-wL_R7G353aLHHrsPEz-NfIYGYR1_EM9MAYjUxLe-_YyRysxe80Fko-qtCuvsUL05RhkdjfiKpX_6FidG9D8wGIOt3ETyoRLB/s1557/Blank%20board.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="362" data-original-width="1557" height="149" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-bmjfYr8lnXZjSIfCGWdDX9Cdu0KVdPtchOVqRZweUS6smV_weeBpY6XqR1ttlW4S0lc4cK2uUBKZmu92-2MBighyphenhyphenH2k-wL_R7G353aLHHrsPEz-NfIYGYR1_EM9MAYjUxLe-_YyRysxe80Fko-qtCuvsUL05RhkdjfiKpX_6FidG9D8wGIOt3ETyoRLB/w640-h149/Blank%20board.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;Another blog post starts with you beautiful people&#128102;. I hope you have explored my&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2024/01/generative-ai-faster-fine-tuning-of.html" target="_blank"&gt;&lt;b&gt;last blog post&lt;/b&gt;&lt;/a&gt;&amp;nbsp;about 2x faster fine-tuning of Mistral 7b model on a custom dataset&#128072;. In this blog post, we are going to learn an essential technique in Generative AI:&amp;nbsp;Retrieval Augmented Generation (RAG).&lt;/div&gt;&lt;div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;What is RAG?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Retrieval Augmented Generation (RAG) is an innovative approach that melds generative models, like transformers, with a retrieval mechanism. By tapping into existing knowledge, RAG retrieves pertinent information from expansive external datasets or knowledge bases to enhance the generation process, thereby elevating the model's content relevance and factual accuracy&#128170;. This versatility renders RAG particularly beneficial for tasks demanding the assimilation of external knowledge, such as question answering or content creation.&lt;/p&gt;&lt;p&gt;Upon receiving input, RAG actively searches for relevant documents from specified sources (e.g., Wikipedia, company knowledge base, etc.). It then seamlessly amalgamates this retrieved data with the input, offering a comprehensive output complete with references. This unique structure enables RAG to effortlessly integrate new and evolving information without the need to retrain the entire model from scratch&#128165;.&lt;/p&gt;&lt;p&gt;&lt;b&gt;RAG vs Fine-Tuning?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;RAG augments the prompt with the external data, while fine-tuning incorporates the additional knowledge into the model itself. RAG requires less labeled data and resources than fine-tuning processes, making it less costly. Much of RAG expenses often go into setting up embedding and retrieval systems. In contrast, fine-tuning requires more labeled data, significant computational resources, and state-of-the-art hardware like high-performance GPUs or TPUs. As a result, the overall cost of fine-tuning is relatively higher than RAG&#128184;.&lt;/p&gt;&lt;p&gt;&lt;b&gt;RAG Architecture?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;A standard RAG application comprises two primary elements:&lt;/p&gt;&lt;p&gt;&lt;b&gt;A. Indexing:&lt;/b&gt; a data ingestion pipeline that sources and indexes data, typically conducted offline. The indexing sequence is as follows-&lt;/p&gt;&lt;p&gt;&lt;b&gt;1. Load:&lt;/b&gt; First we need to load our data. This is done with &lt;a href="https://python.langchain.com/docs/modules/data_connection/document_loaders/" target="_blank"&gt;&lt;b&gt;DocumentLoaders&lt;/b&gt;&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;b&gt;2. Split:&lt;/b&gt; Text splitters break large Documents into smaller chunks. This is useful both for indexing data and for passing it into a model, since large chunks are harder to search over and won’t fit in a model’s finite context window.&lt;/p&gt;&lt;p&gt;&lt;b&gt;3. Store:&lt;/b&gt; We need somewhere to store and index our splits so that they can later be searched. This is often done using a &lt;a href="https://python.langchain.com/docs/modules/data_connection/vectorstores/" target="_blank"&gt;&lt;b&gt;VectorStore&lt;/b&gt;&lt;/a&gt; and &lt;a href="https://python.langchain.com/docs/modules/data_connection/text_embedding/" target="_blank"&gt;&lt;b&gt;Embeddings&lt;/b&gt;&lt;/a&gt; model.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;B. Retrieval and generation:&lt;/b&gt; the operational RAG chain, is responsible for receiving user queries during runtime, retrieving pertinent data from the index, and passing it to the model. The sequence is as follows-&lt;/p&gt;&lt;p&gt;&lt;b&gt;1. Retrieve:&lt;/b&gt; Given a user input, relevant splits are retrieved from storage using a &lt;a href="https://python.langchain.com/docs/modules/data_connection/retrievers/" target="_blank"&gt;&lt;b&gt;Retriever&lt;/b&gt;&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;b&gt;2. Generate:&lt;/b&gt; A ChatModel / LLM produces an answer using a prompt that includes the question and the retrieved data.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Let's build something with RAG!&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Now we are ready to use RAG with a large language model-Mistral 7B that we also used in the&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2024/01/generative-ai-faster-fine-tuning-of.html" target="_blank"&gt;last blog&lt;/a&gt;. As of date 18 January'24, the hot topic in India is the new temple of &lt;a href="https://en.wikipedia.org/wiki/Rama" target="_blank"&gt;lord Ram&lt;/a&gt; in Ayodhya city. If we ask any question related to this to an LLM, they cannot reply with accurate answers since they are not trained with current news. To prove this point, we can load the pre-trained LLM model and ask a related question to it like below-&lt;/p&gt;&lt;p&gt;Load the 4-bit Mistral 7b model as we did in my &lt;a href="https://dsbyprateekg.blogspot.com/2024/01/generative-ai-faster-fine-tuning-of.html" target="_blank"&gt;last post&lt;/a&gt;-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgl-5QxHqpGLbHp-Kp24L6KNqmdi77-FK7tbJI7rVmdikaSy8ISMi2shmazbR5XkeO9GYdYNKUb2imAopoWfscRrnk8jEPb9tMXTThDOna1DW4-4zNOhONnrnCiOJM6GffYra_eCV7zExnV09YjrtqeOmksOfa1NDu2hXwATLBqlgyAJyNx3wsXpPk3RKPX" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="367" data-original-width="868" src="https://blogger.googleusercontent.com/img/a/AVvXsEgl-5QxHqpGLbHp-Kp24L6KNqmdi77-FK7tbJI7rVmdikaSy8ISMi2shmazbR5XkeO9GYdYNKUb2imAopoWfscRrnk8jEPb9tMXTThDOna1DW4-4zNOhONnrnCiOJM6GffYra_eCV7zExnV09YjrtqeOmksOfa1NDu2hXwATLBqlgyAJyNx3wsXpPk3RKPX=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;And I asked a current question to the loaded pre-trained model as below-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgGj2d6CIQP1UYZ4ogBRNJ08Ml033FcbLuzteMT3jIZKsPrTHmykKgFX2MxXjtsSgjduNvMRX1mUddTgggAf3se-WtjCKNYAJ_O8P_HlCpHbFMGztrOrbAtW483_rh8k374eXJrKBsFbfKrHKIatqApZzEzUeZj7zZKdu6vSA9-KO9Rh1YpmkxIiwdkWb7M" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="198" data-original-width="1340" src="https://blogger.googleusercontent.com/img/a/AVvXsEgGj2d6CIQP1UYZ4ogBRNJ08Ml033FcbLuzteMT3jIZKsPrTHmykKgFX2MxXjtsSgjduNvMRX1mUddTgggAf3se-WtjCKNYAJ_O8P_HlCpHbFMGztrOrbAtW483_rh8k374eXJrKBsFbfKrHKIatqApZzEzUeZj7zZKdu6vSA9-KO9Rh1YpmkxIiwdkWb7M=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;And as I expected, the model is unable to give the answer-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjIZ6S1LFI-8gnRIWN73B6TofrCptjfhADNxY8Dp7zAa8uQvTvAKdWZQgnKoF5Ag56l1XCFSlWo5O0RI5su8rAfRlDXyu6K4-HdXZb2CrAfX2xha1SUkJxV8iO0StsbtSqdE3SyO4d-22EC3UVbNPclVBxqJYCmPaFsah5C9I67dKm3vf_Je9KShKEvlWBe" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="82" data-original-width="1391" src="https://blogger.googleusercontent.com/img/a/AVvXsEjIZ6S1LFI-8gnRIWN73B6TofrCptjfhADNxY8Dp7zAa8uQvTvAKdWZQgnKoF5Ag56l1XCFSlWo5O0RI5su8rAfRlDXyu6K4-HdXZb2CrAfX2xha1SUkJxV8iO0StsbtSqdE3SyO4d-22EC3UVbNPclVBxqJYCmPaFsah5C9I67dKm3vf_Je9KShKEvlWBe=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;I hope you have understood the problem statement.&#128064; Now we will use RAG to get the required information from an external source to teach our LLM about the current topic. As you have read above in the architecture section of the RAG, we will need to store that source somewhere. For this purpose, we are going to use &lt;a href="https://js.langchain.com/docs/integrations/vectorstores/faiss" target="_blank"&gt;&lt;b&gt;FAISS&lt;/b&gt;&lt;/a&gt;&amp;nbsp;but you can use any other vector database like &lt;a href="https://python.langchain.com/docs/integrations/vectorstores/chroma" target="_blank"&gt;&lt;b&gt;CHROMA&lt;/b&gt;&lt;/a&gt;. Let's install the FAISS library using pip command in our colab notebook-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEic-E7soosklmsSuM5h4biSdNn4hf2e2JtHNh-ncIONKcgh6ufpGvNIquhUnyx3kaZR28fF_mcoWCOJxgut42z0rHB7DCtV8wRk9IEll_O1vEiQNbXGuvsNQzSyOmN9epUcZo19MYSfCOnIiI17t--3etpF5Zu5CnjxdnQI-2TGC982G-op57ZNf2TS_AsR" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="45" data-original-width="227" height="63" src="https://blogger.googleusercontent.com/img/a/AVvXsEic-E7soosklmsSuM5h4biSdNn4hf2e2JtHNh-ncIONKcgh6ufpGvNIquhUnyx3kaZR28fF_mcoWCOJxgut42z0rHB7DCtV8wRk9IEll_O1vEiQNbXGuvsNQzSyOmN9epUcZo19MYSfCOnIiI17t--3etpF5Zu5CnjxdnQI-2TGC982G-op57ZNf2TS_AsR" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;Also, install other required libraries like&amp;nbsp;langchain, langchain-community, unsloth, transformers and sentence-transformers if you still need to install them.&lt;div&gt;The next step is to download the data from our source. In my case, it is 'https://srjbtkshetra.org/'. You can replace this with any other site of your requirement. To load all text from this webpage into a document format, we will use&amp;nbsp;&lt;a href="https://python.langchain.com/docs/integrations/document_loaders/web_base" target="_blank"&gt;&lt;b&gt;WebBaseLoader&lt;/b&gt;&lt;/a&gt;&amp;nbsp;from the LangChain tool. Please note that LangChain supports various types of data loaders and you can&amp;nbsp;&lt;a href="https://python.langchain.com/docs/integrations/document_loaders" target="_blank"&gt;&lt;b&gt;refer this link&lt;/b&gt;&lt;/a&gt;&amp;nbsp;for all the details-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjkN1314hSq85tkGvDL9t7J_HZUWafyeSWpUsri91ajschltFet5O2AtNPviafWvZCVXfh8a4ZISQhJ1cKWfgB2kP-BN_dAJHT7_J3a06ypM118lPJn7ondxX2ngw05CRki0WcTuPmPBIewM-o0NhXI4oPkPfGWCd8p-QoBnx37AoqZBCzATiyQoO0vz8Dy" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="227" data-original-width="943" src="https://blogger.googleusercontent.com/img/a/AVvXsEjkN1314hSq85tkGvDL9t7J_HZUWafyeSWpUsri91ajschltFet5O2AtNPviafWvZCVXfh8a4ZISQhJ1cKWfgB2kP-BN_dAJHT7_J3a06ypM118lPJn7ondxX2ngw05CRki0WcTuPmPBIewM-o0NhXI4oPkPfGWCd8p-QoBnx37AoqZBCzATiyQoO0vz8Dy=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;The next step is to divide the loaded texts into smaller chunks&amp;nbsp;that can fit into our model's context window. For example, GPT-3.5-turbo has a context window of 4,097 tokens and Mistral 7B has 8,000. So if we try to pass that context window then the behavior of LLMs will be unpredictable and suffer from severe performance degradation&#128125;. For this purpose, we will use&amp;nbsp;&lt;a href="https://python.langchain.com/docs/modules/data_connection/document_transformers/" target="_blank"&gt;text splitter&lt;/a&gt;&amp;nbsp;as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgs1PnO0gpQW3DfIF3TMoEcpjybjlvr6bSD8kSRL4ieUg1heI3UASyykFIEB-twLXgwbGa2XqJp7Gttv1Vd2QgGc1Dv2bkGJw5Ik8QU8sefUAT5_RQI6Z2AqkNZ9pKHs_yMszdYm6jruUWjpRM5_kb_N5ccBqpGfpyQbckLk5GmGRHk21nUq6nRMiEiOnt4" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="150" data-original-width="653" src="https://blogger.googleusercontent.com/img/a/AVvXsEgs1PnO0gpQW3DfIF3TMoEcpjybjlvr6bSD8kSRL4ieUg1heI3UASyykFIEB-twLXgwbGa2XqJp7Gttv1Vd2QgGc1Dv2bkGJw5Ik8QU8sefUAT5_RQI6Z2AqkNZ9pKHs_yMszdYm6jruUWjpRM5_kb_N5ccBqpGfpyQbckLk5GmGRHk21nUq6nRMiEiOnt4=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The next step is to encode these chunks of text into the embeddings and then perform the indexing of those embeddings to our vector database as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgdwjNylKx1qbPORWLveDNGNe7EJGLm9tzXlMvwqivXOQEPYGn2IbpUJh0BPDqMrtJZPxsjFaD6SPN4BSKyTcZdaZ5IkLKJ87Ex-Wv5HP-07kBuRJVllguHpRYs3uG07YWI1a8Flb2-Dw61kPpMegxM6bakWP9R2A5Oa_Uu6wOacf_De3tTGdNN2hbdyOdL" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="91" data-original-width="1388" src="https://blogger.googleusercontent.com/img/a/AVvXsEgdwjNylKx1qbPORWLveDNGNe7EJGLm9tzXlMvwqivXOQEPYGn2IbpUJh0BPDqMrtJZPxsjFaD6SPN4BSKyTcZdaZ5IkLKJ87Ex-Wv5HP-07kBuRJVllguHpRYs3uG07YWI1a8Flb2-Dw61kPpMegxM6bakWP9R2A5Oa_Uu6wOacf_De3tTGdNN2hbdyOdL=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Here, for embeddings, we use the 'sentence-transformers/all-mpnet-base-v2' model from the Hugging Face hub but you can explore other sentence-transformers models as well from&amp;nbsp;&lt;a href="https://huggingface.co/sentence-transformers" target="_blank"&gt;this link&lt;/a&gt;. Now we will construct a &lt;a href="https://python.langchain.com/docs/modules/data_connection/retrievers/vectorstore" target="_blank"&gt;receiver&lt;/a&gt; to fetch the documents from the vector db as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhIscfNc5_VAykoh4C4SScff5zu5eBSfmqodRlh21POoAw2Joqz4aY5VMpgMs-yVKyTPehHhUO9N_kfcxg4kJnuyO4XL0I5CEdrYNyjac3svf0yr4qHNzLrMZbpq0a04k5OYLWYf744pD1gvrPIl1oT039dVbfq062przbHVQL3lM15TiZgf3VlIUKZyKLl" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="47" data-original-width="378" height="40" src="https://blogger.googleusercontent.com/img/a/AVvXsEhIscfNc5_VAykoh4C4SScff5zu5eBSfmqodRlh21POoAw2Joqz4aY5VMpgMs-yVKyTPehHhUO9N_kfcxg4kJnuyO4XL0I5CEdrYNyjac3svf0yr4qHNzLrMZbpq0a04k5OYLWYf744pD1gvrPIl1oT039dVbfq062przbHVQL3lM15TiZgf3VlIUKZyKLl" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we will create and load our pipeline for the inference as you already read in&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2024/01/generative-ai-faster-fine-tuning-of.html" target="_blank"&gt;last blog&lt;/a&gt;-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEj5C5roNKPqkAZdr1uwWY7kHHk2KVVA4RZbs6Q4YY_8G-2dl9LpMW2SMjlqllp7h2N717W3zOQYpihG8LQtAhIGITPK9JbdLPIMrFQm3v_Y-PePP-lUdQqPK-SMDYxzxFqHRIZLDD5aDTb0-R8ExKFRJDYSXbOyf0oBNCoMWR9p2k-YE7w_iT8-WsjbWXU8" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="302" data-original-width="487" src="https://blogger.googleusercontent.com/img/a/AVvXsEj5C5roNKPqkAZdr1uwWY7kHHk2KVVA4RZbs6Q4YY_8G-2dl9LpMW2SMjlqllp7h2N717W3zOQYpihG8LQtAhIGITPK9JbdLPIMrFQm3v_Y-PePP-lUdQqPK-SMDYxzxFqHRIZLDD5aDTb0-R8ExKFRJDYSXbOyf0oBNCoMWR9p2k-YE7w_iT8-WsjbWXU8=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEikhKn9QE3SmnDdcwFPOcdh7_ICWgaA4nHR1JGa8plEmdsXhRlQkT-gutH1egSYsFAiLfrqzOB0PXkbQmNuPEsKvrArF0jBwkbNinGPNnMPHB6HRBJJmFpovpOUpW1wMqVrfK6rrIRg5RBdBFWFVr8zVn39ZhVFFhXr7F6mKZMox422Y_Elz_6gnAlqDEo6" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="36" data-original-width="667" src="https://blogger.googleusercontent.com/img/a/AVvXsEikhKn9QE3SmnDdcwFPOcdh7_ICWgaA4nHR1JGa8plEmdsXhRlQkT-gutH1egSYsFAiLfrqzOB0PXkbQmNuPEsKvrArF0jBwkbNinGPNnMPHB6HRBJJmFpovpOUpW1wMqVrfK6rrIRg5RBdBFWFVr8zVn39ZhVFFhXr7F6mKZMox422Y_Elz_6gnAlqDEo6=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we will create a prompt template for giving the instruction to the model, format the output as our need, and create a chain of RAG all of this like below-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHI-ObaDJHtgCxhNnMH7xoV0ZtAAEPxYcXCSzlfPrEvnCdt-BqYvJezHsrcrs1TqFdmsvAzNMV-18ZbFCf29bIGDEV9yuV5eW0YHT3xGb-Pk3sfhzjsMD9P8SffUgyoagtZd3kYrD5ad_E4-rIj0Akq1rJ_iuo49MVFjtpweLFKTbRH3EFQO5YE44UlZGP/s888/llm1.png" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="513" data-original-width="888" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHI-ObaDJHtgCxhNnMH7xoV0ZtAAEPxYcXCSzlfPrEvnCdt-BqYvJezHsrcrs1TqFdmsvAzNMV-18ZbFCf29bIGDEV9yuV5eW0YHT3xGb-Pk3sfhzjsMD9P8SffUgyoagtZd3kYrD5ad_E4-rIj0Akq1rJ_iuo49MVFjtpweLFKTbRH3EFQO5YE44UlZGP/s16000/llm1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Now our RAG is ready to ask any relative questions to our added source. For example, I asked about the dimensions of the newly built Lord Ram Temple and the model is now able to provide me accurate answers as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEivhhxiMcDKb8hl9R3SW8tiwgzCAAEdpq3mGwG2YsJjE6EFRRP4wtXl6p0Pb6M7ebjkFHzp9Jul8u1HXT8LuU54u9DF22Uh3CeScTU3G_u9U99hKONMMHnV1MTO1tfBTTEoMe3nklsGEDTrqw-YTpmt3_zj2NsGD6zuv4M4A9i69_qHFDANyDWhSPhAzb5N" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="242" data-original-width="1398" src="https://blogger.googleusercontent.com/img/a/AVvXsEivhhxiMcDKb8hl9R3SW8tiwgzCAAEdpq3mGwG2YsJjE6EFRRP4wtXl6p0Pb6M7ebjkFHzp9Jul8u1HXT8LuU54u9DF22Uh3CeScTU3G_u9U99hKONMMHnV1MTO1tfBTTEoMe3nklsGEDTrqw-YTpmt3_zj2NsGD6zuv4M4A9i69_qHFDANyDWhSPhAzb5N=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Another one-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEh-0D-42cJcpulUhJebue7KO_HXXqa-3RMfmDNo71_v2VBLF4b40ehQEdu-0hvQz0YTxilvx-uoeUu4Loq7Z5q6b8PrDuTiFb3Uec0vzk2HoHAoR-kiVu8HDYMR-etqA8CibkONMdoW5IHagzetbyXJFyT1B3DVlVOQkecQSaLyQ8OJAYtAgX7-9pPVqois" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="127" data-original-width="1395" src="https://blogger.googleusercontent.com/img/a/AVvXsEh-0D-42cJcpulUhJebue7KO_HXXqa-3RMfmDNo71_v2VBLF4b40ehQEdu-0hvQz0YTxilvx-uoeUu4Loq7Z5q6b8PrDuTiFb3Uec0vzk2HoHAoR-kiVu8HDYMR-etqA8CibkONMdoW5IHagzetbyXJFyT1B3DVlVOQkecQSaLyQ8OJAYtAgX7-9pPVqois=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;How cool is this, right guys&#128079;. In this post, we learned about the creation of PromptTemplate &amp;amp; Chain, usage of&amp;nbsp; RunnablePassthrough, invocation of Retriever, integration of context Integration, and the LLM invocation. The usage of RAG with any other possible use case is endless&#128171;. So don't wait. Make a copy of&amp;nbsp;&lt;a href="https://colab.research.google.com/drive/1qMItYjwRedwZG0uvxc9DXwBvr4sYZ0Qq?usp=sharing" target="_blank"&gt;&lt;b&gt;this colab notebook&lt;/b&gt;&lt;/a&gt;&amp;nbsp;in your colab notebook and start playing with your own data and any source you want to update with the LLM.&amp;nbsp;&amp;nbsp;In the next post, we will further learn something useful use case of Gen AI, till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post.&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-bmjfYr8lnXZjSIfCGWdDX9Cdu0KVdPtchOVqRZweUS6smV_weeBpY6XqR1ttlW4S0lc4cK2uUBKZmu92-2MBighyphenhyphenH2k-wL_R7G353aLHHrsPEz-NfIYGYR1_EM9MAYjUxLe-_YyRysxe80Fko-qtCuvsUL05RhkdjfiKpX_6FidG9D8wGIOt3ETyoRLB/s72-w640-h149-c/Blank%20board.png" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">8</thr:total></item><item><title>Generative AI: Faster fine-tuning of a Mistral Model with less memory</title><link>https://dsbyprateekg.blogspot.com/2024/01/generative-ai-faster-fine-tuning-of.html</link><category>alpaca</category><category>dpo</category><category>fine tuning</category><category>generative ai</category><category>mistral</category><category>qlora</category><category>unsloth</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Mon, 15 Jan 2024 20:40:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-3791218755515012539</guid><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-xylU5KIFPp6b3YXQgOLLHt3Je5GVZl67XQX2c650KGs7tiBsHnZNaV5m5UBz-Eb4UF5aBuWE4ni1vJBU6lPTlnVgUsnxJI6ZweKVxeInZQynmmGfbXV52Fa4d2FHiuc93b4vYinY51RxXxjr0Ryv55TQbZVYqrlQRUv73oMpSVWsKjRwDYfI61ssJOk4/s702/Blank%20diagram.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="220" data-original-width="702" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-xylU5KIFPp6b3YXQgOLLHt3Je5GVZl67XQX2c650KGs7tiBsHnZNaV5m5UBz-Eb4UF5aBuWE4ni1vJBU6lPTlnVgUsnxJI6ZweKVxeInZQynmmGfbXV52Fa4d2FHiuc93b4vYinY51RxXxjr0Ryv55TQbZVYqrlQRUv73oMpSVWsKjRwDYfI61ssJOk4/s16000/Blank%20diagram.png" /&gt;&lt;/a&gt;&lt;/div&gt;Another blog post starts with you beautiful people&#128165;. I hope you have started your Generative AI learning from my&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2024/01/generative-ai-with-langchain-basics.html" target="_blank"&gt;&lt;b&gt;last post&lt;/b&gt;&lt;/a&gt;&amp;nbsp;and if you have not, then I recommend you read that one before proceeding to this blog.&amp;nbsp; In this post, we will delve into the transformative power of generative AI, specifically exploring the cutting-edge Mistral 7B model fine-tuning&#128640;. This revolutionary technology has not only redefined the boundaries of artificial intelligence but also sparked a paradigm shift in how we approach data generation and creativity. Join me on a journey through the fascinating intersection of machine learning and creativity, where &lt;a href="https://docs.mistral.ai/" target="_blank"&gt;Mistral&lt;/a&gt; 7B stands as a beacon of innovation, pushing the boundaries of what's possible in the realm of generative AI&#128072;.&lt;p&gt;&lt;/p&gt;&lt;p&gt;Fine-tuning a large language model(LLM) refers to the process of training the model on a specific, smaller dataset to adapt it to a particular task or domain. Large language models, like ChatGPT, are pre-trained on massive datasets to learn general language patterns and context. However, fine-tuning allows users to tailor the model for more specific applications, such as sentiment analysis, question-answering, or domain-specific content generation&#128076;.&lt;/p&gt;&lt;p&gt;But Fine-tuning an LLM has its own challenges&#128127; if you want to do it in your local system due to the following reasons-&lt;/p&gt;&lt;p&gt;&lt;b&gt;1. Memory Requirements:&lt;/b&gt; Large language models often have a considerable number of parameters, resulting in high memory requirements. Fine-tuning involves loading both the pre-trained model and the dataset into memory, and low RAM can limit the size of the model and the dataset that can be efficiently processed.&lt;/p&gt;&lt;p&gt;&lt;b&gt;2. Processing Power:&lt;/b&gt; Fine-tuning is a computationally intensive task. Low RAM computers may lack the necessary processing power to handle the optimization process efficiently. This can lead to slow training times and potential issues with model convergence.&lt;/p&gt;&lt;p&gt;&lt;b&gt;3. Batch Size Constraints:&lt;/b&gt; During fine-tuning, models are typically trained in batches to improve computational efficiency. Low RAM can restrict the batch size, impacting the model's ability to generalize well and slowing down the convergence of the training process.&lt;/p&gt;&lt;p&gt;&lt;b&gt;4. Data Loading Constraints:&lt;/b&gt; Loading and processing large datasets during fine-tuning can strain the available memory. Insufficient RAM may lead to data loading errors or inefficient use of available resources.&lt;/p&gt;&lt;p&gt;To address these challenges, we are going to use a cutting-edge fine-tuning approach:&lt;a href="https://arxiv.org/abs/2305.14314" target="_blank"&gt;&lt;b&gt;QLORA&lt;/b&gt;&lt;/a&gt;&amp;nbsp;but with &lt;b&gt;2.2X faster &amp;amp; 62% less memory&lt;/b&gt;&#128129;. Can you believe that? Yes, we are so let's start this in our colab notebook using T4 GPU only. To achieve this faster and less RAM-consuming fine-tuning approach we are going to use a library called&amp;nbsp;&lt;a href="https://github.com/unslothai/unsloth#installation-instructions---conda" target="_blank"&gt;&lt;b&gt;unsloth&lt;/b&gt;&lt;/a&gt;&#128110;. This fantastic library can be used to fine-tune other LLMs like&amp;nbsp;Llama 2 7b as well so don't forget to give a star in github to the library&#128077;. For our learning, we will stick to my favorite Mistral 7b. This library has a built-in 4-bit pre-quantized Mistral 7b model which means neither we need to download this model from elsewhere nor do we have to convert the original model to 4-bit&#128076;. Another benefit of using this library is that it supports Huggingface's TRL, Trainer, Seq2SeqTrainer, or even Pytorch code!&lt;/p&gt;&lt;p&gt;First, we will install the unsloth library based on our GPU type since it is sensitive to GPU type, and then the Transformers library from the Hugging Face which provides APIs and tools to easily download and train state-of-the-art pre-trained models&#128170; -&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivoBi2htx4PTNf7dsEfPQuDbbP-YFZ0VpWZLHS6dKJzZpN3AmtmIdaTrG_okCHcn3Hj6WZwvHQ1VPpg3se7rCHFb7DHO-B1wyHz1ld_FtetEoNKVVIuAzVzArThKVmPhEv2uOJ4dzpaLpw6rjrHGvuNwz1F9RZ56saLF1x8Pn-ZUBOI7SmaYDYxP8yyNwm/s927/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="307" data-original-width="927" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivoBi2htx4PTNf7dsEfPQuDbbP-YFZ0VpWZLHS6dKJzZpN3AmtmIdaTrG_okCHcn3Hj6WZwvHQ1VPpg3se7rCHFb7DHO-B1wyHz1ld_FtetEoNKVVIuAzVzArThKVmPhEv2uOJ4dzpaLpw6rjrHGvuNwz1F9RZ56saLF1x8Pn-ZUBOI7SmaYDYxP8yyNwm/s16000/1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Next, we download the pre-trained 4-bit Mistral 7B model with its tokenizer like the below-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1YnOIup_mtV4gU7jKweta60gM6K4izcyJ7c0XlMFjvIMnaR5rvrPp1G00bxQ5iJYt_3HLQYq4dXtGb3Uj_ayWrtcDH71cWMUL9laGU8JG-C_Pa4djEh2pweqX80HGPKOdeFCqWo5EobGBMhIRMQOTLH9OqRgmbfgj-czzOrVorR3waUqLUCOktml0OyzL/s863/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="372" data-original-width="863" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1YnOIup_mtV4gU7jKweta60gM6K4izcyJ7c0XlMFjvIMnaR5rvrPp1G00bxQ5iJYt_3HLQYq4dXtGb3Uj_ayWrtcDH71cWMUL9laGU8JG-C_Pa4djEh2pweqX80HGPKOdeFCqWo5EobGBMhIRMQOTLH9OqRgmbfgj-czzOrVorR3waUqLUCOktml0OyzL/s16000/2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;See, how easily and with very few lines of code we can download the model&#128171;. You will see the following output in the console while running the above code-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxo0CbSPQRnRMiVD6Jf_9S24KHVXOqXhrLEA2gtreSgNx7sdkJO8rQRDZvJ3iUjQYvjXkaRmA9kT0ZiswFruSQSUSBmj5Bf0VbSplqajiqBLIUXpkHHYfA_w4uySg8uQgmpP7S3-z5DhrpD1EtEnqaJ5t7bOA4bufUGqFNmEEinPLqdpB1J3yon8rX_fW0/s1261/3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="442" data-original-width="1261" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxo0CbSPQRnRMiVD6Jf_9S24KHVXOqXhrLEA2gtreSgNx7sdkJO8rQRDZvJ3iUjQYvjXkaRmA9kT0ZiswFruSQSUSBmj5Bf0VbSplqajiqBLIUXpkHHYfA_w4uySg8uQgmpP7S3-z5DhrpD1EtEnqaJ5t7bOA4bufUGqFNmEEinPLqdpB1J3yon8rX_fW0/s16000/3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;Before running the next line of code, we need to know that large-scale unsupervised language models (LMs) learn broad world knowledge and some reasoning skills, achieving precise control of their behavior is difficult&#128529; due to the completely unsupervised nature of their training. Existing methods for gaining such steerability collect human labels of the relative quality of model generations and fine-tune the unsupervised LM to align with these preferences, often with reinforcement learning from human feedback (RLHF).&amp;nbsp;&lt;/p&gt;&lt;p&gt;However, RLHF is a complex and often unstable procedure&#128531;, first fitting a reward model that reflects the human preferences, and then fine-tuning the large unsupervised LM using reinforcement learning to maximize this estimated reward without drifting too far from the original model. To address this big issue, we are going to use another cutting-edge algorithm called&amp;nbsp;&lt;a href="https://arxiv.org/abs/2305.18290" target="_blank"&gt;&lt;b&gt;DPO&lt;/b&gt;&lt;/a&gt;&amp;nbsp;&#128170;which can fine-tune LMs to align with human preferences as well as or better than existing methods. Also, fine-tuning with DPO exceeds PPO-based RLHF in the ability to control the sentiment of generations and matches or improves response quality in summarization and single-turn dialogue while being substantially simpler to implement and train.&amp;nbsp;&lt;/p&gt;&lt;p&gt;Don't worry using the DPO algorithm is also very easy due to unsloth library&#128147;. We will add &lt;a href="https://huggingface.co/docs/diffusers/training/lora" target="_blank"&gt;&lt;b&gt;LoRA adapters&lt;/b&gt;&lt;/a&gt;&amp;nbsp;to update 1 to 10% of all parameters for patching the model like below-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJF90Hsj99ZQL6BF3dMQJCy6N1rVU2hdm5SSBcfmQ7y4nbh__xqOoy-mJ7T4o53G_GsF_b1D3VCeBbiu0vJqmJdCPpWBmXmlcNP18tO9AVZ_r9DCXFrSug5mJdRkxJjm5-4XYkhzmmz5y6_2HufnbwcNoZk_nI_rOvSCqnNc4uvl2DDODoxv0uWxZa8eXx/s655/4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="295" data-original-width="655" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJF90Hsj99ZQL6BF3dMQJCy6N1rVU2hdm5SSBcfmQ7y4nbh__xqOoy-mJ7T4o53G_GsF_b1D3VCeBbiu0vJqmJdCPpWBmXmlcNP18tO9AVZ_r9DCXFrSug5mJdRkxJjm5-4XYkhzmmz5y6_2HufnbwcNoZk_nI_rOvSCqnNc4uvl2DDODoxv0uWxZa8eXx/s16000/4.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Here,&amp;nbsp;&lt;b&gt;r&lt;/b&gt; is the dimension of the low-rank matrices,&amp;nbsp;&lt;b&gt;lora_alpha&lt;/b&gt; is the scaling factor for the low-rank matrices and&amp;nbsp;&lt;b&gt;lora_dropout&lt;/b&gt; is the dropout probability of the LoRA layers.&lt;/p&gt;&lt;p&gt;Next, we need a dataset that we can fine-tune. For this purpose, we will use cleaned 52k samples of&amp;nbsp;&lt;a href="https://huggingface.co/datasets/yahma/alpaca-cleaned" target="_blank"&gt;&lt;b&gt;alpaca-cleaned dataset&lt;/b&gt;&lt;/a&gt;&amp;nbsp;from the original Alpaca Dataset released by Stanford. The Alpaca dataset is designed for instruction training pre-trained language models&#128168;. But you can replace this with your own dataset. It looks like in the below format-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgC095OnXZ30IaQE6mdv23cLKydwxO6VURSrc1fm6IT8YA5JQ3_d6ITtmZPiSRmQENtxPGVCQGkH2xEP0bvP3P0F9AllmuuhEYYMFj_gC8x9I7k9lmF0qLLILSIAgeKUCbuwmFktE9WDEcDf2Oq5QjSbAknYt-EBjMX-L9rTJDl-nqfZcf3TFGvpI1c088Z/s1197/5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="371" data-original-width="1197" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgC095OnXZ30IaQE6mdv23cLKydwxO6VURSrc1fm6IT8YA5JQ3_d6ITtmZPiSRmQENtxPGVCQGkH2xEP0bvP3P0F9AllmuuhEYYMFj_gC8x9I7k9lmF0qLLILSIAgeKUCbuwmFktE9WDEcDf2Oq5QjSbAknYt-EBjMX-L9rTJDl-nqfZcf3TFGvpI1c088Z/s16000/5.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Since from our&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2024/01/generative-ai-with-langchain-basics.html" target="_blank"&gt;last blog post&lt;/a&gt;&amp;nbsp;we know that the Mistral 7B model expects a specific format, we will convert this dataset to the specific format after loading it like below-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN9Cyw-LnwmKGF89IDPnZfSzzncceoBFeofE9RMTLObl4BFlV1S3hnDPOgK94ejFQH5FhT93wp-ds2AbmwvLD1gpbYeMR13f1vjW8nvD22XpE7E8Pmc77NhGXpfcmjw9lxxTQ15u2CchWlXKBqpZGlKp50Ldhb1w534IvDS2oMwobTjX1ZzBcXpoHpP4n1/s1697/6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="598" data-original-width="1697" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN9Cyw-LnwmKGF89IDPnZfSzzncceoBFeofE9RMTLObl4BFlV1S3hnDPOgK94ejFQH5FhT93wp-ds2AbmwvLD1gpbYeMR13f1vjW8nvD22XpE7E8Pmc77NhGXpfcmjw9lxxTQ15u2CchWlXKBqpZGlKp50Ldhb1w534IvDS2oMwobTjX1ZzBcXpoHpP4n1/s16000/6.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;The downloading will look like below-&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEi-uP_fK6a1ESltRFI-5SpStQpPTBOw088K0QxzT57C0jOvF0BBjjp6PZY2PU3qVMM63VKjJSIx0AB4t7oHzd9ZgfKhHXXcSVwCPCBl568ZhzcxECxzkdKU02kgeqiybyB_1XQzPl5HWaASkaXV2CmX21EVNBD7EFKzQBYQ9I7_1BHANLNwP0BQzvTTEu6i" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="158" data-original-width="866" src="https://blogger.googleusercontent.com/img/a/AVvXsEi-uP_fK6a1ESltRFI-5SpStQpPTBOw088K0QxzT57C0jOvF0BBjjp6PZY2PU3qVMM63VKjJSIx0AB4t7oHzd9ZgfKhHXXcSVwCPCBl568ZhzcxECxzkdKU02kgeqiybyB_1XQzPl5HWaASkaXV2CmX21EVNBD7EFKzQBYQ9I7_1BHANLNwP0BQzvTTEu6i=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we can start training the model on this formatted dataset using the Huggingface TRL's SFTTrainer.&amp;nbsp;TRL is a full stack library to train transformer language models with Reinforcement Learning, from the Supervised Fine-tuning step (SFT), and Reward Modeling step (RM) to the Proximal Policy Optimization (PPO) step. The library is integrated with transformers-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjfV8hcIHcRvd9z1QjdOnpZnRcbGHJifgz5jXu7QapHHNGqob09XhJy0Qj0U_IQPFzcZJRaZSJ4PM8s6FNtnRecHD3_QgFyFMhFKO_e_HZeDH4RTQH2eDq_2Rn8bkiNQjPtYDL7RFo7Y9Z1ARwr08o9KroiIMkkcR_GeWIbdeMIcrmPJtTt612_C5Q6aM9V" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="592" data-original-width="876" src="https://blogger.googleusercontent.com/img/a/AVvXsEjfV8hcIHcRvd9z1QjdOnpZnRcbGHJifgz5jXu7QapHHNGqob09XhJy0Qj0U_IQPFzcZJRaZSJ4PM8s6FNtnRecHD3_QgFyFMhFKO_e_HZeDH4RTQH2eDq_2Rn8bkiNQjPtYDL7RFo7Y9Z1ARwr08o9KroiIMkkcR_GeWIbdeMIcrmPJtTt612_C5Q6aM9V=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Here,&amp;nbsp;&lt;b&gt;per_device_train_batch_size&lt;/b&gt; is the batch size per GPU/TPU core/CPU for training, &lt;b&gt;gradient_accumulation_steps&lt;/b&gt; is the number of update steps to accumulate the gradients before performing a backward/update pass, &lt;b&gt;fp16&lt;/b&gt; is whether to use 16-bit (mixed) precision training (through NVIDIA apex) instead of 32-bit training.&amp;nbsp;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Before starting the training, we can check the memory of the notebook as below-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgwU45-qTFJaBQnVpyfEhXgc5KT72VqDtPKMoiYCG8MzWXQvT97x7bMQsJNpe8EqH_5xMornvDv0V6mPVXaRLCb5wrVwHLvJv7kPpZk21fpUQCjR1sKuKaTEi4d1cpwhll8egLuRyJgj8-fvq1-EJbCCp5CKmFuWBbBbq-4KHxVfU6jBkcx0CdQjCyPe6Yc" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="186" data-original-width="808" src="https://blogger.googleusercontent.com/img/a/AVvXsEgwU45-qTFJaBQnVpyfEhXgc5KT72VqDtPKMoiYCG8MzWXQvT97x7bMQsJNpe8EqH_5xMornvDv0V6mPVXaRLCb5wrVwHLvJv7kPpZk21fpUQCjR1sKuKaTEi4d1cpwhll8egLuRyJgj8-fvq1-EJbCCp5CKmFuWBbBbq-4KHxVfU6jBkcx0CdQjCyPe6Yc=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Now, we will start the training using the following command-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEi0xwOa_YrgX3H182LszMq6YpEWkBeOeECrx5r_lOY-n_lQ_gc8UymM4cbFV1_V1wrxYX8zmagSQj9aBlOTt1Gu-b1TNGw1u-yawzGWUGrXK8fPCPPifljvN5_lOzrKC6EIi5jTc7pLT8N1io3zoxUT4OgF7bfTne85e-D91yR-r4KM0Vy5-YiC-79b70Fb" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="112" data-original-width="630" src="https://blogger.googleusercontent.com/img/a/AVvXsEi0xwOa_YrgX3H182LszMq6YpEWkBeOeECrx5r_lOY-n_lQ_gc8UymM4cbFV1_V1wrxYX8zmagSQj9aBlOTt1Gu-b1TNGw1u-yawzGWUGrXK8fPCPPifljvN5_lOzrKC6EIi5jTc7pLT8N1io3zoxUT4OgF7bfTne85e-D91yR-r4KM0Vy5-YiC-79b70Fb=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;For 1 epoch, it took approx 15:36 mins to complete the training and after the training completion when I checked the memory I was quite surprised to see the stats as below-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhPRVoDRFFDfgU2XwBIKqA6GbfdYIiODgd7EmXKf2I0rHV0SUyRnarqBjPKs849_DLAM0lOlU8aubkpLzarg1wVfqmMFdNPl6U7ttnFseBw6AsgdZvhqLr42Bmc1zeP9B4L3IeeIDmE-k2I-K3QOvMvr379IecK1H75fiJGLFC6-A_Mp-rrotUiPVvomqjG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="392" data-original-width="877" src="https://blogger.googleusercontent.com/img/a/AVvXsEhPRVoDRFFDfgU2XwBIKqA6GbfdYIiODgd7EmXKf2I0rHV0SUyRnarqBjPKs849_DLAM0lOlU8aubkpLzarg1wVfqmMFdNPl6U7ttnFseBw6AsgdZvhqLr42Bmc1zeP9B4L3IeeIDmE-k2I-K3QOvMvr379IecK1H75fiJGLFC6-A_Mp-rrotUiPVvomqjG=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Now, we can test our fine-tuned model on a given prompt&#128587;. For example here I am asking my model to complete the &lt;a href="https://en.wikipedia.org/wiki/Fibonacci_sequence" target="_blank"&gt;&lt;b&gt;Fibonacci series&lt;/b&gt;&lt;/a&gt;&amp;nbsp;as below-&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjaj-SdC4fJYkEQfAQ_kDPsQ9tNVOW45hrW1urQkXvyr7rHLq4xEjfzJrNYEaRmSYl19fCaTvSQeQ1YhpRZ744euiy0BZIXKil5kxpT_GiFei4isDlLNUz7NwYzp5D74lgV1CYbRByIt8WgWyVTZiu7O4RHEYSJAz3KW7iA254kZViWie0ZhNxXRepL4JrR" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="302" data-original-width="861" src="https://blogger.googleusercontent.com/img/a/AVvXsEjaj-SdC4fJYkEQfAQ_kDPsQ9tNVOW45hrW1urQkXvyr7rHLq4xEjfzJrNYEaRmSYl19fCaTvSQeQ1YhpRZ744euiy0BZIXKil5kxpT_GiFei4isDlLNUz7NwYzp5D74lgV1CYbRByIt8WgWyVTZiu7O4RHEYSJAz3KW7iA254kZViWie0ZhNxXRepL4JrR=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Here, in the prompt template, I have given one sample sequence of the Fibonacci series and then I asked it to complete the next sequence with the max token size of 128. You can increase this size to see more completion in the final output. I got the following output from my model-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEig32fwp2uGF4EUfwu4KlzZn4_EbwRoIuRvKJdIqnlmSM9HT3ZDwPdW08ZG2ocUulWmoWrbNr-IHAWkD5MJXG1OOxJNtPdSuXz1iK5L3Fk9L3JRqEVb1nr25YBmnWOOfWlrSlXfkK7t82bQIVwQO_zEKqhENBFlVaGf_COLzN5LYWdJkyuYPrzaEMuIHvjJ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="72" data-original-width="1750" src="https://blogger.googleusercontent.com/img/a/AVvXsEig32fwp2uGF4EUfwu4KlzZn4_EbwRoIuRvKJdIqnlmSM9HT3ZDwPdW08ZG2ocUulWmoWrbNr-IHAWkD5MJXG1OOxJNtPdSuXz1iK5L3Fk9L3JRqEVb1nr25YBmnWOOfWlrSlXfkK7t82bQIVwQO_zEKqhENBFlVaGf_COLzN5LYWdJkyuYPrzaEMuIHvjJ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Looks promising right!&#128519; We successfully fine-tuned a 4-bit quantized Mistral 7B model to a dataset. Next, to save this fine-tuned model, we can use Huggingface's push_to_hub for an online save or save_pretrained for a local save like the below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgCBJ8SXNid4-eLKDDmo6iSzsrZBH_S1KgydrH2Cd5jmC1XXIT9gD_cM3hqWQSLfSArxXbvq5vvqs361sb7P9ikHyS4nNwS9uuGRktEO9UuI7H2LG-SirWpOY-W4jN3oiuKVXIkkEkTADYQLAwBSHh9vvGks7X5AsFQYccwZwrsVFrSjc25wBEj0Y0I9Bme" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="83" data-original-width="707" src="https://blogger.googleusercontent.com/img/a/AVvXsEgCBJ8SXNid4-eLKDDmo6iSzsrZBH_S1KgydrH2Cd5jmC1XXIT9gD_cM3hqWQSLfSArxXbvq5vvqs361sb7P9ikHyS4nNwS9uuGRktEO9UuI7H2LG-SirWpOY-W4jN3oiuKVXIkkEkTADYQLAwBSHh9vvGks7X5AsFQYccwZwrsVFrSjc25wBEj0Y0I9Bme=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Next, to load this saved model, we can use the PeftModel class from the Hugging Face as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgiCk3LCPQA_AE2kE8ZbYI9Yep49VDwf8iOdhvQGTzh4qTY0wlMdn3pLkOua1eA9FFgaXcGqW_Tce7p-G2Nu9wvhXm_l6fwsHsosvlG1fIF5es_Bn4WuzOPcF0U6xDdnhQR9xlKTr2n1OFrH-evCpeFVEThgPJm_kXBPIx-7xVj0CqUCGcPuTTubp9vUwHd" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="87" data-original-width="612" src="https://blogger.googleusercontent.com/img/a/AVvXsEgiCk3LCPQA_AE2kE8ZbYI9Yep49VDwf8iOdhvQGTzh4qTY0wlMdn3pLkOua1eA9FFgaXcGqW_Tce7p-G2Nu9wvhXm_l6fwsHsosvlG1fIF5es_Bn4WuzOPcF0U6xDdnhQR9xlKTr2n1OFrH-evCpeFVEThgPJm_kXBPIx-7xVj0CqUCGcPuTTubp9vUwHd=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;That's it guys. In this blog post, we learned about the loading of a 4-bit quantized Mistral 7b model &amp;amp; fine-tuning of that model on a dataset. This process taught us some cutting-edge algorithms like QLORA &amp;amp; DPO. We learned about using a very powerful large language model on a simple T4 GPU-enabled colab notebook. Phew that was indeed quite a lot of learning today&#128119;. But don't need to wait, do your own hands-on by copying&amp;nbsp;&lt;a href="https://colab.research.google.com/drive/1Ps0PezRdR6MVAEsA2SbrNqYcg9fd0ynE?usp=sharing" target="_blank"&gt;&lt;b&gt;my shared notebook&lt;/b&gt;&lt;/a&gt;&amp;nbsp;in your colab environment and play with the parameters of the APIs I used in the notebook. In the next post, we will learn about the retrieval augmented generation in Gen AI, till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-xylU5KIFPp6b3YXQgOLLHt3Je5GVZl67XQX2c650KGs7tiBsHnZNaV5m5UBz-Eb4UF5aBuWE4ni1vJBU6lPTlnVgUsnxJI6ZweKVxeInZQynmmGfbXV52Fa4d2FHiuc93b4vYinY51RxXxjr0Ryv55TQbZVYqrlQRUv73oMpSVWsKjRwDYfI61ssJOk4/s72-c/Blank%20diagram.png" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">4</thr:total></item><item><title>Generative AI with LangChain: Basics</title><link>https://dsbyprateekg.blogspot.com/2024/01/generative-ai-with-langchain-basics.html</link><category>generative ai</category><category>langchain</category><category>large language models</category><category>llm</category><category>mistral</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Thu, 11 Jan 2024 17:37:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-4726878728140314298</guid><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;div style="text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: right;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNAdwg9vMo72yC9uxpiK3Ce9zhw6-C_RQCDcoOEjyyl7i9VGhblYii3ZgKTc_BDFyV7QStIPDqNMoCZxrGoqgYreS3h-zbyo36weI9UngJwUbCgPznbDym7VWPYMmKyOtdvOYrll3nlTe4c2868HQyEyIWrq5_FDIOsEcmMmCXE5lvfiTTYk-BhJzoloHn/s271/l1.png" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="145" data-original-width="271" height="145" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNAdwg9vMo72yC9uxpiK3Ce9zhw6-C_RQCDcoOEjyyl7i9VGhblYii3ZgKTc_BDFyV7QStIPDqNMoCZxrGoqgYreS3h-zbyo36weI9UngJwUbCgPznbDym7VWPYMmKyOtdvOYrll3nlTe4c2868HQyEyIWrq5_FDIOsEcmMmCXE5lvfiTTYk-BhJzoloHn/w258-h145/l1.png" width="258" /&gt;&lt;/a&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWuczd_myX0KaS8vS3uWwvZruXTTQBSAUFWrpPuna9Nrfpf-evHq4VyLSu4BP6oKcnf4ocla4khHF-oOEiWxoY6Vlmvidslk6_7vws7OFYruEdjhwcsXsjLMmR6eOjiVps1fomipZxLB2Z6grCFd0cdrkVtvy8bOezxeQjL4MfWg-HcEse3mXFeIZKnk2F/s368/llm9.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="227" data-original-width="368" height="148" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWuczd_myX0KaS8vS3uWwvZruXTTQBSAUFWrpPuna9Nrfpf-evHq4VyLSu4BP6oKcnf4ocla4khHF-oOEiWxoY6Vlmvidslk6_7vws7OFYruEdjhwcsXsjLMmR6eOjiVps1fomipZxLB2Z6grCFd0cdrkVtvy8bOezxeQjL4MfWg-HcEse3mXFeIZKnk2F/w240-h148/llm9.jpg" width="240" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;Wishing everyone a Happy New Year '24&#128519; I trust that you've found valuable insights in my previous blog posts. Embarking on a new learning adventure with this latest post, we'll delve into the realm of Generative AI applications using LangChain&#128170;. This article will initially cover the basics of Language Models and LangChain. Subsequent posts will guide you through hands-on experiences with various Generative AI use cases using LangChain. Let's kick off by exploring the essential fundamentals&#128129;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;What is a Large Language Model (LLM)?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;A large language model denotes a category of artificial intelligence (AI) models that undergo extensive training with extensive textual data to comprehend and produce language resembling human expression&#128583;. Such a large language model constitutes a scaled-up transformer model, often too extensive for execution on a single computer. Consequently, it is commonly deployed as a service accessible through an API or web interface. These models are typically constructed using deep learning methodologies and neural networks, with a primary emphasis on processing and generating natural language.&amp;nbsp;&lt;/p&gt;&lt;p&gt;As per NVIDIA- "Large language models largely represent a class of deep learning architectures called transformer networks. A transformer model is a neural network that learns context and meaning by tracking relationships in sequential data, like the words in this sentence".&amp;nbsp;The large language model component generates output (in this case, text) based on the prompt and input. These LLMs can further be fine-tuned to match the needs of specific conversational agents (e.g. if you are building a customer care chatbot). A few use cases of LLM are as below-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjV6UAG4uRyBkyL2ylafzMBwKcd_h9ffTvFpwmMDLrHDLxsBpuzvqKPFFso2IuaqxqZpx5iZymnazAljBkSQOQCIkIu63b_-0VgIPvWNkTH_3rfcd0bAiGifmwDdHTpiVFTqdWJ_VJMkVG64043Kq2tMYfm05StaYv4HdGGn0oDQGcbjHJEXGAroucJ_5ij" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="758" data-original-width="665" src="https://blogger.googleusercontent.com/img/a/AVvXsEjV6UAG4uRyBkyL2ylafzMBwKcd_h9ffTvFpwmMDLrHDLxsBpuzvqKPFFso2IuaqxqZpx5iZymnazAljBkSQOQCIkIu63b_-0VgIPvWNkTH_3rfcd0bAiGifmwDdHTpiVFTqdWJ_VJMkVG64043Kq2tMYfm05StaYv4HdGGn0oDQGcbjHJEXGAroucJ_5ij=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;How Large an LLM can be?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;As per Google- "Early language models could predict the probability of a single word but modern large language models can predict the probability of sentences, paragraphs, or even entire documents". The size and capability of language models have exploded over the last few years as computer memory, dataset size, and processing power increase, and more effective techniques for modeling longer text sequences are developed&#128165;.&amp;nbsp;&lt;/p&gt;&lt;p&gt;Here "Large" can refer either to the number of parameters in the model, or sometimes the number of words in the dataset. Here parameters are the weights the model learned during training, used to predict the next token in the sequence. You can refer following table to imagine the largeness of few available pre-trained LLMs-&lt;/p&gt;&lt;p&gt;&lt;google-sheets-html-origin&gt;&lt;/google-sheets-html-origin&gt;&lt;/p&gt;&lt;google-sheets-html-origin&gt;&lt;table border="1" cellpadding="0" cellspacing="0" data-sheets-root="1" dir="ltr" style="border-collapse: collapse; border: none; font-family: Arial; font-size: 10pt; table-layout: fixed; width: 0px;" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;colgroup&gt;&lt;col width="227"&gt;&lt;/col&gt;&lt;col width="262"&gt;&lt;/col&gt;&lt;col width="100"&gt;&lt;/col&gt;&lt;col width="126"&gt;&lt;/col&gt;&lt;/colgroup&gt;&lt;tbody&gt;&lt;tr style="height: 21px;"&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Model_Name&amp;quot;}" style="border: 1px solid rgb(0, 0, 0); font-weight: bold; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Model_Name&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Parameters&amp;quot;}" style="border-color: rgb(0, 0, 0) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; font-weight: bold; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Parameters&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;License&amp;quot;}" style="border-color: rgb(0, 0, 0) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; font-weight: bold; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;License&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Task&amp;quot;}" style="border-color: rgb(0, 0, 0) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; font-weight: bold; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Task&lt;/td&gt;&lt;/tr&gt;&lt;tr style="height: 21px;"&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;ChatGPT&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;ChatGPT&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;175 billion&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;175 billion&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Proprietary&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Proprietary&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Multi&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Multi&lt;/td&gt;&lt;/tr&gt;&lt;tr style="height: 21px;"&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;microsoft/phi-2&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;microsoft/phi-2&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;2.7 billion&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;2.7 billion&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;MIT&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;MIT&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Text Generation&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Text Generation&lt;/td&gt;&lt;/tr&gt;&lt;tr style="height: 21px;"&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;TinyLlama/TinyLlama-1.1B-Chat-v1.0&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;TinyLlama/TinyLlama-1.1B-Chat-v1.0&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;1.1B&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;1.1B&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Apache-2.0&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Apache-2.0&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Text Generation&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Text Generation&lt;/td&gt;&lt;/tr&gt;&lt;tr style="height: 21px;"&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;mistralai/Mixtral-8x7B-Instruct-v0.1&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;mistralai/Mixtral-8x7B-Instruct-v0.1&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;46.7B&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;46.7B&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Apache-2.0&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Apache-2.0&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Text Generation&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Text Generation&lt;/td&gt;&lt;/tr&gt;&lt;tr style="height: 21px;"&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;openai/whisper-large-v3&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;openai/whisper-large-v3&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;1 million hours of weakly labeled audio \nand 4 million hours of pseudolabeled audio&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;1 million hours of weakly labeled audio&lt;br /&gt;and 4 million hours of pseudolabeled audio&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Apache-2.0&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Apache-2.0&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Speech Recognition&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Speech Recognition&lt;/td&gt;&lt;/tr&gt;&lt;tr style="height: 21px;"&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;nvidia/parakeet-rnnt-1.1b&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;nvidia/parakeet-rnnt-1.1b&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;1.1B&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;1.1B&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;cc-by-4.0&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;cc-by-4.0&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Speech Recognition&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Speech Recognition&lt;/td&gt;&lt;/tr&gt;&lt;tr style="height: 21px;"&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;bert-base-uncased&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;bert-base-uncased&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;110 million&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;110 million&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Apache-2.0&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Apache-2.0&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Fill-Mask&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Fill-Mask&lt;/td&gt;&lt;/tr&gt;&lt;tr style="height: 21px;"&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;bert-large-uncased&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;bert-large-uncased&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;340 million&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;340 million&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Apache-2.0&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Apache-2.0&lt;/td&gt;&lt;td data-sheets-value="{&amp;quot;1&amp;quot;:2,&amp;quot;2&amp;quot;:&amp;quot;Fill-Mask&amp;quot;}" style="border-color: rgb(204, 204, 204) rgb(0, 0, 0) rgb(0, 0, 0) rgb(204, 204, 204); border-image: initial; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 3px; text-align: center; vertical-align: bottom;"&gt;Fill-Mask&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/google-sheets-html-origin&gt;&lt;p&gt;&lt;b&gt;How Do Large Language Models Work?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Large language models are trained using unsupervised learning&#128072;. With unsupervised learning, models can find previously unknown patterns in data using unlabelled datasets. This also eliminates the need for extensive data labeling, which is one of the biggest challenges in building AI models.&lt;/p&gt;&lt;p&gt;The ability of the foundation model to generate text for a wide variety of purposes without much instruction or training is called zero-shot learning&#128079;. Different variations of this capability include one-shot or few-shot learning, wherein the foundation model is fed one or a few examples illustrating how a task can be accomplished to understand and better perform on select use cases.&lt;/p&gt;&lt;p&gt;&lt;b&gt;What is Context Length and Token in LLM?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Context length is the number of tokens a language model can process at once. It is the maximum length of the input sequence.&amp;nbsp;A Token is the model’s way of representing a word with a series of numbers. 100 words is around 130 tokens. If a model does not recognize a word, it breaks the word into multiple tokens.&amp;nbsp;A model can only summarize an article no longer than the context length.&amp;nbsp;In chat apps, the context length dictates how much of the previous conversation is remembered. Language models like ChatGPT or Llama 2-Chat do not remember anything. They are stateless. They know the past conversation because they were included in your current input before feeding it to the model.&lt;/p&gt;&lt;p&gt;&lt;b&gt;What is LangChain?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Much like our familiarity with frameworks such as TensorFlow and PyTorch in the domains of image classification and object detection, LangChain serves as a framework tailored for building applications fueled by language models&#128582;. Specifically designed for working with Large Language Models (LLMs), LangChain simplifies the intricate process of developing extensive language models by presenting a systematic sequence of steps to generate text from a given input prompt. This toolkit manages various aspects, including prompt input, text generation, and manipulation of the generated output. Essentially, it facilitates the creation of conversational agents that leverage LLMs to generate coherent and natural language responses☝.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Components of LangChain Framework?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;This framework consists of several parts-&lt;/p&gt;&lt;p&gt;&lt;b&gt;1. LangChain Libraries:&lt;/b&gt; The Python and JavaScript libraries. Contains interfaces and integrations for a myriad of components, a basic run time for combining these components into chains and agents, and off-the-shelf implementations of chains and agents.&lt;/p&gt;&lt;p&gt;&lt;b&gt;2. LangChain Templates:&lt;/b&gt; A collection of easily deployable reference architectures for a wide variety of tasks.&lt;/p&gt;&lt;p&gt;&lt;b&gt;3. LangServe:&lt;/b&gt; A library for deploying LangChain chains as a REST API.&lt;/p&gt;&lt;p&gt;&lt;b&gt;4. LangSmith:&lt;/b&gt; A developer platform that lets you debug, test, evaluate, and monitor chains built on any LLM framework and seamlessly integrates with LangChain. Currently, it is in the beta stage as of writing this post on 10 Jan'24.&lt;/p&gt;&lt;p&gt;&lt;b&gt;How does LangChain work?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;LangChain provides a pipeline of steps that generate text from input prompts. The pipeline comprises seven main components: input (prompt templates), the large language model, agents, utilities, document loaders, chains, indexes, and memory.&lt;/p&gt;&lt;p&gt;The &lt;b&gt;input component&lt;/b&gt; specifies the input prompt or the initial input provided to the pipeline.&amp;nbsp;This comes in the form of a template that defines the structure of the prompt, including the format and content.&amp;nbsp;&lt;/p&gt;&lt;p&gt;LangChain enables access to a range of &lt;b&gt;pre-trained LLMs&lt;/b&gt; (e.g., GPT-3.5) trained on large datasets. The large language model component generates output (in this case, text) based on the prompt and input. These LLMs can further be fine-tuned to match the needs of specific conversational agents (e.g. if you are building a customer care chatbot).&lt;/p&gt;&lt;p&gt;With the&amp;nbsp;&lt;b&gt;Document Loaders&lt;/b&gt; module, we can ingest documents (e.g., PDFs, powerpoints, etc) into the LLM for further analysis (typically question answering).&lt;/p&gt;&lt;p&gt;LangChain provides an extensive collection of common utilities (&lt;b&gt;Utils&lt;/b&gt;) to use in our application, such as Python REPLs (LLM would generate code to calculate the answer, run that code to get the answer, and print it out), bash commands (e.g., to interface with the local system) or search engines, as well as a requests wrapper (to link a URL published post-2021 that ChatGPT doesn’t know about, for instance).&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Agents&lt;/b&gt;&amp;nbsp;are systems that use a language model to interact with other tools. They can be used to create chatbots or power the next generation of personal assistants.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Chains&lt;/b&gt;&amp;nbsp;provide an end-to-end pipeline for utilizing language models. These chains seamlessly integrate models, prompts, memory, parsing output, and debugging capabilities, offering a user-friendly interface.&amp;nbsp;&lt;/p&gt;&lt;p&gt;In LangChain, &lt;b&gt;Memory &lt;/b&gt;refers to the mechanism that stores and manages the conversation history between a user and the AI. It helps maintain context and coherency throughout the interaction, enabling the AI to generate more relevant and accurate responses.&lt;/p&gt;&lt;p&gt;&lt;b&gt;How to install LangChain?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;We can install the LangChain library using the pip or conda command. The pip command is&amp;nbsp;&lt;b style="font-style: italic;"&gt;pip install langchain &lt;/b&gt;and conda command is&amp;nbsp;&lt;i&gt;&lt;b&gt;conda install langchain -c conda-forge&lt;/b&gt;&lt;/i&gt;&lt;/p&gt;&lt;p&gt;For example,, in google colab notebook installation will look like the below-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjhZnZdHbZ_gGNvoizFd61gVKZpSY7icIv0Dvbjl8DXRIiqivPvcxW8-pFbhDNH2B4O4KWgLXzQ6ciF0x9Uz_DZQ5VOCpxttZspk3LNDcE2xtLp0P1zQM_V9VpXPknrfeQnBdmzrWwwmbctlsM0sTZRF8N6iG1bW2YrbU4Ih4qIw0373tTcjleZH3uWMhgY" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="57" data-original-width="310" height="59" src="https://blogger.googleusercontent.com/img/a/AVvXsEjhZnZdHbZ_gGNvoizFd61gVKZpSY7icIv0Dvbjl8DXRIiqivPvcxW8-pFbhDNH2B4O4KWgLXzQ6ciF0x9Uz_DZQ5VOCpxttZspk3LNDcE2xtLp0P1zQM_V9VpXPknrfeQnBdmzrWwwmbctlsM0sTZRF8N6iG1bW2YrbU4Ih4qIw0373tTcjleZH3uWMhgY" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;b&gt;Building something with LangChain?&lt;/b&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;LangChain enables building applications that connect external sources of data and computation to LLMs. We will&amp;nbsp;start with a simple LLM chain, which just relies on information in the prompt template to respond using one of the open-sourced LLM&amp;nbsp;&lt;b&gt;mistralai/Mistral-7B-v0.1&lt;/b&gt;. Mistral 7B is a new 7.3 billion parameter language model that represents a major advance in large language model (LLM) capabilities&#128064;. It has outperformed the 13 billion parameter Llama 2 model on all tasks and outperforms the 34 billion parameter Llama 1 on many benchmarks&#128074;. You can refer following table showing the number of parameters and minimum RAM required to run the two available Mistalai LLMs:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEi6tIhrFcquUIxPmvQa7r6WRnfXWadGgU86PxVbmVuYmISpKXoK7pVUCiXYAbW-gvAaCdwMIj5h0nJhsm9TSnlJCW7aI4dnd24_98OKhH5SEVvG6DH_RLFhSX-UhHuwDwyHPp34vYZSYq5En3gDSl-K8_5XPVffytqFizVWSKn9o95zccxoVN6vY9R0EiDO" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="182" data-original-width="982" src="https://blogger.googleusercontent.com/img/a/AVvXsEi6tIhrFcquUIxPmvQa7r6WRnfXWadGgU86PxVbmVuYmISpKXoK7pVUCiXYAbW-gvAaCdwMIj5h0nJhsm9TSnlJCW7aI4dnd24_98OKhH5SEVvG6DH_RLFhSX-UhHuwDwyHPp34vYZSYq5En3gDSl-K8_5XPVffytqFizVWSKn9o95zccxoVN6vY9R0EiDO=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;These models are available in&amp;nbsp;&lt;a href="https://huggingface.co/mistralai" target="_blank"&gt;&lt;b&gt;Hugging Face&lt;/b&gt;&lt;/a&gt;&amp;nbsp;and Hugging Face has inbuilt APIs to use these models easily. Let's start with installing the required library from the hugging face-&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEi3iA3IxBtyPCkJS1o6QVv4JRpS-ljEx66Wtfn0u-HYcEyeb1TobHpxcBuFTj2nzrgG9KWqnDY2WAuMWpWUAFVgy3LatuDdAvGAWkS7t2XybGr517q44mE3w8fWI0erc-bF7qEh1DFmUbAg-oS7dUMXEcyss03s_6quHCIFzQUWY83CnfbziOn3ZXtzgXHS" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="147" data-original-width="626" src="https://blogger.googleusercontent.com/img/a/AVvXsEi3iA3IxBtyPCkJS1o6QVv4JRpS-ljEx66Wtfn0u-HYcEyeb1TobHpxcBuFTj2nzrgG9KWqnDY2WAuMWpWUAFVgy3LatuDdAvGAWkS7t2XybGr517q44mE3w8fWI0erc-bF7qEh1DFmUbAg-oS7dUMXEcyss03s_6quHCIFzQUWY83CnfbziOn3ZXtzgXHS=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we will import the required libraries-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjWHYJTgdWp0GJTouZ2ARVomBqzcm6EB7VyjEOQapngygsb3Cc0acnOkQh23QyScqZcqlGrBV5aeVDxpuWO8h_3h5k2ZIIuSta4BQ3rwxlgfarQJ1vSqZi9t1TEfWNplW_zepah4CGqxfxAcfzlAkq43eA2Vo4EAGZIIWR3n6M_v2Z7pnCCNdswRzBMdn6c" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="90" data-original-width="593" src="https://blogger.googleusercontent.com/img/a/AVvXsEjWHYJTgdWp0GJTouZ2ARVomBqzcm6EB7VyjEOQapngygsb3Cc0acnOkQh23QyScqZcqlGrBV5aeVDxpuWO8h_3h5k2ZIIuSta4BQ3rwxlgfarQJ1vSqZi9t1TEfWNplW_zepah4CGqxfxAcfzlAkq43eA2Vo4EAGZIIWR3n6M_v2Z7pnCCNdswRzBMdn6c=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we will download the pre-trained LLM from the Hugging Face space along with its tokenizer. Please note that tokenization is a crucial text-processing step for large language models&#128072;. It splits text into smaller units called tokens that can be fed into the language model. Also to download the model you will need 16GB RAM at least. But free and Pro Google Colab accounts do not have enough CPU RAM to download the official model containing the weights and you will get Out of memory error&#128531; if you try to download the whole model at once-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjsXOzP7kqhoV5xQfg-X8IYIS65mKZXNso68QumUZkWvqAqhHpE3jYMbVw9mSt6R-LuPx-IWlhmfCIIB3gDTxwhMfgksxhfbz9Y3111fGFhEVFLha0RcSgy8HMKCyQHUMPm6gczHreTQqEXZpp5Ur2_l05C-i_golvH-ScQ8sVuJnpozGyg6Pm803sEWYxl" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="205" data-original-width="707" src="https://blogger.googleusercontent.com/img/a/AVvXsEjsXOzP7kqhoV5xQfg-X8IYIS65mKZXNso68QumUZkWvqAqhHpE3jYMbVw9mSt6R-LuPx-IWlhmfCIIB3gDTxwhMfgksxhfbz9Y3111fGFhEVFLha0RcSgy8HMKCyQHUMPm6gczHreTQqEXZpp5Ur2_l05C-i_golvH-ScQ8sVuJnpozGyg6Pm803sEWYxl=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhv-r6-nGIFRdZT-13GWlA6Bst4XuT4qNiWzZ9AZU-Aorl1vZaWYxPaCEB5TJ1wFAJLLZxYQQGlLw_fJJy36annq4R_JwbBbTtG36M6gQDGp8U3XFtz1ADGTaTh0MtsX2V8AEnxJ-flWG0DJJPkOUnrD2MFoViYKu-eFHNqHXGiLk0RA31Jo8Qhd0N8EPYg" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="548" data-original-width="1777" src="https://blogger.googleusercontent.com/img/a/AVvXsEhv-r6-nGIFRdZT-13GWlA6Bst4XuT4qNiWzZ9AZU-Aorl1vZaWYxPaCEB5TJ1wFAJLLZxYQQGlLw_fJJy36annq4R_JwbBbTtG36M6gQDGp8U3XFtz1ADGTaTh0MtsX2V8AEnxJ-flWG0DJJPkOUnrD2MFoViYKu-eFHNqHXGiLk0RA31Jo8Qhd0N8EPYg=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To solve the out-of-memory error or to be able to download the model in a resource-constrained notebook, we can download the sharded version of the original model that somebody had already done for us&#128512;. Before that install following these two libraries as well in colab and don't forget to restart the session-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhJVrSY_kI7mJLcQMK3Gb8aMRpL73D-jUvLRWboLjpgw48kg07AXBTmp_zoztbEiXnFhFO3flXeRaW1V5MyuGY0vihl5EE_ax3ZZ8epknOqMMgeQh4spiIPNFLI-QUwvA2h4v_JKcI9-dN7KRExyj90WcrHZRPBEuPuA0xZnRtsyh18Np3m2O1HdC_lPMLA" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="37" data-original-width="238" height="50" src="https://blogger.googleusercontent.com/img/a/AVvXsEhJVrSY_kI7mJLcQMK3Gb8aMRpL73D-jUvLRWboLjpgw48kg07AXBTmp_zoztbEiXnFhFO3flXeRaW1V5MyuGY0vihl5EE_ax3ZZ8epknOqMMgeQh4spiIPNFLI-QUwvA2h4v_JKcI9-dN7KRExyj90WcrHZRPBEuPuA0xZnRtsyh18Np3m2O1HdC_lPMLA" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhmNPeeVDb5rfwANiW8qQA445iPK7QO_VP6gR4xIeWpD6Fhv5o4b_p8ksLgLLxOxhKVykUHhKbAABRY_28vxGeYjKKtVv4aErZcCSnFEMtlEPpEQ1VQLMy2ZJt50XezRJFpzTBs0GNYYr-q-STmkm2Zj802wr1GnlTGPvnG1RDzAkI_vvQ9NWkLgfHPRN81" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="46" data-original-width="252" height="58" src="https://blogger.googleusercontent.com/img/a/AVvXsEhmNPeeVDb5rfwANiW8qQA445iPK7QO_VP6gR4xIeWpD6Fhv5o4b_p8ksLgLLxOxhKVykUHhKbAABRY_28vxGeYjKKtVv4aErZcCSnFEMtlEPpEQ1VQLMy2ZJt50XezRJFpzTBs0GNYYr-q-STmkm2Zj802wr1GnlTGPvnG1RDzAkI_vvQ9NWkLgfHPRN81" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;Now we will load the shared version of the model and it's tokenizer like below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEiNXK5v4UV9I5xCsK60mlCwTsSQrWBLz-cq_1H7ZXn_-BuE9l6Gfi7euG9APdVmMRD4tcrCslmyj6PuOibgbqP1cdizbQimxAPUVGUUlgwGQMrmzp75kPMTOCA9ogYPscqQ-9oWqu7BSt62QWH7xXuo-b13OtfcOV8E1RBZjsC0YGi10V4ZZi3qVatRgHyJ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="373" data-original-width="760" src="https://blogger.googleusercontent.com/img/a/AVvXsEiNXK5v4UV9I5xCsK60mlCwTsSQrWBLz-cq_1H7ZXn_-BuE9l6Gfi7euG9APdVmMRD4tcrCslmyj6PuOibgbqP1cdizbQimxAPUVGUUlgwGQMrmzp75kPMTOCA9ogYPscqQ-9oWqu7BSt62QWH7xXuo-b13OtfcOV8E1RBZjsC0YGi10V4ZZi3qVatRgHyJ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Here, the BitsAndBytes library is used to&amp;nbsp;create 4-bit quantization with NF4-type configuration to load our model in 4-bit precision. It will help us load the model faster and reduce the memory footprint&#128163;.&lt;/div&gt;&lt;div&gt;Once you run the above-mentioned code, downloading will be started without any out of memory error like the below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEg7RdbYZyv2GKaLifFLc1QdMyxO-yFmJtjLsMWAl-Qe7rI3X2z8pp-ILX3IsiCvAtNBQDVH4aXJQfUNAUGdLK4Je1PUUo5MBa4oVGpkUpLbuc1u4WgzJ9a2sL-b9qVTV6ZN8GrKUj0HALE85Ic0KACHngrcxOAh1JFnCsQT49A56f4XLYBFhpZMgwxHGtKz" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="430" data-original-width="987" src="https://blogger.googleusercontent.com/img/a/AVvXsEg7RdbYZyv2GKaLifFLc1QdMyxO-yFmJtjLsMWAl-Qe7rI3X2z8pp-ILX3IsiCvAtNBQDVH4aXJQfUNAUGdLK4Je1PUUo5MBa4oVGpkUpLbuc1u4WgzJ9a2sL-b9qVTV6ZN8GrKUj0HALE85Ic0KACHngrcxOAh1JFnCsQT49A56f4XLYBFhpZMgwxHGtKz=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Once we download the model successfully&#128077;, the next step is to test it with user given prompt and see how it answers that prompt query. Mistral expects the prompts in the following format-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJgQxj1e5hEWEDQxOF0lAfDdNvWYttP6pduplkB1w5A6hkv2DpYo5WNk3d0nEflsR3RPoyCq0ehC_PmJ1JMzhBrnEPBheM60QtV2drjbPKuj-Urj94ONSiOVjUGSXCo_L3DowbTKekVy4n_RndwBZCpxeZdMDe3m9wlUUqbFOksUwgO6A6ZyRWNjkQtaLL/s347/llm5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="202" data-original-width="347" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJgQxj1e5hEWEDQxOF0lAfDdNvWYttP6pduplkB1w5A6hkv2DpYo5WNk3d0nEflsR3RPoyCq0ehC_PmJ1JMzhBrnEPBheM60QtV2drjbPKuj-Urj94ONSiOVjUGSXCo_L3DowbTKekVy4n_RndwBZCpxeZdMDe3m9wlUUqbFOksUwgO6A6ZyRWNjkQtaLL/s16000/llm5.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;So let's create a sample prompt by following the above format:&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikxiAjI-LJHroNe82AxD89sq5g7HTUFsMNEi06BU-tgOGC2Kwa8UTwaO3DLeGtFtI-rWuPrj1qhEpVWOhwd9s2W0yahHtFm1RZUqD_VNZYMB0fmvm2HuwZur8H-_sFR0wyYP2jgAmYGn1KQirJWW4sYP8SsrIFLxNuwsmaOtNNWBfR7UwCA3IhgIbbJJ2J/s776/llm6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="102" data-original-width="776" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikxiAjI-LJHroNe82AxD89sq5g7HTUFsMNEi06BU-tgOGC2Kwa8UTwaO3DLeGtFtI-rWuPrj1qhEpVWOhwd9s2W0yahHtFm1RZUqD_VNZYMB0fmvm2HuwZur8H-_sFR0wyYP2jgAmYGn1KQirJWW4sYP8SsrIFLxNuwsmaOtNNWBfR7UwCA3IhgIbbJJ2J/s16000/llm6.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Now, we will pass out text to the tokenizer and then generate the answer to the user query from our model in the following way-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZNmXI9_iI9TKSqv8IM-9Ccw5PibPpOqZVLBCm_uxaIUFLB_ZdmfLQptJUrZjTUkNO2ArYgJ9XDVCyERncRy_GVj1EQInJRZKIxIwML58G39buTSTjnotIRw0TWpXb95DoNa8WGz0vG2AqsEeFUYzC4WOYen5ZuLh75Mx82hZ9bUSuxKmWQow8XfhLqFTw/s1035/llm5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="197" data-original-width="1035" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZNmXI9_iI9TKSqv8IM-9Ccw5PibPpOqZVLBCm_uxaIUFLB_ZdmfLQptJUrZjTUkNO2ArYgJ9XDVCyERncRy_GVj1EQInJRZKIxIwML58G39buTSTjnotIRw0TWpXb95DoNa8WGz0vG2AqsEeFUYzC4WOYen5ZuLh75Mx82hZ9bUSuxKmWQow8XfhLqFTw/s16000/llm5.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Here, we use the model's generate function to generate the text based on our query. The output of the above code will look like below-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJz9xO4aNjFvBFpeJUQSUOnHVVnE9avdeqKRk71CcLGFCHAXQ3xAIPPdHfpU4p2s3WnTnwK6QQ9XNJabErZBGOqVlRsI5I1-AvcTXlTfQzJvnfuGpMzrVTKIR9rggezsRs229v8GFvpuhgdM3QXk-pc5tab6FjjJWP0yFVVlbM12PNDFtaL2jXlPj94NaM/s775/llm7.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="628" data-original-width="775" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJz9xO4aNjFvBFpeJUQSUOnHVVnE9avdeqKRk71CcLGFCHAXQ3xAIPPdHfpU4p2s3WnTnwK6QQ9XNJabErZBGOqVlRsI5I1-AvcTXlTfQzJvnfuGpMzrVTKIR9rggezsRs229v8GFvpuhgdM3QXk-pc5tab6FjjJWP0yFVVlbM12PNDFtaL2jXlPj94NaM/s16000/llm7.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;As you can see, the answer is quite promising and accurate&#128076;. We have a list of models with their source links and explanations. This shows the power of text generation and understanding of human language by a machine&#128165;. The generative AI field is quite impressive, right?&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;That's it for today's guys! It's time for you to do some hands-on and play with the code using&amp;nbsp;&lt;a href="https://colab.research.google.com/drive/1lVU4MtQhL7z9p5UAKGbo8JGO9Qq0UqUM?usp=sharing" target="_blank"&gt;&lt;b&gt;this link&lt;/b&gt;&lt;/a&gt;. Copy the notebook in your space and do some experiments with it to get familiar with LangChain and Large Language Models&#128114;. In the next post, we will learn to fine-tune this Mistral model to our use case,&amp;nbsp;till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;/div&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNAdwg9vMo72yC9uxpiK3Ce9zhw6-C_RQCDcoOEjyyl7i9VGhblYii3ZgKTc_BDFyV7QStIPDqNMoCZxrGoqgYreS3h-zbyo36weI9UngJwUbCgPznbDym7VWPYMmKyOtdvOYrll3nlTe4c2868HQyEyIWrq5_FDIOsEcmMmCXE5lvfiTTYk-BhJzoloHn/s72-w258-h145-c/l1.png" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">10</thr:total></item><item><title>How to train a custom dataset with YOLOv7 for instance segmentation?</title><link>https://dsbyprateekg.blogspot.com/2022/09/how-to-train-custom-dataset-with-yolov7.html</link><category>PyTorch</category><category>segmentation</category><category>YOLOv7</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Fri, 16 Sep 2022 18:48:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-9220735969648010197</guid><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2FPLceMb2WKsx_FiXIRBM92Uqtlyw6IxXwHirKnXxuyo9dWkw9jmcWS8S9L3g-A0QJVVr-Al0jRU-yVkhLrMj8IgOzpkmEIocS9tbDLAMbHcE8qbnnuSAimGfG5cKDvo7VNAyUus1grMHMcpSD87tWJ-U4RYSR6auWtzwHG5xd7_THj7IIsB0Kbom3w/s469/yolo-seg.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="169" data-original-width="469" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2FPLceMb2WKsx_FiXIRBM92Uqtlyw6IxXwHirKnXxuyo9dWkw9jmcWS8S9L3g-A0QJVVr-Al0jRU-yVkhLrMj8IgOzpkmEIocS9tbDLAMbHcE8qbnnuSAimGfG5cKDvo7VNAyUus1grMHMcpSD87tWJ-U4RYSR6auWtzwHG5xd7_THj7IIsB0Kbom3w/s16000/yolo-seg.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;Another post starts with you beautiful people! It is overwhelming for me to see massive interest in my last three posts about the YOLOv7 series&#128147;. Your response keeps me motivated to share my learning with you all &#128157;. If you have not checked my previous posts about YOLOv7, then I am sharing here links to read those once and then proceed with this post-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol style="text-align: left;"&gt;&lt;li&gt;&lt;a href="https://dsbyprateekg.blogspot.com/2022/08/how-to-train-custom-dataset-and-make.html" target="_blank"&gt;&lt;b&gt;Train a custom dataset with YOLOv7&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://dsbyprateekg.blogspot.com/2022/09/export-your-custom-yolov7-pytorch-model.html" target="_blank"&gt;&lt;b&gt;Export custom YOLOv7 model to ONNX&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://dsbyprateekg.blogspot.com/2022/09/export-your-custom-yolov7-model-to.html" target="_blank"&gt;&lt;b&gt;Export custom YOLOv7 model to TensorRT&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;Till now we have learned about object detection with YOLOv7. In this post, we are going to learn how can we train a custom dataset for instance segmentation task with YOLOv7 &#128076;. For your information instance segmentation is the task of detecting and delineating each distinct object of interest appearing in an image. For our hands-on we need a dataset having images and their annotations in polygon format and of course in YOLO format. So I have found and downloaded the&amp;nbsp;American Sign Language dataset in the required format from&amp;nbsp;&lt;a href="https://universe.roboflow.com/team-roboflow/american-sign-language-poly" target="_blank"&gt;this link&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This dataset has&amp;nbsp;all letters A through Z in American Sign Language labeled with polygon labels. To our knowledge, the alphabet and its correspondence signs look like below-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOy02ZbvmBk7aksGVlfC8G_Sh0av7r9aTK_xLo0O45yDP4YF6xU98DMf_wsKy1WJOuraMmuPdIy42Mlz8ZSZprH51MIfEhJrVNDJJ5IPnxTbEJrDgj0M992X0GVNQ1ZsVyOV1BeEjoxcOFvc1vz6-gt-qUKzgYPb4y46ns8lNJ_5HXRJoJH6td4cicyA/s805/sign.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="805" data-original-width="607" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOy02ZbvmBk7aksGVlfC8G_Sh0av7r9aTK_xLo0O45yDP4YF6xU98DMf_wsKy1WJOuraMmuPdIy42Mlz8ZSZprH51MIfEhJrVNDJJ5IPnxTbEJrDgj0M992X0GVNQ1ZsVyOV1BeEjoxcOFvc1vz6-gt-qUKzgYPb4y46ns8lNJ_5HXRJoJH6td4cicyA/s16000/sign.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;For this post also I am using the google colab environment. Let's first install its required library and restart the colab notebook runtime once it is installed-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjpFnTosb1iSifIpA1WXzmdAvkUDs_BxVHEnp-7npgInFB1fED5Zg3TYaZ3QHOxkWTI03YyWlFjrw8U9xQ3njJkdheFB27y8NlyOykR-fc-3Cy_SMuc_oFLiQFoLyBDmjxJGFa661JDN7w6lrDhxcGh8_jJrAxWxqhMVF8_aBuUBNe7q2UBsaMjzm6wiA" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="47" data-original-width="705" src="https://blogger.googleusercontent.com/img/a/AVvXsEjpFnTosb1iSifIpA1WXzmdAvkUDs_BxVHEnp-7npgInFB1fED5Zg3TYaZ3QHOxkWTI03YyWlFjrw8U9xQ3njJkdheFB27y8NlyOykR-fc-3Cy_SMuc_oFLiQFoLyBDmjxJGFa661JDN7w6lrDhxcGh8_jJrAxWxqhMVF8_aBuUBNe7q2UBsaMjzm6wiA=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we will download the code repo of the YOLOv7's segmentation branch since while writing this post author has not officially declared the main branch for the segmentation code. So I have downloaded the&amp;nbsp;&lt;a href="https://github.com/WongKinYiu/yolov7/archive/refs/heads/u7.zip" target="_blank"&gt;&lt;b&gt;u7 branch&lt;/b&gt;&lt;/a&gt;&amp;nbsp;and uploaded it to my gdrive so that I can access it in my colab notebook &#128512;. After uploading the branch code to drive, I unzipped it like as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEh9GMkgDVtdi5Tq9buYYdaclzUHytc7p5gA5UIzSQUnL6gdW8LZ_oOpVD9J6pNGCbgeFJPvQCHDF-qkGYLhe6wzlGt_TAotFJC0apASQldm-2Kq59dplaTiTu-3DVX4J6LAqShtHYdj8G-Xsk7R42eYZHyQJI_8A8fKHqcwXAnp0PFXemfvwCkb5BKXIg" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="44" data-original-width="916" src="https://blogger.googleusercontent.com/img/a/AVvXsEh9GMkgDVtdi5Tq9buYYdaclzUHytc7p5gA5UIzSQUnL6gdW8LZ_oOpVD9J6pNGCbgeFJPvQCHDF-qkGYLhe6wzlGt_TAotFJC0apASQldm-2Kq59dplaTiTu-3DVX4J6LAqShtHYdj8G-Xsk7R42eYZHyQJI_8A8fKHqcwXAnp0PFXemfvwCkb5BKXIg=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The unzipped folder structure of the dataset will look like as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEiQ60Bk4IN9y9y9sUhvsHaXtPsNGvUUTgnycWc2hIDZLK0-a_hQ9aPxY4kxGdT1o9fXuwqLqRkPxKtgUyjQmbGGWJ5VvCEycVgyxSGroB8ikIp4EpseoUPLwPXrVhgb4ijtQNkutoEk3vAk5_w6l7ClaYXmQVMri0iZUseb1DqoLEa0WUewMOdPi9qaqA" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="213" data-original-width="868" src="https://blogger.googleusercontent.com/img/a/AVvXsEiQ60Bk4IN9y9y9sUhvsHaXtPsNGvUUTgnycWc2hIDZLK0-a_hQ9aPxY4kxGdT1o9fXuwqLqRkPxKtgUyjQmbGGWJ5VvCEycVgyxSGroB8ikIp4EpseoUPLwPXrVhgb4ijtQNkutoEk3vAk5_w6l7ClaYXmQVMri0iZUseb1DqoLEa0WUewMOdPi9qaqA=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Next, we will prepare the required YAML files for our instance segmentation training. First, we will create our configuration YAML file where will define the path of the training and validation images, the number of the target classes, and the names of the target classes as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEiC8cvKTGNN7AUekIRjHMaMpra6uG9m1JAOjVZnxgyQGRuEMxvYDsjo_-ZxmWyggKKYHNLEWDTKOZt49uizrrEYc82Zxdzpghb-BVDL9oQimjq0qdshw-GI8_ATvuKnq5WLaUJMtWCE5HnpiNqRO26l20rtJxqRuTvSPRxm1ABRdyU1PBe-q7C81l7B9A" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="254" data-original-width="1009" src="https://blogger.googleusercontent.com/img/a/AVvXsEiC8cvKTGNN7AUekIRjHMaMpra6uG9m1JAOjVZnxgyQGRuEMxvYDsjo_-ZxmWyggKKYHNLEWDTKOZt49uizrrEYc82Zxdzpghb-BVDL9oQimjq0qdshw-GI8_ATvuKnq5WLaUJMtWCE5HnpiNqRO26l20rtJxqRuTvSPRxm1ABRdyU1PBe-q7C81l7B9A=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Here I have provided the train and validation images path, and the number of classes as 26 since our classes are from the alphabet A-Z. Next, we will create a second YAML file having the content of&amp;nbsp;&lt;a href="https://github.com/WongKinYiu/yolov7/blob/u7/seg/models/segment/yolov7-seg.yaml" target="_blank"&gt;it's default yolov7-seg.yaml&lt;/a&gt;&amp;nbsp;but with one difference- the value of 'nc' parameter from 80 to 26-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgJbAVQ5s6rhuzGHihaGWgls4H0RNPfYCelxNZOD6w17mhMPxm5CNc7_Js3ZbQjp5yMS8OyteHOaNb32IgiwiWDG4Dbn2MG2SUgbtpAA3rWu1U1Xsn6k8WRX5lMB0TrwfqAPr9-nW17-OO5OLc2Fh-_dlfLIA2zHClnmAYm7eSQl88WUPoIMpJP58iesQ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="599" data-original-width="656" src="https://blogger.googleusercontent.com/img/a/AVvXsEgJbAVQ5s6rhuzGHihaGWgls4H0RNPfYCelxNZOD6w17mhMPxm5CNc7_Js3ZbQjp5yMS8OyteHOaNb32IgiwiWDG4Dbn2MG2SUgbtpAA3rWu1U1Xsn6k8WRX5lMB0TrwfqAPr9-nW17-OO5OLc2Fh-_dlfLIA2zHClnmAYm7eSQl88WUPoIMpJP58iesQ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we will create the hyperparameter YAML file having the same content as its&amp;nbsp;&lt;a href="https://github.com/WongKinYiu/yolov7/blob/u7/seg/data/hyps/hyp.scratch-high.yaml" target="_blank"&gt;&lt;b&gt;official hyp.scratch-high.yaml&lt;/b&gt;&lt;/a&gt;&amp;nbsp;as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEi5h9bI3wU18P3iHxAah_s_9RprkGqiVijdH-k6FaIWQ4ep_mhtXYN_WsjterbxTL1OUEinl236-yZqXoblWRMfZ98QiOUH3gX9r9zNBOoArf1TfvDs_YGNWImc_wobxNjrgDEV1jEY9p8o7wEHuSS8m4pHja8ME2FHZSUwR_h6_ohYCs2m3akXMhK9Ng" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="469" data-original-width="711" src="https://blogger.googleusercontent.com/img/a/AVvXsEi5h9bI3wU18P3iHxAah_s_9RprkGqiVijdH-k6FaIWQ4ep_mhtXYN_WsjterbxTL1OUEinl236-yZqXoblWRMfZ98QiOUH3gX9r9zNBOoArf1TfvDs_YGNWImc_wobxNjrgDEV1jEY9p8o7wEHuSS8m4pHja8ME2FHZSUwR_h6_ohYCs2m3akXMhK9Ng=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we will download the pre-trained weight file for the training as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgWDMaT02gKSyLJdwEV4u-iz8KtyS_zuMiIBZem8kQmO6shS7L5DGurBStc6tnmUVLJ8uuO959GawSfH_ctWEsXJrbil0lFtMz5KukEXrxT2Q_RPJpMV89ivZ1vWsSbIruMFAbSdteom2HuHSDUKms2i0tUzwxUyQ4DkCmfUjpJh7s_FhvsH2dQeXoITA" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="55" data-original-width="774" src="https://blogger.googleusercontent.com/img/a/AVvXsEgWDMaT02gKSyLJdwEV4u-iz8KtyS_zuMiIBZem8kQmO6shS7L5DGurBStc6tnmUVLJ8uuO959GawSfH_ctWEsXJrbil0lFtMz5KukEXrxT2Q_RPJpMV89ivZ1vWsSbIruMFAbSdteom2HuHSDUKms2i0tUzwxUyQ4DkCmfUjpJh7s_FhvsH2dQeXoITA=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Now we are ready to start our instance segmentation training with the below command-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEiOGgT7iw1BFBPAQAfiPg0doPcNyHPoeLQbK0mSIjxJ1YrBUDqYTeIYBswaJbDKQ6zFU-g2DHnIooceg4WLj2owjaBOi6N4riJ0JWm3w2z4QgoRQdALM6dm1tzNQQTh7XWje16KlwCa7ATH4-fk6jGjI4NcG8-osgLycvIZGDfw7o24JwWJamoTrUjHDg" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="44" data-original-width="649" src="https://blogger.googleusercontent.com/img/a/AVvXsEiOGgT7iw1BFBPAQAfiPg0doPcNyHPoeLQbK0mSIjxJ1YrBUDqYTeIYBswaJbDKQ6zFU-g2DHnIooceg4WLj2owjaBOi6N4riJ0JWm3w2z4QgoRQdALM6dm1tzNQQTh7XWje16KlwCa7ATH4-fk6jGjI4NcG8-osgLycvIZGDfw7o24JwWJamoTrUjHDg=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEisTrHx-B91NnvCU91Dypa4HbaPoNuRmF6i_Ywt_jrSZKI83NNxpQqQ55WDjxe02t6wXxCShV34VU8vmqc0DE2_AUGYpHz9LtsWiHMMvdhGkBxaNInmh4er8OXA9WtbkaUj5Rgg6x9VinnUTUwSX03u1Uwkemy26D8rTlic0UGbU7741Y-fmfH47T5T5A" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="60" data-original-width="875" src="https://blogger.googleusercontent.com/img/a/AVvXsEisTrHx-B91NnvCU91Dypa4HbaPoNuRmF6i_Ywt_jrSZKI83NNxpQqQ55WDjxe02t6wXxCShV34VU8vmqc0DE2_AUGYpHz9LtsWiHMMvdhGkBxaNInmh4er8OXA9WtbkaUj5Rgg6x9VinnUTUwSX03u1Uwkemy26D8rTlic0UGbU7741Y-fmfH47T5T5A=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Here, change the .yaml files paths and the weight file path as per yours. In my colab environment for 100 epochs it has taken 1 hour to complete. for better accuracy, you should train it for 300 epochs for this dataset. Once the training is completed, you will see the following output in the console-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjfj-RhqsmStekt2X0fLnHoPivmTXnfKjuHyxK-WvxCBIEKyTmokfp4UZUsSIo5Wt3SeEcU1EIojIlgCyoQdyC-P6375Rpf0wOzAXQ2h8AwCakvn-crex9t5aWK9fN7RqHuuZuGsDev0DBQOoPbA7JUZqqt1mxhzz0o3ikY8uQpr3p3EaC67adAr1YcKg" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="678" data-original-width="1344" src="https://blogger.googleusercontent.com/img/a/AVvXsEjfj-RhqsmStekt2X0fLnHoPivmTXnfKjuHyxK-WvxCBIEKyTmokfp4UZUsSIo5Wt3SeEcU1EIojIlgCyoQdyC-P6375Rpf0wOzAXQ2h8AwCakvn-crex9t5aWK9fN7RqHuuZuGsDev0DBQOoPbA7JUZqqt1mxhzz0o3ikY8uQpr3p3EaC67adAr1YcKg=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;After completion of the training, let's check the trained model on a test image as below-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEj1LL9PfmLpwp8O5aeF0uQg1zo5DirKcaGo7ztPLnP8y0bTL0KTCgcEHpC4vPOIAoqjHmE8S7aOFHTAm1MfaC10K-OmqRGOn91l0h33nu8tWo9Mz83mUSXJoeDooz3HIrcGaQo7GH6SED_FRkBXylzc7roV8rSL8X4wTQofCU0ceqZI5tycg2nTK4h6FA" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="94" data-original-width="734" src="https://blogger.googleusercontent.com/img/a/AVvXsEj1LL9PfmLpwp8O5aeF0uQg1zo5DirKcaGo7ztPLnP8y0bTL0KTCgcEHpC4vPOIAoqjHmE8S7aOFHTAm1MfaC10K-OmqRGOn91l0h33nu8tWo9Mz83mUSXJoeDooz3HIrcGaQo7GH6SED_FRkBXylzc7roV8rSL8X4wTQofCU0ceqZI5tycg2nTK4h6FA=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;You will see the following output in the console after successfully running the above command-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEg-E9zZrDxi6gWJpbwNalB946N2rNpIEVq30uaGCr-FegakjSZ0mnCPcNDFMFg8jj7aU4A-oZGVJHDJu-zF7JC4RB6lsaV0tDnCxAd47rmg0NRGo_IGZR25xO4rfB1GFRRIReTP5KveQbH87ShUCiaMZuGY3PDeIxDh8_VPrk2QAFNL3yzv6zuHDtRvnQ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="124" data-original-width="873" src="https://blogger.googleusercontent.com/img/a/AVvXsEg-E9zZrDxi6gWJpbwNalB946N2rNpIEVq30uaGCr-FegakjSZ0mnCPcNDFMFg8jj7aU4A-oZGVJHDJu-zF7JC4RB6lsaV0tDnCxAd47rmg0NRGo_IGZR25xO4rfB1GFRRIReTP5KveQbH87ShUCiaMZuGY3PDeIxDh8_VPrk2QAFNL3yzv6zuHDtRvnQ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Let's open the processed image and see what it looks like-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEi_BJ7nXaQNJEOjjUil70zqoAPss27FwRrPOaQmKwvIyF1ZK6-kDSr886nhuJ-WIII0by7pln2KysCuKuLhPJHrejczLy1AkPPiwonqxWIiEOF5Ptan5whXeLVd7dcaeWB2484rxzZJ569TDBz-VzBXwKRTUCIlHUEDd3JS0BaeW2HyXHeYVq3CPkEZ2Q" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="73" data-original-width="779" src="https://blogger.googleusercontent.com/img/a/AVvXsEi_BJ7nXaQNJEOjjUil70zqoAPss27FwRrPOaQmKwvIyF1ZK6-kDSr886nhuJ-WIII0by7pln2KysCuKuLhPJHrejczLy1AkPPiwonqxWIiEOF5Ptan5whXeLVd7dcaeWB2484rxzZJ569TDBz-VzBXwKRTUCIlHUEDd3JS0BaeW2HyXHeYVq3CPkEZ2Q=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgUNgqwm_X51JXwSBTrSNpFmz6_oaOSASm9LF8IKYnrtJS_ZWJV308_RlQX6cgIYhpH3zCqW0WNT8ZJC1a3NT6Igbwj9keQAjfXxxWGUEZlNjRWE-JvVRVUrgoIbGIRCyeS7pNvXrklVA3bVko8qd7P0V6VELG4FGTQLUjI8nwdjrWypW3YaLmkyVyCbg" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="411" data-original-width="513" height="240" src="https://blogger.googleusercontent.com/img/a/AVvXsEgUNgqwm_X51JXwSBTrSNpFmz6_oaOSASm9LF8IKYnrtJS_ZWJV308_RlQX6cgIYhpH3zCqW0WNT8ZJC1a3NT6Igbwj9keQAjfXxxWGUEZlNjRWE-JvVRVUrgoIbGIRCyeS7pNvXrklVA3bVko8qd7P0V6VELG4FGTQLUjI8nwdjrWypW3YaLmkyVyCbg" width="300" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;See our trained model is able to do the instance segmentation and is able to predict the target class of a sign &#128165;. Let me share a few more output images-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhsdIsHqC_VEq09lCvUbVjvoEC-8BOv1BxbLCsb91yZLci41qdKMIuTBVWBBsDWldG77rOIHFMg9OKDUqUDnVTIyUCq0Aq4hdbdGsLs4FKSKs6a07Q8ITUy_-7GOCJ1nC-3HPL4DOPvGmaWKJWNTnT2ycWaOKKzMFBjCXpWKY9wcPfjFlRMvXHW7PDUfQ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="396" data-original-width="383" height="240" src="https://blogger.googleusercontent.com/img/a/AVvXsEhsdIsHqC_VEq09lCvUbVjvoEC-8BOv1BxbLCsb91yZLci41qdKMIuTBVWBBsDWldG77rOIHFMg9OKDUqUDnVTIyUCq0Aq4hdbdGsLs4FKSKs6a07Q8ITUy_-7GOCJ1nC-3HPL4DOPvGmaWKJWNTnT2ycWaOKKzMFBjCXpWKY9wcPfjFlRMvXHW7PDUfQ" width="232" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjmZfnrXVs2WNXECn6DKD0RetuqHo6ka3tY0TXXOywJVYFgNuD73VPzOUDMOUuHdJ_0psBLVWxg2cS_7uFX1rxZM62tZPcywyXZwcA3gZeeiHjHI3TMK6IOy8FSbETguF0hEg9I5XDzS0WjjDD10sicykxCUmffpoBK8AMlfUZwlowqZQWrCwdsZyiq2Q" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="442" data-original-width="268" height="240" src="https://blogger.googleusercontent.com/img/a/AVvXsEjmZfnrXVs2WNXECn6DKD0RetuqHo6ka3tY0TXXOywJVYFgNuD73VPzOUDMOUuHdJ_0psBLVWxg2cS_7uFX1rxZM62tZPcywyXZwcA3gZeeiHjHI3TMK6IOy8FSbETguF0hEg9I5XDzS0WjjDD10sicykxCUmffpoBK8AMlfUZwlowqZQWrCwdsZyiq2Q" width="146" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhoAD_RH1DX4a23gvxa7wQfOwgeZ6SqUBocyeIS06fbyxrGqkJvF4Aiwld7AFphGTvlh05LPIe2iDFW0eJJ2W7CKxgzN7KuUuAOJctOex78awvqfdMRrwHoXcNe5dnpqDYswmSCjBNWaotHkZPl5T5MBLKr4b-wTy8_erHawoluKE5xkMpzQ7wmbDVE3Q" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="457" data-original-width="487" height="240" src="https://blogger.googleusercontent.com/img/a/AVvXsEhoAD_RH1DX4a23gvxa7wQfOwgeZ6SqUBocyeIS06fbyxrGqkJvF4Aiwld7AFphGTvlh05LPIe2iDFW0eJJ2W7CKxgzN7KuUuAOJctOex78awvqfdMRrwHoXcNe5dnpqDYswmSCjBNWaotHkZPl5T5MBLKr4b-wTy8_erHawoluKE5xkMpzQ7wmbDVE3Q" width="256" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjSljR0jYP0BqZwYet1AzUpLbRseSRX_Pbdz1EAwnZ9CA7uP0Eo1J7AhB7Nv-T1lDH_DfzvTbGmwICM5ea6GHhiIrhlGu9BOLKaXtfUndXjo5vR_vPhEZQo5oh-FBVMfMiRoDL0Wnaz504659pHrDaiC6YFZqsBL_3EZ_rtrALC2PCHnLI6QuYbjvOEtw" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="449" data-original-width="508" height="240" src="https://blogger.googleusercontent.com/img/a/AVvXsEjSljR0jYP0BqZwYet1AzUpLbRseSRX_Pbdz1EAwnZ9CA7uP0Eo1J7AhB7Nv-T1lDH_DfzvTbGmwICM5ea6GHhiIrhlGu9BOLKaXtfUndXjo5vR_vPhEZQo5oh-FBVMfMiRoDL0Wnaz504659pHrDaiC6YFZqsBL_3EZ_rtrALC2PCHnLI6QuYbjvOEtw" width="272" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Indeed we have a strong sign language detection model. Let's run the inference on a video-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgh8Q8Yk2wvfrxzVeD-Bn_uaBCm7UgVpJAz4LJLqJAs4T0e5UVfl8ZM3kJsNrfWayIk4QnZ9kKW9Xejogwkl4-xtOgvPmJvX9JYimVqpmuio-svOilWjC10Zilk5efPEDPYguSBg4MOkPeG_82YS1etuNa3wBcrX16RVzNsLvtxZTxd4cXakP14Hes1qQ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="88" data-original-width="729" src="https://blogger.googleusercontent.com/img/a/AVvXsEgh8Q8Yk2wvfrxzVeD-Bn_uaBCm7UgVpJAz4LJLqJAs4T0e5UVfl8ZM3kJsNrfWayIk4QnZ9kKW9Xejogwkl4-xtOgvPmJvX9JYimVqpmuio-svOilWjC10Zilk5efPEDPYguSBg4MOkPeG_82YS1etuNa3wBcrX16RVzNsLvtxZTxd4cXakP14Hes1qQ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The output in the console will look like the below after a successful run of the above command-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEjmLvoiiZIulEOSrHa_mv6AIK37EyjCWr6__8ORZQrJwm5dswvm2J0Dw06KSI2rfIC0PTGSrYzaM0PuvBlHhpB2kvIuBaOuM6s97I732SjTeQR4i_iOSjWPgfAsHldf5HnpoLcAJuX0B1veijOD5A-zyzdszaAriJD_xYQY9anngTFF5feVP1Y-90gZfQ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="84" data-original-width="1121" src="https://blogger.googleusercontent.com/img/a/AVvXsEjmLvoiiZIulEOSrHa_mv6AIK37EyjCWr6__8ORZQrJwm5dswvm2J0Dw06KSI2rfIC0PTGSrYzaM0PuvBlHhpB2kvIuBaOuM6s97I732SjTeQR4i_iOSjWPgfAsHldf5HnpoLcAJuX0B1veijOD5A-zyzdszaAriJD_xYQY9anngTFF5feVP1Y-90gZfQ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Let's open this video in our colab notebook-&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgdphX0vmZo1rZphZc7O7xZh2pJTljRzEwSBIZljLLV6uusP_DwBcfpD18mBMD0eE8ly7vyxbiC1atWCzv9NDyUzXT6fj-rkUb18_cHMsnJQFV82MgOT6f_q7T10ObynEa9FFS6l9S1JWI_Ut4LfS8z7fz82Co_L1r13TmAn4ePgePp6waJf01JqFI3Qw" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="486" data-original-width="723" src="https://blogger.googleusercontent.com/img/a/AVvXsEgdphX0vmZo1rZphZc7O7xZh2pJTljRzEwSBIZljLLV6uusP_DwBcfpD18mBMD0eE8ly7vyxbiC1atWCzv9NDyUzXT6fj-rkUb18_cHMsnJQFV82MgOT6f_q7T10ObynEa9FFS6l9S1JWI_Ut4LfS8z7fz82Co_L1r13TmAn4ePgePp6waJf01JqFI3Qw=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;And it will show the processed video file as below-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='320' height='266' src='https://www.blogger.com/video.g?token=AD6v5dwc4gj4dIJIP8oR4ta0I18EWYcEhTOmI2_sTCT98VM2oar3-yzdWUNMYkuLIH0ooJ0RKbhMDcOAKT0cOon00A' class='b-hbp-video b-uploaded' frameborder='0'&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;That's it for today's guys! We have learned another useful technique with YOLOv7. Here I am sharing&amp;nbsp;&lt;a href="https://colab.research.google.com/drive/1XXH_OoTxTpjhM9WYR0hoLgfHuB5I5hUw?usp=sharing" target="_blank"&gt;&lt;b&gt;my colab notebook&lt;/b&gt;&lt;/a&gt;&amp;nbsp;for this tutorial. So no need to rest, just copy the notebook into your colab environment and try it with your own dataset. In my next post I will share something useful again till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2FPLceMb2WKsx_FiXIRBM92Uqtlyw6IxXwHirKnXxuyo9dWkw9jmcWS8S9L3g-A0QJVVr-Al0jRU-yVkhLrMj8IgOzpkmEIocS9tbDLAMbHcE8qbnnuSAimGfG5cKDvo7VNAyUus1grMHMcpSD87tWJ-U4RYSR6auWtzwHG5xd7_THj7IIsB0Kbom3w/s72-c/yolo-seg.jpg" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">18</thr:total><enclosure length="-1" type="application/zip" url="https://github.com/WongKinYiu/yolov7/archive/refs/heads/u7.zip"/><itunes:explicit>no</itunes:explicit><itunes:subtitle>&amp;nbsp;Another post starts with you beautiful people! It is overwhelming for me to see massive interest in my last three posts about the YOLOv7 series&#128147;. Your response keeps me motivated to share my learning with you all &#128157;. If you have not checked my previous posts about YOLOv7, then I am sharing here links to read those once and then proceed with this post- Train a custom dataset with YOLOv7 Export custom YOLOv7 model to ONNX Export custom YOLOv7 model to TensorRT Till now we have learned about object detection with YOLOv7. In this post, we are going to learn how can we train a custom dataset for instance segmentation task with YOLOv7 &#128076;. For your information instance segmentation is the task of detecting and delineating each distinct object of interest appearing in an image. For our hands-on we need a dataset having images and their annotations in polygon format and of course in YOLO format. So I have found and downloaded the&amp;nbsp;American Sign Language dataset in the required format from&amp;nbsp;this link. This dataset has&amp;nbsp;all letters A through Z in American Sign Language labeled with polygon labels. To our knowledge, the alphabet and its correspondence signs look like below- For this post also I am using the google colab environment. Let's first install its required library and restart the colab notebook runtime once it is installed- Next, we will download the code repo of the YOLOv7's segmentation branch since while writing this post author has not officially declared the main branch for the segmentation code. So I have downloaded the&amp;nbsp;u7 branch&amp;nbsp;and uploaded it to my gdrive so that I can access it in my colab notebook &#128512;. After uploading the branch code to drive, I unzipped it like as below- The unzipped folder structure of the dataset will look like as below- Next, we will prepare the required YAML files for our instance segmentation training. First, we will create our configuration YAML file where will define the path of the training and validation images, the number of the target classes, and the names of the target classes as below- Here I have provided the train and validation images path, and the number of classes as 26 since our classes are from the alphabet A-Z. Next, we will create a second YAML file having the content of&amp;nbsp;it's default yolov7-seg.yaml&amp;nbsp;but with one difference- the value of 'nc' parameter from 80 to 26- Next, we will create the hyperparameter YAML file having the same content as its&amp;nbsp;official hyp.scratch-high.yaml&amp;nbsp;as below- Next, we will download the pre-trained weight file for the training as below- Now we are ready to start our instance segmentation training with the below command- Here, change the .yaml files paths and the weight file path as per yours. In my colab environment for 100 epochs it has taken 1 hour to complete. for better accuracy, you should train it for 300 epochs for this dataset. Once the training is completed, you will see the following output in the console- After completion of the training, let's check the trained model on a test image as below- You will see the following output in the console after successfully running the above command- Let's open the processed image and see what it looks like- See our trained model is able to do the instance segmentation and is able to predict the target class of a sign &#128165;. Let me share a few more output images- Indeed we have a strong sign language detection model. Let's run the inference on a video- The output in the console will look like the below after a successful run of the above command- Let's open this video in our colab notebook- And it will show the processed video file as below- That's it for today's guys! We have learned another useful technique with YOLOv7. Here I am sharing&amp;nbsp;my colab notebook&amp;nbsp;for this tutorial. So no need to rest, just copy the notebook into your colab environment and try it with your own dataset. In my next post I will share something useful again till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;</itunes:subtitle><itunes:author>noreply@blogger.com (Learn Data Science with PrateekG)</itunes:author><itunes:summary>&amp;nbsp;Another post starts with you beautiful people! It is overwhelming for me to see massive interest in my last three posts about the YOLOv7 series&#128147;. Your response keeps me motivated to share my learning with you all &#128157;. If you have not checked my previous posts about YOLOv7, then I am sharing here links to read those once and then proceed with this post- Train a custom dataset with YOLOv7 Export custom YOLOv7 model to ONNX Export custom YOLOv7 model to TensorRT Till now we have learned about object detection with YOLOv7. In this post, we are going to learn how can we train a custom dataset for instance segmentation task with YOLOv7 &#128076;. For your information instance segmentation is the task of detecting and delineating each distinct object of interest appearing in an image. For our hands-on we need a dataset having images and their annotations in polygon format and of course in YOLO format. So I have found and downloaded the&amp;nbsp;American Sign Language dataset in the required format from&amp;nbsp;this link. This dataset has&amp;nbsp;all letters A through Z in American Sign Language labeled with polygon labels. To our knowledge, the alphabet and its correspondence signs look like below- For this post also I am using the google colab environment. Let's first install its required library and restart the colab notebook runtime once it is installed- Next, we will download the code repo of the YOLOv7's segmentation branch since while writing this post author has not officially declared the main branch for the segmentation code. So I have downloaded the&amp;nbsp;u7 branch&amp;nbsp;and uploaded it to my gdrive so that I can access it in my colab notebook &#128512;. After uploading the branch code to drive, I unzipped it like as below- The unzipped folder structure of the dataset will look like as below- Next, we will prepare the required YAML files for our instance segmentation training. First, we will create our configuration YAML file where will define the path of the training and validation images, the number of the target classes, and the names of the target classes as below- Here I have provided the train and validation images path, and the number of classes as 26 since our classes are from the alphabet A-Z. Next, we will create a second YAML file having the content of&amp;nbsp;it's default yolov7-seg.yaml&amp;nbsp;but with one difference- the value of 'nc' parameter from 80 to 26- Next, we will create the hyperparameter YAML file having the same content as its&amp;nbsp;official hyp.scratch-high.yaml&amp;nbsp;as below- Next, we will download the pre-trained weight file for the training as below- Now we are ready to start our instance segmentation training with the below command- Here, change the .yaml files paths and the weight file path as per yours. In my colab environment for 100 epochs it has taken 1 hour to complete. for better accuracy, you should train it for 300 epochs for this dataset. Once the training is completed, you will see the following output in the console- After completion of the training, let's check the trained model on a test image as below- You will see the following output in the console after successfully running the above command- Let's open the processed image and see what it looks like- See our trained model is able to do the instance segmentation and is able to predict the target class of a sign &#128165;. Let me share a few more output images- Indeed we have a strong sign language detection model. Let's run the inference on a video- The output in the console will look like the below after a successful run of the above command- Let's open this video in our colab notebook- And it will show the processed video file as below- That's it for today's guys! We have learned another useful technique with YOLOv7. Here I am sharing&amp;nbsp;my colab notebook&amp;nbsp;for this tutorial. So no need to rest, just copy the notebook into your colab environment and try it with your own dataset. In my next post I will share something useful again till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;</itunes:summary><itunes:keywords>PyTorch, segmentation, YOLOv7</itunes:keywords></item><item><title>Export your custom YOLOv7 model to TensorRT</title><link>https://dsbyprateekg.blogspot.com/2022/09/export-your-custom-yolov7-model-to.html</link><category>ONNX</category><category>TensorRT</category><category>YOLOv7</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Thu, 8 Sep 2022 17:45:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-2491178868426514776</guid><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuRm8XfvvaUV6zw0qrhGAOdrLLBx8Ng-eAx24xVme0USQIG4WcQqYFzdrJOuNr3hFCyQNnqaEZcOF1cLmUM-ZQveqwbCm1VohhF7Q9TKWg7NXuzgqtWP-Cq_qI8dQHhCqVa2SOucfzpFdHIaMjE3phx-VsN0KuxV366eSBDpxM9tT1-fR7p7M9h27AfA/s696/yolov7_tensorrt.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="76" data-original-width="696" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuRm8XfvvaUV6zw0qrhGAOdrLLBx8Ng-eAx24xVme0USQIG4WcQqYFzdrJOuNr3hFCyQNnqaEZcOF1cLmUM-ZQveqwbCm1VohhF7Q9TKWg7NXuzgqtWP-Cq_qI8dQHhCqVa2SOucfzpFdHIaMjE3phx-VsN0KuxV366eSBDpxM9tT1-fR7p7M9h27AfA/s16000/yolov7_tensorrt.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Another post starts with you beautiful people! Thanks for the great response to my&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2022/09/export-your-custom-yolov7-pytorch-model.html" target="_blank"&gt;&lt;b&gt;last post&lt;/b&gt;&lt;/a&gt;&amp;nbsp;where we successfully exported a custom-trained YOLOv7 model to ONNX format.&amp;nbsp; In this post, we are going to move one step further to achieve high-performance inference using&amp;nbsp;&lt;a href="https://developer.nvidia.com/tensorrt" target="_blank"&gt;&lt;b&gt;NVIDIA TensorRT&lt;/b&gt;&lt;/a&gt;. We will learn how can we export our YOLOv7 ONNX model to a TensorRT format.&lt;/p&gt;&lt;p&gt;NVIDIA TensorRT is&amp;nbsp;an SDK for high-performance deep learning inference, includes a deep learning inference optimizer and runtime that delivers low latency and high throughput for inference applications. This file format is widely used in case you need to deploy your model into edge devices but with high inference speed. So knowledge of exporting your YOLOv7 model to TensorRT format is very beneficial learning.&lt;/p&gt;&lt;p&gt;Please note the exportation of the model is environmentally sensitive. It is recommended to use the same environment where you have trained the original YOLOv7 model. For this post as well I am using the same Google Colab environment which I used for&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2022/08/how-to-train-custom-dataset-and-make.html" target="_blank"&gt;&lt;b&gt;YOLOv7 training&lt;/b&gt;&lt;/a&gt;&amp;nbsp;as well as for&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2022/09/export-your-custom-yolov7-pytorch-model.html" target="_blank"&gt;&lt;b&gt;exporting to ONNX&lt;/b&gt;&lt;/a&gt;. As a first step, we will install the required dependencies as below-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEg_dQtP2QY4aO9j2jSReC2rUCQ9z98Mz1WalCAZ67Ov7mA8-10T9KzIM0YJhMr4fwHeRsQ0BESkvcf-dri4Om2Jo-ks9B5Rl3uPY1yqeMhmk4h7zG4ZD1FzRb0Ov3gex4YeUAsCYJJUlW6Y3RUaKKggefi1oK0foj2Nx9txhVN1eRrkgIb4kQ76jBMzSQ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="230" data-original-width="437" src="https://blogger.googleusercontent.com/img/a/AVvXsEg_dQtP2QY4aO9j2jSReC2rUCQ9z98Mz1WalCAZ67Ov7mA8-10T9KzIM0YJhMr4fwHeRsQ0BESkvcf-dri4Om2Jo-ks9B5Rl3uPY1yqeMhmk4h7zG4ZD1FzRb0Ov3gex4YeUAsCYJJUlW6Y3RUaKKggefi1oK0foj2Nx9txhVN1eRrkgIb4kQ76jBMzSQ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Once the above-required libraries are installed successfully, as a second step we will download the original YOLOv7 code repo for exporting our custom trained YOLOv7 model to ONNX format. Please skip this step if you have already exported it using my&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2022/09/export-your-custom-yolov7-pytorch-model.html" target="_blank"&gt;last post&lt;/a&gt;.&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgvGM5t86EIRe0KnMQWb_NQLcT2br6qqXqSjcQCYWjDy8LFvkyhTMFvcfWp9tJE6jGkKT_MTLcSwUwnI4QPX91xbO-YPquMNlMLfuNnFGENsoVU9s0JoCtffwonocCxXHwOXLINKbUns34MjIn934U4--yr7UxP87jW0hJGcGXXEREDj9OrOVrLyrAdvA" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="80" data-original-width="466" src="https://blogger.googleusercontent.com/img/a/AVvXsEgvGM5t86EIRe0KnMQWb_NQLcT2br6qqXqSjcQCYWjDy8LFvkyhTMFvcfWp9tJE6jGkKT_MTLcSwUwnI4QPX91xbO-YPquMNlMLfuNnFGENsoVU9s0JoCtffwonocCxXHwOXLINKbUns34MjIn934U4--yr7UxP87jW0hJGcGXXEREDj9OrOVrLyrAdvA=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Let's export the YOLOv7 weight file to ONNX format using the below command -&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEj3EPWz2BJQ_eY1FuP-KK5VQcTr5UkYJqB0ox0vOET9o5NuI8x0SI_yQXvr7SEWUqV-X-mqabOyWyvKB18m9BM_0kCSFYfntIN0pIUxDCptNZua1EFx9vawNCqz4v1Jff0qQ1Sr780uCENWBtKtopo6VsJhl89jheppjAYNYWDNjsSL8el41wzMSbbDiw" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="68" data-original-width="1324" src="https://blogger.googleusercontent.com/img/a/AVvXsEj3EPWz2BJQ_eY1FuP-KK5VQcTr5UkYJqB0ox0vOET9o5NuI8x0SI_yQXvr7SEWUqV-X-mqabOyWyvKB18m9BM_0kCSFYfntIN0pIUxDCptNZua1EFx9vawNCqz4v1Jff0qQ1Sr780uCENWBtKtopo6VsJhl89jheppjAYNYWDNjsSL8el41wzMSbbDiw=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;In the above command, replace the .pt file with your own YOLOv7 file and remove the zero after --device argument if you don't have GPU. Once successfully finished, this command will save a .onnx format file in the same location where your .pt file is. Now as a third step we will clone another code repo for TensorRT export as below-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhyqkD4_2nAs48P-HfP9TU7qeICjfs0rKGgg7q8bmPmguaBZC8TvKuyjt7rcwLBLf8gmAx3hTkosGxXF5bNOZirsoeTnoOQJv24DtmwS_V-3CNp75zV0lzF8BVokenftgBk197OTIAOjXn6t4HGb1v78gDNwtShOHHASk7qFw_grIUd-8SlYZX-KWbEAg" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="78" data-original-width="597" src="https://blogger.googleusercontent.com/img/a/AVvXsEhyqkD4_2nAs48P-HfP9TU7qeICjfs0rKGgg7q8bmPmguaBZC8TvKuyjt7rcwLBLf8gmAx3hTkosGxXF5bNOZirsoeTnoOQJv24DtmwS_V-3CNp75zV0lzF8BVokenftgBk197OTIAOjXn6t4HGb1v78gDNwtShOHHASk7qFw_grIUd-8SlYZX-KWbEAg=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;After successfully cloning the repo, as a fourth step, we will use its export.py file to convert our ONNX file to TensorRT format as below-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEhs35h9tVOCfjklajIjxx8pwjVSfLY5ddWrvwfGnVasMhgDOEobToQ4bwfs24eKLHY0DWkkzn_fCpfns8Qohq_xe8h4lVyuYHm1Sp2f9aFjYMqMUf3wY112FVU2mh9Y_7joJN_b1HIc2AyRVlaV2Fxc_e0ZYgL6bpD62SdVcnO4ufrdWxGdioS_FL4QEw" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="57" data-original-width="1266" src="https://blogger.googleusercontent.com/img/a/AVvXsEhs35h9tVOCfjklajIjxx8pwjVSfLY5ddWrvwfGnVasMhgDOEobToQ4bwfs24eKLHY0DWkkzn_fCpfns8Qohq_xe8h4lVyuYHm1Sp2f9aFjYMqMUf3wY112FVU2mh9Y_7joJN_b1HIc2AyRVlaV2Fxc_e0ZYgL6bpD62SdVcnO4ufrdWxGdioS_FL4QEw=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;Here again, replace the .onnx file with your own file and rename .trt file name as per your wish, the final exported file will be saved with that name. Once the above command runs successfully, you will see the following output at the end of the console-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgmWsQaudkaBt3tEkKlnCWqis8y4ceGF1nQKHLejF2bIZu77NILxEIxLyuiFnSgdQffgLZZW3i-ACVplmxdMa7a0gwV5gGDogqy77ttfjPKnqxJyPB7BsWm1WoFSBOl4GKa1BYOPBmVZgqoGvKVt6xVROsvx7WWWw9MnLPONmHKnUKS3dZLfKo-45cWEw" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="50" data-original-width="1322" src="https://blogger.googleusercontent.com/img/a/AVvXsEgmWsQaudkaBt3tEkKlnCWqis8y4ceGF1nQKHLejF2bIZu77NILxEIxLyuiFnSgdQffgLZZW3i-ACVplmxdMa7a0gwV5gGDogqy77ttfjPKnqxJyPB7BsWm1WoFSBOl4GKa1BYOPBmVZgqoGvKVt6xVROsvx7WWWw9MnLPONmHKnUKS3dZLfKo-45cWEw=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;So our onnx to trt exportation is done successfully ✌. Next as the fifth and last step, we need an inference script to load this .trt file and test it on a test image. Let's understand the inference script with code snippets-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgNd9KfzKktDi9WMoVrOfE96kMKcrkq6BDISIPCt1Jg1UzZgTrVEWp9W6KatkJoiSZJPoGb-D-YYZ5_ufoW7gae1kxZit5ZnDCNl7Hknbc7vPvu5RnuXcePwXeQXKJiUG4oYf1lgPRjLpAjIPnQ82ArkE-aGp8EJmz2w-jyXWRlTdqDwvGMCQ6GTSFfEw" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="126" data-original-width="285" src="https://blogger.googleusercontent.com/img/a/AVvXsEgNd9KfzKktDi9WMoVrOfE96kMKcrkq6BDISIPCt1Jg1UzZgTrVEWp9W6KatkJoiSZJPoGb-D-YYZ5_ufoW7gae1kxZit5ZnDCNl7Hknbc7vPvu5RnuXcePwXeQXKJiUG4oYf1lgPRjLpAjIPnQ82ArkE-aGp8EJmz2w-jyXWRlTdqDwvGMCQ6GTSFfEw=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;First, we have imported the required library for the inference. Next, we will write a class with a few necessary functions. Inside this class, Our first function is for loading and initializing the TensorRT engine as below-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEiSyOBUz5QAeqsKUQj5m0ior-cc3cd_IYOWmSg0sPHY1Ce0Dxyj3stJ7sxl9vHn5HEl32DzsIqLOkr3eQ6pA_2qmKPdgSYgyvOoX_PMeB6tQGt-edix7X7gL5u19wFKYwSOCyhwTcKaElApZoMMBiN3xPIVq7-BVN0QAEoeOmqz1prrk7eNdN4U0-yd_Q" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="595" data-original-width="751" src="https://blogger.googleusercontent.com/img/a/AVvXsEiSyOBUz5QAeqsKUQj5m0ior-cc3cd_IYOWmSg0sPHY1Ce0Dxyj3stJ7sxl9vHn5HEl32DzsIqLOkr3eQ6pA_2qmKPdgSYgyvOoX_PMeB6tQGt-edix7X7gL5u19wFKYwSOCyhwTcKaElApZoMMBiN3xPIVq7-BVN0QAEoeOmqz1prrk7eNdN4U0-yd_Q=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;Our next four functions are for running the inference, transforming the input image into the required format, detecting the target object with confidence scores, printing the frame per second, and the bounding box coordinates as below-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgLlR2he1KVAuADWGZn8ahHA_8N4CXxBiOdpVNa7CmTHG2prMa4paXFMkkmzgLjJi1h0jfh3Hy8CTEPJiBLSxzRlCiT4xUGTrZdVXCF5Lvu2SYg-NY0O-Dxbhjj0bnpR5fFQJzdyoIzw05_6tBfY3SWNAKKUYSXGYbDUeHpNDOCQwoQCVJtcP0KgrQdgQ" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="738" data-original-width="1137" src="https://blogger.googleusercontent.com/img/a/AVvXsEgLlR2he1KVAuADWGZn8ahHA_8N4CXxBiOdpVNa7CmTHG2prMa4paXFMkkmzgLjJi1h0jfh3Hy8CTEPJiBLSxzRlCiT4xUGTrZdVXCF5Lvu2SYg-NY0O-Dxbhjj0bnpR5fFQJzdyoIzw05_6tBfY3SWNAKKUYSXGYbDUeHpNDOCQwoQCVJtcP0KgrQdgQ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEh9tyTc9GN8M2kYx26bkD299-lAP3sJxEG3xcULIzR8Csajt1aH1NTQiZFkUgCEbnVuxueBx-ytec5x60PVm2jiugEKXzOak3hAchXZBz-_LUdkbRZmDQeL0jxyFq2Qk89_cOSaSG2r8rRpKSaLapFbnXJLELLcd58SPHft61n2gWm1Xgv8JJffvZjFWg" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="237" data-original-width="521" src="https://blogger.googleusercontent.com/img/a/AVvXsEh9tyTc9GN8M2kYx26bkD299-lAP3sJxEG3xcULIzR8Csajt1aH1NTQiZFkUgCEbnVuxueBx-ytec5x60PVm2jiugEKXzOak3hAchXZBz-_LUdkbRZmDQeL0jxyFq2Qk89_cOSaSG2r8rRpKSaLapFbnXJLELLcd58SPHft61n2gWm1Xgv8JJffvZjFWg=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEiFmBPHi9xOggN12DMDqVqK2LGetrAMvyydnv_7u5KCyiGoutoOpV68Pj_zNbxAOE4Fz0Xe9Xgub7w9CXTVZYc3PAtQyjHufbb3z8BHNrwJEtwt9zQtQLnWuCofFXMRPDiqCavwAmN-THNbxz9kpGsdIF-AbUuwjVNlQWMTmNBDnEc4cC2rhSf4s_m-5w" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="554" data-original-width="766" src="https://blogger.googleusercontent.com/img/a/AVvXsEiFmBPHi9xOggN12DMDqVqK2LGetrAMvyydnv_7u5KCyiGoutoOpV68Pj_zNbxAOE4Fz0Xe9Xgub7w9CXTVZYc3PAtQyjHufbb3z8BHNrwJEtwt9zQtQLnWuCofFXMRPDiqCavwAmN-THNbxz9kpGsdIF-AbUuwjVNlQWMTmNBDnEc4cC2rhSf4s_m-5w=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next, we will create a list of colors for use in the bounding box. A sample list will be looked like as below-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgKRAQ4Bf_Vgs0LPb92BBUTazEpgipDKsYjkANyD6etLr6ku_Z7OFJxArrUduPbhHnsoOz6Gbul2nMqZQrcrJyRKtSWV_vySoUtcUYwFTiQ0xxKfrHrJDMkW3BMZ13IxGaill_py9FU2f7_u2o2BB8z8Tv9saCgAmXHVwLVAH3JQWrLhjTDOWercjl0TA" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="198" data-original-width="349" height="182" src="https://blogger.googleusercontent.com/img/a/AVvXsEgKRAQ4Bf_Vgs0LPb92BBUTazEpgipDKsYjkANyD6etLr6ku_Z7OFJxArrUduPbhHnsoOz6Gbul2nMqZQrcrJyRKtSWV_vySoUtcUYwFTiQ0xxKfrHrJDMkW3BMZ13IxGaill_py9FU2f7_u2o2BB8z8Tv9saCgAmXHVwLVAH3JQWrLhjTDOWercjl0TA" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;And our last function is for visualizing the bounding boxes around our target class as below-&lt;/div&gt;&lt;div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEg7Qr8G17Lb8v8QIKfdjG2dS00fwXfXyewBspjPcqljq4g0uVKKHzcyav0l9j76HK8gLSapqsG5KaQuXav_uhH4Rjl1ILFflgCN5r5lisCFcJciYebR_ObXPRzMPmh946yqj4ea1EdlJ5KbSW9Qxw6lbUzybUg6bGJl6P-cg5Mqxu0OEgLORZg0UeEcHA" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="734" data-original-width="870" src="https://blogger.googleusercontent.com/img/a/AVvXsEg7Qr8G17Lb8v8QIKfdjG2dS00fwXfXyewBspjPcqljq4g0uVKKHzcyav0l9j76HK8gLSapqsG5KaQuXav_uhH4Rjl1ILFflgCN5r5lisCFcJciYebR_ObXPRzMPmh946yqj4ea1EdlJ5KbSW9Qxw6lbUzybUg6bGJl6P-cg5Mqxu0OEgLORZg0UeEcHA=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Now we are ready to make an inference. Let's test an image with our TensorRT model-&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEirwENa0ERQxJnRgdmA-Nf041rsM-oi6dGTBQWBXE1hcDba1HVTLrFsXig71usM2d9cMXdvYUMmjEd0a5K9yVW1Xwp3Y89CQlJRklk2R1P7h59b2OrwYx6_YWS0DaqCP0c0lICO3TsyYGYFQhRIRMDQA8PnW1FREajGgEaO3kmnp0EPEgCg1c3SA63bTA" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="146" data-original-width="940" src="https://blogger.googleusercontent.com/img/a/AVvXsEirwENa0ERQxJnRgdmA-Nf041rsM-oi6dGTBQWBXE1hcDba1HVTLrFsXig71usM2d9cMXdvYUMmjEd0a5K9yVW1Xwp3Y89CQlJRklk2R1P7h59b2OrwYx6_YWS0DaqCP0c0lICO3TsyYGYFQhRIRMDQA8PnW1FREajGgEaO3kmnp0EPEgCg1c3SA63bTA=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Here in the first line pass your .trt file as engine_path and provide your test image in the inference() function. Next, I used matplotlib library to visualize the processed image and it looks like the below-&lt;div&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEgK9Iy2Gs-zkAgs6ynE-lwItEJWsKIYciSFxUHGIu7B_ZwfiNEk3Wk_GqwYTMlaQRrYmCwm4Xla10gTDAs_lTHbiCQFWD0nYKymHGu8BgE3Jfn0XvLxjoH-z4Rv3q2eszeXbgTcEClNeo7HyV_QXHdUL8-DcwhybW78yAbx7BHcmhJWPGpFpCYv1slTmQ" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="28" data-original-width="205" src="https://blogger.googleusercontent.com/img/a/AVvXsEgK9Iy2Gs-zkAgs6ynE-lwItEJWsKIYciSFxUHGIu7B_ZwfiNEk3Wk_GqwYTMlaQRrYmCwm4Xla10gTDAs_lTHbiCQFWD0nYKymHGu8BgE3Jfn0XvLxjoH-z4Rv3q2eszeXbgTcEClNeo7HyV_QXHdUL8-DcwhybW78yAbx7BHcmhJWPGpFpCYv1slTmQ=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjj4CGamMlmrkR45SbG6F3tN1yy7SWPDAg01WQ0ifv_uf6tkFHUB-y5IfFGzmOovvi1c726QxzS5ruDaqAMFqmgkzMDd87Qyqp3SVip_mfGMKL37usmMBxIyn3FEbA4JB5LQWO80NEgQSc5huqEovsLKhBQaCRDiWLEst0GNXkUMT7l23eXueLeV4WmXA/s750/yolov7_to_onnx_tensorrt.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="749" data-original-width="750" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjj4CGamMlmrkR45SbG6F3tN1yy7SWPDAg01WQ0ifv_uf6tkFHUB-y5IfFGzmOovvi1c726QxzS5ruDaqAMFqmgkzMDd87Qyqp3SVip_mfGMKL37usmMBxIyn3FEbA4JB5LQWO80NEgQSc5huqEovsLKhBQaCRDiWLEst0GNXkUMT7l23eXueLeV4WmXA/s16000/yolov7_to_onnx_tensorrt.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;It looks perfect&#128076;. I have got the result with 39.9 FPS which is very fast &#128170; considering my original YOLOv7 custom model was based on its&amp;nbsp;&lt;a href="https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e_training.pt" target="_blank"&gt;biggest size pre-trained weight&lt;/a&gt;&amp;nbsp;which performance on MS COCO dataset was as below-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/a/AVvXsEiqN767gJQw1-b4d_T8cx8ZBS8YQJOCOr_5ImWTGT7XnQkYJcT70OryX-KjABRmGbAxiMS800Lz4Fe9bX8yLgIu2DP50k_mrDaeE_5LcGicXvApjlByOqBdgIOCEMaD3UkqFBetm_iv5FCdHgDXid43WwGFjh4S3RoKBV0NLFnbLG7A3XobZVYctubf6w" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" data-original-height="50" data-original-width="916" src="https://blogger.googleusercontent.com/img/a/AVvXsEiqN767gJQw1-b4d_T8cx8ZBS8YQJOCOr_5ImWTGT7XnQkYJcT70OryX-KjABRmGbAxiMS800Lz4Fe9bX8yLgIu2DP50k_mrDaeE_5LcGicXvApjlByOqBdgIOCEMaD3UkqFBetm_iv5FCdHgDXid43WwGFjh4S3RoKBV0NLFnbLG7A3XobZVYctubf6w=s16000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;So if you train your dataset with its&amp;nbsp;&lt;a href="https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7_training.pt" target="_blank"&gt;smallest version&lt;/a&gt;&amp;nbsp;and then convert it to TensorRT, you may get 100+ FPS. Sounds interesting&#128558; Then why are you waiting? Start&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2022/08/how-to-train-custom-dataset-and-make.html" target="_blank"&gt;training your own YOLOv7 model&lt;/a&gt;,&amp;nbsp;export it to the TensorRT engine, and compare the inference speed in CPU/GPU-based devices. Like always, you can find this post's colab notebook&amp;nbsp;&lt;a href="https://colab.research.google.com/drive/1vvSKwpIGHfrVKRfScK2rpbrEua0pk5qN?usp=sharing" target="_blank"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;. That's it for today guys. In my next post I will again share something useful&amp;nbsp;till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuRm8XfvvaUV6zw0qrhGAOdrLLBx8Ng-eAx24xVme0USQIG4WcQqYFzdrJOuNr3hFCyQNnqaEZcOF1cLmUM-ZQveqwbCm1VohhF7Q9TKWg7NXuzgqtWP-Cq_qI8dQHhCqVa2SOucfzpFdHIaMjE3phx-VsN0KuxV366eSBDpxM9tT1-fR7p7M9h27AfA/s72-c/yolov7_tensorrt.jpg" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total><enclosure length="333898497" type="application/octet-stream" url="https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e_training.pt"/><itunes:explicit>no</itunes:explicit><itunes:subtitle>&amp;nbsp; Another post starts with you beautiful people! Thanks for the great response to my&amp;nbsp;last post&amp;nbsp;where we successfully exported a custom-trained YOLOv7 model to ONNX format.&amp;nbsp; In this post, we are going to move one step further to achieve high-performance inference using&amp;nbsp;NVIDIA TensorRT. We will learn how can we export our YOLOv7 ONNX model to a TensorRT format. NVIDIA TensorRT is&amp;nbsp;an SDK for high-performance deep learning inference, includes a deep learning inference optimizer and runtime that delivers low latency and high throughput for inference applications. This file format is widely used in case you need to deploy your model into edge devices but with high inference speed. So knowledge of exporting your YOLOv7 model to TensorRT format is very beneficial learning. Please note the exportation of the model is environmentally sensitive. It is recommended to use the same environment where you have trained the original YOLOv7 model. For this post as well I am using the same Google Colab environment which I used for&amp;nbsp;YOLOv7 training&amp;nbsp;as well as for&amp;nbsp;exporting to ONNX. As a first step, we will install the required dependencies as below- Once the above-required libraries are installed successfully, as a second step we will download the original YOLOv7 code repo for exporting our custom trained YOLOv7 model to ONNX format. Please skip this step if you have already exported it using my&amp;nbsp;last post. Let's export the YOLOv7 weight file to ONNX format using the below command - In the above command, replace the .pt file with your own YOLOv7 file and remove the zero after --device argument if you don't have GPU. Once successfully finished, this command will save a .onnx format file in the same location where your .pt file is. Now as a third step we will clone another code repo for TensorRT export as below- After successfully cloning the repo, as a fourth step, we will use its export.py file to convert our ONNX file to TensorRT format as below- Here again, replace the .onnx file with your own file and rename .trt file name as per your wish, the final exported file will be saved with that name. Once the above command runs successfully, you will see the following output at the end of the console- So our onnx to trt exportation is done successfully ✌. Next as the fifth and last step, we need an inference script to load this .trt file and test it on a test image. Let's understand the inference script with code snippets- &amp;nbsp;First, we have imported the required library for the inference. Next, we will write a class with a few necessary functions. Inside this class, Our first function is for loading and initializing the TensorRT engine as below- Our next four functions are for running the inference, transforming the input image into the required format, detecting the target object with confidence scores, printing the frame per second, and the bounding box coordinates as below- Next, we will create a list of colors for use in the bounding box. A sample list will be looked like as below- And our last function is for visualizing the bounding boxes around our target class as below- Now we are ready to make an inference. Let's test an image with our TensorRT model- Here in the first line pass your .trt file as engine_path and provide your test image in the inference() function. Next, I used matplotlib library to visualize the processed image and it looks like the below- It looks perfect&#128076;. I have got the result with 39.9 FPS which is very fast &#128170; considering my original YOLOv7 custom model was based on its&amp;nbsp;biggest size pre-trained weight&amp;nbsp;which performance on MS COCO dataset was as below- So if you train your dataset with its&amp;nbsp;smallest version&amp;nbsp;and then convert it to TensorRT, you may get 100+ FPS. Sounds interesting&#128558; Then why are you waiting? Start&amp;nbsp;training your own YOLOv7 model,&amp;nbsp;export it to the TensorRT engine, and compare the inference speed in CPU/GPU-based devices. Like always, you can find this post's colab notebook&amp;nbsp;here. That's it for today guys. In my next post I will again share something useful&amp;nbsp;till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;</itunes:subtitle><itunes:author>noreply@blogger.com (Learn Data Science with PrateekG)</itunes:author><itunes:summary>&amp;nbsp; Another post starts with you beautiful people! Thanks for the great response to my&amp;nbsp;last post&amp;nbsp;where we successfully exported a custom-trained YOLOv7 model to ONNX format.&amp;nbsp; In this post, we are going to move one step further to achieve high-performance inference using&amp;nbsp;NVIDIA TensorRT. We will learn how can we export our YOLOv7 ONNX model to a TensorRT format. NVIDIA TensorRT is&amp;nbsp;an SDK for high-performance deep learning inference, includes a deep learning inference optimizer and runtime that delivers low latency and high throughput for inference applications. This file format is widely used in case you need to deploy your model into edge devices but with high inference speed. So knowledge of exporting your YOLOv7 model to TensorRT format is very beneficial learning. Please note the exportation of the model is environmentally sensitive. It is recommended to use the same environment where you have trained the original YOLOv7 model. For this post as well I am using the same Google Colab environment which I used for&amp;nbsp;YOLOv7 training&amp;nbsp;as well as for&amp;nbsp;exporting to ONNX. As a first step, we will install the required dependencies as below- Once the above-required libraries are installed successfully, as a second step we will download the original YOLOv7 code repo for exporting our custom trained YOLOv7 model to ONNX format. Please skip this step if you have already exported it using my&amp;nbsp;last post. Let's export the YOLOv7 weight file to ONNX format using the below command - In the above command, replace the .pt file with your own YOLOv7 file and remove the zero after --device argument if you don't have GPU. Once successfully finished, this command will save a .onnx format file in the same location where your .pt file is. Now as a third step we will clone another code repo for TensorRT export as below- After successfully cloning the repo, as a fourth step, we will use its export.py file to convert our ONNX file to TensorRT format as below- Here again, replace the .onnx file with your own file and rename .trt file name as per your wish, the final exported file will be saved with that name. Once the above command runs successfully, you will see the following output at the end of the console- So our onnx to trt exportation is done successfully ✌. Next as the fifth and last step, we need an inference script to load this .trt file and test it on a test image. Let's understand the inference script with code snippets- &amp;nbsp;First, we have imported the required library for the inference. Next, we will write a class with a few necessary functions. Inside this class, Our first function is for loading and initializing the TensorRT engine as below- Our next four functions are for running the inference, transforming the input image into the required format, detecting the target object with confidence scores, printing the frame per second, and the bounding box coordinates as below- Next, we will create a list of colors for use in the bounding box. A sample list will be looked like as below- And our last function is for visualizing the bounding boxes around our target class as below- Now we are ready to make an inference. Let's test an image with our TensorRT model- Here in the first line pass your .trt file as engine_path and provide your test image in the inference() function. Next, I used matplotlib library to visualize the processed image and it looks like the below- It looks perfect&#128076;. I have got the result with 39.9 FPS which is very fast &#128170; considering my original YOLOv7 custom model was based on its&amp;nbsp;biggest size pre-trained weight&amp;nbsp;which performance on MS COCO dataset was as below- So if you train your dataset with its&amp;nbsp;smallest version&amp;nbsp;and then convert it to TensorRT, you may get 100+ FPS. Sounds interesting&#128558; Then why are you waiting? Start&amp;nbsp;training your own YOLOv7 model,&amp;nbsp;export it to the TensorRT engine, and compare the inference speed in CPU/GPU-based devices. Like always, you can find this post's colab notebook&amp;nbsp;here. That's it for today guys. In my next post I will again share something useful&amp;nbsp;till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;</itunes:summary><itunes:keywords>ONNX, TensorRT, YOLOv7</itunes:keywords></item><item><title>Export your custom YOLOv7 PyTorch model to ONNX</title><link>https://dsbyprateekg.blogspot.com/2022/09/export-your-custom-yolov7-pytorch-model.html</link><category>ONNX</category><category>YOLOv7</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Fri, 2 Sep 2022 22:10:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-3619175350649432388</guid><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZFAX-q7HhJ4AyfWPVICwa1NhwO_QvOISBFKbrUF6_TrJCTRnhHhh3Xt3LI9pPDaR0whuxm26wMyBCqvfwPB6-aQs9x_6Dz7lDdXs1OiwTECLw5F_P4-xr6Yv-NDAtohqDW_5CSrg12dn5uPNY6EJiWr7kS9gYBCFpnEe9I8gSgnWx6BqjFXLodRvVNw/s755/yolo.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="119" data-original-width="755" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZFAX-q7HhJ4AyfWPVICwa1NhwO_QvOISBFKbrUF6_TrJCTRnhHhh3Xt3LI9pPDaR0whuxm26wMyBCqvfwPB6-aQs9x_6Dz7lDdXs1OiwTECLw5F_P4-xr6Yv-NDAtohqDW_5CSrg12dn5uPNY6EJiWr7kS9gYBCFpnEe9I8gSgnWx6BqjFXLodRvVNw/s16000/yolo.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Another post starts with you beautiful people&#128147;. In my previous post, we learned about training a custom dataset with an official Pytorch-based YOLOv7 object detector. If you have not seen that post, I recommend you check it once. The link is&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2022/08/how-to-train-custom-dataset-and-make.html" target="_blank"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;. Once we achieve the best model, the next important step is to use that model. Sometimes you may need to use multiple ML models which you have trained on different-different ML frameworks like PyTorch, TensorFlow, Caffe, etc. In production or the real world, the trained model can be deployed as Rest API, or integrated with a web application without changing its form but what if you need to use that within your mobile device as an Android or iOS app as well as you want to use it with an embedded system like Nvidia Jetson &#128163;? Here comes the problem of&amp;nbsp;interoperability. In this post, we are going to learn how can we export our custom YOLOv7 model to ONNX format.&lt;/p&gt;&lt;p&gt;&lt;a href="https://onnx.ai/index.html" target="_blank"&gt;ONNX&lt;/a&gt;&amp;nbsp;is an open format built to represent machine learning models. ONNX defines a common set of operators - the building blocks of machine learning and deep learning models - and a common file format to enable AI developers to use models with a variety of frameworks, tools, runtimes, and compilers. For our export, I will use the same Google Colab platform where I had trained&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2022/08/how-to-train-custom-dataset-and-make.html" target="_blank"&gt;my custom yolov7 model&lt;/a&gt;. Let's first import some required dependencies-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjP8VndI_q4HB1TiwhAaCjLFrK7KG1ZHVGemiVDmixbdHLvQAkp26CKuUN3wmdJ8PxW8kY6ebxXfJ2EfjkI6p-ZVlsIE7UXQIEVFAPBMv6f6ntvRdDhc56w1Pmn5j9dwxPvpfEskPp6SrupR_wRbd_YzC6QozZuglqvG5RmEszpUNO5u_K5DFB92W0Mpw/s447/o1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="147" data-original-width="447" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjP8VndI_q4HB1TiwhAaCjLFrK7KG1ZHVGemiVDmixbdHLvQAkp26CKuUN3wmdJ8PxW8kY6ebxXfJ2EfjkI6p-ZVlsIE7UXQIEVFAPBMv6f6ntvRdDhc56w1Pmn5j9dwxPvpfEskPp6SrupR_wRbd_YzC6QozZuglqvG5RmEszpUNO5u_K5DFB92W0Mpw/s16000/o1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;Since Google Colab has already PyTorch installed so we don't need to install it. During this work, my Colab had the following version of Python and PyTorch-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPJqE3A-DWq6vZ1nokzH_3aAZT3KCB_LkahAcAJUlL2iE-cLfcFwpQIHDvEaOkdr_nDJx1VzqYY9CGKDSkUvT-QotbvBW_ZYY8kwQDDaFvJuBztvlEwmtZh9_X2Wgsl0_aw9_2vPhiJzWniqQs-1syZGGHhd9sf_Wz6T3UuZ6qiIPHzG3mw08io85jsA/s867/o2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="194" data-original-width="867" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPJqE3A-DWq6vZ1nokzH_3aAZT3KCB_LkahAcAJUlL2iE-cLfcFwpQIHDvEaOkdr_nDJx1VzqYY9CGKDSkUvT-QotbvBW_ZYY8kwQDDaFvJuBztvlEwmtZh9_X2Wgsl0_aw9_2vPhiJzWniqQs-1syZGGHhd9sf_Wz6T3UuZ6qiIPHzG3mw08io85jsA/s16000/o2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Next, we will download the official code repo of YOLOv7 as below-&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhr6rzPKeOKM6-Ej5CBLNhSX_yUZk-4s9lRc5_oMxjdEIWnbeEf9xw4--z9PCrMb3SnT2xI957zFQ-yAKAx8ZkDwszCpv1cxqr_h2v3_kK8AL55Om7FfeNeM7_hObch0qZNujLV91zLqhCgpJh0a4Gj-1WOqdpQ6GTvj2xjzAuy6rrQCV6gpk-MKYoukQ/s774/o3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="364" data-original-width="774" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhr6rzPKeOKM6-Ej5CBLNhSX_yUZk-4s9lRc5_oMxjdEIWnbeEf9xw4--z9PCrMb3SnT2xI957zFQ-yAKAx8ZkDwszCpv1cxqr_h2v3_kK8AL55Om7FfeNeM7_hObch0qZNujLV91zLqhCgpJh0a4Gj-1WOqdpQ6GTvj2xjzAuy6rrQCV6gpk-MKYoukQ/s16000/o3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Once the code base is downloaded, let's check a test image with our trained YOLOv7 model so that in the last of this post we can compare it with the exported ONNX model inference. The command to do the inference is as below-&lt;/div&gt;&lt;div&gt;&lt;b&gt;! python detect.py --weights best.pt --img-size 1280 --source test_image.jpg&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;Here, just replace best.pt with your custom YOLOv7 weight file and&amp;nbsp; test_image.jpg with your test image. After successfully running of above command the resulting image will be saved in the path-&amp;nbsp;&lt;span style="background-color: #fffffe; color: #a31515; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;runs/detect/exp/test_image.jpg. &lt;/span&gt;&lt;span style="background-color: #fffffe; font-size: 14px; white-space: pre;"&gt;&lt;span style="font-family: arial;"&gt;Let's check the outcome by opening the image below&lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: #fffffe; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;-&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOE8Q5HiKhMm2q1HDc_Xt7VrA0wVeCPmjSJdd6xWixwXkNy0PodSJXd1S_GGnWUn62njoX0QUA-B-zO3D6obCYti3_T1x0zRO2RcbNzvCgChlzW-68XSHLun6NwN5qRThpQmBj6CPljVHjN7Pa618L5I0se9VG8RpoMbtVR8-csIaZMjeJiqmk3NyTbA/s433/o4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="83" data-original-width="433" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOE8Q5HiKhMm2q1HDc_Xt7VrA0wVeCPmjSJdd6xWixwXkNy0PodSJXd1S_GGnWUn62njoX0QUA-B-zO3D6obCYti3_T1x0zRO2RcbNzvCgChlzW-68XSHLun6NwN5qRThpQmBj6CPljVHjN7Pa618L5I0se9VG8RpoMbtVR8-csIaZMjeJiqmk3NyTbA/s16000/o4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="background-color: #fffffe; font-size: 14px; white-space: pre;"&gt;&lt;span style="font-family: arial;"&gt;And it looks like as below&lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: #fffffe; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;-&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVFZpWEmahsNJBwz4yYKRgSFxFfmPSrdhY0Gtd40RfvzKhEVIQkANHmuIRqoGjcm1JO-yXWiMLz_xHmvCDHhi3NNtA2SzfBWT9VK5yi-GR67k15gxJHi6nHWPvh9cVpX2PpfqIjegZP3iD9ATRegPFeuxG7-SC5kndCdLlJz83rRqHDPHMPo_m0eEAQQ/s1280/o5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="745" data-original-width="1280" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVFZpWEmahsNJBwz4yYKRgSFxFfmPSrdhY0Gtd40RfvzKhEVIQkANHmuIRqoGjcm1JO-yXWiMLz_xHmvCDHhi3NNtA2SzfBWT9VK5yi-GR67k15gxJHi6nHWPvh9cVpX2PpfqIjegZP3iD9ATRegPFeuxG7-SC5kndCdLlJz83rRqHDPHMPo_m0eEAQQ/s16000/o5.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="background-color: #fffffe; font-size: 14px; white-space: pre;"&gt;&lt;span style="font-family: arial;"&gt;So till now, everything looks fine. Let's move to our export. For this purpose, &lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: #fffffe; font-family: arial; font-size: 14px; white-space: pre;"&gt;the official code  repo that we had already downloaded has a script named export.py.&lt;/span&gt;&lt;div&gt;&lt;span style="background-color: #fffffe; font-size: 14px; white-space: pre;"&gt;&lt;span style="font-family: arial;"&gt;We will use  the same  to export the model into ONNX format as below-&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtICs6MNsXKMiXz7YScQTrtF1AAzVSyXIF87LziYIvTw90tjZWlFRwTPq4sWCTDw2_HO8CEzaY2yfPej98sfIKglXOcGVERjL91gJLtFWCElBCrdbUwZ8iWf0x5RDCdfKVI5viJVJ_NKGN-ZRewhdlWOk7X5-ggRrG3dHvHsifeiyow07Gk1s4qm-5pw/s1329/o6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="130" data-original-width="1329" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtICs6MNsXKMiXz7YScQTrtF1AAzVSyXIF87LziYIvTw90tjZWlFRwTPq4sWCTDw2_HO8CEzaY2yfPej98sfIKglXOcGVERjL91gJLtFWCElBCrdbUwZ8iWf0x5RDCdfKVI5viJVJ_NKGN-ZRewhdlWOk7X5-ggRrG3dHvHsifeiyow07Gk1s4qm-5pw/s16000/o6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace;"&gt;&lt;span style="font-family: monospace, Consolas, Courier New, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: arial;"&gt;You can check export.py to understand it's each argument. After successfully running of above command you will see the following output in the console-&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGv8O8M2bT2rC0dh7xo0cjKBnLx7BHOkGRkbwzvk0AxSgIA2JFsXMR1FWMBGXQ1BDKIR8s7zROQXy9mSvDCd7VWovKrl01bh7LcpcZRUur3GqXeHhMbUffqsoKOw3g2RfaphgV9gTTolLHcdcB9p8B_rCBlD_uegBSoljtOitRo-kxoCGxboHLYOAt4A/s1354/o7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="99" data-original-width="1354" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGv8O8M2bT2rC0dh7xo0cjKBnLx7BHOkGRkbwzvk0AxSgIA2JFsXMR1FWMBGXQ1BDKIR8s7zROQXy9mSvDCd7VWovKrl01bh7LcpcZRUur3GqXeHhMbUffqsoKOw3g2RfaphgV9gTTolLHcdcB9p8B_rCBlD_uegBSoljtOitRo-kxoCGxboHLYOAt4A/s16000/o7.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Now, the next most important step is to do the inference with this exported model. Let's understand this with below code snippets one by one-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3TKhW9WKkETEg8N5IFoR5ibSV7L4gP9uPjzJX79Hxt_Dr84XlYGM52ypqKq5As5vUmgUzqiEMzXwsIXjzMRSAye10Hk1l7lECqMs8yOPpAr9OX85pey4P7jAD41iGIZH3hfMQ3PpwQKryjQ8TYMzYaePWR-_jyqmNHmj4KJ9-SjVYEqggpJ6iAnA2WA/s1108/o8.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="420" data-original-width="1108" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3TKhW9WKkETEg8N5IFoR5ibSV7L4gP9uPjzJX79Hxt_Dr84XlYGM52ypqKq5As5vUmgUzqiEMzXwsIXjzMRSAye10Hk1l7lECqMs8yOPpAr9OX85pey4P7jAD41iGIZH3hfMQ3PpwQKryjQ8TYMzYaePWR-_jyqmNHmj4KJ9-SjVYEqggpJ6iAnA2WA/s16000/o8.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;In the above code snippet, first, we have loaded the ONNX model, our test image, required libraries for inference, CPU or GPU selection, and the main class of the ONNX runtime as a session. In the next code snippet, we will create a function to do the following things with the given input image-&lt;div&gt;&lt;ol style="text-align: left;"&gt;&lt;li&gt;Resize and pad image while meeting stride-multiple constraints&lt;/li&gt;&lt;li&gt;Scale ratio&lt;/li&gt;&lt;li&gt;Compute padding&lt;/li&gt;&lt;li&gt;divide padding into 2 sides&lt;/li&gt;&lt;li&gt;Resize and find the top, bottom, left, and right coordinates&lt;/li&gt;&lt;li&gt;Add border and return the image with the scale ratio and sides&lt;/li&gt;&lt;/ol&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiagGflKWGMRYHsbbCDtZTHQneKEk3hBG9rUUXM0sL9EQLs5d9sVf9m5Ne8-Zb1S2My9xqLrrUHP1tpBULY1H8qHzx2pgwntc-ZUR6CJarKicEYzUPhkReq6MNnaFMx9JB9KnD_q5VM7EhaHiKD9iGUJ_9dJEl7jNj9OyTNJJWOGVtmqyjFvnt2z_SAbA/s1022/o9.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="649" data-original-width="1022" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiagGflKWGMRYHsbbCDtZTHQneKEk3hBG9rUUXM0sL9EQLs5d9sVf9m5Ne8-Zb1S2My9xqLrrUHP1tpBULY1H8qHzx2pgwntc-ZUR6CJarKicEYzUPhkReq6MNnaFMx9JB9KnD_q5VM7EhaHiKD9iGUJ_9dJEl7jNj9OyTNJJWOGVtmqyjFvnt2z_SAbA/s16000/o9.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Next, we will create a list of our target object names- in my case, it is wheat, color scheme for the bounding boxes. Then we will apply the above-created function- letterbox() to the image, will convert the image to a NumPy array and then we will normalize it for further processing. Next, we will extract the output from the model as below-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgntcPu7HHpXDjPHMD4M93K-QqFi3KyBOzYazptElsGpmaGNOWvt22muMGk8W1YCc2m9JCDXk-7y5-jS8a1BI4gsyMIjbY2GLehZMTHAT__ZWvnNtswfgHuu7OCbKE1nCTYXrMkqa9WWWCkBgPBqjWf9R622bI5c47Jj5fZsNDBVv119JNQ_P5k_Rq8Ug/s875/o10.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="534" data-original-width="875" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgntcPu7HHpXDjPHMD4M93K-QqFi3KyBOzYazptElsGpmaGNOWvt22muMGk8W1YCc2m9JCDXk-7y5-jS8a1BI4gsyMIjbY2GLehZMTHAT__ZWvnNtswfgHuu7OCbKE1nCTYXrMkqa9WWWCkBgPBqjWf9R622bI5c47Jj5fZsNDBVv119JNQ_P5k_Rq8Ug/s16000/o10.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Now, we are ready for making the inference. First, we will fetch the output of the ONNX inference session and then we will iterate it to get the image, bounding box coordinates, confidence score, and the class name, and then we will use this information to draw the bounding boxes around the detected class with its name as below-&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpprHuJe2pO5iOdz8ec_6IAzIOl0IoKOPSFsMuStq1fme3uvaPiz8phqx1lsmPaQoDLphtvPzPGKHKgFhqfG_EEbAS_dgIIoQeLeAF4TBXwCUqcSreE7RlMWIM44ZEyX0cEZEk38C1ItI0Rf8YTx11nEiNFHggTo-gnxp17pbezYR2GcHxaxhJ021RGw/s1035/o11.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="460" data-original-width="1035" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpprHuJe2pO5iOdz8ec_6IAzIOl0IoKOPSFsMuStq1fme3uvaPiz8phqx1lsmPaQoDLphtvPzPGKHKgFhqfG_EEbAS_dgIIoQeLeAF4TBXwCUqcSreE7RlMWIM44ZEyX0cEZEk38C1ItI0Rf8YTx11nEiNFHggTo-gnxp17pbezYR2GcHxaxhJ021RGw/s16000/o11.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp; That's it! Our inference script is ready for our ONNX file and once executed it had shown me the output image as below-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNRIG8iBYRRC55JbNlOcQVVIfPARgPUVb9IFt5qDvJ88pRT_Atpavp363auNE8AF1udU5JWHl8n9kD9foC6m7-bx0I_HQ35HdeYEJcixbgsHRnNgQp16vLlAxFsLqFDRP1fhKuxXC3K6DX8Hcltfpq-Mdk8Up5EyQTsgoxnhKW7WBLDGKC_kV2k3WH9w/s1280/o12.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="779" data-original-width="1280" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNRIG8iBYRRC55JbNlOcQVVIfPARgPUVb9IFt5qDvJ88pRT_Atpavp363auNE8AF1udU5JWHl8n9kD9foC6m7-bx0I_HQ35HdeYEJcixbgsHRnNgQp16vLlAxFsLqFDRP1fhKuxXC3K6DX8Hcltfpq-Mdk8Up5EyQTsgoxnhKW7WBLDGKC_kV2k3WH9w/s16000/o12.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;If you compare this image with the previous output image you will find although the confidence score of the detected class has been dropped yet the accuracy of the exported model is the same. It means our exported ONNX model is working as expected. One more point to observe in our inference script is that we have not imported the PyTorch library which we used to train the custom dataset with YOLOv7. That is the interoperability of the ONNX format. Now our production deployment team can choose any ML framework they are comfortable with and using this unified inference framework (ONNX), the exported model can be deployed to anywhere. You can find the complete Colab Notebook&amp;nbsp;&lt;a href="https://colab.research.google.com/drive/1B-9YEVdBVncoJBG_XHi7CQpjRAK-7ilX?usp=sharing" target="_blank"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So why wait now☝ Just copy the notebook, export your custom YOLOv7 model to ONNX format and deploy it to production&#128165;. That's it for today guys.&amp;nbsp;In my next post, I will share to export the model into TensorRT format till then&#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;div&gt;&lt;div&gt;&lt;span style="font-family: monospace, Consolas, Courier New, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: monospace, Consolas, Courier New, monospace;"&gt;&lt;span style="font-size: 14px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="background-color: #fffffe; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="background-color: #fffffe; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="background-color: #fffffe; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="background-color: #fffffe; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="background-color: #fffffe; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="background-color: #fffffe; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="background-color: #fffffe; font-family: monospace, Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZFAX-q7HhJ4AyfWPVICwa1NhwO_QvOISBFKbrUF6_TrJCTRnhHhh3Xt3LI9pPDaR0whuxm26wMyBCqvfwPB6-aQs9x_6Dz7lDdXs1OiwTECLw5F_P4-xr6Yv-NDAtohqDW_5CSrg12dn5uPNY6EJiWr7kS9gYBCFpnEe9I8gSgnWx6BqjFXLodRvVNw/s72-c/yolo.jpg" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">11</thr:total></item><item><title>How to train a custom dataset and make inference with Official YOLOv7 on Kaggle?</title><link>https://dsbyprateekg.blogspot.com/2022/08/how-to-train-custom-dataset-and-make.html</link><category>kaggle</category><category>PyTorch</category><category>YOLOv7</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Thu, 4 Aug 2022 21:28:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-7207602191513404698</guid><description>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPG7L1B24TNTsgdnmjWwOYkcznZv8KVe1GjPzPrOWyQNxmO2IncthHRk2eNQRMh9pFLlTMweaDIFFSWtDr1WVn-waN1vLVBcWWB6N2qanzGs8_6aa_oPze8juBzpdBaZEZ-nlxoHPV536qpiZzsDBifu3LCF02YuiPkS-YCr1zeoQDSEat7JUF_jAHSw/s790/1.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="593" data-original-width="790" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPG7L1B24TNTsgdnmjWwOYkcznZv8KVe1GjPzPrOWyQNxmO2IncthHRk2eNQRMh9pFLlTMweaDIFFSWtDr1WVn-waN1vLVBcWWB6N2qanzGs8_6aa_oPze8juBzpdBaZEZ-nlxoHPV536qpiZzsDBifu3LCF02YuiPkS-YCr1zeoQDSEat7JUF_jAHSw/w400-h300/1.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;Another post starts with you beautiful people! I hope you have already learned the state-of-the-art object detection technique Darknet's YOLOv4 from my previous posts. If you are new to my blog then I recommend you to once go through&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2020/08/how-to-use-opencv-python-with-darknets.html" target="_blank"&gt;&lt;b&gt;this link&lt;/b&gt;&lt;/a&gt;&amp;nbsp;to learn about end-to-end implementation of YOLOv4. In this post we are going to learn the successor of the&amp;nbsp;&lt;a href="https://github.com/AlexeyAB/darknet" target="_blank"&gt;official YOLOv4&lt;/a&gt;&amp;nbsp;:&amp;nbsp;&lt;a href="https://arxiv.org/abs/2207.02696" target="_blank"&gt;&lt;b&gt;YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors&lt;/b&gt;&lt;/a&gt;&amp;nbsp;&#128165;. Since the official implementation of YOLOv7 is still not done in the Darknet framework, in this post we are going to use the Pytorch framework to train a custom dataset and then make inferences using the same Pytorch framework. But don't worry if you are new to PyTorch &#128114;, we are not going to reinvent the wheel in fact as a practitioner our goal should be to learn how to use it.&lt;/p&gt;&lt;p&gt;For our learning, we will use a real-world dataset and the problem-&amp;nbsp;&lt;a href="https://www.kaggle.com/competitions/tensorflow-great-barrier-reef/overview" target="_blank"&gt;&lt;b&gt;detecting starfish in underwater images&lt;/b&gt;&lt;/a&gt;. This challenge was held on Kaggle. It is a perfect use case for our learning since there are many other tutorials that use public datasets. However, preparing a dataset is also an important job before applying the training. So first let's understand the given dataset format-&lt;/p&gt;&lt;p&gt;&lt;b&gt;Goal: &lt;/b&gt;To predict the presence and position of crown-of-thorns starfish in sequences of underwater images taken at various times and locations around the Great Barrier Reef.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Dataset:&amp;nbsp;&lt;/b&gt;train/ - Folder containing training set photos of the form video_{video_id}/{video_frame_number}.jpg. [train/test].csv - containes following metadata for the images: video_id, video_frame, sequence id, sequence_frame number, image_id, annotations in COCO format.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Dataset Preparation:&lt;/b&gt; Let's take a look at the dataset with our Python code:&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0XlGrVLf59pqy1CVvhVVtXqTAbYMlvBzwn_OumhEuwyJUv22dyKbkzaGkY-6O0BriajrOaWFug5J9u39hzUGDc3vnqF-sEzOdCJLn0CXVL_GWKE0PwOGrazjig2mEee0veQg8wRpp_K8-8F2clzmv81AxaksUApxxdRfk19R0MIYZrBdDeCvAAXQcXw/s1265/2.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="243" data-original-width="1265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0XlGrVLf59pqy1CVvhVVtXqTAbYMlvBzwn_OumhEuwyJUv22dyKbkzaGkY-6O0BriajrOaWFug5J9u39hzUGDc3vnqF-sEzOdCJLn0CXVL_GWKE0PwOGrazjig2mEee0veQg8wRpp_K8-8F2clzmv81AxaksUApxxdRfk19R0MIYZrBdDeCvAAXQcXw/s16000/2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOTGv8ArW1saM26ecWXbF7hTcfXD-ivhixKrfvg-0CpFXj580ymG6q41KRrg_LRotbdKEYxqUgE8Oi7OR1iVIEvWRBdNFTKnmEhEoeQIWFtV0zjCI3sWpgPnvDJd0nd6enltnYxjhR0s74omTINdn5fhk1mqT92q8njBAExL6vSuJRiyiDYtkZuI8sRQ/s1374/3.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="105" data-original-width="1374" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOTGv8ArW1saM26ecWXbF7hTcfXD-ivhixKrfvg-0CpFXj580ymG6q41KRrg_LRotbdKEYxqUgE8Oi7OR1iVIEvWRBdNFTKnmEhEoeQIWFtV0zjCI3sWpgPnvDJd0nd6enltnYxjhR0s74omTINdn5fhk1mqT92q8njBAExL6vSuJRiyiDYtkZuI8sRQ/s16000/3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Let's verify if all the images have the annotations or not. We will later remove the images from our training dataset which don't have the annotations otherwise it will affect our model accuracy-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFXJY9aJM839XGieJyjjOxnWwVAPo1AlPTRQZUNl6_eBX6wvxKTKUcCs1QQwgTDM1IEsVVyO5ORXbk1SgnsCuJHTRzn5gm4rSRk70Zm309vFhyk7tlrdwDfDCX7mEjR2W0KRlPa9Yw1pmvlxNGmhXLwK56JR4wd3Ft49zFgNiX7Krt91elLm44FOZ3Jw/s818/4.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="275" data-original-width="818" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFXJY9aJM839XGieJyjjOxnWwVAPo1AlPTRQZUNl6_eBX6wvxKTKUcCs1QQwgTDM1IEsVVyO5ORXbk1SgnsCuJHTRzn5gm4rSRk70Zm309vFhyk7tlrdwDfDCX7mEjR2W0KRlPa9Yw1pmvlxNGmhXLwK56JR4wd3Ft49zFgNiX7Krt91elLm44FOZ3Jw/s16000/4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;So we have only 20.93% annotated images in the given dataset. Let's remove un-annotated images metadata from our dataframe-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEic0zptZZIlAZhyRyTIzVETdQV7Kc6SpLu51JsVT-uC-cvWM5MigfTM3OFhJPnW6hHJK7boEcwJ95w4LMhzSzQqvZbHTa7CtB85Cv9FVoI9Sp59sQVmADlfRHvFlfrMgReBnn2Pe-xZSvDFvbzOZWn1mPwtlBj4u4LHp_aw3mFhafLgIBFUbL_q7OuilQ/s292/5.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="31" data-original-width="292" height="34" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEic0zptZZIlAZhyRyTIzVETdQV7Kc6SpLu51JsVT-uC-cvWM5MigfTM3OFhJPnW6hHJK7boEcwJ95w4LMhzSzQqvZbHTa7CtB85Cv9FVoI9Sp59sQVmADlfRHvFlfrMgReBnn2Pe-xZSvDFvbzOZWn1mPwtlBj4u4LHp_aw3mFhafLgIBFUbL_q7OuilQ/w320-h34/5.JPG" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Also, let's remove the unannotated images with the help of &lt;a href="https://joblib.readthedocs.io/en/latest/generated/joblib.Parallel.html" target="_blank"&gt;joblib's parallel function&lt;/a&gt;&amp;nbsp;for speedup. Here we need to copy the data to Kaggle's output directory since the input directory doesn't have the write access-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwnvrY0XCFOgRNrKMIvoQ4_J7WQVQYfKv0EoVPe6CeTQvNImg4gNU5lIbgtsXnGxfru5eD6F1BqUnGPwr72wSionfnbVyOsu8Pnb5qD2-N5IJIClTF4mzMQjyCVkIHOniJLsMv8ODgCoeujyjpNUc8h2RwFVjVnN5hKzNjUipwn6MeMwzUIXKY2Jl-Bg/s1264/6.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="322" data-original-width="1264" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwnvrY0XCFOgRNrKMIvoQ4_J7WQVQYfKv0EoVPe6CeTQvNImg4gNU5lIbgtsXnGxfru5eD6F1BqUnGPwr72wSionfnbVyOsu8Pnb5qD2-N5IJIClTF4mzMQjyCVkIHOniJLsMv8ODgCoeujyjpNUc8h2RwFVjVnN5hKzNjUipwn6MeMwzUIXKY2Jl-Bg/s16000/6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;So, we have now a total of 4919 annotated images to train/validate. Our next step is to visualize the images and see what annotations look like in these images. For this purpose, we don't need to write the code from scratch instead we will use&amp;nbsp;&lt;a href="https://github.com/awsaf49/bbox" target="_blank"&gt;bounding box utility github&lt;/a&gt;&amp;nbsp;&#128124;. Don't forget to give a start to this GitHub repo &#128077;. Let's import the helpful bounding box creation functions from the repo as below-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6RWYgfOfWbb8opNFXSVvizz9OcP97LWCFfZ-x1jgsRg1T6DLN01xJKlEQNNNzvnZpNu59YADscJ8TnCtjsWy5B4DcsC9-oPoznvaxx7pW529Ee61dasjc4TwNnppSonGZHWgKcFwWRA8jpUhZc5gauKFjluZE_GkKxus_atae4hzzOEK6ZIgd9P0Npw/s890/7.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="451" data-original-width="890" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6RWYgfOfWbb8opNFXSVvizz9OcP97LWCFfZ-x1jgsRg1T6DLN01xJKlEQNNNzvnZpNu59YADscJ8TnCtjsWy5B4DcsC9-oPoznvaxx7pW529Ee61dasjc4TwNnppSonGZHWgKcFwWRA8jpUhZc5gauKFjluZE_GkKxus_atae4hzzOEK6ZIgd9P0Npw/s16000/7.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Next, let's fetch the bounding box coordinates using the above function-&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX5zsqayfhQojNf-3s2JsVlFtJ2Vobqe0j0TUOwAkkkmDHY6aQ_Sre25IBiP5_pVtknVWQAvAyneU9vh1dGvEelZ26B5nRigHlHTKiW7vH30cX-VkoMzXA7eolOPPcGVw0wZZXTguWq_nkDFMJnqVLT308UARl-jhQaIAr1WyrbWN799mOzLsgD4uBvw/s591/8.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="85" data-original-width="591" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX5zsqayfhQojNf-3s2JsVlFtJ2Vobqe0j0TUOwAkkkmDHY6aQ_Sre25IBiP5_pVtknVWQAvAyneU9vh1dGvEelZ26B5nRigHlHTKiW7vH30cX-VkoMzXA7eolOPPcGVw0wZZXTguWq_nkDFMJnqVLT308UARl-jhQaIAr1WyrbWN799mOzLsgD4uBvw/s16000/8.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;Let's check the newly created column in our dataframe-&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6IKzrdJzrKzCbUoWeidYkMHjm8NkkqH1ATvI8mnIl56A54d2kjHCARzecJ2aT3k41xVWORHlK8J6gORZ1wjNwJ6no02luRQ6k12aOb119I9UzTlIKzdGg0MGTur92MhrCNmDgUG2al9_Ce0hTMHCgGv9t039YdKrlWNcm2de6WF5LtFMamma_fDfY0Q/s1323/9.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="207" data-original-width="1323" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6IKzrdJzrKzCbUoWeidYkMHjm8NkkqH1ATvI8mnIl56A54d2kjHCARzecJ2aT3k41xVWORHlK8J6gORZ1wjNwJ6no02luRQ6k12aOb119I9UzTlIKzdGg0MGTur92MhrCNmDgUG2al9_Ce0hTMHCgGv9t039YdKrlWNcm2de6WF5LtFMamma_fDfY0Q/s16000/9.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Now we have bounding box coordinates as well in COCO format. Since the given dataset has images of fixed sizes" (1280. 720); it's better to set another two columns- width and height in our dataframe as below-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHdtkgZKmlF-mTVDdaJTGrIj3ypUxHnqTj47wkb6YiQCKIbEo3Okjj-ISGDZU0pgsNbFN5KREVTOqP-Liz5qrhHEq7GqCV9-dNeHgG-nNgaDy2A-55L8PjMzJ4wuUfodUpTd2QqI1PYZp_KgwvpC7Ps4ghassaVDwFPbGOuKogWOFIqIbtdEqTqzCvlg/s218/10.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="96" data-original-width="218" height="96" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHdtkgZKmlF-mTVDdaJTGrIj3ypUxHnqTj47wkb6YiQCKIbEo3Okjj-ISGDZU0pgsNbFN5KREVTOqP-Liz5qrhHEq7GqCV9-dNeHgG-nNgaDy2A-55L8PjMzJ4wuUfodUpTd2QqI1PYZp_KgwvpC7Ps4ghassaVDwFPbGOuKogWOFIqIbtdEqTqzCvlg/s1600/10.JPG" width="218" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA-ojaoDZsr9wUTVLQ62hlLCPhvhEgS14RRNSz4MqW9g3E2czd9A9Mj1-SgLDfKZg3ATTaF1441i78TX6S4hcnN9qaeQNntZHgvLGucZ6b761_USjiVsBzJf5pfmyrhZhLvnPb_EHbbuL-z-pvFfFm6uz1ehjWQ9ZOT0bT1pTrth8dqGoSPFCV1N-a8g/s1365/11.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="266" data-original-width="1365" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA-ojaoDZsr9wUTVLQ62hlLCPhvhEgS14RRNSz4MqW9g3E2czd9A9Mj1-SgLDfKZg3ATTaF1441i78TX6S4hcnN9qaeQNntZHgvLGucZ6b761_USjiVsBzJf5pfmyrhZhLvnPb_EHbbuL-z-pvFfFm6uz1ehjWQ9ZOT0bT1pTrth8dqGoSPFCV1N-a8g/s16000/11.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Next, following the best practice of training dataset preparation, we will apply cross-validation to the dataset as below-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnzQpMC8c52fQvf8q0YJr2RAPhHW-CG3ZHPnE_hCKYYZ_mQKC6wOFJZFl5-0oXrALjgKyPXBtSeD1fCg2nTpYbADDZft5Dx4LNS0s5Ci_o5S4Klk4Lk0rIPzQvSlLapbm6iP9UNPZ-p_G1ep6dh_xG_Z7idCwaRtQTbR-oJI1Wqi1TrX5QjVBbmT16ug/s938/14.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="356" data-original-width="938" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnzQpMC8c52fQvf8q0YJr2RAPhHW-CG3ZHPnE_hCKYYZ_mQKC6wOFJZFl5-0oXrALjgKyPXBtSeD1fCg2nTpYbADDZft5Dx4LNS0s5Ci_o5S4Klk4Lk0rIPzQvSlLapbm6iP9UNPZ-p_G1ep6dh_xG_Z7idCwaRtQTbR-oJI1Wqi1TrX5QjVBbmT16ug/s16000/14.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;As you can see the number of samples is not same in each fold, so for the training we should experiment with each fold but for this post, I&amp;nbsp; will use the first fold-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjp15CAWqEw2sEb7iIFlUEwLdLhipz1BDYf2U3suGWSZ6Lx8s4rEPWQwUme1hS9WpugErnRJ6okbccAKlmDMTnYWBLzL_bHXyAKX_isCpEjHr0KopqTRDotMWSzFxAi9R8yyZFTLrn5y4jluRr5i0fB3J_e9mYa6HEw8PDSbw0o8w8hylq65jQCO1bFog/s530/20.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="291" data-original-width="530" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjp15CAWqEw2sEb7iIFlUEwLdLhipz1BDYf2U3suGWSZ6Lx8s4rEPWQwUme1hS9WpugErnRJ6okbccAKlmDMTnYWBLzL_bHXyAKX_isCpEjHr0KopqTRDotMWSzFxAi9R8yyZFTLrn5y4jluRr5i0fB3J_e9mYa6HEw8PDSbw0o8w8hylq65jQCO1bFog/s16000/20.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;b&gt;Prepare YOLO format dataset: &lt;/b&gt;Since till now we have cleaned and finalized the training dataset; the next step is to prepare this dataset in YOLO required format. For this step, we will export the labels into a .txt file. This .txt file will contain one row per target class where each row is in the following format: &lt;b&gt;&lt;i&gt;target_class_name [x_center, y_center, width, height]&lt;/i&gt;&lt;/b&gt; . Here the coordinates must be in normalized form and target class numbers start with zero. All these required conversions can be done using the below code-&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfP3qtXJNCNh0ZtzVk4j_uuwxZQxNWGcsIE3QrvdLLBNnOkMBk779ungDyrno6CsivNjj4ZSlOVvaPbi49-RV3-OBzNaYTqds1fduhhm4QYSS0sJB5En2GIFfUqQz-v8Jv4ulQxCCUeAENOlOi5mlQxle061jBkXCh42xCeMhNBS2oWjBWpEzHFIo9XQ/s916/12.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="527" data-original-width="916" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfP3qtXJNCNh0ZtzVk4j_uuwxZQxNWGcsIE3QrvdLLBNnOkMBk779ungDyrno6CsivNjj4ZSlOVvaPbi49-RV3-OBzNaYTqds1fduhhm4QYSS0sJB5En2GIFfUqQz-v8Jv4ulQxCCUeAENOlOi5mlQxle061jBkXCh42xCeMhNBS2oWjBWpEzHFIo9XQ/s16000/12.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNumafau9UO8SMWaHRt-d_d8vl_AXYKW3gh3gyBGgZ_49e259PbYH13kcLiq0ZiKbpVWpQOw-EhFlnD7rzJ23aABC9GhgekM9yfU1nr_14QTdxaD0-d7VAS5BUfVZgVXOKbcgej90YRsQmf6tAhwMg4YRjoItFmlGXP3gM9QSqVIZIqVxmHJZ_f_BObQ/s948/13.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="239" data-original-width="948" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNumafau9UO8SMWaHRt-d_d8vl_AXYKW3gh3gyBGgZ_49e259PbYH13kcLiq0ZiKbpVWpQOw-EhFlnD7rzJ23aABC9GhgekM9yfU1nr_14QTdxaD0-d7VAS5BUfVZgVXOKbcgej90YRsQmf6tAhwMg4YRjoItFmlGXP3gM9QSqVIZIqVxmHJZ_f_BObQ/s16000/13.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;Let's check our converted bounding box coordinates in one of our cross folded dataframe-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOFLxX4gFzTszGvN_gCgtDxukjpJ-FbgpUnGC6Fmki0SGokNChK7qQjiXpNzghl1VSQXRUtJ2sUJD1USF2i3a9D3y9knoqA3HacLIjJ-5K37Y5FXz1v8wIvbeqYvvuAfTSaAWVCl01bCDIXN_whNciBF-tF6vIvyXMzt1mCstwLNtUIItCs2K1fFDKZQ/s871/16.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="384" data-original-width="871" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOFLxX4gFzTszGvN_gCgtDxukjpJ-FbgpUnGC6Fmki0SGokNChK7qQjiXpNzghl1VSQXRUtJ2sUJD1USF2i3a9D3y9knoqA3HacLIjJ-5K37Y5FXz1v8wIvbeqYvvuAfTSaAWVCl01bCDIXN_whNciBF-tF6vIvyXMzt1mCstwLNtUIItCs2K1fFDKZQ/s16000/16.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Now we can visualize the training dataset images with their annotations using the matplotlib library as below-&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoe2drA6gOCa_eSUM5LBDqLE0flH3hyIY14MVwPW004bX5KzWfkpJ5RRPUzIF9b1MBjNlLo_JMlK2SI8-rAMp9o9U2E7k1SnlZsgJuQnUmfYIwhcHbFNgPBxJtIRE_04xVrIkVhdtqrabEjMOOdFThgXv6qWTYkbHOciC0MMg1Cjj13NQMC5uxBdYKfQ/s673/17.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="673" data-original-width="612" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoe2drA6gOCa_eSUM5LBDqLE0flH3hyIY14MVwPW004bX5KzWfkpJ5RRPUzIF9b1MBjNlLo_JMlK2SI8-rAMp9o9U2E7k1SnlZsgJuQnUmfYIwhcHbFNgPBxJtIRE_04xVrIkVhdtqrabEjMOOdFThgXv6qWTYkbHOciC0MMg1Cjj13NQMC5uxBdYKfQ/s16000/17.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;And the sample annotated images look like the below-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY8uIWv-SX8RyJS5CgjwBs85nyjY5rS6mt7U4MUNYYrU-4Y4BcENtgL0oiCA7QIKvZgiVeBXqQ_Niq49PuXAfckGsgWEeORIv2iKPb1hCn5_dMyiOgJtHRaFnO8Sjym0JEzFbCYzre2iArXw3cQuAIwPi7TlrGjuc4_i4AeJ5nD0wMUx4nfvqJl7jmIQ/s680/18.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="379" data-original-width="680" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY8uIWv-SX8RyJS5CgjwBs85nyjY5rS6mt7U4MUNYYrU-4Y4BcENtgL0oiCA7QIKvZgiVeBXqQ_Niq49PuXAfckGsgWEeORIv2iKPb1hCn5_dMyiOgJtHRaFnO8Sjym0JEzFbCYzre2iArXw3cQuAIwPi7TlrGjuc4_i4AeJ5nD0wMUx4nfvqJl7jmIQ/s16000/18.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpdrbMhFnwSwrlRJfYFb7c6HbAMjaMm8IArDi708QMg7UguYuJrr2pSGfMjFudL9bFXEtQO6kaUU_mYF95QZqoEuoDlaN5OWJ1rhgsUJX80HeTXBDqg41vffOhexqmY04jLuuOxwoPVNgkZivsJ6C2hVYsayrbOlou7unRZiBUYAA5m5K6d9S26nviMA/s680/19.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="372" data-original-width="680" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpdrbMhFnwSwrlRJfYFb7c6HbAMjaMm8IArDi708QMg7UguYuJrr2pSGfMjFudL9bFXEtQO6kaUU_mYF95QZqoEuoDlaN5OWJ1rhgsUJX80HeTXBDqg41vffOhexqmY04jLuuOxwoPVNgkZivsJ6C2hVYsayrbOlou7unRZiBUYAA5m5K6d9S26nviMA/s16000/19.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;After seeing the above few samples, we can imagine how complex a problem is this to solve with computer vision as compared to publically available datasets. Here the target class is very small and images are blurred. Let's see how YOLOv7 is going to detect these starfish &#128064;. But for now, let's create the .txt files required by YOLO-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfrK4Umh7HXbIq8L8r0WdiOTgnZ2SO-5IxdPzP4BzYKZRDSau9909PrmR3_PUDcP-cgc_mPzzJ8zn9Ps315txfQM_MZj4iNBQoe6PlNs-CA7xtF5HGmeJcnaGRnY_ku69l5UmsXqXY6yyKDY6cqBskC1Jw42PSHI4sB3U3BDSyPkh3H4RHHz586i29Yg/s645/21.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="645" data-original-width="570" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfrK4Umh7HXbIq8L8r0WdiOTgnZ2SO-5IxdPzP4BzYKZRDSau9909PrmR3_PUDcP-cgc_mPzzJ8zn9Ps315txfQM_MZj4iNBQoe6PlNs-CA7xtF5HGmeJcnaGRnY_ku69l5UmsXqXY6yyKDY6cqBskC1Jw42PSHI4sB3U3BDSyPkh3H4RHHz586i29Yg/s16000/21.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Here 'nc' means a number of classes to detect that is 1 in our case, 'names' is the target class name that I defined as cots but you can give any name to it.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;YOLOv7 training:&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Till now we have prepared the dataset to start the training. For the custom training, YOLOv7 has provided the different sizes of pre-trained weights-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_coKEyQPyh5Gb9Ljv_S1OdVmx2xGgBvVh6gMTcP_G47JFoNwm-A2wpbXdht98wDkD2-ymUqX-W37uHBgEm9veDHjbxMaXgyz3BCByAJf5oIUNX3iYgbEzasCVi42UTSSS_ngf3pzDv6hf_Yobb1kkA09P5mp2SHqPeaTqYH7GmgOAhUAWSETEQKRy0g/s929/22.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="472" data-original-width="929" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_coKEyQPyh5Gb9Ljv_S1OdVmx2xGgBvVh6gMTcP_G47JFoNwm-A2wpbXdht98wDkD2-ymUqX-W37uHBgEm9veDHjbxMaXgyz3BCByAJf5oIUNX3iYgbEzasCVi42UTSSS_ngf3pzDv6hf_Yobb1kkA09P5mp2SHqPeaTqYH7GmgOAhUAWSETEQKRy0g/s16000/22.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Here I am going to use the largest one:&amp;nbsp;&lt;a href="https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e_training.pt"&gt;&lt;b&gt;yolov7-e6e_training.pt&lt;/b&gt;&lt;/a&gt;. To use this pre-trained weight for our training, we also need to use its configuration file (yolov7-e6e.yaml) that we can get from this&amp;nbsp;&lt;a href="https://github.com/WongKinYiu/yolov7/tree/main/cfg/training"&gt;official config files link&lt;/a&gt;&amp;nbsp;and the hyperparameter file (hyp.scratch.custom.yaml) that we can get from this&amp;nbsp;&lt;a href="https://github.com/WongKinYiu/yolov7/blob/main/data/hyp.scratch.custom.yaml" target="_blank"&gt;custom hyperparameter file link&lt;/a&gt;. Now first we will create a new hyperparameter configuration file and then we will copy the content from&amp;nbsp;hyp.scratch.custom.yaml file to this new file in Kaggle as below-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZDEUZYGgomjMyq8o20SHcnQ2Jrk4iDm-X5Zst1DZ29aLxVezZ9-f9YXzmQSeo0V1R3g3PwnbTNk1YyXhbhbkvMMGNmFbU5K45rxK2na0lG0hhAazQKHBlwUmF4JT9ln8qdOdyVhox3k5F4rUiSM0efzlw6O7Eqm5GDF_tQHcCZuzuAysQsxgYzLAsoQ/s745/23.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="745" data-original-width="575" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZDEUZYGgomjMyq8o20SHcnQ2Jrk4iDm-X5Zst1DZ29aLxVezZ9-f9YXzmQSeo0V1R3g3PwnbTNk1YyXhbhbkvMMGNmFbU5K45rxK2na0lG0hhAazQKHBlwUmF4JT9ln8qdOdyVhox3k5F4rUiSM0efzlw6O7Eqm5GDF_tQHcCZuzuAysQsxgYzLAsoQ/s16000/23.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;The same we will do to copy the content of the&amp;nbsp;yolov7-e6e.yaml file to a new file in Kaggle-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCjIH9tjHoLCmb8O10cgp-OMSKmYvDTS1df4J3KD10KhRnxIIkW8FKdtoSUdSfTDxFRWu4vvel_XsZqFypxg68tuRnV99M4DCW0UK_K1rFpJWadChiu0Prd-dm3smP1pvrPjPDt4ddCOa4_DnS2Q1ghzHbKRHcEx6sFGnxs_YGy1SPfdExi3TGKmrYug/s750/24.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="750" data-original-width="490" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCjIH9tjHoLCmb8O10cgp-OMSKmYvDTS1df4J3KD10KhRnxIIkW8FKdtoSUdSfTDxFRWu4vvel_XsZqFypxg68tuRnV99M4DCW0UK_K1rFpJWadChiu0Prd-dm3smP1pvrPjPDt4ddCOa4_DnS2Q1ghzHbKRHcEx6sFGnxs_YGy1SPfdExi3TGKmrYug/s16000/24.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Notice here in the above content, I have changed 'nc:80' to 'nc:1' since we have only one class to predict. Rest all parameters are the same. Next, we will clone the official&amp;nbsp;&lt;a href="https://github.com/WongKinYiu/yolov7" target="_blank"&gt;&lt;b&gt;YOLOv7 repo&lt;/b&gt;&lt;/a&gt;&amp;nbsp;in Kaggle's output directory, and then we will install the dependencies as below-&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg302YtXnDFbTy5fZgadzRgmIDyNeUqD2APJYlkoxv-b1UO7L6dzBV_M5ht1EawHM3QpqaC9SydJWHuOmLR6JV5O3zhS4KE6p1KA_fSHLw2An6KF7ZCc7SnfkE6ZPlkKBwpaXbvgWo-guAnQbB50I2XPHyu_6S318jbQ0Ah6JNunf8VcRvXvO6mmP3b0g/s637/25.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="147" data-original-width="637" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg302YtXnDFbTy5fZgadzRgmIDyNeUqD2APJYlkoxv-b1UO7L6dzBV_M5ht1EawHM3QpqaC9SydJWHuOmLR6JV5O3zhS4KE6p1KA_fSHLw2An6KF7ZCc7SnfkE6ZPlkKBwpaXbvgWo-guAnQbB50I2XPHyu_6S318jbQ0Ah6JNunf8VcRvXvO6mmP3b0g/s16000/25.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Once all dependencies are installed, we will download the pre-trained weights as below-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDqPcsJUfnsSz_AQFpeKsEdNjc3F6IRAyGO94JQe47tNfDqxCZIhbzvDGUoK1HbhwJcGLk88vO1KKKgMQw0rTXu6BLnywcjBpZ0ipDQGNxQHwRTpHRUwDyefVH0xydaxYCNS_uaHLy8U5gGsWO8agCmH7f5Y6MO90knQ5KsX-2OCduqR4XL7N7bRqxGA/s948/26.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="48" data-original-width="948" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDqPcsJUfnsSz_AQFpeKsEdNjc3F6IRAyGO94JQe47tNfDqxCZIhbzvDGUoK1HbhwJcGLk88vO1KKKgMQw0rTXu6BLnywcjBpZ0ipDQGNxQHwRTpHRUwDyefVH0xydaxYCNS_uaHLy8U5gGsWO8agCmH7f5Y6MO90knQ5KsX-2OCduqR4XL7N7bRqxGA/s16000/26.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Now, we are ready to start the training with the below command-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9Q6FVOc3E_lppdQdXgmsCHgFpV05pTE_wJfSg4NH8WW0lt8cnxfAUyUjjYw-bJqvumG5tDljFz6oyWp4Mpv00OPB6jQIUYPWaLRmTuP8-79KCchUfTDw_J83bwhwhzq0zeIJPiwrrRHtYyLP-Q06ThEXBFBKxL4eYvxwW9IpsbDYpE3fzh6dXFBCMJg/s1364/27.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="62" data-original-width="1364" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9Q6FVOc3E_lppdQdXgmsCHgFpV05pTE_wJfSg4NH8WW0lt8cnxfAUyUjjYw-bJqvumG5tDljFz6oyWp4Mpv00OPB6jQIUYPWaLRmTuP8-79KCchUfTDw_J83bwhwhzq0zeIJPiwrrRHtYyLP-Q06ThEXBFBKxL4eYvxwW9IpsbDYpE3fzh6dXFBCMJg/s16000/27.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div&gt;Here, a number of workers I have defined as 8 which you can change accordingly to the system of yours, device as zero means single GPU training, the default number of epochs are 300 but due to limited training time in Kaggle I have used 26 as epoch size, batch size I have used as 3 to avoid out of memory issue but it should be 16 and rest other parameters are the pre-trained weight file path and the config file paths. Once the training is completed you can find the best-trained weight file (best.pt) under&amp;nbsp;/yolov7/runs/train/ path as below-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLAbLuN7wb0sMmQR_BEJS2Ye5Ql1qPSF0IAJ-Sa22SH8p0xo10TKYZa6m2lvoN9oAUwX_4GQ3rTpyyjw_yz6CzFAYWeulzKvt6OUKWJBSuuSe23OwJh9asCfAfKioPObzOUz0HAn8tycFUutzQQQoyug7P9LhO1Xh8HxGVmIwBxVAjPcOtD3r02CeBbg/s598/28.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="598" data-original-width="289" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLAbLuN7wb0sMmQR_BEJS2Ye5Ql1qPSF0IAJ-Sa22SH8p0xo10TKYZa6m2lvoN9oAUwX_4GQ3rTpyyjw_yz6CzFAYWeulzKvt6OUKWJBSuuSe23OwJh9asCfAfKioPObzOUz0HAn8tycFUutzQQQoyug7P9LhO1Xh8HxGVmIwBxVAjPcOtD3r02CeBbg/s16000/28.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;b&gt;YOLOV7 Inference: &lt;/b&gt;Since our training is completed, it's time to make inferences on the test images and prepare a submission file as required by the competition. For making inferences first step is to load our trained model. For this purpose, we can use&amp;nbsp;&lt;a href="https://pytorch.org/docs/stable/hub.html" target="_blank"&gt;&lt;b&gt;torch.hub.load()&lt;/b&gt;&lt;/a&gt;&amp;nbsp;function as below-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhatXkCt726kGGdd0n3_Xr-oq750iPrCHCuEa9vwG7oOzb2_xX6zpn8tkvRgcRCJ0GZXTQy9DAGe9Bfho9ytm2SjI96uF2sspTNUVX5h-4WzdbKDGu_xdYkXn2FHqLaRcaICMKqG5BLNk_qlUdTYkpI-hHfX56d27gbHPzUa8ZAZOhBeS-a4wyozBMkpA/s1125/29.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="281" data-original-width="1125" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhatXkCt726kGGdd0n3_Xr-oq750iPrCHCuEa9vwG7oOzb2_xX6zpn8tkvRgcRCJ0GZXTQy9DAGe9Bfho9ytm2SjI96uF2sspTNUVX5h-4WzdbKDGu_xdYkXn2FHqLaRcaICMKqG5BLNk_qlUdTYkpI-hHfX56d27gbHPzUa8ZAZOhBeS-a4wyozBMkpA/s16000/29.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;In this function, the first parameter is the YOLOv7 repo path, the second parameter is 'custom' since we are using a custom trained model, the third parameter is the best weight file path, and the fourth parameter is given as 'local' since we are loading the model from our local file system and the rest other configurations we are setting to our loaded model are easily understandable.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;After loading the model next step is to make the prediction. As required by this competition we need to predict the bounding box coordinates in COCO format and the confidence score of a given image in prediction and that can be achieved as below-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOHLkbTzVgMSusGth_8HzvmusbQh_K2elzhPFxHdJO8gzK5Si3bjFrguvdy2JezgBubMEU7jQmQyCEQf1x3y-HSAYXHaL2K2JMfjF5n95myfN5Y_3XztFxAh4p0z3mshRqJkrM2feNkhzVRaHetoXdl4gTnVqLshXRq5wf7yieya2nTU9tD9Fxd6SCag/s819/30.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="331" data-original-width="819" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOHLkbTzVgMSusGth_8HzvmusbQh_K2elzhPFxHdJO8gzK5Si3bjFrguvdy2JezgBubMEU7jQmQyCEQf1x3y-HSAYXHaL2K2JMfjF5n95myfN5Y_3XztFxAh4p0z3mshRqJkrM2feNkhzVRaHetoXdl4gTnVqLshXRq5wf7yieya2nTU9tD9Fxd6SCag/s16000/30.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Here for bounding box conversion, I have used the helper code from&amp;nbsp;&lt;a href="https://github.com/awsaf49/bbox" target="_blank"&gt;this awsome repo&lt;/a&gt;&amp;nbsp;&#128591;. Now we can make the required submission as below-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdplUvdwUCUVvEqV2cqp7GBxlzuH4gdeNisBk1XTWmCe_7HMAJhvCfUu0wsed58iS0RPRfI3mfRFGaOHpnpKM1fnT1b9KNobL-oH-DcpqH8pnfVPmHRc2pi2Uny4JlXSJXlK5y2q83aRlEadEqCb4HTswDlPZ4qGyp2kN64L9wL9OjePqhx5pyjM-czA/s1035/32.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="409" data-original-width="1035" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdplUvdwUCUVvEqV2cqp7GBxlzuH4gdeNisBk1XTWmCe_7HMAJhvCfUu0wsed58iS0RPRfI3mfRFGaOHpnpKM1fnT1b9KNobL-oH-DcpqH8pnfVPmHRc2pi2Uny4JlXSJXlK5y2q83aRlEadEqCb4HTswDlPZ4qGyp2kN64L9wL9OjePqhx5pyjM-czA/s16000/32.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhauPYn2kVPgCPa4ONtgFWBm3XCOd1VcLO8x3wVbHdusyc47x9rYtrEVv-Ejkd2hK0SoP30jIaVYpeTXCHedKZMJ_cnV_QRDwmYfLAxLWVgH-2m-0rx9qe7HJQIEcWmV0TZQ1zCKqzVjbbbS4OuRqrIGL7JPY_MroJYKcUVilHSQEwIoSRjACjV1NsVsA/s560/31.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="304" data-original-width="560" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhauPYn2kVPgCPa4ONtgFWBm3XCOd1VcLO8x3wVbHdusyc47x9rYtrEVv-Ejkd2hK0SoP30jIaVYpeTXCHedKZMJ_cnV_QRDwmYfLAxLWVgH-2m-0rx9qe7HJQIEcWmV0TZQ1zCKqzVjbbbS4OuRqrIGL7JPY_MroJYKcUVilHSQEwIoSRjACjV1NsVsA/s16000/31.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Here, we have iterated the iter_test variable since test images were not given as a whole instead organizer shared the API. After iterating each test image we made predictions using our custom-trained YOLOv7 model and formatted the predictions in the required format. Let's see one test image with the prediction-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN9Vlmos34mAtSZ7rKsx5HDQtCC6UDR8XHY8kHx4jn9fHucgk2xr43x1ke70zCEGWy2MLahfzQHD3MsYOEJgHN4hrFRywE9CCWPQKTmhQCRuenyiBkPb79JSIa_orjF9YO97wbPehEAZuhpdRakJf2eumDNPysCO4LGlslRZN-GKUhiomPKU5FwiJqLA/s702/33.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="541" data-original-width="702" height="494" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN9Vlmos34mAtSZ7rKsx5HDQtCC6UDR8XHY8kHx4jn9fHucgk2xr43x1ke70zCEGWy2MLahfzQHD3MsYOEJgHN4hrFRywE9CCWPQKTmhQCRuenyiBkPb79JSIa_orjF9YO97wbPehEAZuhpdRakJf2eumDNPysCO4LGlslRZN-GKUhiomPKU5FwiJqLA/w640-h494/33.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;It looks good &#128076; but how can we know the goodness of our model? That's why I used the Kaggle dataset so that I can submit the prediction to this challenge and see my private score. Same I did and I got the following score with my trained model-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh99NMp4EPRqLrD5hX0eXuen5ZJ4eIEhnwh5xDGfnf7AhI4iTJaSwlnIK1hzpnmeWfnovbxxLhsEu8DFAeOgtNBsWLSeNLDQy19a6JlKauRDQUdyzPYh1lQWqXRsavlzApg6mc_86GHSw0jrFRFHeDlIjSr0FfmqWxwyn1vW0Jr8Wzc_AsNvTx_sy66zg/s1508/34.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="493" data-original-width="1508" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh99NMp4EPRqLrD5hX0eXuen5ZJ4eIEhnwh5xDGfnf7AhI4iTJaSwlnIK1hzpnmeWfnovbxxLhsEu8DFAeOgtNBsWLSeNLDQy19a6JlKauRDQUdyzPYh1lQWqXRsavlzApg6mc_86GHSw0jrFRFHeDlIjSr0FfmqWxwyn1vW0Jr8Wzc_AsNvTx_sy66zg/s16000/34.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Next, I checked for any other submission but with&amp;nbsp;&lt;a href="https://github.com/ultralytics/yolov5" target="_blank"&gt;&lt;b&gt;YOLOv5&lt;/b&gt;&lt;/a&gt; and found the below score-&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYPfOZ8n5eS16M-KgdpxD_IrH7Nw74aYYc9JaIyna0AezIQW1xfHIkWUjGMI49p7_1kun3MSaB-YPJDRmAQUvrimYrdiZpKwp5pd6ijcRiKBu-y430cX0EAH6Tk33-NXJ82j3OH2QlwT9dCuVkCxhQzn-QBmzmtrRBkG6ilktvj7SQjCXEyVBlJmzucA/s1143/35.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="378" data-original-width="1143" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYPfOZ8n5eS16M-KgdpxD_IrH7Nw74aYYc9JaIyna0AezIQW1xfHIkWUjGMI49p7_1kun3MSaB-YPJDRmAQUvrimYrdiZpKwp5pd6ijcRiKBu-y430cX0EAH6Tk33-NXJ82j3OH2QlwT9dCuVkCxhQzn-QBmzmtrRBkG6ilktvj7SQjCXEyVBlJmzucA/s16000/35.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;As we can see here, with default configurations of YOLOv7, my model has achieved a 0.602 private score as compared to 0.588 achieved by YOLOv5 which indicates that indeed the latest official release of YOLOv7 is faster and more accurate than other object detectors &#128170;. We can achieve a better result with hyperparameter tuning, different augmentation, etc.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For complete code reference please follow my training script&amp;nbsp;&lt;a href="https://www.kaggle.com/code/iamprateek/cots-yolov7-training/notebook" target="_blank"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;&amp;nbsp;and the inference script&amp;nbsp;&lt;a href="https://www.kaggle.com/code/iamprateek/cots-yolov7-inference/notebook" target="_blank"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;. So no need to rest guys &#128163;. Just forked my training notebook, do experiments, make submissions, and see how much can you score on the leaderboard &#128168;. This post and the shared notebook will be helpful in your journey to learn and use the state-of-the-art object detector. In my next post I will again bring you something amazing to learn till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post &#128519;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; font-weight: bold; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPG7L1B24TNTsgdnmjWwOYkcznZv8KVe1GjPzPrOWyQNxmO2IncthHRk2eNQRMh9pFLlTMweaDIFFSWtDr1WVn-waN1vLVBcWWB6N2qanzGs8_6aa_oPze8juBzpdBaZEZ-nlxoHPV536qpiZzsDBifu3LCF02YuiPkS-YCr1zeoQDSEat7JUF_jAHSw/s72-w400-h300-c/1.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total><enclosure length="333898497" type="application/octet-stream" url="https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e_training.pt"/><itunes:explicit>no</itunes:explicit><itunes:subtitle>Another post starts with you beautiful people! I hope you have already learned the state-of-the-art object detection technique Darknet's YOLOv4 from my previous posts. If you are new to my blog then I recommend you to once go through&amp;nbsp;this link&amp;nbsp;to learn about end-to-end implementation of YOLOv4. In this post we are going to learn the successor of the&amp;nbsp;official YOLOv4&amp;nbsp;:&amp;nbsp;YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors&amp;nbsp;&#128165;. Since the official implementation of YOLOv7 is still not done in the Darknet framework, in this post we are going to use the Pytorch framework to train a custom dataset and then make inferences using the same Pytorch framework. But don't worry if you are new to PyTorch &#128114;, we are not going to reinvent the wheel in fact as a practitioner our goal should be to learn how to use it. For our learning, we will use a real-world dataset and the problem-&amp;nbsp;detecting starfish in underwater images. This challenge was held on Kaggle. It is a perfect use case for our learning since there are many other tutorials that use public datasets. However, preparing a dataset is also an important job before applying the training. So first let's understand the given dataset format- Goal: To predict the presence and position of crown-of-thorns starfish in sequences of underwater images taken at various times and locations around the Great Barrier Reef. Dataset:&amp;nbsp;train/ - Folder containing training set photos of the form video_{video_id}/{video_frame_number}.jpg. [train/test].csv - containes following metadata for the images: video_id, video_frame, sequence id, sequence_frame number, image_id, annotations in COCO format.&amp;nbsp; Dataset Preparation: Let's take a look at the dataset with our Python code: Let's verify if all the images have the annotations or not. We will later remove the images from our training dataset which don't have the annotations otherwise it will affect our model accuracy- So we have only 20.93% annotated images in the given dataset. Let's remove un-annotated images metadata from our dataframe- Also, let's remove the unannotated images with the help of joblib's parallel function&amp;nbsp;for speedup. Here we need to copy the data to Kaggle's output directory since the input directory doesn't have the write access- So, we have now a total of 4919 annotated images to train/validate. Our next step is to visualize the images and see what annotations look like in these images. For this purpose, we don't need to write the code from scratch instead we will use&amp;nbsp;bounding box utility github&amp;nbsp;&#128124;. Don't forget to give a start to this GitHub repo &#128077;. Let's import the helpful bounding box creation functions from the repo as below- Next, let's fetch the bounding box coordinates using the above function- Let's check the newly created column in our dataframe- Now we have bounding box coordinates as well in COCO format. Since the given dataset has images of fixed sizes" (1280. 720); it's better to set another two columns- width and height in our dataframe as below- Next, following the best practice of training dataset preparation, we will apply cross-validation to the dataset as below- As you can see the number of samples is not same in each fold, so for the training we should experiment with each fold but for this post, I&amp;nbsp; will use the first fold- Prepare YOLO format dataset: Since till now we have cleaned and finalized the training dataset; the next step is to prepare this dataset in YOLO required format. For this step, we will export the labels into a .txt file. This .txt file will contain one row per target class where each row is in the following format: target_class_name [x_center, y_center, width, height] . Here the coordinates must be in normalized form and target class numbers start with zero. All these required conversions can be done using the below code- Let's check our converted bounding box coordinates in one of our cross folded dataframe- Now we can visualize the training dataset images with their annotations using the matplotlib library as below- And the sample annotated images look like the below- After seeing the above few samples, we can imagine how complex a problem is this to solve with computer vision as compared to publically available datasets. Here the target class is very small and images are blurred. Let's see how YOLOv7 is going to detect these starfish &#128064;. But for now, let's create the .txt files required by YOLO- Here 'nc' means a number of classes to detect that is 1 in our case, 'names' is the target class name that I defined as cots but you can give any name to it. YOLOv7 training:Till now we have prepared the dataset to start the training. For the custom training, YOLOv7 has provided the different sizes of pre-trained weights- Here I am going to use the largest one:&amp;nbsp;yolov7-e6e_training.pt. To use this pre-trained weight for our training, we also need to use its configuration file (yolov7-e6e.yaml) that we can get from this&amp;nbsp;official config files link&amp;nbsp;and the hyperparameter file (hyp.scratch.custom.yaml) that we can get from this&amp;nbsp;custom hyperparameter file link. Now first we will create a new hyperparameter configuration file and then we will copy the content from&amp;nbsp;hyp.scratch.custom.yaml file to this new file in Kaggle as below- The same we will do to copy the content of the&amp;nbsp;yolov7-e6e.yaml file to a new file in Kaggle- Notice here in the above content, I have changed 'nc:80' to 'nc:1' since we have only one class to predict. Rest all parameters are the same. Next, we will clone the official&amp;nbsp;YOLOv7 repo&amp;nbsp;in Kaggle's output directory, and then we will install the dependencies as below- Once all dependencies are installed, we will download the pre-trained weights as below- Now, we are ready to start the training with the below command- Here, a number of workers I have defined as 8 which you can change accordingly to the system of yours, device as zero means single GPU training, the default number of epochs are 300 but due to limited training time in Kaggle I have used 26 as epoch size, batch size I have used as 3 to avoid out of memory issue but it should be 16 and rest other parameters are the pre-trained weight file path and the config file paths. Once the training is completed you can find the best-trained weight file (best.pt) under&amp;nbsp;/yolov7/runs/train/ path as below- YOLOV7 Inference: Since our training is completed, it's time to make inferences on the test images and prepare a submission file as required by the competition. For making inferences first step is to load our trained model. For this purpose, we can use&amp;nbsp;torch.hub.load()&amp;nbsp;function as below- In this function, the first parameter is the YOLOv7 repo path, the second parameter is 'custom' since we are using a custom trained model, the third parameter is the best weight file path, and the fourth parameter is given as 'local' since we are loading the model from our local file system and the rest other configurations we are setting to our loaded model are easily understandable. After loading the model next step is to make the prediction. As required by this competition we need to predict the bounding box coordinates in COCO format and the confidence score of a given image in prediction and that can be achieved as below- Here for bounding box conversion, I have used the helper code from&amp;nbsp;this awsome repo&amp;nbsp;&#128591;. Now we can make the required submission as below- Here, we have iterated the iter_test variable since test images were not given as a whole instead organizer shared the API. After iterating each test image we made predictions using our custom-trained YOLOv7 model and formatted the predictions in the required format. Let's see one test image with the prediction- It looks good &#128076; but how can we know the goodness of our model? That's why I used the Kaggle dataset so that I can submit the prediction to this challenge and see my private score. Same I did and I got the following score with my trained model- Next, I checked for any other submission but with&amp;nbsp;YOLOv5 and found the below score- As we can see here, with default configurations of YOLOv7, my model has achieved a 0.602 private score as compared to 0.588 achieved by YOLOv5 which indicates that indeed the latest official release of YOLOv7 is faster and more accurate than other object detectors &#128170;. We can achieve a better result with hyperparameter tuning, different augmentation, etc. For complete code reference please follow my training script&amp;nbsp;here&amp;nbsp;and the inference script&amp;nbsp;here. So no need to rest guys &#128163;. Just forked my training notebook, do experiments, make submissions, and see how much can you score on the leaderboard &#128168;. This post and the shared notebook will be helpful in your journey to learn and use the state-of-the-art object detector. In my next post I will again bring you something amazing to learn till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post &#128519;</itunes:subtitle><itunes:author>noreply@blogger.com (Learn Data Science with PrateekG)</itunes:author><itunes:summary>Another post starts with you beautiful people! I hope you have already learned the state-of-the-art object detection technique Darknet's YOLOv4 from my previous posts. If you are new to my blog then I recommend you to once go through&amp;nbsp;this link&amp;nbsp;to learn about end-to-end implementation of YOLOv4. In this post we are going to learn the successor of the&amp;nbsp;official YOLOv4&amp;nbsp;:&amp;nbsp;YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors&amp;nbsp;&#128165;. Since the official implementation of YOLOv7 is still not done in the Darknet framework, in this post we are going to use the Pytorch framework to train a custom dataset and then make inferences using the same Pytorch framework. But don't worry if you are new to PyTorch &#128114;, we are not going to reinvent the wheel in fact as a practitioner our goal should be to learn how to use it. For our learning, we will use a real-world dataset and the problem-&amp;nbsp;detecting starfish in underwater images. This challenge was held on Kaggle. It is a perfect use case for our learning since there are many other tutorials that use public datasets. However, preparing a dataset is also an important job before applying the training. So first let's understand the given dataset format- Goal: To predict the presence and position of crown-of-thorns starfish in sequences of underwater images taken at various times and locations around the Great Barrier Reef. Dataset:&amp;nbsp;train/ - Folder containing training set photos of the form video_{video_id}/{video_frame_number}.jpg. [train/test].csv - containes following metadata for the images: video_id, video_frame, sequence id, sequence_frame number, image_id, annotations in COCO format.&amp;nbsp; Dataset Preparation: Let's take a look at the dataset with our Python code: Let's verify if all the images have the annotations or not. We will later remove the images from our training dataset which don't have the annotations otherwise it will affect our model accuracy- So we have only 20.93% annotated images in the given dataset. Let's remove un-annotated images metadata from our dataframe- Also, let's remove the unannotated images with the help of joblib's parallel function&amp;nbsp;for speedup. Here we need to copy the data to Kaggle's output directory since the input directory doesn't have the write access- So, we have now a total of 4919 annotated images to train/validate. Our next step is to visualize the images and see what annotations look like in these images. For this purpose, we don't need to write the code from scratch instead we will use&amp;nbsp;bounding box utility github&amp;nbsp;&#128124;. Don't forget to give a start to this GitHub repo &#128077;. Let's import the helpful bounding box creation functions from the repo as below- Next, let's fetch the bounding box coordinates using the above function- Let's check the newly created column in our dataframe- Now we have bounding box coordinates as well in COCO format. Since the given dataset has images of fixed sizes" (1280. 720); it's better to set another two columns- width and height in our dataframe as below- Next, following the best practice of training dataset preparation, we will apply cross-validation to the dataset as below- As you can see the number of samples is not same in each fold, so for the training we should experiment with each fold but for this post, I&amp;nbsp; will use the first fold- Prepare YOLO format dataset: Since till now we have cleaned and finalized the training dataset; the next step is to prepare this dataset in YOLO required format. For this step, we will export the labels into a .txt file. This .txt file will contain one row per target class where each row is in the following format: target_class_name [x_center, y_center, width, height] . Here the coordinates must be in normalized form and target class numbers start with zero. All these required conversions can be done using the below code- Let's check our converted bounding box coordinates in one of our cross folded dataframe- Now we can visualize the training dataset images with their annotations using the matplotlib library as below- And the sample annotated images look like the below- After seeing the above few samples, we can imagine how complex a problem is this to solve with computer vision as compared to publically available datasets. Here the target class is very small and images are blurred. Let's see how YOLOv7 is going to detect these starfish &#128064;. But for now, let's create the .txt files required by YOLO- Here 'nc' means a number of classes to detect that is 1 in our case, 'names' is the target class name that I defined as cots but you can give any name to it. YOLOv7 training:Till now we have prepared the dataset to start the training. For the custom training, YOLOv7 has provided the different sizes of pre-trained weights- Here I am going to use the largest one:&amp;nbsp;yolov7-e6e_training.pt. To use this pre-trained weight for our training, we also need to use its configuration file (yolov7-e6e.yaml) that we can get from this&amp;nbsp;official config files link&amp;nbsp;and the hyperparameter file (hyp.scratch.custom.yaml) that we can get from this&amp;nbsp;custom hyperparameter file link. Now first we will create a new hyperparameter configuration file and then we will copy the content from&amp;nbsp;hyp.scratch.custom.yaml file to this new file in Kaggle as below- The same we will do to copy the content of the&amp;nbsp;yolov7-e6e.yaml file to a new file in Kaggle- Notice here in the above content, I have changed 'nc:80' to 'nc:1' since we have only one class to predict. Rest all parameters are the same. Next, we will clone the official&amp;nbsp;YOLOv7 repo&amp;nbsp;in Kaggle's output directory, and then we will install the dependencies as below- Once all dependencies are installed, we will download the pre-trained weights as below- Now, we are ready to start the training with the below command- Here, a number of workers I have defined as 8 which you can change accordingly to the system of yours, device as zero means single GPU training, the default number of epochs are 300 but due to limited training time in Kaggle I have used 26 as epoch size, batch size I have used as 3 to avoid out of memory issue but it should be 16 and rest other parameters are the pre-trained weight file path and the config file paths. Once the training is completed you can find the best-trained weight file (best.pt) under&amp;nbsp;/yolov7/runs/train/ path as below- YOLOV7 Inference: Since our training is completed, it's time to make inferences on the test images and prepare a submission file as required by the competition. For making inferences first step is to load our trained model. For this purpose, we can use&amp;nbsp;torch.hub.load()&amp;nbsp;function as below- In this function, the first parameter is the YOLOv7 repo path, the second parameter is 'custom' since we are using a custom trained model, the third parameter is the best weight file path, and the fourth parameter is given as 'local' since we are loading the model from our local file system and the rest other configurations we are setting to our loaded model are easily understandable. After loading the model next step is to make the prediction. As required by this competition we need to predict the bounding box coordinates in COCO format and the confidence score of a given image in prediction and that can be achieved as below- Here for bounding box conversion, I have used the helper code from&amp;nbsp;this awsome repo&amp;nbsp;&#128591;. Now we can make the required submission as below- Here, we have iterated the iter_test variable since test images were not given as a whole instead organizer shared the API. After iterating each test image we made predictions using our custom-trained YOLOv7 model and formatted the predictions in the required format. Let's see one test image with the prediction- It looks good &#128076; but how can we know the goodness of our model? That's why I used the Kaggle dataset so that I can submit the prediction to this challenge and see my private score. Same I did and I got the following score with my trained model- Next, I checked for any other submission but with&amp;nbsp;YOLOv5 and found the below score- As we can see here, with default configurations of YOLOv7, my model has achieved a 0.602 private score as compared to 0.588 achieved by YOLOv5 which indicates that indeed the latest official release of YOLOv7 is faster and more accurate than other object detectors &#128170;. We can achieve a better result with hyperparameter tuning, different augmentation, etc. For complete code reference please follow my training script&amp;nbsp;here&amp;nbsp;and the inference script&amp;nbsp;here. So no need to rest guys &#128163;. Just forked my training notebook, do experiments, make submissions, and see how much can you score on the leaderboard &#128168;. This post and the shared notebook will be helpful in your journey to learn and use the state-of-the-art object detector. In my next post I will again bring you something amazing to learn till then &#128073; Go chase your dreams, have an awesome day, make every second count, and see you later in my next post &#128519;</itunes:summary><itunes:keywords>kaggle, PyTorch, YOLOv7</itunes:keywords></item><item><title>YOLObile- a new state of the art Real-Time Object Detection model for Mobile Devices </title><link>https://dsbyprateekg.blogspot.com/2020/10/yolobile-new-state-of-art-real-time.html</link><category>PyTorch</category><category>YOLObile</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Wed, 14 Oct 2020 16:36:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-4885430010271503112</guid><description>&lt;p&gt;&amp;nbsp;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVSxy7q-PjBap0LWmls212y3x2ECnFH3NJcEnR1svy4zi61SI08Nsnlvkem0uw1sNit68Utx0h0bVFbGS2IQ8L55UFEpCO3_K3t7fZmodGUs3B5LrShczUvdH0utP-sju8dp9Pa29IIe5e/s1788/yolobilemap.png" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="1212" data-original-width="1788" height="271" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVSxy7q-PjBap0LWmls212y3x2ECnFH3NJcEnR1svy4zi61SI08Nsnlvkem0uw1sNit68Utx0h0bVFbGS2IQ8L55UFEpCO3_K3t7fZmodGUs3B5LrShczUvdH0utP-sju8dp9Pa29IIe5e/w400-h271/yolobilemap.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Another post starts with you beautiful people!&lt;/p&gt;&lt;p&gt;Thanks for giving so many views on my &lt;a href="https://dsbyprateekg.blogspot.com/2020/08/how-to-use-opencv-python-with-darknets.html" target="_blank"&gt;previous post &#128077;.&lt;/a&gt;&lt;/p&gt;&lt;p&gt;I am glad to see my previous posts are helping people to use state of the art object detection and recognition deep learning model in their projects. If you are new to my blog, I recommend seeing once my previous posts, and you will not be disappointed if your goal is to learn applied computer vision free of cost. Continuing my journey of sharing knowledge in this post I am going to share with you a new state of the art framework for object detection on mobile devices-&amp;nbsp;&lt;b&gt;&lt;a href="https://arxiv.org/abs/2009.05697" target="_blank"&gt;YOLObile&lt;/a&gt;&amp;nbsp;&#128241;&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;There has been a trade-off between speed and the accuracy of object detections. For example, the state of the art,&amp;nbsp;&lt;a href="http://dsbyprateekg.blogspot.com/2020/05/how-to-install-and-compile-yolo-v4-with.html" target="_blank"&gt;YOLOv4&lt;/a&gt; model gives us a very accurate detection but its speed is slow if we want to use it on a mobile device. On the other hand, its lighter version YOLOv4-tiny works very fast on a mobile device but its accuracy reduces. For a detailed comparison of FPS vs mAP you can refer to the above diagram. Nowadays computer vision techniques are improving day by day due to hardware advancement and so using AI in mobile devices is a hot choice for every mobile app.&lt;/p&gt;&lt;p&gt;However, the current state-of-the-art object detection works are either accuracy-oriented using a large model but leading to high latency or speed-oriented using a lightweight model but sacrificing accuracy. To solve this problem respected authors [&lt;b&gt;Yuxuan Cai, Hongjia Li, Geng Yuan, Wei Niu, Yanyu Li, Xulong Tang, Bin Ren, Yanzhi Wang&lt;/b&gt;] of&amp;nbsp;&lt;b&gt;&lt;a href="https://arxiv.org/abs/2009.05697" target="_blank"&gt;this paper&lt;/a&gt;&amp;nbsp;&lt;/b&gt;have introduced the &lt;b&gt;YOLObile framework&lt;/b&gt;, a real-time object detection on mobile devices via &lt;b&gt;compression-compilation co-design. &#128165;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;As per the authors- A novel &lt;b&gt;block-punched pruning scheme&lt;/b&gt; is proposed for &lt;b&gt;any &lt;/b&gt;kernel size. To improve computational efficiency on mobile devices, a &lt;b&gt;GPU-CPU collaborative scheme&lt;/b&gt; is adopted along with &lt;b&gt;advanced &lt;/b&gt;compiler-assisted optimizations. Experimental results indicate that our pruning scheme achieves a 14x compression rate of YOLOv4 with 49.0 mAP. Under our YOLObile framework, we achieve 17 FPS inference speed using GPU on Samsung Galaxy S20. By incorporating our proposed GPU-CPU collaborative scheme, the inference speed is increased to 19.1 FPS and outperforms the original YOLOv4 by &lt;b&gt;5x speedup&lt;/b&gt;.&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjVXIYJfkH8DrdUIL6CELaemffFWlvAKpAuOU1V4yPN9wO2gZHxoVEQPymQ8MreJb27kdmVoITanOwLrh0m66saTNyYu_Bt1gGusZkVC-AclFO3GPCwyZ8w4y34Gbkc9cRXQoYCWI6nE5Z/s434/acc_speed_yolobile.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="227" data-original-width="434" height="209" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjVXIYJfkH8DrdUIL6CELaemffFWlvAKpAuOU1V4yPN9wO2gZHxoVEQPymQ8MreJb27kdmVoITanOwLrh0m66saTNyYu_Bt1gGusZkVC-AclFO3GPCwyZ8w4y34Gbkc9cRXQoYCWI6nE5Z/w400-h209/acc_speed_yolobile.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;That's quite a solid approach right &#128170;. If I summarize their approach then I would say that their approach is basically &lt;b&gt;unstructured weights pruning&lt;/b&gt;, with a bit of structure on top, where they group the weights in blocks of 4 channels and 8 filters (x32). This structure allows them to improve parallelism. One important point to know here is that&amp;nbsp;unstructured sparsity is very difficult to parallelize compare to structured sparsity also it is hardware-specific.On the other hand, up to &lt;b&gt;93% of reduction&lt;/b&gt; in weight size without a real loss in accuracy is really tempting with this framework. &#128157;&lt;/p&gt;&lt;p&gt;I know after reading this all of you want to see the result of this new approach on any benchmark dataset&#128165; and the good news is that authors have recently released the weights (in PyTorch format) which you can download from&amp;nbsp;&lt;b&gt;&lt;a href="https://drive.google.com/drive/folders/1FcWdXcWc3vScV-guIrxWsWGhjQwPOEQW?usp=sharing" target="_blank"&gt;this link&lt;/a&gt;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;From the download link, you will get two weights files of 245 MB size: best8x-514.pt and best14x-49.pt. These two weights are the output of the 8x and 14x pruning models. According to the authors- these models are trained on a server with eight NVIDIA RTX 2080Ti GPUs. The training methods are implemented using PyTorch API. The details of both weights are as below:-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbgDAxXnuGxLvNlNKvMOfCfEqQF1QdjnI932WTMTrKMr7fmRcJpkCBoCXAtcMC6cSC546C__vhcwsWO2eGcTr1s_8pww6CyNHlp0CuWf1aBGsjzw4KmaddvZUucnc3Cgo5UwGJcsFdN0Ss/s702/dense_model.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="370" data-original-width="702" height="338" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbgDAxXnuGxLvNlNKvMOfCfEqQF1QdjnI932WTMTrKMr7fmRcJpkCBoCXAtcMC6cSC546C__vhcwsWO2eGcTr1s_8pww6CyNHlp0CuWf1aBGsjzw4KmaddvZUucnc3Cgo5UwGJcsFdN0Ss/w640-h338/dense_model.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;I tested the MAP on coco 2017 dataset (80 classes) with image-size of 320, batch-size of 8, best8x-514 weights in my Windows 10 laptop having 6GB of NVIDIA GTX 1060 and results are as below-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaRm49_51RRxnFlaQsMGOBSx6Euw7I9zZSqVjK5vPru6UI-TMPP235R_K1XPQK6nIyLQ2zy3_xYmWOLrcIfVLP18fJGD4X4PcOpSsIIp1TgWIBIc6S1uR7zIWY2jwlc9Nd-F35Bcq05UG0/s847/yolobile_res1.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="847" data-original-width="738" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaRm49_51RRxnFlaQsMGOBSx6Euw7I9zZSqVjK5vPru6UI-TMPP235R_K1XPQK6nIyLQ2zy3_xYmWOLrcIfVLP18fJGD4X4PcOpSsIIp1TgWIBIc6S1uR7zIWY2jwlc9Nd-F35Bcq05UG0/w558-h640/yolobile_res1.JPG" width="558" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiazppCyoWt-6GFRq-luQjNSQwfFS4jUro5TfOYDHcsVYJUIMxTU4fPvnGJNowWRbx1cx6c-ZlKss0qgf5HVJKXB1l5E4vNfSyNqDra2scMTOAOY-L33kHTcukuFLnBpn0TqR2pDxbXGTJp/s836/yolobile_res2.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="836" data-original-width="745" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiazppCyoWt-6GFRq-luQjNSQwfFS4jUro5TfOYDHcsVYJUIMxTU4fPvnGJNowWRbx1cx6c-ZlKss0qgf5HVJKXB1l5E4vNfSyNqDra2scMTOAOY-L33kHTcukuFLnBpn0TqR2pDxbXGTJp/w570-h640/yolobile_res2.JPG" width="570" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWQsWydsYghOM57V73ejW5yuwLhGVs1tSTy19wpbCGyrCQhNlY1SrZR37bveJ0WJiAIUyQ5pETZZ5aDCd_aH3gPT4aXCX2ttCqNbgpRCmrk34faYST3DeIbGytpdu2S3ibOsEPnqJohY5N/s723/yolobile_coco_map.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="253" data-original-width="723" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWQsWydsYghOM57V73ejW5yuwLhGVs1tSTy19wpbCGyrCQhNlY1SrZR37bveJ0WJiAIUyQ5pETZZ5aDCd_aH3gPT4aXCX2ttCqNbgpRCmrk34faYST3DeIbGytpdu2S3ibOsEPnqJohY5N/w640-h224/yolobile_coco_map.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;For evaluating this framework, the authors used an off-the-shelf Samsung Galaxy S20 
smart-phone, which has a Qualcomm Snapdragon 865 Octa-core CPU&amp;nbsp; and&amp;nbsp; a&amp;nbsp; 
Qualcomm&amp;nbsp; Adreno&amp;nbsp; 650&amp;nbsp; GPU.&amp;nbsp; Each test ran on&amp;nbsp; 50&amp;nbsp; different input frames&amp;nbsp; (images),&amp;nbsp; with the average speed results reported. The 
YOLObile is derived based onYOLOv4, with 320×320 input size, and train on the MS COCO dataset. They denote mAP as the Average 
Precision under IoU 0.5 threshold and AP@[.5:.95] as the Aver-age 
Precision under IoU from 0.5 to 0.95-&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7LZ7T_GmK2CoprWgmXqMa_rIV9XDTbcf0AqAxyzrRqfwkCVmXCmmjAXGBWI-mwQ_B4QwKve6F7bcMYVMZL9T5f979RXhuWiGnSz5covZMTi4csyjt9N8uFgkGQCRWspnTvPKBE-JEKGH-/s1039/yolobile_fps.JPG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="301" data-original-width="1039" height="185" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7LZ7T_GmK2CoprWgmXqMa_rIV9XDTbcf0AqAxyzrRqfwkCVmXCmmjAXGBWI-mwQ_B4QwKve6F7bcMYVMZL9T5f979RXhuWiGnSz5covZMTi4csyjt9N8uFgkGQCRWspnTvPKBE-JEKGH-/w640-h185/yolobile_fps.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;The above evaluation result is quite amazing right&#128165;. A real-time object detection framework on mobile devices via compression-compilation co-design will definitely going to be a game-changer. I have uploaded the inference code in&amp;nbsp;&lt;a href="https://github.com/dsbyprateekg/YOLObile_compression-compilation" target="_blank"&gt;my github repository&lt;/a&gt; You can follow the same to test the new framework in your own system. Since this framework is quite new there are no open-source code is available for the training on a custom dataset but as soon as I found any one of the Darknet or TensorFlow implementation, I will share with you all. Till then Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVSxy7q-PjBap0LWmls212y3x2ECnFH3NJcEnR1svy4zi61SI08Nsnlvkem0uw1sNit68Utx0h0bVFbGS2IQ8L55UFEpCO3_K3t7fZmodGUs3B5LrShczUvdH0utP-sju8dp9Pa29IIe5e/s72-w400-h271-c/yolobilemap.png" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">59</thr:total></item><item><title>How to use opencv-python with Darknet's YOLOv4?</title><link>https://dsbyprateekg.blogspot.com/2020/08/how-to-use-opencv-python-with-darknets.html</link><category>computer vision</category><category>Darknet</category><category>opencv</category><category>YOLOv4</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Fri, 14 Aug 2020 21:22:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-6576429248425253473</guid><description>&lt;p&gt;&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg00ZoKTLDHTZTYxfQeXJmgJTB6siaXBX5fwohMMjz150O0wR9eDtWKHqF1wUVQsT9oaR4j68eplqRH8h0fM7fviBONie3UNtPeDJC7vJxkqNdmWWPQDZ3GXrUZBMnexclNtc-Eldq_Irs6/s741/plus.PNG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="316" data-original-width="741" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg00ZoKTLDHTZTYxfQeXJmgJTB6siaXBX5fwohMMjz150O0wR9eDtWKHqF1wUVQsT9oaR4j68eplqRH8h0fM7fviBONie3UNtPeDJC7vJxkqNdmWWPQDZ3GXrUZBMnexclNtc-Eldq_Irs6/s640/plus.PNG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;Another post starts with you beautiful people &#128522;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;Thank you all for messaging me your doubts about Darknet's YOLOv4. I am very happy to see in a very short amount of time my lovely aspiring data scientists have learned a state of the art object detection and recognition technique. If you are new to my blog and to computer vision then please check my following blog posts one by one-&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;ol style="text-align: left;"&gt;&lt;li&gt;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/05/how-to-install-and-compile-yolo-v4-with.html" target="_blank"&gt;Setup Darknet's YOLOv4&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/06/identify-eight-types-of-indian.html" target="_blank"&gt;Train custom dataset with YOLOv4&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/06/how-to-deploy-your-ml-model-as-fast-api.html" target="_blank"&gt;Create production-ready API of YOLOv4 model&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/07/learn-fastest-way-to-build-data-apps.html" target="_blank"&gt;Create a web app for your YOLOv4 model&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;Since now we have learned to use YOLOv4 built on Darknet's framework. In this post, I am going to share with you how can you use your trained YOLOv4 model with another awesome&amp;nbsp;computer vision and machine learning software library-&amp;nbsp;&lt;a href="https://opencv.org/about/" target="_blank"&gt;OpenCV&lt;/a&gt;&amp;nbsp;and of course with Python &#128013;. Yes, the Python wrapper of OpenCV library has just released it's latest version with support of YOLOv4 which you can install in your system using below command-&lt;/div&gt;&lt;blockquote&gt;&lt;div&gt;&lt;div style="background-color: #fffffe; font-family: monospace, consolas, &amp;quot;courier new&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;"&gt;pip&amp;nbsp;install&amp;nbsp;opencv-python&amp;nbsp;--upgrade&lt;/div&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;p&gt;For Google Colab and Kaggle, use the following command-&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;!&lt;span face="" style="background-color: #fffffe; font-family: monospace, consolas, &amp;quot;courier new&amp;quot;, monospace; font-size: 14px; white-space: pre;"&gt;pip&amp;nbsp;install&amp;nbsp;opencv-python&amp;nbsp;--upgrade&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;For example in my Colab notebook, the above command with output is like below-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgn-UOAuDJhzhuvc-CHVZB9b1xrHU9B7MBz-rN2KumuvYE14kPuWwUbCoLBSCoD2RMYn_3VQC7Jyw5vceigQL-ES83c-EtntPhzc6qxSyHJ9SA6kbnI8PE-NS57c_nWGzit3XA6Cw9IyugC/s1300/install_opencv_python.PNG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="279" data-original-width="1300" height="171" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgn-UOAuDJhzhuvc-CHVZB9b1xrHU9B7MBz-rN2KumuvYE14kPuWwUbCoLBSCoD2RMYn_3VQC7Jyw5vceigQL-ES83c-EtntPhzc6qxSyHJ9SA6kbnI8PE-NS57c_nWGzit3XA6Cw9IyugC/w800-h171/install_opencv_python.PNG" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;As you can see in the last line of the output, the version of opencv-python is 4.4.0.40 which is, in fact, the latest one so our installation is done successfully. Now I will show you how can we test a pre-trained YOLOv4 model trained on coco dataset with Python and OpenCV-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijuLA3RADda1sFb5sroonFpCmjVEYr6er0wMlihizGVMLoN5AClCSV_6z0afArPTMhG1KX87I7DtAg-kBud08puEUn9lgLfBC4t_MSRhPj6C778CS48kjd0jeU3qx5-rj0zM2uJiS7Wsjl/s1154/coco_code.PNG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="649" data-original-width="1154" height="563" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijuLA3RADda1sFb5sroonFpCmjVEYr6er0wMlihizGVMLoN5AClCSV_6z0afArPTMhG1KX87I7DtAg-kBud08puEUn9lgLfBC4t_MSRhPj6C778CS48kjd0jeU3qx5-rj0zM2uJiS7Wsjl/w1000-h563/coco_code.PNG" width="1000" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;Let's understand the above code snippet&#128173;-&lt;/p&gt;&lt;p&gt;First I have imported the OpenCV library as cv and cv2_imshow library specific to Colab for opening image in Colab notebook.&lt;/p&gt;&lt;p&gt;In the next line, I have created an OpenCV model from the YOLOv4 configuration and pre-trained weights file. If you don't have these files then you can download those from&amp;nbsp;&lt;a href="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights" target="_blank"&gt;this link&lt;/a&gt;. Here OpenCV's &lt;b&gt;dnn_DetectionModel()&lt;/b&gt; function is all that you need for this purpose &#128147;&lt;/p&gt;&lt;p&gt;After creating the model, I did some input image preprocessing for this model like resizing the image to a specific resolution, normalizing, and maintaining the RGB format.&lt;/p&gt;&lt;p&gt;In the next line, I have uploaded a new unseen image where I want to detect some objects.&lt;/p&gt;&lt;p&gt;Next, I am iterating the names of the objects. Since our pre-trained model is trained on 80 classes known as the coco dataset, we need to take the names one by one from the file.&lt;/p&gt;&lt;p&gt;In the next line, I am using &lt;b&gt;detect()&lt;/b&gt; function to detect the objects on the input image. This function is equivalent to the Darknet's detection function. It gives the object name, calculates the confidence score, and gives the bounding boxes.&lt;/p&gt;&lt;p&gt;Next, inside the for loop, I have written logic to put object name, confidence score, and draw a bounding box around the detected object using OpenCV inbuilt functions.&lt;/p&gt;&lt;p&gt;In the end, to show the image with the detection I am using the cv2_imshow() function. If you are using your own system then instead of this function use the OpenCV's imshow() function.&lt;/p&gt;&lt;p&gt;Now we are ready to test our image. Once I run the above code, I get the following image with the detection-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg01cROc5yv9dS2ditrJk_U3OcDMHa4VMeIRm4rkaqVYFXg4Vx6MLIC-9OnkIbc8WoHzsagtCBzpefKsFC1nXXFawcQjLtYMJUmjYGgzGPbKvG1_rAxe9H87IbMXKdV71Py5LeFRMimYlLJ/s1366/coco.png" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="768" data-original-width="1366" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg01cROc5yv9dS2ditrJk_U3OcDMHa4VMeIRm4rkaqVYFXg4Vx6MLIC-9OnkIbc8WoHzsagtCBzpefKsFC1nXXFawcQjLtYMJUmjYGgzGPbKvG1_rAxe9H87IbMXKdV71Py5LeFRMimYlLJ/s640/coco.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;See the result, it's amazing right &#128165;. The pre-trained YOLOv4 model with OpenCV and Python is successfully able to detect person, wine glass, dining table, bowl, bottle with a high confidence score. In the same way, you can test your custom trained YOLOv4 model. You just need to replace YOLOv4 specific configuration file, weights, and class name file with your own, and the remaining code will be the same as it is. Let me show you the example where I have used my custom YOLOv4 model which I trained for a&amp;nbsp;&lt;b&gt;&lt;a href="https://www.kaggle.com/iamprateek/wheat-head-detection-yolov4" target="_blank"&gt;Global Wheat Head Detection Challenge&lt;/a&gt;. &lt;/b&gt;Since at the time of the participation OpenCV support was not released for Darknet's YOLOv4 so I had to convert my YOLOv4 model to keras/TensorFlow model and made changes accordingly. But now I can use it very easily &#128114;-&lt;/p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifPH1kp21e_tE50K3chax9s-f0lM-LR5XKIKEjFyDYhKRHHD2zvjXggv93VgVy7Ix67_6ZAYgzFSLcVBFEdAbJx6dBeGBmmNtEhZNGFF7hA-hhMOk5gVsbYIK64XoJ5VQU3ByM9Zs1g4X0/s1403/opencv_python_code.PNG" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="630" data-original-width="1403" height="359" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifPH1kp21e_tE50K3chax9s-f0lM-LR5XKIKEjFyDYhKRHHD2zvjXggv93VgVy7Ix67_6ZAYgzFSLcVBFEdAbJx6dBeGBmmNtEhZNGFF7hA-hhMOk5gVsbYIK64XoJ5VQU3ByM9Zs1g4X0/w800-h359/opencv_python_code.PNG" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiktlxrGIXYoOXwokgERQgzvL7ahAcwcDmGVbzh9eiqGvTEN24RPuQ5h3RArNQ3OIpvqy7itgIhnym21FoFRcNkARFdWnoIZ4ycff8MNjkGdJTWkWcfrXaVXJriu6Xw6e1dX8WL_sTJtNdn/s1024/wheat_prediction.png" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="1024" data-original-width="1024" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiktlxrGIXYoOXwokgERQgzvL7ahAcwcDmGVbzh9eiqGvTEN24RPuQ5h3RArNQ3OIpvqy7itgIhnym21FoFRcNkARFdWnoIZ4ycff8MNjkGdJTWkWcfrXaVXJriu6Xw6e1dX8WL_sTJtNdn/s640/wheat_prediction.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;Wow, that was so easy &#128170;. For this competition, I had to put so much effort to make Darknet work in the Kaggle platform. But now with opencv-python, it is very easy for me. If you are interested to see my complete solution to the challenge you can check&amp;nbsp;&lt;b&gt;&lt;a href="https://www.kaggle.com/iamprateek/wheat-head-detection-yolov4" target="_blank"&gt;this link&lt;/a&gt;&amp;nbsp;☝.&lt;/b&gt;&lt;/p&gt;&lt;p&gt;That's it guys for today! From this post, you have learned to use your Darknet's YOLOv4 model with OpenCV and Python. Now there is no solid reason to not use this state of the art object detection and recognition neural network model in your project. The links I have shared at the starting of this blog and my solution link of a real-world object detection competition are quite enough to master in this field. So don't stop yourself &#128165;, try the learning from this post on your own, explore the code with experiments and in my next blog post, I will again share with you something new and exciting.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg00ZoKTLDHTZTYxfQeXJmgJTB6siaXBX5fwohMMjz150O0wR9eDtWKHqF1wUVQsT9oaR4j68eplqRH8h0fM7fviBONie3UNtPeDJC7vJxkqNdmWWPQDZ3GXrUZBMnexclNtc-Eldq_Irs6/s72-c/plus.PNG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">93</thr:total><enclosure length="257717640" type="application/octet-stream" url="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights"/><itunes:explicit>no</itunes:explicit><itunes:subtitle>Another post starts with you beautiful people &#128522;Thank you all for messaging me your doubts about Darknet's YOLOv4. I am very happy to see in a very short amount of time my lovely aspiring data scientists have learned a state of the art object detection and recognition technique. If you are new to my blog and to computer vision then please check my following blog posts one by one-Setup Darknet's YOLOv4Train custom dataset with YOLOv4Create production-ready API of YOLOv4 modelCreate a web app for your YOLOv4 modelSince now we have learned to use YOLOv4 built on Darknet's framework. In this post, I am going to share with you how can you use your trained YOLOv4 model with another awesome&amp;nbsp;computer vision and machine learning software library-&amp;nbsp;OpenCV&amp;nbsp;and of course with Python &#128013;. Yes, the Python wrapper of OpenCV library has just released it's latest version with support of YOLOv4 which you can install in your system using below command-pip&amp;nbsp;install&amp;nbsp;opencv-python&amp;nbsp;--upgrade For Google Colab and Kaggle, use the following command- !pip&amp;nbsp;install&amp;nbsp;opencv-python&amp;nbsp;--upgrade For example in my Colab notebook, the above command with output is like below- As you can see in the last line of the output, the version of opencv-python is 4.4.0.40 which is, in fact, the latest one so our installation is done successfully. Now I will show you how can we test a pre-trained YOLOv4 model trained on coco dataset with Python and OpenCV- Let's understand the above code snippet&#128173;- First I have imported the OpenCV library as cv and cv2_imshow library specific to Colab for opening image in Colab notebook. In the next line, I have created an OpenCV model from the YOLOv4 configuration and pre-trained weights file. If you don't have these files then you can download those from&amp;nbsp;this link. Here OpenCV's dnn_DetectionModel() function is all that you need for this purpose &#128147; After creating the model, I did some input image preprocessing for this model like resizing the image to a specific resolution, normalizing, and maintaining the RGB format. In the next line, I have uploaded a new unseen image where I want to detect some objects. Next, I am iterating the names of the objects. Since our pre-trained model is trained on 80 classes known as the coco dataset, we need to take the names one by one from the file. In the next line, I am using detect() function to detect the objects on the input image. This function is equivalent to the Darknet's detection function. It gives the object name, calculates the confidence score, and gives the bounding boxes. Next, inside the for loop, I have written logic to put object name, confidence score, and draw a bounding box around the detected object using OpenCV inbuilt functions. In the end, to show the image with the detection I am using the cv2_imshow() function. If you are using your own system then instead of this function use the OpenCV's imshow() function. Now we are ready to test our image. Once I run the above code, I get the following image with the detection- See the result, it's amazing right &#128165;. The pre-trained YOLOv4 model with OpenCV and Python is successfully able to detect person, wine glass, dining table, bowl, bottle with a high confidence score. In the same way, you can test your custom trained YOLOv4 model. You just need to replace YOLOv4 specific configuration file, weights, and class name file with your own, and the remaining code will be the same as it is. Let me show you the example where I have used my custom YOLOv4 model which I trained for a&amp;nbsp;Global Wheat Head Detection Challenge. Since at the time of the participation OpenCV support was not released for Darknet's YOLOv4 so I had to convert my YOLOv4 model to keras/TensorFlow model and made changes accordingly. But now I can use it very easily &#128114;- Wow, that was so easy &#128170;. For this competition, I had to put so much effort to make Darknet work in the Kaggle platform. But now with opencv-python, it is very easy for me. If you are interested to see my complete solution to the challenge you can check&amp;nbsp;this link&amp;nbsp;☝. That's it guys for today! From this post, you have learned to use your Darknet's YOLOv4 model with OpenCV and Python. Now there is no solid reason to not use this state of the art object detection and recognition neural network model in your project. The links I have shared at the starting of this blog and my solution link of a real-world object detection competition are quite enough to master in this field. So don't stop yourself &#128165;, try the learning from this post on your own, explore the code with experiments and in my next blog post, I will again share with you something new and exciting.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;</itunes:subtitle><itunes:author>noreply@blogger.com (Learn Data Science with PrateekG)</itunes:author><itunes:summary>Another post starts with you beautiful people &#128522;Thank you all for messaging me your doubts about Darknet's YOLOv4. I am very happy to see in a very short amount of time my lovely aspiring data scientists have learned a state of the art object detection and recognition technique. If you are new to my blog and to computer vision then please check my following blog posts one by one-Setup Darknet's YOLOv4Train custom dataset with YOLOv4Create production-ready API of YOLOv4 modelCreate a web app for your YOLOv4 modelSince now we have learned to use YOLOv4 built on Darknet's framework. In this post, I am going to share with you how can you use your trained YOLOv4 model with another awesome&amp;nbsp;computer vision and machine learning software library-&amp;nbsp;OpenCV&amp;nbsp;and of course with Python &#128013;. Yes, the Python wrapper of OpenCV library has just released it's latest version with support of YOLOv4 which you can install in your system using below command-pip&amp;nbsp;install&amp;nbsp;opencv-python&amp;nbsp;--upgrade For Google Colab and Kaggle, use the following command- !pip&amp;nbsp;install&amp;nbsp;opencv-python&amp;nbsp;--upgrade For example in my Colab notebook, the above command with output is like below- As you can see in the last line of the output, the version of opencv-python is 4.4.0.40 which is, in fact, the latest one so our installation is done successfully. Now I will show you how can we test a pre-trained YOLOv4 model trained on coco dataset with Python and OpenCV- Let's understand the above code snippet&#128173;- First I have imported the OpenCV library as cv and cv2_imshow library specific to Colab for opening image in Colab notebook. In the next line, I have created an OpenCV model from the YOLOv4 configuration and pre-trained weights file. If you don't have these files then you can download those from&amp;nbsp;this link. Here OpenCV's dnn_DetectionModel() function is all that you need for this purpose &#128147; After creating the model, I did some input image preprocessing for this model like resizing the image to a specific resolution, normalizing, and maintaining the RGB format. In the next line, I have uploaded a new unseen image where I want to detect some objects. Next, I am iterating the names of the objects. Since our pre-trained model is trained on 80 classes known as the coco dataset, we need to take the names one by one from the file. In the next line, I am using detect() function to detect the objects on the input image. This function is equivalent to the Darknet's detection function. It gives the object name, calculates the confidence score, and gives the bounding boxes. Next, inside the for loop, I have written logic to put object name, confidence score, and draw a bounding box around the detected object using OpenCV inbuilt functions. In the end, to show the image with the detection I am using the cv2_imshow() function. If you are using your own system then instead of this function use the OpenCV's imshow() function. Now we are ready to test our image. Once I run the above code, I get the following image with the detection- See the result, it's amazing right &#128165;. The pre-trained YOLOv4 model with OpenCV and Python is successfully able to detect person, wine glass, dining table, bowl, bottle with a high confidence score. In the same way, you can test your custom trained YOLOv4 model. You just need to replace YOLOv4 specific configuration file, weights, and class name file with your own, and the remaining code will be the same as it is. Let me show you the example where I have used my custom YOLOv4 model which I trained for a&amp;nbsp;Global Wheat Head Detection Challenge. Since at the time of the participation OpenCV support was not released for Darknet's YOLOv4 so I had to convert my YOLOv4 model to keras/TensorFlow model and made changes accordingly. But now I can use it very easily &#128114;- Wow, that was so easy &#128170;. For this competition, I had to put so much effort to make Darknet work in the Kaggle platform. But now with opencv-python, it is very easy for me. If you are interested to see my complete solution to the challenge you can check&amp;nbsp;this link&amp;nbsp;☝. That's it guys for today! From this post, you have learned to use your Darknet's YOLOv4 model with OpenCV and Python. Now there is no solid reason to not use this state of the art object detection and recognition neural network model in your project. The links I have shared at the starting of this blog and my solution link of a real-world object detection competition are quite enough to master in this field. So don't stop yourself &#128165;, try the learning from this post on your own, explore the code with experiments and in my next blog post, I will again share with you something new and exciting.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count, and see you later in my next post&#128519;</itunes:summary><itunes:keywords>computer vision, Darknet, opencv, YOLOv4</itunes:keywords></item><item><title>Learn the fastest way to build data apps</title><link>https://dsbyprateekg.blogspot.com/2020/07/learn-fastest-way-to-build-data-apps.html</link><category>Streamlit</category><category>yolov3</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Sat, 11 Jul 2020 22:37:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-2554964895024505106</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVmqkuFzOfPa4BmpF1rTx4eOlnTAhCAid1wY_sGHzmlWPWuaDFra6JFTskcCIGFXmiKioO5xLD0uz7mR_OxXHE8XoYSfy1rXFyJVnyje0WMWrGrK_CEYAQKDKHANW1fBlmv6don6u77Gor/s1600/ys.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="391" data-original-width="651" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVmqkuFzOfPa4BmpF1rTx4eOlnTAhCAid1wY_sGHzmlWPWuaDFra6JFTskcCIGFXmiKioO5xLD0uz7mR_OxXHE8XoYSfy1rXFyJVnyje0WMWrGrK_CEYAQKDKHANW1fBlmv6don6u77Gor/s400/ys.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
Another post starts with you beautiful people!&lt;br /&gt;
I hope you have enjoyed and learned something new from my previous three posts about machine learning model deployment. In one post we have learned&amp;nbsp;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/06/how-to-deploy-your-ml-model-as-fast-api.html" target="_blank"&gt;How to deploy a model as FastAPI?&lt;/a&gt;&amp;nbsp;I&lt;/b&gt;n the second post, we have learned&amp;nbsp;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2019/02/how-to-deploy-and-consume-my-base-deep.html" target="_blank"&gt;How to deploy a deep learning model as RestAPI&lt;/a&gt;? &lt;/b&gt;and in the third post, we have also learned&amp;nbsp;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2019/03/how-to-use-redis-in-windows-along-with.html" target="_blank"&gt;How to scale your deep learning model API?&lt;/a&gt;&amp;nbsp;&lt;/b&gt;If you are following my blog posts, you have seen how easily you have transit yourselves from aspiring to a mature data scientist. In this new post, I am going to share a new framework-&amp;nbsp;&lt;a href="https://www.streamlit.io/" target="_blank"&gt;&lt;b&gt;Streamlit&lt;/b&gt;&lt;/a&gt; which will help you to easily create a beautiful app with Python only. I will show here how had I used the Streamlit framework to create an app for my YOLOv3 custom model.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is Streamlit?&lt;/b&gt;&lt;br /&gt;
Streamlit’s open-source app framework is the easiest way for data scientists and machine learning engineers to create beautiful, performant apps in only a few hours!&amp;nbsp; All in pure Python. All for free. With this framework, building an app with ML/DL/NLP model is as easy as writing Python scripts.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Installing Streamlit?&lt;/b&gt;&lt;br /&gt;
To install Streamlit, open your activated virtual env in Anaconda Prompt, or if you have not one then create it. Then run the following command to install the Streamlit framework: &lt;b&gt;pip install --upgrade streamlit&lt;/b&gt;&lt;br /&gt;
In my case, I have created a virtual env 'stream' in Anaconda Prompt with Python 3.7 and it looks like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrgLl26E38dBoD6cLNQI-5h5HPiwK7kZyptZCT-IDDDK5oxqReT_jio8Nb2IVraUoWqYcj95ly-cZUtnqNK1d1gAdYyjyRSc_NwEgNoHB8koZ464wY0AnRIYjB8GfCJ1oV2lj1yqDFrOre/s1600/st1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="153" data-original-width="559" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrgLl26E38dBoD6cLNQI-5h5HPiwK7kZyptZCT-IDDDK5oxqReT_jio8Nb2IVraUoWqYcj95ly-cZUtnqNK1d1gAdYyjyRSc_NwEgNoHB8koZ464wY0AnRIYjB8GfCJ1oV2lj1yqDFrOre/s1600/st1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Once the installation is done we can verify it by running its inbuilt help app with below command-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vzHsmLdwGDVupXGRxj1-BawIwyx7YevyrfUBgl5oPOg7gFM5Fi9KHcaGZYKFX0K8_ih6jfyXRkanJRqSLewqRrKWlDXjemmK69wsGVr8mWPAqlFkpqcqkt_HMIJsHeGQ5YalGfUqG8bC/s1600/st2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="47" data-original-width="411" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vzHsmLdwGDVupXGRxj1-BawIwyx7YevyrfUBgl5oPOg7gFM5Fi9KHcaGZYKFX0K8_ih6jfyXRkanJRqSLewqRrKWlDXjemmK69wsGVr8mWPAqlFkpqcqkt_HMIJsHeGQ5YalGfUqG8bC/s1600/st2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
As soon as you run the above command, it will ask you to enter your email. Fill up your email and press enter. After this, a welcome screen will appear as well as in the next few seconds the sample app will open in a new tab in your default browser. The welcome console will look like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaB7Eg64LqN1Xr23ou3HX3s9OasS8qcBcJopUDCmIzt5xJxYm2mUCWw3AZ6TZ9nY96-Lj3OGjFFmqB_l9aJpGFIGFzK3d07biW0SU8OwkD1IPfFe7Gez7frJHk6WynvtEjZuJZ67K7hKW7/s1600/st3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="647" data-original-width="741" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaB7Eg64LqN1Xr23ou3HX3s9OasS8qcBcJopUDCmIzt5xJxYm2mUCWw3AZ6TZ9nY96-Lj3OGjFFmqB_l9aJpGFIGFzK3d07biW0SU8OwkD1IPfFe7Gez7frJHk6WynvtEjZuJZ67K7hKW7/s1600/st3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
And the default app in the browser will look like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzhRPWtY3YOcwpVpw55Yznz-x0sA9SNhdJQY2hMOV5D2XV6X3xOeBhNQ3jjIC-FgLun9C-PPQefzYcSfKHpOAMc1GRcks2uJ0blmjK6scVu1Uj4YI1jQrDTmQj14mlCfOJPad2ufjE3OhZ/s1600/st4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="758" data-original-width="1299" height="373" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzhRPWtY3YOcwpVpw55Yznz-x0sA9SNhdJQY2hMOV5D2XV6X3xOeBhNQ3jjIC-FgLun9C-PPQefzYcSfKHpOAMc1GRcks2uJ0blmjK6scVu1Uj4YI1jQrDTmQj14mlCfOJPad2ufjE3OhZ/s640/st4.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Click on the 'Choose a demo' dropdown. You will see their sample demo options-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZGwr1xNlRMXkZ8v4btsSCvOKV0hl4kVaoduoQg_zOeX08twjfAC2LhNgcgiqo5Qf8iUA_E_8qOghkiO-qpxopn5waxpbETL-YTx6sTGgSOZg0ebO_XrqUUh1Tx9V9AUPzw8TiODtfGq1S/s1600/st5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="757" data-original-width="1297" height="372" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZGwr1xNlRMXkZ8v4btsSCvOKV0hl4kVaoduoQg_zOeX08twjfAC2LhNgcgiqo5Qf8iUA_E_8qOghkiO-qpxopn5waxpbETL-YTx6sTGgSOZg0ebO_XrqUUh1Tx9V9AUPzw8TiODtfGq1S/s640/st5.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Now click on any of the options. I selected the last option and it looks like below after the selection-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEie3Wy13fnjc8hgj7DI9iQwXMVypb-23awjzYklewa8Bx0w3ac88lTME7hq26opp4bjmzM2SWMBdvKubnUITQGIdAZAnNg_wb1048y921En0jYiGWFN4PUolIMTa5_ya6mgKap9nacYAUKk/s1600/st8.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="914" data-original-width="1327" height="440" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEie3Wy13fnjc8hgj7DI9iQwXMVypb-23awjzYklewa8Bx0w3ac88lTME7hq26opp4bjmzM2SWMBdvKubnUITQGIdAZAnNg_wb1048y921En0jYiGWFN4PUolIMTa5_ya6mgKap9nacYAUKk/s640/st8.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
How beautiful it is, right! This demo app demonstrates a Pandas Dataframe representing the gross agricultural production for some years. Now scroll down a little bit and you will see the code used to build this beautiful app. See the complete code is in Python. The dataset 'agri.csv.gz' is imported from AWS S3 bucket location like we usually do while importing the data in csv format and rest in few lines the UI logic is written as per Streamlit framework-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzfo_7moGSzJralev3Y3C0lS3X_SMjAhU3XM14sVcJxJdsvhXQMxiB7r00Dj8_aRLtYp7zwqJnKBKR-5m-_ktoQhFmNkhJUq0kflkZ_OiFXtG-5xUmvLy-EEk1fZBQpjorPC4wQ9ZiSeiN/s1600/st9.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="762" data-original-width="1213" height="402" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzfo_7moGSzJralev3Y3C0lS3X_SMjAhU3XM14sVcJxJdsvhXQMxiB7r00Dj8_aRLtYp7zwqJnKBKR-5m-_ktoQhFmNkhJUq0kflkZ_OiFXtG-5xUmvLy-EEk1fZBQpjorPC4wQ9ZiSeiN/s640/st9.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Now I will jump to the actual demo of my app. Here I will show you how had I created an app for my object detection and recognition neural networks model. This YOLOv3 based model I created to detect wine bottles and recognized their names from the image. With Pycharm IDE I have created a project, then I created an empty folder 'model' . There I copied my model weights and configuration files. Next, I created the app.py file where I will write my code of image uploading, image preprocessing, passing the image to the model, and showing results in Streamlit UI.&lt;br /&gt;
&lt;br /&gt;
Here, I show you my project structure in Pycharm IDE-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-VkjyAKlw0t3dT8YTmgmBT2QDJKBtutXRsnshoPItF1sX2-H_JNLjvdxtHV56OacjO9E94_G5UHmTmCQS2uqVKOlXW_uGPCnfpoABSZX0OEtWMg1XPWUV-07PNIfV285KfvJB9A-Y0MAj/s1600/st10.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="555" data-original-width="386" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-VkjyAKlw0t3dT8YTmgmBT2QDJKBtutXRsnshoPItF1sX2-H_JNLjvdxtHV56OacjO9E94_G5UHmTmCQS2uqVKOlXW_uGPCnfpoABSZX0OEtWMg1XPWUV-07PNIfV285KfvJB9A-Y0MAj/s640/st10.JPG" width="444" /&gt;&lt;/a&gt;&lt;/div&gt;
I have saves some sample images inside the images folder. the model folder contains weights and configuration files of my object detection and recognition models I trained on YOLOv3. app.py is my main file which will act as Streamlit application and contains logic to display bounding boxes and brand names in the image. brand_recognition.py is another file which contains logic to recognize the brand names. Based on your machine learning or deep learning model you can maintain this structure as you want. Here the main thing is to see how app.py is using Streamlit related features. Let's understand app.py structure-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3uloVtsQ5YW-EeiRNvZaYVVt5vcGoVXL9j_HG3xiElyyCjvdwn7xzYb-CCEde2wsnMEQceCimk1kdWc5i3GzPW02FWajSDz8VSP1TQHEt-uinUWh8d29g5X2qzGdNxvwUi8_EQsRXuJTP/s1600/st11.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="569" data-original-width="1131" height="321" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3uloVtsQ5YW-EeiRNvZaYVVt5vcGoVXL9j_HG3xiElyyCjvdwn7xzYb-CCEde2wsnMEQceCimk1kdWc5i3GzPW02FWajSDz8VSP1TQHEt-uinUWh8d29g5X2qzGdNxvwUi8_EQsRXuJTP/s640/st11.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
In above code snippet, after importing required libraries I have defined the app layout containing title, header, and a slider. The 'st' object of Streamlit has many useful functions for UI which you can easily check in Pycharm IDE as soon as you type dot(.) after st object like below screenshot-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinDjGy8yy0wjDWBF9lCwJ1izvUkTQIQ7MgC6UkLd5lGhrjyH9jCIGBM7E1XhiiIKsrjnIegOYLy7svV9AbObTtwApXfQYjkV1zNdSwOCZn4e4OYCtGrbQ3RwC8xrJCK2SqD3XWan01S_1m/s1600/st12.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="402" data-original-width="619" height="258" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinDjGy8yy0wjDWBF9lCwJ1izvUkTQIQ7MgC6UkLd5lGhrjyH9jCIGBM7E1XhiiIKsrjnIegOYLy7svV9AbObTtwApXfQYjkV1zNdSwOCZn4e4OYCtGrbQ3RwC8xrJCK2SqD3XWan01S_1m/s400/st12.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
For details inbuilt functions please refer &lt;a href="https://docs.streamlit.io/en/stable/main_concepts.html" target="_blank"&gt;this official link&lt;/a&gt;. Next, I have defined a function to read the image. For this purpose I used OpenCV. Since OpenCV loads the image in BGR format I converted the image in RGB format otherwise your image will show in blue color-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuWojo50cBlo5uLBZvTa9CmcbpYHqvxCPVeLvqC_ptfyzQsi00aGpLg9wYkwPrs7BWtrq-x9yUjC9l7DOBAZd8Kfb_6LBmCckegxRFleC-7OWvuU76Mre3YXEmhmqLQ6HbMUtfIJHIwkNT/s1600/st13.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="165" data-original-width="707" height="148" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuWojo50cBlo5uLBZvTa9CmcbpYHqvxCPVeLvqC_ptfyzQsi00aGpLg9wYkwPrs7BWtrq-x9yUjC9l7DOBAZd8Kfb_6LBmCckegxRFleC-7OWvuU76Mre3YXEmhmqLQ6HbMUtfIJHIwkNT/s640/st13.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Above this function, I used a cache decorator.&amp;nbsp;The Streamlit cache allows our app to execute quickly even when loading data from the web, manipulating large datasets, or performing expensive computations. Based on your need you can add/remove this. After this, I have defined a function that contains YOLOv3 related logic- loading the weights and configuration files, getting the output layer, identifying the best bounding boxes, and recognized the brand names to the detected bottles. Here you can call or write your logic of your ML/DL model-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij58XKb6sV8-lulKKVo6zqxBTx_PGEWzFgKlnaq_tLuGgTManBhm1oSfJ0jr57tdXU8hkfbXPLahhgl6q2C6v7L2c25-QyEIsMzcoO7eAhe9ejPKE4esRygrvhzL4fnnhauJiMnd41uEuI/s1600/st14.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="51" data-original-width="909" height="35" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij58XKb6sV8-lulKKVo6zqxBTx_PGEWzFgKlnaq_tLuGgTManBhm1oSfJ0jr57tdXU8hkfbXPLahhgl6q2C6v7L2c25-QyEIsMzcoO7eAhe9ejPKE4esRygrvhzL4fnnhauJiMnd41uEuI/s640/st14.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Inside the above yolo_v3() function in the last, I used the following Streamlit functions to display my final output image-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRnCCoBpg_VVEEyRAaxWzBZz0CyV63XleuYShMnS71HIZp-rgFLGDtobAIHc9MF8qW8wwwyrbWQFp6ejTJKWQv2bVSkva3nkBzSWftsZTOA8kI1FaB_bpAd1MK7RGNqxVTKYVFHjE1cHGs/s1600/st20.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="270" data-original-width="955" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRnCCoBpg_VVEEyRAaxWzBZz0CyV63XleuYShMnS71HIZp-rgFLGDtobAIHc9MF8qW8wwwyrbWQFp6ejTJKWQv2bVSkva3nkBzSWftsZTOA8kI1FaB_bpAd1MK7RGNqxVTKYVFHjE1cHGs/s640/st20.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
Next, for showing some sample images options in the left sidebar of the app I used st.sidebar.selectbox() function and passed some options there. You can think of this as a dropdown value. In the end, I read the given image using my read_img() function and then called the yolo_v3() function to show the output-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhG8OhqxMaWFhsDrIt9SIeaiYnM9oFM_cW_1l668ZlgW-7VMyYuKwnXpks7cjoqy7N_47ywDdnBBkctd34rmdz1KXLW4BocnfHqoC0N1QGx7U7_XEamud8SxFd1taa9FYCdvs5NUZqU9lD5/s1600/st21.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="300" data-original-width="801" height="239" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhG8OhqxMaWFhsDrIt9SIeaiYnM9oFM_cW_1l668ZlgW-7VMyYuKwnXpks7cjoqy7N_47ywDdnBBkctd34rmdz1KXLW4BocnfHqoC0N1QGx7U7_XEamud8SxFd1taa9FYCdvs5NUZqU9lD5/s640/st21.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
That's it we have our simple app is ready to test. For running the Streamlit server run following command:&amp;nbsp;&lt;b&gt;streamlit run app.py&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1do_VqireA0FZh84zl7EP2O8MSZ4Ef4_cT8ggxzT35XHNlXx2xbPsFGkqDOUP_UW62QIb91Fqj_jmkVulDiAGHCmTWqQVuDl45UHdIos1UcQmucc_d0ZKvnbw4ux-PR_zOJD49nE4eHvI/s1600/st22.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="149" data-original-width="526" height="112" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1do_VqireA0FZh84zl7EP2O8MSZ4Ef4_cT8ggxzT35XHNlXx2xbPsFGkqDOUP_UW62QIb91Fqj_jmkVulDiAGHCmTWqQVuDl45UHdIos1UcQmucc_d0ZKvnbw4ux-PR_zOJD49nE4eHvI/s400/st22.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
Your default browser window open as soon the above command runs and you will see following look like screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFqZFvbft74rfqrmlwA2Dn18cu3JA0C8KC0BbwvxC-c12DIBPnvR_Z-KagzCoKmQXUlNec1Lshbn8XrjlYfh-NWXMeqd5hDh202rs-oZ6YjOTU9PbExM4ig2DOhDXQ88FSClKO_hGw4xbs/s1600/st23.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="941" data-original-width="1263" height="475" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFqZFvbft74rfqrmlwA2Dn18cu3JA0C8KC0BbwvxC-c12DIBPnvR_Z-KagzCoKmQXUlNec1Lshbn8XrjlYfh-NWXMeqd5hDh202rs-oZ6YjOTU9PbExM4ig2DOhDXQ88FSClKO_hGw4xbs/s640/st23.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Looks amazing right! With very few Python-like syntaxes of Streamlit, we have easily created an app for our model. No need of UI or JavaScript knowledge to create an app now. As a Data Scientist with Python knowledge now you can give more focus on your model performance and in less time without any help you can create an app also. That's the power of the Streamlit framework.&lt;br /&gt;
&lt;br /&gt;
Let's move ahead and what if you want to give the user an option of uploading the image instead of static images? For this, we need only a few changes in our app.py file. Using Streamlit's&amp;nbsp;&lt;b&gt;.file_uploader()&lt;/b&gt; function, we can provide image uploading option to the user. We need to make the following changes in our app.py file to achieve this functionality-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3-QzofYXE36ILKY3oqughOTlp-MgwdO_NG37A08cg46ZLsMJNdQtRalQiocGm0pzm1NnON_0rKW1JEcSj08j9IY-DzqyQyDEnElX-ic8arYwystuwb7mGCix2ZL4VcScucCJhnuBkNx8F/s1600/st25.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="127" data-original-width="708" height="112" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3-QzofYXE36ILKY3oqughOTlp-MgwdO_NG37A08cg46ZLsMJNdQtRalQiocGm0pzm1NnON_0rKW1JEcSj08j9IY-DzqyQyDEnElX-ic8arYwystuwb7mGCix2ZL4VcScucCJhnuBkNx8F/s640/st25.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Here I am going to open the uploaded image using PIL's Image module that's why we don't need to again open the image using cv2.imread() function and also we don't need to convert the image in RGB format. Next change is the comment the left sidebar sections and put the image upload option of Streamlit-&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4d7g7Gy4DM9eLDR-V4c8ehO6zLlzjFVYUQpcvbIGWra3nFgvckVpj9ASUJoE8Hohxk_D1Ayf0148C6cFU57AIw-pe3rh2oanJc1gxP_X3C1p614KLp6gPQa9-rg1WyyTNbO3gRVnic1Lv/s1600/st26.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="402" data-original-width="836" height="306" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4d7g7Gy4DM9eLDR-V4c8ehO6zLlzjFVYUQpcvbIGWra3nFgvckVpj9ASUJoE8Hohxk_D1Ayf0148C6cFU57AIw-pe3rh2oanJc1gxP_X3C1p614KLp6gPQa9-rg1WyyTNbO3gRVnic1Lv/s640/st26.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
That's it. Our app is now ready. Restart the Streamlit server and you will see upload image option in the app like below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggcUg9rdbq3zpgYtboWOw-8_CICjtec9TRUwmh1brHSzF9A3L9lKawXaKntsujjPuyP_nAnYbwkapz1XsYugmPYzgFHPLmNJ8pRYlfre_1901y5J7AMoPpMuhvbR4zVFwTS1qzDSVMfWZT/s1600/st27.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="581" data-original-width="1265" height="292" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggcUg9rdbq3zpgYtboWOw-8_CICjtec9TRUwmh1brHSzF9A3L9lKawXaKntsujjPuyP_nAnYbwkapz1XsYugmPYzgFHPLmNJ8pRYlfre_1901y5J7AMoPpMuhvbR4zVFwTS1qzDSVMfWZT/s640/st27.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&amp;nbsp;Click on the browse files link and upload an image to test your model. After uploading an image following look like screen will appear-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfzn5rVNl1rBuGID9WSikRWYJneGdH0eUDTza_lJ-koV_0-1pBLK9NR26cBPMxgMqMdJL78rqUHwFnZKaYshhr7w2s3v6lGF3QciBZrrrZlqGuB5TWvohrd0PQfd7eq1eeuO2qWgGUGDSu/s1600/st24.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="987" data-original-width="1279" height="492" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfzn5rVNl1rBuGID9WSikRWYJneGdH0eUDTza_lJ-koV_0-1pBLK9NR26cBPMxgMqMdJL78rqUHwFnZKaYshhr7w2s3v6lGF3QciBZrrrZlqGuB5TWvohrd0PQfd7eq1eeuO2qWgGUGDSu/s640/st24.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
See, now our app is ready to test any new image user wants to check. With the Streamlit framework, we can now easily create an app with our ML/DL model. In this post, I shared you the basics of this amazing and powerful framework.&amp;nbsp;The easiest way to master in using Streamlit is to try things out yourself. I know you have many ML/DL models to test and after reading this post you are very excited to create many apps for all of your models. So don't stop yourself! Use this post as reference, start creating your app, do more experiments with it's UI, and deploy your app in Heroku.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count, and see you later in my next post.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVmqkuFzOfPa4BmpF1rTx4eOlnTAhCAid1wY_sGHzmlWPWuaDFra6JFTskcCIGFXmiKioO5xLD0uz7mR_OxXHE8XoYSfy1rXFyJVnyje0WMWrGrK_CEYAQKDKHANW1fBlmv6don6u77Gor/s72-c/ys.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">49</thr:total></item><item><title>My solution to HackerEarth's Identify the dance form challenge</title><link>https://dsbyprateekg.blogspot.com/2020/07/my-solution-to-hackerearths-identify.html</link><category>dance form</category><category>HackerEarth</category><category>Hackethon</category><category>Solution</category><category>YOLOv4</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Sun, 5 Jul 2020 21:14:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-4430815257839003458</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLmAp_jvBOmcNEqpNk8vNQxDl9TkTAN1e7ZiQS0jxfrY6EIZxy1pvlRx-wl_cLV5sGaEelam5ZFuQKsbkCRKBHMhgwEb0RM9fTlAodmreo0drSB5iOlHseu339ThtWJVgaNfgEru2q_Uxk/s1600/pred.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="326" data-original-width="358" height="291" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLmAp_jvBOmcNEqpNk8vNQxDl9TkTAN1e7ZiQS0jxfrY6EIZxy1pvlRx-wl_cLV5sGaEelam5ZFuQKsbkCRKBHMhgwEb0RM9fTlAodmreo0drSB5iOlHseu339ThtWJVgaNfgEru2q_Uxk/s320/pred.JPG" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
Another post starts with you beautiful people!&lt;br /&gt;
Today an interesting deep learning challenge is finished in&amp;nbsp;&lt;a href="https://www.hackerearth.com/challenges/competitive/hackerearth-deep-learning-challenge-identify-dance-form/problems/" target="_blank"&gt;HackerEarth&lt;/a&gt;&amp;nbsp;and I got 91.17026 mAP score in the leader board. One drawback I see in HackerEarth is due to small dataset many participants manually prepare the submission files and show 100% score in the leader board. Many aspiring data scientists see this and become nervous. Even with getting score 75+, they become demotivated and leave their experiments in between the challenge. Also the winning approach is not disclosed after the challenge. With this post I will try to motivate my all aspiring data scientists and I will share my solution so that in their next challenge they can easily get 85+ score or even 92+ score :)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Problem statement&lt;/b&gt;&lt;br /&gt;
An event management company organized an evening of Indian classical dance performances to celebrate the rich, eloquent, and elegant art of dance. After the event, the company plans to create a micro site to promote and raise awareness among people about these dance forms. However, identifying them from images is a difficult task.&lt;br /&gt;
&lt;br /&gt;
You are appointed as a Machine Learning Engineer for this project. Your task is to build a deep learning model that can help the company classify these images into eight categories of Indian classical dance-&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Manipuri&lt;/li&gt;
&lt;li&gt;Bharatanatyam&lt;/li&gt;
&lt;li&gt;Odissi&lt;/li&gt;
&lt;li&gt;Kathakali&lt;/li&gt;
&lt;li&gt;Kathak&lt;/li&gt;
&lt;li&gt;Sattriya&lt;/li&gt;
&lt;li&gt;Kuchipudi&lt;/li&gt;
&lt;li&gt;Mohiniyattam&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;b&gt;My Solution&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Due to small size of the dataset, I decided to frame this challenge as object detection and recognition problem and used&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/AlexeyAB/darknet" target="_blank"&gt;AlexeyAB/darknet/YOLOv4&lt;/a&gt;&amp;nbsp;&lt;/b&gt;to train and test the images. If you are new to this framework, please follow my below two posts to get started-&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/05/how-to-install-and-compile-yolo-v4-with.html" target="_blank"&gt;Installing and building Darknet&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/06/identify-eight-types-of-indian.html" target="_blank"&gt;&lt;b&gt;Training dance form dataset with YOLOv4&lt;/b&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
For the dataset preparation, I manually annotated the dataset in required YOLO format. The complete annotation process took 1.5 hours. The complete training of the model took 18 hours in my system. If you don't have GPU enabled system, don't worry! Follow this amazing notebook to install, build and train your custom dataset with YOLOv4 in colab-&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;a href="https://colab.research.google.com/drive/1_GdoqCJWXsChrOiY8sZMr_zbr_fH-0Fg?usp=sharing" target="_blank"&gt;YOLOv4 in Colab&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Once the training is completed, weights files are generated inside the backup folder of the darknet root directory. The file name is&amp;nbsp;yolo-obj_final.weights and it's size is around 250 mb. Now to make prediction on your all test images run the following command-&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;./darknet detector test data/obj.data cfg/yolo-obj.cfg backup/yolo-obj_final.weights -ext_output -dont_show -out data/dance_result.json &amp;lt; data/test.txt&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
The above command will generate a json file. This json file contains the detected object name, it's bounding box coordinates and&amp;nbsp;confidence score. To prepare the final submission file I created a script&amp;nbsp;json_csv_submission2.py. After submitting this submission file I got score 91.17026 in the leaderboard. I have uploaded my solutions files to the github and weight file in the kaggle since we cannot push file larger than 100 mb. You can fork or download the training files from below link-&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;a href="https://github.com/dsbyprateekg/hackerearth-deep-learning-challenge-identify-dance-form" target="_blank"&gt;my github repository&lt;/a&gt;&amp;nbsp;&lt;/b&gt;And you can download the trained weights from&amp;nbsp;&lt;b&gt;&lt;a href="https://www.kaggle.com/iamprateek/yolo4dance-weights" target="_blank"&gt;this link&lt;/a&gt;.&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
In this post all required things are mentioned to solve an image related deep learning problem. Only one thing is expected from all of you is to get started. Until you practice yourself, you will not learn. I am quite sure that in your next image related challenge you will definitely going to use this object detection and recognition technique and spot a better rank. So push yourself, read my mentioned links, practice yourselves and in my next blog post I will share you another interesting learning. Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;/div&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLmAp_jvBOmcNEqpNk8vNQxDl9TkTAN1e7ZiQS0jxfrY6EIZxy1pvlRx-wl_cLV5sGaEelam5ZFuQKsbkCRKBHMhgwEb0RM9fTlAodmreo0drSB5iOlHseu339ThtWJVgaNfgEru2q_Uxk/s72-c/pred.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">9</thr:total></item><item><title>How to deploy your ML model as Fast API?</title><link>https://dsbyprateekg.blogspot.com/2020/06/how-to-deploy-your-ml-model-as-fast-api.html</link><category>Deployment</category><category>FastAPI</category><category>Heroku</category><category>Rest API</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Mon, 29 Jun 2020 20:17:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-1319909914233177137</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSegs4kY50jwifKvzoAcoKY-0EU6QK2ayEwgYfDPszLcxqASCMaNJqQhPE3TbvL-pRQxTwxXBCtKURAGOr_jARClld7I-Gvg2w__HGKUcIYEIMA5lWv4J_CXFlNYBOrD0Gr8PE0-MNQtx2/s1600/f10.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="369" data-original-width="1023" height="115" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSegs4kY50jwifKvzoAcoKY-0EU6QK2ayEwgYfDPszLcxqASCMaNJqQhPE3TbvL-pRQxTwxXBCtKURAGOr_jARClld7I-Gvg2w__HGKUcIYEIMA5lWv4J_CXFlNYBOrD0Gr8PE0-MNQtx2/s320/f10.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
Another post starts with you beautiful people!&lt;br /&gt;
Thank you all for showing so much interests in my last posts about object detection and recognition using YOLOv4. I was very happy to see many aspiring data scientists have learnt from my past three posts about using YOLOv4. Today I am going to share you all a new skill to learn. Most of you have seen my post about&amp;nbsp;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2019/02/how-to-deploy-and-consume-my-base-deep.html" target="_blank"&gt;deploying and consuming ML models as Flask API&lt;/a&gt;&amp;nbsp;&lt;/b&gt;where we have learnt to deploy and consume a keras model with Flask API&amp;nbsp;&lt;b&gt;. &lt;/b&gt;In this post you are going to learn a new framework-&amp;nbsp;&lt;a href="https://fastapi.tiangolo.com/" target="_blank"&gt;&lt;b&gt;FastAPI&lt;/b&gt;&lt;/a&gt; to deploy your model as Rest API. After completing this post you will have a new industry standard skill.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is FastAPI?&lt;/b&gt;&lt;br /&gt;
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It is&amp;nbsp;easy to learn, fast to code and &lt;b&gt;ready for production&lt;/b&gt;. Yes, you heard it right! Flask is not meant to be used in production but with FastAPI you can use your model in production with minimal code. Along with this advantage FastAPI also have inbuilt OpenAPI (previously known as Swagger) support. With this OpenAPI support you don't need to use Postman or any other tool for testing your API. You will have a UI where you can easily test your endpoints.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How to install FastAPI?&lt;/b&gt;&lt;br /&gt;
We can install FastAPI using pip. For this installation open your Anaconda Prompt with admin rights, activate your virtual environment and run the following two commands one by one-&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;&lt;b&gt;pip install fastapi&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;pip install uvicorn&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
Here&amp;nbsp;&lt;b&gt;&lt;a href="http://www.uvicorn.org/" target="_blank"&gt;uvicorn&lt;/a&gt;&amp;nbsp;&lt;/b&gt;is&amp;nbsp;a lightning-fast ASGI server and enables us to use FastAPI for all our asynchronous calls without any worry. Once you installed this framework, you can confirm the installation by following code snippet-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcINRaXFfGD678GkDjfRccD1ZD3oR_Xv17BNzMSSA7pFMXZuT7AM7CjUYcSOwMAjnhWLTmn0m3ChPWhqWdWXpIgCJ8vdAkPn440JYfoIPt4yOhhJPOHhES008dVT74Z1YIis42Dkc3Qu5M/s1600/f1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="397" data-original-width="492" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcINRaXFfGD678GkDjfRccD1ZD3oR_Xv17BNzMSSA7pFMXZuT7AM7CjUYcSOwMAjnhWLTmn0m3ChPWhqWdWXpIgCJ8vdAkPn440JYfoIPt4yOhhJPOHhES008dVT74Z1YIis42Dkc3Qu5M/s1600/f1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
See the similarity with Flask API. Here in the same manner we have initialized the FastAPI as app variable. Save the above file and to run this file use the command:&amp;nbsp;&lt;b&gt;uvicorn main:app --reload &lt;/b&gt;where main is your Python file name and app is the variable we used for the initialization of FastAPI() function. Here --reload parameter is used to take any changes in Python file immediately. After making any changes you will not need to rerun the server. Once your server is up and running you will see following look like message in console-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLwxwH1aCq-3pPE3Ov6E2AnWq1LG2Wn8tbS-CDbT5SsmVVlKQC7abgspTl_gtjmpLumqrQHEi_oFvWwg3pAvfmtitdLpV3JScpttlary7rUuqX3L7Q7_IMXKW3J1KlKg5dmoPIr-ta_T9D/s1600/f2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="257" data-original-width="741" height="138" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLwxwH1aCq-3pPE3Ov6E2AnWq1LG2Wn8tbS-CDbT5SsmVVlKQC7abgspTl_gtjmpLumqrQHEi_oFvWwg3pAvfmtitdLpV3JScpttlary7rUuqX3L7Q7_IMXKW3J1KlKg5dmoPIr-ta_T9D/s400/f2.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Now open the url&amp;nbsp;&amp;nbsp;&lt;b&gt;http://127.0.0.1:8000/items/5?q=somequery&lt;/b&gt; in a browser window. You will see a json response. Now open the url&amp;nbsp;&amp;nbsp;&lt;b&gt;http://127.0.0.1:8000/docs &lt;/b&gt;and you will see screen like below-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfevOfvzJEePBcO_XDZstq-wuubGip32D03xNv1LYK9g1eWoY9YRcQkm-zcqIjRwwoQuW7yT4b5vm-eKHVA2PMi3nLdAOR8MSiUmB5gJtKgecL8ZVoa0Uc8Jr1Y8GEj7rYwRdr7wxaHKT9/s1600/f3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="700" data-original-width="834" height="335" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfevOfvzJEePBcO_XDZstq-wuubGip32D03xNv1LYK9g1eWoY9YRcQkm-zcqIjRwwoQuW7yT4b5vm-eKHVA2PMi3nLdAOR8MSiUmB5gJtKgecL8ZVoa0Uc8Jr1Y8GEj7rYwRdr7wxaHKT9/s400/f3.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
See, this is the OpenAPI UI where you can easily test your API endpoints. Now you are aware of basic of FastAPI, we will move to the next step.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Serving my YOLO model as FastAPI?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
In this post I am going to deploy my YOLO model as FastAPI. If you still have not tried YOLO then you can learn from&amp;nbsp;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/06/identify-eight-types-of-indian.html" target="_blank"&gt;previous post&lt;/a&gt;&amp;nbsp;&lt;/b&gt;and trained your custom model. The API deployment process will be the same for any kind of ML model. Here I am using my custom YOLOv3 models which detects price tags in a given shelf image and then extract the price from those tags. Basically aim of these models are to extract the price of a brand and send in response to front end. So in this example input will be the complete path of an image and response will be extracted price.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Here is my code snippet of main Python file-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYSXj7gNbAUOSu-Ar-XW1Q2JPOn2qEbxPnj06Zg8OBWyMXdC-oq5Axh2NHKHWrPSI0RbNRolPJRR5XGb-Nc3tnzKzySzJS8jtA3ky7Y_W2uI2NSMIQMpNTrcVznMrA0zIEBQVDC49BfkFi/s1600/f4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="506" data-original-width="493" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYSXj7gNbAUOSu-Ar-XW1Q2JPOn2qEbxPnj06Zg8OBWyMXdC-oq5Axh2NHKHWrPSI0RbNRolPJRR5XGb-Nc3tnzKzySzJS8jtA3ky7Y_W2uI2NSMIQMpNTrcVznMrA0zIEBQVDC49BfkFi/s1600/f4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
As you can see code is similar as we saw earlier. Here I have defined my get_price() function as asynchronous using 'async' keyword. Function is taking one input arguement- image which is the complete path of the image. I am reading the image using OpenCV. The HTTP method of this call is 'GET'. The complete logic for price tag detection and recognition is written in another Python file tag_detection.py which I have imported and called the required function. This tag_detection() function will return the extracted prices. You can replace this function with your own classification/regression or NLP model logic.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I have saved this file as price_extraction.py. So I ran following command to run the server:&amp;nbsp;&lt;b&gt;uvicorn price_extraction:app --reload &lt;/b&gt;and after server started I open the OpenAPI UI with command&amp;nbsp;&lt;b&gt;http://127.0.0.1:8000/docs-&lt;/b&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLY6gxxWyE07NKoTKGF6uA1Rm-xe5qLKMabvEjzplyNLeWFQnajW4ISkAlmjrS2srEgl778A4j80-mKxftc3KPmyXxr44-6GR27OjLekDqae2DMsuUua1hoRQ9aybX9B7g10YkSbSfaf16/s1600/f5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="696" data-original-width="713" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLY6gxxWyE07NKoTKGF6uA1Rm-xe5qLKMabvEjzplyNLeWFQnajW4ISkAlmjrS2srEgl778A4j80-mKxftc3KPmyXxr44-6GR27OjLekDqae2DMsuUua1hoRQ9aybX9B7g10YkSbSfaf16/s1600/f5.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
Now to test the endpoint click on the GET or your HTTP method button. In my case it is looking like below-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV_U3A9KBsh7OCvyN5ZwkcovxZoRY4a48EPZW6Nw8uSwtquChfvBX0OzWdiPA3gKIfxggiinY41gi8FNJ_JhVwoGtsR1xezcXEwK1yEwVersOpmnbGKghPuZQE2zydi4iaMUsh9iAQdx6x/s1600/f6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="528" data-original-width="1600" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV_U3A9KBsh7OCvyN5ZwkcovxZoRY4a48EPZW6Nw8uSwtquChfvBX0OzWdiPA3gKIfxggiinY41gi8FNJ_JhVwoGtsR1xezcXEwK1yEwVersOpmnbGKghPuZQE2zydi4iaMUsh9iAQdx6x/s1600/f6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
Once you expand your HTTP method, you need to click on Try it out button to enable the input field. Based on your ML model input fields may be more than one. In my case it is one. After clicking on this button, enter the values of the input field and click on the &lt;b&gt;Execute&lt;/b&gt; button. This button will trigger your endpoint and return the response.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinf9AwNJnJ2qW_AQIfXlMrz_cAyMg2jG4KlUD6MOg7HPsac3xMQwDi_yuD2zCO3axtYFXQgrXVV3z7d6fVc-X8AXFP3oTR22rP8ENBnI8Ni9PSPhOBhkvfd88aum4FOZRkqwn8vpo9Kq-9/s1600/f7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="677" data-original-width="1177" height="368" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinf9AwNJnJ2qW_AQIfXlMrz_cAyMg2jG4KlUD6MOg7HPsac3xMQwDi_yuD2zCO3axtYFXQgrXVV3z7d6fVc-X8AXFP3oTR22rP8ENBnI8Ni9PSPhOBhkvfd88aum4FOZRkqwn8vpo9Kq-9/s640/f7.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
In my case response screen looks like below-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_RjpSNf64mRT7vbfM9Xm6mwPS-ERHmAnn-oXErByI26_9JtLra6UD8gSEFon_4KtqJyttV3YbNtermkHwkhuF4qOCEnpivXtjqAaBKGgdlIzz5YoLj4_MuPQ88MpBqYoV5Bqm_zhqFJA8/s1600/f9.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="605" data-original-width="763" height="505" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_RjpSNf64mRT7vbfM9Xm6mwPS-ERHmAnn-oXErByI26_9JtLra6UD8gSEFon_4KtqJyttV3YbNtermkHwkhuF4qOCEnpivXtjqAaBKGgdlIzz5YoLj4_MuPQ88MpBqYoV5Bqm_zhqFJA8/s640/f9.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div&gt;
And my input image was this-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbthxCxcz5ey3RAWkJ1kvYFNrHiiIV1lDGz2sCS4UReP60QODd-Wtr7ejQluipFgI1W1eqqh2WZnQDSUXR_5RtlNBXU4h4y7NCCGAIFhpgIfijZfG9OIbO-O1ido9AecoIdl9tw8OucYRC/s1600/test2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="1600" data-original-width="1200" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbthxCxcz5ey3RAWkJ1kvYFNrHiiIV1lDGz2sCS4UReP60QODd-Wtr7ejQluipFgI1W1eqqh2WZnQDSUXR_5RtlNBXU4h4y7NCCGAIFhpgIfijZfG9OIbO-O1ido9AecoIdl9tw8OucYRC/s320/test2.jpg" width="240" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Quite interesting right! With minimal code we are able to deploy and consume our model as Rest API. We also don't need to use any external tool for testing the endpoints. With OpenAPI inbuilt support no wonder companies like Microsoft, Netflix and Uber are using the FastAPI in production. Now you don't need to think too much; just take your existing ML model, wrap it in FastAPI code you learned today from this post and start experimenting with it because we learn by doing experiments in Data Science.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Once you are done with this go ahead and deploy your FastAPI in &lt;b&gt;Heroku &lt;/b&gt;by following &lt;b&gt;&lt;a href="https://www.tutlinks.com/create-and-deploy-fastapi-app-to-heroku/" target="_blank"&gt;this link&lt;/a&gt;&amp;nbsp;&lt;/b&gt;to showcase your model with others. In my next post I will share you my new learning till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSegs4kY50jwifKvzoAcoKY-0EU6QK2ayEwgYfDPszLcxqASCMaNJqQhPE3TbvL-pRQxTwxXBCtKURAGOr_jARClld7I-Gvg2w__HGKUcIYEIMA5lWv4J_CXFlNYBOrD0Gr8PE0-MNQtx2/s72-c/f10.png" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">50</thr:total></item><item><title>How to convert your YOLOv4 weights to TensorFlow 2.2.0?</title><link>https://dsbyprateekg.blogspot.com/2020/06/how-to-convert-your-yolov4-weights-to.html</link><category>keras 2.3.0</category><category>tensorflow 2.2.0</category><category>YOLOv4</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Sat, 6 Jun 2020 11:02:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-887692847062399617</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7QddfX-AGbAo_mjONyqjj-b_rLpFq_l9cVl3yPWTGtYcJDS68kzvs5NtE02VLMIIx5-x-ix9Pu6_zgwj30u3uiErJAG0AhkVct9kfT8Lx4kVvL7JdPpU_otYGjD4QcAcDUD4vYXLqI3to/s1600/tf1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="212" data-original-width="542" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7QddfX-AGbAo_mjONyqjj-b_rLpFq_l9cVl3yPWTGtYcJDS68kzvs5NtE02VLMIIx5-x-ix9Pu6_zgwj30u3uiErJAG0AhkVct9kfT8Lx4kVvL7JdPpU_otYGjD4QcAcDUD4vYXLqI3to/s1600/tf1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Another post starts with you beautiful people!&lt;br /&gt;
Thank you all for your overwhelming response in my last two posts about the YOLOv4. It is quite clear that my beloved aspiring data scientists are very much curious to learn state of the art computer vision technique but they were not able to achieve that due to the lack of proper guidance. Now they have learnt exact steps to use a state of the art object detection and recognition technique from my last two posts. If you are new to my blog and want to use YOLOv4 in your project then please follow below two links-&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/05/how-to-install-and-compile-yolo-v4-with.html" target="_blank"&gt;&lt;b&gt;How to install and compile Darknet code with GPU?&lt;/b&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/06/identify-eight-types-of-indian.html" target="_blank"&gt;How to train your custom data with YOLOv4?&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
In my&amp;nbsp;&lt;a href="http://dsbyprateekg.blogspot.com/2020/06/identify-eight-types-of-indian.html" target="_blank"&gt;last post&lt;/a&gt; we have trained our custom dataset to identify eight types of Indian classical dance forms. After the model training we have got the YOLOv4 specific weights file as 'yolo-obj_final.weights'. This YOLOv4 specific weight file cannot be used directly to either with OpenCV or with TensorFlow currently because in the latest release of YOLO a new activation function-&lt;a href="https://arxiv.org/ftp/arxiv/papers/1908/1908.08681.pdf" target="_blank"&gt;&lt;b&gt;Mish&lt;/b&gt;&lt;/a&gt; is introduced. With current version of OpenCV and TF using YOLOv4 directly is not possible. In this blog post I am going to share you my jupyter notebook code for converting YOLOv4 weights to TF2.2.0.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
TensorFlow(TF) is an easy to use framework in production. If we have a TF model we can deploy it in a web application, mobile application or embedded devices. Since I am a Keras guy, I prefer to use Keras API with TensorFlow backend in my projects :) . I have taken the help from the older post of&amp;nbsp;&lt;b&gt;&lt;a href="https://machinelearningmastery.com/how-to-perform-object-detection-with-yolov3-in-keras/" target="_blank"&gt;Jason Brownlee PhD&lt;/a&gt;&amp;nbsp;&lt;/b&gt;and modified the code with YOLOv4 related changes. You can fork or download the notebook from&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/dsbyprateekg/YOLOv4" target="_blank"&gt;my github repository&lt;/a&gt;&amp;nbsp; &lt;/b&gt;You can gone through the notebook and if you want to update or add any thing to it, send me a PR in github :)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In the first cell of the notebook I have imported required libraries and also checking the TF and Keras version-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixHHAe4fOtf_SrOC72ZwUMej6O1HVyvc6E2cpoYmh0td7rgGA7_opM3lhmWJFhIbDsWH9Y5fpqrWuAKW1-l8CxGF4Ac6wcr_ppZa7oHtPiT5jS6DIfOr8hYq_DsuW3qmRjtMtXT7y-uXYK/s1600/lib.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="499" data-original-width="827" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixHHAe4fOtf_SrOC72ZwUMej6O1HVyvc6E2cpoYmh0td7rgGA7_opM3lhmWJFhIbDsWH9Y5fpqrWuAKW1-l8CxGF4Ac6wcr_ppZa7oHtPiT5jS6DIfOr8hYq_DsuW3qmRjtMtXT7y-uXYK/s1600/lib.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
So we have the latest keras and TF version currently available in colab notebook. In next cell, I have defined the newly added activation function Mish as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgf94gNpUMuAxDbl7tc9zQnwcaKoV_JP51P4EB7z3xzK88EWBwScoPWSHynqb8nU9mc_KzNoStZpbvyKBdejKRAJV3c2rUEDApMjmAZNfj6cK4ZCpc8JDwwhaMxm-fkBoMEu-LG3Vuyo3OL/s1600/mish.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="604" data-original-width="552" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgf94gNpUMuAxDbl7tc9zQnwcaKoV_JP51P4EB7z3xzK88EWBwScoPWSHynqb8nU9mc_KzNoStZpbvyKBdejKRAJV3c2rUEDApMjmAZNfj6cK4ZCpc8JDwwhaMxm-fkBoMEu-LG3Vuyo3OL/s1600/mish.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
And in third cell, this activation function is added to the convolution layer blocks-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHDMxhP_S5VvR5FLi_1xN_I6zRMXauVrhJ967DbRMjLWH7Ikxf4FBxuqDrLpKtM6iDP9nU1zJP8Blh88kiJ2engOKnZ5Kx1dpYgKTqzZ0_rm2ExuUpyekSugj5t745e9FgtNSVAu0sTQOQ/s1600/conv.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="562" data-original-width="1043" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHDMxhP_S5VvR5FLi_1xN_I6zRMXauVrhJ967DbRMjLWH7Ikxf4FBxuqDrLpKtM6iDP9nU1zJP8Blh88kiJ2engOKnZ5Kx1dpYgKTqzZ0_rm2ExuUpyekSugj5t745e9FgtNSVAu0sTQOQ/s1600/conv.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
In further cells, I have defined the YOLOv4 architecture as per keras &amp;amp; TF and then other helper functions like drawing bounding boxes, removing unwanted bounding boxes and showing the detecting in an image. Let me show you the screenshots where I have converted a pretrained YOLOv4&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights" target="_blank"&gt;weights&lt;/a&gt;&amp;nbsp;&lt;/b&gt;to a TF model file-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfRkGCK1ngMVgawvCy7IbyeMnBuiaMn0TRFHpEz-bE4FR2lRDkKshaok0wqAhGwlhWA9u9CjOIPjPYl7wapm4YSCngO0o7x9AaGZOk8JDHf7rtmQ-S4rPiXtdGzSQTEGLtPdcDzJBUBEPq/s1600/df1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="62" data-original-width="271" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfRkGCK1ngMVgawvCy7IbyeMnBuiaMn0TRFHpEz-bE4FR2lRDkKshaok0wqAhGwlhWA9u9CjOIPjPYl7wapm4YSCngO0o7x9AaGZOk8JDHf7rtmQ-S4rPiXtdGzSQTEGLtPdcDzJBUBEPq/s1600/df1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNAI4Lsjys1E4swwzNUSV-uifqBOq_kuXTU-igrzGNyE7AMdp7tGaSQA7XKaLK6mFR1FUO6EUrVsCAfDxEaVZsIiaEgGsnzAYd0S09ejtK4g_ZOedxcMNGaWRfPL-P0xhNSNO1MZTZnXdc/s1600/df2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="82" data-original-width="458" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNAI4Lsjys1E4swwzNUSV-uifqBOq_kuXTU-igrzGNyE7AMdp7tGaSQA7XKaLK6mFR1FUO6EUrVsCAfDxEaVZsIiaEgGsnzAYd0S09ejtK4g_ZOedxcMNGaWRfPL-P0xhNSNO1MZTZnXdc/s1600/df2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqclGDbT7OTX2WNPtmBuirpymI9Oe_nkv6iyc5Mc3GA9pnGmX3u8oT2BwZsns5kTAWIpsExeyaXatr2m6i8_OpcXMR7OpiSQoFBoi7A5C5_MiQVsVNsLBbJuvOxVVNI616VkXD8sDY4k6w/s1600/df3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="123" data-original-width="298" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqclGDbT7OTX2WNPtmBuirpymI9Oe_nkv6iyc5Mc3GA9pnGmX3u8oT2BwZsns5kTAWIpsExeyaXatr2m6i8_OpcXMR7OpiSQoFBoi7A5C5_MiQVsVNsLBbJuvOxVVNI616VkXD8sDY4k6w/s1600/df3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
After running above cells you will have a .h5 format file which is infact a TensorFlow model file. Now we will verify that our converted model file is working same as YOLOv4 weights or not. To do so let's import this TensorFlow model file-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaoQqulxE7tdw9spTMM3NjNTdnjiUbnimcR4UcpUtbAve9c-CCXPNbwKBNCQWsioE5kA4y2WFSsVslkqmQq3L8XYxMe7rJb60hW-iUHOnaLrDC0VM7AubcpxLMuHWp1Mp1umx5ubJZRtsw/s1600/df4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="101" data-original-width="418" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaoQqulxE7tdw9spTMM3NjNTdnjiUbnimcR4UcpUtbAve9c-CCXPNbwKBNCQWsioE5kA4y2WFSsVslkqmQq3L8XYxMe7rJb60hW-iUHOnaLrDC0VM7AubcpxLMuHWp1Mp1umx5ubJZRtsw/s1600/df4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
With this code our .h5 file is successfully loaded as keras model. Let's define a list of objects on which original YOLOv4 weights was trained and also a function to read those object labels-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimsWJDr8fslx5NThJVRZ-LGVb4fdBxViS2nrTciw8YFdQFq7dK_WbJ2PqE55AvKB_w7jiz_45Ycq38ppoqbM0za9RYOwaXH_MQouQW02Gsgz-UPBO8bfU7Jt-jPWqy0ZIqEUnbf7mIzK5R/s1600/df5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="360" data-original-width="1037" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimsWJDr8fslx5NThJVRZ-LGVb4fdBxViS2nrTciw8YFdQFq7dK_WbJ2PqE55AvKB_w7jiz_45Ycq38ppoqbM0za9RYOwaXH_MQouQW02Gsgz-UPBO8bfU7Jt-jPWqy0ZIqEUnbf7mIzK5R/s1600/df5.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Next step is to define a function to load and preprocess an image for keras-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDc2FRMM3I5epiKdIOy_Nz7sFikZANJHuMUEyJKoO0m4VKUBFHYNLtiaYI30IMLRjYCPY6pCzHfjDTUfZsBiBt6dIX1lcWglapUyYvbwCGOE_HETbPc8G5Ekp-S86XhuWz78eblA4fVMMC/s1600/df6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="459" data-original-width="755" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDc2FRMM3I5epiKdIOy_Nz7sFikZANJHuMUEyJKoO0m4VKUBFHYNLtiaYI30IMLRjYCPY6pCzHfjDTUfZsBiBt6dIX1lcWglapUyYvbwCGOE_HETbPc8G5Ekp-S86XhuWz78eblA4fVMMC/s1600/df6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Let's apply this preprocessing function to an image in this way-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcw-hDrcteSrpgpVg5rqnGLGSb3d74VpjnHssjYrkAGilkHDYmsoRKegOa6XzXwZHgmN3pYVvorALTtxs10KHEXJh8VCVdBIOpXcbUCm7fcogwKq9LScytb0hHCqyPba77EZ3bi16W1U3k/s1600/df7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="204" data-original-width="770" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcw-hDrcteSrpgpVg5rqnGLGSb3d74VpjnHssjYrkAGilkHDYmsoRKegOa6XzXwZHgmN3pYVvorALTtxs10KHEXJh8VCVdBIOpXcbUCm7fcogwKq9LScytb0hHCqyPba77EZ3bi16W1U3k/s1600/df7.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Next, we will use the predict() function to make the prediction on our image-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9l2JUXhohyphenhyphenOPnPNuVyziS9ZKVCkx6lyQ0Vo2nYPXufnC8TktE5w5IaGhv4saIYFyG72gwiAzTf04DHBbUR3ykN2Q6X-PtBjdfKnTyDZLNgEzW870lNGOCcSECRdGUqvnnbpMBAIIXXP7f/s1600/df8.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="117" data-original-width="610" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9l2JUXhohyphenhyphenOPnPNuVyziS9ZKVCkx6lyQ0Vo2nYPXufnC8TktE5w5IaGhv4saIYFyG72gwiAzTf04DHBbUR3ykN2Q6X-PtBjdfKnTyDZLNgEzW870lNGOCcSECRdGUqvnnbpMBAIIXXP7f/s1600/df8.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
As a last step we will draw the bounding boxes and predicted object on that bounding boxes in our picture. All required processing for this step is already done in the notebook.Once we run the following cell-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMcPqlMVy1s2VqLhN1S3hyphenhypheniKpf2Wk66rOirJnxU43DP3EySGs0e2DM0oRaVooCEUn_zY3UGvdvvVcOMwjrZcrMLbi2eszNyhxEWSWuSObpK_sqyePbpc0XfTsOPurOY388bLCQVHMvSYZP/s1600/df9.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="102" data-original-width="1017" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMcPqlMVy1s2VqLhN1S3hyphenhypheniKpf2Wk66rOirJnxU43DP3EySGs0e2DM0oRaVooCEUn_zY3UGvdvvVcOMwjrZcrMLbi2eszNyhxEWSWuSObpK_sqyePbpc0XfTsOPurOY388bLCQVHMvSYZP/s1600/df9.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Along with predicted object with accuracy, it will give you following like image-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMeKIiCBpXoTP6L5OQms8AqlHCXDyQCvafngjL8MyqgbiKuAYRoFsdSm0kPjNMCVSwYWecRY-5FBk8_VcErN5cNOgVqfE6f4tYW24DD3OMm3gTjo8d9bkHk4Lr_jsolxY8amWd31jgvq-_/s1600/df10.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="266" data-original-width="509" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMeKIiCBpXoTP6L5OQms8AqlHCXDyQCvafngjL8MyqgbiKuAYRoFsdSm0kPjNMCVSwYWecRY-5FBk8_VcErN5cNOgVqfE6f4tYW24DD3OMm3gTjo8d9bkHk4Lr_jsolxY8amWd31jgvq-_/s1600/df10.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
The above image and result confirms that our conversion code is working as expected. That's it for today guys! Go ahead- Fork or download the complete notebook from&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/dsbyprateekg/YOLOv4" target="_blank"&gt;my github repo&lt;/a&gt;, &lt;/b&gt;try it yourself, explore it with your custom YOLOv4 weights file and see how can you make this notebook more useful. In my next blog post I will share my new learning with you all. Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7QddfX-AGbAo_mjONyqjj-b_rLpFq_l9cVl3yPWTGtYcJDS68kzvs5NtE02VLMIIx5-x-ix9Pu6_zgwj30u3uiErJAG0AhkVct9kfT8Lx4kVvL7JdPpU_otYGjD4QcAcDUD4vYXLqI3to/s72-c/tf1.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">92</thr:total><enclosure length="257717640" type="application/octet-stream" url="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights"/><itunes:explicit>no</itunes:explicit><itunes:subtitle>Another post starts with you beautiful people! Thank you all for your overwhelming response in my last two posts about the YOLOv4. It is quite clear that my beloved aspiring data scientists are very much curious to learn state of the art computer vision technique but they were not able to achieve that due to the lack of proper guidance. Now they have learnt exact steps to use a state of the art object detection and recognition technique from my last two posts. If you are new to my blog and want to use YOLOv4 in your project then please follow below two links- How to install and compile Darknet code with GPU? How to train your custom data with YOLOv4? In my&amp;nbsp;last post we have trained our custom dataset to identify eight types of Indian classical dance forms. After the model training we have got the YOLOv4 specific weights file as 'yolo-obj_final.weights'. This YOLOv4 specific weight file cannot be used directly to either with OpenCV or with TensorFlow currently because in the latest release of YOLO a new activation function-Mish is introduced. With current version of OpenCV and TF using YOLOv4 directly is not possible. In this blog post I am going to share you my jupyter notebook code for converting YOLOv4 weights to TF2.2.0. TensorFlow(TF) is an easy to use framework in production. If we have a TF model we can deploy it in a web application, mobile application or embedded devices. Since I am a Keras guy, I prefer to use Keras API with TensorFlow backend in my projects :) . I have taken the help from the older post of&amp;nbsp;Jason Brownlee PhD&amp;nbsp;and modified the code with YOLOv4 related changes. You can fork or download the notebook from&amp;nbsp;my github repository&amp;nbsp; You can gone through the notebook and if you want to update or add any thing to it, send me a PR in github :) In the first cell of the notebook I have imported required libraries and also checking the TF and Keras version- So we have the latest keras and TF version currently available in colab notebook. In next cell, I have defined the newly added activation function Mish as below- And in third cell, this activation function is added to the convolution layer blocks- In further cells, I have defined the YOLOv4 architecture as per keras &amp;amp; TF and then other helper functions like drawing bounding boxes, removing unwanted bounding boxes and showing the detecting in an image. Let me show you the screenshots where I have converted a pretrained YOLOv4&amp;nbsp;weights&amp;nbsp;to a TF model file- After running above cells you will have a .h5 format file which is infact a TensorFlow model file. Now we will verify that our converted model file is working same as YOLOv4 weights or not. To do so let's import this TensorFlow model file- With this code our .h5 file is successfully loaded as keras model. Let's define a list of objects on which original YOLOv4 weights was trained and also a function to read those object labels- Next step is to define a function to load and preprocess an image for keras- Let's apply this preprocessing function to an image in this way- Next, we will use the predict() function to make the prediction on our image- As a last step we will draw the bounding boxes and predicted object on that bounding boxes in our picture. All required processing for this step is already done in the notebook.Once we run the following cell- Along with predicted object with accuracy, it will give you following like image- The above image and result confirms that our conversion code is working as expected. That's it for today guys! Go ahead- Fork or download the complete notebook from&amp;nbsp;my github repo, try it yourself, explore it with your custom YOLOv4 weights file and see how can you make this notebook more useful. In my next blog post I will share my new learning with you all. Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.</itunes:subtitle><itunes:author>noreply@blogger.com (Learn Data Science with PrateekG)</itunes:author><itunes:summary>Another post starts with you beautiful people! Thank you all for your overwhelming response in my last two posts about the YOLOv4. It is quite clear that my beloved aspiring data scientists are very much curious to learn state of the art computer vision technique but they were not able to achieve that due to the lack of proper guidance. Now they have learnt exact steps to use a state of the art object detection and recognition technique from my last two posts. If you are new to my blog and want to use YOLOv4 in your project then please follow below two links- How to install and compile Darknet code with GPU? How to train your custom data with YOLOv4? In my&amp;nbsp;last post we have trained our custom dataset to identify eight types of Indian classical dance forms. After the model training we have got the YOLOv4 specific weights file as 'yolo-obj_final.weights'. This YOLOv4 specific weight file cannot be used directly to either with OpenCV or with TensorFlow currently because in the latest release of YOLO a new activation function-Mish is introduced. With current version of OpenCV and TF using YOLOv4 directly is not possible. In this blog post I am going to share you my jupyter notebook code for converting YOLOv4 weights to TF2.2.0. TensorFlow(TF) is an easy to use framework in production. If we have a TF model we can deploy it in a web application, mobile application or embedded devices. Since I am a Keras guy, I prefer to use Keras API with TensorFlow backend in my projects :) . I have taken the help from the older post of&amp;nbsp;Jason Brownlee PhD&amp;nbsp;and modified the code with YOLOv4 related changes. You can fork or download the notebook from&amp;nbsp;my github repository&amp;nbsp; You can gone through the notebook and if you want to update or add any thing to it, send me a PR in github :) In the first cell of the notebook I have imported required libraries and also checking the TF and Keras version- So we have the latest keras and TF version currently available in colab notebook. In next cell, I have defined the newly added activation function Mish as below- And in third cell, this activation function is added to the convolution layer blocks- In further cells, I have defined the YOLOv4 architecture as per keras &amp;amp; TF and then other helper functions like drawing bounding boxes, removing unwanted bounding boxes and showing the detecting in an image. Let me show you the screenshots where I have converted a pretrained YOLOv4&amp;nbsp;weights&amp;nbsp;to a TF model file- After running above cells you will have a .h5 format file which is infact a TensorFlow model file. Now we will verify that our converted model file is working same as YOLOv4 weights or not. To do so let's import this TensorFlow model file- With this code our .h5 file is successfully loaded as keras model. Let's define a list of objects on which original YOLOv4 weights was trained and also a function to read those object labels- Next step is to define a function to load and preprocess an image for keras- Let's apply this preprocessing function to an image in this way- Next, we will use the predict() function to make the prediction on our image- As a last step we will draw the bounding boxes and predicted object on that bounding boxes in our picture. All required processing for this step is already done in the notebook.Once we run the following cell- Along with predicted object with accuracy, it will give you following like image- The above image and result confirms that our conversion code is working as expected. That's it for today guys! Go ahead- Fork or download the complete notebook from&amp;nbsp;my github repo, try it yourself, explore it with your custom YOLOv4 weights file and see how can you make this notebook more useful. In my next blog post I will share my new learning with you all. Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.</itunes:summary><itunes:keywords>keras 2.3.0, tensorflow 2.2.0, YOLOv4</itunes:keywords></item><item><title>Identify Eight types of Indian Classical Dance forms with YOLOv4</title><link>https://dsbyprateekg.blogspot.com/2020/06/identify-eight-types-of-indian.html</link><category>custom dataset</category><category>labellmg</category><category>training</category><category>YOLOv4</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Mon, 1 Jun 2020 18:30:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-2346379177450991945</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM-0ZkQxeBM-s17_VNtdR7VFHlxSt9whxAo8ArwU8iJff7toZDTl8aB7-gO9Yj0iHOMjc52GgoYjSDqbLdgC-8e7JEgMhhRyRBo0hrz3Pco5xjPbednDWfQyZ9yEgifpNLEgbT4maIJYx8/s1600/dance.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="442" data-original-width="614" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM-0ZkQxeBM-s17_VNtdR7VFHlxSt9whxAo8ArwU8iJff7toZDTl8aB7-gO9Yj0iHOMjc52GgoYjSDqbLdgC-8e7JEgMhhRyRBo0hrz3Pco5xjPbednDWfQyZ9yEgifpNLEgbT4maIJYx8/s1600/dance.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Another post starts with you beautiful people!&lt;br /&gt;
Thank you all who had followed my last post about&amp;nbsp;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2020/05/how-to-install-and-compile-yolo-v4-with.html" target="_blank"&gt;install and compile YOLOv4 in Windows10&lt;/a&gt;&amp;nbsp;&lt;/b&gt;and could able to successfully set up the Darknet in their machines. As I promised in last post and you asked for, in this post I am going to share you the steps required for training a custom object with YOLOv4. If you are seeing my blog first time, I recommend you to first follow my&amp;nbsp;&lt;a href="http://dsbyprateekg.blogspot.com/2020/05/how-to-install-and-compile-yolo-v4-with.html" target="_blank"&gt;last post&lt;/a&gt;&amp;nbsp;and then proceed further.&lt;br /&gt;
For this exercise I have choosen a dataset of eight Indian Classical Dance forms-&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;&lt;b&gt;Manipuri&lt;/b&gt; from Manipur&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Bharatanatyam&lt;/b&gt; from Tamil Nadu&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Odissi&lt;/b&gt; from Orissa&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Kathakali&lt;/b&gt; from Kerala&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Kathak&lt;/b&gt; from Uttar Pradesh&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Sattriya&lt;/b&gt; from Assam&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Kuchipudi&lt;/b&gt; from Andhra Pradesh&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Mohiniyattam&lt;/b&gt; from Kerala&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
You can download the dataset from &lt;a href="https://www.hackerearth.com/challenges/competitive/hackerearth-deep-learning-challenge-identify-dance-form/problems/" target="_blank"&gt;this hackethon link&lt;/a&gt;. After downloading the dataset , you need to create 8 folders with class name and copy respective images from train folder to there. For this work I have written a simple Python script that creates eight folders with the images of dance forms. So you can also do the same. After completing this post you will be well aware of building a Neural Network model to classify eight categories of Indian classical dance. Our first step is to label the images in YOLO format. YOLO requires a specific type of labeling. The labelled format of an image should be in&amp;nbsp;&lt;b&gt;&amp;lt;object-class&amp;gt; &amp;lt;x_center&amp;gt; &amp;lt;y_center&amp;gt; &amp;lt;width&amp;gt; &amp;lt;height&amp;gt; &lt;/b&gt;format. Here&amp;nbsp;&lt;b&gt;&amp;lt;object-class&amp;gt;&lt;/b&gt;&amp;nbsp;is integer object number from 0 to (classes-1). In our case it will be zero to seven. &lt;b&gt;&amp;lt;x_center&amp;gt; &amp;lt;y_center&amp;gt; &amp;lt;width&amp;gt; &amp;lt;height&amp;gt;&lt;/b&gt;&amp;nbsp;are float values relative to width and height of image which can be equal from [0.0 to 1.0]. This steps is the backbone of the YOLO and so it must be performed with precisely.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
To label the image we will use &lt;b&gt;labellmg&lt;/b&gt; annotation tool. Clone or Download the github code from&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/tzutalin/labelImg" target="_blank"&gt;this link&lt;/a&gt;&amp;nbsp;&lt;/b&gt;in your machine. Once cloned/downloaded, open anaconda prompt with admin rights, go to the path of labellmg and install required &lt;b&gt;pyqt &lt;/b&gt;library using command:&amp;nbsp;&lt;b&gt;conda install pyqt=5&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
After installing pyqt, run following command:&amp;nbsp;&lt;b&gt;pyrcc5 -o libs/resources.py resources.qrc&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
This will install annotation tool in your machine.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Now, create a text file named as &lt;b&gt;classes.txt &lt;/b&gt;in labellmg root directory and put our target class names(dance forms) in this format-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuJlZPI-GBUHLDhjwNkWiTpesP-DFdLupamFIlOw96NiZFL3_jbbhTObmKSxgZB34pFaDBT9oNoqptWtVWBAVVK8CLElUaGkwFEPmgy2E36OTxdzUP5Pesx_dUiDfZJYal9kmDrb9NOIhI/s1600/a1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="259" data-original-width="219" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuJlZPI-GBUHLDhjwNkWiTpesP-DFdLupamFIlOw96NiZFL3_jbbhTObmKSxgZB34pFaDBT9oNoqptWtVWBAVVK8CLElUaGkwFEPmgy2E36OTxdzUP5Pesx_dUiDfZJYal9kmDrb9NOIhI/s1600/a1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Now our labellmg setup is done. We will start annotating the images later. first we will prepare some files and make some changes in our YOLO installed directory for the training of our custom dataset. For this step go to your YOLO installed directory(\vcpkg-master\installed\x64-windows\tools\darknet\cfg) and follow below steps-&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;1.&lt;/b&gt; Copy the&amp;nbsp;&lt;b&gt;yolov4-custom.cfg &lt;/b&gt;file, paste there and rename it to&amp;nbsp;&lt;b&gt;yolo-obj.cfg&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;2. &lt;/b&gt;Open&amp;nbsp;yolo-obj.cfg file in notepad++ and make below changes-&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;change line &lt;b&gt;batch&lt;/b&gt; to batch=&lt;b&gt;64&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;change line &lt;b&gt;subdivisions&lt;/b&gt; to subdivisions=&lt;b&gt;64&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;change line &lt;b&gt;max_batches&lt;/b&gt; to &lt;b&gt;16000&lt;/b&gt;(classes*2000)&lt;/li&gt;
&lt;li&gt;change line &lt;b&gt;steps&lt;/b&gt; to 80% and 90% of max_batches, f.e.&lt;b&gt;12800&lt;/b&gt;, &lt;b&gt;14400&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;set network size &lt;b&gt;width&lt;/b&gt;=&lt;b&gt;416&lt;/b&gt; &lt;b&gt;height&lt;/b&gt;=&lt;b&gt;416&lt;/b&gt; or any value multiple of 32&lt;/li&gt;
&lt;li&gt;change line &lt;b&gt;classes&lt;/b&gt;=&lt;b&gt;8&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;change &lt;b&gt;[filters=255]&lt;/b&gt; to &lt;b&gt;filters=39&lt;/b&gt;(classes + 5)x3 in the &lt;b&gt;3&lt;/b&gt; [convolutional] &lt;b&gt;before&lt;/b&gt; each [yolo] layer&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;3. &lt;/b&gt;Go to&amp;nbsp;\vcpkg-master\installed\x64-windows\tools\darknet\&lt;b&gt;data&lt;/b&gt; directory and&lt;b&gt;&amp;nbsp;&lt;/b&gt;create a file &lt;b&gt;obj.names. &lt;/b&gt;Copy the content of the classes.txt file there&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;4.&amp;nbsp;&lt;/b&gt;Create a file &lt;b&gt;obj.data &lt;/b&gt;in the same directory with below content-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSjFACaA8JAf25en6RuW2CdP8ujCMvzlDPZiqi5X7d8rCICkAKFybV6hVhVc528KWhRXr48XcVHH0V2h5OIyH9mOYpBXh-JInthy0PxX1wZzXSOAz3WcoRbhiOzzf_9i9lt2ufNGcg5uqS/s1600/a1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="128" data-original-width="880" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSjFACaA8JAf25en6RuW2CdP8ujCMvzlDPZiqi5X7d8rCICkAKFybV6hVhVc528KWhRXr48XcVHH0V2h5OIyH9mOYpBXh-JInthy0PxX1wZzXSOAz3WcoRbhiOzzf_9i9lt2ufNGcg5uqS/s1600/a1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Here update the path according to your directory. For backup, create a folder with name backup in the root directory (\vcpkg-master\installed\x64-windows\tools\&lt;b&gt;darknet&lt;/b&gt;). In this folder our custom trained weights files will be saved.&lt;br /&gt;
&lt;b&gt;5. &lt;/b&gt;Create a folder named as &lt;b&gt;obj &lt;/b&gt;inside the directory(\vcpkg-master\installed\x64-windows\tools\darknet\&lt;b&gt;data&lt;/b&gt;)&lt;br /&gt;
&lt;br /&gt;
Now your YOLO related changes are done. Next, we will start annotating the images. For this open the anaconda prompt,go to the labellmg directory path and run the following command-&lt;br /&gt;
&lt;b&gt;python labelImg.py /dataset/images/bharatanatyam classes.txt&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
This command will open labellmg window. Here click on the 'Change Save Dir' icon and select the&amp;nbsp;bharatanatyam folder path (/dataset/images/bharatanatyam)-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKHTQMY33brrff8zDvqaRUPtWZHVpuTkbojG2v_Dpin-AvAuadgwmckWS7GBgVB8g5JWWQREFLYhnojgTiedR_oDxGvlIHSQt6Jpcu6QjuPPoqNWiiZ_Up7RDenHTpK5aG2AnNL8NcH-FM/s1600/k1+%25281%2529.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="572" data-original-width="495" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKHTQMY33brrff8zDvqaRUPtWZHVpuTkbojG2v_Dpin-AvAuadgwmckWS7GBgVB8g5JWWQREFLYhnojgTiedR_oDxGvlIHSQt6Jpcu6QjuPPoqNWiiZ_Up7RDenHTpK5aG2AnNL8NcH-FM/s1600/k1+%25281%2529.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Above step will save our annotation txt file in the same location where our images are. So in this case of annotation we will have &amp;lt;img_name&amp;gt;.txt file inside the /dataset/images/bharatanatyam folder. Next click on the 'PascalVOC' icon-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzZzsUzFk8bsjkahQT5OtQQzUgrWujpRJ3TIOsemikw9uxC3G7AgiQrDcbYGuhthuTagStsO2TnMkUKRtyapU4pR5DPleuSl0e6CGnGF_XAzCxlEIv1E2CgoOIVA_yIyF_zWaITODPdxT5/s1600/l1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="641" data-original-width="532" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzZzsUzFk8bsjkahQT5OtQQzUgrWujpRJ3TIOsemikw9uxC3G7AgiQrDcbYGuhthuTagStsO2TnMkUKRtyapU4pR5DPleuSl0e6CGnGF_XAzCxlEIv1E2CgoOIVA_yIyF_zWaITODPdxT5/s1600/l1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once you click on it, it will change to YOLO like in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitxyyl61evhpRsgAfD3-XI9WHtmtedf_CniGYpXEPAcJfs226ATBiETjehd0qZpRlvwDwsUlnEhSRPLSTPIH0PreAH0KYLeGZ_xLY0vABnrfwTdpp9QHJYn33fmHr1PWGOuaNP4WPsY7XH/s1600/l2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="629" data-original-width="420" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitxyyl61evhpRsgAfD3-XI9WHtmtedf_CniGYpXEPAcJfs226ATBiETjehd0qZpRlvwDwsUlnEhSRPLSTPIH0PreAH0KYLeGZ_xLY0vABnrfwTdpp9QHJYn33fmHr1PWGOuaNP4WPsY7XH/s1600/l2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Since we have already given the image folder path and classes.txt file while running the labellmg command, the first image from the image folder will be opened by default in labellmg window. Now click on the 'Create RectBox' icon and draw the annotation on the object you want to capture like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPzo5ezqPGRV_SjCihdyCxQyCktI2qL9l4EJ1M6j2c-zREs93fudB3Gsazesb2a1NPqeha5lFkCtfBQX4cU5GqQkn0wZ7_HXr6Om8anWB1Mu0-gfvGAeuTt8Oi1K7rnUi_4LTVXDD_rn9M/s1600/l3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="985" data-original-width="1423" height="443" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPzo5ezqPGRV_SjCihdyCxQyCktI2qL9l4EJ1M6j2c-zREs93fudB3Gsazesb2a1NPqeha5lFkCtfBQX4cU5GqQkn0wZ7_HXr6Om8anWB1Mu0-gfvGAeuTt8Oi1K7rnUi_4LTVXDD_rn9M/s640/l3.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
After drawing the bounding box, select the bharatnatyam option from the small window and click on ok then click on save button. That's it your first annotation file will be save in the location you have chosen while 'Change Save Dir' option. For next image just click on 'Next Image' icon and repeat the same till you get the last image. Once one folder is completed close this labellmg window, go to the anaconda prompt and run the command with change image directory path as below-&lt;br /&gt;
&lt;b&gt;python labelImg.py /dataset/images/kathak classes.txt&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
It will again open a labellmg window with showing kathak's folder first image. Click on 'Change Save Dir' icon and select the /dataset/images/&lt;b&gt;kathak &lt;/b&gt;and start annotating images like we did earlier. Repeat the same step for all target classes but don't forget to change the save dir path with every new class. Although this annotation step is time taking but requires your attention so do it carefully. I have done my annotations in 1.5 hours. Once done with all folders, got to each folder copy the .txt and .jpg files and paste in obj folder you have created in step 5. Do this for all folders. Now in obj folder you will have all image files and their respective txt files. Please make sure you vae not copied the classes.txt file in the obj folder. If yes then remove that file from obj folder.&lt;br /&gt;
&lt;br /&gt;
Next, go to the directory \vcpkg-master\installed\x64-windows\tools\darknet\&lt;b&gt;data &lt;/b&gt;and create a txt file named as &lt;b&gt;train.txt&lt;/b&gt;. In this file we need to give the image name with file path. Here file path is &lt;br /&gt;/&lt;b&gt;vcpkg-master/installed/x64-windows/tools/darknet/data/obj &lt;/b&gt;so the whole file will be look like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCHa0rQKs9Toeq2iYP2kBkBXV8dzkUJ_6XCZJ8DMUS5VQ3HPmKTaDEkmpwe9KbVM1vbATaijUgkqDcDcotnAp-lDWJFppjsKuORscElSICIo1qVj4oIWO2j3Ts8Rg_ZLSTZ3wHnzXTwEGL/s1600/d1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="242" data-original-width="811" height="190" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCHa0rQKs9Toeq2iYP2kBkBXV8dzkUJ_6XCZJ8DMUS5VQ3HPmKTaDEkmpwe9KbVM1vbATaijUgkqDcDcotnAp-lDWJFppjsKuORscElSICIo1qVj4oIWO2j3Ts8Rg_ZLSTZ3wHnzXTwEGL/s640/d1.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
You can write a Python script for this work. With this step we are ready to start the training of our dance form custom data.&lt;br /&gt;
&lt;br /&gt;
Open windows powershell with admin rights and go to the root directory of the Darknet and run the command&amp;nbsp;&lt;b&gt;.\darknet.exe detector train data/obj.data cfg/yolo-obj.cfg yolov4.conv.137&lt;/b&gt;&lt;br /&gt;
If you have not downloaded the weights yolov4.conv.137, download from&amp;nbsp;&lt;a href="https://drive.google.com/open?id=1JKF-bdIklxOOVy-2Cr5qdvjgGpmGfcbp" target="_blank"&gt;this link&lt;/a&gt;&amp;nbsp;and put inside root directory of Darknet. Once you run the above command, Darknet will load the layers from this weights file and start the training on our custom data. It finds our data from the obj.data file where we have mentioned number of classes, training data path and location of saving the trained weights. This training will take quite enough time since we have given max_batches with 16000 and we have total 8 classes. In my machine it took &lt;b&gt;18 hours&lt;/b&gt; to complete the training so I advise you to disable the sleep property of your machine and start the training in night. After successfully completion of the training you will see following like screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiL8SKmspfGO4Hp10ZEfLXVwH0b3LQxYupDoL03kP1Z0rnkdf5F5dUbieIvN8BXe_HMtcs2EdWGlMJ2c-fg2O5Wj7HFED2TPWgcttGwJulK4Ne2StjPu26KA-CJH5qzcwyTgA2Ox5kBR6pH/s1600/dance_training_output.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="135" data-original-width="902" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiL8SKmspfGO4Hp10ZEfLXVwH0b3LQxYupDoL03kP1Z0rnkdf5F5dUbieIvN8BXe_HMtcs2EdWGlMJ2c-fg2O5Wj7HFED2TPWgcttGwJulK4Ne2StjPu26KA-CJH5qzcwyTgA2Ox5kBR6pH/s1600/dance_training_output.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
This training command will also save &lt;b&gt;loss vs iteration number &lt;/b&gt;chart in the root directory of the Darknet like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgY9DKtLYIWABGAOtalhX0T1VbntI-m6ck8mHfuXUtH0pwlP9KzvjB28Q3HXgXxGZ5lCSqGR4KzTt6DRZ1eZCKdKorOr_u80f1rB3NXr0ukqHA1CjqB0VfvZL4pM5muOXDuoWtnSu9HETcg/s1600/chart.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="908" data-original-width="927" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgY9DKtLYIWABGAOtalhX0T1VbntI-m6ck8mHfuXUtH0pwlP9KzvjB28Q3HXgXxGZ5lCSqGR4KzTt6DRZ1eZCKdKorOr_u80f1rB3NXr0ukqHA1CjqB0VfvZL4pM5muOXDuoWtnSu9HETcg/s1600/chart.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
You can see in the starting of the training the average loss was very high but in the end of the training it is 0.6408.&amp;nbsp;The final average loss can be from 0.05 (for a small model and easy dataset) to 3.0 (for a big model and a difficult dataset). So we have a very good model. Now we can test our model on unseen images. For this run the following command in the same powershell window-&lt;br /&gt;
&lt;b&gt;.\darknet.exe detector test data/obj.data cfg/yolo-obj.cfg backup/yolo-obj_final.weights /dataset/test/6.jpg -dont_show -out_filename&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Here just change the test image path according to your system. Above command will predict the class on the given image with confidence in the powershell console as well as it will save the image with bounding box and class name in the root Darknet directory with name as 'predictions.jpg'-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx-OQavEILvpZtKhYTwpudhvVMXPMr9Yk0FlessUjO2US6nGX8tH2cOZJuRxC704IZAAgHEdXRK77_vKdjGf840mTIIuD4gI0OyaqtfN_4_KTX0F_VbyXyJK1NZ2b8PdSLeXdP4iEHUTA6/s1600/test1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="221" data-original-width="755" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx-OQavEILvpZtKhYTwpudhvVMXPMr9Yk0FlessUjO2US6nGX8tH2cOZJuRxC704IZAAgHEdXRK77_vKdjGf840mTIIuD4gI0OyaqtfN_4_KTX0F_VbyXyJK1NZ2b8PdSLeXdP4iEHUTA6/s1600/test1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
And it's corresponding prediction image file is as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-5Y33KjLuLlz337uuHukd6Z52p5WwokMipmqazPcodRb2fWE5-yx5Uy_PSuGStP-1m8cCE9dDHk7LXAwxkMaj7xtcCTcd1OyxZHyb1ooCY9eq8422TUaFHXukFqjolmmqyr2hNpXnA7wK/s1600/pred.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="326" data-original-width="358" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-5Y33KjLuLlz337uuHukd6Z52p5WwokMipmqazPcodRb2fWE5-yx5Uy_PSuGStP-1m8cCE9dDHk7LXAwxkMaj7xtcCTcd1OyxZHyb1ooCY9eq8422TUaFHXukFqjolmmqyr2hNpXnA7wK/s1600/pred.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
See our model is able to detect the dance form as Kathak for this unseen image which is correct. Let's check another unseen image using command&amp;nbsp;&lt;b&gt;.\darknet.exe detector test data/obj.data cfg/yolo-obj.cfg backup/yolo-obj_final.weights /dataset/test/6.jpg -dont_show -out_filename&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-NmUFmuhSCh00-qkg_uXs210ssrn01WIC_fCELpksib-pzMJq1uuc3-lYlanOcdwgYLbfY7b4XZbWQvQHaXlQL95ZZImsP8ubqgXn-CenvmzsWUqslvmiEQh6utc005rie2R1bouMi6y5/s1600/s1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="338" data-original-width="242" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-NmUFmuhSCh00-qkg_uXs210ssrn01WIC_fCELpksib-pzMJq1uuc3-lYlanOcdwgYLbfY7b4XZbWQvQHaXlQL95ZZImsP8ubqgXn-CenvmzsWUqslvmiEQh6utc005rie2R1bouMi6y5/s1600/s1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Looks like our model is working as expected. Please note for a very good YOLO model we should preferably have 2000 different images for each class or more, and we should train 2000*classes iterations or more. Due to my system configurations I have used image width and height as 416 but if you have got bigger avg loss then you should increase the network resolution to 608 or 832. Also if you don't want to detect any specific object then you should also include that object images with &lt;b&gt;empty&lt;/b&gt; &lt;b&gt;txt&lt;/b&gt; files in the obj folder.&lt;br /&gt;
&lt;br /&gt;
That's it guys for today. If you have followed till the end, you have spent your valuable time in learning a state of the art computer vision technique. Here we are using the original Darknet framework, no any wrapper for our work. There are only very few persons who know using such a state of the art computer vision technique. Now you are one of them but to be a master on a technique you must practice and do experiments. So don't stop yourself just reading here. Do practical on your system, solve issues while doing hands on, train YOLOv4 on another custom dataset, deploy your custom model as Rest API and showcase in your github repository. In my next post I will share my new learning to you; till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM-0ZkQxeBM-s17_VNtdR7VFHlxSt9whxAo8ArwU8iJff7toZDTl8aB7-gO9Yj0iHOMjc52GgoYjSDqbLdgC-8e7JEgMhhRyRBo0hrz3Pco5xjPbednDWfQyZ9yEgifpNLEgbT4maIJYx8/s72-c/dance.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total></item><item><title>How to install and compile YOLO v4 with GPU enable settings in Windows 10?</title><link>https://dsbyprateekg.blogspot.com/2020/05/how-to-install-and-compile-yolo-v4-with.html</link><category>GPU</category><category>Windows</category><category>YOLOv4</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Sat, 23 May 2020 22:28:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-4679478917990274849</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='320' height='266' src='https://www.blogger.com/video.g?token=AD6v5dwJmZny6hRWTRhUPdHFeUuQrghEGd1MAD3JnS22ddLTuPtXPmyFdnmay95uv8G-dLLZvbfLGz-sQTZ6-m3xQw' class='b-hbp-video b-uploaded' frameborder='0'&gt;&lt;/iframe&gt;&lt;/div&gt;
&lt;br /&gt;
Another post starts with you beautiful people!&lt;br /&gt;
Last year I had shared a post about&amp;nbsp;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2019/12/how-can-i-install-and-use-darknet.html" target="_blank"&gt;installing and compiling Darknet YOLOv3&lt;/a&gt;&amp;nbsp;&lt;/b&gt;in your Windows machine and also how to detect an object using&amp;nbsp;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2019/11/how-to-detect-object-in-real-time-using.html" target="_blank"&gt;YOLOv3 with Keras&lt;/a&gt;. &lt;/b&gt;This year on April' 2020 the fourth generation of YOLO has arrived and since then I was curious to use this as soon as possible. Due to my project (built on YOLOv3 :)) work I could not find a chance to check this latest release. Today I got some relief and successfully able to install and compile &lt;a href="https://github.com/AlexeyAB/darknet" target="_blank"&gt;&lt;b&gt;YOLOv4&lt;/b&gt; &lt;/a&gt;in my machine. In this post I am going to share a single shot way to do the same in your Windows 10 machine. If your machine does not have GPU then you can follow my&amp;nbsp;&lt;a href="http://dsbyprateekg.blogspot.com/2019/12/how-can-i-install-and-use-darknet.html" target="_blank"&gt;previous post&lt;/a&gt;&amp;nbsp;by just replacing YOLOv3 related files with YOLOv4 files. For GPU having Windows machine, follow my steps to avoid any issue while building the Darknet repository.&lt;br /&gt;
&lt;br /&gt;
My machine has following configurations:&lt;br /&gt;
Windows 10 64 bit Intel Core i7&lt;br /&gt;
16 GB RAM&lt;br /&gt;
NVIDIA GeForce&amp;nbsp;GTX 1660 Ti Version 445.87&lt;br /&gt;
CUDA 10.1, cuDNN 7.6&lt;br /&gt;
Visual Studio 2017&lt;br /&gt;
&lt;br /&gt;
Before proceeding please make sure that you have installed CUDA, cuDNN as per your GPU drivers and Visual Studio 2017 is installed with C++ distributions. An invalid CUDA/cuDNN version will show unnecessary errors while installing. Once you check and assure all setup is up to date; disable your antivirus settings till the end of this process. For building the Darknet code I am here using &lt;b&gt;Vcpkg&lt;/b&gt; instead of Darknet repo's build.ps1 file since with this build.ps1 file I was not able to build the code with GPU settings. Let's start the process-&lt;br /&gt;
&lt;br /&gt;
1. Download Vcpkg from&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/Microsoft/vcpkg" target="_blank"&gt;this link&lt;/a&gt;. &lt;/b&gt;It is a C++ library manager and will be used to install and compile the Darknet code.&lt;br /&gt;
&lt;br /&gt;
2. Open Windows PowerShell with admin rights and go to the root directory of the Vcpkg folder you have downloaded from step 1 and run following two commands one by one:&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;&lt;span style="color: #24292e; font-family: SFMono-Regular, Consolas, &amp;quot;Liberation Mono&amp;quot;, Menlo, monospace; font-size: 13.6px;"&gt;&lt;b&gt;.\bootstrap-vcpkg.bat&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="color: #24292e; font-family: SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;"&gt;&lt;span style="font-size: 13.6px;"&gt;&lt;b&gt;.\vcpkg integrate install&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi43MD9hNGGL4K74MekjMFsTfsTYmTCT5gNuBWx2B6kIiXo-aJGitEDPiZV7vM3G7KjgGCu0PdMz12zQsvp065ldAJhmedzYlzKlD9pWTQKqQVviAwigoMyxaD3OB-JoGFA8Wuc9Q-z7pZf/s1600/d1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="185" data-original-width="509" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi43MD9hNGGL4K74MekjMFsTfsTYmTCT5gNuBWx2B6kIiXo-aJGitEDPiZV7vM3G7KjgGCu0PdMz12zQsvp065ldAJhmedzYlzKlD9pWTQKqQVviAwigoMyxaD3OB-JoGFA8Wuc9Q-z7pZf/s1600/d1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
Please note if you are using Windows PowerShell first time, run&amp;nbsp;Set-ExecutionPolicy RemoteSigned and press A when asked and then press Enter.&lt;/blockquote&gt;
3. Next, run following command:&amp;nbsp;&lt;b&gt;.\vcpkg install darknet[full]:x64-windows&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbcbNejo34C8h7dJfxG0mEoH87HU3vDCxVVRzWsshq3T8DZpNQEysyLSwGhlJkMoiC8iA6CZWkeD3Nt_2fEehzXmC-Q6RUzc6uAixDVEYNHBhCRKqWnpXIx3KFts27texMrfgtfCgBSz35/s1600/d2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="194" data-original-width="585" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbcbNejo34C8h7dJfxG0mEoH87HU3vDCxVVRzWsshq3T8DZpNQEysyLSwGhlJkMoiC8iA6CZWkeD3Nt_2fEehzXmC-Q6RUzc6uAixDVEYNHBhCRKqWnpXIx3KFts27texMrfgtfCgBSz35/s1600/d2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Above command will download the opencv with cuda, other required libraries and Darknet code. After downloading it will install the all libraries and then it will compile the Darknet code with GPU enable settings automatically. This process will take some time so have patience and wait to complete. Once this process is completed, you will see logs in your PowerShell and some green lines with done status. Make sure all green lines have done status. Once you see done status in all it means Darknet code is &lt;b&gt;successfully&lt;/b&gt; &lt;b&gt;compiled&lt;/b&gt; with GPU enable settings in your Windows machine. You can check it by going into the darknet directory: &amp;lt;Your_System_dir&amp;gt;\&lt;b&gt;vcpkg-master\installed\x64-windows\tools\darknet&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
4. Download the &lt;b&gt;yolov4.weights&lt;/b&gt; file from&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights" target="_blank"&gt;this link&lt;/a&gt;&amp;nbsp;&lt;/b&gt;and put it into the darknet directory&lt;br /&gt;
5. Download a sample mp4 video file and put into the same darknet directory&lt;br /&gt;
6. In the same PowerShell window go to the path of your darknet directory and run the following command-&lt;br /&gt;
&lt;b&gt;.\darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights Car_Racing.mp4 -dont_show -out_filename carRacing_result.mp4&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFYi_txcRoeN4JIF4T6M6InM3nBWKk1MeTvqNSI4Q7XznY61TutoiLT71wqSxYqXIjPzYlhBaXpJHhtHqUSS81BVORbFFxyCu3xXWqGAarLJG6G7k_W5248GEHWGbG3_vuirssSmVLVpLI/s1600/d3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="77" data-original-width="1600" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFYi_txcRoeN4JIF4T6M6InM3nBWKk1MeTvqNSI4Q7XznY61TutoiLT71wqSxYqXIjPzYlhBaXpJHhtHqUSS81BVORbFFxyCu3xXWqGAarLJG6G7k_W5248GEHWGbG3_vuirssSmVLVpLI/s1600/d3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once you run the above command, in the starting of the running command you will see following like line-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYaPgK1eJks70ZlIjEr9dNkmA0hWG9qKXNnVlZktEWpxz7qdDOts0xOKNqSOxAEgMXW801Y7amM6lTNvvX3viH4-qe5IvPh9fxAX59gSccRkGCgUHGqRDUtOgpLCov8Duz49Yb3hSx4Klk/s1600/d4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="169" data-original-width="800" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYaPgK1eJks70ZlIjEr9dNkmA0hWG9qKXNnVlZktEWpxz7qdDOts0xOKNqSOxAEgMXW801Y7amM6lTNvvX3viH4-qe5IvPh9fxAX59gSccRkGCgUHGqRDUtOgpLCov8Duz49Yb3hSx4Klk/s1600/d4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
You can see my CUDA, cuDNN and GPU count are showing in the console which confirms that my GPU is used while using the pre-trained YOLOv4 weights on a sample video file. Once this run is completed you can go to your darknet directory and found a output file carRacing_result.mp4 or your sample video output file. Open that file in a media player and you will see bounding boxes around the detected object names like person, car, truck on them. Inspite of fast moving objects in my video file, YOLOv4 is able to detect objects accurate. You can see my output video from &lt;a href="https://drive.google.com/open?id=1M1N4_57KqcFaOR-SgjE2vp0mQsBXE4qP" target="_blank"&gt;&lt;b&gt;this link&lt;/b&gt; &lt;/a&gt;. In my machine the average FPS with GPU was 6.7 for this video and with CPU it was 0.4. True power of using a real time object detector like YOLO is with GPU only :)&lt;br /&gt;
&lt;br /&gt;
You can also test the detector with an image by running the same command but replacing video file with image file like below-&lt;br /&gt;
&lt;b&gt;.\darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights &amp;lt;IMAGE_FiLE&amp;gt; -dont_show -out_filename&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBrOM2rhenClbDseAio9uaFThPVtM-f-jNNiIHW8CvtX-boF1KJWCLL3yuRX1HBypXaFm4ufnjOW5wIp0TkDDQQV5Be1x6qCokNiSPEjokHfwoxBmMdrpkwxTddAYWsor96oYtgdxstoXu/s1600/d6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="167" data-original-width="1428" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBrOM2rhenClbDseAio9uaFThPVtM-f-jNNiIHW8CvtX-boF1KJWCLL3yuRX1HBypXaFm4ufnjOW5wIp0TkDDQQV5Be1x6qCokNiSPEjokHfwoxBmMdrpkwxTddAYWsor96oYtgdxstoXu/s1600/d6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once you open the saved image (predictions.jpg), the image will show the detection result as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFJjQZdpfprhrxZ1I52sOBlK_-8BZlBEyWsWnI6Up-fl_RTfJBTCu4GwYjFXPUi3t5WgHLk2YU2cO2J9UygHpOUgQFY6sJpJvU4uzB3c9cHvdad6YPsexXWsmroYl1Hy-jlynJ-i7V0kIa/s1600/predictions.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="630" data-original-width="1200" height="336" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFJjQZdpfprhrxZ1I52sOBlK_-8BZlBEyWsWnI6Up-fl_RTfJBTCu4GwYjFXPUi3t5WgHLk2YU2cO2J9UygHpOUgQFY6sJpJvU4uzB3c9cHvdad6YPsexXWsmroYl1Hy-jlynJ-i7V0kIa/s640/predictions.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
You can see pre-trained weights of YOLOv4 is able to detect person, cup, spoon, wine glass, fork, bowl, chair, backpack and knife in the above picture.&lt;br /&gt;
&lt;br /&gt;
That's it for today. With this installation we have a real time object detector. Using it's pre-trained weights only we are able to detect many objects. I am using 2 custom YOLOv3 based models in my project and both are successfully running in production. Soon I will replace both models with the 4th generation YOLO. In my next post I will share you how I prepared my custom objects and trained them with YOLOv4.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi43MD9hNGGL4K74MekjMFsTfsTYmTCT5gNuBWx2B6kIiXo-aJGitEDPiZV7vM3G7KjgGCu0PdMz12zQsvp065ldAJhmedzYlzKlD9pWTQKqQVviAwigoMyxaD3OB-JoGFA8Wuc9Q-z7pZf/s72-c/d1.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">46</thr:total><enclosure length="257717640" type="application/octet-stream" url="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights"/><itunes:explicit>no</itunes:explicit><itunes:subtitle>Another post starts with you beautiful people! Last year I had shared a post about&amp;nbsp;installing and compiling Darknet YOLOv3&amp;nbsp;in your Windows machine and also how to detect an object using&amp;nbsp;YOLOv3 with Keras. This year on April' 2020 the fourth generation of YOLO has arrived and since then I was curious to use this as soon as possible. Due to my project (built on YOLOv3 :)) work I could not find a chance to check this latest release. Today I got some relief and successfully able to install and compile YOLOv4 in my machine. In this post I am going to share a single shot way to do the same in your Windows 10 machine. If your machine does not have GPU then you can follow my&amp;nbsp;previous post&amp;nbsp;by just replacing YOLOv3 related files with YOLOv4 files. For GPU having Windows machine, follow my steps to avoid any issue while building the Darknet repository. My machine has following configurations: Windows 10 64 bit Intel Core i7 16 GB RAM NVIDIA GeForce&amp;nbsp;GTX 1660 Ti Version 445.87 CUDA 10.1, cuDNN 7.6 Visual Studio 2017 Before proceeding please make sure that you have installed CUDA, cuDNN as per your GPU drivers and Visual Studio 2017 is installed with C++ distributions. An invalid CUDA/cuDNN version will show unnecessary errors while installing. Once you check and assure all setup is up to date; disable your antivirus settings till the end of this process. For building the Darknet code I am here using Vcpkg instead of Darknet repo's build.ps1 file since with this build.ps1 file I was not able to build the code with GPU settings. Let's start the process- 1. Download Vcpkg from&amp;nbsp;this link. It is a C++ library manager and will be used to install and compile the Darknet code. 2. Open Windows PowerShell with admin rights and go to the root directory of the Vcpkg folder you have downloaded from step 1 and run following two commands one by one: .\bootstrap-vcpkg.bat .\vcpkg integrate install Please note if you are using Windows PowerShell first time, run&amp;nbsp;Set-ExecutionPolicy RemoteSigned and press A when asked and then press Enter. 3. Next, run following command:&amp;nbsp;.\vcpkg install darknet[full]:x64-windows Above command will download the opencv with cuda, other required libraries and Darknet code. After downloading it will install the all libraries and then it will compile the Darknet code with GPU enable settings automatically. This process will take some time so have patience and wait to complete. Once this process is completed, you will see logs in your PowerShell and some green lines with done status. Make sure all green lines have done status. Once you see done status in all it means Darknet code is successfully compiled with GPU enable settings in your Windows machine. You can check it by going into the darknet directory: &amp;lt;Your_System_dir&amp;gt;\vcpkg-master\installed\x64-windows\tools\darknet 4. Download the yolov4.weights file from&amp;nbsp;this link&amp;nbsp;and put it into the darknet directory 5. Download a sample mp4 video file and put into the same darknet directory 6. In the same PowerShell window go to the path of your darknet directory and run the following command- .\darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights Car_Racing.mp4 -dont_show -out_filename carRacing_result.mp4 Once you run the above command, in the starting of the running command you will see following like line- You can see my CUDA, cuDNN and GPU count are showing in the console which confirms that my GPU is used while using the pre-trained YOLOv4 weights on a sample video file. Once this run is completed you can go to your darknet directory and found a output file carRacing_result.mp4 or your sample video output file. Open that file in a media player and you will see bounding boxes around the detected object names like person, car, truck on them. Inspite of fast moving objects in my video file, YOLOv4 is able to detect objects accurate. You can see my output video from this link . In my machine the average FPS with GPU was 6.7 for this video and with CPU it was 0.4. True power of using a real time object detector like YOLO is with GPU only :) You can also test the detector with an image by running the same command but replacing video file with image file like below- .\darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights &amp;lt;IMAGE_FiLE&amp;gt; -dont_show -out_filename Once you open the saved image (predictions.jpg), the image will show the detection result as below- You can see pre-trained weights of YOLOv4 is able to detect person, cup, spoon, wine glass, fork, bowl, chair, backpack and knife in the above picture. That's it for today. With this installation we have a real time object detector. Using it's pre-trained weights only we are able to detect many objects. I am using 2 custom YOLOv3 based models in my project and both are successfully running in production. Soon I will replace both models with the 4th generation YOLO. In my next post I will share you how I prepared my custom objects and trained them with YOLOv4.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.</itunes:subtitle><itunes:author>noreply@blogger.com (Learn Data Science with PrateekG)</itunes:author><itunes:summary>Another post starts with you beautiful people! Last year I had shared a post about&amp;nbsp;installing and compiling Darknet YOLOv3&amp;nbsp;in your Windows machine and also how to detect an object using&amp;nbsp;YOLOv3 with Keras. This year on April' 2020 the fourth generation of YOLO has arrived and since then I was curious to use this as soon as possible. Due to my project (built on YOLOv3 :)) work I could not find a chance to check this latest release. Today I got some relief and successfully able to install and compile YOLOv4 in my machine. In this post I am going to share a single shot way to do the same in your Windows 10 machine. If your machine does not have GPU then you can follow my&amp;nbsp;previous post&amp;nbsp;by just replacing YOLOv3 related files with YOLOv4 files. For GPU having Windows machine, follow my steps to avoid any issue while building the Darknet repository. My machine has following configurations: Windows 10 64 bit Intel Core i7 16 GB RAM NVIDIA GeForce&amp;nbsp;GTX 1660 Ti Version 445.87 CUDA 10.1, cuDNN 7.6 Visual Studio 2017 Before proceeding please make sure that you have installed CUDA, cuDNN as per your GPU drivers and Visual Studio 2017 is installed with C++ distributions. An invalid CUDA/cuDNN version will show unnecessary errors while installing. Once you check and assure all setup is up to date; disable your antivirus settings till the end of this process. For building the Darknet code I am here using Vcpkg instead of Darknet repo's build.ps1 file since with this build.ps1 file I was not able to build the code with GPU settings. Let's start the process- 1. Download Vcpkg from&amp;nbsp;this link. It is a C++ library manager and will be used to install and compile the Darknet code. 2. Open Windows PowerShell with admin rights and go to the root directory of the Vcpkg folder you have downloaded from step 1 and run following two commands one by one: .\bootstrap-vcpkg.bat .\vcpkg integrate install Please note if you are using Windows PowerShell first time, run&amp;nbsp;Set-ExecutionPolicy RemoteSigned and press A when asked and then press Enter. 3. Next, run following command:&amp;nbsp;.\vcpkg install darknet[full]:x64-windows Above command will download the opencv with cuda, other required libraries and Darknet code. After downloading it will install the all libraries and then it will compile the Darknet code with GPU enable settings automatically. This process will take some time so have patience and wait to complete. Once this process is completed, you will see logs in your PowerShell and some green lines with done status. Make sure all green lines have done status. Once you see done status in all it means Darknet code is successfully compiled with GPU enable settings in your Windows machine. You can check it by going into the darknet directory: &amp;lt;Your_System_dir&amp;gt;\vcpkg-master\installed\x64-windows\tools\darknet 4. Download the yolov4.weights file from&amp;nbsp;this link&amp;nbsp;and put it into the darknet directory 5. Download a sample mp4 video file and put into the same darknet directory 6. In the same PowerShell window go to the path of your darknet directory and run the following command- .\darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights Car_Racing.mp4 -dont_show -out_filename carRacing_result.mp4 Once you run the above command, in the starting of the running command you will see following like line- You can see my CUDA, cuDNN and GPU count are showing in the console which confirms that my GPU is used while using the pre-trained YOLOv4 weights on a sample video file. Once this run is completed you can go to your darknet directory and found a output file carRacing_result.mp4 or your sample video output file. Open that file in a media player and you will see bounding boxes around the detected object names like person, car, truck on them. Inspite of fast moving objects in my video file, YOLOv4 is able to detect objects accurate. You can see my output video from this link . In my machine the average FPS with GPU was 6.7 for this video and with CPU it was 0.4. True power of using a real time object detector like YOLO is with GPU only :) You can also test the detector with an image by running the same command but replacing video file with image file like below- .\darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights &amp;lt;IMAGE_FiLE&amp;gt; -dont_show -out_filename Once you open the saved image (predictions.jpg), the image will show the detection result as below- You can see pre-trained weights of YOLOv4 is able to detect person, cup, spoon, wine glass, fork, bowl, chair, backpack and knife in the above picture. That's it for today. With this installation we have a real time object detector. Using it's pre-trained weights only we are able to detect many objects. I am using 2 custom YOLOv3 based models in my project and both are successfully running in production. Soon I will replace both models with the 4th generation YOLO. In my next post I will share you how I prepared my custom objects and trained them with YOLOv4.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.</itunes:summary><itunes:keywords>GPU, Windows, YOLOv4</itunes:keywords></item><item><title>Building and deploying your ChatBot with Amazon Lex, AWS Lambda, Python and MongoDB</title><link>https://dsbyprateekg.blogspot.com/2020/05/building-and-deploying-your-chatbot.html</link><category>AWS CloudFormation</category><category>AWS Lambda</category><category>AWS Lex</category><category>chatbot</category><category>MongoDB</category><category>python</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Sat, 9 May 2020 21:49:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-4739302384648939007</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhC3BtinlwKWBOffyDTnqke4v87olPzWmiwZVsX_6N8bDDC_x7HtIN-7jcvenwxgEKltiHO5-pXfjlZaxrhidXNNWzXuKo4jNq4dTQIoCa6Ea5M9koHzPMToyEnbHxpXI5bcQp-1MkERj3x/s1600/c1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="165" data-original-width="784" height="132" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhC3BtinlwKWBOffyDTnqke4v87olPzWmiwZVsX_6N8bDDC_x7HtIN-7jcvenwxgEKltiHO5-pXfjlZaxrhidXNNWzXuKo4jNq4dTQIoCa6Ea5M9koHzPMToyEnbHxpXI5bcQp-1MkERj3x/s640/c1.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Another post starts with you beautiful people!&lt;br /&gt;
Most of the businesses are adopting digital transformation to modernize customer communication and improve internal processes. By personalizing the user experience whether in a chatbot conversation, on a website or in email, you can make your user feel more valued and understood.&amp;nbsp;&lt;a href="https://dialogflow.com/" target="_blank"&gt;Google DialogFlow&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://docs.aws.amazon.com/lex/latest/dg/what-is.html" style="font-weight: bold;" target="_blank"&gt;Amazon Lex&lt;/a&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&amp;nbsp;are two pioneer vendors for building end to end personalized chatbot applications. In this post we are going to use Amazon Lex to build our chatbot and after the end of this post you will have your chatbot integrated with a web page and also your web page will be deployed on AWS cloud. This post is going to be long and very interesting so stay focus and keep reading the post till the end.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 1. Creating your account in AWS&lt;/b&gt;&lt;br /&gt;
To proceed with this post you must have an AWS account. If you don't have , just follow&amp;nbsp;&lt;a href="https://portal.aws.amazon.com/billing/signup?refid=em_127222&amp;amp;redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#/start" target="_blank"&gt;&lt;b&gt;this link&lt;/b&gt;&lt;/a&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;to create a free tier account there. While registration it may ask for your card details. No need to worry about any deduction. It's only for verification of your identity. Also we are going to use free tier services which is quite enough for our exercise so go ahead and make your account. As an extra security to avoid any charges after free tier expiration you must follow &lt;b&gt;&lt;a href="https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/checklistforunwantedcharges.html" target="_blank"&gt;this link&lt;/a&gt;.&amp;nbsp;&lt;/b&gt;After successful activation of your account you must login there by clicking on 'Sign In to the Console' button as showing here:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZXWatDGFg6-me_G8zLWvbdbQRgHt2igafxse8D2wLkJ_B9zK7y_IZROXG6IbpO_yBnzYyMwPwZ6mQEMuMcZ0ioiS0_u_MjTs3K2BtAlloQRtJRgQJmrCbc4rOQdcgx-IZxqInnU6tndPT/s1600/c2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="140" data-original-width="1600" height="56" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZXWatDGFg6-me_G8zLWvbdbQRgHt2igafxse8D2wLkJ_B9zK7y_IZROXG6IbpO_yBnzYyMwPwZ6mQEMuMcZ0ioiS0_u_MjTs3K2BtAlloQRtJRgQJmrCbc4rOQdcgx-IZxqInnU6tndPT/s640/c2.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once you login in your account, you need to update your &lt;b&gt;region&lt;/b&gt; to '&lt;b&gt;US East (N. Virginia) us-east-1&lt;/b&gt;' as this region is &lt;b&gt;required&lt;/b&gt; to &lt;b&gt;deploy&lt;/b&gt; our chatbot using &lt;b&gt;Amazon's CloudFormation. &lt;/b&gt;Your region should be look like as in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZIX5ZWQqUwKG0KiB4oRRogRjC9JnpWbzVacOIYWgqCzKBZyWVfgIhJKzWAMvvJJH9WvpWwYrGjTfE9Tyu-IJFCTkl61sgPY3XNgy-SJTfyEghi8CR-z5pwcSg3sF6vnDiKTxmnkdW_U6g/s1600/c3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="101" data-original-width="1600" height="40" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZIX5ZWQqUwKG0KiB4oRRogRjC9JnpWbzVacOIYWgqCzKBZyWVfgIhJKzWAMvvJJH9WvpWwYrGjTfE9Tyu-IJFCTkl61sgPY3XNgy-SJTfyEghi8CR-z5pwcSg3sF6vnDiKTxmnkdW_U6g/s640/c3.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Step 2. Configuring Amazon Lex&lt;/b&gt;&lt;br /&gt;
Next, you need to open your Amazon Lex console. For this type 'Lex' or 'Amazon Lex' in the Find Services search box and select the Amazon Lex option as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpSHrHWU7j7_6Py6hwBrfb736qNUjOa9F08XAc_5nXBxut9tk7oE6FDeiHE3yTxRQC4dLXaqJ5EZvFkI-WIOtOCMfGxeoU3aBckcJBuzInJg6wo-gJv5BugCjuxe8bAl8O3SexSJzCKmqg/s1600/c4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="463" data-original-width="1600" height="184" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpSHrHWU7j7_6Py6hwBrfb736qNUjOa9F08XAc_5nXBxut9tk7oE6FDeiHE3yTxRQC4dLXaqJ5EZvFkI-WIOtOCMfGxeoU3aBckcJBuzInJg6wo-gJv5BugCjuxe8bAl8O3SexSJzCKmqg/s640/c4.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
As soon you select the Amazon Lex option the page will be refreshed and you will see your Lex screen as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmWAFPamXjukbohnGPqtAjYkfpHh4LfF-s8DGZiZJ6yot_1KVMfPKYLSbDmo2n9YH7xcG4Es3Sxx_0_xHsYYzKAtEv3tn1vuKc_7ecnw5OsvTUhcDh2WoNarqOT5q04tBTAbYTi5l_Db_q/s1600/c5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="478" data-original-width="1600" height="190" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmWAFPamXjukbohnGPqtAjYkfpHh4LfF-s8DGZiZJ6yot_1KVMfPKYLSbDmo2n9YH7xcG4Es3Sxx_0_xHsYYzKAtEv3tn1vuKc_7ecnw5OsvTUhcDh2WoNarqOT5q04tBTAbYTi5l_Db_q/s640/c5.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Click on the Get Started button and your lex console screen will be opened as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaLHBvbzLXQakZVzbijbLsJNqlmyQVwQQK_4glidnNRPO8xeBmTYbRfDiP4oRCQVfurzkfHsPzYkoA8OikHKdMP5Dl4TPChixFwAZq4gorAzEJK_8NQ81rGzr8FScMRdTbaGyP3Cx354y6/s1600/c6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="460" data-original-width="1600" height="184" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaLHBvbzLXQakZVzbijbLsJNqlmyQVwQQK_4glidnNRPO8xeBmTYbRfDiP4oRCQVfurzkfHsPzYkoA8OikHKdMP5Dl4TPChixFwAZq4gorAzEJK_8NQ81rGzr8FScMRdTbaGyP3Cx354y6/s640/c6.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
As you can see in above screen, Lex provides us some in built examples of personalized chatbots with Custom bot options. You can try any in built examples by selecting a Sample template. Before making our custom bot I recommend you to select OrderFlowers, check following options and click on create button -&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiewJbr6vDX-1Xdv2GMkAw4ojbJYEESljSbIlArwkqObLhRfkJyQwa9LqjWzOo_1Tx_oREtDdcv0zuZhlI1FTqg2pZcVfSt5wqFtmFZ2wJzuvjoZeLZaoGX-EsXAEFqCJoaiID-5bwtQAyT/s1600/c7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="401" data-original-width="757" height="338" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiewJbr6vDX-1Xdv2GMkAw4ojbJYEESljSbIlArwkqObLhRfkJyQwa9LqjWzOo_1Tx_oREtDdcv0zuZhlI1FTqg2pZcVfSt5wqFtmFZ2wJzuvjoZeLZaoGX-EsXAEFqCJoaiID-5bwtQAyT/s640/c7.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Above selections are also applicable to Custom bot option. Based on your actual use case you can enable the Sentiment analysis option. If you want to enable both text and voice input to your bot the default IAM role is necessary. Once you click on the create button bot will open following look like console-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjygw69DAYo7TRFOJiTPSte9ITlHyxchXZngVrPoIhZG4fq6zXSBnORtRYRMDox_pN3Na4rOyWKPfv7NUAZxRKp2CwoVrPava1vBglpn-B1dVR8VLCpvgR6EKEhoMDRYTORNw1Tyuo_Svh2/s1600/c8.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="369" data-original-width="1600" height="146" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjygw69DAYo7TRFOJiTPSte9ITlHyxchXZngVrPoIhZG4fq6zXSBnORtRYRMDox_pN3Na4rOyWKPfv7NUAZxRKp2CwoVrPava1vBglpn-B1dVR8VLCpvgR6EKEhoMDRYTORNw1Tyuo_Svh2/s640/c8.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Since it is in built bot example, we have all set to test this bot functionality. you can test the bot by typing a question (&lt;span style="background-color: white; color: #444444; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;roboto&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 14px; white-space: nowrap;"&gt;I would like to order some flowers)&lt;/span&gt;&amp;nbsp;mentioned in Sample utterances box in your test bot screen as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCEMbq_-xEVYaRdWZMXOLeM0NyE5lBwvPaNGA80B1Fg7NOmvvkFOAy_kTUYWCWKOvMsPJokzcyz5LvdmWTu31SgVlKgtFaDJS1WwtLTh6AuYykO1wPXC3va4yNm6V4ITRq2T5jGWz9UlOd/s1600/c9.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="433" data-original-width="499" height="277" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCEMbq_-xEVYaRdWZMXOLeM0NyE5lBwvPaNGA80B1Fg7NOmvvkFOAy_kTUYWCWKOvMsPJokzcyz5LvdmWTu31SgVlKgtFaDJS1WwtLTh6AuYykO1wPXC3va4yNm6V4ITRq2T5jGWz9UlOd/s320/c9.JPG" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Go ahead follow the bot conversation flow and explore yourself with this example. Now I will move to the main goal of this bot- creating your custom bot and interact it with your MongoDB database to fetch some queries. This is an actual use case example where you have some product's or person's information stored in MongoDB database and to answer a question of a user you need to get from that db otherwise your bot is incomplete and could not able to fulfill the conversation.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 3. Setup A MongoDB Data&lt;/b&gt;&lt;br /&gt;
To move further you need some sample data in MongoDB. You can import any specific data for this exercise. If you are new to MongoDB don't worry I will tell you the steps. Any experienced person can skip this section and move to the step 4. To setup MongoDB, go to the&amp;nbsp;&lt;b&gt;&lt;a href="https://www.mongodb.com/cloud/atlas" target="_blank"&gt;Atlas Link&lt;/a&gt;&amp;nbsp;&lt;/b&gt;and click on start free button. Please note MongoDB Atlas is a cloud based db setup so you don't need to do the db setup in your local machine. Complete the signup process and sign in your atlas account. We will use it's free tier (512 MB size) known as &lt;b&gt;Shared Clusters&amp;nbsp;&lt;/b&gt;to setup our db infrastructure as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEcagDqSAmmr2Yn-l0KWFNro7-qbscD4GazPNl5zLPBWg7VnVYiwwdeDXhjdCEaB8x2a7h22pDV1DIoQpF1hj3yejqQoKHD-ECf8PpOiVvcL0pxWY5XNvxBJIt3lTocRPSLRpsAg8zlIsS/s1600/c10.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="704" data-original-width="334" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEcagDqSAmmr2Yn-l0KWFNro7-qbscD4GazPNl5zLPBWg7VnVYiwwdeDXhjdCEaB8x2a7h22pDV1DIoQpF1hj3yejqQoKHD-ECf8PpOiVvcL0pxWY5XNvxBJIt3lTocRPSLRpsAg8zlIsS/s400/c10.JPG" width="188" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Click on Create a cluster button. In next screen select the aws as cloud provider &amp;amp; region, choose region as us-east-1 and click on create cluster button. After this step in next screen complete all options one by one as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBe7CTqqfLiZSCHC8DkhoVjke6mEY6z8p9W2hCl9BDJ-o_2vpjB-zglzUOtjf14HhioS4jM8fcOoffcwrYn91XJI25r2HVZs4CGBevP0s_TDgmeDxxVIYSqDRXB3VA-_uYEvLEF3Cm10Uq/s1600/c11.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="586" data-original-width="425" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBe7CTqqfLiZSCHC8DkhoVjke6mEY6z8p9W2hCl9BDJ-o_2vpjB-zglzUOtjf14HhioS4jM8fcOoffcwrYn91XJI25r2HVZs4CGBevP0s_TDgmeDxxVIYSqDRXB3VA-_uYEvLEF3Cm10Uq/s320/c11.JPG" width="232" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
For example first create a database user there by clicking on 'Database Access' option and enter your username/password in below look like screen. Please must follow &lt;b&gt;&lt;a href="https://docs.atlas.mongodb.com/troubleshoot-connection/#special-characters-in-connection-string-password" target="_blank"&gt;password guidelines link&lt;/a&gt;&amp;nbsp;&lt;/b&gt;while creating your password otherwise you will face connection error later-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibXbEHPtgG0uuwiIWP_DCLfmQ1hD9sCx2jajo_O3O1Dcqep-VeNBSFiavsdV6bUh0857VuBB-uSB_0PFPJzowT-YTzuDSekFhZRdKPzIr-Mq69W-V7Gf9d2lpuX0rYpG5NY3BOFyvd_w27/s1600/c13.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="788" data-original-width="955" height="528" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibXbEHPtgG0uuwiIWP_DCLfmQ1hD9sCx2jajo_O3O1Dcqep-VeNBSFiavsdV6bUh0857VuBB-uSB_0PFPJzowT-YTzuDSekFhZRdKPzIr-Mq69W-V7Gf9d2lpuX0rYpG5NY3BOFyvd_w27/s640/c13.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
After creating your database user, next step is to add your ip to the Network Access option otherwise you will not be able to access this cloud cluster. Go to the Network Access and either add your ip to the list or select 'Allow Access From Anywhere' option as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDaXPBiHUsMLro2bYAm5HettMCGA7f_z7FbbCEncBCvXpUSF5nNCcQ3PQvCDo0cwTckNMabs4xckQUkjinFe3qEIi03jSFp6el8jEhj99bvFML303oAp3itZLaRCd5WO96vTjxDHwAiPUZ/s1600/c14.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="460" data-original-width="832" height="352" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDaXPBiHUsMLro2bYAm5HettMCGA7f_z7FbbCEncBCvXpUSF5nNCcQ3PQvCDo0cwTckNMabs4xckQUkjinFe3qEIi03jSFp6el8jEhj99bvFML303oAp3itZLaRCd5WO96vTjxDHwAiPUZ/s640/c14.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Once you whitelisted the ip, next step is to load some sample data to complete this exercise. Click on load sample option as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhWgy8g6EPR1iBJmI6Mlw5fuZi5jwAZeTO-fQcYYxpOwuCFgvUY8zg77UyuaLNt0KgpXoVCUvOujPo8gBMZ0-uLZL6jV2TXA06Z9ubCs9_-nOk-S-yQAF5MymYXrCLQHp9tUgB5J6cvRC_/s1600/c15.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="402" data-original-width="683" height="376" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhWgy8g6EPR1iBJmI6Mlw5fuZi5jwAZeTO-fQcYYxpOwuCFgvUY8zg77UyuaLNt0KgpXoVCUvOujPo8gBMZ0-uLZL6jV2TXA06Z9ubCs9_-nOk-S-yQAF5MymYXrCLQHp9tUgB5J6cvRC_/s640/c15.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once the sample data is loaded last step is to connect to the cluster. We have three options to interact with our db. We will choose MongoDB Compass tool. Select the third option-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw7a42VLUcoj6CUNZEX_5E6fuu3BsJ-WmKtZaBJ_j86TUaeOCoMliWV-uFj9kQO23YCbiXQL6xNJp-4MC1Yz3IYaxBSAEHxIkMxv6xTKV9Z1KMdbtKMX6GrJqhiZ0xRVCPyn2V1r5IBKMz/s1600/c17.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="716" data-original-width="834" height="547" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw7a42VLUcoj6CUNZEX_5E6fuu3BsJ-WmKtZaBJ_j86TUaeOCoMliWV-uFj9kQO23YCbiXQL6xNJp-4MC1Yz3IYaxBSAEHxIkMxv6xTKV9Z1KMdbtKMX6GrJqhiZ0xRVCPyn2V1r5IBKMz/s640/c17.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
After selecting MongoDB Compass, in next screen choose your OS and click on Download Compass button. It will start the downloading of MongoDB Compass in your system-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtb8hZjANJ0wXyCioRVaneyjtxDkYnCPfeG6MlOT_C_nELPOoa5aw9n77s32wD11uPHtaFuJ4U0Af5eILqS4Rv7HVJBvPnWVBLuOCZJSlVGl91jpcQ5YyiEv10Etueu9BKi_NONUgiHo8Q/s1600/c18.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="758" data-original-width="830" height="584" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtb8hZjANJ0wXyCioRVaneyjtxDkYnCPfeG6MlOT_C_nELPOoa5aw9n77s32wD11uPHtaFuJ4U0Af5eILqS4Rv7HVJBvPnWVBLuOCZJSlVGl91jpcQ5YyiEv10Etueu9BKi_NONUgiHo8Q/s640/c18.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Meanwhile copy the connection screen as showing in above screen and after downloading completed open the MongoDB Compass and paste there the connection string. Don't forget to update your password in this connection string. Once you entered the connection string there and connect you will see following look like screen with sample data-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipi60-AM38Su5xVvtATBraXBzhOtzLhWhbWIPM6IlklytsSYN4y7-ao6bl1NvjbfqTsJ1hYL_9BIcsXBnBJPy9dzeUIJe03vMWHRx3H4v3gqCZaApl_C5VrrxLqb7BPwWxPE49ksSGEQA8/s1600/c19.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="860" data-original-width="1551" height="353" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipi60-AM38Su5xVvtATBraXBzhOtzLhWhbWIPM6IlklytsSYN4y7-ao6bl1NvjbfqTsJ1hYL_9BIcsXBnBJPy9dzeUIJe03vMWHRx3H4v3gqCZaApl_C5VrrxLqb7BPwWxPE49ksSGEQA8/s640/c19.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
You can choose any of the sample data. To know how to connect and fetch db query in MongoDB please follow&amp;nbsp;&lt;b&gt;&lt;a href="https://www.w3schools.com/python/python_mongodb_getstarted.asp" target="_blank"&gt;this link&lt;/a&gt;&amp;nbsp;&lt;/b&gt;before moving to the next step. I will use &lt;b&gt;airbnb&lt;/b&gt; data for this exercise.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 4: Configuring Custom bot&lt;/b&gt;&lt;br /&gt;
Following the step 2, we will now create a custom bot. Select the Custom bot template and fill up the details of your bot as I have filled for my bot in below like screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicnF9OYmqq8N8sKxpiQ6XR8SebRdz-CfDf-8ew9-U9E3p3cxQ56vktunYxu96voYOaF2VSGNsOxCLxBfuBO2SKaP_ikideDlwtMeKzEf8BD3P5alQLz3CoSXM3w-Ym-eX7iWv9W87MabBs/s1600/c20.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="822" data-original-width="916" height="572" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicnF9OYmqq8N8sKxpiQ6XR8SebRdz-CfDf-8ew9-U9E3p3cxQ56vktunYxu96voYOaF2VSGNsOxCLxBfuBO2SKaP_ikideDlwtMeKzEf8BD3P5alQLz3CoSXM3w-Ym-eX7iWv9W87MabBs/s640/c20.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
In next screen, click on '+ Create Intent button' as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAwBx_4Nn4PFXHCYluKvp26oH005I-U85Dbs98YQn6Om4-pWFwwfOI3sZy29ncAcRFFKUOiWx2mh4nlBoVgGWu5NhBtZX-aJXQa0WSRsENE7LnR6dJjnOH5JM_CzpK-EOBanvRvcwFxXBZ/s1600/c21.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="438" data-original-width="796" height="352" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAwBx_4Nn4PFXHCYluKvp26oH005I-U85Dbs98YQn6Om4-pWFwwfOI3sZy29ncAcRFFKUOiWx2mh4nlBoVgGWu5NhBtZX-aJXQa0WSRsENE7LnR6dJjnOH5JM_CzpK-EOBanvRvcwFxXBZ/s640/c21.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Give a unique name to your intent and click on Add button in your next screen. Since in this exercise my goal is to show property details to the user. I have given my intent name as 'GetHouseDetails'. You will see following like screen after creating this intent-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiONigwAslkY-6J6zMlSP0y53AlMc6dTE9SxHBOKrKsiyp-yXFuImnxZCLQLrD5w2B0UQmRAJRMGIVYhku_3Z3zkUZm05uKF6F66WP3yepmq_WULj2u00mBdtdnEeLFyni2shmGKanUEC_y/s1600/c22.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="364" data-original-width="1244" height="185" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiONigwAslkY-6J6zMlSP0y53AlMc6dTE9SxHBOKrKsiyp-yXFuImnxZCLQLrD5w2B0UQmRAJRMGIVYhku_3Z3zkUZm05uKF6F66WP3yepmq_WULj2u00mBdtdnEeLFyni2shmGKanUEC_y/s640/c22.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Next, we will add some questions a user can ask in the Sample utterances section. Go ahead add some questions there as I have added like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHuHeolyi23WQmXNUIZvdRufwEEvWoEo_cw-brQ7NBGrNYVgFhHijlyAQcNfpAIoQ_Zz36qae-ejDkEzIGh1oi31fpvTi7hTj5lfm6QemBUkvQx0L4b9U18AjP-LNY9P_FqLSfOiV8rtRD/s1600/c23.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="346" data-original-width="868" height="252" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHuHeolyi23WQmXNUIZvdRufwEEvWoEo_cw-brQ7NBGrNYVgFhHijlyAQcNfpAIoQ_Zz36qae-ejDkEzIGh1oi31fpvTi7hTj5lfm6QemBUkvQx0L4b9U18AjP-LNY9P_FqLSfOiV8rtRD/s640/c23.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Next, we will add a slot type to this intent. This slot type will appear once user a question you have just added in utterances. Click on + icon in Slot types and give it a name 'PropertyType'. Add some values in this slot type like- Villa, Apartment etc. and save the slot type to the intent. Once you save this slot type, it will be added automatically under the Slot tab as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJdyEXkzHP3ktSXBP1sO7tk2h6J4vw73243nIjtH09L7RCzgRP5pKyLHL9zbpJLwJGUbgIHNkHg1ZAQhAKMPDhEAny3i-4Okp9ICHiMIDHq9om2SMckCc2OS40JJUowgUVPRvMsAGFDEs6/s1600/c24.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="209" data-original-width="1417" height="94" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJdyEXkzHP3ktSXBP1sO7tk2h6J4vw73243nIjtH09L7RCzgRP5pKyLHL9zbpJLwJGUbgIHNkHg1ZAQhAKMPDhEAny3i-4Okp9ICHiMIDHq9om2SMckCc2OS40JJUowgUVPRvMsAGFDEs6/s640/c24.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Next, fill up the Prompt screen with the bot response you want to provide and click on the Settings icon to add response card in this slot. Response card is a way in Lex to provide an option template to the user. You will see it once we complete this setup. After clicking on the Settings icon, a pop window will appear and there you need to fill the options as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheifYwIHn__uXJCEbIyHkTuiGQl4-P9hN0UtVDqoZ6tss4w3zhoswSRMKIq9j49TUFagE9FrFt4Fi1aU3h41kr3IpB8-5sLCmCnFeCeQcyex6b2weLOZ0X-oP2FGWUft4nOEHXiNMmFI7D/s1600/25.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="636" data-original-width="753" height="539" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheifYwIHn__uXJCEbIyHkTuiGQl4-P9hN0UtVDqoZ6tss4w3zhoswSRMKIq9j49TUFagE9FrFt4Fi1aU3h41kr3IpB8-5sLCmCnFeCeQcyex6b2weLOZ0X-oP2FGWUft4nOEHXiNMmFI7D/s640/25.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9wx9cGctLDkApjVuYP1mXLwKYj3-xEfXEr1ye5V0pXLE79S5jhbDfoSUm6_qKDecVw3_Vn5WAcWs3LyhdXIsdEh9CsYeZ2ciqNzy2GEEHE9paM65EkUhATmtZ7y6fWzzGCxxiXzFkxhDn/s1600/26.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="661" data-original-width="742" height="569" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9wx9cGctLDkApjVuYP1mXLwKYj3-xEfXEr1ye5V0pXLE79S5jhbDfoSUm6_qKDecVw3_Vn5WAcWs3LyhdXIsdEh9CsYeZ2ciqNzy2GEEHE9paM65EkUhATmtZ7y6fWzzGCxxiXzFkxhDn/s640/26.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
Go ahead fill the options and save the settings.Similarly we will add 1 more slot type as 'BedroomCount' and will provide it's value as 1,2 and 3-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh53oPsIS5f4z7cQAtinO9N-rjKKfxo8-ZI27ZTSTnJEHDrxW7sKvS6T2aAiMixfiIXnahaonAOja3Kx8Scy2LSBjEs4AmsBQHoAzyyzEzQGhvI4V2nO23KtPR5UeTKO61HP1Mny37ZW4r3/s1600/27.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="590" data-original-width="751" height="500" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh53oPsIS5f4z7cQAtinO9N-rjKKfxo8-ZI27ZTSTnJEHDrxW7sKvS6T2aAiMixfiIXnahaonAOja3Kx8Scy2LSBjEs4AmsBQHoAzyyzEzQGhvI4V2nO23KtPR5UeTKO61HP1Mny37ZW4r3/s640/27.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-kl-UuxqB-ZfuIXPdBP2zeBKXumzEOz4FMYwJ9jCdk6LF5SnRsKB-Dq9HG7CTHqlhBCj0Wh3UWnk-3sIuJ4ynAbW86U0q5sykviyolYAt2eZwQfaeuh73cgyJWo3G1R2lir9gPFNW94Ji/s1600/28.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="654" data-original-width="721" height="579" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-kl-UuxqB-ZfuIXPdBP2zeBKXumzEOz4FMYwJ9jCdk6LF5SnRsKB-Dq9HG7CTHqlhBCj0Wh3UWnk-3sIuJ4ynAbW86U0q5sykviyolYAt2eZwQfaeuh73cgyJWo3G1R2lir9gPFNW94Ji/s640/28.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
You can add slot types as much as you think it can help to make conversation better and complete. Now save the intent and click on build button.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 5: Creating AWS Lambda function in Python&lt;/b&gt;&lt;br /&gt;
Once we configure our bot, next step is to fetch the values from the database. In our case user will select the property type, number of bedrooms, cancellation policy and our bot will give him the total number of properties available with this criteria. This is just a sample use case for this exercise. You can make any query to db and fetch required details. Now to make lex and db interaction successful, we will create AWS lambda function in Python. This function requires some AWS specific format which you can read in&amp;nbsp;&lt;b&gt;&lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/welcome.html" target="_blank"&gt;this official link&lt;/a&gt;&amp;nbsp;.&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
I generally create a python file in PyCharm and write the required code. Please note after creating the required Python file you need to create a folder in your local system and put that Python file there. Then you need to also import all required Python libraries in that folder and compress the content of that folder for uploading in AWS Lambda function console. For this example I have used pymongo library for db connection so I have installed this library in that folder. To do so once you have created an empty folder and pasted your Python file there, open anaconda prompt with that folder location run following command:&lt;br /&gt;
&lt;b&gt;pip install --target &amp;lt;target_fodler_path&amp;gt; pymongo[tls,srv,gssapi]&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Once above command run successfully, you can see some folders and files in your target folder. Now just select all files, compress them and a zip file will appear. This is the zip file you will upload in AWS Lambda console. Don't worry about writing the AWS lambda function from scratch. For your reference I am &lt;b&gt;sharing my complete zip&lt;/b&gt; file which I have written by following best practices. You can download the complete zip file from&amp;nbsp;&lt;a href="https://drive.google.com/open?id=1WIO6W204rvIoyQoe_nhLeGN4tro8Miav" target="_blank"&gt;&lt;b&gt;this link&lt;/b&gt;&lt;/a&gt;&lt;b&gt;&amp;nbsp;.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Once you are ready with the zip file , in a new tab open Amazon Lambda console and click on Create function as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSgCXxMjWkl057opdGtjgcfDb9Sug9JHqCxntIsHqqVkU-rB1RQLYByu7X46zl0caFyJXFJFlH6odl8jTHNeATP7Bro40TD6h8forb1OG1WBQ7LhgyuM82Bc5rw-zVxyqUU5B4jfOClTaG/s1600/lambda.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="326" data-original-width="1600" height="129" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSgCXxMjWkl057opdGtjgcfDb9Sug9JHqCxntIsHqqVkU-rB1RQLYByu7X46zl0caFyJXFJFlH6odl8jTHNeATP7Bro40TD6h8forb1OG1WBQ7LhgyuM82Bc5rw-zVxyqUU5B4jfOClTaG/s640/lambda.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
In next screen fill up your function name, choose your Python version as 3.7 and click on Create function button-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5PcqBJzajvlcs2y3hUkJfRrLpnoF7syoOYMlJTNq42rWEjh_kq1Bk27gxqk9ZO5BviR8zDANZI098gVpxONHSjibJTKtYG_P09i3KHwQjy-HIgpX2_s_cPIBQ0LJV-I9AUWYG7A8Xh6tT/s1600/c25.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="834" data-original-width="1600" height="332" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5PcqBJzajvlcs2y3hUkJfRrLpnoF7syoOYMlJTNq42rWEjh_kq1Bk27gxqk9ZO5BviR8zDANZI098gVpxONHSjibJTKtYG_P09i3KHwQjy-HIgpX2_s_cPIBQ0LJV-I9AUWYG7A8Xh6tT/s640/c25.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once your function is created, in next screen select option Upload a .zip file from the Code entry type dropdown as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQEU39tamuYIPd6hgVVJKj1JxwBk200Wb2p3IR3HjJmfy5AwJY94CZ5FgSjhKd7KMuNqRuXYX16M3SZTurhE4d1jg5VWqVB5ZqvtHzxQzolqrexfEZADKKuVZJQE6bOr_dQ54kp73cMqro/s1600/c26.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="436" data-original-width="1105" height="251" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQEU39tamuYIPd6hgVVJKj1JxwBk200Wb2p3IR3HjJmfy5AwJY94CZ5FgSjhKd7KMuNqRuXYX16M3SZTurhE4d1jg5VWqVB5ZqvtHzxQzolqrexfEZADKKuVZJQE6bOr_dQ54kp73cMqro/s640/c26.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Click on Upload button and select your zip file and click on Save button. Once your zip file is uploaded there, next you need to change the Handler name. This &lt;b&gt;handler&lt;/b&gt; name &lt;b&gt;must&lt;/b&gt; starts with the same name as your Python file name. So change the name and click on save button. In my case my python file name is getpropertydetails.py so Handler name will be getpropertydetails.lambda_handler as showing in below screen&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMQ0mI0sDhXQhnuC01vL6gzs5bzLPGqzbst76Ss3sFgHpIEfvbbT3iBL7-lA1U8vOBKuGn3xgaN-cj-VDSa1mPoyo9dLAJyNAxwmaaR5VdLuBdwxWxqEAzvh1G2PrYGUS_Iy6vEAUg8SFo/s1600/c30.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="542" data-original-width="1541" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMQ0mI0sDhXQhnuC01vL6gzs5bzLPGqzbst76Ss3sFgHpIEfvbbT3iBL7-lA1U8vOBKuGn3xgaN-cj-VDSa1mPoyo9dLAJyNAxwmaaR5VdLuBdwxWxqEAzvh1G2PrYGUS_Iy6vEAUg8SFo/s640/c30.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Step 6: Enabling AWS Lambda in Lex:&lt;/b&gt;&lt;br /&gt;
Once you have completed the step 5, go back to your Lex console and select the following two options with your lambda function name:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Lambda initialization and validation&lt;/li&gt;
&lt;li&gt;Fulfillment&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhygM6gj78kIb7bfraSG_IKYNvGmCVIWpP7hwfilo-YsIhUmWL4pIMbgJeb0HSpsQN3r5zaNEVmK5DfMQ-0nEekayeAwwiIhyphenhyphen3p228UokUsLkwWqmcCmt9xiyHXMbcvBOYCcxVoUfO02uCd/s1600/c28.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="261" data-original-width="725" height="143" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhygM6gj78kIb7bfraSG_IKYNvGmCVIWpP7hwfilo-YsIhUmWL4pIMbgJeb0HSpsQN3r5zaNEVmK5DfMQ-0nEekayeAwwiIhyphenhyphen3p228UokUsLkwWqmcCmt9xiyHXMbcvBOYCcxVoUfO02uCd/s400/c28.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP-WeQcqVUJWF47JyzTATnMjDZbdLQwj5Gt7CwT0-MklU9lo4645g-yuOtdCIn3WUHBa_W41MoLgREarWwUMYiY6OCce_DjNipeJe5erIkTw87PDTqRQ-V4o7cVeJtWm_QQC6gpDc8_47R/s1600/c29.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="256" data-original-width="683" height="236" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP-WeQcqVUJWF47JyzTATnMjDZbdLQwj5Gt7CwT0-MklU9lo4645g-yuOtdCIn3WUHBa_W41MoLgREarWwUMYiY6OCce_DjNipeJe5erIkTw87PDTqRQ-V4o7cVeJtWm_QQC6gpDc8_47R/s640/c29.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Click on Save Intent and then click on the Build button. Once build is successful, you can check your bot in Test bot window as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBD4gg2hRnyxGMcqudfVpATu13Ah8Wfd1jYEkX3d3aFrqf1m7ybsPlB-tijLNCfLzqfNQLqo3skNsy8H6RfE2VvroCWe6s1d1mUJyTK9zXcRQUOUjja_YSvD-8JP0E418xEhOHfijGk1K7/s1600/c31.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="697" data-original-width="497" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBD4gg2hRnyxGMcqudfVpATu13Ah8Wfd1jYEkX3d3aFrqf1m7ybsPlB-tijLNCfLzqfNQLqo3skNsy8H6RfE2VvroCWe6s1d1mUJyTK9zXcRQUOUjja_YSvD-8JP0E418xEhOHfijGk1K7/s400/c31.JPG" width="283" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Quite interesting right! All hard work you have done till now, is paying you. Your bot is working as expected and you are now ready to integrate it with an actual web app.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Step 7: Integrate Lex bot with a web app and deploy in cloud:&lt;/b&gt;&lt;br /&gt;
You have created and tested your bot till step 6. Now you want to show it to your boss or client. You cannot show your test bot instead it should be shown either integrated with an app or a web page. Since I am not a mobile or app developer I will choose the web page integration. For this purpose AWS has it's own solution. With this in built solution not only you can integrate your bot with a working web page also you can deploy your bot in AWS cloud. So first publish your bot by clicking on Publish button in lex console, type an alias name and click on Publish button as showing in below screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwbXeyroTuLsCbxW1BNH29lgB8DSM0hjVRZspdIMgSzlQXJXjzQh1A5JYIibYek6YBN61WQhgip68JAcmtqR907usl28af6oGbJ3Ty-Zzkg-xrvJqXziqVrbMoL_pkOMhvwwsyf6HJRiqP/s1600/c32.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="354" data-original-width="983" height="228" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwbXeyroTuLsCbxW1BNH29lgB8DSM0hjVRZspdIMgSzlQXJXjzQh1A5JYIibYek6YBN61WQhgip68JAcmtqR907usl28af6oGbJ3Ty-Zzkg-xrvJqXziqVrbMoL_pkOMhvwwsyf6HJRiqP/s640/c32.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once your bot is published, you will see following screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3mGTp9ep1JHrEe4v456SYL92nwQBZqGL44E5a0Cusjt1Jht22tr5EvEVFf65DshzlXYmaIoQJN0EwxnIaCD2yyrLSVc07ivUoZGPiX8NOvOutGeKSOA7awT5Wk8N7FCdtbU-HXPm5T3Wd/s1600/c33.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="622" data-original-width="974" height="408" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3mGTp9ep1JHrEe4v456SYL92nwQBZqGL44E5a0Cusjt1Jht22tr5EvEVFf65DshzlXYmaIoQJN0EwxnIaCD2yyrLSVc07ivUoZGPiX8NOvOutGeKSOA7awT5Wk8N7FCdtbU-HXPm5T3Wd/s640/c33.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
If you are a mobile developer you can click on Download connection info button and if you want to integrate your bot with Facebook, Messenger, Slack etc click on Go to channels buttons otherwise close this window. For integrating with a web page, open AWS CloudFormation in a new tab and click on Create stack button-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAo27eKgScYgrKggy2PjViScwRj1KCjahbrJem0YXWlhGiYFQLC293nExKPwpGKiAlDa0HmnYA4igks6CJd9_jkyHfcqsay0GHiO7N2OWIoJyob-ypkdi8Bm_0g-GrFZfNtdJ7H2v9rSga/s1600/c34.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="348" data-original-width="1244" height="111" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAo27eKgScYgrKggy2PjViScwRj1KCjahbrJem0YXWlhGiYFQLC293nExKPwpGKiAlDa0HmnYA4igks6CJd9_jkyHfcqsay0GHiO7N2OWIoJyob-ypkdi8Bm_0g-GrFZfNtdJ7H2v9rSga/s400/c34.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Select the first option Template is ready. I am providing you my template which you can download from&amp;nbsp;&lt;b&gt;&lt;a href="https://drive.google.com/open?id=1c4m5ZLMZ2AYrhCcopbYI2XgePLgto4Rj" target="_blank"&gt;this link&lt;/a&gt;&amp;nbsp;. &lt;/b&gt;Use the same template and click on Next-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg52TKiA2RIK65ySur3_RKN83y2uY1_jXk6m7ey3jPiND_ZhoX5HZVDPWYdSrgbSN6a-2OzyLW_JSOrv-XC_KEUgRRsWD6Fkd9PjD7K-MfTL_RJGqJ_TUOr3yXFeWQudS1d1ncC8cFlLRkp/s1600/l1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="798" data-original-width="1275" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg52TKiA2RIK65ySur3_RKN83y2uY1_jXk6m7ey3jPiND_ZhoX5HZVDPWYdSrgbSN6a-2OzyLW_JSOrv-XC_KEUgRRsWD6Fkd9PjD7K-MfTL_RJGqJ_TUOr3yXFeWQudS1d1ncC8cFlLRkp/s640/l1.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
In next screen give any name in Stack name field, enter your lex bot name in BotName field, enter bot's alias name in BotAlias field. Leave all fields as default and click on Next button. In next screen also click on Next button. Select both check boxes in next screen and click on Create stack button-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijS0glDL8RORSqyYFoEwbfUVIz2Oyu-ypzKPBqkqcpFxoz91PlmP2QOSgnu0Sf8FPnRug-YfZelbxmowph7K16y3dwmr2lnsJcRwIZ8eT7KbYPEZoaLZiCG5JzIOxUA8o3BDIBHEulf8oU/s1600/l2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="455" data-original-width="1288" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijS0glDL8RORSqyYFoEwbfUVIz2Oyu-ypzKPBqkqcpFxoz91PlmP2QOSgnu0Sf8FPnRug-YfZelbxmowph7K16y3dwmr2lnsJcRwIZ8eT7KbYPEZoaLZiCG5JzIOxUA8o3BDIBHEulf8oU/s640/l2.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
You will see following like screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqockXg-tS6RANELWYVF80uH1VzM7e4r8gF0XBnCdaL8EUKRzQIItFjyakdSBVF2bBVyq_bqVS0f40udcuQsv2192olxPDefiRyMnmkpyhKgT214_PFEYVCZ7M-5qBbk34-N6qS7xY1Upu/s1600/l3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="425" data-original-width="1179" height="228" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqockXg-tS6RANELWYVF80uH1VzM7e4r8gF0XBnCdaL8EUKRzQIItFjyakdSBVF2bBVyq_bqVS0f40udcuQsv2192olxPDefiRyMnmkpyhKgT214_PFEYVCZ7M-5qBbk34-N6qS7xY1Upu/s640/l3.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
Wait till it's status appears as CREATE_COMPLETED. Once you see this status go to the Outputs tab and you will see following look like screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZ0fF5eznN5TK03SolLJLEGiw5ZNYdebYSi7vukt9hqCtTrixf94eto869ryuoBGQ9LD_2Smc5Gbb2ysm74UIluBm06qAcHioysuCDds-mFZab2_g4HbFhfs3LX7WEtHzdRyiAM4_YYmaf/s1600/l4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="588" data-original-width="1188" height="315" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZ0fF5eznN5TK03SolLJLEGiw5ZNYdebYSi7vukt9hqCtTrixf94eto869ryuoBGQ9LD_2Smc5Gbb2ysm74UIluBm06qAcHioysuCDds-mFZab2_g4HbFhfs3LX7WEtHzdRyiAM4_YYmaf/s640/l4.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
That's it you have successfully deployed your chatbot web app in AWS cloud. Open the last link mentioned as &lt;b&gt;WebAppUrl &lt;/b&gt;in a new tab and your chatbot deployed as web page will appear. Go ahead and ask some question to your property agent-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRWkuF73NeYyQWMPKdJkkP9Bz95avsKp2LZH8bL1QBvG1kS5QRrJeqwkb4V9KEfDrwr07gKRB0dG53lXWvM929hwM_sfTEZIs4e2WGF1IZWaQ-y0ggKejcsddJWgNJVCgVLMRIw3U_vI34/s1600/l5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="837" data-original-width="1600" height="334" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRWkuF73NeYyQWMPKdJkkP9Bz95avsKp2LZH8bL1QBvG1kS5QRrJeqwkb4V9KEfDrwr07gKRB0dG53lXWvM929hwM_sfTEZIs4e2WGF1IZWaQ-y0ggKejcsddJWgNJVCgVLMRIw3U_vI34/s640/l5.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
You have come so long! In this blog post not only you learned about Amazon Lex configuration but also some advanced tasks like creating AWS Lambda function, integration with MongoDB using Python and deploying your bot as a web page in AWS cloud. After completing this exercise not only you have completed an end to end real world project but also you have gained a rare skill sets. But don't stop here. Make your bot more mature by adding more slot types, db queries etc and help a customer to find out the best available Airbnb property for him/her.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhC3BtinlwKWBOffyDTnqke4v87olPzWmiwZVsX_6N8bDDC_x7HtIN-7jcvenwxgEKltiHO5-pXfjlZaxrhidXNNWzXuKo4jNq4dTQIoCa6Ea5M9koHzPMToyEnbHxpXI5bcQp-1MkERj3x/s72-c/c1.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">13</thr:total></item><item><title>How to achieve maximum parallel processing capabilities with XGBoost-1.0.0?</title><link>https://dsbyprateekg.blogspot.com/2020/02/how-to-achieve-maximum-parallel.html</link><category>k-fold cross validation</category><category>multi-threading</category><category>xgboost</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Fri, 21 Feb 2020 16:36:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-5347031586129299074</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvJDnWOBiHA3mfoLns0ghJ2MLqz2MHT8gkAzgyCNKWCc_atQCGcDyaRB7b22-uJh-Vw7jHlZem6qtN_O14LgrvgIQVyMMPF5q6qxrblCK0yLgMmiJIDyRkn13IaQpEEZwZw7gamHH3z02r/s1600/download.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="166" data-original-width="304" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvJDnWOBiHA3mfoLns0ghJ2MLqz2MHT8gkAzgyCNKWCc_atQCGcDyaRB7b22-uJh-Vw7jHlZem6qtN_O14LgrvgIQVyMMPF5q6qxrblCK0yLgMmiJIDyRkn13IaQpEEZwZw7gamHH3z02r/s1600/download.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
Another post starts with you beautiful people!&lt;br /&gt;
XGBoost is an optimized distributed gradient boosting library designed to be highly efficient, flexible and portable. It implements machine learning algorithms under the Gradient Boosting framework. XGBoost provides a parallel tree boosting (also known as GBDT, GBM) that solve many data science problems in a fast and accurate way. Recently XGBoost is released with it's newer version 1.0.0 which has improvements like performance scaling for multi core CPUs, improved installation experience on Mac OSX, availability of distributed XGBoost on Kubernates etc. In this post we are going to explore it's multi processing capabilities on a real world ml problem&amp;nbsp;&lt;b&gt;&lt;a href="https://www.kaggle.com/c/otto-group-product-classification-challenge" target="_blank"&gt;Otto Group Product Classification Challenge&lt;/a&gt;. &lt;/b&gt;In the end of the post I will share my kaggle kernel link also so that you can explore my complete code.&lt;br /&gt;
&lt;br /&gt;
Once you go to the challenge link in Kaggle and start your kernel, first you need to enable the Internet option in the notebook since current version of XGBoost installed in kernel notebook is 0.90. So for upgrading it run the following command in your anaconda prompt-&lt;br /&gt;
&lt;b&gt;pip install --upgrade xgboost &lt;/b&gt;or&amp;nbsp;&lt;b&gt;!pip install --upgrade xgboost&lt;/b&gt; in your kaggle kernel. Screenshot of Anaconda prompt is as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHv2bA1c1LFebro0Ru57Guza7I_nqRbwVT2oW6IBa-KvY1XcU6ghdSMJf6PyB0RxOIqGIzWUa78X16edauHsyJlfcb9Y3P1Xvf34U57Jbc5PYod-sTsnpmdZdvFly5YEpopOL4HbipcsWC/s1600/1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="50" data-original-width="555" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHv2bA1c1LFebro0Ru57Guza7I_nqRbwVT2oW6IBa-KvY1XcU6ghdSMJf6PyB0RxOIqGIzWUa78X16edauHsyJlfcb9Y3P1Xvf34U57Jbc5PYod-sTsnpmdZdvFly5YEpopOL4HbipcsWC/s1600/1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
And screen shot of my kernel is as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1j5SNiAH3Qf3LNHx2eFmcXWnHkppkBtyZFWh-qgcKWHMdRg7V3J99vBUqSbqEyFqTCXGSzrXRC7sMtEibPMbyja6AdJXcXTPIumJx3sh_sZSZEsuK0uDyfstCm6g95J71TtvvmbFtbaWX/s1600/3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="76" data-original-width="394" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1j5SNiAH3Qf3LNHx2eFmcXWnHkppkBtyZFWh-qgcKWHMdRg7V3J99vBUqSbqEyFqTCXGSzrXRC7sMtEibPMbyja6AdJXcXTPIumJx3sh_sZSZEsuK0uDyfstCm6g95J71TtvvmbFtbaWX/s1600/3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
After running above command you will see following screen showing successful installation of the library-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXtBTFbq3M5xQPfk1SxCbaRyjL_ZQKaIo_CATHo_VN4-nB3Pi6FzsJc3l9cL87GVSm8mGrr7PGg7GVmz42prtFTTFXZbLoItQw8h3e6e9PB0tk-E5uyG0qzJZ0Xajds3LzRtY95z-fx720/s1600/2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="279" data-original-width="654" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXtBTFbq3M5xQPfk1SxCbaRyjL_ZQKaIo_CATHo_VN4-nB3Pi6FzsJc3l9cL87GVSm8mGrr7PGg7GVmz42prtFTTFXZbLoItQw8h3e6e9PB0tk-E5uyG0qzJZ0Xajds3LzRtY95z-fx720/s1600/2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Let's import the dataset-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmzpdhSaB-OsB6FrYdKUdEU4v1Mi7Qa9C0jmuotIOcP0OzGRa8a5GwK1qqk_3uTvU3vp07srSt9Ro3DxdddBwgdiaA_-mH_-5uUHSCHb0fSbOVPW0CUzlw5PoWMkCe8C9NlMT7E8mI4_E-/s1600/4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="213" data-original-width="1009" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmzpdhSaB-OsB6FrYdKUdEU4v1Mi7Qa9C0jmuotIOcP0OzGRa8a5GwK1qqk_3uTvU3vp07srSt9Ro3DxdddBwgdiaA_-mH_-5uUHSCHb0fSbOVPW0CUzlw5PoWMkCe8C9NlMT7E8mI4_E-/s1600/4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
This dataset describes the 95 details of 61,878 products grouped into 10 product categories (e.g. fashion, electronics, etc.). Input attributes are counts of different events of some kind. The goal is to make predictions for new products as an array of probabilities for each of the 10 categories and models are evaluated using multi-class logarithmic loss (also called cross entropy).&lt;br /&gt;
&lt;br /&gt;
Please note that we get multi-threading support by default with XGBoost. But depending on our Python environment (e.g. Python 3) we may need to explicitly enable multi-threading support for XGBoost. We can confirm that XGBoost multi-threading support is working fine by building a&lt;br /&gt;
number of different XGBoost models, specifying the number of threads and timing how long it takes to build each model. The trend will both show us that multi-threading support is enabled and give us an indication of the effect it has when building models. Below is the code snippet showing how can you check this-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBaa6iWI_Qdk5XroPaMYFi2KNEXJ30aJyA2Ee1BwK85lMsGwErUaucteHYUefGupA-thyShhwzb9o_SgpoVjG_bYNgDG37iY4o_Cy5izskZbBX28GJMs-jwKI_v1ElScWUBV78Fpl_jHsW/s1600/5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="416" data-original-width="510" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBaa6iWI_Qdk5XroPaMYFi2KNEXJ30aJyA2Ee1BwK85lMsGwErUaucteHYUefGupA-thyShhwzb9o_SgpoVjG_bYNgDG37iY4o_Cy5izskZbBX28GJMs-jwKI_v1ElScWUBV78Fpl_jHsW/s1600/5.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
You can update the number of threads list based on your system configuration. &lt;b&gt;Recommended way to set number of threads (nthread) is it should be equal to the number of physical CPU cores in your machine&lt;/b&gt;. After running the above code cell I am getting following result-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHx1EWp8zPJ6S7ktTJDI9HpZ77Ktx1b6BVyTvvFU4cj6fYtTcbTqV0vmIOZ4f3jtBjTmXzsqyNNVHajTXmOYFK_mNUjFgNJtnrLGDDKhyt_gkSlW4MRXHUdrpakPtbJzy7J8Y9xizB-jM1/s1600/6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="136" data-original-width="262" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHx1EWp8zPJ6S7ktTJDI9HpZ77Ktx1b6BVyTvvFU4cj6fYtTcbTqV0vmIOZ4f3jtBjTmXzsqyNNVHajTXmOYFK_mNUjFgNJtnrLGDDKhyt_gkSlW4MRXHUdrpakPtbJzy7J8Y9xizB-jM1/s1600/6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Also we can plot the above trend in following way-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHFRWvM84hXWBeCL87I3OX5FnPLeUXcrzg_0ApqYJ_F2YWin3migrBZ3k0D3Yw_4hEE38LiLbmRcSuMxiGgfw-zXg5GBfpFZmHVp-IiVidrl6pXIwAEFsreOzEzCgLIVA_MpY1jtZvI0vD/s1600/8.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="184" data-original-width="664" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHFRWvM84hXWBeCL87I3OX5FnPLeUXcrzg_0ApqYJ_F2YWin3migrBZ3k0D3Yw_4hEE38LiLbmRcSuMxiGgfw-zXg5GBfpFZmHVp-IiVidrl6pXIwAEFsreOzEzCgLIVA_MpY1jtZvI0vD/s1600/8.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_hKTzsvIKqGYt8GsBTcFqwU6dCEd1hkp1xxBxxBFBfIr36kgi4FrTu95ny1rqtU7HmSRjWscorG2erXHERz4we30DNmOmKCV_2XYmZtRfoFReTBUfoOnZI5rCsM3oAgSW8P9ALS-InOy6/s1600/7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="362" data-original-width="545" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_hKTzsvIKqGYt8GsBTcFqwU6dCEd1hkp1xxBxxBFBfIr36kgi4FrTu95ny1rqtU7HmSRjWscorG2erXHERz4we30DNmOmKCV_2XYmZtRfoFReTBUfoOnZI5rCsM3oAgSW8P9ALS-InOy6/s1600/7.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
From above plot we can see a nice trend in the decrease in execution time as the number of threads is increased. You can run the same code on a machine with a lot more cores and decrease the model training time. Now you know how to configure the number of threads with XGBoost in your machine. But there is one more important thing we can do as tuning. We always do cross validation to avoid overfiting in our model and this steps also time consuming. So is there any way to tune this process. Answer is absolutely yes! We can enable the multi-threading in both XGBoost as well as in cross validation.&lt;br /&gt;
&lt;br /&gt;
The k-fold cross-validation also supports multi-threading. For example the n_jobs argument on the cross val score() function allows us to specify the number of parallel jobs to run. By default, this is set to 1, but can be set to -1 to use all of the CPU cores on our system. In following code snippet we will check three configurations with cross validation and XGBoost to achieve multi-threading and then compare the output of all three configurations-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoLWXE_FcGAVDXyG3ah7PDAp2KobBw3BYIvF_ByijAxSffVrNF282ewQdpht7XnH6LGPLLYgBdYEn-lRUkJj2kcXXjsm9aw8uFSVdXX_Pp4bMQYOD1284TkPjet96V8wtuCy1s7EYWrGkQ/s1600/9.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="605" data-original-width="862" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoLWXE_FcGAVDXyG3ah7PDAp2KobBw3BYIvF_ByijAxSffVrNF282ewQdpht7XnH6LGPLLYgBdYEn-lRUkJj2kcXXjsm9aw8uFSVdXX_Pp4bMQYOD1284TkPjet96V8wtuCy1s7EYWrGkQ/s1600/9.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
In above code snippet you can see only configurable parameter required for multi-threading is nthread in XGBoost and is n_jobs in cross validation. For example only I am using 1 thread but you can change it to your no. of cores. After running above cell the best result is achieved by enabling multi-threading within XGBoost and not in cross-validation as you can see in below screen shot also-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZIDZ0GETPp1Iiklp0LWvMTNDk16A2YbPrrPMo9kjkhwYiIBO2QXyQl78A7fvLedfhovhze_JCH8HP-Mq0HM2uNhjO8C67zTxOVoxN_7wMH7KsZw7OtHI2wmq_pkWQaPWs38MJNbD-86Pr/s1600/10.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="85" data-original-width="560" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZIDZ0GETPp1Iiklp0LWvMTNDk16A2YbPrrPMo9kjkhwYiIBO2QXyQl78A7fvLedfhovhze_JCH8HP-Mq0HM2uNhjO8C67zTxOVoxN_7wMH7KsZw7OtHI2wmq_pkWQaPWs38MJNbD-86Pr/s1600/10.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
So if you are going to to do cross validation with any library other than XGBoost 1.0.0, don't forget to enable the multi-treading feature in cross validation and if you are going to use XGBoost which you should be, don't forget to check no. of threads and enabling multi-threading within XGBoost. For the complete solution, you can find my kernel in following link:&amp;nbsp;&lt;a href="https://www.kaggle.com/iamprateek/exploring-multi-processing-with-xgboost-1-0-0" target="_blank"&gt;&lt;b&gt;my kernel&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;
Fork my kernel and start experimenting with it and if you like to learn more about XGBoost, follow the tutorials of this amazing guy:&amp;nbsp;&lt;a href="https://machinelearningmastery.com/develop-first-xgboost-model-python-scikit-learn/" target="_blank"&gt;Jason Brownlee PhD&lt;/a&gt;.Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvJDnWOBiHA3mfoLns0ghJ2MLqz2MHT8gkAzgyCNKWCc_atQCGcDyaRB7b22-uJh-Vw7jHlZem6qtN_O14LgrvgIQVyMMPF5q6qxrblCK0yLgMmiJIDyRkn13IaQpEEZwZw7gamHH3z02r/s72-c/download.jpg" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">7</thr:total></item><item><title>How can I become a TPU expert?</title><link>https://dsbyprateekg.blogspot.com/2020/02/how-can-i-become-tpu-expert.html</link><category>kaggle</category><category>Tensorflow 2.1</category><category>TPU</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Fri, 14 Feb 2020 18:00:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-6460641143881902820</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiB5AwHeBHdjA8e-btI5N3Wu_6XS3wegxSAzKLkWI3UsrfhftMlVTigIqlDcLVn7T0dY8_UltUuTUPNf82SrcoV82VtQA0CZoLr79R8U55gdsve2u_SpyKJmAmuDpi45IM27AU5a_Wjc44/s1600/tpu_kaggle.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="428" data-original-width="1584" height="172" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiB5AwHeBHdjA8e-btI5N3Wu_6XS3wegxSAzKLkWI3UsrfhftMlVTigIqlDcLVn7T0dY8_UltUuTUPNf82SrcoV82VtQA0CZoLr79R8U55gdsve2u_SpyKJmAmuDpi45IM27AU5a_Wjc44/s640/tpu_kaggle.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div&gt;
Another post starts with you beautiful people!&lt;/div&gt;
&lt;div&gt;
I have two good news for all of you! First good news is that Tensorflow has released it's new version (TF 2.1) which is focused on TPUs and the most interesting thing about this release is that it now also supports Keras high level API. And second wonderful news is to help us get started Kaggle has launched a &lt;a href="https://www.kaggle.com/c/flower-classification-with-tpus/overview" target="_blank"&gt;&lt;b&gt;TPU Playground Challenge&lt;/b&gt;&lt;/a&gt;. This means there is no any way to stop you learning &amp;amp; using TPUs. In this post I am going to share you how to configure and use TPUs while solving a image classification problem.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;What are TPUs?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
You must have heard about TPU while using&amp;nbsp;&lt;a href="https://colab.research.google.com/" target="_blank"&gt;Google Colab&lt;/a&gt;. Now Kaggle&amp;nbsp;also supports this hardware accelerator. TPUs or Tensor Processing Units are hardware accelerators specialized in deep learning tasks. They were created by Google and have been behind many cutting edge results in machine learning research. Kaggle Notebooks are configured with TPU v3-8s, which is a specialized hardware with 4 dual-core TPU chips for a total of 8 TPU cores. We can enable this accelerator in our Kaggle notebook by selecting&amp;nbsp;&amp;nbsp;'TPU v3-8' option in Accelerator menu.&amp;nbsp;We can use up to 30 hours per week of TPUs and up to 3 hours at a time in a single session.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;How to enable TPU training in Tensorflow Keras?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Once we enable the TPU accelerator, next step is to enable the TPU training with Keras. You just need to first import the Tensorflow library and execute the following code to enable this-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicXAnISYCHByle4r4HNnU89dfQl-VewsTJlYTPZTLfFtqVy-G_HLbN4xLOhsITc2EjJT5JwOmt90piKMpKYJ9iXCLMkpVr9JfTjT_B2EHaSQ2O1njv0gUd3F7tBsRLY36wHyd5Ywab3Ui-/s1600/tpu1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="438" data-original-width="789" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicXAnISYCHByle4r4HNnU89dfQl-VewsTJlYTPZTLfFtqVy-G_HLbN4xLOhsITc2EjJT5JwOmt90piKMpKYJ9iXCLMkpVr9JfTjT_B2EHaSQ2O1njv0gUd3F7tBsRLY36wHyd5Ywab3Ui-/s1600/tpu1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Here TPUClusterResolver() locate the TPUs, TPUStrategy object contains the necessary distributed training code that will work on TPUs with their 8 compute cores. Finally, we use the TPUStrategy by instantiating our model in the scope of the strategy. This creates the model on the TPU. Model size is constrained by the TPU RAM only, not by the amount of memory available on the VM running our Python code. Model creation and model training use the usual Keras APIs.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;TPUs and GCS?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
TPUs read training data exclusively from Google Cloud Storage (GCS). Format of this training data in GCS is&amp;nbsp;TFRecords. The TFRecord format is a container format frequently used in Tensorflow to group and shared data files for optimal training performance. Each file contains the id, label (the class of the sample, for training data) and img (the actual pixels in array form) information for many images. On Kaggle we can also use &lt;b&gt;KaggleDatasets().get_gcs_path()&lt;/b&gt; to obtain the GCS path of a Kaggle dataset.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;How to load a dataset from TFRecords file?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
We can load a dataset from TFRecords files by using following code snippet:-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfUhwH_9Rs9fF8UDi0tJ6zBin8wwSlKDK4ZRWmdy76a7HFhSPu5doAhY8By7HMF-YbItFdbSNXXkWWzBLwIUwx28r7S3uHTXvH4EsyxLfLuusfZYSV-w88knU7r4tEH4QUparoZeP6zFQP/s1600/tpu2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="102" data-original-width="697" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfUhwH_9Rs9fF8UDi0tJ6zBin8wwSlKDK4ZRWmdy76a7HFhSPu5doAhY8By7HMF-YbItFdbSNXXkWWzBLwIUwx28r7S3uHTXvH4EsyxLfLuusfZYSV-w88knU7r4tEH4QUparoZeP6zFQP/s1600/tpu2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
From multiple TFRecords we can also enable parallel streaming with below code snippet:&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifMUwx-1iXgNIGYuio83rVrGBR0qcZdJMsFIKkqeucfOXczuGFWl_pn6lehg0l9_95WM478emNhC_tRSmG90MGK7BGSHAIZ6glOjaGdAXAde6tE23KuDMI7d4mwhoXr-Ek8xMd5blatHyZ/s1600/tpu3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="294" data-original-width="801" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifMUwx-1iXgNIGYuio83rVrGBR0qcZdJMsFIKkqeucfOXczuGFWl_pn6lehg0l9_95WM478emNhC_tRSmG90MGK7BGSHAIZ6glOjaGdAXAde6tE23KuDMI7d4mwhoXr-Ek8xMd5blatHyZ/s1600/tpu3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Above three code snippets are only required to use TPUs. Now I will show you how can we use TPUs while solving a 100 flower classification problem. I have tried different architectures with pretrained weights- VGG16, VGG19, ResNet, DenseNet etc and found best result (0.92313) with DenseNet201 with default learning rate. With a custom learning rate function I even reached a public score of 0.93205. In the end of the post I will share my Kaggle notebook link so that you can check it and also fork it to explore with any new model or settings. Let's start the steps I have followed to solve the problem-&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;1. Initiate and enable TPUs:&lt;/b&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8slkN1v7r4LWJ7Z87iqbGKyV8ROKZRR61UmfWxiLf64-PItGsVSD7CLOZuOZnReZnHkjW2peyaH_xs4phn7bNzQxJnKmcyQ6ThFjhOcN1c6NddRLCCbVFb7SWejDg33S7cXhI_PoD18WF/s1600/t1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="663" data-original-width="844" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8slkN1v7r4LWJ7Z87iqbGKyV8ROKZRR61UmfWxiLf64-PItGsVSD7CLOZuOZnReZnHkjW2peyaH_xs4phn7bNzQxJnKmcyQ6ThFjhOcN1c6NddRLCCbVFb7SWejDg33S7cXhI_PoD18WF/s1600/t1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;2. Load the Datasets: &lt;/b&gt;In this playground challenge,&amp;nbsp;we're classifying 104 types of flowers based on their images drawn from five different public datasets and images are provided in TFRecord format. The dataset is already divided into train, validation and test sets. Each set contains 4 types of images having dimension of 192x192, 224x224, 331x331 and 512x512. We will load the datasets from GCS in following way:&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4EOrf98PnMTwkextYKFYwhNO-2ezSHbrmtQpoCnNhJ7-2jHqqQvxEPk8OBjQWlSLq8zSOwPMEAUxuT07qWchMUXbyFgnK2ZCjpWDH3-e11TgYjcYze7pk36Zn0EnnCY46KC-BCbBkDc9W/s1600/t2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="679" data-original-width="801" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4EOrf98PnMTwkextYKFYwhNO-2ezSHbrmtQpoCnNhJ7-2jHqqQvxEPk8OBjQWlSLq8zSOwPMEAUxuT07qWchMUXbyFgnK2ZCjpWDH3-e11TgYjcYze7pk36Zn0EnnCY46KC-BCbBkDc9W/s1600/t2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
Here I am using 224x224 dimension images, You can change it to any of the given dimension. After selecting the dataset we can use Tensorflow's io.gfile.glob() function to load the datasets. In above way you can load any TFRecord format files. Next step is to wrap your model initialization and compilation code in the TPU strategy scope. In my initial try I experimented with various pretrained weights of Keras API. I am here showing the best one.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;3. Compile model with TPU: &lt;/b&gt;Model initialization, compiling and training step is same as you did while using keras API. Only different point here is to wrap our code inside TPU strategy scope. Below is code snippet of the same I followed:&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikUYx76YlSeuutGO7LcuaALJgYx2gRZCb9VuDbsBeZezT9oIVcvzf4el5PLF6tnUw6kdIthvi5TeEUrSsbza9Z5jGpKyxYTOIyg2f-T1sBb8KvU7KMrRC_YhnJsDYElQODWslWSxhXtJkt/s1600/t3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="632" data-original-width="1027" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikUYx76YlSeuutGO7LcuaALJgYx2gRZCb9VuDbsBeZezT9oIVcvzf4el5PLF6tnUw6kdIthvi5TeEUrSsbza9Z5jGpKyxYTOIyg2f-T1sBb8KvU7KMrRC_YhnJsDYElQODWslWSxhXtJkt/s1600/t3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;4. Train the model: &lt;/b&gt;Training of the model is as same as we did with any of Keras API. We need to call .fit() function with required parameters. below is code snippet of the same:&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkOkolplwfaOJsCM-hO65D8Sy54ewbW-YEWgyiZiY8bPmVLxpxeu9z-XkYJZ3RcKy3ulHOaWS92fCTLDj0Ji_YFWz3_DVRy40afdWSGz6bi2hb7NYAUK84sE1an3JFvlYsd_gIXXUnMdrM/s1600/t4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="194" data-original-width="720" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkOkolplwfaOJsCM-hO65D8Sy54ewbW-YEWgyiZiY8bPmVLxpxeu9z-XkYJZ3RcKy3ulHOaWS92fCTLDj0Ji_YFWz3_DVRy40afdWSGz6bi2hb7NYAUK84sE1an3JFvlYsd_gIXXUnMdrM/s1600/t4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Once my model training was completed. I calculated the confusion matrix and got below result:&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3bK3tWezfq13G9be5_9fZKWWziJJN5xscDAQruGMDtO2VjE_2AqiSBkQZCGUKAxRZnJRabiDctMUKFNOqGAl4NBMfDm1qGW5AWvAaKWnkxLjJVNaEdjmk823zx4Xewod5nnHZxkZlHFT0/s1600/t5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="74" data-original-width="546" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3bK3tWezfq13G9be5_9fZKWWziJJN5xscDAQruGMDtO2VjE_2AqiSBkQZCGUKAxRZnJRabiDctMUKFNOqGAl4NBMfDm1qGW5AWvAaKWnkxLjJVNaEdjmk823zx4Xewod5nnHZxkZlHFT0/s1600/t5.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
When I submit my final submission file I got&amp;nbsp;0.93205 public score while the top score is&amp;nbsp;0.96652 which means there are many improvement still left. I am still trying. What about you? You don't need to type the above mentioned code manually. Just click on 'Copy and Edit' button in my notebook:&amp;nbsp;&lt;b&gt;&lt;a href="https://www.kaggle.com/iamprateek/flower-classification-with-tpu" target="_blank"&gt;my notebook url&lt;/a&gt;&amp;nbsp;&lt;/b&gt;and start experimenting with new things till you get the maximum score.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiB5AwHeBHdjA8e-btI5N3Wu_6XS3wegxSAzKLkWI3UsrfhftMlVTigIqlDcLVn7T0dY8_UltUuTUPNf82SrcoV82VtQA0CZoLr79R8U55gdsve2u_SpyKJmAmuDpi45IM27AU5a_Wjc44/s72-c/tpu_kaggle.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">4</thr:total></item><item><title>How I 'Dockerized' my deep learning project?</title><link>https://dsbyprateekg.blogspot.com/2019/12/how-i-dockerized-my-deep-learning.html</link><category>Docker</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Sat, 28 Dec 2019 20:14:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-2947200841726286761</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidwxIW-rrZkA9bCfb5MJrW8HVp9YXUhLdKOHnAcyA0MRc077ow3LRgH58fW9vI7mGVaL2X5AK82mT6SyN-Q03EPQM0JMXRM4MG5pgRSIhADFXucK839BQnyp6SETOtML49SshhMMRTW7jm/s1600/new5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="165" data-original-width="306" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidwxIW-rrZkA9bCfb5MJrW8HVp9YXUhLdKOHnAcyA0MRc077ow3LRgH58fW9vI7mGVaL2X5AK82mT6SyN-Q03EPQM0JMXRM4MG5pgRSIhADFXucK839BQnyp6SETOtML49SshhMMRTW7jm/s1600/new5.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
Another post starts with you beautiful people!&lt;br /&gt;
I hope you have enjoyed my last two posts about&amp;nbsp;&lt;b&gt;&lt;a href="http://dsbyprateekg.blogspot.com/2019/12/how-can-i-install-and-use-darknet.html" target="_blank"&gt;Yolo&lt;/a&gt;&amp;nbsp;&lt;/b&gt;system&lt;b&gt;&amp;nbsp;&lt;/b&gt;and now you are well aware of using Yolo with kers api as well as Yolo with Darknet framework in your Windows machine. I know Linux is the most suitable environment for production ready configurations, but like me there are many Windows users who find sometime very difficult to setup an environment. In my previous post I have shared my&amp;nbsp;&lt;a href="http://dsbyprateekg.blogspot.com/2019/11/how-to-detect-object-in-real-time-using.html" target="_blank"&gt;&lt;b&gt;Python setup requirements&lt;/b&gt;&lt;/a&gt;&amp;nbsp;which you can use to setup Python computer vision environment in Windows. Continuing my knowledge sharing journey, today I am going to share you how can we setup and use Docker with our Python project.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;What is Docker?&lt;/b&gt;&lt;br /&gt;
Generally in our Python project structure we use a requirements.txt file where we define Python library name as key and it's version as value. This file is used to install required Python libraries when we deploy our code to new environment ( for example from Windows to Linux environment). We just need to run 'pip install -r requirements.txt' command to install all dependencies in new environment and our application will be run in that new environment. But this process creates issue specially if you have installed Windows specific versions of different libraries and trying to use the same in Linux environment. In solving such dependency issues, we spent our valuable time. What if we can remove this issue? Is there any way of sharing and running our application in any environment without need of handling dependencies?&lt;br /&gt;
&lt;br /&gt;
Yes,&amp;nbsp;&lt;b&gt;&lt;a href="https://docs.docker.com/install/" target="_blank"&gt;Docker&lt;/a&gt;&amp;nbsp;&lt;/b&gt;is the solution of this dependency!&amp;nbsp;&amp;nbsp;Docker is a platform which uses the combination of the &lt;b&gt;images&lt;/b&gt; and the &lt;b&gt;containers &lt;/b&gt;concepts&lt;b&gt;. &lt;/b&gt;Here we can think 'image' as a self contained package and 'container' as a running image on Docker platform. Currently Docker is a must know skill for building a cloud native microservices based applications. And future of all monolithic architecture based application is microservices. With Docker we can deliver our application with all required dependencies as an image. So as a Data Scientist we must also know atleast high level of this platform. Let's start todays learning-&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How to install Docker?&lt;/b&gt;&lt;br /&gt;
We will use community version of the Docker since it is free. We can download the Docker Desktop from the&amp;nbsp;&lt;b&gt;&lt;a href="https://docs.docker.com/docker-for-windows/" target="_blank"&gt;official site&lt;/a&gt;-&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuS3QSv7jF1h8yg4qwjTOrXkWZQLujrwTD_hqXO3f8qQug1YCBIQ05hshgBeYpnnqoFO9YaWxd2fUAayLtjeePRkB3dPDLHVUL6KZRyTdOZuHvbhpzHRw85d49XgchZ1d-mbT3qPIt4w_c/s1600/doc2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="446" data-original-width="1344" height="212" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuS3QSv7jF1h8yg4qwjTOrXkWZQLujrwTD_hqXO3f8qQug1YCBIQ05hshgBeYpnnqoFO9YaWxd2fUAayLtjeePRkB3dPDLHVUL6KZRyTdOZuHvbhpzHRw85d49XgchZ1d-mbT3qPIt4w_c/s640/doc2.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Download the setup file&lt;b&gt;. &lt;/b&gt;It will ask you to create an account so make it and then download the setup-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuRCnrnk3wKAjYzK__R42HCNU3WNl0xX1Kozwp8yz9fN_uMASqrtaJSRtB3rnW_PoXIq8I8V7sEjxqGsmd41L7Q3xfICfuz-9HzRsFgULpY6w7Xf2SwMFi6k0kVUQs30yRo-ecAdTYV1zv/s1600/doc5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="738" data-original-width="1053" height="448" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuRCnrnk3wKAjYzK__R42HCNU3WNl0xX1Kozwp8yz9fN_uMASqrtaJSRtB3rnW_PoXIq8I8V7sEjxqGsmd41L7Q3xfICfuz-9HzRsFgULpY6w7Xf2SwMFi6k0kVUQs30yRo-ecAdTYV1zv/s640/doc5.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Before installing please make sure following two things-&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Windows 10 64-bit: Pro, Enterprise, or Education (Build 15063 or later).&lt;/li&gt;
&lt;li&gt;Hyper-V and Containers Windows features must be enabled.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
Following is the way to do second option-&lt;/div&gt;
&lt;div&gt;
Open command prompt with admin rights and run the following command-&lt;/div&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V&lt;/b&gt;&lt;/blockquote&gt;
In my machine it looks like below once we run the above command-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTug6uGNoybV4yZEz_8ONvaoh7oghRGtG9r64gEKyGaivwFkzzyDL40eFkoF16ekywS6TnzdhXWkac77-rhi3Gi15gBMbVzKEEUNkfb457ycdbwGjE_hezVojGEtJdN6vAniDaCEZc3Rnu/s1600/docker1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="480" data-original-width="1103" height="278" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTug6uGNoybV4yZEz_8ONvaoh7oghRGtG9r64gEKyGaivwFkzzyDL40eFkoF16ekywS6TnzdhXWkac77-rhi3Gi15gBMbVzKEEUNkfb457ycdbwGjE_hezVojGEtJdN6vAniDaCEZc3Rnu/s640/docker1.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
When you type Y, system will be restarted. Now we are ready to install the Docker Desktop. Go to the downloaded file, run it as admin and follow the instruction. The setup process will look like below screens-&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZSG9KtQF8V8nGx06mk4rucYoPHM18LGh20tCjN50yHHKnUYerfdJTetnSO85LrsReuTLjtGx8QA_e2Xc7KhmBEkxk5ijyU8qEXV2Te5iIrmt0j66mY66YqmtZ8nWNG-nSIjPB0M0bRUdO/s1600/doc6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="267" data-original-width="885" height="192" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZSG9KtQF8V8nGx06mk4rucYoPHM18LGh20tCjN50yHHKnUYerfdJTetnSO85LrsReuTLjtGx8QA_e2Xc7KhmBEkxk5ijyU8qEXV2Te5iIrmt0j66mY66YqmtZ8nWNG-nSIjPB0M0bRUdO/s640/doc6.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Click on Ok button, next screen will unpack the installation and then will ask you to logout of machine-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqrcvKtCxhCBzEuU93V1DItOnMay7EFb2mWAkQApcZmymqVI_XhXo6r91azE-DL2uugGVDFt5UxSyEs5eVh4CzhTChf_HvO2qjsJJaWuMoc2VC_DlX96xGv49bcK1D9zJJeXNKXdy0NWho/s1600/doc8.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="400" data-original-width="873" height="182" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqrcvKtCxhCBzEuU93V1DItOnMay7EFb2mWAkQApcZmymqVI_XhXo6r91azE-DL2uugGVDFt5UxSyEs5eVh4CzhTChf_HvO2qjsJJaWuMoc2VC_DlX96xGv49bcK1D9zJJeXNKXdy0NWho/s400/doc8.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once you click on Close and log out button an re login. You can see Docker desktop icon in your desktop and a small whale icon in Taskbar. If the setup has not asked you for re-login then you must restart your machine. Now when I click on the Desktop icon of Docker, it was showing me following access error-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuZZD3148JwfjW0Bi5neLqlsl9u6gaqfGYvZ7TydK0gdmsAbbsuLIlOUZ_KjN0W7JsH9cnedeq2DtS0IxlbhXE9N_Yz9JHTkflKyHaxLFj_gCwzRlzWkkAcZEa2y9tk7rg4iSm7pR2s8o5/s1600/docerror.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="441" data-original-width="862" height="203" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuZZD3148JwfjW0Bi5neLqlsl9u6gaqfGYvZ7TydK0gdmsAbbsuLIlOUZ_KjN0W7JsH9cnedeq2DtS0IxlbhXE9N_Yz9JHTkflKyHaxLFj_gCwzRlzWkkAcZEa2y9tk7rg4iSm7pR2s8o5/s400/docerror.JPG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
To solve this issue copy the path till 'C:\ProgramData\docker' in address bar and press enter. A pop up will appear and ask for permanent access, select this button and you will be given the access. Now go back to the error showing console and click on Quit button. Again double click on the Desktop icon of Docker. This time a pop up will come and ask you for enabling the containers-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1IVF-H8dePkigmoRKni4iIyxord2x6bCicF84vRJZAo0anwjehbfvGsMreD86Ot_7qku2vaeUpbc2AqXMXsfd1o9vzMcd1zPT5qBOElzv5NXtNPBFroK5bQz8YeT42qHW6Q9yVDzL5LBk/s1600/docker2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="303" data-original-width="862" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1IVF-H8dePkigmoRKni4iIyxord2x6bCicF84vRJZAo0anwjehbfvGsMreD86Ot_7qku2vaeUpbc2AqXMXsfd1o9vzMcd1zPT5qBOElzv5NXtNPBFroK5bQz8YeT42qHW6Q9yVDzL5LBk/s640/docker2.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Click on Ok button and your system will be restarted. Once the system is restarted, you will see that Docker is running notification in taskbar notification. It means you have successfully install the Docker Desktop in your machine. Now whenever your system is restarted, Docker will be run automatically. But this default behaviour takes lots of RAM utilization, so it is recommended to disable this auto start feature and start the Docker when it is needed. We can disable this feature by following this step-&lt;br /&gt;
Right click on Docker Whale icon in Taskbar, Go to Settings, Click on General tab and uncheck the first option 'Start Docker Desktop when you log in' and restart the Docker-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgH5XH5JVwCYmj8VWp7-wkrSH82zVX-eTw8PRjoX-CSX_0s-U6npUsiIhEvXhLmBYBy9bKPPMWtPmvQ3FhYNfeCcyIv11e8tYyBUutkf8Jjq472tW8lXOA-etAURJ-HpoZUaXqtb0jkxOnG/s1600/docker3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="532" data-original-width="1039" height="326" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgH5XH5JVwCYmj8VWp7-wkrSH82zVX-eTw8PRjoX-CSX_0s-U6npUsiIhEvXhLmBYBy9bKPPMWtPmvQ3FhYNfeCcyIv11e8tYyBUutkf8Jjq472tW8lXOA-etAURJ-HpoZUaXqtb0jkxOnG/s640/docker3.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Testing the installation&lt;/b&gt;&lt;br /&gt;
Once we completed the above steps, we can check if our installation is correct or not. To do so open command prompt and run following command- &lt;b&gt;docker --version&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMVaQE3b_00mTzhK5lNWPKVwD2JFKDfcVoX7fWsjDeSaszMeKD3Qwzxn4PnF53_pmHJyCh-0kf2DOcLhajV99l3950Ek3kAJapS0YVTsVdSQnrZ4Qjoq-KIQVwCXkhm7yvLGSeM1lkIVhM/s1600/docker4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="218" data-original-width="730" height="190" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMVaQE3b_00mTzhK5lNWPKVwD2JFKDfcVoX7fWsjDeSaszMeKD3Qwzxn4PnF53_pmHJyCh-0kf2DOcLhajV99l3950Ek3kAJapS0YVTsVdSQnrZ4Qjoq-KIQVwCXkhm7yvLGSeM1lkIVhM/s640/docker4.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
There are various repositories with inbuilt Docker images are available for us to use. A simple example is 'Docker Hub' repository. We can use this and test our Hello World image by running following command-&amp;nbsp;&lt;b&gt;docker run hello-world&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNKJ8L08nIhb8q48l4viO7M3A7p0J_qAmaBDL7f1rCMM159zzAjEv02cV9BVCmVbmmQP6-m6P86aLLWf53SFvOL98EckoSNZGRPCwPsewQ6XM_SpcNU-A0RFMdsa5K5FHwGugFZV7DMrkM/s1600/docker5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="637" data-original-width="768" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNKJ8L08nIhb8q48l4viO7M3A7p0J_qAmaBDL7f1rCMM159zzAjEv02cV9BVCmVbmmQP6-m6P86aLLWf53SFvOL98EckoSNZGRPCwPsewQ6XM_SpcNU-A0RFMdsa5K5FHwGugFZV7DMrkM/s1600/docker5.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
You can see in above console,a hello message is printed. It means hello-world image is downloaded from Docker Hub repository. Now we can print details of this downloaded image by running following command-&amp;nbsp;&lt;b&gt;docker image ls&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwZkJ0CI_imrVoGOp8kVpF4cwMwyyOBdmN6FABN-Ch_St2kORN741Rrou9JbxAlWm-saIwKWMZatNLQHiRcR6Gni73Wxf04HTQykNN1xvq133zN00qBTdI3V1niZNOBFbefIfNq0SyeYMm/s1600/docker6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="91" data-original-width="784" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwZkJ0CI_imrVoGOp8kVpF4cwMwyyOBdmN6FABN-Ch_St2kORN741Rrou9JbxAlWm-saIwKWMZatNLQHiRcR6Gni73Wxf04HTQykNN1xvq133zN00qBTdI3V1niZNOBFbefIfNq0SyeYMm/s1600/docker6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;Exploring an application&lt;/b&gt;&lt;br /&gt;
Next, we will see real power of the Docker by running a Ubuntu OS and a webserver- nginx. Sounds interesting, right! For this we need to switch our container from Windows to Linux. To do so, right click on Whale Docker icon in the Taskbar and select 'Switch to Linux containers.." option.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM6fKEHL9fL__0BZhPQrSnOtq9MC1NaYWjZUWDJ6F5bgy6MnbqPGTVAu6kzOK939qU-dHKGIJo7vXsqMaiCwwqY2nx6ar0P_c90acC9ZjUa9DjMdURfi2ZznW0pOT0eMfK8x_mDwREakLh/s1600/docker7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="353" data-original-width="863" height="260" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM6fKEHL9fL__0BZhPQrSnOtq9MC1NaYWjZUWDJ6F5bgy6MnbqPGTVAu6kzOK939qU-dHKGIJo7vXsqMaiCwwqY2nx6ar0P_c90acC9ZjUa9DjMdURfi2ZznW0pOT0eMfK8x_mDwREakLh/s640/docker7.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Click on Switch button. Close the existing command prompt console and open a new one and run the following command-&amp;nbsp;&lt;b&gt;docker run --interactive --tty ubuntu bash&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRRt8EVHdwnQN4OdPeo9kYJjVmXQp6pJWfuJdcSxgS-6dVEOus-em_fxtRisxZltidBMmnXnoGho8iN79OhyxgCLxlF5NafKBBQadazUGwHPq3Gokh9M2MG8m4vwSruXO-ynW15bV1sjD5/s1600/docker8.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="375" data-original-width="814" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRRt8EVHdwnQN4OdPeo9kYJjVmXQp6pJWfuJdcSxgS-6dVEOus-em_fxtRisxZltidBMmnXnoGho8iN79OhyxgCLxlF5NafKBBQadazUGwHPq3Gokh9M2MG8m4vwSruXO-ynW15bV1sjD5/s1600/docker8.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
See, the above root prompt. We are in the container. Let's check the hostname of the container by running typing 'hostname' and pressing enter-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPskq7-t0n2g9aVIQaa2Ay1u1stHGU5jIBD72MnlMAhGDGFL-MQkscR4W3b_nQE2AdWUfACMxfayLPfjr3VVDLcqH0bM8tESTcS2FkuEdRCBqYmm_Oszlm-GEOrlCZOXZq_HyN6paQ1Hu1/s1600/docker10.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="87" data-original-width="490" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPskq7-t0n2g9aVIQaa2Ay1u1stHGU5jIBD72MnlMAhGDGFL-MQkscR4W3b_nQE2AdWUfACMxfayLPfjr3VVDLcqH0bM8tESTcS2FkuEdRCBqYmm_Oszlm-GEOrlCZOXZq_HyN6paQ1Hu1/s1600/docker10.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;'3b9f82bf5dea' &lt;/b&gt;is the id of the hostname. Exit the prompt by typing 'exit' and pressing enter. Now we will check containers available in this image by running following command-&amp;nbsp;&lt;b&gt;docker container ls --all&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvUy_MCQkWzrLwZUpvAWJ5VTAMN1y1MugwvZWLD7Q8kycvi-Zry3M9olYYQv9HXKSwbGIcO5-jIf1Z9iTKyAs91_Z7MD52V95jaPoGIjdNTx85fvE4y0u73MSeg9d0QUfiR1OhdJDz8_m1/s1600/docker11.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="76" data-original-width="1357" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvUy_MCQkWzrLwZUpvAWJ5VTAMN1y1MugwvZWLD7Q8kycvi-Zry3M9olYYQv9HXKSwbGIcO5-jIf1Z9iTKyAs91_Z7MD52V95jaPoGIjdNTx85fvE4y0u73MSeg9d0QUfiR1OhdJDz8_m1/s1600/docker11.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Now we will start the nginx webserver but to do so &lt;b&gt;we must restart our Docke&lt;/b&gt;r and &lt;b&gt;open a new window&lt;/b&gt; of command prompt because if you do not do so and run directly the following command:&amp;nbsp;&lt;b&gt;docker run --detach --publish 80:80 --name mywebserver nginx&lt;/b&gt;&amp;nbsp;, you will get following error-&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
docker: Error response from daemon: driver failed programming external connectivity on endpoint mywebserver&lt;/blockquote&gt;
After restarting Docker, open a new console and run following command:&lt;b&gt;&amp;nbsp;docker run --detach --publish 8080:80 --name mywebserver nginx. &lt;/b&gt;Here mywebserver is my server name so you can use any name and also can give any port number instead of 8080.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5Fp18sjR2UMYpes8m50GNZXA0pB66yd8gRuuZKWSIcQC-6-T75C3n8Ez-1CTFwxAFuZsm_9xsIbZdR3UN_UpljzhngKoRzai9rCwbxwSbf0rTsaeih0n6w0p-wW-XnUxC9HkYfyduP2vf/s1600/docker12.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="58" data-original-width="769" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5Fp18sjR2UMYpes8m50GNZXA0pB66yd8gRuuZKWSIcQC-6-T75C3n8Ez-1CTFwxAFuZsm_9xsIbZdR3UN_UpljzhngKoRzai9rCwbxwSbf0rTsaeih0n6w0p-wW-XnUxC9HkYfyduP2vf/s1600/docker12.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Our server is up and running, we can find it's details by running following command-&lt;b&gt;docker container ls&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP497eBsTwEfve1iawGmzBxE0Cb9DPqut52ViS5VtidXJ99vpKEDrjkqvv5IDwTDinYJNtTF0cLR7fPH_CvEX4V8Ktk3gU5FXruzjbLXfI1rgSTzPKHPmugNbQU68sX1P5ZbRR2-AE1fqd/s1600/docker13.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="84" data-original-width="1276" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP497eBsTwEfve1iawGmzBxE0Cb9DPqut52ViS5VtidXJ99vpKEDrjkqvv5IDwTDinYJNtTF0cLR7fPH_CvEX4V8Ktk3gU5FXruzjbLXfI1rgSTzPKHPmugNbQU68sX1P5ZbRR2-AE1fqd/s1600/docker13.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Open a browser window and type&amp;nbsp;&lt;b&gt;http://localhost:8080, &lt;/b&gt;press enter. You will see following screen-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSFkF0vJ6g_mv-Es-b6wLKECoTc1YWmhw0vpUQxlQKy0Zk49gMsizbwXuMFXuKuKThWC8Ys3SIygzv2zSJDg4yYMYXgtwIw7tzdztW_4I5ucFK3ULMkpPfg921MW_5wMw6cBbxgyqM2eBf/s1600/docker14.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="322" data-original-width="1122" height="183" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSFkF0vJ6g_mv-Es-b6wLKECoTc1YWmhw0vpUQxlQKy0Zk49gMsizbwXuMFXuKuKThWC8Ys3SIygzv2zSJDg4yYMYXgtwIw7tzdztW_4I5ucFK3ULMkpPfg921MW_5wMw6cBbxgyqM2eBf/s640/docker14.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
You can stop your server by running following command:&lt;b&gt;docker container stop &amp;lt;your_server_name&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;Running a Python image&lt;/b&gt;&lt;br /&gt;
Let's run a Python image (for example python:latest) from Docker Hub repository. Open a command prompt and run following command:&lt;b&gt;docker run -it --rm python:latest. &lt;/b&gt;This command will download the python:latest image from Docker Hub repository.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYoSSRhSave4mDynJXFxO3KgnpFk2PvPP2E7Lm5_eqM64vc-0LFeIJrfnKtx-KaWH31cPGE-mnT_9Al7yO_PghWtty95O1S6XW-F_gGHsZUhfwbpGfaRmou30D4JlXpkWgBvZscoVtmkHf/s1600/docker16.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="380" data-original-width="720" height="336" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYoSSRhSave4mDynJXFxO3KgnpFk2PvPP2E7Lm5_eqM64vc-0LFeIJrfnKtx-KaWH31cPGE-mnT_9Al7yO_PghWtty95O1S6XW-F_gGHsZUhfwbpGfaRmou30D4JlXpkWgBvZscoVtmkHf/s640/docker16.JPG" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
As you can see in console, command has downloaded latest version of Python- 3.8.1 on our Linux container. If you want to use a specific version then instead of python:latest use python:&amp;lt;version_number&amp;gt;. We have now our Python 3.8.1 running in Linux container.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Preparing my Deep Learning project specific Docker image&lt;/b&gt;&lt;br /&gt;
Now I will tell you how I have made my project as a Docker image. To do so we use concept of a &lt;b&gt;Dockerfile&lt;/b&gt;. This file is simple text file having instructions and arguments. Docker reads this file and builds image automatically. According to&amp;nbsp;&lt;b&gt;&lt;a href="https://docs.docker.com/develop/develop-images/dockerfile_best-practices/" target="_blank"&gt;official site&lt;/a&gt;&amp;nbsp;&lt;/b&gt;syntax of a sample docker file looks like below format-&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;blockquote class="tr_bq"&gt;
FROM ubuntu:18.04&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
COPY . /myapp&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
RUN make /myapp&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
CMD python /myapp/app.py&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;b&gt;FROM&lt;/b&gt; creates a layer from the ubuntu:18.04 Docker image.&lt;br /&gt;
&lt;b&gt;COPY&lt;/b&gt; adds files from your Docker client’s current directory.&lt;br /&gt;
&lt;b&gt;RUN&lt;/b&gt; builds your application with make.&lt;br /&gt;
&lt;b&gt;CMD&lt;/b&gt; specifies what command to run within the container.&lt;br /&gt;
&lt;br /&gt;
Following the above syntax I have created a Dockerfile.txt for my project:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLEQegzJdog0v4lLevO2ea0R80aocOsgmfcmvpFWfh6d4GUdGryMPtJvqvlAKoE0nFoHCq120uivzdaEMO2vGExmLSp8UZJYXZXWkLgU-OWJYiT4hYtWo8acajapLK6DGSfBgYhuS9rh9k/s1600/new1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="654" data-original-width="761" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLEQegzJdog0v4lLevO2ea0R80aocOsgmfcmvpFWfh6d4GUdGryMPtJvqvlAKoE0nFoHCq120uivzdaEMO2vGExmLSp8UZJYXZXWkLgU-OWJYiT4hYtWo8acajapLK6DGSfBgYhuS9rh9k/s1600/new1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
My project structure looks like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJShhkHLyYhwiW8IyYTLbyDu6ClqCI6pb1OeX9UXO9X0RdJFBY2_xwPatcKg8nNyLHRHCN2fSpL1ZrFRUwpvCbQXSHMQ5qa5YYxUAumrPjotTclFDorCe922rciVHcyPmTc0hgGl26eQXs/s1600/project.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="631" data-original-width="310" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJShhkHLyYhwiW8IyYTLbyDu6ClqCI6pb1OeX9UXO9X0RdJFBY2_xwPatcKg8nNyLHRHCN2fSpL1ZrFRUwpvCbQXSHMQ5qa5YYxUAumrPjotTclFDorCe922rciVHcyPmTc0hgGl26eQXs/s400/project.JPG" width="196" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
Before moving further please make sure you have created a requirements.txt file in your project structure. requirements.txt file for my project looks like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijPDlMClajevMo7j4MB3YxOm93-Ud2oMUgEUJd4yfE8-kGaYTYmU9ndE1kYjMqmwBVoG8jMQBrOgnvYxbRGrTGK1Ug8HajzUXoT5ny56T_dOyXR55Bndpbz-B9cxnY0qFqVzos4W4KQmKG/s1600/docker19.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="588" data-original-width="331" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijPDlMClajevMo7j4MB3YxOm93-Ud2oMUgEUJd4yfE8-kGaYTYmU9ndE1kYjMqmwBVoG8jMQBrOgnvYxbRGrTGK1Ug8HajzUXoT5ny56T_dOyXR55Bndpbz-B9cxnY0qFqVzos4W4KQmKG/s400/docker19.JPG" width="225" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Now we will build our image. Go to the the path of your project and open it in a command prompt and run following command:&lt;b&gt;docker build -t dsbyprateekg .&lt;/b&gt;&lt;br /&gt;
Here&amp;nbsp;&lt;b&gt;dsbyprateekg &lt;/b&gt;is my image name. One you run the above command, Docker will read your Dockerfile.txt and follow it's instruction.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5GUqV3zT07-FnHPf2-5ZcEHEHQSfSUpXyL-OnhxmbP13XA4i-vMHQYEkPJKTSSZ8KFVpQQVMKuBCgLv4cMEf1tzEpd_WvkqsMZqPc4Uo7OgFkGDDKNlus970hR2ai-Zk2avZAH8XqvLad/s1600/docker20.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="93" data-original-width="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5GUqV3zT07-FnHPf2-5ZcEHEHQSfSUpXyL-OnhxmbP13XA4i-vMHQYEkPJKTSSZ8KFVpQQVMKuBCgLv4cMEf1tzEpd_WvkqsMZqPc4Uo7OgFkGDDKNlus970hR2ai-Zk2avZAH8XqvLad/s1600/docker20.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once the process is completed you will see following look like message in the end of the server console-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpgk27XTUHHYrsvY0Xp1heFN3-TPT6sClzc5Iws9bZqm8zoYw4kmAZgVQLRr30-MVhLpdpEGvcPpJ6GmK6v2YzST-B1W33dh-6xwrTQyKaqHd-YyhGeud673VTBWqivvzvrax_zK13Bnxw/s1600/docker21.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="364" data-original-width="891" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpgk27XTUHHYrsvY0Xp1heFN3-TPT6sClzc5Iws9bZqm8zoYw4kmAZgVQLRr30-MVhLpdpEGvcPpJ6GmK6v2YzST-B1W33dh-6xwrTQyKaqHd-YyhGeud673VTBWqivvzvrax_zK13Bnxw/s1600/docker21.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Now you can check if your newly created image exist or not by running following command:&lt;b&gt;docker images&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNEgT2obIm9Q3CSwASg0jL-Fn4JJPKctajqUyn5hlVUruVcOLT1BviLxu7epNgYvjcg6pS77K0fUqrl_CULM4xj91VR8WXN9pjysa8ch01-Cslcfpi3e94owGXy6vhf1DRcdkyzmg9rBCV/s1600/docker22.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="74" data-original-width="786" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNEgT2obIm9Q3CSwASg0jL-Fn4JJPKctajqUyn5hlVUruVcOLT1BviLxu7epNgYvjcg6pS77K0fUqrl_CULM4xj91VR8WXN9pjysa8ch01-Cslcfpi3e94owGXy6vhf1DRcdkyzmg9rBCV/s1600/docker22.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Here under the 'TAG' column you see 'latest'. We can change this value to a more specific name. For example I have changed 'latest' to 'prateek_gupta' by running following command:&lt;b&gt;docker tag &amp;lt;IMAGE_ID&amp;gt; &amp;lt;IMAGE_NAME&amp;gt;:&amp;lt;TAG&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQhpgQnfJvQVJImLQen4klmTeBld8lmlTzY0mVE1rVpngFgy7P5zvuE36Q2_oXReFvbGdzR32_mb1e8WLDyI35lUIe2B8FVa7mm6slpQHBKaE5dvbrrK3olGVjpKtMG1HRsVrSkgpARhwG/s1600/docker23.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="133" data-original-width="821" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQhpgQnfJvQVJImLQen4klmTeBld8lmlTzY0mVE1rVpngFgy7P5zvuE36Q2_oXReFvbGdzR32_mb1e8WLDyI35lUIe2B8FVa7mm6slpQHBKaE5dvbrrK3olGVjpKtMG1HRsVrSkgpARhwG/s1600/docker23.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Next, we can test our container locally by running following command:&lt;b&gt;docker run &amp;lt;IMAGE_NAME&amp;gt;:&amp;lt;TAG&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6MnXx5othm_2QtFXMceZQayBERY7Ud5pxDFICZZE3OYQltlQSqxmvKOP1JtydfDOZ2N53a3vtYsymOemNlzHKfJomeHljDi2Z7Z2UQKsczr0PQDEQaXx-sFafXhoyO6ekq9zhrHMpcmWe/s1600/new2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="100" data-original-width="801" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6MnXx5othm_2QtFXMceZQayBERY7Ud5pxDFICZZE3OYQltlQSqxmvKOP1JtydfDOZ2N53a3vtYsymOemNlzHKfJomeHljDi2Z7Z2UQKsczr0PQDEQaXx-sFafXhoyO6ekq9zhrHMpcmWe/s1600/new2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
If all is well then you will see your Python file running successfully as in my case I have deployed my deep learning project as Flask Rest API so my flask server is up and running-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNY5q_A-IOSRdmEWYQan5TfeE3ujP-pU7W8f_d2uCz4ILx7EqY1XZbTLs9O3g-_pqpWeDLKPkpsIryFNWiOhNmS-k2Hgf6LTfJAX13EX1_2la2uJCEeJKGzANMZC12IPcn7TNWFsLCy62Z/s1600/new3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="130" data-original-width="750" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNY5q_A-IOSRdmEWYQan5TfeE3ujP-pU7W8f_d2uCz4ILx7EqY1XZbTLs9O3g-_pqpWeDLKPkpsIryFNWiOhNmS-k2Hgf6LTfJAX13EX1_2la2uJCEeJKGzANMZC12IPcn7TNWFsLCy62Z/s1600/new3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
It means we have tested our Docker container locally and it can be run anywhere Docker is installed. In last step we can now push our repository to our account in Docker Hub. For this first we need to tag our image in require format:&amp;nbsp;&lt;b&gt;docker tag &amp;lt;IMAGE_NAME&amp;gt; &amp;lt;YOUR_DOCKERHUB_NAME&amp;gt;/&amp;lt;IMAGE_NAME&amp;gt; .&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
In my case my&amp;nbsp;&lt;b&gt;IMAGE_NAME&lt;/b&gt;&amp;nbsp;is dsbyprateekg&lt;b&gt; &lt;/b&gt;and&amp;nbsp;&lt;b&gt;YOUR_DOCKERHUB_NAME &lt;/b&gt;is my Docker account name. Then we need to login in Docker with our account by running following command:&lt;b&gt;docker login --username &amp;lt;Your_Docker_Username&amp;gt;&lt;/b&gt;&lt;br /&gt;
It will ask you to enter your password, give the password and press enter. Now you can push your changes by running following command: &lt;b&gt;docker&lt;/b&gt; &lt;b&gt;push&lt;/b&gt;&amp;nbsp;&lt;b&gt;&amp;lt;Your_Docker_Username&amp;gt;/&amp;lt;IMAGE_NAME&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-Yt4tvdO8EKkPUpYze-RYw_MfuL9VtpQBYGI8sY22dilJWNolX93uUEtpfcw3T2ldxCXPUlESf7vOFLzvZIu_hYOVWVQ96VBtzafF-esizVtqwYbt9rpH5d6xioRwDl0NZpbCovesKgiw/s1600/new4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="299" data-original-width="847" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-Yt4tvdO8EKkPUpYze-RYw_MfuL9VtpQBYGI8sY22dilJWNolX93uUEtpfcw3T2ldxCXPUlESf7vOFLzvZIu_hYOVWVQ96VBtzafF-esizVtqwYbt9rpH5d6xioRwDl0NZpbCovesKgiw/s1600/new4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once your Docker image is pushed, you can verify it also by going to your Docker account. Now you can easily share this to anyone. You just need to ask them to install Docker in their system, run following command:&lt;b&gt;docker pull &amp;lt;IMAGE_NAME&amp;gt; &lt;/b&gt;to get the image and then just run the container&lt;b&gt;. &lt;/b&gt;They don't need to do any code setup in their system.That's it for today. We have learnt about Docker, it's installation in our machine, switching from Windows to Linux container, successfully made a Docker image of a project and pushed our changes in Docker Hub. I have shared my project structure, Dockerfile screenshot so that you can get a reference from it and can easily apply it in your machine learning or deep learning based project. So try it yourself in your project and explore more.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidwxIW-rrZkA9bCfb5MJrW8HVp9YXUhLdKOHnAcyA0MRc077ow3LRgH58fW9vI7mGVaL2X5AK82mT6SyN-Q03EPQM0JMXRM4MG5pgRSIhADFXucK839BQnyp6SETOtML49SshhMMRTW7jm/s72-c/new5.jpg" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">9</thr:total></item><item><title>How can I install and use Darknet framework in Windows?</title><link>https://dsbyprateekg.blogspot.com/2019/12/how-can-i-install-and-use-darknet.html</link><category>Darknet</category><category>object detection</category><category>Windows</category><category>yolov3</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Thu, 5 Dec 2019 12:37:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-1285285662783078729</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzHZni2zHM979FaOvA4poA2hpQIT4tRYMEVlaE5o9Q4Pf1oYXgKwHc7nz6ECtsX0UPUjJ_tZAr-rTwf3QR-_MFqD2ZAkZUWKBuUoB5r1WMPuZXhLdoqqXrGALJ_VkWgVQRS_laQiTOWUzM/s1600/dark.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="376" data-original-width="649" height="185" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzHZni2zHM979FaOvA4poA2hpQIT4tRYMEVlaE5o9Q4Pf1oYXgKwHc7nz6ECtsX0UPUjJ_tZAr-rTwf3QR-_MFqD2ZAkZUWKBuUoB5r1WMPuZXhLdoqqXrGALJ_VkWgVQRS_laQiTOWUzM/s320/dark.JPG" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
Another post starts with you beautiful people! I hope you have enjoyed my&amp;nbsp;&lt;b&gt;&lt;a href="https://dsbyprateekg.blogspot.com/2019/11/how-to-detect-object-in-real-time-using.html" target="_blank"&gt;last post&lt;/a&gt;&amp;nbsp;&lt;/b&gt;about using real time object detection system- Yolo with keras api. In that post I mentioned that Yolo is built on Darknet framework and this framework is written on C and cuda. That's why we used Python wrapper of Darknet&amp;nbsp; framework instead of installing original framework. Many readers asked me about how can we install and use the original framework in our window machine. In this post I will try to show you the steps about this installation. Before following the steps I strongly recommend you to activate virtual env and install all libraries I have mentioned in my&amp;nbsp;&lt;b&gt;&lt;a href="https://dsbyprateekg.blogspot.com/2019/11/how-to-detect-object-in-real-time-using.html" target="_blank"&gt;last post&lt;/a&gt;.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
For this setup I have followed this original github repository-&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/AlexeyAB/darknet#how-to-compile-on-windows-using-cmake-gui" target="_blank"&gt;AlexeyAB/darknet&lt;/a&gt;&amp;nbsp;&lt;/b&gt;. this repo is as same as original Darknet repo with additional Windows support. So don't forget to give a star to this repo as a token of our respect to the author. If you are reading my blog first time, then I recommend to read the Requirements section mentioned in this repo first.&lt;br /&gt;
&lt;br /&gt;
Once your machine is ready with all requirements, next step is to install a compilation software- &lt;b&gt;vcpkg&lt;/b&gt; which you can download from&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/Microsoft/vcpkg" target="_blank"&gt;this link&lt;/a&gt;. &lt;/b&gt;Once you download the zip file, extract it, open command prompt with admin rights, navigate to the extracted vcpkg-master folder and run the following command-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRjhuGJsEFdsXLVPvCIjS2IuvT23rgnobaUBdADeitRN_yH7WtKsKXgCaFKLiLg5ty-EeBYlU9lXDcI657O33Klg-4ce2VQoBGFPhQU_zNTekgKVK9Q0lK0lOqrRf9kZ9OArjJqeoXNkRH/s1600/d1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="224" data-original-width="498" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRjhuGJsEFdsXLVPvCIjS2IuvT23rgnobaUBdADeitRN_yH7WtKsKXgCaFKLiLg5ty-EeBYlU9lXDcI657O33Klg-4ce2VQoBGFPhQU_zNTekgKVK9Q0lK0lOqrRf9kZ9OArjJqeoXNkRH/s1600/d1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
Once the above command finishes, run the below command in same prompt-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiF3NfSeAlV8pgKt04Ytkcw4_YeqhQcKbh0l-fBmAhfcDb9FSFRf_GtGM-8_wkG87HtJS3blbb5K2PV2Kxe4FwExSrKmSXPQrbt49fwC-1E3ASgOUp4jx4I3VkqEGBE00GGMNIXR6ZfojDu/s1600/d2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="219" data-original-width="481" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiF3NfSeAlV8pgKt04Ytkcw4_YeqhQcKbh0l-fBmAhfcDb9FSFRf_GtGM-8_wkG87HtJS3blbb5K2PV2Kxe4FwExSrKmSXPQrbt49fwC-1E3ASgOUp4jx4I3VkqEGBE00GGMNIXR6ZfojDu/s1600/d2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
Next, we need to add vcpkg root path in our environment variable. So open the system variable and add the location of the vcpkg-master folder under the name of &lt;b&gt;VCPKG_ROOT&lt;/b&gt; like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmIgR2SwhjKQP7rvMS-JfjP4dGP67x4f2wx0m8K_XL9E-E-8YpUu6NcPOzYzgDym34qKn5aDvmcNDLcYkHah71FiNI8FLKp5y-UHM8Jog_7VfYx22_wqqdcaAgwgKlxzPYniB7UTSpnzeR/s1600/d3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="34" data-original-width="449" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmIgR2SwhjKQP7rvMS-JfjP4dGP67x4f2wx0m8K_XL9E-E-8YpUu6NcPOzYzgDym34qKn5aDvmcNDLcYkHah71FiNI8FLKp5y-UHM8Jog_7VfYx22_wqqdcaAgwgKlxzPYniB7UTSpnzeR/s1600/d3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Along with this new variable, add another variable&amp;nbsp;&lt;b&gt;VCPKG_DEFAULT_TRIPLET &lt;/b&gt;with following value-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmUHJrW_Ln9ISt8rFsJV0lhNST7DZADdr3ppjvb9TIJjDe0BLr6NcNfU_rVqbzus4EbHe5cNdhHQqKZsums9u_gTmQsNzcAo4Ytpw4QC6F4GGq1fa1tLQ9Thd319mTyl4ZBLrlnd93N8Kc/s1600/d4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="28" data-original-width="316" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmUHJrW_Ln9ISt8rFsJV0lhNST7DZADdr3ppjvb9TIJjDe0BLr6NcNfU_rVqbzus4EbHe5cNdhHQqKZsums9u_gTmQsNzcAo4Ytpw4QC6F4GGq1fa1tLQ9Thd319mTyl4ZBLrlnd93N8Kc/s1600/d4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Now we are ready for installing Darknet with this compilation software. Open &lt;b&gt;Anaconda Poweshell Prompt&lt;/b&gt; with admin rights, navigate to the vcpkg-master folder by running following command:&lt;br /&gt;
&lt;b&gt;cd $env:VCPKG_ROOT&lt;/b&gt;&lt;br /&gt;
After this run the following command:&lt;br /&gt;
&lt;b&gt;.\vcpkg install pthreads opencv[ffmpeg]&lt;/b&gt;&lt;br /&gt;
Once you run above two commands, screen will look like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwKc9ZxSJpOvA2d430B8tqKb5AQmTB_8qeVdP-8dFKh7z_vGGTHRtthdCJu03hV9U7d1YMn-0bCm1n951tyvJ0k92w7bmP_BpfyK68kOEHHii3IMuVWCJgaRBNo951OOJVDbN7g8ZU3di6/s1600/d5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="508" data-original-width="1102" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwKc9ZxSJpOvA2d430B8tqKb5AQmTB_8qeVdP-8dFKh7z_vGGTHRtthdCJu03hV9U7d1YMn-0bCm1n951tyvJ0k92w7bmP_BpfyK68kOEHHii3IMuVWCJgaRBNo951OOJVDbN7g8ZU3di6/s1600/d5.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
It will take some time to complete. Once this process is completed next step is to download the code from&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/AlexeyAB/darknet" target="_blank"&gt;AlexeyAB/darknet&lt;/a&gt;. &lt;/b&gt;Once you downlaod the zip file from this repo, extract it and in the Anaconda Powershell Prompt navigate to the location of the darknet folder and run &lt;b&gt;.\build.ps1&lt;/b&gt; like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhG-jGEZ0i-gIgAKfmIn_pzj0Z8EtQN0tKh8jdmV8fYjIyUSXSYoGrlFxUMa1AbY3qJdIAVEHW7_bZwtS7LbcN77_2Q7LeFyDGjX4gLZRp34n99h7JuyVQQimOWlOV-QWC_BGp8Vnx-7WLv/s1600/d6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="37" data-original-width="399" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhG-jGEZ0i-gIgAKfmIn_pzj0Z8EtQN0tKh8jdmV8fYjIyUSXSYoGrlFxUMa1AbY3qJdIAVEHW7_bZwtS7LbcN77_2Q7LeFyDGjX4gLZRp34n99h7JuyVQQimOWlOV-QWC_BGp8Vnx-7WLv/s1600/d6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
This command will install darknet repository once finishes. After this run following command:&lt;br /&gt;
&lt;b&gt;Set-ExecutionPolicy -ExecutionPolicy Restricted &lt;/b&gt;and then when asked press &lt;b&gt;A &lt;/b&gt;to exclude any restriction. Once this is done, you can navigate to the installed repository in your respective folder and see all installed files there. Now you need to copy the pretrained Yolo weights file in this location. You can download the weights from &lt;a href="https://pjreddie.com/media/files/yolov3.weights" target="_blank"&gt;&lt;b&gt;this link&lt;/b&gt;&lt;/a&gt;. After this we are ready to test the original YOLO system built on Darknet framework on our image.&lt;br /&gt;
&lt;br /&gt;
To test any image you need to run following command in same Anaconda Powershell Prompt-&lt;br /&gt;
&lt;b&gt;.\darknet.exe detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights -thresh 0.25&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB3rM_oFaeW8VvQdpnscMn5fQeBL-tb8S40VvIa7zjbA6J2Vf7tP1pIVzGuLgGFFJw1ftWKS2YgCYykGZf6qAEjDZElDcfJ7MNtID9TfJrj5NqSTZ16ySgOpo6WJqn8X2gEKweCvEf2ddP/s1600/d7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="26" data-original-width="1049" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB3rM_oFaeW8VvQdpnscMn5fQeBL-tb8S40VvIa7zjbA6J2Vf7tP1pIVzGuLgGFFJw1ftWKS2YgCYykGZf6qAEjDZElDcfJ7MNtID9TfJrj5NqSTZ16ySgOpo6WJqn8X2gEKweCvEf2ddP/s1600/d7.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once this command finishes , it will ask you to enter the path of your test image. enter the full path and press enter-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-qqTa11KgxIHm9kzrMPiVe3QmeVS_bvFCQEqsuQZv41WBboX79C10BlHk_V3XL2CkqB_2WDWFAONIsz-XEs5kJc8nEz2ESTJa_GaJU7U16rQXTJow8bDiRB3Cmw7uWP0k2hRa4QC-rUow/s1600/d8.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="414" data-original-width="746" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-qqTa11KgxIHm9kzrMPiVe3QmeVS_bvFCQEqsuQZv41WBboX79C10BlHk_V3XL2CkqB_2WDWFAONIsz-XEs5kJc8nEz2ESTJa_GaJU7U16rQXTJow8bDiRB3Cmw7uWP0k2hRa4QC-rUow/s1600/d8.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once you enter the image path and press enter, your input image will be opened in default photo viewer having bounding boxes and label. Also in server console accuracy of the prediction will be shown with each detected object-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO6QgdvfrjCW5JbkjtlXeq3BhOzfUP91_anYDoRE5wgqkk5pgDgkQrOXHmLgqO2cCkWSxduGX5vZ5Luty65sFAGxU1TeedLa2kr76-Ldzvupb58uMrJ_hhM4lGOBEL7gCQXKCCkAo8iDvs/s1600/d10.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="641" data-original-width="880" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO6QgdvfrjCW5JbkjtlXeq3BhOzfUP91_anYDoRE5wgqkk5pgDgkQrOXHmLgqO2cCkWSxduGX5vZ5Luty65sFAGxU1TeedLa2kr76-Ldzvupb58uMrJ_hhM4lGOBEL7gCQXKCCkAo8iDvs/s1600/d10.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
That's amazing right. In this post we have successfully installed Darknet framework in our Windows machine and in my last post we have successfully setup the Python wrapper of Darknet. Now you are familiar with both ways. If you are working with a client who has Windows infrastructure like mine then this post will help you to deliver state of the art object detection model built on Yolo system. I recommend you to give some time to learn this framework and get your hands dirty by practicing it on different image dataset many times. In my next post I will share another my learnings.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzHZni2zHM979FaOvA4poA2hpQIT4tRYMEVlaE5o9Q4Pf1oYXgKwHc7nz6ECtsX0UPUjJ_tZAr-rTwf3QR-_MFqD2ZAkZUWKBuUoB5r1WMPuZXhLdoqqXrGALJ_VkWgVQRS_laQiTOWUzM/s72-c/dark.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">5</thr:total><enclosure length="248007048" type="application/octet-stream" url="https://pjreddie.com/media/files/yolov3.weights"/><itunes:explicit>no</itunes:explicit><itunes:subtitle>Another post starts with you beautiful people! I hope you have enjoyed my&amp;nbsp;last post&amp;nbsp;about using real time object detection system- Yolo with keras api. In that post I mentioned that Yolo is built on Darknet framework and this framework is written on C and cuda. That's why we used Python wrapper of Darknet&amp;nbsp; framework instead of installing original framework. Many readers asked me about how can we install and use the original framework in our window machine. In this post I will try to show you the steps about this installation. Before following the steps I strongly recommend you to activate virtual env and install all libraries I have mentioned in my&amp;nbsp;last post. For this setup I have followed this original github repository-&amp;nbsp;AlexeyAB/darknet&amp;nbsp;. this repo is as same as original Darknet repo with additional Windows support. So don't forget to give a star to this repo as a token of our respect to the author. If you are reading my blog first time, then I recommend to read the Requirements section mentioned in this repo first. Once your machine is ready with all requirements, next step is to install a compilation software- vcpkg which you can download from&amp;nbsp;this link. Once you download the zip file, extract it, open command prompt with admin rights, navigate to the extracted vcpkg-master folder and run the following command- Once the above command finishes, run the below command in same prompt- Next, we need to add vcpkg root path in our environment variable. So open the system variable and add the location of the vcpkg-master folder under the name of VCPKG_ROOT like below- Along with this new variable, add another variable&amp;nbsp;VCPKG_DEFAULT_TRIPLET with following value- Now we are ready for installing Darknet with this compilation software. Open Anaconda Poweshell Prompt with admin rights, navigate to the vcpkg-master folder by running following command: cd $env:VCPKG_ROOT After this run the following command: .\vcpkg install pthreads opencv[ffmpeg] Once you run above two commands, screen will look like below- It will take some time to complete. Once this process is completed next step is to download the code from&amp;nbsp;AlexeyAB/darknet. Once you downlaod the zip file from this repo, extract it and in the Anaconda Powershell Prompt navigate to the location of the darknet folder and run .\build.ps1 like below- This command will install darknet repository once finishes. After this run following command: Set-ExecutionPolicy -ExecutionPolicy Restricted and then when asked press A to exclude any restriction. Once this is done, you can navigate to the installed repository in your respective folder and see all installed files there. Now you need to copy the pretrained Yolo weights file in this location. You can download the weights from this link. After this we are ready to test the original YOLO system built on Darknet framework on our image. To test any image you need to run following command in same Anaconda Powershell Prompt- .\darknet.exe detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights -thresh 0.25 Once this command finishes , it will ask you to enter the path of your test image. enter the full path and press enter- Once you enter the image path and press enter, your input image will be opened in default photo viewer having bounding boxes and label. Also in server console accuracy of the prediction will be shown with each detected object- That's amazing right. In this post we have successfully installed Darknet framework in our Windows machine and in my last post we have successfully setup the Python wrapper of Darknet. Now you are familiar with both ways. If you are working with a client who has Windows infrastructure like mine then this post will help you to deliver state of the art object detection model built on Yolo system. I recommend you to give some time to learn this framework and get your hands dirty by practicing it on different image dataset many times. In my next post I will share another my learnings.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.</itunes:subtitle><itunes:author>noreply@blogger.com (Learn Data Science with PrateekG)</itunes:author><itunes:summary>Another post starts with you beautiful people! I hope you have enjoyed my&amp;nbsp;last post&amp;nbsp;about using real time object detection system- Yolo with keras api. In that post I mentioned that Yolo is built on Darknet framework and this framework is written on C and cuda. That's why we used Python wrapper of Darknet&amp;nbsp; framework instead of installing original framework. Many readers asked me about how can we install and use the original framework in our window machine. In this post I will try to show you the steps about this installation. Before following the steps I strongly recommend you to activate virtual env and install all libraries I have mentioned in my&amp;nbsp;last post. For this setup I have followed this original github repository-&amp;nbsp;AlexeyAB/darknet&amp;nbsp;. this repo is as same as original Darknet repo with additional Windows support. So don't forget to give a star to this repo as a token of our respect to the author. If you are reading my blog first time, then I recommend to read the Requirements section mentioned in this repo first. Once your machine is ready with all requirements, next step is to install a compilation software- vcpkg which you can download from&amp;nbsp;this link. Once you download the zip file, extract it, open command prompt with admin rights, navigate to the extracted vcpkg-master folder and run the following command- Once the above command finishes, run the below command in same prompt- Next, we need to add vcpkg root path in our environment variable. So open the system variable and add the location of the vcpkg-master folder under the name of VCPKG_ROOT like below- Along with this new variable, add another variable&amp;nbsp;VCPKG_DEFAULT_TRIPLET with following value- Now we are ready for installing Darknet with this compilation software. Open Anaconda Poweshell Prompt with admin rights, navigate to the vcpkg-master folder by running following command: cd $env:VCPKG_ROOT After this run the following command: .\vcpkg install pthreads opencv[ffmpeg] Once you run above two commands, screen will look like below- It will take some time to complete. Once this process is completed next step is to download the code from&amp;nbsp;AlexeyAB/darknet. Once you downlaod the zip file from this repo, extract it and in the Anaconda Powershell Prompt navigate to the location of the darknet folder and run .\build.ps1 like below- This command will install darknet repository once finishes. After this run following command: Set-ExecutionPolicy -ExecutionPolicy Restricted and then when asked press A to exclude any restriction. Once this is done, you can navigate to the installed repository in your respective folder and see all installed files there. Now you need to copy the pretrained Yolo weights file in this location. You can download the weights from this link. After this we are ready to test the original YOLO system built on Darknet framework on our image. To test any image you need to run following command in same Anaconda Powershell Prompt- .\darknet.exe detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights -thresh 0.25 Once this command finishes , it will ask you to enter the path of your test image. enter the full path and press enter- Once you enter the image path and press enter, your input image will be opened in default photo viewer having bounding boxes and label. Also in server console accuracy of the prediction will be shown with each detected object- That's amazing right. In this post we have successfully installed Darknet framework in our Windows machine and in my last post we have successfully setup the Python wrapper of Darknet. Now you are familiar with both ways. If you are working with a client who has Windows infrastructure like mine then this post will help you to deliver state of the art object detection model built on Yolo system. I recommend you to give some time to learn this framework and get your hands dirty by practicing it on different image dataset many times. In my next post I will share another my learnings.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.</itunes:summary><itunes:keywords>Darknet, object detection, Windows, yolov3</itunes:keywords></item><item><title>How to detect an object in real time using keras-yolo3?</title><link>https://dsbyprateekg.blogspot.com/2019/11/how-to-detect-object-in-real-time-using.html</link><category>computer vision</category><category>deep learning</category><category>keras</category><category>yolov3</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Mon, 25 Nov 2019 15:19:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-4234586141299537783</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgf8jcCm8WSqHqZSBeSvXnufxVDegBC7bTx9wi-CRVuT_3K1omLevlwwnqeRmrpy13wyMxKEA3tU7mU8kRF26QkbG4qRzWKnmRT2aWv5Veo-OTM7QGTyGr0o-carPkXB7IpX6iIkwoNpdgF/s1600/result.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="929" data-original-width="1259" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgf8jcCm8WSqHqZSBeSvXnufxVDegBC7bTx9wi-CRVuT_3K1omLevlwwnqeRmrpy13wyMxKEA3tU7mU8kRF26QkbG4qRzWKnmRT2aWv5Veo-OTM7QGTyGr0o-carPkXB7IpX6iIkwoNpdgF/s1600/result.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Another post starts with you beautiful people! In the past few months I was working on a complex object detection and recognition problem. My client is from a leading winery industry and they had an existing system built on&amp;nbsp;&lt;b&gt;&lt;a href="https://keras.io/applications/#vgg19" target="_blank"&gt;vgg19&lt;/a&gt;&amp;nbsp;&lt;/b&gt;and&amp;nbsp;&lt;b&gt;&lt;a href="https://github.com/fizyr/keras-retinanet" target="_blank"&gt;keras-retinanet&lt;/a&gt;. &lt;/b&gt;This system was built to help client in their sales forecasting. The problem with current system was it was inaccurate- it did not detect most of the wine bottles and brands, it did not give the result in real time. You can imagine how this bad model can affect the business!&lt;br /&gt;
&lt;br /&gt;
To solve the existing issues I tried a lot of things- changing hyperparameters, increasing the datasets, different&amp;nbsp;&lt;a href="https://keras.io/applications/" target="_blank"&gt;keras applications&lt;/a&gt;&amp;nbsp;but it was not giving me satisfactory result. May be I was not doing it right but I had put a lot of time and efforts with it. Then while doing R&amp;amp;D, I read&amp;nbsp;&lt;a href="https://machinelearningmastery.com/how-to-perform-object-detection-with-yolov3-in-keras/" target="_blank"&gt;&lt;b&gt;this fantastic blog&lt;/b&gt;&lt;/a&gt;&amp;nbsp;and I came to know about a state of the art and real time object detection system-&amp;nbsp;&lt;b&gt;&lt;a href="https://pjreddie.com/darknet/yolo/" target="_blank"&gt;YOLO&lt;/a&gt;. You Only Look Once &lt;/b&gt;or &lt;b&gt;YOLO&lt;/b&gt;&amp;nbsp;is a custom deep learning framework written on C and you can read more about it in it's&amp;nbsp;&lt;b&gt;&lt;a href="https://pjreddie.com/darknet/yolo/" target="_blank"&gt;official site&lt;/a&gt;.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Instead of writing the code from scratch I found two github repository as third party implementation of YOLO version 3-&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;&lt;a href="https://github.com/experiencor/keras-yolo3" target="_blank"&gt;&lt;b&gt;experiencor/keras-yolo3&lt;/b&gt;&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;a href="https://github.com/qqwweee/keras-yolo3" target="_blank"&gt;qqwweee/keras-yolo3&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
You can follow any of the above link and run this code to see how it works. In this post I will share how I tested this in my system. For testing this model on unseen pictures you need to follow below steps-&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;A. Prepare your virtual environment- &lt;/b&gt;The first step before starting your object detection and recognition journey is to install all required libraries. I recommend to create a virtual environment and install all libraries there instead of installing in base location. This saves your base location in case of any corruption while installing. To create and activate the virtual environment, open anaconda prompt with admin rights and run following two commands one by one-&lt;/div&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;conda create --name myNewEnv python=3.7.3&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;activate myNewEnv&lt;/b&gt;&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;div&gt;
Here myNewEnv is the name of my virtual environment and 3.7.3 is my Python version. Replace it with your own name and version. Once you activate it, it's time to install required libraries. Before installing these libraries make sure you have &lt;b&gt;Visual Studio 2017 with C++ extension&lt;/b&gt; is installed. If not then please install it and add C++ extension in it otherwise you will face unnecessary issues. Here I share list of mine which you can also use-&lt;/div&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install keras==2.2.4&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install tensorflow-gpu==1.14.0&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install scikit-learn&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;conda install anaconda-client&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;conda install -c conda-forge/label/cf201901 opencv&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install keras-retinanet&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;conda install shapely&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;conda install -c conda-forge imgaug&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;conda install -c conda-forge google-cloud-vision&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;conda install -c pjamesjoyce imutils&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;conda install -c anaconda flask&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;conda install -c conda-forge/label/cf201901 flask-restful&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install tqdm&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install boto3&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install matplotlib&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install seaborn&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install xlrd&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install pytesseract&lt;/b&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;b&gt;pip install apscheduler&lt;/b&gt;&lt;/blockquote&gt;
&lt;/blockquote&gt;
In above list you can see I have used variation of conda and pip. It is because while installing opencv and keras-retinanet in my windows machine I faced so much issues and I resolved those issues after so many efforts. So it is recommended to use above command as exact as I have mentioned. This is one time setup and most of the required image processing related libraries are mentioned in this list. Once all the installation is successful you can proceed with next step.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;B. Download pre-trained weights- &lt;/b&gt;Second step is to download the pre-trained weights from&amp;nbsp;&lt;b&gt;&lt;a href="https://pjreddie.com/media/files/yolov3.weights" target="_blank"&gt;This Link&lt;/a&gt;. &lt;/b&gt;It's a 235 mb file with name yolov3.weights.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;C. Define keras model- &lt;/b&gt;Our next step is to define a keras model to match with the downloaded weights. It means our keras model should have right number of layers and right types of the layers to match with Yolo weights.&amp;nbsp; This is the actual complex part but in the github repositories I have shared earlier, you can find the functions written already for this task. So just copy those but &lt;b&gt;don't forget to give a Star to the original authors in github&lt;/b&gt;. Here is screen shot of the code snippet-&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;# Create block of layers&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1RX5chSuI_XtpeAYy_tFa0djz5lCmsoK3bVymzLZBOUVRR05Yctr7K_eghyBzly8Uirw2T7-eV-sYxiFvH6qp9CsJQ_LGIVJ1ZkSrCF15yYEP-EXW56kl5itpadqYEFn7SJqFUfsUHdMm/s1600/make.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="442" data-original-width="1092" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1RX5chSuI_XtpeAYy_tFa0djz5lCmsoK3bVymzLZBOUVRR05Yctr7K_eghyBzly8Uirw2T7-eV-sYxiFvH6qp9CsJQ_LGIVJ1ZkSrCF15yYEP-EXW56kl5itpadqYEFn7SJqFUfsUHdMm/s1600/make.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;# Create the model&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4YxqKudWCYjXpK3GOjpv693hMBa2XS7vetO-3qSCLmo-J12Ma6rvJ_Cj9DNUkotgNwU29URRgG0_WXenL8fzpACwInv1OU9ZdAg1dwKJqVEqK00GHTjfhLlmgmoHef0GkQZfUVh7e8Ovq/s1600/model1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="728" data-original-width="1053" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4YxqKudWCYjXpK3GOjpv693hMBa2XS7vetO-3qSCLmo-J12Ma6rvJ_Cj9DNUkotgNwU29URRgG0_WXenL8fzpACwInv1OU9ZdAg1dwKJqVEqK00GHTjfhLlmgmoHef0GkQZfUVh7e8Ovq/s1600/model1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQVhwFQoe6wIitDBaIJg91bCMgxHuC_d4imrL4N_pLSebGgJ5rsFbjLY0sOrIVEE9RrTrEJirKbVxYGLwwi_3qMmXAgsTeT0MeOFsgsL__yL62bQEMViNJr_2l-CjxqQ8CWjHbAaaxbX8_/s1600/model2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="654" data-original-width="1094" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQVhwFQoe6wIitDBaIJg91bCMgxHuC_d4imrL4N_pLSebGgJ5rsFbjLY0sOrIVEE9RrTrEJirKbVxYGLwwi_3qMmXAgsTeT0MeOFsgsL__yL62bQEMViNJr_2l-CjxqQ8CWjHbAaaxbX8_/s1600/model2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaVdrTVmkh5XO5d_cBGIE1rcztlxaWy7n_yzRkY5Wvh60Hhzbl_5eGzjBmum9Xl3kngAdbxTgOfi2GdjhHIgdERdYNVgydY84YNWRqLa4ONWTxDjH8G35U28VvL50n2NYXBScqq9rGcAtu/s1600/model3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="530" data-original-width="1090" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaVdrTVmkh5XO5d_cBGIE1rcztlxaWy7n_yzRkY5Wvh60Hhzbl_5eGzjBmum9Xl3kngAdbxTgOfi2GdjhHIgdERdYNVgydY84YNWRqLa4ONWTxDjH8G35U28VvL50n2NYXBScqq9rGcAtu/s1600/model3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirKEt5hWqzgcRmyaopDp2sCrG253MsdhN_-Ok3mmKiBn-GdK6Y3v2GPIljwK1OWKtIIu0LhtvQtp22M_tEEs1-aLBOku17B8B8VtdLwA3cTez4W-y5U6ygkhWuxqP1kl9zHnW_EDpfuf86/s1600/model4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="411" data-original-width="1095" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirKEt5hWqzgcRmyaopDp2sCrG253MsdhN_-Ok3mmKiBn-GdK6Y3v2GPIljwK1OWKtIIu0LhtvQtp22M_tEEs1-aLBOku17B8B8VtdLwA3cTez4W-y5U6ygkhWuxqP1kl9zHnW_EDpfuf86/s1600/model4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;b&gt;D. Load Model Weights- &lt;/b&gt;Our next step is to load the downloaded weights. But we cannot load and read that file directly in keras since downloaded weights are written in Darknet architecture and we are using keras architecture here. For this reading and parsing purpose we can use following class-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhopPScVk2rEArGkUmLDHDIeyWupeMIDy7e91a65GDvSQPWylDvxIxrr7cMB5HJTNTsHetWWqhoj7jsM199jOLaNot1C5_aN3o2sX_9N3BGx2zqMk8yBrPQfvBvxVnhaH4yOlOA4hQDtWBL/s1600/save1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="462" data-original-width="682" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhopPScVk2rEArGkUmLDHDIeyWupeMIDy7e91a65GDvSQPWylDvxIxrr7cMB5HJTNTsHetWWqhoj7jsM199jOLaNot1C5_aN3o2sX_9N3BGx2zqMk8yBrPQfvBvxVnhaH4yOlOA4hQDtWBL/s1600/save1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUUOcrppu2sFdjuSk_rX_IV7Oo-bWNVBwmjGG9mGy7KqoaVOl6bG7GY70P-j7t1Etf3aWK4wX_KDTYuUTbXH2uzkZDGeK_F5CJq5nLc3MEqWzI9tVjOSrF7uRdzuTyY6zAvkCYRLOhemCV/s1600/save2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="656" data-original-width="855" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUUOcrppu2sFdjuSk_rX_IV7Oo-bWNVBwmjGG9mGy7KqoaVOl6bG7GY70P-j7t1Etf3aWK4wX_KDTYuUTbXH2uzkZDGeK_F5CJq5nLc3MEqWzI9tVjOSrF7uRdzuTyY6zAvkCYRLOhemCV/s1600/save2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
We can then easily use above functions and classes and then save result in keras format like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJfxLOPTs-XrxCrSqXj495UA11vu8-Kr4CMosxd1kIpxPKri7CAYBGI4AS3K3vJWNOoWWkl6mrD3DLrVGRw6sKMLh2BhaaUQ8H-baOvFqzmKCZ2KdQzeaRkgQtY7zONYb38VmXaw0Y94-P/s1600/save3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="209" data-original-width="428" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJfxLOPTs-XrxCrSqXj495UA11vu8-Kr4CMosxd1kIpxPKri7CAYBGI4AS3K3vJWNOoWWkl6mrD3DLrVGRw6sKMLh2BhaaUQ8H-baOvFqzmKCZ2KdQzeaRkgQtY7zONYb38VmXaw0Y94-P/s1600/save3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
That's it. We have successfully completed the complex part. Now we can use this model like any other keras model we have used. Once you run the above code, you will see following like output in console-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgg40dartx3jl-6RABJmyj0tTaSFWpCijj9povLRUCuDJz3HWjaAUG6M7vM1aJ3R8ZL_Lm-sXX_pq02_Zid60lQd-Nxls-jSDxVAaUOMqjubzngML71zZHqz8OxrO8Tt-H1RhmVjdo6FMES/s1600/save4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="549" data-original-width="325" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgg40dartx3jl-6RABJmyj0tTaSFWpCijj9povLRUCuDJz3HWjaAUG6M7vM1aJ3R8ZL_Lm-sXX_pq02_Zid60lQd-Nxls-jSDxVAaUOMqjubzngML71zZHqz8OxrO8Tt-H1RhmVjdo6FMES/s1600/save4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once the script is completed, in your current working directory 'model.h5' will be saved.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;E. Test the model- &lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
Like any other keras application, this model also requires input image in a defined shape. The YOLO system expects input shape of the image is in 416 x 416 pixels. You can use following code snippet to test the model-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinLOpwhjnzFLf6jxlR3e530gNRM1hXYXH8m72j_9trUMCCpB1SIx8xu75MEYaV2dU6Rf82bR3bWBAJc0IvEKDMlawI179rL-ZQjacD2e5YsXSGSSrpN6YpXD3c_rT0HIrSSVyltxhOFV2F/s1600/save5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="719" data-original-width="725" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinLOpwhjnzFLf6jxlR3e530gNRM1hXYXH8m72j_9trUMCCpB1SIx8xu75MEYaV2dU6Rf82bR3bWBAJc0IvEKDMlawI179rL-ZQjacD2e5YsXSGSSrpN6YpXD3c_rT0HIrSSVyltxhOFV2F/s1600/save5.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once you run the above code, it will display output in NumPy array format-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2s3iR16R7Bgetn5x7sCW4f7f9tcIa3Xuud2OzKFFb_Z5UW9zifXBJl9ioaXkFGUY0ptfnJ-tZe3XdsM0Xtg4RrqDv9mTbZPoC1KDNOe5t4qypOgEcp7gNqiqC8ca1wQra1bDe0oRZfbRx/s1600/save6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="38" data-original-width="584" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2s3iR16R7Bgetn5x7sCW4f7f9tcIa3Xuud2OzKFFb_Z5UW9zifXBJl9ioaXkFGUY0ptfnJ-tZe3XdsM0Xtg4RrqDv9mTbZPoC1KDNOe5t4qypOgEcp7gNqiqC8ca1wQra1bDe0oRZfbRx/s1600/save6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;F. Decode the output- &lt;/b&gt;Currently from the output we cannot say anything because it is NumPy format, in order to understand this we need to decode it. Here decoding means in terms of bounding boxes around our object. In the github repository of experiencor there is a function called &lt;b&gt;decode netout(), &lt;/b&gt;which takes each NumPy array from our output one by one and decode the bounding box and prediction-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP1_O1pUv7eCQIfWeYbTwqRvUzDa_GkakoSn8x0iblDTSDg3oCQAa7o5X2dJJ4dgfOPrKNluoP9Ve61oodiNU9OLx9y7Y16zd6u2uCvH49Pa4uNnLlj3qfUSGRppJ2p2GEqcuWoS_BeUZE/s1600/save7.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="741" data-original-width="833" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP1_O1pUv7eCQIfWeYbTwqRvUzDa_GkakoSn8x0iblDTSDg3oCQAa7o5X2dJJ4dgfOPrKNluoP9Ve61oodiNU9OLx9y7Y16zd6u2uCvH49Pa4uNnLlj3qfUSGRppJ2p2GEqcuWoS_BeUZE/s1600/save7.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once we apply the above function it will return the bounding boxes. But these bounding boxes can be stretched back into the shape of original image. To fix this issue the experiencor script provides the &lt;b&gt;correct_yolo_boxes()&lt;/b&gt; function-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwQ6-u3L7J_UfbceISwB926u16lxME3jOm0vms8Tnxrq1dCk0FM_tUg0QkO4SYWga6RXqlN7Qrenb71pQZuDvHyFIvwxmHWWuoC4dPqd9AMGg9OhD8-KQcU5suaUt0pCqZ_BctsoxfwJFG/s1600/save8.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="607" data-original-width="727" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwQ6-u3L7J_UfbceISwB926u16lxME3jOm0vms8Tnxrq1dCk0FM_tUg0QkO4SYWga6RXqlN7Qrenb71pQZuDvHyFIvwxmHWWuoC4dPqd9AMGg9OhD8-KQcU5suaUt0pCqZ_BctsoxfwJFG/s1600/save8.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Now we will get fixed bounding boxes but one issue with this is those bounding boxes may overlap. To fix this issue the experiencor script provides a &lt;b&gt;do_nms()&lt;/b&gt; function that takes the list of bounding boxes and a threshold parameter-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfa6qKdh-VUzvMa-BdBAtlb9paVpB5SWZ0p5MkohvBml2lDQaZuc-GAn-OX4ItEW-ZS1V3ZODlHq9r8hEibd6FAFhpnzR64sRtJMsAw4zJBA4k6VgeRZsDL6ctQKHG32yOY3yojg2mo4T7/s1600/save9.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="364" data-original-width="678" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfa6qKdh-VUzvMa-BdBAtlb9paVpB5SWZ0p5MkohvBml2lDQaZuc-GAn-OX4ItEW-ZS1V3ZODlHq9r8hEibd6FAFhpnzR64sRtJMsAw4zJBA4k6VgeRZsDL6ctQKHG32yOY3yojg2mo4T7/s1600/save9.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
Next, we need to assure to get only those bounding boxes which have strong presence of an object. For this we need to enumerate over all boxes and checking the class predictions. In this way we can then add class label. Following code snippet does the same-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhad1eq9YMJEOmz0CqgQKhW5zi26IttiUeVyRNOPt2dQak7oxsUwfe5VT1eIZUU2VRbIqj625yOsuLARgi2HVzki2RsMavRnDJlhnsx7BdQdXDCVD5dU8FuzDzI3kcxSEvjlwPHvwjtjYjK/s1600/do1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="363" data-original-width="608" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhad1eq9YMJEOmz0CqgQKhW5zi26IttiUeVyRNOPt2dQak7oxsUwfe5VT1eIZUU2VRbIqj625yOsuLARgi2HVzki2RsMavRnDJlhnsx7BdQdXDCVD5dU8FuzDzI3kcxSEvjlwPHvwjtjYjK/s1600/do1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
You can test this function on a list. In our case list contains the name of various objects like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVFoqf0dgUfvPlttmA51tCpQm8GC5n-bUHHSuEkvtMpsm2ku3prDs0JoNX26NqtG67vmkZ2cNYUGhV3xmDAZQeBmpgydEOxpbtMyJGHQlEAtf47JZAQmxtOixssu6Ti9svgEkY0i5CEoLZ/s1600/do2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="274" data-original-width="958" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVFoqf0dgUfvPlttmA51tCpQm8GC5n-bUHHSuEkvtMpsm2ku3prDs0JoNX26NqtG67vmkZ2cNYUGhV3xmDAZQeBmpgydEOxpbtMyJGHQlEAtf47JZAQmxtOixssu6Ti9svgEkY0i5CEoLZ/s1600/do2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Our next step is to draw the bounding boxes around our detected object. That can be done using below function-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCjB2C_3U2TCEPesjekqCuXAfGIQiFL5E39AxkYfxAh3LOvbrRJZ4Vb24ZkdILHh_EH7NYU-9_iFQ_4wcGBN28f8Ue7XVRGH94agwToxCF_RpMa2ZLjOpDejyUcxLU4i3t42VesuAbcFNT/s1600/do3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="605" data-original-width="694" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCjB2C_3U2TCEPesjekqCuXAfGIQiFL5E39AxkYfxAh3LOvbrRJZ4Vb24ZkdILHh_EH7NYU-9_iFQ_4wcGBN28f8Ue7XVRGH94agwToxCF_RpMa2ZLjOpDejyUcxLU4i3t42VesuAbcFNT/s1600/do3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Once you summarize all above functions or run the script provided in github repo, you will see bounding boxes and name of the object detected in your image like I am getting in my input image-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6ZXd3ukGPYHfsl0hS82JTrYqhTIkRIGr4DRek8HcYwU_SLRYoGYGVrmCu96xLRmtlVCtITWFaN6fGgFGeN-mnSZDGK1gKRzlkgoUqDxfsXG4g3Hn5CSzCqfB1IziuDvHa-XWktvBgQsRz/s1600/result1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="688" data-original-width="1353" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6ZXd3ukGPYHfsl0hS82JTrYqhTIkRIGr4DRek8HcYwU_SLRYoGYGVrmCu96xLRmtlVCtITWFaN6fGgFGeN-mnSZDGK1gKRzlkgoUqDxfsXG4g3Hn5CSzCqfB1IziuDvHa-XWktvBgQsRz/s1600/result1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
For my image, the model with pre-trained weights is showing amazing results. It is correctly able to detect bottle in my image. Check yourself on different images with different objects and see how this amazing model works in real time. I have used the same repositories to do R&amp;amp;D for my work and after a lot of practice and trials I was able to successfully use this model with my custom dataset. For now try the above steps I have shown you, read the code many times, change it's configurable values and analyze the effect.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;br /&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgf8jcCm8WSqHqZSBeSvXnufxVDegBC7bTx9wi-CRVuT_3K1omLevlwwnqeRmrpy13wyMxKEA3tU7mU8kRF26QkbG4qRzWKnmRT2aWv5Veo-OTM7QGTyGr0o-carPkXB7IpX6iIkwoNpdgF/s72-c/result.JPG" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">5</thr:total><enclosure length="248007048" type="application/octet-stream" url="https://pjreddie.com/media/files/yolov3.weights"/><itunes:explicit>no</itunes:explicit><itunes:subtitle>Another post starts with you beautiful people! In the past few months I was working on a complex object detection and recognition problem. My client is from a leading winery industry and they had an existing system built on&amp;nbsp;vgg19&amp;nbsp;and&amp;nbsp;keras-retinanet. This system was built to help client in their sales forecasting. The problem with current system was it was inaccurate- it did not detect most of the wine bottles and brands, it did not give the result in real time. You can imagine how this bad model can affect the business! To solve the existing issues I tried a lot of things- changing hyperparameters, increasing the datasets, different&amp;nbsp;keras applications&amp;nbsp;but it was not giving me satisfactory result. May be I was not doing it right but I had put a lot of time and efforts with it. Then while doing R&amp;amp;D, I read&amp;nbsp;this fantastic blog&amp;nbsp;and I came to know about a state of the art and real time object detection system-&amp;nbsp;YOLO. You Only Look Once or YOLO&amp;nbsp;is a custom deep learning framework written on C and you can read more about it in it's&amp;nbsp;official site. Instead of writing the code from scratch I found two github repository as third party implementation of YOLO version 3- experiencor/keras-yolo3&amp;nbsp; qqwweee/keras-yolo3 You can follow any of the above link and run this code to see how it works. In this post I will share how I tested this in my system. For testing this model on unseen pictures you need to follow below steps- A. Prepare your virtual environment- The first step before starting your object detection and recognition journey is to install all required libraries. I recommend to create a virtual environment and install all libraries there instead of installing in base location. This saves your base location in case of any corruption while installing. To create and activate the virtual environment, open anaconda prompt with admin rights and run following two commands one by one- conda create --name myNewEnv python=3.7.3 activate myNewEnv Here myNewEnv is the name of my virtual environment and 3.7.3 is my Python version. Replace it with your own name and version. Once you activate it, it's time to install required libraries. Before installing these libraries make sure you have Visual Studio 2017 with C++ extension is installed. If not then please install it and add C++ extension in it otherwise you will face unnecessary issues. Here I share list of mine which you can also use- pip install keras==2.2.4 pip install tensorflow-gpu==1.14.0 pip install scikit-learn conda install anaconda-client conda install -c conda-forge/label/cf201901 opencv pip install keras-retinanet conda install shapely conda install -c conda-forge imgaug conda install -c conda-forge google-cloud-vision conda install -c pjamesjoyce imutils conda install -c anaconda flask conda install -c conda-forge/label/cf201901 flask-restful pip install tqdm pip install boto3 pip install matplotlib pip install seaborn pip install xlrd pip install pytesseract pip install apscheduler In above list you can see I have used variation of conda and pip. It is because while installing opencv and keras-retinanet in my windows machine I faced so much issues and I resolved those issues after so many efforts. So it is recommended to use above command as exact as I have mentioned. This is one time setup and most of the required image processing related libraries are mentioned in this list. Once all the installation is successful you can proceed with next step. B. Download pre-trained weights- Second step is to download the pre-trained weights from&amp;nbsp;This Link. It's a 235 mb file with name yolov3.weights. C. Define keras model- Our next step is to define a keras model to match with the downloaded weights. It means our keras model should have right number of layers and right types of the layers to match with Yolo weights.&amp;nbsp; This is the actual complex part but in the github repositories I have shared earlier, you can find the functions written already for this task. So just copy those but don't forget to give a Star to the original authors in github. Here is screen shot of the code snippet- # Create block of layers # Create the model D. Load Model Weights- Our next step is to load the downloaded weights. But we cannot load and read that file directly in keras since downloaded weights are written in Darknet architecture and we are using keras architecture here. For this reading and parsing purpose we can use following class- We can then easily use above functions and classes and then save result in keras format like below- That's it. We have successfully completed the complex part. Now we can use this model like any other keras model we have used. Once you run the above code, you will see following like output in console- Once the script is completed, in your current working directory 'model.h5' will be saved. E. Test the model- Like any other keras application, this model also requires input image in a defined shape. The YOLO system expects input shape of the image is in 416 x 416 pixels. You can use following code snippet to test the model- Once you run the above code, it will display output in NumPy array format- F. Decode the output- Currently from the output we cannot say anything because it is NumPy format, in order to understand this we need to decode it. Here decoding means in terms of bounding boxes around our object. In the github repository of experiencor there is a function called decode netout(), which takes each NumPy array from our output one by one and decode the bounding box and prediction- Once we apply the above function it will return the bounding boxes. But these bounding boxes can be stretched back into the shape of original image. To fix this issue the experiencor script provides the correct_yolo_boxes() function- Now we will get fixed bounding boxes but one issue with this is those bounding boxes may overlap. To fix this issue the experiencor script provides a do_nms() function that takes the list of bounding boxes and a threshold parameter- Next, we need to assure to get only those bounding boxes which have strong presence of an object. For this we need to enumerate over all boxes and checking the class predictions. In this way we can then add class label. Following code snippet does the same- You can test this function on a list. In our case list contains the name of various objects like below- Our next step is to draw the bounding boxes around our detected object. That can be done using below function- Once you summarize all above functions or run the script provided in github repo, you will see bounding boxes and name of the object detected in your image like I am getting in my input image- For my image, the model with pre-trained weights is showing amazing results. It is correctly able to detect bottle in my image. Check yourself on different images with different objects and see how this amazing model works in real time. I have used the same repositories to do R&amp;amp;D for my work and after a lot of practice and trials I was able to successfully use this model with my custom dataset. For now try the above steps I have shown you, read the code many times, change it's configurable values and analyze the effect.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.</itunes:subtitle><itunes:author>noreply@blogger.com (Learn Data Science with PrateekG)</itunes:author><itunes:summary>Another post starts with you beautiful people! In the past few months I was working on a complex object detection and recognition problem. My client is from a leading winery industry and they had an existing system built on&amp;nbsp;vgg19&amp;nbsp;and&amp;nbsp;keras-retinanet. This system was built to help client in their sales forecasting. The problem with current system was it was inaccurate- it did not detect most of the wine bottles and brands, it did not give the result in real time. You can imagine how this bad model can affect the business! To solve the existing issues I tried a lot of things- changing hyperparameters, increasing the datasets, different&amp;nbsp;keras applications&amp;nbsp;but it was not giving me satisfactory result. May be I was not doing it right but I had put a lot of time and efforts with it. Then while doing R&amp;amp;D, I read&amp;nbsp;this fantastic blog&amp;nbsp;and I came to know about a state of the art and real time object detection system-&amp;nbsp;YOLO. You Only Look Once or YOLO&amp;nbsp;is a custom deep learning framework written on C and you can read more about it in it's&amp;nbsp;official site. Instead of writing the code from scratch I found two github repository as third party implementation of YOLO version 3- experiencor/keras-yolo3&amp;nbsp; qqwweee/keras-yolo3 You can follow any of the above link and run this code to see how it works. In this post I will share how I tested this in my system. For testing this model on unseen pictures you need to follow below steps- A. Prepare your virtual environment- The first step before starting your object detection and recognition journey is to install all required libraries. I recommend to create a virtual environment and install all libraries there instead of installing in base location. This saves your base location in case of any corruption while installing. To create and activate the virtual environment, open anaconda prompt with admin rights and run following two commands one by one- conda create --name myNewEnv python=3.7.3 activate myNewEnv Here myNewEnv is the name of my virtual environment and 3.7.3 is my Python version. Replace it with your own name and version. Once you activate it, it's time to install required libraries. Before installing these libraries make sure you have Visual Studio 2017 with C++ extension is installed. If not then please install it and add C++ extension in it otherwise you will face unnecessary issues. Here I share list of mine which you can also use- pip install keras==2.2.4 pip install tensorflow-gpu==1.14.0 pip install scikit-learn conda install anaconda-client conda install -c conda-forge/label/cf201901 opencv pip install keras-retinanet conda install shapely conda install -c conda-forge imgaug conda install -c conda-forge google-cloud-vision conda install -c pjamesjoyce imutils conda install -c anaconda flask conda install -c conda-forge/label/cf201901 flask-restful pip install tqdm pip install boto3 pip install matplotlib pip install seaborn pip install xlrd pip install pytesseract pip install apscheduler In above list you can see I have used variation of conda and pip. It is because while installing opencv and keras-retinanet in my windows machine I faced so much issues and I resolved those issues after so many efforts. So it is recommended to use above command as exact as I have mentioned. This is one time setup and most of the required image processing related libraries are mentioned in this list. Once all the installation is successful you can proceed with next step. B. Download pre-trained weights- Second step is to download the pre-trained weights from&amp;nbsp;This Link. It's a 235 mb file with name yolov3.weights. C. Define keras model- Our next step is to define a keras model to match with the downloaded weights. It means our keras model should have right number of layers and right types of the layers to match with Yolo weights.&amp;nbsp; This is the actual complex part but in the github repositories I have shared earlier, you can find the functions written already for this task. So just copy those but don't forget to give a Star to the original authors in github. Here is screen shot of the code snippet- # Create block of layers # Create the model D. Load Model Weights- Our next step is to load the downloaded weights. But we cannot load and read that file directly in keras since downloaded weights are written in Darknet architecture and we are using keras architecture here. For this reading and parsing purpose we can use following class- We can then easily use above functions and classes and then save result in keras format like below- That's it. We have successfully completed the complex part. Now we can use this model like any other keras model we have used. Once you run the above code, you will see following like output in console- Once the script is completed, in your current working directory 'model.h5' will be saved. E. Test the model- Like any other keras application, this model also requires input image in a defined shape. The YOLO system expects input shape of the image is in 416 x 416 pixels. You can use following code snippet to test the model- Once you run the above code, it will display output in NumPy array format- F. Decode the output- Currently from the output we cannot say anything because it is NumPy format, in order to understand this we need to decode it. Here decoding means in terms of bounding boxes around our object. In the github repository of experiencor there is a function called decode netout(), which takes each NumPy array from our output one by one and decode the bounding box and prediction- Once we apply the above function it will return the bounding boxes. But these bounding boxes can be stretched back into the shape of original image. To fix this issue the experiencor script provides the correct_yolo_boxes() function- Now we will get fixed bounding boxes but one issue with this is those bounding boxes may overlap. To fix this issue the experiencor script provides a do_nms() function that takes the list of bounding boxes and a threshold parameter- Next, we need to assure to get only those bounding boxes which have strong presence of an object. For this we need to enumerate over all boxes and checking the class predictions. In this way we can then add class label. Following code snippet does the same- You can test this function on a list. In our case list contains the name of various objects like below- Our next step is to draw the bounding boxes around our detected object. That can be done using below function- Once you summarize all above functions or run the script provided in github repo, you will see bounding boxes and name of the object detected in your image like I am getting in my input image- For my image, the model with pre-trained weights is showing amazing results. It is correctly able to detect bottle in my image. Check yourself on different images with different objects and see how this amazing model works in real time. I have used the same repositories to do R&amp;amp;D for my work and after a lot of practice and trials I was able to successfully use this model with my custom dataset. For now try the above steps I have shown you, read the code many times, change it's configurable values and analyze the effect.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.</itunes:summary><itunes:keywords>computer vision, deep learning, keras, yolov3</itunes:keywords></item><item><title>How to use Keras for Computer Vision- Part 3</title><link>https://dsbyprateekg.blogspot.com/2019/07/how-to-use-keras-for-computer-vision_17.html</link><category>color images</category><category>convolution</category><category>keras</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Wed, 17 Jul 2019 14:16:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-4408289925079292329</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjw-F4Fj_jugUxy86QY2sB4WHD2xtG2rkdAHXtnPF7Qc_nOmT1Bhp7jhTMz3vSBkWqc_2LcFcYb17xq0QTjWE-PXaRUloitGaSnf6C4nzmmODV7jtW0hJtbdo1zU2SOHGyWXPElZw9RwFyS/s1600/k1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="225" data-original-width="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjw-F4Fj_jugUxy86QY2sB4WHD2xtG2rkdAHXtnPF7Qc_nOmT1Bhp7jhTMz3vSBkWqc_2LcFcYb17xq0QTjWE-PXaRUloitGaSnf6C4nzmmODV7jtW0hJtbdo1zU2SOHGyWXPElZw9RwFyS/s1600/k1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
Another post starts with you beautiful people!&lt;br /&gt;
I hope you have enjoyed my&amp;nbsp;&lt;b&gt;&lt;a href="https://dsbyprateekg.blogspot.com/2019/07/how-to-use-keras-for-computer-vision_13.html" target="_blank"&gt;last post&lt;/a&gt;&amp;nbsp;&lt;/b&gt;about&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2019/07/how-to-use-keras-for-computer-vision_13.html" target="_blank"&gt;image data augmentation&lt;/a&gt;&amp;nbsp;and now you are comfortable with increasing the size of your small training dataset. Today's post is going to be interesting because we are going to learn how to handle color images and examples of using convolution with keras. Let's starts our learning-&lt;br /&gt;
&lt;br /&gt;
In our first post of this series we have seen that typically an image can be stored in 3-dimensional format- one is for height, one is for width and one is for channels. Colored images have three channels- red,blue,green components. A deep learning network requires that image data should be provided as a 3-D arrays. There are 2 ways to represent the image data as a 3-D array. First way is known as '&lt;b&gt;channel last&lt;/b&gt;' and second way is known as '&lt;b&gt;channel first&lt;/b&gt;'.&lt;br /&gt;
&lt;br /&gt;
In 'channel last', last channel represents the color channels while in 'channel first', first channel represents the color channels. Please note some deep learning libraries prefer channel first ordering while some prefers last channel ordering. For example If we are using Tensorflow backend with keras then it prefers 'channels last' ordering. If you want to see the configuration file of keras with this setting you need to open &lt;b&gt;keras.json&lt;/b&gt; file which resides in your home directory under &lt;b&gt;.keras folder&lt;/b&gt;. In my system it is in following path:&amp;nbsp;C:\Users\gupta\.keras and it's look like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0xlXwoN4XqiHUYt-_7wy8nSyTMqDdI9UdwOWR-qg6QvwwfXGv1odNXLcGtnNmwZ0FUQ38waWSol1bAJqOH_tPuA1K4ZZU5uUqoKKRo7pBKZp1DNDrrDX5RfTIeZorRlsmMG4UlT-I734p/s1600/k2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="171" data-original-width="481" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0xlXwoN4XqiHUYt-_7wy8nSyTMqDdI9UdwOWR-qg6QvwwfXGv1odNXLcGtnNmwZ0FUQ38waWSol1bAJqOH_tPuA1K4ZZU5uUqoKKRo7pBKZp1DNDrrDX5RfTIeZorRlsmMG4UlT-I734p/s1600/k2.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
You can modify above settings as per your need.&lt;br /&gt;
&lt;br /&gt;
Let's see &lt;b&gt;how can we use NumPy library to add a channel to a grayscale image?&lt;/b&gt;&lt;br /&gt;
Grayscale images are loaded as 2-D array so before modeling we need to add a channel dimension to that image. This means we are not adding any new data to the image, infact we are changing the array structure to have an extra axes with one dimension so that it can hold grayscale pixel values. NumPy has &lt;b&gt;expand_dims()&lt;/b&gt; function for doing this task. Let's load a color image using Pillow library and see how can we use this function-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQcmEaPkNC3Bi3GwckO1jOIh0vzXCtzt9wA3ru6GYIKa786XlwZNGqA1LUIFfQkcIcNBP2fAqnsaxM9rxITdZd9FoLO6Zby6bWf9zH_l68IHWBo-fomPhOjGG6Gy-iAuTdJ0TBvOeCVy9/s1600/k1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="502" data-original-width="858" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQcmEaPkNC3Bi3GwckO1jOIh0vzXCtzt9wA3ru6GYIKa786XlwZNGqA1LUIFfQkcIcNBP2fAqnsaxM9rxITdZd9FoLO6Zby6bWf9zH_l68IHWBo-fomPhOjGG6Gy-iAuTdJ0TBvOeCVy9/s1600/k1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
In above input cell you can see we have used '&lt;b&gt;axes&lt;/b&gt;' argument with &lt;b&gt;expand_dims()&lt;/b&gt; function to define channel first and channel last techniques. Our loaded image has dimension of (375,500) which is a 2-D array. Once we added the channel first then it's dimension changed to a 3-D array (1,375,500) and when we added channel last then it became (375,500,1) dimension array.&lt;br /&gt;
&lt;br /&gt;
Now we will move further and learn about &lt;b&gt;Convolution in computer vision.&amp;nbsp;&lt;/b&gt;A convolution is the simple application of a filter to an input that results in an activation. It is a common technique used in CV.&amp;nbsp;Convolution neural networks do not learn a single filter; they learn multiple features in parallel for a given input. For color image filter must always have the same number of channels as the input, also known as depth. If an input image has 3 channels then a filter applied to that image must also have 3 channels. In this case, a 3 × 3 filter would be 3 × 3 × 3 or [3, 3, 3] for rows, columns, and depth.&lt;br /&gt;
&lt;br /&gt;
Let's see a simple example of 1-D Convolution Layer: [0,0,0,1,1,0,0,0]. In this example we have eight elements.Now to use this input in keras we need to pass it as 3 dimension. We can represent this as [1,8,1] where first dimension represents the input sample, second dimension represents length of each sample and third dimension represents no of channels in each sample. So in keras we represent our 1-D convolution layer as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgze0TF8M-UpEX3GbBzJCTKqHP0UarMXcL5oH9_U2izyiBw1dKujch2NfQPkKQD7tXDxylWyPXydZwD6AGYLdFy87HauSyxnF4QiS4x_EGAmOeBq_F6bHZqb2M9i-6BWODCyJ12Wn3zrcjw/s1600/k3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="65" data-original-width="431" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgze0TF8M-UpEX3GbBzJCTKqHP0UarMXcL5oH9_U2izyiBw1dKujch2NfQPkKQD7tXDxylWyPXydZwD6AGYLdFy87HauSyxnF4QiS4x_EGAmOeBq_F6bHZqb2M9i-6BWODCyJ12Wn3zrcjw/s1600/k3.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
Next, we will define/create a model using keras' Sequential api as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDsw0MUCmiURsKGQVZM-X0wBMk9cjFDTC5zTT8QwyFX38DZH0ypg6WLNDLOlSLf60YNdCZgjAZW0g-ulf2l8A3Xyf767K8JYcyxkaW92AxkdPb7o91JVL1TnNWX2bA72S21L1vQq5S65lR/s1600/k4.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="131" data-original-width="753" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDsw0MUCmiURsKGQVZM-X0wBMk9cjFDTC5zTT8QwyFX38DZH0ypg6WLNDLOlSLf60YNdCZgjAZW0g-ulf2l8A3Xyf767K8JYcyxkaW92AxkdPb7o91JVL1TnNWX2bA72S21L1vQq5S65lR/s1600/k4.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
In above input cell we have added our convolution 1-D layer with some required arguments. Here input_shape() argument represents input samples which is (8,1). kernel_size represents the shape of the filter which is 3 in our case and the filters argument represents the default filter of Conv1D layer which is 1 in our example.&amp;nbsp;By default, the filters in a convolution layer are initialized with random weights. Let's apply this model to our input data using predict() function-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjRS7fuo4cdbyiZS5H24gZ8ExHY7TPlM8FQ39s4aCliy9ka6IrzOOGPPtOMOiO8Pe7L95HEIH-H0a18Y7hsXbPsf29jt9d8hLTqKHmPyhqvGWlAJNtsdi2gRb4ex3MD9Cpq5C-4Tg96bxY/s1600/k5.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="249" data-original-width="353" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjRS7fuo4cdbyiZS5H24gZ8ExHY7TPlM8FQ39s4aCliy9ka6IrzOOGPPtOMOiO8Pe7L95HEIH-H0a18Y7hsXbPsf29jt9d8hLTqKHmPyhqvGWlAJNtsdi2gRb4ex3MD9Cpq5C-4Tg96bxY/s320/k5.JPG" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
In above output you can see some negative values. This is because each filter in convolution layer has a bias input value which requires to manually specify a weight that will set to zero. We can achieve this by forcing the weights in our convolution layer to use a handcrafted filter. In our case it will be like [0,1,0]. So with this change our code will be like below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgihipFfEjHlT-gK3cH8B5_e2snkZVcGc71PWcmnxeVAGH0sABZrwsqT8Qp23zUEOlLoG477Y-zvWTzeS_eSSY_BJ3uQy9X9Z_PjXf01Au3xqQ0VJGkVgD-y2dvxNeyEuFgGOGn-2xmC4Z3/s1600/k6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="474" data-original-width="758" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgihipFfEjHlT-gK3cH8B5_e2snkZVcGc71PWcmnxeVAGH0sABZrwsqT8Qp23zUEOlLoG477Y-zvWTzeS_eSSY_BJ3uQy9X9Z_PjXf01Au3xqQ0VJGkVgD-y2dvxNeyEuFgGOGn-2xmC4Z3/s1600/k6.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
Now you can see with our custom filter our model's prediction is correct!&lt;br /&gt;
&lt;br /&gt;
In the same way you can use convolution 2-D layer. Here you need to use Conv2D() instead of Conv1D() layer. Also remember in this case input must be 4-dimensional where first dimension defines the samples, second dimension defines the rows, the third dimension defines the columns and the last dimension defines the channels.For in depth knowledge, I would recommend you to read following awesome blog-&lt;br /&gt;
&lt;a href="https://machinelearningmastery.com/convolutional-layers-for-deep-learning-neural-networks/"&gt;https://machinelearningmastery.com/convolutional-layers-for-deep-learning-neural-networks/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
That's it for today. We have learnt color channel ordering and basics of convolution in computer vision. Try above examples with 2-D layer and see what changes do you need. In my next post we will learn more about filter and kernel in convolution neural networks.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjw-F4Fj_jugUxy86QY2sB4WHD2xtG2rkdAHXtnPF7Qc_nOmT1Bhp7jhTMz3vSBkWqc_2LcFcYb17xq0QTjWE-PXaRUloitGaSnf6C4nzmmODV7jtW0hJtbdo1zU2SOHGyWXPElZw9RwFyS/s72-c/k1.jpg" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><title>How to use Keras for Computer Vision- Part 2</title><link>https://dsbyprateekg.blogspot.com/2019/07/how-to-use-keras-for-computer-vision_13.html</link><category>augmentation</category><category>computer vision</category><category>imagedatagenerator</category><category>keras</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Sat, 13 Jul 2019 22:21:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-5671681461020403773</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBKF_mX-mruvDkLE3TZ5RYKZ-WqPn9GPuf5KImWJs412SS-a_ZMXPup1JBBbn3JwuDvXO3S1lmAe6RCrxV4olWwJTtlYVlKnecSwQh7pNEhi2iLuNSxMRmZebVsGARbV6TI_FvxVXPeje4/s1600/1_Lv7IwOePp0lyYX0PE_xfew.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="500" data-original-width="1500" height="106" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBKF_mX-mruvDkLE3TZ5RYKZ-WqPn9GPuf5KImWJs412SS-a_ZMXPup1JBBbn3JwuDvXO3S1lmAe6RCrxV4olWwJTtlYVlKnecSwQh7pNEhi2iLuNSxMRmZebVsGARbV6TI_FvxVXPeje4/s320/1_Lv7IwOePp0lyYX0PE_xfew.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Another post starts with you beautiful people!&lt;br /&gt;
I hope you have enjoyed my&amp;nbsp;&lt;b&gt;&lt;a href="https://dsbyprateekg.blogspot.com/2019/07/how-to-use-keras-for-computer-vision.html" target="_blank"&gt;last post&lt;/a&gt;&amp;nbsp;&lt;/b&gt;and now you have become familiar with image loading, manipulation and saving in deep learning. If you have not read my last post then I recommend you to read the&amp;nbsp;&lt;b&gt;&lt;a href="https://dsbyprateekg.blogspot.com/2019/07/how-to-use-keras-for-computer-vision.html" target="_blank"&gt;first part&lt;/a&gt;&amp;nbsp;&lt;/b&gt;of this series. In this second part of my post we will learn about one of the most important technique used for a small image training dataset- &lt;b&gt;Image&lt;/b&gt;&amp;nbsp;&lt;b&gt;Data Augmentation&lt;/b&gt;!&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;1. What is Image Data Augmentation?&lt;/b&gt;&lt;br /&gt;
Image Data Augmentation is a technique to expand the size of a training dataset. This technique can create modified versions of images which helps when we have a small dataset. Image shifting, flipping and zooming are some examples of this technique. Keras provides &lt;b&gt;ImageDataGenerator&lt;/b&gt; class for this purpose.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;2. How to use this technique?&lt;/b&gt;&lt;br /&gt;
We will learn different uses of this technique one by one by applying on an image-&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;A. How to shift an image?&lt;/b&gt;&lt;br /&gt;
Shifting an image means we are moving all pixels in one direction while keeping the image dimensions the same. ImageDataGenerator class has&amp;nbsp;&lt;b&gt;width_shift_range&lt;/b&gt; and &lt;b&gt;height_shift_range &lt;/b&gt;arguements for shifting an image. Let's see how can we horizontal shift an image-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRaD9u4MRSZAg9bY-TyFNr1hc72Gaq7sTYZbGn8qiTzvqDwOYBjeMhsvVA4goe3kWmNPNvKfzUAyNBfjBCioMW2lz-qLVZWvN8u_4VKYkP13HrFHeBZRi5y_q85UpTXuLvTUKtQm27u87b/s1600/k1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="697" data-original-width="794" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRaD9u4MRSZAg9bY-TyFNr1hc72Gaq7sTYZbGn8qiTzvqDwOYBjeMhsvVA4goe3kWmNPNvKfzUAyNBfjBCioMW2lz-qLVZWvN8u_4VKYkP13HrFHeBZRi5y_q85UpTXuLvTUKtQm27u87b/s1600/k1.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
In above code cell I have loaded an image of ICC Cricket World Cup 2019 Trophy. I will horizontal shift this image using &lt;b&gt;width_shift_range &lt;/b&gt;arguement. This arguement takes min and max range. In our example I am taking -150 and 150 as min and max pixels. After creating the iterator we are&amp;nbsp; calling it nine times for augmentation. Once we run above cell we get following output-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEuzRMZr59qjEv9qSjMF9Sxpey2Aut7nOjhyphenhyphen-KdEVpgah_FrKz2sMU4Rs-3ndxM039n5TOfwN41FeuxQzbcY_fPvSEpKdUsTX7Ceggva2fD9jfP-uQrJQYVFr4GFyw7SPU7I7JzauvobYg/s1600/k2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="359" data-original-width="526" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEuzRMZr59qjEv9qSjMF9Sxpey2Aut7nOjhyphenhyphen-KdEVpgah_FrKz2sMU4Rs-3ndxM039n5TOfwN41FeuxQzbcY_fPvSEpKdUsTX7Ceggva2fD9jfP-uQrJQYVFr4GFyw7SPU7I7JzauvobYg/s1600/k2.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
In above output cell you can see our loaded image is augmented 9 times. The plot is showing randomly selected positive and negative horizontal shifts. You can try the same code but with height_shift_range arguement for vertical shifting. Remember in this case you need to pass the percentage of image to shift (for example height_shift_range=0.4) instead of [min,max] values.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;B. How to flip an image?&lt;/b&gt;&lt;br /&gt;
Flipping an image means reversing the rows or column of pixels. The ImageDataGenerator class has &lt;b&gt;horizontal_flip &lt;/b&gt;and &lt;b&gt;vertical_flip &lt;/b&gt;arguements for flipping. Let's see how can we augment an image with horizontal flip-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHWGqlbu5_YmrtZIiHqvoaWKXd8fcXDlYIZIyGsfq0CutUYirVSJK-Gp2FxufaU3QqZXBFTRwvAxebP0zawRa8w9t5WhrGiNSIrCvk3LKguczkAQKCnXY7mhk9nVUVAKjjmYYAqT8cEYqH/s1600/k3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="519" data-original-width="651" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHWGqlbu5_YmrtZIiHqvoaWKXd8fcXDlYIZIyGsfq0CutUYirVSJK-Gp2FxufaU3QqZXBFTRwvAxebP0zawRa8w9t5WhrGiNSIrCvk3LKguczkAQKCnXY7mhk9nVUVAKjjmYYAqT8cEYqH/s1600/k3.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
The above code is quite same as we saw earliar except one change. Here we are passing horizontal_flip arguement which takes a boolean value- either True or False. After running the above cell we will get following plot-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxRsa43WMKZRF8jGQZjsbRUiSXShw-5p-V6gj3h1UkWK-L3LmhhLQpAFM9g28cBIqxbwpRQQTzWyB2_wYLoOZOmKBnilZhfv6h0bYfINpHqx1PNbkuO5gQ6EjFnnC4EotHsQbY0rpmncnf/s1600/k4.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="349" data-original-width="524" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxRsa43WMKZRF8jGQZjsbRUiSXShw-5p-V6gj3h1UkWK-L3LmhhLQpAFM9g28cBIqxbwpRQQTzWyB2_wYLoOZOmKBnilZhfv6h0bYfINpHqx1PNbkuO5gQ6EjFnnC4EotHsQbY0rpmncnf/s1600/k4.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
Compare this plot with first one. What changes are you seeing?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;C. How to rotate an image?&lt;/b&gt;&lt;br /&gt;
Image rotation is also part of augmentation technique.We can randomly rotate the image clockwise by just giving a number between 0 to 360 in &lt;b&gt;rotation_range &lt;/b&gt;arguement. Let's see how can we do it-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP24XbRFWepz6qPJ4GqpgBqipqCaRbFu9a9naE219goAHFa6jxu97rNcjLhUU57-8HKBnaMe0XLQEHniuOS8gY-Q6sAkEN1TZsZfGSam-8GrYMSDXT4n8Ttu7H0s_ITwTnmgxeO6qf5RzT/s1600/k5.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="525" data-original-width="651" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP24XbRFWepz6qPJ4GqpgBqipqCaRbFu9a9naE219goAHFa6jxu97rNcjLhUU57-8HKBnaMe0XLQEHniuOS8gY-Q6sAkEN1TZsZfGSam-8GrYMSDXT4n8Ttu7H0s_ITwTnmgxeO6qf5RzT/s1600/k5.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
Once we run above cell we see our loaded image is rotated as below-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkyvXjn4AsZ4zx5mKD9AjWeu7SZp1qBhPBE58tdGAHNqX3E7l0LVfob3BMTaxqge4cPqvx1Oa33VCGwv3K-QL5d-J_V5NsZWL4LUQS7X8t5tllIqd_1R_AB-B3u0AjrD7w-v0eGsS8w9hx/s1600/k6.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="356" data-original-width="516" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkyvXjn4AsZ4zx5mKD9AjWeu7SZp1qBhPBE58tdGAHNqX3E7l0LVfob3BMTaxqge4cPqvx1Oa33VCGwv3K-QL5d-J_V5NsZWL4LUQS7X8t5tllIqd_1R_AB-B3u0AjrD7w-v0eGsS8w9hx/s1600/k6.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;D. How to augment an image by brightness?&lt;/b&gt;&lt;br /&gt;
We can augment an image by it's brightness using the &lt;b&gt;brightness_range &lt;/b&gt;arguement. This arguement takes min and max range indicating percentage of darkness or brightness amount. Let's see how can we use this type of augmentation-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA88eh_cPNTRMjLv0YkTTe9BW-a_dLSlqAxwPY0RRTI_zbTQPgB_CvR0Hv9sFWRdSnCLKzAe8jwsmTg0reRbRphdz1SLyYcZPpioojNpadr5FNdz_7wTrhCk3aO6wOpYFKREjzpL98h6tH/s1600/k7.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="515" data-original-width="677" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA88eh_cPNTRMjLv0YkTTe9BW-a_dLSlqAxwPY0RRTI_zbTQPgB_CvR0Hv9sFWRdSnCLKzAe8jwsmTg0reRbRphdz1SLyYcZPpioojNpadr5FNdz_7wTrhCk3aO6wOpYFKREjzpL98h6tH/s1600/k7.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
In above code cell I have given brightness_range arguement with 0.2 and 1.0 value. Here value less than 1.0 means darkening the image while larger than 1.0 means brighten the image whereas 1.0 means no affect on brightness. running above cell gives us following plot-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjO0phZtfVv-JSCpVZDqvRek5vuDzN-_6-e8LXLRYAnQQIzVIQ96G-N2EMY9CAN-RNyV4YyJXjYzQ6UXvVLVeNdi3vINHZU5mJ96FUe95DMAWP0YUzB17a7UZMTiLOVR-bZAh0fmrqzmNrn/s1600/k8.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="350" data-original-width="528" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjO0phZtfVv-JSCpVZDqvRek5vuDzN-_6-e8LXLRYAnQQIzVIQ96G-N2EMY9CAN-RNyV4YyJXjYzQ6UXvVLVeNdi3vINHZU5mJ96FUe95DMAWP0YUzB17a7UZMTiLOVR-bZAh0fmrqzmNrn/s1600/k8.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;E. How to apply zoom augmentation?&lt;/b&gt;&lt;br /&gt;
Zoom augmentation randomly zooms an image. We can use &lt;b&gt;zoom_range &lt;/b&gt;arguement which takes percentage of zoom as value. The zoom values less than 1.0 will zoom the image in and values larger than 1.0 will zoom the image out. Let's see how can we do that-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHkAWiX2fFfBbWExfz-xvdFRpWiVIBl6P_ebFq-jl3VqGvIZGjYDC5kZiAYKInKp_7umV302NlpPnt6KWdzQDm6weMFoPwxNaZGNwTyrVKGX99lC__QROK_qOl_OW7dngy9O0QMFwc-Mzc/s1600/k9.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="524" data-original-width="645" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHkAWiX2fFfBbWExfz-xvdFRpWiVIBl6P_ebFq-jl3VqGvIZGjYDC5kZiAYKInKp_7umV302NlpPnt6KWdzQDm6weMFoPwxNaZGNwTyrVKGX99lC__QROK_qOl_OW7dngy9O0QMFwc-Mzc/s1600/k9.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
Running above code gives you following result having zoom in effect-&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVb6bhOGK8V2LJ5Y124c0K6QGQG3qOIKS23rZn1bKbBKubw8Z3I2nnEe9aMKmFGXTbJ-THpmhRN5goqFXQFS-T_ydn2Sr18xwDkoiu3WgtAdkxm5EhPvgF5xtagpLLy_FX2YZ_CKJDwXoZ/s1600/k10.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="347" data-original-width="515" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVb6bhOGK8V2LJ5Y124c0K6QGQG3qOIKS23rZn1bKbBKubw8Z3I2nnEe9aMKmFGXTbJ-THpmhRN5goqFXQFS-T_ydn2Sr18xwDkoiu3WgtAdkxm5EhPvgF5xtagpLLy_FX2YZ_CKJDwXoZ/s1600/k10.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
That's it for today. You can save the augmented images using the save_img() function as we did in our first post. In this way you can larger your small image training datatset. We have learnt 5 types of image augmentation. Apply each technique on your own image and experiment it with different different values of arguements. For more details of image data augmentation I recommend you to read following two awesome blogs-&lt;br /&gt;
1.&amp;nbsp;&lt;a href="https://www.pyimagesearch.com/2019/07/08/keras-imagedatagenerator-and-data-augmentation/"&gt;&lt;b&gt;https://www.pyimagesearch.com/2019/07/08/keras-imagedatagenerator-and-data-augmentation/&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;
2.&amp;nbsp;&lt;a href="https://machinelearningmastery.com/how-to-configure-image-data-augmentation-when-training-deep-learning-neural-networks/"&gt;&lt;b&gt;https://machinelearningmastery.com/how-to-configure-image-data-augmentation-when-training-deep-learning-neural-networks/&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;
In next post of this series we will learn about the different colour channel ordering.&amp;nbsp;Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBKF_mX-mruvDkLE3TZ5RYKZ-WqPn9GPuf5KImWJs412SS-a_ZMXPup1JBBbn3JwuDvXO3S1lmAe6RCrxV4olWwJTtlYVlKnecSwQh7pNEhi2iLuNSxMRmZebVsGARbV6TI_FvxVXPeje4/s72-c/1_Lv7IwOePp0lyYX0PE_xfew.png" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">4</thr:total></item><item><title>How to use Keras for Computer Vision- Part 1</title><link>https://dsbyprateekg.blogspot.com/2019/07/how-to-use-keras-for-computer-vision.html</link><category>computer vision</category><category>image scaling</category><category>imagedatagenerator</category><category>keras</category><author>noreply@blogger.com (Learn Data Science with PrateekG)</author><pubDate>Tue, 9 Jul 2019 21:59:00 +0530</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8172482839059562238.post-9079889674963269927</guid><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnRg4cp-vkUiK21EFhymad9WY9PIXLD1TRx0G8Sy4JUEtOdMgHAfSmb_ssjk_54Aaidd_Zt5BwNHEJtuDHcjJ_bNRYFVBbXRZ2Yk7tdtCNR8k5J1cslKIBwSl1Z9hlOpTbWzdOBZwmWGHN/s1600/k3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="218" data-original-width="231" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnRg4cp-vkUiK21EFhymad9WY9PIXLD1TRx0G8Sy4JUEtOdMgHAfSmb_ssjk_54Aaidd_Zt5BwNHEJtuDHcjJ_bNRYFVBbXRZ2Yk7tdtCNR8k5J1cslKIBwSl1Z9hlOpTbWzdOBZwmWGHN/s1600/k3.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
Another post starts with you beautiful people! Today we are going to enter into the &lt;b&gt;Computer Vision (CV)&lt;/b&gt; field with this first post. With this and further coming posts, we will learn how to use Deep Learning&amp;nbsp;for CV. We will cover following three key concepts with Python code in this post-&lt;br /&gt;
1. What is CV?&lt;br /&gt;
2. How to load and manipulate image using Keras?&lt;br /&gt;
3. How to scale your image using Keras?&lt;br /&gt;
Let's starts our learning-&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;1. What is CV?&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
Computer Vision or CV is a sub-field of Machine Learning and Artificial Intelligence, focused on the problem of helping computers to see. The goal of CV is to understand the content of digital images. CV has a wide variety of tasks-&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Object Classification&lt;/li&gt;
&lt;li&gt;Object Identification&lt;/li&gt;
&lt;li&gt;Object Verification&lt;/li&gt;
&lt;li&gt;Object Detection&lt;/li&gt;
&lt;li&gt;Object Segmentation&lt;/li&gt;
&lt;li&gt;Object Recognition&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;2. How to load and manipulate image using Keras?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;A.&lt;/b&gt; &lt;b&gt;Loading&lt;/b&gt; an image using Keras api is very easy. Keras provides &lt;b&gt;load_img()&lt;/b&gt; function for loading an image. Before moving further make sure you have install keras. If not then run following command in Anaconda Prompt: &lt;b&gt;conda install keras&lt;/b&gt;. The below Python code snippet demonstrates how to use this function-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZgQ_A8azYrFac4n4swsh57EJR2K9e8B2K3oLC-3bWTOb3woVepjcoO2sCEPYBD3Muy_21wmfkglnQxEXEVwxY97AdrCEHlibFK8YI_a9xbrrD3anevyoC1DHrXnPEJtwS9yxZm-m_RDZ8/s1600/k1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="347" data-original-width="615" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZgQ_A8azYrFac4n4swsh57EJR2K9e8B2K3oLC-3bWTOb3woVepjcoO2sCEPYBD3Muy_21wmfkglnQxEXEVwxY97AdrCEHlibFK8YI_a9xbrrD3anevyoC1DHrXnPEJtwS9yxZm-m_RDZ8/s1600/k1.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In above output shell we can see my uploaded image format, it's mode and size. The image will not open in our notebook. It will be displayed using default application. In my case it was displayed by Photos app-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvNqlRm9sqanNx_-pJR5nYf3fup1w6eU-8CTXbvlUmVylB19MC7WseSWli8iQb_V6oFi5sLkWt6RFi3xJ6gsFTBso0CG96iGxosn0WsNXTKOOcxuAqsjq3VSnEroJWJcV63IcIHuyuGfv7/s1600/k3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="707" data-original-width="844" height="335" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvNqlRm9sqanNx_-pJR5nYf3fup1w6eU-8CTXbvlUmVylB19MC7WseSWli8iQb_V6oFi5sLkWt6RFi3xJ6gsFTBso0CG96iGxosn0WsNXTKOOcxuAqsjq3VSnEroJWJcV63IcIHuyuGfv7/s400/k3.PNG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;B. Manipulating&lt;/b&gt; an image is an integral part in deep learning and it starts with converting the image into numpy array format. Keras api provided &lt;b&gt;img_to_array()&lt;/b&gt; and &lt;b&gt;array_to_img()&lt;/b&gt; functions for this manipulation. Let's see how can we use both these two functions-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBcceEkTJPnYDT1cUOqTrI3z1vOSKHVoybzgVMvwVC9cM1F9rxl7VrZ1UVj1A1K7Jt_YWk33QNWE7eOmeRrwV1wUhXYFgam516Irkdf9iPmbWd1rhYgxNjPUdPPgOFdI776XiZCw_nU8JH/s1600/k2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="338" data-original-width="571" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBcceEkTJPnYDT1cUOqTrI3z1vOSKHVoybzgVMvwVC9cM1F9rxl7VrZ1UVj1A1K7Jt_YWk33QNWE7eOmeRrwV1wUhXYFgam516Irkdf9iPmbWd1rhYgxNjPUdPPgOFdI776XiZCw_nU8JH/s1600/k2.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
In above output cell, the uploaded image is converted into numpy array in float data type. The pixel values are converted into the format of (Height, Width, Channels). Finally the image is converted back to original PIL format.&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;C. Saving&lt;/b&gt; an image using Keras api is also easy. It provides &lt;b&gt;save_img()&lt;/b&gt; function for this task. Following example will demonstrate how to load an image in grayscale format, converts it to numpy array and then save it to a new file name-&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSMwse_S5Q92kL5_bLyIOvAuYYMgpAREjPbnmcB_niA_PrqZTvu5OpNBmQmu3gdQ5fouPUxe5ZrnAHsfwVX5zFF-ImT6RzOBT7nu1oOFZBTaNx_BU4InRYJpJu1KYhIEEwPtmOAXBmtlbL/s1600/k5.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="474" data-original-width="788" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSMwse_S5Q92kL5_bLyIOvAuYYMgpAREjPbnmcB_niA_PrqZTvu5OpNBmQmu3gdQ5fouPUxe5ZrnAHsfwVX5zFF-ImT6RzOBT7nu1oOFZBTaNx_BU4InRYJpJu1KYhIEEwPtmOAXBmtlbL/s1600/k5.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgq-h7zpHfMo5XBUYp8chV6uIEoBXuOmUhKXfy8CJDUVNjrpQRJnU-VYPWL0MCL0mjadngLBBOcDpQF2psKBpFsTLHzisQRlzh6nqjHr4FgRoZGPRuAq0plJ8LozvYuvlLkSec4M38jK_e_/s1600/k6.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="671" data-original-width="817" height="327" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgq-h7zpHfMo5XBUYp8chV6uIEoBXuOmUhKXfy8CJDUVNjrpQRJnU-VYPWL0MCL0mjadngLBBOcDpQF2psKBpFsTLHzisQRlzh6nqjHr4FgRoZGPRuAq0plJ8LozvYuvlLkSec4M38jK_e_/s400/k6.PNG" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In above output, you can see our loaded image is converted into grayscale format and in numpy array. We have also saved and uploaded the new grayscale image.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;3. How to scale your image using Keras?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Deep Learning models accept input images in scaled form. So we must scale our input images before training and evaluation of our deep learning model. Keras provides&amp;nbsp;&lt;b&gt;ImageDataGenerator&lt;/b&gt;&amp;nbsp;class and API for just-in-time image scaling. To understand how to use this class and API, we will work on the same MNIST digit dataset which we have seen&amp;nbsp;&lt;a href="https://dsbyprateekg.blogspot.com/2019/01/making-deep-learning-model-to-predict.html" style="font-weight: bold;" target="_blank"&gt;earliar in this post.&lt;/a&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;For a quick revision the MNIST dataset, is an image classification problem comprised of 70,000 images of handwritten digits. The goal of the problem is to classify a given image of a handwritten digit as an integer from 0 to 9. So it is a multiclass image classification problem.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;A. Load the dataset using keras&lt;/b&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRNdCdemOGWBfiMH_CtFXQtdvZbjjieg3TEKVxPKDRBUlj06LZsw7PuA3UokXg1X4Zv8zm4ehIrWgJZ22JJs585nqafxiID1cTC0AbrbCsCden04uwyVEhiaUHqf16LYbBM8x3m5SWpzRB/s1600/k7.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="342" data-original-width="1199" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRNdCdemOGWBfiMH_CtFXQtdvZbjjieg3TEKVxPKDRBUlj06LZsw7PuA3UokXg1X4Zv8zm4ehIrWgJZ22JJs585nqafxiID1cTC0AbrbCsCden04uwyVEhiaUHqf16LYbBM8x3m5SWpzRB/s1600/k7.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
From above output we can see that there are 60000 images in training dataset and 10000 images in testing dataset. All images are 28 x 28 pixels. Pixel values are in between 0 to 255 and mean/std deviation of datasets are similar.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;B. Scale the pixels using&amp;nbsp;ImageDataGenerator&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
ImageDataGenerator class has three main scaling methods: Pixel &lt;b&gt;Normalization&lt;/b&gt;, Pixel &lt;b&gt;Centering&lt;/b&gt;, Pixel &lt;b&gt;Standardization&lt;/b&gt;. Pixel Normalization technique scales pixel value to the range 0-1. Pixel Centering technique scales pixel values to have a zero mean. Pixel Standardization technique scales pixel values to have a zero mean and unit variance. Let's learn how to use each technique one by one-&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;1. Pixel Normalization:&amp;nbsp;&lt;/b&gt;This can be achieved by setting the rescale argument to a ratio by which each pixel can be multiplied to achieve the desired range. In our case it would be 1.0/255.0.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnlQ2ws3Gccs7obQS45erXlA8xwuxXsb4VPC2OOH7echmX9d6IDPlLa0rwGhjjp65AWT1_so_ZpAlaBENdDbYJpJ35Vnr0qIpJ2vfo1zJwcuyIxcRbzGWtjLUy2l732QpxPZ5_X_kJsl6U/s1600/k8.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="580" data-original-width="980" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnlQ2ws3Gccs7obQS45erXlA8xwuxXsb4VPC2OOH7echmX9d6IDPlLa0rwGhjjp65AWT1_so_ZpAlaBENdDbYJpJ35Vnr0qIpJ2vfo1zJwcuyIxcRbzGWtjLUy2l732QpxPZ5_X_kJsl6U/s1600/k8.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
In above output cell we can see that minimum and maximum pixels of training and testing datasets- 0 and 255. Since MNIST dataset has only black and white images, it has only one channel. If you are applying this to color images then minimum and maximum values will be calculated across all channels. In this example the ImageDataGenerator does not need to be fit on the training dataset as there is nothing that needs to be calculated, we have provided the scale factor directly.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;2. Pixel Centering:&lt;/b&gt;&amp;nbsp;In this technique we calculate the mean pixel value across the entire training dataset, then subtract it from each image.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitwUysRdIpCd-HpS1w0Saw_wADwO5ply7WQAFcUUVMwlv65a8hZ3WwVAttM18aSGUk-yQEGiZRxO_K7n4z2lCBy6heiBQw_vYmnTlUiDwSlH6mDHYzp2Yzli168hEl6h9QdtztUyOKuA_f/s1600/k9.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="683" data-original-width="939" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitwUysRdIpCd-HpS1w0Saw_wADwO5ply7WQAFcUUVMwlv65a8hZ3WwVAttM18aSGUk-yQEGiZRxO_K7n4z2lCBy6heiBQw_vYmnTlUiDwSlH6mDHYzp2Yzli168hEl6h9QdtztUyOKuA_f/s1600/k9.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
In above output cell you are seeing the mean pixel values of training and test datasets. The&amp;nbsp;ImageDataGenerator is fit on the training dataset and we have confirmed that this mean pixel value matches with our calculated mean pixel value in next line. You can see mean pixel value for the scaled dataset is close to zero.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;3. Pixel Standardization: &lt;/b&gt;Standardization of images is achieved by subtracting the mean pixel value and dividing the result by the standard deviation of the pixel values. The mean and standard deviation statistics can be calculated on the training dataset.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWjH1-1Pxo-tmAezJbjFcLWDzVWWGB6J0eMwsW32vVBCDPaq-XyygneeWWG8f4qSj8qdV1DjeSsrZ3U-OIHzjIuv2SPfkQzKHebt-QL0JurK-MZ-Yv6sxngY60IrOJ_ENAxaxyl2KQrEoX/s1600/k10.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" data-original-height="694" data-original-width="1268" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWjH1-1Pxo-tmAezJbjFcLWDzVWWGB6J0eMwsW32vVBCDPaq-XyygneeWWG8f4qSj8qdV1DjeSsrZ3U-OIHzjIuv2SPfkQzKHebt-QL0JurK-MZ-Yv6sxngY60IrOJ_ENAxaxyl2KQrEoX/s1600/k10.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
In above output cell, you can see in the first line- the mean and standard deviation of pixel values in both datasets.&amp;nbsp;The data generator is then configured for feature-wise standardization and the statistics are calculated on the training dataset, which is matching what we would expect when the statistics are calculated manually.&amp;nbsp;The test is then repeated on the entire training dataset and we can confirm that the mean is very small value close to 0.0 and the standard deviation is a value very close to 1.0.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
That is enough learning for today. After reading this post I recommend you to run above code examples in your notebook and understand each line. After this you can repeat the same techniques with any color image dataset. Working on a color dataset will teach you how to modify data scaling techniques. If you follow what I suggest then believe me you will become computer vision Ninja at the end of coming posts. In the next post we will learn the standard way to lay out our image data and image augmentation for modeling. Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.&lt;/div&gt;
&lt;/div&gt;
</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnRg4cp-vkUiK21EFhymad9WY9PIXLD1TRx0G8Sy4JUEtOdMgHAfSmb_ssjk_54Aaidd_Zt5BwNHEJtuDHcjJ_bNRYFVBbXRZ2Yk7tdtCNR8k5J1cslKIBwSl1Z9hlOpTbWzdOBZwmWGHN/s72-c/k3.jpg" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item></channel></rss>