<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>YOLOv4 | Haobin Tan</title><link>https://haobin-tan.netlify.app/tags/yolov4/</link><atom:link href="https://haobin-tan.netlify.app/tags/yolov4/index.xml" rel="self" type="application/rss+xml"/><description>YOLOv4</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Sat, 19 Dec 2020 00:00:00 +0000</lastBuildDate><image><url>https://haobin-tan.netlify.app/media/icon_hu7d15bc7db65c8eaf7a4f66f5447d0b42_15095_512x512_fill_lanczos_center_3.png</url><title>YOLOv4</title><link>https://haobin-tan.netlify.app/tags/yolov4/</link></image><item><title>YOLOv4: Run Pretrained YOLOv4 on COCO Dataset</title><link>https://haobin-tan.netlify.app/docs/ai/computer-vision/object-detection/train-yolo-v4/</link><pubDate>Wed, 04 Nov 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/computer-vision/object-detection/train-yolo-v4/</guid><description>&lt;p>Here we will learn how to get YOLOv4 Object Detection running in the Cloud with Google Colab step by step.&lt;/p>
&lt;p>Check out the &lt;a href="https://colab.research.google.com/drive/1o-xfVm7A-kgtFZRrehJvnibuBwzNPs1-?authuser=1#scrollTo=P5WqSvgwqmLT">Google Colab Notebook&lt;/a>&lt;/p>
&lt;h2 id="clone-and-build-darknet">Clone and build DarkNet&lt;/h2>
&lt;p>Clone darknet from AlexeyAB&amp;rsquo;s &lt;a href="https://github.com/AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects">repository&lt;/a>,&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">!git clone https://github.com/AlexeyAB/darknet
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Adjust the Makefile to enable OPENCV and GPU for darknet&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># change makefile to have GPU and OPENCV enabled&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">%cd darknet
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">!sed -i &lt;span class="s1">&amp;#39;s/OPENCV=0/OPENCV=1/&amp;#39;&lt;/span> Makefile
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">!sed -i &lt;span class="s1">&amp;#39;s/GPU=0/GPU=1/&amp;#39;&lt;/span> Makefile
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">!sed -i &lt;span class="s1">&amp;#39;s/CUDNN=0/CUDNN=1/&amp;#39;&lt;/span> Makefile
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">!sed -i &lt;span class="s1">&amp;#39;s/CUDNN_HALF=0/CUDNN_HALF=1/&amp;#39;&lt;/span> Makefile
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Verify CUDA&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># verify CUDA&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">!/usr/local/cuda/bin/nvcc --version
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Build darknet&lt;/p>
&lt;blockquote>
&lt;p>Note: Do not worry about any warnings when running the &lt;code>!make&lt;/code> cell!&lt;/p>
&lt;/blockquote>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># make darknet &lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># (builds darknet so that you can then use the darknet executable file &lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># to run or train object detectors)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">!make
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="download-pretrained-yolo-v4-weights">Download pretrained YOLO v4 weights&lt;/h2>
&lt;p>YOLOv4 has been trained already on the coco dataset which has 80 classes that it can predict. We will grab these pretrained weights so that we can run YOLOv4 on these pretrained classes and get detections.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="define-helper-functions">Define helper functions&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">cv2&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">matplotlib.pyplot&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="nn">plt&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="o">%&lt;/span>&lt;span class="n">matplotlib&lt;/span> &lt;span class="n">inline&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">imShow&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">path&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> Show image
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> &amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">image&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">cv2&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">imread&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">path&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">height&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">width&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">image&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">shape&lt;/span>&lt;span class="p">[:&lt;/span>&lt;span class="mi">2&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">resized_image&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">cv2&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">resize&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">image&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="mi">3&lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="n">width&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">3&lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="n">height&lt;/span>&lt;span class="p">),&lt;/span> &lt;span class="n">interpolation&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">cv2&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">INTER_CUBIC&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">fig&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">plt&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">gcf&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">fig&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">set_size_inches&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">18&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">10&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">plt&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">axis&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;off&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">plt&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">imshow&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">cv2&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">cvtColor&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">resized_image&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">cv2&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">COLOR_BGR2RGB&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">plt&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">show&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">upload&lt;/span>&lt;span class="p">():&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> upload files to Google Colab
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> &amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kn">from&lt;/span> &lt;span class="nn">google.colab&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">files&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">uploaded&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">files&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">upload&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">for&lt;/span> &lt;span class="n">name&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">data&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="n">uploaded&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">items&lt;/span>&lt;span class="p">():&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">with&lt;/span> &lt;span class="nb">open&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">name&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;wb&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="n">f&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">f&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">write&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">data&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s1">&amp;#39;saved file &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">name&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s1">&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">download&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">path&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> Download from Google Colab
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> &amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kn">from&lt;/span> &lt;span class="nn">google.colab&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">files&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">files&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">download&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">path&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="run-detections-with-darknet-and-yolov4">Run detections with Darknet and YOLOv4&lt;/h2>
&lt;p>The object detector can be run using the following command&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">!./darknet detector &lt;span class="nb">test&lt;/span> &amp;lt;path to .data file&amp;gt; &amp;lt;path to config&amp;gt; &amp;lt;path to weights&amp;gt; &amp;lt;path to image&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This will output the image with the detections shown. The most recent detections are always saved to &amp;lsquo;&lt;strong>predictions.jpg&lt;/strong>&amp;rsquo;&lt;/p>
&lt;p>&lt;strong>Note:&lt;/strong> After running detections OpenCV can&amp;rsquo;t open the image instantly in the cloud so we must run:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="n">imShow&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;predictions.jpg&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Darknet comes with a few images already installed in the &lt;code>darknet/data/&lt;/code> folder. Let&amp;rsquo;s test one of the images inside:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># run darknet detection on test images&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">!./darknet detector &lt;span class="nb">test&lt;/span> cfg/coco.data cfg/yolov4.cfg yolov4.weights data/person.jpg
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="n">imShow&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;predictions.jpg&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/predictions.png" alt="predictions">&lt;/p>
&lt;h3 id="run-detections-using-uploaded-image">Run detections using uploaded image&lt;/h3>
&lt;p>We can also mount Google drive into the cloud VM a&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">google.colab&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">drive&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">drive&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">mount&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;/content/gdrive&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># this creates a symbolic link &lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># so that now the path /content/gdrive/My\ Drive/ is equal to /mydrive&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">!ln -s /content/gdrive/My&lt;span class="se">\ &lt;/span>Drive/ /mydrive
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">!ls /mydrive
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>nd run YOLOv4 with images from Google drive using the following command:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">!./darknet detector &lt;span class="nb">test&lt;/span> cfg/coco.data cfg/yolov4.cfg yolov4.weights /mydrive/&amp;lt;path to image&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>For example, I uploaded an image called &amp;ldquo;pedestrian.jpg&amp;rdquo; in &lt;code>images/&lt;/code> folder:&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/pedestrian.jpg" alt="pedestrian">&lt;/p>
&lt;p>and run detection on it:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="err">!&lt;/span>&lt;span class="o">./&lt;/span>&lt;span class="n">darknet&lt;/span> &lt;span class="n">detector&lt;/span> &lt;span class="n">test&lt;/span> &lt;span class="n">cfg&lt;/span>&lt;span class="o">/&lt;/span>&lt;span class="n">coco&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">data&lt;/span> &lt;span class="n">cfg&lt;/span>&lt;span class="o">/&lt;/span>&lt;span class="n">yolov4&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">cfg&lt;/span> &lt;span class="n">yolov4&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">weights&lt;/span> &lt;span class="o">/&lt;/span>&lt;span class="n">mydrive&lt;/span>&lt;span class="o">/&lt;/span>&lt;span class="n">images&lt;/span>&lt;span class="o">/&lt;/span>&lt;span class="n">pedestrian&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">jpg&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">imShow&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;predictions.jpg&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/pedestrian_predictions.png" alt="pedestrian_predictions">&lt;/p>
&lt;h2 id="reference">Reference&lt;/h2>
&lt;ul>
&lt;li>
&lt;p>YOLOv4 in the CLOUD: Install and Run Object Detector (FREE GPU)&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;a href="https://colab.research.google.com/drive/1_GdoqCJWXsChrOiY8sZMr_zbr_fH-0Fg?usp=sharing#scrollTo=iZULaGX7_H1u">Google Colab Notebook&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="https://github.com/theAIGuysCode/YOLOv4-Cloud-Tutorial">https://github.com/theAIGuysCode/YOLOv4-Cloud-Tutorial&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Video Tutorial&lt;/p>
&lt;div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
&lt;iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="allowfullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/mKAEGSxwOAY?autoplay=0&amp;controls=1&amp;end=0&amp;loop=0&amp;mute=0&amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"
>&lt;/iframe>
&lt;/div>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul></description></item><item><title>YOLOv4: Train on Custom Dataset</title><link>https://haobin-tan.netlify.app/docs/ai/computer-vision/object-detection/train-yolo-v4-custom-dataset/</link><pubDate>Wed, 04 Nov 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/computer-vision/object-detection/train-yolo-v4-custom-dataset/</guid><description>&lt;h2 id="clone-and-build-darknet">Clone and build Darknet&lt;/h2>
&lt;p>Clone darknet repo&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">git clone https://github.com/AlexeyAB/darknet
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Change makefile to have GPU and OPENCV enabled&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">cd&lt;/span> darknet
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sed -i &lt;span class="s1">&amp;#39;s/OPENCV=0/OPENCV=1/&amp;#39;&lt;/span> Makefile
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sed -i &lt;span class="s1">&amp;#39;s/GPU=0/GPU=1/&amp;#39;&lt;/span> Makefile
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sed -i &lt;span class="s1">&amp;#39;s/CUDNN=0/CUDNN=1/&amp;#39;&lt;/span> Makefile
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sed -i &lt;span class="s1">&amp;#39;s/CUDNN_HALF=0/CUDNN_HALF=1/&amp;#39;&lt;/span> Makefile
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Verify CUDA&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">/usr/local/cuda/bin/nvcc --version
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="compile-on-linux-using-make">Compile on Linux using &lt;code>make&lt;/code>&lt;/h2>
&lt;p>Make darknet&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">make
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;code>GPU=1&lt;/code> : build with CUDA to accelerate by using GPU&lt;/li>
&lt;li>&lt;code>CUDNN=1&lt;/code> : build with cuDNN v5-v7 to accelerate training by using GPU&lt;/li>
&lt;li>&lt;code>CUDNN_HALF=1&lt;/code> to build for Tensor Cores (on Titan V / Tesla V100 / DGX-2 and later) speedup Detection 3x, Training 2x&lt;/li>
&lt;li>&lt;code>OPENCV=1&lt;/code> to build with OpenCV 4.x/3.x/2.4.x - allows to detect on video files and video streams from network cameras or web-cams&lt;/li>
&lt;li>&lt;code>DEBUG=1&lt;/code> to bould debug version of Yolo&lt;/li>
&lt;li>&lt;code>OPENMP=1&lt;/code> to build with OpenMP support to accelerate Yolo by using multi-core CPU&lt;/li>
&lt;/ul>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="m11.25 11.25l.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0a9 9 0 0 1 18 0m-9-3.75h.008v.008H12z"/>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">Do not worry about any warnings when running &lt;code>make&lt;/code> command.&lt;/span>
&lt;/div>
&lt;h2 id="prepare-custom-dataset">Prepare custom dataset&lt;/h2>
&lt;p>The custom dataset should be in &lt;strong>YOLOv4&lt;/strong> or &lt;strong>darknet&lt;/strong> format:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>For each &lt;code>.jpg&lt;/code> image file, there should be a corresponding &lt;code>.txt&lt;/code> file&lt;/p>
&lt;ul>
&lt;li>
&lt;p>In the same directory, with the same name, but with &lt;code>.txt&lt;/code>-extension&lt;/p>
&lt;p>For example, if there&amp;rsquo;s an &lt;code>.jpg&lt;/code> image named &lt;code>BloodImage_00001.jpg&lt;/code>, there should also be a corresponding &lt;code>.txt&lt;/code> file named &lt;code>BloodImage_00001.txt&lt;/code>&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>In this &lt;code>.txt&lt;/code> file: object number and object coordinates on this image, for each object in new line.&lt;/p>
&lt;p>Format:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">&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;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;code>&amp;lt;object-class&amp;gt;&lt;/code> : integer object number from &lt;code>0&lt;/code> to &lt;code>(classes-1)&lt;/code>&lt;/li>
&lt;li>&lt;code>&amp;lt;x_center&amp;gt; &amp;lt;y_center&amp;gt; &amp;lt;width&amp;gt; &amp;lt;height&amp;gt;&lt;/code> : float values &lt;strong>relative&lt;/strong> to width and height of image, it can be equal from &lt;code>(0.0 to 1.0]&lt;/code>
&lt;ul>
&lt;li>&lt;code>&amp;lt;x_center&amp;gt; &amp;lt;y_center&amp;gt;&lt;/code> are center of rectangle (are not top-left corner)&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="configure-files-for-training">Configure files for training&lt;/h2>
&lt;ol start="0">
&lt;li>
&lt;p>For training &lt;code>cfg/yolov4-custom.cfg&lt;/code> download the pre-trained weights-file &lt;a href="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137">yolov4.conv.137&lt;/a>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">cd&lt;/span> darknet
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>In folder &lt;code>./cfg&lt;/code>, create custom config file (let&amp;rsquo;s call it &lt;code>custom-yolov4-detector.cfg&lt;/code>) with the same content as in &lt;code>yolov4-custom.cfg&lt;/code> and&lt;/p>
&lt;ul>
&lt;li>
&lt;p>change line &lt;strong>batch&lt;/strong> to &lt;a href="https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L3">&lt;code>batch=64&lt;/code>&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>change line &lt;strong>subdivisions&lt;/strong> to &lt;a href="https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L4">&lt;code>subdivisions=16&lt;/code>&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>change line &lt;strong>max_batches&lt;/strong> to &lt;code>classes*2000&lt;/code> but&lt;/p>
&lt;ul>
&lt;li>NOT less than number of training images&lt;/li>
&lt;li>NOT less than number of training images&lt;/li>
&lt;li>NOT less than 6000&lt;/li>
&lt;/ul>
&lt;p>&lt;em>e.g. &lt;code>max_batches=6000&lt;/code> if you train for 3 classes&lt;/em>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>change line &lt;strong>steps&lt;/strong> to 80% and 90% of &lt;strong>max_batches&lt;/strong> (&lt;em>e.g. &lt;code>steps=4800, 5400&lt;/code>&lt;/em>)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>set network size &lt;code>width=416 height=416&lt;/code> or any value multiple of 32&lt;/p>
&lt;/li>
&lt;li>
&lt;p>change line &lt;code>classes=80&lt;/code> to number of objects in &lt;strong>each&lt;/strong> of 3 &lt;code>[yolo]&lt;/code>-layers&lt;/p>
&lt;/li>
&lt;li>
&lt;p>change [&lt;code>filters=255&lt;/code>] to $ \text{filters}=(\text{classes} + 5) \times 3$ in the 3 &lt;code>[convolutional]&lt;/code> before each &lt;code>[yolo]&lt;/code> layer, keep in mind that it only has to be the last &lt;code>[convolutional]&lt;/code> before each of the &lt;code>[yolo]&lt;/code> layers.&lt;/p>
&lt;blockquote>
&lt;p>Note: &lt;strong>Do not write in the cfg-file: &lt;code>filters=(classes + 5) x 3&lt;/code>&lt;/strong>!!!&lt;/p>
&lt;p>It has to be the specific number!&lt;/p>
&lt;p>E.g. &lt;code>classes=1&lt;/code> then should be &lt;code>filters=18&lt;/code>; &lt;code>classes=2&lt;/code> then should be &lt;code>filters=21&lt;/code>&lt;/p>
&lt;p>So for example, for 2 objects, your custom config file should differ from &lt;code>yolov4-custom.cfg&lt;/code> in such lines in &lt;strong>each&lt;/strong> of &lt;strong>3&lt;/strong> [yolo]-layers:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">[convolutional]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">filters=21
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">[region]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">classes=2
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/blockquote>
&lt;/li>
&lt;li>
&lt;p>when using &lt;a href="https://github.com/AlexeyAB/darknet/blob/6e5bdf1282ad6b06ed0e962c3f5be67cf63d96dc/cfg/Gaussian_yolov3_BDD.cfg#L608">&lt;code>[Gaussian_yolo]&lt;/code>&lt;/a> layers, change [&lt;code>filters=57&lt;/code>] $ \text{filters}=(\text{classes} + 9) \times 3$ in the 3 &lt;code>[convolutional]&lt;/code> before each &lt;code>[Gaussian_yolo]&lt;/code> layer&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Create file &lt;code>obj.names&lt;/code> in the directory &lt;code>data/&lt;/code>, with objects names - each in new line&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Create fiel &lt;code>obj.data&lt;/code> in the directory &lt;code>data/&lt;/code>, containing (where &lt;strong>classes = number of objects&lt;/strong>):&lt;/p>
&lt;p>For example, if we two objects&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">classes = 2
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">train = data/train.txt
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">valid = data/test.txt
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">names = data/obj.names
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">backup = backup/
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>Put image files (&lt;code>.jpg&lt;/code>) of your objects in the directory &lt;code>data/obj/&lt;/code>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Create &lt;code>train.txt&lt;/code> in directory &lt;code>data/&lt;/code> with filenames of your images, each filename in new line, with path relative to &lt;code>darknet&lt;/code>.&lt;/p>
&lt;p>For example containing:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">data/obj/img1.jpg
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">data/obj/img2.jpg
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">data/obj/img3.jpg
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>Download pre-trained weights for the convolutional layers and put to the directory &lt;code>darknet&lt;/code> (root directory of the project)&lt;/p>
&lt;ul>
&lt;li>for &lt;code>yolov4.cfg&lt;/code>, &lt;code>yolov4-custom.cfg&lt;/code> (162 MB): &lt;a href="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137">yolov4.conv.137&lt;/a>&lt;/li>
&lt;li>for &lt;code>yolov4-tiny.cfg&lt;/code>, &lt;code>yolov4-tiny-3l.cfg&lt;/code>, &lt;code>yolov4-tiny-custom.cfg&lt;/code>(19 MB): &lt;a href="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29">yolov4-tiny.conv.29&lt;/a>&lt;/li>
&lt;li>for &lt;code>csresnext50-panet-spp.cfg&lt;/code> (133 MB): &lt;a href="https://drive.google.com/file/d/16yMYCLQTY_oDlCIZPfn_sab6KD3zgzGq/view?usp=sharing">csresnext50-panet-spp.conv.112&lt;/a>&lt;/li>
&lt;li>for &lt;code>yolov3.cfg, yolov3-spp.cfg&lt;/code> (154 MB): &lt;a href="https://pjreddie.com/media/files/darknet53.conv.74">darknet53.conv.74&lt;/a>&lt;/li>
&lt;li>for &lt;code>yolov3-tiny-prn.cfg , yolov3-tiny.cfg&lt;/code> (6 MB): &lt;a href="https://drive.google.com/file/d/18v36esoXCh-PsOKwyP2GWrpYDptDY8Zf/view?usp=sharing">yolov3-tiny.conv.11&lt;/a>&lt;/li>
&lt;li>for &lt;code>enet-coco.cfg (EfficientNetB0-Yolov3)&lt;/code> (14 MB): &lt;a href="https://drive.google.com/file/d/1uhh3D6RSn0ekgmsaTcl-ZW53WBaUDo6j/view?usp=sharing">enetb0-coco.conv.132&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ol>
&lt;h2 id="start-training">Start training&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">./darknet detector train data/obj.data custom-yolov4-detector.cfg yolov4.conv.137 -dont_show
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>
&lt;p>file &lt;code>yolo-obj_last.weights&lt;/code> will be saved to the &lt;code>backup\&lt;/code> for each 100 iterations&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>-dont_show&lt;/code>: disable Loss-Window, if you train on computer without monitor (e.g remote server)&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>To see the mAP &amp;amp; loss0chart during training on remote server:&lt;/p>
&lt;ul>
&lt;li>use command &lt;code>./darknet detector train data/obj.data yolo-obj.cfg yolov4.conv.137 -dont_show -mjpeg_port 8090 -map&lt;/code>&lt;/li>
&lt;li>then open URL &lt;code>http://ip-address:8090&lt;/code> in Chrome/Firefox browser)&lt;/li>
&lt;/ul>
&lt;p>After training is complete, you can get weights from &lt;code>backup/&lt;/code>&lt;/p>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="m11.25 11.25l.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0a9 9 0 0 1 18 0m-9-3.75h.008v.008H12z"/>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">&lt;p>If you want the training to output only main information (e.g loss, mAP, remaining training time) instead of full logging, you can use this command&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">./darknet detector train data/obj.data custom-yolov4-detector.cfg yolov4.conv.137 -dont_show -map 2&amp;gt;&lt;span class="p">&amp;amp;&lt;/span>&lt;span class="m">1&lt;/span> &lt;span class="p">|&lt;/span> tee log/train.log &lt;span class="p">|&lt;/span> grep -E &lt;span class="s2">&amp;#34;hours left|mean_average&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Then the output will look like followings:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl"> 1189: 1.874030, 2.934438 avg loss, 0.002610 rate, 2.930427 seconds, 76096 images, 3.905244 hours left
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/span>
&lt;/div>
&lt;h3 id="notes">Notes&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>If during training you see &lt;code>nan&lt;/code> values for &lt;code>avg&lt;/code> (loss) field - then training goes wrong! ​🤦‍♂️​&lt;/p>
&lt;p>But if &lt;code>nan&lt;/code> is in some other lines - then training goes well.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>if error &lt;code>Out of memory&lt;/code> occurs then in &lt;code>.cfg&lt;/code>-file you should increase &lt;code>subdivisions=16&lt;/code>, 32 or 64&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="train-tiny-yolo">Train tiny-YOLO&lt;/h2>
&lt;p>Do all the same steps as for the full yolo model as described above. With the exception of:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Download file with the first 29-convolutional layers of yolov4-tiny:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>(Or get this file from yolov4-tiny.weights file by using command: &lt;code>./darknet partial cfg/yolov4-tiny-custom.cfg yolov4-tiny.weights yolov4-tiny.conv.29 29&lt;/code>)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Make your custom model &lt;code>yolov4-tiny-obj.cfg&lt;/code> based on &lt;code>cfg/yolov4-tiny-custom.cfg&lt;/code> instead of &lt;code>yolov4.cfg&lt;/code>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">re&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># num_classes: number of object classes&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">max_batches&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">max&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">num_classes&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="mi">2000&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">num_train_images&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">6000&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">steps1&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mf">.8&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="n">max_batches&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">steps2&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mf">.9&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="n">max_batches&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">num_filters&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">num_classes&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="mi">5&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="mi">3&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Assuming that we have already defined the following hyperparameters:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># - TINY_CONFIG_FILE: config file we&amp;#39;re gonna use for training&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># - WIDTH, HEIGHT: width and height of image&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">with&lt;/span> &lt;span class="nb">open&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;cfg/yolov4-tiny-custom.cfg&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;r&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="n">reader&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nb">open&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">TINY_CONFIG_FILE&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;w&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="n">writer&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">content&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">reader&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">read&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">content&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">re&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sub&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;subdivisions=\d*&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;subdivisions=&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">SUBDIVISION&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">content&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">content&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">re&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sub&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;width=\d*&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;width=&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">WIDTH&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">content&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">content&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">re&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sub&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;height=\d*&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;height=&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">HEIGHT&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">content&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">content&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">re&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sub&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;max_batches = \d*&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;max_batches = &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">max_batches&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">content&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">content&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">re&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sub&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;steps=\d*,\d*&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;steps=&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">steps1&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">,&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">steps2&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">content&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">content&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">re&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sub&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;classes=\d*&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;classes=&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">num_classes&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">content&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">content&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">re&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sub&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;pad=1&lt;/span>&lt;span class="se">\n&lt;/span>&lt;span class="s2">filters=\d*&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;pad=1&lt;/span>&lt;span class="se">\n&lt;/span>&lt;span class="s2">filters=&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">num_filters&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">content&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">writer&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">write&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">content&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>Start training:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">./darknet detector train data/obj.data yolov4-tiny-obj.cfg yolov4-tiny.conv.29
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;h2 id="google-colab-notebook">Google Colab Notebook&lt;/h2>
&lt;p>&lt;a href="https://colab.research.google.com/drive/1aIc5xS8vVukVg-FiUA3aw0PUqYrXs8aO?authuser=1#scrollTo=Zz8v67_2kgWh">Colab Notebook&lt;/a>&lt;/p>
&lt;h3 id="small-hacks-to-keep-colab-notebook-training">Small hacks to keep colab notebook training&lt;/h3>
&lt;ol>
&lt;li>
&lt;p>Open up the inspector view on Chrome&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Switch to the console window&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Paste the following code&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="kd">function&lt;/span> &lt;span class="nx">ClickConnect&lt;/span>&lt;span class="p">(){&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">log&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;Working&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">document&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">.&lt;/span>&lt;span class="nx">querySelector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;#top-toolbar &amp;gt; colab-connect-button&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">.&lt;/span>&lt;span class="nx">shadowRoot&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">querySelector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;#connect&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">.&lt;/span>&lt;span class="nx">click&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">setInterval&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">ClickConnect&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="mi">60000&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>and hit &lt;strong>Enter&lt;/strong>.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;p>It will click the screen every 10 minutes so that you don&amp;rsquo;t get kicked off for being idle!&lt;/p>
&lt;h2 id="convert-yolov4-to-tensorrt-through-onnx">Convert YOLOv4 to TensorRT through ONNX&lt;/h2>
&lt;p>To convert YOLOv4 to TensorRT engine through ONNX, I used the code from &lt;a href="https://github.com/jkjung-avt/tensorrt_demos">TensorRT_demos&lt;/a> following its &lt;a href="https://github.com/jkjung-avt/tensorrt_demos#demo-5-yolov4">step-by-step instructions&lt;/a>. For more details about the code, check out this &lt;a href="https://jkjung-avt.github.io/tensorrt-yolov4/">blog post&lt;/a>.&lt;/p>
&lt;p>Note that the Code in this repo was designed to run on &lt;a href="https://developer.nvidia.com/embedded-computing">Jetson platforms&lt;/a>. In my case, conversion from YOLOv4 to TensorRT engine was conducted on Jetson Nano.&lt;/p>
&lt;h3 id="convert-yolov4-for-custom-trained-models">Convert YOLOv4 for custom trained models&lt;/h3>
&lt;p>To apply the conversion for custom trained models, see &lt;a href="https://jkjung-avt.github.io/trt-yolov3-custom/">TensorRT YOLOv3 For Custom Trained Models&lt;/a>. You need to stick to the naming convention &lt;code>{yolo_version}-{custom_name}-{image_size}&lt;/code>. Otherwise you&amp;rsquo;ll get errors during conversion.&lt;/p>
&lt;h2 id="reference">Reference&lt;/h2>
&lt;ul>
&lt;li>
&lt;p>Guide from &lt;a href="https://github.com/AlexeyAB">AlexeyAB&lt;/a>/&lt;strong>&lt;a href="https://github.com/AlexeyAB/darknet">darknet&lt;/a>&lt;/strong> repo: &lt;a href="https://github.com/AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects">How to train (to detect your custom objects)&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Tutorials&lt;/p>
&lt;ul>
&lt;li>
&lt;p>👨‍🏫 How to Train YOLOv4 on a Custom Dataset in Darknet&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;a href="https://colab.research.google.com/drive/1mzL6WyY9BRx4xX476eQdhKDnd_eixBlG?authuser=0#scrollTo=QyMBDkaL-Aep">Colab Notebook&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Blog post: &lt;a href="https://blog.roboflow.com/training-yolov4-on-a-custom-dataset/">https://blog.roboflow.com/training-yolov4-on-a-custom-dataset/&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Video tutorial:&lt;/p>
&lt;div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
&lt;iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="allowfullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/N-GS8cmDPog?autoplay=0&amp;controls=1&amp;end=0&amp;loop=0&amp;mute=0&amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"
>&lt;/iframe>
&lt;/div>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="https://blog.roboflow.com/yolov4-tactics/">YOLOv4 - Ten Tactics to Build a Better Model&lt;/a>&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Train YOLOv4-tiny on custom dataset: &lt;a href="https://blog.roboflow.com/train-yolov4-tiny-on-custom-data-lighting-fast-detection/">Train YOLOv4-tiny on Custom Data - Lightning Fast Object Detection&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>YOLOv4 in the CLOUD: Build and Train Custom Object Detector (FREE GPU)&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;a href="https://colab.research.google.com/drive/1_GdoqCJWXsChrOiY8sZMr_zbr_fH-0Fg#scrollTo=O2w9w1Ye_nk1">Colab Notebook&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Video tutorial:&lt;/p>
&lt;div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
&lt;iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="allowfullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/mmj3nxGT2YQ?autoplay=0&amp;controls=1&amp;end=0&amp;loop=0&amp;mute=0&amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"
>&lt;/iframe>
&lt;/div>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="https://jkjung-avt.github.io/colab-yolov4/">Custom YOLOv4 Model on Google Colab&lt;/a>&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://colab.research.google.com/drive/1eoa2_v6wVlcJiDBh3Tb_umhm7a09lpIE?usp=sharing#scrollTo=J1oTF_YRoGSZ">Colab Notebook&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="https://jkjung-avt.github.io/tensorrt-yolov4/">TensorRT YOLOv4&lt;/a>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="https://jkjung-avt.github.io/yolov4/">YOLOv4 on Jetson Nano&lt;/a>&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul></description></item><item><title>YOLOv4: Training Tips</title><link>https://haobin-tan.netlify.app/docs/ai/computer-vision/object-detection/yolov4-training-tips/</link><pubDate>Sat, 19 Dec 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/computer-vision/object-detection/yolov4-training-tips/</guid><description>&lt;h2 id="model-zoo">Model zoo&lt;/h2>
&lt;p>&lt;a href="https://github.com/AlexeyAB/darknet/wiki/YOLOv4-model-zoo#yolov4-model-zoo">YOLOv4 model zoo&lt;/a>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Pretrained models&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Proper configuration based on GPU&lt;/p>
&lt;blockquote>
&lt;p>We do NOT suggest you train the model with subdivisions equal or larger than 32, it will takes very long training time.&lt;/p>
&lt;/blockquote>
&lt;/li>
&lt;/ul>
&lt;h2 id="faq">FAQ&lt;/h2>
&lt;h3 id="low-accuracy-1">Low accuracy &lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup>&lt;/h3>
&lt;h4 id="the-most-common-problem---you-do-not-follow-strictly-the-manual">The most common problem - you do NOT follow strictly the manual.&lt;/h4>
&lt;ul>
&lt;li>You must use
&lt;ul>
&lt;li>&lt;code>default anchors&lt;/code>&lt;/li>
&lt;li>&lt;code>learning_rate=0.001&lt;/code>&lt;/li>
&lt;li>&lt;code>batch=64&lt;/code>&lt;/li>
&lt;li>&lt;code>max_batches = max(6000, number_of_training_images, 2000*classes)&lt;/code>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>You can only change &lt;code>subdivisions&lt;/code>&lt;/li>
&lt;li>&lt;strong>Do not do anything that is not written in the manual.&lt;/strong> 🙅‍♂️&lt;/li>
&lt;/ul>
&lt;h4 id="your-datasets-are-wrong">Your datasets are wrong.&lt;/h4>
&lt;ul>
&lt;li>
&lt;p>check the AP50 (average precision) for validation and training dataset by using &lt;code>./darknet detector map obj.data yolo.cfg yolo.weights&lt;/code>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>If you get high mAP for both Training and Validation datasets, but the network detects objects poorly in real life, then your training dataset is not representative &amp;ndash;&amp;gt; &lt;strong>add more images from real life to it&lt;/strong>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>If you get high mAP for Training dataset, but low for Validation dataset, then your Training dataset isn&amp;rsquo;t suitable for Validation dataset.&lt;/p>
&lt;p>For example&lt;/p>
&lt;ul>
&lt;li>Training dataset contains: cars (rear view) from distance 100m&lt;/li>
&lt;li>Test dataset contains: cars (side view) from distance 5m&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>if you get low mAP for both Training and Validation datasets, then labels in your Training dataset are wrong&lt;/p>
&lt;ul>
&lt;li>Run training with flag &lt;code>-show_imgs&lt;/code>, i.e. &lt;code>./darknet detector train ... -show_imgs&lt;/code> , do you see correct bounded boxes?&lt;/li>
&lt;li>Or check your dataset by using &lt;a href="https://github.com/AlexeyAB/Yolo_mark">Yolo_mark&lt;/a> tool&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h3 id="darknet-trainingdetection-crashes-with-an-error-2">Darknet training/detection crashes with an error &lt;sup id="fnref:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup>&lt;/h3>
&lt;ul>
&lt;li>If &lt;code>CUDA Out of memory&lt;/code> error occurs, then increase &lt;code>subdivisions=&lt;/code> 2 times in cfg-file, but not higher than &lt;code>batch=&lt;/code> (don&amp;rsquo;t change batch)!
&lt;ul>
&lt;li>If it doesn&amp;rsquo;t help - set &lt;code>random=0&lt;/code> and &lt;code>width=416 height=416&lt;/code> in cfg-file.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Check content of files &lt;code>bad.list&lt;/code> and &lt;code>bad_label.list&lt;/code> if they exist near with &lt;code>./darknet&lt;/code> executable file.&lt;/li>
&lt;li>Do not move some files from Darknet folder - you may forget the necessary files.&lt;/li>
&lt;li>Download libraries CUDA, cuDNN, OpenCV, &amp;hellip; only from official sources. Don&amp;rsquo;t download libs from other sites.&lt;/li>
&lt;li>Make sure that you do everything in accordance with the manual, and do not do anything that is not written in the manual.&lt;/li>
&lt;/ul>
&lt;h2 id="train-with-multiple-gpus-3">Train with multiple GPUs &lt;sup id="fnref:3">&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref">3&lt;/a>&lt;/sup>&lt;/h2>
&lt;ol>
&lt;li>
&lt;p>Train it first on 1 GPU for like 1000 iterations:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">./darknet detector train cfg/coco.data cfg/yolov4.cfg yolov4.conv.137
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>Then stop and by using partially-trained model &lt;code>/backup/yolov4_1000.weights&lt;/code>. Run training with multigpu (up to 4 GPUs): &lt;code>./darknet detector train cfg/coco.data cfg/yolov4.cfg /backup/yolov4_1000.weights -gpus 0,1,2,3&lt;/code>&lt;/p>
&lt;blockquote>
&lt;p>If you get a Nan, then for some datasets better to decrease learning rate, for 4 GPUs set &lt;code>learning_rate = 0,00065&lt;/code> (i.e. learning_rate = 0.00261 / GPUs). In this case also increase 4x times &lt;code>burn_in =&lt;/code> in your cfg-file. I.e. use &lt;code>burn_in = 4000&lt;/code> instead of &lt;code>1000&lt;/code>.&lt;/p>
&lt;/blockquote>
&lt;/li>
&lt;/ol>
&lt;h2 id="train-custom-datasets">Train custom datasets&lt;/h2>
&lt;p>Configuration setup see: &lt;a href="https://haobin-tan.netlify.app/docs/ai/computer-vision/object-detection/train-yolo-v4-custom-dataset/">Train YOLO v4 on Custom Dataset&lt;/a>&lt;/p>
&lt;p>Start training:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">./darknet detector train data/obj.data &amp;lt;custom-cfg&amp;gt; yolov4.conv.137
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>
&lt;p>File &lt;code>&amp;lt;custom-cfg&amp;gt;_last.weights&lt;/code> will be saved to &lt;code>backup/&lt;/code> for each 100 iterations&lt;/p>
&lt;/li>
&lt;li>
&lt;p>File &lt;code>&amp;lt;custom-cfg&amp;gt;_xxxx.weights&lt;/code> will be saved to &lt;code>backup/&lt;/code> for each 1000 iterations&lt;/p>
&lt;/li>
&lt;li>
&lt;p>if you train on server without monitor, disable Loss-window by using argument &lt;code>--dont_show&lt;/code>. I.e.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">./darknet detector train data/obj.data &amp;lt;custom-cfg&amp;gt; yolov4.conv.137 -dont_show
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>To see the mAP &amp;amp; Loss-chart during training on remote server without GUI, use&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">./darknet detector train data/obj.data &amp;lt;custom-cfg&amp;gt; yolov4.conv.137 -dont_show -mjpeg_port &lt;span class="m">8090&lt;/span> -map
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Then open URL &lt;code>http://ip-address:8090&lt;/code> in browser&lt;/p>
&lt;/li>
&lt;li>
&lt;p>For training with mAP calculation for each 4 Epochs, you need to&lt;/p>
&lt;ul>
&lt;li>
&lt;p>set &lt;code>valid=valid.txt&lt;/code> or &lt;code>train.txt&lt;/code> in &lt;code>obj.data&lt;/code> file&lt;/p>
&lt;/li>
&lt;li>
&lt;p>run training with &lt;code>-map&lt;/code> argument&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">./darknet detector train data/obj.data &amp;lt;custom-cfg&amp;gt; yolov4.conv.137 -map
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>After training is complete - get result &lt;code>yolo-obj_final.weights&lt;/code> from &lt;code>backup/&lt;/code>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>After each 100 iterations you can stop and later start training from this point. For example, after 2000 iterations you can stop training, and later just start training using:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">./darknet detector train data/obj.data &amp;lt;custom-cfg&amp;gt; backup/yolo-obj_2000.weights
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>You can get result earlier than all 45000 iterations.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h3 id="notes-">Notes 📝&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>If during training you see &lt;code>nan&lt;/code> values for &lt;code>avg&lt;/code> (loss) field, then training goes wrong. 😭&lt;/p>
&lt;p>But if &lt;code>nan&lt;/code> is in some other lines, then training goes well. 🙏&lt;/p>
&lt;/li>
&lt;li>
&lt;p>If you changed &lt;code>width=&lt;/code> or &lt;code>height=&lt;/code> in your cfg-file, then new width and height must be &lt;strong>divisible by 32&lt;/strong>.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>If error &lt;code>Out of memory&lt;/code> occurs then in &lt;code>.cfg&lt;/code>-file you should increase &lt;code>subdivisions=16&lt;/code>, 32 or 64&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="when-should-i-stop-training-4">When should I stop training &lt;sup id="fnref:4">&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref">4&lt;/a>&lt;/sup>&lt;/h2>
&lt;ul>
&lt;li>
&lt;p>Usually sufficient 2000 iterations for each class(object),&lt;/p>
&lt;ul>
&lt;li>but NOT less than number of training images and&lt;/li>
&lt;li>NOT less than 6000 iterations in total.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>During training, you will see varying indicators of error, and you should stop when no longer decreases &lt;strong>0.XXXXXXX avg&lt;/strong>&lt;/p>
&lt;blockquote>
&lt;p>For example&lt;/p>
&lt;p>&lt;strong>9002&lt;/strong>: 0.211667, &lt;strong>0.60730 avg&lt;/strong>, 0.001000 rate, 3.868000 seconds, 576128 images Loaded: 0.000000 seconds&lt;/p>
&lt;ul>
&lt;li>&lt;strong>9002&lt;/strong> - iteration number (number of batch)&lt;/li>
&lt;li>&lt;strong>0.60730 avg&lt;/strong> - average loss (error) - &lt;strong>the lower, the better&lt;/strong>&lt;/li>
&lt;/ul>
&lt;/blockquote>
&lt;p>he final avgerage loss can be from &lt;code>0.05&lt;/code> (for a small model and easy dataset) to &lt;code>3.0&lt;/code> (for a big model and a difficult dataset).&lt;/p>
&lt;/li>
&lt;li>
&lt;p>if you train with flag &lt;code>-map&lt;/code> then you will see mAP indicator like &lt;code>Last accuracy mAP@0.5 = 18.50%&lt;/code> in the console. This indicator is better than Loss, so keep training while mAP increases.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="choose-the-best-weights">Choose the best weights&lt;/h2>
&lt;p>Once training is stopped, you should take some of last &lt;code>.weights&lt;/code>-files from &lt;code>backup/&lt;/code> and choose the best of them.&lt;/p>
&lt;p>&lt;em>For example, you stopped training after 9000 iterations, but the best result can give one of previous weights (7000, 8000, 9000). It can happen due to overfitting.&lt;/em>&lt;/p>
&lt;p>In order to choose best weight, just train with &lt;code>-map&lt;/code> flag&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">./darknet detector train data/obj.data &amp;lt;custom-cfg&amp;gt; yolov4.conv.137 -dont_show -map
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>So you will see mAP-chart (red-line) in the Loss-chart Window looks like the following figure. mAP will be calculated for each 4 Epochs using &lt;code>valid=valid.txt&lt;/code> file that is specified in &lt;code>obj.data&lt;/code> file (&lt;code>1 Epoch = images_in_train_txt / batch&lt;/code> iterations)&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/68747470733a2f2f6873746f2e6f72672f776562742f79642f766c2f61672f7964766c616775746f66327a636e6a6f64737467726f656e3861632e6a706567.jpeg" alt="loss_chart_map_chart">&lt;/p>
&lt;h2 id="how-to-improve-object-detection-5">How to improve object detection&lt;sup id="fnref:5">&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref">5&lt;/a>&lt;/sup>&lt;/h2>
&lt;p>Before training&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Set flag &lt;code>random=1&lt;/code> in your &lt;code>.cfg&lt;/code>-file - it will increase precision by training Yolo for different resolutions&lt;/p>
&lt;/li>
&lt;li>
&lt;p>increase network resolution in your &lt;code>.cfg&lt;/code>-file (&lt;code>height=608&lt;/code>, &lt;code>width=608&lt;/code> or any value multiple of 32) - it will increase precision&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Check that each object that you want to detect is mandatory labeled in your dataset - no one object in your data set should not be without label.&lt;/p>
&lt;ul>
&lt;li>In the most training issues, there are wrong labels in your dataset. Always check your dataset by using: &lt;a href="https://github.com/AlexeyAB/Yolo_mark">https://github.com/AlexeyAB/Yolo_mark&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>My Loss is very high and mAP is very low, is training wrong?&lt;/p>
&lt;p>&amp;ndash;&amp;gt; Run training with &lt;code>-show_imgs&lt;/code> flag at the end of training command, do you see correct bounded boxes of objects? If no, your training dataset is wrong.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>For each object which you want to detect - there must be &lt;strong>at least 1 similar object&lt;/strong> in the Training dataset with about the same: shape, side of object, relative size, angle of rotation, tilt, illumination.&lt;/p>
&lt;ul>
&lt;li>So desirable that your training dataset include images with objects at diffrent: scales, rotations, lightings, from different sides, on different backgrounds&lt;/li>
&lt;li>You should preferably have 2000 different images for each class or more, and you should train &lt;code>2000*classes&lt;/code> iterations or more&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Desirable that your training dataset include images with non-labeled objects that you do not want to detect, i.e. negative samples without bounded box (empty &lt;code>.txt&lt;/code> files). Use as many images of negative samples as there are images with objects.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>More see: &lt;a href="https://github.com/AlexeyAB/darknet#how-to-improve-object-detection">https://github.com/AlexeyAB/darknet#how-to-improve-object-detection&lt;/a>&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>After training, for detection:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Increase network-resolution by set in your &lt;code>.cfg&lt;/code>-file (&lt;code>height=608&lt;/code> and &lt;code>width=608&lt;/code>) or (&lt;code>height=832&lt;/code> and &lt;code>width=832&lt;/code>) or (any value multiple of 32). This increases the precision and makes it possible to detect small objects.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>It is not necessary to train the network again, just use &lt;code>.weights&lt;/code>-file already trained for 416x416 resolution&lt;/p>
&lt;/li>
&lt;li>
&lt;p>To get even greater accuracy you should train with higher resolution 608x608 or 832x832.&lt;/p>
&lt;ul>
&lt;li>Note: if error &lt;code>Out of memory&lt;/code> occurs then in &lt;code>.cfg&lt;/code>-file you should increase &lt;code>subdivisions=16&lt;/code>, 32 or 64&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="other-questions">Other questions&lt;/h2>
&lt;h3 id="will-darknet-automaticly-resize-the-image-size">Will darknet automaticly resize the image size?&lt;/h3>
&lt;p>Yes (see: &lt;a href="https://github.com/AlexeyAB/darknet/issues/5842">https://github.com/AlexeyAB/darknet/issues/5842&lt;/a>)&lt;/p>
&lt;h3 id="does-the-network-have-to-be-perfectly-square">Does the network have to be perfectly square?&lt;/h3>
&lt;blockquote>
&lt;p>No.&lt;/p>
&lt;p>The default network sizes in the common template configuration files is defined as 416x416 or 608x608, but &lt;em>those are only examples!&lt;/em>&lt;/p>
&lt;p>Choose a size that works for you and your images. The only restrictions are:&lt;/p>
&lt;ul>
&lt;li>the width has to be evenly divisible by 32&lt;/li>
&lt;li>the height has to be evenly divisible by 32&lt;/li>
&lt;li>you must have enough video memory to train a network of that size&lt;/li>
&lt;/ul>
&lt;p>Whatever size you choose, Darknet will stretch (without preserving the aspect ratio!) your images to be exactly that size prior to processing the image. This includes both training and inference. So use a size that makes sense for you and the images you need to process, but remember that there are important speed and memory limitations. The larger the size, the slower it will be to train and run, and the more GPU memory will be required.&lt;/p>
&lt;/blockquote>
&lt;p>See:&lt;/p>
&lt;p>&lt;a href="https://www.ccoderun.ca/programming/2020-09-25_Darknet_FAQ/#square_network">https://www.ccoderun.ca/programming/2020-09-25_Darknet_FAQ/#square_network&lt;/a>&lt;/p>
&lt;h3 id="detection-with-aspect-ratio-change">Detection with aspect ratio change&lt;/h3>
&lt;ol>
&lt;li>First of all, the high network resolution is important (the higher - the better). I.e. 800 x 800 will be better than 736 x 416, even if your input image 1600 x 900.&lt;/li>
&lt;li>And only In second place in importance is the aspect ratio.&lt;/li>
&lt;/ol>
&lt;p>See: &lt;a href="https://github.com/AlexeyAB/darknet/issues/131">https://github.com/AlexeyAB/darknet/issues/131&lt;/a>&lt;/p>
&lt;h2 id="useful-resources">Useful resources&lt;/h2>
&lt;ul>
&lt;li>Tips from Roboflow: &lt;a href="https://blog.roboflow.com/yolov4-tactics/">YOLOv4 - Ten Tactics to Build a Better Model&lt;/a>&lt;/li>
&lt;li>Articles from Aleksey Bochkovskiy (author of YOLOv4)
&lt;ul>
&lt;li>&lt;strong>&lt;a href="https://alexeyab84.medium.com/yolov4-the-most-accurate-real-time-neural-network-on-ms-coco-dataset-73adfd3602fe">YOLOv4 — the most accurate real-time neural network on MS COCO dataset.&lt;/a>&lt;/strong>&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://alexeyab84.medium.com/scaled-yolo-v4-is-the-best-neural-network-for-object-detection-on-ms-coco-dataset-39dfa22fa982">Scaled YOLO v4 is the best neural network for object detection on MS COCO dataset&lt;/a>&lt;/strong>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;a href="https://www.ccoderun.ca/programming/2020-09-25_Darknet_FAQ/#how_to_get_started">DARKNET FAQ&lt;/a>&lt;/li>
&lt;/ul>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>&lt;a href="https://github.com/AlexeyAB/darknet/wiki/FAQ---frequently-asked-questions#1-i-get-low-accuracy">FAQ: I get low accuracy&lt;/a>&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:2">
&lt;p>&lt;a href="https://github.com/AlexeyAB/darknet/wiki/FAQ---frequently-asked-questions#2-darknet-trainingdetection-crashes-with-an-error">FAQ: Darknet training/detection crashes with an error&lt;/a>&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:3">
&lt;p>&lt;a href="https://github.com/AlexeyAB/darknet#how-to-train-with-multi-gpu">How to train with multi-GPU&lt;/a>&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:4">
&lt;p>&lt;a href="https://github.com/AlexeyAB/darknet#when-should-i-stop-training">When should I stop training&lt;/a>&amp;#160;&lt;a href="#fnref:4" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:5">
&lt;p>&lt;a href="https://github.com/AlexeyAB/darknet#how-to-improve-object-detection">How to improve object detection&lt;/a>&amp;#160;&lt;a href="#fnref:5" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description></item></channel></rss>