Hi, Below is the code snippet used for generating the engine file
!tao converter -k $KEY \
-p Input,1x3x544x960,8x3x544x960,16x3x544x960 \
-c $USER_EXPERIMENT_DIR/export/cal.bin \
-e $USER_EXPERIMENT_DIR/export/trt.engine \
-b 8 \
-t int8 \
$USER_EXPERIMENT_DIR/export/yolov4_resnet18_epoch_$EPOCH.etlt
Following is the entire code used for training in tao
#!/usr/bin/env python
# coding: utf-8
# # Object Detection using TAO YOLOv4
#
# Transfer learning is the process of transferring learned features from one application to another. It is a commonly used training technique where you use a model trained on one task and re-train to use it on a different task.
#
# Train Adapt Optimize (TAO) Toolkit is a simple and easy-to-use Python based AI toolkit for taking purpose-built AI models and customizing them with users' own data.
#
# <img align="center" src="https://developer.nvidia.com/sites/default/files/akamai/embedded-transfer-learning-toolkit-software-stack-1200x670px.png" width="1080">
#
# ## Learning Objectives
# In this notebook, you will learn how to leverage the simplicity and convenience of TAO to:
#
# * Take a pretrained resnet18 model and train a ResNet-18 Yolo_v4 model on the KITTI dataset
# * Prune the trained yolo_V4 model
# * Retrain the pruned model to recover lost accuracy
# * Export the pruned model
# * Quantize the pruned model using QAT
# * Run Inference on the trained model
# * Export the pruned, quantized and retrained model to a .etlt file for deployment to DeepStream
# * Run inference on the exported. etlt model to verify deployment using TensorRT
#
# ## Table of Contents
#
# This notebook shows an example usecase of YOLO v4 object detection using Train Adapt Optimize (TAO) Toolkit.
#
# 0. [Set up env variables and map drives](#head-0)
# 1. [Install the TAO launcher](#head-1)
# 2. [Prepare dataset and pre-trained model](#head-2) <br>
# 2.1 [Download the dataset](#head-2-1)<br>
# 2.2 [Verify the downloaded dataset](#head-2-2)<br>
# 2.3 [Generate tfrecords](#head-2-3)<br>
# 2.4 [Download pretrained model](#head-2-4)
# 3. [Provide training specification](#head-3)
# 4. [Run TAO training](#head-4)
# 5. [Evaluate trained models](#head-5)
# 6. [Prune trained models](#head-6)
# 7. [Retrain pruned models](#head-7)
# 8. [Evaluate retrained model](#head-8)
# 9. [Visualize inferences](#head-9)
# 10. [Model Export](#head-10)
# 11. [Verify deployed model](#head-11)
#
# ## 0. Set up env variables and map drives <a class="anchor" id="head-0"></a>
#
# When using the purpose-built pretrained models from NGC, please make sure to set the `$KEY` environment variable to the key as mentioned in the model overview. Failing to do so, can lead to errors when trying to load them as pretrained models.
#
# The following notebook requires the user to set an env variable called the `$LOCAL_PROJECT_DIR` as the path to the users workspace. Please note that the dataset to run this notebook is expected to reside in the `$LOCAL_PROJECT_DIR/data`, while the TAO experiment generated collaterals will be output to `$LOCAL_PROJECT_DIR/yolo_v4`. More information on how to set up the dataset and the supported steps in the TAO workflow are provided in the subsequent cells.
#
# *Note: Please make sure to remove any stray artifacts/files from the `$USER_EXPERIMENT_DIR` or `$DATA_DOWNLOAD_DIR` paths as mentioned below, that may have been generated from previous experiments. Having checkpoint files etc may interfere with creating a training graph for a new experiment.*
# In[4]:
import os
get_ipython().run_line_magic('env', 'KEY=ZGNpYXZ0NHE1czFmbDBlcGR0Z2RzOHJqcWw6NGZjMjUwMDMtN2QyNC00MzYzLTlhZDctOTA1MDM3YTUwYTMy')
# In[8]:
# Setting up env variables for cleaner command line commands.
import os
print("Please replace the variable with your key.")
get_ipython().run_line_magic('env', 'KEY=ZGNpYXZ0NHE1czFmbDBlcGR0Z2RzOHJqcWw6NGZjMjUwMDMtN2QyNC00MzYzLTlhZDctOTA1MDM3YTUwYTMy')
#%env KEY= tlt_encode
get_ipython().run_line_magic('env', 'USER_EXPERIMENT_DIR=/workspace/tao-experiments/yolo_v4')
get_ipython().run_line_magic('env', 'DATA_DOWNLOAD_DIR=/workspace/tao-experiments/data')
# Set this path if you don't run the notebook from the samples directory.
# %env NOTEBOOK_ROOT=~/tao-samples/yolo_v4
# Please define this local project directory that needs to be mapped to the TAO docker session.
# The dataset expected to be present in $LOCAL_PROJECT_DIR/data, while the results for the steps
# in this notebook will be stored at $LOCAL_PROJECT_DIR/yolo_v4
get_ipython().run_line_magic('env', 'LOCAL_PROJECT_DIR=/opt/nvidia/deepstream/deepstream-6.2/Amiya_test')
os.environ["LOCAL_DATA_DIR"] = os.path.join(os.getenv("LOCAL_PROJECT_DIR", os.getcwd()), "data")
os.environ["LOCAL_EXPERIMENT_DIR"] = os.path.join(os.getenv("LOCAL_PROJECT_DIR", os.getcwd()), "yolo_v4")
# The sample spec files are present in the same path as the downloaded samples.
os.environ["LOCAL_SPECS_DIR"] = os.path.join(
os.getenv("NOTEBOOK_ROOT", os.getcwd()),
"specs"
)
get_ipython().run_line_magic('env', 'SPECS_DIR=/workspace/tao-experiments/yolo_v4/specs')
# Showing list of specification files.
get_ipython().system('ls -rlt $LOCAL_SPECS_DIR')
# In[9]:
# Create local dir
get_ipython().system('mkdir -p $LOCAL_DATA_DIR')
get_ipython().system('mkdir -p $LOCAL_EXPERIMENT_DIR')
# The cell below maps the project directory on your local host to a workspace directory in the TAO docker instance, so that the data and the results are mapped from outside to inside of the docker instance.
# In[10]:
# Mapping up the local directories to the TAO docker.
import json
mounts_file = os.path.expanduser("~/.tao_mounts.json")
# Define the dictionary with the mapped drives
drive_map = {
"Mounts": [
# Mapping the data directory
{
"source": os.environ["LOCAL_PROJECT_DIR"],
"destination": "/workspace/tao-experiments"
},
# Mapping the specs directory.
{
"source": os.environ["LOCAL_SPECS_DIR"],
"destination": os.environ["SPECS_DIR"]
},
],
"DockerOptions": {
"user": "0:0"
}
}
# Writing the mounts file.
with open(mounts_file, "w") as mfile:
json.dump(drive_map, mfile, indent=4)
# In[11]:
get_ipython().system('cat ~/.tao_mounts.json')
# ## 1. Install the TAO launcher <a class="anchor" id="head-1"></a>
# The TAO launcher is a python package distributed as a python wheel listed in the `nvidia-pyindex` python index. You may install the launcher by executing the following cell.
#
# Please note that TAO Toolkit recommends users to run the TAO launcher in a virtual env with python 3.6.9. You may follow the instruction in this [page](https://virtualenvwrapper.readthedocs.io/en/latest/install.html) to set up a python virtual env using the `virtualenv` and `virtualenvwrapper` packages. Once you have setup virtualenvwrapper, please set the version of python to be used in the virtual env by using the `VIRTUALENVWRAPPER_PYTHON` variable. You may do so by running
#
# ```sh
# export VIRTUALENVWRAPPER_PYTHON=/path/to/bin/python3.x
# ```
# where x >= 6 and <= 8
#
# We recommend performing this step first and then launching the notebook from the virtual environment. In addition to installing TAO python package, please make sure of the following software requirements:
# * python >=3.6.9 < 3.8.x
# * docker-ce > 19.03.5
# * docker-API 1.40
# * nvidia-container-toolkit > 1.3.0-1
# * nvidia-container-runtime > 3.4.0-1
# * nvidia-docker2 > 2.5.0-1
# * nvidia-driver > 455+
#
# Once you have installed the pre-requisites, please log in to the docker registry nvcr.io by following the command below
#
# ```sh
# docker login nvcr.io
# ```
#
# You will be triggered to enter a username and password. The username is `$oauthtoken` and the password is the API key generated from `ngc.nvidia.com`. Please follow the instructions in the [NGC setup guide](https://docs.nvidia.com/ngc/ngc-overview/index.html#generating-api-key) to generate your own API key.
#
# After setting up your virtual environment with the above requirements, install TAO pip package.
# In[2]:
# SKIP this step IF you have already installed the TAO launcher.
get_ipython().system('pip3 install nvidia-pyindex')
get_ipython().system('pip3 install nvidia-tao')
# In[12]:
# View the versions of the TAO launcher
get_ipython().system('tao info')
# ## 2. Prepare dataset and pre-trained model <a class="anchor" id="head-2"></a>
# We will be using the KITTI detection dataset for the tutorial. To find more details please visit
# http://www.cvlibs.net/datasets/kitti/eval_object.php?obj_benchmark=2d. Please download the KITTI detection images (http://www.cvlibs.net/download.php?file=data_object_image_2.zip) and labels (http://www.cvlibs.net/download.php?file=data_object_label_2.zip) to $DATA_DOWNLOAD_DIR.
#
# The data will then be extracted to have
# * training images in `$LOCAL_DATA_DIR/training/image_2`
# * training labels in `$LOCAL_DATA_DIR/training/label_2`
# * testing images in `$LOCAL_DATA_DIR/testing/image_2`
#
# You may use this notebook with your own dataset as well. To use this example with your own dataset, please follow the same directory structure as mentioned below.
#
# *Note: There are no labels for the testing images, therefore we use it just to visualize inferences for the trained model.*
# ### 2.1 Download the dataset <a class="anchor" id="head-2-1"></a>
# Once you have gotten the download links in your email, please populate them in place of the `KITTI_IMAGES_DOWNLOAD_URL` and the `KITTI_LABELS_DOWNLOAD_URL`. This next cell, will download the data and place in `$LOCAL_DATA_DIR`
# In[19]:
import os
get_ipython().system('mkdir -p $LOCAL_DATA_DIR')
os.environ["URL_IMAGES"]="https://protect2.fireeye.com/v1/url?k=8b9a4b96-d4010b69-8b9bc169-00259087933a-82e4b91cd99e6c98&q=1&e=2b12f31f-399a-4fd8-a28c-6a41db16d05a&u=https%3A%2F%2Fs3.eu-central-1.amazonaws.com%2Favg-kitti%2Fdata_object_image_2.zip"
get_ipython().system('if [ ! -f $LOCAL_DATA_DIR/data_object_image_2.zip ]; then wget $URL_IMAGES -O $LOCAL_DATA_DIR/data_object_image_2.zip; else echo "image archive already downloaded"; fi ')
os.environ["URL_LABELS"]="https://protect2.fireeye.com/v1/url?k=bae0d8ec-e57be7c0-bae15213-00259087546c-a1542cded5a6d59f&q=1&e=943fa944-bce8-41ac-ae39-b4ebfcff20d4&u=https%3A%2F%2Fs3.eu-central-1.amazonaws.com%2Favg-kitti%2Fdata_object_label_2.zip"
get_ipython().system('if [ ! -f $LOCAL_DATA_DIR/data_object_label_2.zip ]; then wget $URL_LABELS -O $LOCAL_DATA_DIR/data_object_label_2.zip; else echo "label archive already downloaded"; fi ')
# ### 2.2 Verify the downloaded dataset <a class="anchor" id="head-2-2"></a>
# In[20]:
# Check the dataset is present
get_ipython().system('mkdir -p $LOCAL_DATA_DIR')
get_ipython().system("if [ ! -f $LOCAL_DATA_DIR/data_object_image_2.zip ]; then echo 'Image zip file not found, please download.'; else echo 'Found Image zip file.';fi")
get_ipython().system("if [ ! -f $LOCAL_DATA_DIR/data_object_label_2.zip ]; then echo 'Label zip file not found, please download.'; else echo 'Found Labels zip file.';fi")
# In[21]:
# This may take a while: verify integrity of zip files
get_ipython().system('sha256sum $LOCAL_DATA_DIR/data_object_image_2.zip | cut -d \' \' -f 1 | grep -xq \'^351c5a2aa0cd9238b50174a3a62b846bc5855da256b82a196431d60ff8d43617$\' ; if test $? -eq 0; then echo "images OK"; else echo "images corrupt, re-download!" && rm -f $LOCAL_DATA_DIR/data_object_image_2.zip; fi ')
get_ipython().system('sha256sum $LOCAL_DATA_DIR/data_object_label_2.zip | cut -d \' \' -f 1 | grep -xq \'^4efc76220d867e1c31bb980bbf8cbc02599f02a9cb4350effa98dbb04aaed880$\' ; if test $? -eq 0; then echo "labels OK"; else echo "labels corrupt, re-download!" && rm -f $LOCAL_DATA_DIR/data_object_label_2.zip; fi ')
# In[5]:
# unpack
#!unzip -u $LOCAL_DATA_DIR/data_object_image_2.zip -d $LOCAL_DATA_DIR
#!unzip -u $LOCAL_DATA_DIR/data_object_label_2.zip -d $LOCAL_DATA_DIR
get_ipython().system('unzip -u $LOCAL_DATA_DIR/images.zip -d $LOCAL_DATA_DIR')
# In[55]:
# verify
import os
DATA_DIR = os.environ.get('LOCAL_DATA_DIR')
num_training_images = len(os.listdir(os.path.join(DATA_DIR, "training/image_2")))
num_training_labels = len(os.listdir(os.path.join(DATA_DIR, "training/label_2")))
num_testing_images = len(os.listdir(os.path.join(DATA_DIR, "testing/image_2")))
print("Number of images in the train/val set. {}".format(num_training_images))
print("Number of labels in the train/val set. {}".format(num_training_labels))
print("Number of images in the test set. {}".format(num_testing_images))
# In[7]:
# Sample kitti label.
get_ipython().system('cat $LOCAL_DATA_DIR/training/label_2/frame1.txt')
# In[13]:
# Generate val dataset out of training dataset
get_ipython().system('python3 ../ssd/generate_val_dataset.py --input_image_dir=$LOCAL_DATA_DIR/training/image_2 --input_label_dir=$LOCAL_DATA_DIR/training/label_2 --output_dir=$LOCAL_DATA_DIR/val')
# Additionally, if you have your own dataset already in a volume (or folder), you can mount the volume on `LOCAL_DATA_DIR` (or create a soft link). Below shows an example:
# ```bash
# # if your dataset is in /dev/sdc1
# mount /dev/sdc1 $LOCAL_DATA_DIR
#
# # if your dataset is in folder /var/dataset
# ln -sf /var/dataset $LOCAL_DATA_DIR
# ```
# In[6]:
# If you use your own dataset, you will need to run the code below to generate the best anchor shape
get_ipython().system('tao yolo_v4 kmeans -l $DATA_DOWNLOAD_DIR/training/label_2 -i $DATA_DOWNLOAD_DIR/training/image_2 -n 9 -x 960 -y 544')
# -x 1248 \
# -y 38
# The anchor shape generated by this script is sorted. Write the first 3 into small_anchor_shape in the config
# file. Write middle 3 into mid_anchor_shape. Write last 3 into big_anchor_shape.
# ### 2.3 Generate tfrecords <a class="anchor" id="head-2-3"></a>
# The default YOLOv4 data format requires generation of TFRecords. Currently, the old sequence data format (image folders and label txt folders) is still supported and if you prefer to use the sequence data format, you can skip this section. To use sequence data format, please use spec file `yolo_v4_train_resnet18_kitti_seq.txt` and `yolo_v4_retrain_resnet18_kitti_seq.txt`. And you can check our [user guide](https://docs.nvidia.com/tao/tao-toolkit/text/object_detection/yolo_v4.html#dataset-config) for more details about tfrecords generation and sequence data format usage.
#
# Note: we observe that for YOLOv4, when mosaic augmentation is turned on (mosaic_prob > 0), the sequence format has faster training speed.
#
# Note: we observe the TFRecords format sometimes results in CUDA error during evaluation. Setting `force_on_cpu` in `nms_config` to `true` can help prevent this problem.
# In[8]:
get_ipython().system('tao yolo_v4 dataset_convert -d $SPECS_DIR/yolo_v4_tfrecords_kitti_train.txt -o $DATA_DOWNLOAD_DIR/training/tfrecords/train')
# In[9]:
get_ipython().system('tao yolo_v4 dataset_convert -d $SPECS_DIR/yolo_v4_tfrecords_kitti_val.txt -o $DATA_DOWNLOAD_DIR/val/tfrecords/val')
# ### 2.4 Download pre-trained model <a class="anchor" id="head-2-4"></a>
# We will use NGC CLI to get the pre-trained models. For more details, go to [ngc.nvidia.com](ngc.nvidia.com) and click the SETUP on the navigation bar.
# In[56]:
# Installing NGC CLI on the local machine.
## Download and install
get_ipython().run_line_magic('env', 'CLI=ngccli_cat_linux.zip')
get_ipython().system('mkdir -p $LOCAL_PROJECT_DIR/ngccli')
# Remove any previously existing CLI installations
get_ipython().system('rm -rf $LOCAL_PROJECT_DIR/ngccli/*')
get_ipython().system('wget "https://ngc.nvidia.com/downloads/$CLI" -P $LOCAL_PROJECT_DIR/ngccli')
get_ipython().system('unzip -u "$LOCAL_PROJECT_DIR/ngccli/$CLI" -d $LOCAL_PROJECT_DIR/ngccli/')
get_ipython().system('rm $LOCAL_PROJECT_DIR/ngccli/*.zip ')
os.environ["PATH"]="{}/ngccli:{}".format(os.getenv("LOCAL_PROJECT_DIR", ""), os.getenv("PATH", ""))
# In[7]:
get_ipython().system('ngc registry model list nvidia/tao/pretrained_object_detection:*')
# In[12]:
get_ipython().system('mkdir -p $LOCAL_EXPERIMENT_DIR/pretrained_resnet18/')
# In[57]:
# Pull pretrained model from NGC
get_ipython().system('ngc registry model download-version nvidia/tao/pretrained_object_detection:resnet18 --dest $LOCAL_EXPERIMENT_DIR/pretrained_resnet18')
# In[58]:
print("Check that model is downloaded into dir.")
get_ipython().system('ls -l $LOCAL_EXPERIMENT_DIR/pretrained_resnet18/pretrained_object_detection_vresnet18')
# ## 3. Provide training specification <a class="anchor" id="head-3"></a>
# * Augmentation parameters for on-the-fly data augmentation
# * Other training (hyper-)parameters such as batch size, number of epochs, learning rate etc.
# * Whether to use quantization aware training (QAT)
# In[6]:
# Provide pretrained model path
get_ipython().system('sed -i \'s,EXPERIMENT_DIR,\'"$USER_EXPERIMENT_DIR"\',\' $LOCAL_SPECS_DIR/yolo_v4_train_resnet18_kitti.txt')
# To enable QAT training on sample spec file, uncomment following lines
# !sed -i "s/enable_qat: false/enable_qat: true/g" $LOCAL_SPECS_DIR/yolo_v4_train_resnet18_kitti.txt
# !sed -i "s/enable_qat: false/enable_qat: true/g" $LOCAL_SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt
# In[13]:
# By default, the sample spec file disables QAT training. You can force non-QAT training by running lines below
# !sed -i "s/enable_qat: true/enable_qat: false/g" $LOCAL_SPECS_DIR/yolo_v4_train_resnet18_kitti.txt
# !sed -i "s/enable_qat: true/enable_qat: false/g" $LOCAL_SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt
# In[7]:
get_ipython().system('cat $LOCAL_SPECS_DIR/yolo_v4_train_resnet18_kitti.txt')
# ## 4. Run TAO training <a class="anchor" id="head-4"></a>
# * Provide the sample spec file and the output directory location for models
# * WARNING: training will take several hours or one day to complete
# In[16]:
get_ipython().system('mkdir -p $LOCAL_EXPERIMENT_DIR/experiment_dir_unpruned')
# In[6]:
print("To run with multigpu, please change --gpus based on the number of available GPUs in your machine.")
get_ipython().system('tao yolo_v4 train -e $SPECS_DIR/yolo_v4_train_resnet18_kitti.txt -r $USER_EXPERIMENT_DIR/experiment_dir_unpruned -k $KEY --gpus 1')
# In[ ]:
print("To resume from checkpoint, please change pretrain_model_path to resume_model_path in config file.")
# In[12]:
print('Model for each epoch:')
print('---------------------')
get_ipython().system('ls -ltrh $LOCAL_EXPERIMENT_DIR/experiment_dir_unpruned/weights')
# In[5]:
# Now check the evaluation stats in the csv file and pick the model with highest eval accuracy.
get_ipython().system('cat $LOCAL_EXPERIMENT_DIR/experiment_dir_unpruned/yolov4_training_log_resnet18.csv')
get_ipython().run_line_magic('set_env', 'EPOCH=080')
# ## 5. Evaluate trained models <a class="anchor" id="head-5"></a>
# In[8]:
get_ipython().system('tao yolo_v4 evaluate -e $SPECS_DIR/yolo_v4_train_resnet18_kitti.txt -m $USER_EXPERIMENT_DIR/experiment_dir_unpruned/weights/resnet18_peoplenet.tlt -k $KEY')
# In[ ]:
#-m $USER_EXPERIMENT_DIR/experiment_dir_unpruned/weights/yolov4_resnet18_epoch_$EPOCH.tlt \
# In[98]:
get_ipython().system('ls -ltrh $KEY')
# ## 6. Prune trained models <a class="anchor" id="head-6"></a>
# * Specify pre-trained model
# * Equalization criterion (`Only for resnets as they have element wise operations or MobileNets.`)
# * Threshold for pruning.
# * A key to save and load the model
# * Output directory to store the model
#
# Usually, you just need to adjust `-pth` (threshold) for accuracy and model size trade off. Higher `pth` gives you smaller model (and thus higher inference speed) but worse accuracy. The threshold value depends on the dataset and the model. `0.5` in the block below is just a start point. If the retrain accuracy is good, you can increase this value to get smaller models. Otherwise, lower this value to get better accuracy.
# In[10]:
get_ipython().system('mkdir -p $LOCAL_EXPERIMENT_DIR/experiment_dir_pruned')
# In[11]:
get_ipython().system('tao yolo_v4 prune -m $USER_EXPERIMENT_DIR/experiment_dir_unpruned/weights/yolov4_resnet18_epoch_$EPOCH.tlt -e $SPECS_DIR/yolo_v4_train_resnet18_kitti.txt -o $USER_EXPERIMENT_DIR/experiment_dir_pruned/yolov4_resnet18_pruned.tlt -eq intersection -pth 0.1 -k $KEY')
# In[14]:
get_ipython().system('ls -rlt $LOCAL_EXPERIMENT_DIR/experiment_dir_pruned/')
# ## 7. Retrain pruned models <a class="anchor" id="head-7"></a>
# * Model needs to be re-trained to bring back accuracy after pruning
# * Specify re-training specification
# * WARNING: training will take several hours or one day to complete
# In[13]:
# Printing the retrain spec file.
# Here we have updated the spec file to include the newly pruned model as a pretrained weights.
get_ipython().system('sed -i \'s,EXPERIMENT_DIR,\'"$USER_EXPERIMENT_DIR"\',\' $LOCAL_SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt')
get_ipython().system('cat $LOCAL_SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt')
# In[14]:
get_ipython().system('mkdir -p $LOCAL_EXPERIMENT_DIR/experiment_dir_retrain')
# In[16]:
# Retraining using the pruned model as pretrained weights
get_ipython().system('tao yolo_v4 train --gpus 1 -e $SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt -r $USER_EXPERIMENT_DIR/experiment_dir_retrain -k $KEY')
# In[ ]:
# Listing the newly retrained model.
get_ipython().system('ls -rlt $LOCAL_EXPERIMENT_DIR/experiment_dir_retrain/weights')
# In[ ]:
# Now check the evaluation stats in the csv file and pick the model with highest eval accuracy.
get_ipython().system('cat $LOCAL_EXPERIMENT_DIR/experiment_dir_retrain/yolov4_training_log_resnet18.csv')
get_ipython().run_line_magic('set_env', 'EPOCH=080')
# In[13]:
get_ipython().run_line_magic('set_env', 'EPOCH=080')
# ## 8. Evaluate retrained model <a class="anchor" id="head-8"></a>
# In[16]:
get_ipython().system('tao yolo_v4 evaluate -e $SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt #-m $USER_EXPERIMENT_DIR/experiment_dir_retrain/weights/yolov4_resnet18_epoch_$EPOCH.tlt \\')
-m $USER_EXPERIMENT_DIR/experiment_dir_unpruned/weights/resnet18_peoplenet.tlt -k $KEY
# ## 9. Visualize inferences <a class="anchor" id="head-9"></a>
# In this section, we run the `infer` tool to generate inferences on the trained models and visualize the results.
# In[28]:
# Copy some test images
get_ipython().system('mkdir -p $LOCAL_DATA_DIR/test_samples')
get_ipython().system('cp $LOCAL_DATA_DIR/testing/image_2/00000* $LOCAL_DATA_DIR/test_samples/')
# In[21]:
# Running inference for detection on n images
get_ipython().system('tao yolo_v4 inference -i $DATA_DOWNLOAD_DIR/test_samples -o $USER_EXPERIMENT_DIR/yolo_infer_images -e $SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt #-m $USER_EXPERIMENT_DIR/experiment_dir_retrain/weights/yolov4_resnet18_epoch_$EPOCH.tlt \\')
-m $USER_EXPERIMENT_DIR/experiment_dir_unpruned/weights/resnet18_peoplenet.tlt -l $USER_EXPERIMENT_DIR/yolo_infer_labels -k $KEY
# The `inference` tool produces two outputs.
# 1. Overlain images in `$LOCAL_EXPERIMENT_DIR/yolo_infer_images`
# 2. Frame by frame bbox labels in kitti format located in `$LOCAL_EXPERIMENT_DIR/yolo_infer_labels`
# In[14]:
# Simple grid visualizer
get_ipython().system('pip3 install matplotlib==3.3.3')
import matplotlib.pyplot as plt
import os
from math import ceil
valid_image_ext = ['.jpg', '.png', '.jpeg', '.ppm']
def visualize_images(image_dir, num_cols=4, num_images=10):
output_path = os.path.join(os.environ['LOCAL_EXPERIMENT_DIR'], image_dir)
num_rows = int(ceil(float(num_images) / float(num_cols)))
f, axarr = plt.subplots(num_rows, num_cols, figsize=[80,30])
f.tight_layout()
a = [os.path.join(output_path, image) for image in os.listdir(output_path)
if os.path.splitext(image)[1].lower() in valid_image_ext]
for idx, img_path in enumerate(a[:num_images]):
col_id = idx % num_cols
row_id = idx // num_cols
img = plt.imread(img_path)
axarr[row_id, col_id].imshow(img)
# In[15]:
# Visualizing the sample images.
OUTPUT_PATH = 'yolo_infer_images' # relative path from $USER_EXPERIMENT_DIR.
COLS = 3 # number of columns in the visualizer grid.
IMAGES = 9 # number of images to visualize.
visualize_images(OUTPUT_PATH, num_cols=COLS, num_images=IMAGES)
# ## 10. Model Export <a class="anchor" id="head-10"></a>
# If you trained a non-QAT model, you may export in FP32, FP16 or INT8 mode using the code block below. For INT8, you need to provide calibration image directory.
# In[17]:
# tao <task> export will fail if .etlt already exists. So we clear the export folder before tao <task> export
get_ipython().system('rm -rf $LOCAL_EXPERIMENT_DIR/export')
get_ipython().system('mkdir -p $LOCAL_EXPERIMENT_DIR/export')
# Export in FP32 mode. Change --data_type to fp16 for FP16 mode
# !tao yolo_v4 export -m $USER_EXPERIMENT_DIR/experiment_dir_retrain/weights/yolov4_resnet18_epoch_$EPOCH.tlt \
# -k $KEY \
# -o $USER_EXPERIMENT_DIR/export/yolov4_resnet18_epoch_$EPOCH.etlt \
# -e $SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt \
# --batch_size 16 \
# --data_type fp32 \
# --gen_ds_config
# Uncomment to export in INT8 mode (generate calibration cache file).
get_ipython().system('tao yolo_v4 export -m $USER_EXPERIMENT_DIR/experiment_dir_unpruned/weights/yolov4_resnet18_epoch_$EPOCH.tlt -o $USER_EXPERIMENT_DIR/export/yolov4_resnet18_epoch_$EPOCH.etlt -e $SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt -k $KEY -v /var/run/docker.sock:/var/run/docker.sock --cal_image_dir $DATA_DOWNLOAD_DIR/testing/image_2 --data_type int8 --batch_size 16 --batches 10 --cal_cache_file $USER_EXPERIMENT_DIR/export/cal.bin --cal_data_file $USER_EXPERIMENT_DIR/export/cal.tensorfile --gen_ds_config')
# In[6]:
get_ipython().system('rm -rf ./export')
get_ipython().system('mkdir -p ./export')
get_ipython().system('tao yolo_v4 export -m ./yolov4_resnet18_epoch_$EPOCH.tlt -o ./yolov4_resnet18_epoch_$EPOCH.etlt -e ./yolo_v4_retrain_resnet18_kitti.txt -k $KEY --cal_./testing/image_2 --data_type int8 --batch_size 16 --batches 10 --cal_cache_file./export/cal.bin --cal_data_file ./export/cal.tensorfile --gen_ds_config')
# `Note:` In this example, for ease of execution we restrict the number of calibrating batches to 10. TAO Toolkit recommends the use of at least 10% of the training dataset for int8 calibration.
#
# If you train a QAT model, you may only export in INT8 mode using following code block. This generates an etlt file and the corresponding calibration cache. You can throw away the calibration cache and just use the etlt file in tao-converter or DeepStream for FP32 or FP16 mode. But please note this gives sub-optimal results. If you want to deploy in FP32 or FP16, you should disable QAT in training.
# In[ ]:
# Uncomment to export QAT model in INT8 mode (generate calibration cache file).
# !rm -rf $LOCAL_EXPERIMENT_DIR/export
# !mkdir -p $LOCAL_EXPERIMENT_DIR/export
# !tao yolo_v4 export -m $USER_EXPERIMENT_DIR/experiment_dir_retrain/weights/yolov4_resnet18_epoch_$EPOCH.tlt \
# -o $USER_EXPERIMENT_DIR/export/yolov4_resnet18_epoch_$EPOCH.etlt \
# -e $SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt \
# -k $KEY \
# --data_type int8 \
# --cal_cache_file $USER_EXPERIMENT_DIR/export/cal.bin
# In[15]:
print('Exported model:')
print('------------')
get_ipython().system('ls -lh $LOCAL_EXPERIMENT_DIR/export')
# Verify engine generation using the `tao-converter` utility included with the docker.
#
# The `tao-converter` produces optimized tensorrt engines for the platform that it resides on. Therefore, to get maximum performance, please instantiate this docker and execute the `tao-converter` command, with the exported `.etlt` file and calibration cache (for int8 mode) on your target device. The tao-converter utility included in this docker only works for x86 devices, with discrete NVIDIA GPU's.
#
# For the jetson devices, please download the tao-converter for jetson from the dev zone link [here](https://developer.nvidia.com/tao-converter).
#
# The -p argument in following command is the optimization profile. This should be in format `<input_node>,<min_shape>,<opimal_shape>,<max_shape>`. In YOLOv3, the three shapes should only have differences at the batch dimension
# In[16]:
# Convert to TensorRT engine (FP32)
# !tao converter -k $KEY \
# -p Input,1x3x384x1248,8x3x384x1248,16x3x384x1248 \
# -e $USER_EXPERIMENT_DIR/export/trt.engine \
# -t fp32 \
# $USER_EXPERIMENT_DIR/export/yolov4_resnet18_epoch_$EPOCH.etlt
# Convert to TensorRT engine (FP16)
# !tao converter -k $KEY \
# -p Input,1x3x384x1248,8x3x384x1248,16x3x384x1248 \
# -e $USER_EXPERIMENT_DIR/export/trt.engine \
# -t fp16 \
# $USER_EXPERIMENT_DIR/export/yolov4_resnet18_epoch_$EPOCH.etlt
# Convert to TensorRT engine (INT8).
get_ipython().system('tao converter -k $KEY -p Input,1x3x544x960,8x3x544x960,16x3x544x960 -c $USER_EXPERIMENT_DIR/export/cal.bin -e $USER_EXPERIMENT_DIR/export/trt.engine -b 8 -t int8 $USER_EXPERIMENT_DIR/export/yolov4_resnet18_epoch_$EPOCH.etlt')
# In[ ]:
print('Exported engine:')
print('------------')
get_ipython().system('ls -lh $LOCAL_EXPERIMENT_DIR/export/trt.engine')
# ## 11. Verify the deployed model <a class="anchor" id="head-11"></a>
# Verify the converted engine by visualizing TensorRT inferences.
#
# In[ ]:
# Infer using TensorRT engine
get_ipython().system('tao yolo_v4 inference -m $USER_EXPERIMENT_DIR/export/trt.engine -e $SPECS_DIR/yolo_v4_retrain_resnet18_kitti.txt -i $DATA_DOWNLOAD_DIR/test_samples -o $USER_EXPERIMENT_DIR/yolo_infer_images -t 0.6')
# In[ ]:
# Visualizing the sample images.
OUTPUT_PATH = 'yolo_infer_images' # relative path from $USER_EXPERIMENT_DIR.
COLS = 3 # number of columns in the visualizer grid.
IMAGES = 9 # number of images to visualize.
visualize_images(OUTPUT_PATH, num_cols=COLS, num_images=IMAGES)