PyTorch Mobile Runtime for iOS

PyTorch · Intermediate ·📰 AI News & Updates ·5y ago

Key Takeaways

The video demonstrates how to use PyTorch Mobile Runtime for iOS to deploy machine learning models on edge devices, with a focus on model optimization, layer fusion, and just-in-time compilation. It covers the end-to-end workflow from Python to deployment on iOS using tools like Xcode, CocoaPods, and PyTorch.

Full Transcript

hello and welcome to the pike torch summer hack 2020 I'm Brad Heights and I'm a partner engineer with the PI torch team if you're not sure what a partner engineer is it means that my day job is making sure that developers like yourselves get the most they can out of PI torch and related products in this video I'm going to give you a walk-through of setting up the PI torch mobile runtime in an iOS project to follow along you'll need Xcode version 11 or higher on Mac OS you'll need to have cocoa pods installed and if you want to deploy to a device you'll need an Apple Developer account in an iOS device running at least iOS 12 you should also be using PI torch 1.5 or higher PI torch offers native runtimes for iOS and Android allowing you to bring your machine learning app to mobile devices including the library in your project as a one-liner but there is more to do getting set up for best performance on mobile in this video I'm going to show you how to set up your project to include the PI torch runtime how to export your model to torch script PI torches optimized model representation how to further optimize your model for best performance how to add your model to your Xcode project and how to call the model from your code let's dive in for this demonstration we're going to build an image classifier into an iOS app first we'll create the project I'm going to create a single page app with UIKit UI now close Xcode and switch to your command line so here's the pod file we're going to attach to this project you can see it specifies iOS 12 and PI torch 1.5 so now pod install that and we'll open the workspace now instead of the project it's important when you're using cocoapods and then we'll go ahead and run the project just to make sure all as well and there's the sim nothing there yet no surprise we'll stop that the next thing we'll need is a UI I'm going to have one image of you one button and one textview in my app the image view and text view will have outlets in the view controller and the button will have an attached action rather than making you watch me set up a bunch of storyboard constraints we'll do a little movie magic Yola now I'll show you how to set up and optimize your model for use with pi towards mobile so first thing first we'll import PI torch and torch vision and this is a good place to point out that you should be using PI torch 1.5 or higher for this example in the next cell we're going to create a PI torch model object now for the actual app I have a pre trained optimized model ready to go but for the optimization process I wanted to show you this on a custom model so that you could duplicate the process with your own models this model happens to contain some very common layer types A to D convolutional layer a to D batch norming layer and a rectified linear unit for activation and the forward function just strings those three operations together so now that we have our model how do we optimize it first thing let's get an instance of the model in my get model helper you'll notice that besides just instantiating the model I call MD Val so eval turns off things in the model that you don't want on during inference time training only layers like drop out automated gradient tracking all this training related stuff meets up CPU cycles and we don't need it for inference so we're gonna make sure the model is in eval mode the second thing we're going to do is some layer fusion fusing layers means taking multiple operations and combining them together into a single operation this improves performance in memory footprint now with diffuse modules method that I'm going to show you there are only certain combinations of layers that you can fuse together I'm going to refer you to the documentation for the latest information on that but here we're going to try to use together convolution bachelor-man Darrell you once modules are fused the next one we're going to do is quantize the model PI torch tensors default to using 32-bit floating-point numbers as their underlying type when we quantize we're going to change that to an 8-bit integer this will perform faster and reduce the models footprint both on disk and in memory the I don't think we're going to do is save the model as torch script torch script is an optimized format for your model including both your computation graph and you're learning weights it's meant to be consumed by the PI torch in just-in-time compiler or JIT which is included with the PI torch mobile runtime so once it's exported will save now there are subtleties to layer fusion into quantizing your model that you'll want to be familiar with when you're optimizing your own model for use with PI torch mobile all of this is covered in the PI torch documentation for quantization which I encourage you to check out now I'm going to pull in the resources for this project they include the model so in my case that's an optimized version of mobile nephew trained on the thousand categories of the image net data set we also have a text file that contains the human readable labels of those thousand categories and finally we have an image file for our classifier to work on there should be links to a sample project that includes these resources wherever you got this video now we can flip back to Xcode and I'll add the three files to my project now how do we call pi torch from our code because pi torch is implemented under the hood in C++ with Lib torch we need a wrapper for that C++ library so we're going to go ahead and create a new group in the project we're going to flip back to the command line and I'm going to add these two files torch module dot H and torch module dot mmm again these should be available wherever you've got this video and then Xcode is going to offer to make a bridging header for me and I'm going to let it do that so you can see here in the header there are only two methods that are really interesting one which initializes the PI torch runtime with a model which is store as a file and it's our model of resource in our project and the second one that does the actual prediction when you pass it in a buffer full of image information and you can see the C++ bridge code and torch module mmm and now add my image resource to my image view so we can see what it is we're classifying and speaking of images I'm going to add one more source file and this is going to be in addition to the UI image framework class that allows us to resize it more easily the particular model I'm using because it was trained on image net expects three color images size 224 by 224 pixels so this helper role helped me achieve that we have some stuff to fill in the my viewcontroller first I'm going to add a lazy loaded instance of our module our model you can see I'm loading it from the file and initializing the torch module object with it also I have lazily loaded array of strings the labels those thousand human readable labels for the categories that the model recognizes I'm also setting that up here now you notice we've got an error on the torch module type it suppose we're to go back to the bridging header and make sure we include torch module page if for whatever reason your project isn't recognizing the type go check the bridging header make sure you've included a header for torch module dot H now we're going to fill in the body of our inference buttons action and you can see here we're going to load the image we're going to resize it to do 224 by 224 we're also going to normalize it so just as with the size of the image our model expects the image to be normalized in a certain way and we have a method to do that and finally we take our image buffer and we pass it to module dot predict after that we retrieve the label or the predicted category and display it in our text view so now at last let's run our app yeah you'll see it starts up and there's our kitten we press the infer button and be aware that even if you run this on a laptop it may take a minute to run the first time but there we are our model thinks that this cat is a cat and that is how you get a pi torch model onto iOS thanks for participating in the hackathon and thanks for watching this video

