Train image classification models

Train image classification models1.Preparation before training2.Start retraining the cat and dog dataset3.Execute training commands4.Interpretation of information at the beginning of training5.Explanation of model accuracy6.After training, convert the model into an ONNX model file7.Processing Images with TensorRT8. Results of 1 training cycle, 35 training cycles, and 100 training cycles9.appendix

1.Preparation before training

Before starting this tutorial, Python must have the torchCPU+GPU version installed, and the image of Yahboom is already installed. If not installed, please install it yourself. You need to go online scientifically, and you can also read the Torch installation tutorial in this series of tutorials.

2.Start retraining the cat and dog dataset

If the image cannot be downloaded successfully due to network issues, you can find cat in the "model/cat_dog" in the attachment_ Transfer dog.tar.gz to NX, yahboom can skip this step

3.Execute training commands

Note: If the training is "terminated" due to insufficient memory, please try to exchange virtual memory and disable the desktop GUI

Swap virtual memory (see link):https://github.com/dusty-nv/jetson-inference/blob/master/docs/pytorch-transfer-learning.md#mounting-swap

4.Interpretation of information at the beginning of training

5.Explanation of model accuracy

On this dataset consisting of 5000 images, training ResNet-18 on Jetson orin NX takes approximately 2 minutes per period, or approximately 55 minutes to train the model to achieve 35 periods and 80% classification accuracy. The following figure is used to analyze the relationship between the training progress of epochs and the accuracy of the model: image-2023042800005 In approximately the 30th period, the ResNet-18 model achieved an accuracy of 80%, and in the 65th period, it converged to an accuracy of 82.5%. With additional training time, you can further improve accuracy or try more complex models by increasing the size of the dataset. By default, the training script is set to run for 35 periods, but if you don't want to wait that long to test your model, you can exit the training early and continue to the next step (you can choose to restart the training later from where you stopped)Restore with the parameter '--resume' Our * * Appendix/Model File/cat_ Dog * * provides models for training 35 cycles and 100 cycles. If you don't want to train, you can transfer the model to NX yourselfyour path/jetson-inference/python/training/classification/models/cat_dogJust decompress it, but there is no need to manipulate the image of yahboom, as there are already 100 trained models in it

6.After training, convert the model into an ONNX model file

The model file of onnx is convenient for TensorRT to process

This will create a file named resnet18.onnx jetson-inference/python/training/classification/models/cat_dog/

7.Processing Images with TensorRT

Taking the command for yahboom mirroring as an example

The above command has been executed and the results can be observed in the path below. This command is used to process an image path:/home/jetson/jetson-inference/python/training/classification

If you want to test all the images, you can enter the command

The output results are in the /home/jetson/jetson-inference/python/training/classification/data/cat_dog/test_output_dog

If you want to turn on the camera for detection

For other video stream file opening methods, please refer to:https://github.com/dusty-nv/jetson-inference/blob/master/docs/aux-streaming.md

8. Results of 1 training cycle, 35 training cycles, and 100 training cycles

9.appendix

If you want to train yourself on different types of images You can refer to the following link: https://github.com/dusty-nv/jetson-inference/blob/master/docs/pytorch-collect.md