- XCode 11.0 or above
- iOS 12.0 or above
HelloWorld is a simple image classification application that demonstrates how to use PyTorch C++ libraries on iOS. The code is written in Swift and uses Objective-C as a bridge.
The model we are going to use is MobileNet v2, a pre-trained image classification model that has been packaged in TorchVision. To install it, run the command below.
We highly recommend following the Pytorch Github page to set up the Python development environment on your local machine.
pip install torch torchvision
Once we have TorchVision installed successfully, navigate to the HelloWorld folder and run trace_model.py
to generate our model. The script contains the code of tracing and saving a torchscript model that can be run on mobile devices.
python trace_model.py
If everything works well, model.pt
should be generated and saved in the HelloWorld/HelloWorld/model
.
The PyTorch C++ library is available in Cocoapods, to integrate it to our project, we can run
pod install
Now open the HelloWorld.xcworkspace
in XCode, select an iOS simulator and launch it (cmd + R). If everything works well, we should see a wolf picture on the simulator screen along with the prediction results.