Original Description

Running ML on edge devices is growing in importance as applications continue to demand lower latency. It is also a foundational element for privacy-preserving techniques such as federated learning. As of PyTorch 1.3, PyTorch supports an end-to-end workflow from Python to deployment on iOS. In this talk, PyTorch Partner Engineer Brad Heintz walks through steps for setting up the PyTorch Runtime for iOS projects. For more information on PyTorch Mobile, visit: https://pytorch.org/mobile. Check out the updated video utilizing PyTorch 1.7 releases here: https://youtu.be/amTepUIR93k
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from PyTorch · PyTorch · 56 of 60

1 What is PyTorch?
What is PyTorch?
PyTorch
2 PyTorch Tutorial: A Quick Preview
PyTorch Tutorial: A Quick Preview
PyTorch
3 PyTorch Summer Hackathon 2019
PyTorch Summer Hackathon 2019
PyTorch
4 Tips and Tricks on Hacking with PyTorch: A Quick Tutorial by Brad Heintz
Tips and Tricks on Hacking with PyTorch: A Quick Tutorial by Brad Heintz
PyTorch
5 PyTorch 1.2 and PyTorch Hub: A Quick Introduction by Soumith Chintala and Ailing Zhang
PyTorch 1.2 and PyTorch Hub: A Quick Introduction by Soumith Chintala and Ailing Zhang
PyTorch
6 Torchtext 0.4 with Supervised Learning Datasets: A Quick Introduction by George Zhang
Torchtext 0.4 with Supervised Learning Datasets: A Quick Introduction by George Zhang
PyTorch
7 Torchaudio 0.3 with Kaldi Compatibility, New Transforms: A Quick Introduction by Jason Lian
Torchaudio 0.3 with Kaldi Compatibility, New Transforms: A Quick Introduction by Jason Lian
PyTorch
8 Torchvision 0.4 with Support for Video: A Quick Introduction by Francisco Massa
Torchvision 0.4 with Support for Video: A Quick Introduction by Francisco Massa
PyTorch
9 Introduction to Machine Learning for Developers at F8 2019
Introduction to Machine Learning for Developers at F8 2019
PyTorch
10 Powered by PyTorch at F8 2019
Powered by PyTorch at F8 2019
PyTorch
11 Developing and Scaling AI Experiences at Facebook with PyTorch at F8 2019
Developing and Scaling AI Experiences at Facebook with PyTorch at F8 2019
PyTorch
12 New Approaches to Image and Video Reconstruction Using Deep Learning at Facebook at F8 2019
New Approaches to Image and Video Reconstruction Using Deep Learning at Facebook at F8 2019
PyTorch
13 PyTorch Developer Conference 2018: Recap
PyTorch Developer Conference 2018: Recap
PyTorch
14 PyTorch Developer Conference 2018: Keynote & Deep Dive
PyTorch Developer Conference 2018: Keynote & Deep Dive
PyTorch
15 PyTorch Developer Conference 2018: Production & Research Sessions
PyTorch Developer Conference 2018: Production & Research Sessions
PyTorch
16 PyTorch Developer Conference 2018: Cloud & Academia Sessions
PyTorch Developer Conference 2018: Cloud & Academia Sessions
PyTorch
17 PyTorch Developer Conference 2018: Enterprise, Education, & Future of AI Panel
PyTorch Developer Conference 2018: Enterprise, Education, & Future of AI Panel
PyTorch
18 PyTorch Developer Conference 2019 | Full Livestream
PyTorch Developer Conference 2019 | Full Livestream
PyTorch
19 PyTorch Developer Conference 2019: Recap
PyTorch Developer Conference 2019: Recap
PyTorch
20 PyTorch Developer Conference Keynote - Mike Schroepfer
PyTorch Developer Conference Keynote - Mike Schroepfer
PyTorch
21 What’s new in PyTorch 1.3 - Lin Qiao
What’s new in PyTorch 1.3 - Lin Qiao
PyTorch
22 PyTorch Front-End Features: Named Tensors and Type Promotion - Gregory Chanan
PyTorch Front-End Features: Named Tensors and Type Promotion - Gregory Chanan
PyTorch
23 Research to Production: PyTorch JIT/TorchScript Updates - Michael Suo
Research to Production: PyTorch JIT/TorchScript Updates - Michael Suo
PyTorch
24 Quantization - Dmytro Dzhulgakov
Quantization - Dmytro Dzhulgakov
PyTorch
25 PyTorch ONNX Export Support - Lara Haidar, Microsoft
PyTorch ONNX Export Support - Lara Haidar, Microsoft
PyTorch
26 Apex -  Michael Carilli, NVIDIA
Apex - Michael Carilli, NVIDIA
PyTorch
27 Dataloader Design for PyTorch - Tongzhou Wang, MIT
Dataloader Design for PyTorch - Tongzhou Wang, MIT
PyTorch
28 Linear Algebra in PyTorch - Vishwak Srinivasan, CMU
Linear Algebra in PyTorch - Vishwak Srinivasan, CMU
PyTorch
29 PyTorch Mobile - David Reiss
PyTorch Mobile - David Reiss
PyTorch
30 Model Interpretability with Captum - Narine Kokhilkyan
Model Interpretability with Captum - Narine Kokhilkyan
PyTorch
31 Detectron2 - Next Gen Object Detection Library - Yuxin Wu
Detectron2 - Next Gen Object Detection Library - Yuxin Wu
PyTorch
32 Speech Extensions to Fairseq - Dmytro Okhonko
Speech Extensions to Fairseq - Dmytro Okhonko
PyTorch
33 PyTorch on Google Cloud TPUs - Google, Salesforce, Facebook
PyTorch on Google Cloud TPUs - Google, Salesforce, Facebook
PyTorch
34 PyTorch Summer Hackathon Winners - Joe Spisak, Sebastien Arnold, Tristan Deleu
PyTorch Summer Hackathon Winners - Joe Spisak, Sebastien Arnold, Tristan Deleu
PyTorch
35 PyTorch in Robotics - Yisong Yue, Caltech
PyTorch in Robotics - Yisong Yue, Caltech
PyTorch
36 StanfordNLP - Yuhao Zhang, Stanford
StanfordNLP - Yuhao Zhang, Stanford
PyTorch
37 Sotabench for Reproducible Research - Robert Stojnic, Papers with Code
Sotabench for Reproducible Research - Robert Stojnic, Papers with Code
PyTorch
38 Collaborative Natural Language Inference - Sasha Rush, Cornell
Collaborative Natural Language Inference - Sasha Rush, Cornell
PyTorch
39 Privacy Preserving AI - Andrew Trask, OpenMined
Privacy Preserving AI - Andrew Trask, OpenMined
PyTorch
40 CrypTen - Laurens van der Maaten
CrypTen - Laurens van der Maaten
PyTorch
41 PyTorch at Uber - Sidney Zhang, Uber
PyTorch at Uber - Sidney Zhang, Uber
PyTorch
42 PyTorch at Tesla - Andrej Karpathy, Tesla
PyTorch at Tesla - Andrej Karpathy, Tesla
PyTorch
43 PyTorch at Microsoft - Saurabh Tiwary, Microsoft
PyTorch at Microsoft - Saurabh Tiwary, Microsoft
PyTorch
44 PyTorch at Dolby Labs - Vivek Kumar, Dolby Labs
PyTorch at Dolby Labs - Vivek Kumar, Dolby Labs
PyTorch
45 PyTorch Developer Conference 2019 - Panel Discussion
PyTorch Developer Conference 2019 - Panel Discussion
PyTorch
46 Using deep learning and PyTorch to power next gen aircraft at Caltech
Using deep learning and PyTorch to power next gen aircraft at Caltech
PyTorch
47 Named Tensors, Model Quantization, and the Latest PyTorch Features - Part 1
Named Tensors, Model Quantization, and the Latest PyTorch Features - Part 1
PyTorch
48 TorchScript and PyTorch JIT | Deep Dive
TorchScript and PyTorch JIT | Deep Dive
PyTorch
49 Announcing the PyTorch Global Summer Hackathon 2020
Announcing the PyTorch Global Summer Hackathon 2020
PyTorch
50 Opening Up the Black Box: Model Understanding with Captum and PyTorch
Opening Up the Black Box: Model Understanding with Captum and PyTorch
PyTorch
51 PyTorch Mobile Runtime for Android
PyTorch Mobile Runtime for Android
PyTorch
52 Torchvision in 5 minutes
Torchvision in 5 minutes
PyTorch
53 3D Deep Learning with PyTorch3D
3D Deep Learning with PyTorch3D
PyTorch
54 What is Torchtext?
What is Torchtext?
PyTorch
55 TorchAudio: A Quick Intro
TorchAudio: A Quick Intro
PyTorch
PyTorch Mobile Runtime for iOS
PyTorch Mobile Runtime for iOS
PyTorch
57 PySlowFast: Deep learning with Video
PySlowFast: Deep learning with Video
PyTorch
58 PyTorch Pruning | How it's Made by Michela Paganini
PyTorch Pruning | How it's Made by Michela Paganini
PyTorch
59 Measuring Fairness in Machine Learning Systems
Measuring Fairness in Machine Learning Systems
PyTorch
60 PyTorch for Hackathons
PyTorch for Hackathons
PyTorch

