Skip to content

Kuchunan/SnapSort-Trash-Classification-with-YOLO-v4-Darknet-Onnx-TensorRT-for-Jetson-Nano

Repository files navigation

SnapSort Introduction

SnapSort is a trash sorting assistant with the YOLO V4 model. It will take in a video feed from the camera and return visual and audio instructions to users. The project is designed to help people sort their waste properly before waste collection. We placed the device, Jetson Nano or GTX 1080 with a screen, on top of trash cans to guide people on sorting waste.

The project is built for the University of Washington GIX TECHIN 514 Hardware Software Lab 1 course, Microsoft Imagine Cup, and Alaska Environment Innovation Challenges. The code and dataset are collected and built by Yun Liu, Joey Wang, and me.

The dataset reaches 71% MAP for 12 different categories on an 80/20 train/test split trained on the YOLO v4 object detection model.

I also convert the Snapsort Yolo model to Onnx and TensorRT format for better performance on Jetson Nano.

alt text

Table of content

  1. Introduction
  2. What's new
  3. Dataset
  4. Running Snapsort on Windows/Linux
  5. Runing Snapsort on Jetson Nano
  6. Others

What's new (2021.03.04)

  • Add trained weights with Yolo V4, V4 Tiny model
  • Optimized performance for Jetson Nano with Onnx and Tensor RT core

Dataset

The dataset contains 4600 original images including images from Coco Dataset, Google open images v4, and images we collected by ourselves. Images are labeled into 12 classes manually following Seattle Government's recycling rule. After data augmentation, 80% of images are used for training and 20% of images are used for testing. The best training result we got is 71% MAP @ 50% confidence threshold.

Running Snapsort on Windows/Linux

To run Snapsort on a Windows device, please install all the dependencies listed below and Darknet Yolo V4 by AlexyAB(see Prerequisites below).

Prerequisites

Installing Python and dependencies

Installing Yolo V4

The Yolo V4 model needs to be installed before using our trained weight. The most Windows-friendly version I found is from AlexAB.

Usage

Step 1: Download and unzip

Download and unzip all the files into the Yolo build file. My file location C:\darknet-master\build\darknet\x64.

cd C:\darknet-master\build\darknet\x64
git clone https://github.com/Kuchunan/SnapSort-Trash-Classification-with-YOLO-v4-Darknet-Onnx-TensorRT-for-Jetson-Nano.git

Step 2: select a weight to be used

Change the file location according to the weights you want to use at line 159~162 in darknet_video-Ku.py. Option 1. 12 classes detection

configPath = "./backup/0311_files/yolov3_5l-GIX_0304_test.cfg"
weightPath = "./backup/0311_files/yolov3_5l-GIX_0326_best.weights"
metaPath = "./backup/0311_files/Aug.data"

Option 2. 3 classes detection(Recycle, Landfill, Compost)

configPath = "./backup/0311_files/yolov3_5l-GIX_0308_RLC_test.cfg"
weightPath = "./backup/0311_files/yolov3_5l-GIX_0308_RLC_best.weights"
metaPath = "./backup/0311_files/Aug_RLC.data"

Option 3. 12 classes with Yolo V2

configPath = "./backup/yolov2/yolov2_GIX_test.cfg"
weightPath = "./backup/yolov2/yolov2_GIX_best.weights"
metaPath = "./backup/0311_files/Aug.data"

Option 4. 12 classes with Yolo V4

configPath = "./backup/Yolo_V4&V4_Tiny/yolov4_GIX_best-416.cfg"
weightPath = "./backup/Yolo_V4&V4_Tiny/yolov4_GIX_best-416.weights"
metaPath = "./backup/0311_files/Aug.data"

Option 5. 12 classes with Yolo V4 Tiny (416*416)

configPath = "./backup/Yolo_V4&V4_Tiny/yolov4-tiny_GIX-416.cfg"
weightPath = "./backup/Yolo_V4&V4_Tiny/yolov4-tiny_GIX-416.weights"
metaPath = "./backup/0311_files/Aug.data"

Option 6. 12 classes with Yolo V4 Tiny (640*640)

configPath = "./backup/Yolo_V4&V4_Tiny/yolov4-tiny_GIX-640.cfg"
weightPath = "./backup/Yolo_V4&V4_Tiny/yolov4-tiny_GIX-640.weights"
metaPath = "./backup/0311_files/Aug.data"

Step 3: change the interval of audio instruction

Audio instruction will be given every 45 frames(default). It will collect all the detection results in these frames, merge the result for IoU higher than 50%, and give the audio feedback of the highest possibility. You can change the interval on line 14.

AvergeOverTime = 45

You can change the IOU on line 71.

def nms(detections,threshold=.5):

Step 4: other minor changes

