git clone https://github.com/iamjeerge/ios-object-detection-yolov5.git
cd ios-object-detection-yolov5
The Python script export.py
in the models
folder of the YOLOv5 repo is used to generate a TorchScript-formatted YOLOv5 model named yolov5s.torchscript.ptl
for mobile apps.
Open a Mac/Linux/Windows Terminal, run the following commands:
git clone https://github.com/ultralytics/yolov5
cd yolov5
pip install -r requirements.txt wanb
Note the steps below have been tested with the commit cd35a009ba964331abccd30f6fa0614224105d39
and if there's any issue with running the script or using the model, try git reset --hard cd35a009ba964331abccd30f6fa0614224105d39
.
Edit export.py
to make the following two changes:
-
After
f = file.with_suffix('.torchscript.pt')
, add a linefl = file.with_suffix('.torchscript.ptl')
-
After
(optimize_for_mobile(ts) if optimize else ts).save(f)
, add(optimize_for_mobile(ts) if optimize else ts)._save_for_lite_interpreter(str(fl))
Finally, run the script below to generate the optimized TorchScript Lite Interpreter model and copy the generated model file yolov5s.torchscript.ptl
to the ios-demo-app/ObjectDetection/ObjectDetection
folder (the original full JIT model yolov5s.torchscript.pt
was also generated for comparison):
python export.py --weights yolov5s.pt --include torchscript
Note that small sized version of the YOLOv5 model, which runs faster but with less accuracy, is generated by default when running the export.py
. You can also change the value of the weights
parameter in the export.py
to generate the medium, large, and extra large version of the model.
Run the commands below:
pod install
open ObjectDetection.xcworkspace/
Select an iOS simulator or device on Xcode to run the app. You can go through the included example test images to see the detection results.