Transfer Learning
Transfer learning is a useful way to quickly retrain a model on new data without having to retrain the entire network. Instead, part of the initial weights are frozen in place, and the rest of the weights are used to compute loss and are updated by the optimizer.
Pretrained Model
YOLOv8
YOLO (You Only Look Once) is a real-time object detection algorithm developed by Joseph Redmon and Ali Farhadi in 2015. It is a single-stage object detector that uses a convolutional neural network (CNN) to predict the bounding boxes and class probabilities of objects in input images.
YOLOv8 is a state-of-the-art deep learning model designed for real-time object detection in computer vision applications. With its advanced architecture and cutting-edge algorithms, YOLOv8 has revolutionized the field of object detection, enabling accurate and efficient detection of objects in real-time scenarios.
Tools
-
Homebrew 4.3.7
-
Python 3.9.6
-
Pytorch 2.3.1
-
coremltools 7.2
-
Ultralytics yolo cli
-
LabelImg 1.8.6
Dependencies
-
torch 2.3.1
-
torchaudio 2.3.1
-
torchvision 0.18.1
-
numpy 1.26.4
-
ultralytics 8.2.48
-
segmentation_models_pytorch 0.3.0
-
six 1.16.0
Hardware
MacBook Pro M3 pro
Steps
-
Install brew
-
Install Python 3.9
-
update environment variable and PATH
-
create python virtual environment - venv
-
activate venv
-
Update pip
-
Install dependencies
-
prepare dataset
-
prepare image annotation
-
upload the images and labels to "train" and "ver" folders
-
create yolo config yaml file (define train, verification, number of class, class names, pretrained model folders)
-
train a new model based on Yolov8 pretrained model
-
convert pytorch trained model to coreML package
-
Install coreML package in Xcode
-
Preview the model
-
The model is ready to use in iOS APP
Command
1. Install HomeBrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2. Config path and shell script
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> ~/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"
3. Install Homebrew python 3.9
brew install python@3.9
4. Create python virtual environment
python3 -m venv {Path}/venv
5. Activate python virtual environment
source {Path}/venv/bin/activate
6. Update pip
python3.9 -m pip install --upgrade pip
7. Install dependencies
pip install -lv torchvision==0.18.1
pip install -lv torch==2.3.1
pip install -lv torchaudio==2.3.1
pip install -lv segmentation_models_pytorch==0.3.0
pip install -lv six
pip install -lv numpy=1.26.4
8. Install coremltools
pip install -U coremltools
9. Install ultralytics
pip install ultralytics
10. Install labelImg
pip install labelImg
launch labelImg
labelImg
Set the "Open Dir" - images folder and "Change Save Dir" - labels folder, format set "YOLO"
11. Download yolov8m pretrained model
https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8m.pt
12. Train the model
yolo task=detect mode=train epochs=100 data=config.yaml model=yolov8.pt imgsz=640 device=mps
13. Convert pytorch pt file to coreml package
from ultralytics import YOLO
model=YOLO(yolov8m_retrained.pt)
model.export(format='coreml', nms=True)