To change video quality:

VWidth = 1280
VHight = 720

To change input camera (0 means built-in camera)

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

To change thredhold:

detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.2,hier_thresh=0.3, nms=0.4) 

Step 5: run the code and start detection

Run the code and the detection will start. Hit ESC to exit.

alt text

Runing Snapsort on Jetson Nano

Prerequisites

Hardware Preparation

  • Jetson Nano * 1
  • 32G micro SD card
  • Webcam * 1 (Recommend: Logitech C920 HD, higher resolution and low noise will improve performance)
  • 5V DC barrel jack power supply
  • Screen (with power supply and HDMI cable)
  • (Optional) Mouse & Keyboard

Step 1: Format SD card and install Jetpack

Step 2: Install Onnx and TensorRT

  • Make sure connected to the internet (using an ethernet cable or wifi dongle)
  • Open Command Line terminal
sudo apt-get install update
sudo apt-get install upgrade
sudo apt install python3-pip
pip3 install protobuf==3.8.0
git clone https://github.com/jkjung-avt/tensorrt_demos.git
cd tensorrt_demos/ssd
export PATH=${PATH}:/usr/local/cuda/bin
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda/lib64
./install_pycuda.sh
sudo apt-get install python-pip protobuf-compiler libprotoc-dev
pip install Cython
sudo pip3 install onnx==1.4.1
cd ..
cd plugins
Make

Step 3: Download models in Onnx & TensorRT

  • Download all the files in the "Onnx & TensorRT' folder (Click here) to folder “~/tensorrt_demos/yolo”

Step 4:Download revised Python script

  • Download yolo_classes.py and visualization.py to ~/tensorrt_demos/utils

    Changes in yolo_classes.py from jkjung-avt's file:

    1. Line 6-21: Replace COCO_CLASSES_LIST with GIX_CLASSES_LIST & GIX_3CLASSES_LIST
    2. Line 36-39: Add parameter support: category_num=12 & category_num=3

    Changes in visualization.py from jkjung-avt's file:

    1. Line 79: Add color_rule list to map with the color of trash bins
    2. Line 99-102: Add rules to apply to 3 classes and 12 classes


**Mapping rule**
12 classes 3 classes
Plastic_soft Landfill
Food_soiled_coated_paper Landfill
Plastic_utensils Landfill
Foam Landfill
Miscellaneous Landfill
Carboard Recycle
Paper Recycle
Plastic_rigid Recycle
Glass Recycle
Metal Recycle
Food_scrapes Compost
Paper_towels Compost

Usage

Step 1:Run testing

  • Open Command Line terminal
  • Plug-in a USB webcam

Option 1. 12 classes detection with Yolo V4 (416x416)

cd tensorrt_demos
python3 trt_yolo.py --usb 0 -m yolov4_GIX_best-416 --category_num=12

Option 2. 12 classes detection with Yolo V4 Tiny (416x416)

cd tensorrt_demos
python3 trt_yolo.py --usb 0 -m yolov4-tiny_GIX-416 --category_num=12

Option 3. 12 classes detection with Yolo V4 Tiny (640x640)

cd tensorrt_demos
python3 trt_yolo.py --usb 0 -m yolov4-tiny_GIX-640 --category_num=12

Option 4. 3 classes detection with Yolo V4 (416x416)

cd tensorrt_demos
python3 trt_yolo.py --usb 0 -m yolov4_GIX_best-416 --category_num=3

Option 5. 3 classes detection with Yolo V4 Tiny (416x416)

cd tensorrt_demos
python3 trt_yolo.py --usb 0 -m yolov4-tiny_GIX-416 --category_num=3

Option 6. 3 classes detection with Yolo V4 Tiny (640x640)

cd tensorrt_demos
python3 trt_yolo.py --usb 0 -m yolov4-tiny_GIX-640 --category_num=3
  • "F" for fullscreen
Model Resolution Fps on Jetson nano
Yolo v4 Tiny 416x416 ~19
Yolo v4 416x416 ~4
Yolo V4 416x416 ~3.x

(Optional) Step 2: Auto fullscreen

  • Double click /tensorrt_demos/trt_yolo.py to edit
  • Add this line to Line #69 (Leave 6 blank spaces for indention)
        set_display(WINDOW_NAME, True)
  • Save the file

(Optional) Step 3: Auto-start after Boot

  • Download reboot.sh to ~/ (“Home” folder)

  • Click the start button on the upper left corner, search and open “Start-up applications”.

  • On the “Start-up preference” window, click “Add”

  • Name: (any)

  • Command: (select reboot.sh)

  • Click “Add”

    Reference

Others

Built With

Co-Author

Inspiration and Acknowledgments

Video Documentary

About

How to classify trash with Yolo v3

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published