Link to this sectionPython 사용법#
Ultralytics YOLO Python 사용법 문서에 오신 것을 환영합니다! 이 가이드는 귀하의 Python 프로젝트에 Ultralytics YOLO를 원활하게 통합하여 객체 탐지, 인스턴스 세그멘테이션, 의미론적 세그멘테이션 및 분류를 수행할 수 있도록 설계되었습니다. 여기에서는 사전 학습된 모델을 로드 및 사용하고, 새로운 모델을 학습시키며, 이미지에 대해 예측을 수행하는 방법을 배울 수 있습니다. 사용하기 쉬운 Python 인터페이스는 YOLO를 Python 프로젝트에 통합하려는 모든 분에게 유용한 리소스이며, 고급 객체 탐지 기능을 빠르게 구현할 수 있게 해줍니다. 시작해 보겠습니다!
Watch: Mastering Ultralytics YOLO: Python
예를 들어, 사용자는 단 몇 줄의 코드로 모델을 로드하고, 학습시키고, 검증 세트에서 성능을 평가하며, ONNX 형식으로 내보내기까지 수행할 수 있습니다.
from ultralytics import YOLO
# Create a new YOLO model from scratch
model = YOLO("yolo26n.yaml")
# Load a pretrained YOLO model (recommended for training)
model = YOLO("yolo26n.pt")
# Train the model using the 'coco8.yaml' dataset for 3 epochs
results = model.train(data="coco8.yaml", epochs=3)
# Evaluate the model's performance on the validation set
results = model.val()
# Perform object detection on an image using the model
results = model("https://ultralytics.com/images/bus.jpg")
# Export the model to ONNX format
success = model.export(format="onnx")Link to this section학습(Train)#
Train 모드는 커스텀 데이터셋에서 YOLO 모델을 학습시키는 데 사용됩니다. 이 모드에서 모델은 지정된 데이터셋과 하이퍼파라미터를 사용하여 학습됩니다. 학습 과정에는 이미지 내 객체의 클래스와 위치를 정확하게 예측할 수 있도록 모델의 파라미터를 최적화하는 작업이 포함됩니다.
from ultralytics import YOLO
model = YOLO("yolo26n.pt") # pass any model type
results = model.train(epochs=5)Link to this section검증(Val)#
Val 모드는 학습이 완료된 YOLO 모델을 검증하는 데 사용됩니다. 이 모드에서 모델은 검증 세트에서 평가되어 정확도와 일반화 성능을 측정합니다. 이 모드를 사용하여 모델의 성능을 향상하기 위한 하이퍼파라미터를 조정할 수 있습니다.
from ultralytics import YOLO
# Load a YOLO model
model = YOLO("yolo26n.yaml")
# Train the model
model.train(data="coco8.yaml", epochs=5)
# Validate on training data
model.val()Link to this section추론(Predict)#
Predict 모드는 학습된 YOLO 모델을 사용하여 새로운 이미지나 비디오에 대해 예측을 수행하는 데 사용됩니다. 이 모드에서 모델은 체크포인트 파일로부터 로드되며, 사용자는 이미지나 비디오를 제공하여 추론을 수행할 수 있습니다. 모델은 입력 이미지나 비디오 내 객체의 클래스와 위치를 예측합니다.
import cv2
from PIL import Image
from ultralytics import YOLO
model = YOLO("model.pt")
# accepts all formats - image/dir/Path/URL/video/PIL/ndarray. 0 for webcam
results = model.predict(source="0")
results = model.predict(source="folder", show=True) # Display preds. Accepts all YOLO predict arguments
# from PIL
im1 = Image.open("bus.jpg")
results = model.predict(source=im1, save=True) # save plotted images
# from ndarray
im2 = cv2.imread("bus.jpg")
results = model.predict(source=im2, save=True, save_txt=True) # save predictions as labels
# from list of PIL/ndarray
results = model.predict(source=[im1, im2])Link to this section내보내기(Export)#
Export 모드는 YOLO 모델을 배포에 사용할 수 있는 형식으로 내보내는 데 사용됩니다. 이 모드에서 모델은 다른 소프트웨어 애플리케이션이나 하드웨어 장치에서 사용할 수 있는 형식으로 변환됩니다. 이 모드는 모델을 프로덕션 환경에 배포할 때 유용합니다.
공식 YOLO 모델을 동적 배치 크기 및 이미지 크기로 ONNX로 내보냅니다.
from ultralytics import YOLO
model = YOLO("yolo26n.pt")
model.export(format="onnx", dynamic=True)Link to this section추적(Track)#
Track 모드는 YOLO 모델을 사용하여 실시간으로 객체를 추적하는 데 사용됩니다. 이 모드에서 모델은 체크포인트 파일로부터 로드되며, 사용자는 실시간 객체 추적을 수행하기 위해 라이브 비디오 스트림을 제공할 수 있습니다. 이 모드는 감시 시스템이나 자율주행 자동차와 같은 애플리케이션에 유용합니다.
from ultralytics import YOLO
# Load a model
model = YOLO("yolo26n.pt") # load an official detection model
model = YOLO("yolo26n-seg.pt") # load an official segmentation model
model = YOLO("path/to/best.pt") # load a custom model
# Track with the model
results = model.track(source="https://youtu.be/LNwODJXcvt4", show=True)
results = model.track(source="https://youtu.be/LNwODJXcvt4", show=True, tracker="bytetrack.yaml")Link to this section벤치마크#
Benchmark 모드는 YOLO의 다양한 내보내기 형식에 대한 속도와 정확도를 프로파일링하는 데 사용됩니다. 벤치마크는 내보낸 형식의 크기, mAP50-95 지표(객체 탐지 및 세그멘테이션의 경우) 또는 accuracy_top1 지표(분류의 경우), 그리고 ONNX, OpenVINO, TensorRT 등 다양한 내보내기 형식에 걸쳐 이미지당 밀리초 단위의 추론 시간을 제공합니다. 이 정보는 사용자가 속도와 정확도 요구 사항에 따라 특정 사용 사례에 맞는 최적의 내보내기 형식을 선택하는 데 도움을 줄 수 있습니다.
모든 내보내기 형식에 걸쳐 공식 YOLO 모델을 벤치마킹합니다.
from ultralytics.utils.benchmarks import benchmark
# Benchmark
benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, half=False, device=0)Link to this sectionTrainer 사용하기#
YOLO 모델 클래스는 Trainer 클래스를 위한 고수준 래퍼(wrapper) 역할을 합니다. 각 YOLO 작업에는 BaseTrainer를 상속하는 자체 트레이너가 있습니다. 이러한 아키텍처는 머신 러닝 워크플로우에서 더 큰 유연성과 사용자 정의 기능을 제공합니다.
from ultralytics.models.yolo.detect import DetectionPredictor, DetectionTrainer, DetectionValidator
# trainer
trainer = DetectionTrainer(overrides={})
trainer.train()
trained_model = trainer.best
# Validator
val = DetectionValidator(args=...)
val(model=trained_model)
# predictor
pred = DetectionPredictor(overrides={})
pred(source=SOURCE, model=trained_model)
# resume from last weight
overrides["resume"] = trainer.last
trainer = DetectionTrainer(overrides=overrides)Trainer를 쉽게 사용자 정의하여 커스텀 작업을 지원하거나 연구 및 개발 아이디어를 탐색할 수 있습니다. Ultralytics YOLO의 모듈식 설계는 새로운 컴퓨터 비전 작업을 진행하든 더 나은 성능을 위해 기존 모델을 미세 조정하든, 프레임워크를 귀하의 특정 요구 사항에 맞게 조정할 수 있도록 합니다.
Link to this sectionFAQ#
Link to this section객체 탐지를 위해 Python 프로젝트에 YOLO를 어떻게 통합할 수 있나요?#
Ultralytics YOLO를 Python 프로젝트에 통합하는 것은 간단합니다. 사전 학습된 모델을 로드하거나 처음부터 모델을 학습시킬 수 있습니다. 시작 방법은 다음과 같습니다:
from ultralytics import YOLO
# Load a pretrained YOLO model
model = YOLO("yolo26n.pt")
# Perform object detection on an image
results = model("https://ultralytics.com/images/bus.jpg")
# Visualize the results
for result in results:
result.show()더 자세한 예제는 Predict 모드 섹션을 참조하십시오.
Link to this sectionYOLO에서 사용 가능한 다양한 모드에는 어떤 것이 있나요?#
Ultralytics YOLO는 다양한 머신 러닝 워크플로우를 지원하기 위해 여러 모드를 제공합니다. 포함된 모드는 다음과 같습니다:
- Train: 커스텀 데이터셋을 사용하여 모델을 학습시킵니다.
- Val: 검증 세트에서 모델 성능을 검증합니다.
- Predict: 새로운 이미지나 비디오 스트림에 대해 예측을 수행합니다.
- Export: 모델을 ONNX 및 TensorRT와 같은 다양한 형식으로 내보냅니다.
- Track: 비디오 스트림에서 실시간으로 객체를 추적합니다.
- Benchmark: 다양한 구성에 걸쳐 모델 성능을 벤치마킹합니다.
각 모드는 모델 개발 및 배포의 여러 단계에 필요한 포괄적인 기능을 제공하도록 설계되었습니다.
Link to this section내 데이터셋을 사용하여 커스텀 YOLO 모델을 어떻게 학습시키나요?#
커스텀 YOLO 모델을 학습하려면 데이터셋과 기타 하이퍼파라미터를 지정해야 합니다. 다음은 간단한 예제입니다:
from ultralytics import YOLO
# Load the YOLO model
model = YOLO("yolo26n.yaml")
# Train the model with custom dataset
model.train(data="path/to/your/dataset.yaml", epochs=10)학습에 대한 자세한 내용과 예제 사용법 하이퍼링크는 Train 모드 페이지를 방문하십시오.
Link to this section배포를 위해 YOLO 모델을 어떻게 내보내나요?#
export 함수를 사용하면 배포에 적합한 형식으로 YOLO 모델을 쉽게 내보낼 수 있습니다. 예를 들어, 모델을 ONNX 형식으로 내보낼 수 있습니다:
from ultralytics import YOLO
# Load the YOLO model
model = YOLO("yolo26n.pt")
# Export the model to ONNX format
model.export(format="onnx")다양한 내보내기 옵션은 Export 모드 문서를 참조하십시오.
Link to this section다른 데이터셋에서 YOLO 모델을 검증할 수 있나요?#
네, 다른 데이터셋에서 YOLO 모델을 검증할 수 있습니다. 학습 후 검증 모드를 사용하여 성능을 평가할 수 있습니다:
from ultralytics import YOLO
# Load a YOLO model
model = YOLO("yolo26n.yaml")
# Train the model
model.train(data="coco8.yaml", epochs=5)
# Validate the model on a different dataset
model.val(data="path/to/separate/data.yaml")자세한 예제와 사용법은 Val 모드 페이지를 확인하십시오.