This video teaches how to deploy machine learning models on iOS devices using PyTorch Mobile Runtime, covering model optimization, layer fusion, and just-in-time compilation. It provides a hands-on guide to creating a mobile app that classifies images using a PyTorch model. The video is essential for developers who want to build AI-powered mobile apps with low latency and high performance.

Key Takeaways
  1. Create a single-page app with UIKit UI
  2. Attach CocoaPods to the project
  3. Install the pod and open the workspace
  4. Run the project to ensure everything is working
  5. Create a UI with an image view, button, and text view
  6. Fuse convolutional modules together
  7. Quantize the model to 8-bit integers
  8. Save the model as a torch script
  9. Create a C++ wrapper for LibTorch
  10. Resize and normalize images to 224x224 pixels
💡 Using PyTorch Mobile Runtime for iOS enables developers to deploy optimized machine learning models on edge devices, reducing latency and improving performance.

Related Reads

📰
Artificial Intelligence and Engels' Pause
Learn how Artificial Intelligence relates to Engels' Pause and its implications on productivity and technological advancements
Hacker News
📰
Your Job Isn’t Being Replaced by AI. It’s Being Replaced by Someone Who Uses AI Better Than You.
Upskill to use AI effectively to stay ahead in your job, as those who leverage AI better will replace those who don't
Medium · AI
📰
Will AI Replace Jobs? Here’s What Most People Get Wrong
Learn the common misconceptions about AI replacing jobs and why it matters for your career
Medium · AI
📰
Jersey Mike’s IPO illustrates how bad the AI hype has become
Jersey Mike's IPO mentions AI, highlighting the overhyped use of AI in business, and why it matters to understand the genuine applications of AI
TechCrunch AI
Up next
Daily Current Affairs 3 July 2026 | National & International News MCQ | Bank, SSC, Railway
Adda247 Bankers
Watch →