Skip to content

Conversation

d4l3k
Copy link
Member

@d4l3k d4l3k commented Feb 8, 2022

This adds a tutorial on how to use PyTorch for real time inference on a Raspberry Pi 4. This requires a number of specialized steps to get it all setup correctly and running permanently so seems worthwhile to add to the tutorials.

This requires a 64-bit operating system and RPi OS 64-bit just came out of beta so seems like a good time to document this. https://www.raspberrypi.com/news/raspberry-pi-os-64-bit/

There's a number of existing tutorials but they're way more complicated than they need to be since pip install torch works out of the box on 64-bit images. Much better to just pip install than install from Google Drive which the two top Google searches recommend.

This also seems to be the fastest known way of running mobilenetv2 on a Raspberry Pi 4 (without accelerators) that I've seen by an order of magnitude. At ~30-32fps that's ~33ms per inference including camera capture and processing. A quick Google search shows:

The images are currently hosted on githubusercontent, let me know if I should place them in the repo instead.

Test plan:

I've tested all of these steps from scratch using the official Raspberry Pi OS (64-bit) images.

Screenshots:

2022-02-07-184540_1156x448_scrot

Screenshot 2022-02-07 at 18-54-37 Real Time Inference on Raspberry Pi 4 (30 fps ) — PyTorch Tutorials 1 10 2+cu102 document

@netlify
Copy link

netlify bot commented Feb 8, 2022

✔️ Deploy Preview for pytorch-tutorials-preview ready!

🔨 Explore the source changes: 8a57a77

🔍 Inspect the deploy log: https://app.netlify.com/sites/pytorch-tutorials-preview/deploys/6201ddf34b45690008843867

😎 Browse the preview: https://deploy-preview-1821--pytorch-tutorials-preview.netlify.app

@msaroufim msaroufim self-requested a review February 8, 2022 03:20
Copy link
Member

@msaroufim msaroufim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall really enjoyed it. It's clear. I think the ideal audience smart kids so I'd explain a bit more detail in some of the trickier code snippets. Otherwise looks all good.

.. code:: shell

$ python3 -c "import torch; print(torch.__version__)"
1.10.0+cpu
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove this line so you don't have to keep updating tutorial with new version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

# convert opencv output from BGR to RGB
image = image[:, :, [2, 1, 0]]

NOTE: You can get even more performance by training the model directly with OpenCV's BGR data format to remove the conversion step.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can link to a doc here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have any docs here, this would be on the training dataset.


preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe explain briefly what this is. This tutorial would be great for someone even if they don't know what pytorch is. Think smart high school students

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(image)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can also briefly elaborate here - can add shape annotations on top of input_tensor and input_batch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

MobileNetV2: Quantization and JIT
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For optimal performance we want a model that's quantized and fused. Quantized
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kinda cool that raspberry pi supports int8, definitely something I was surprised to learn

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thankfully it doesn't require specialized hardware support. Most CPUs support 8bit operations since it's cheap to add hardware wise

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qnnpack is designed for mobile arm devices so it works surprisingly well on an Arm64 Raspberry Pi

from torchvision import models
net = models.quantization.mobilenet_v2(pretrained=True, quantize=True)

We then want to jit the model to reduce Python overhead and fuse any ops. Jit gives us ~30fps instead of ~20fps without it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda interesting that vanilla pytorch worked fine. curious if you tried using pytorch live

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think PyTorch Live is only for iOS/Android https://pytorch.org/live/docs/tutorials/get-started/ -- RPi is just aarch64 Linux so standard PyTorch works out of the box

most of the work to fuse and quantize has already been done for you so you can
directly deploy with good performance on a Raspberry Pi.

See more:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a gif of the model working, you can even showcase what it's seeing - will also be nice to share in tweet threads

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, definitely planning on it. no more mysterious gdrive python packages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants