[pytorch]Box operator로 Bounding Box 쉽게 다루기

2023. 3. 17. 16:37·💻 Programming/AI & ML
반응형
torchvision.ops

torchvision.ops 에서는 computer vision 관련 각종 operator 들을 제공하는데, 그중에서 Bounding Box를 다루는 Box Operators를 소개한다. Detection 관련 코드를 만지다보면 bounding box를 다룰 일이 많은데, torchvision.ops 에서는 bactch 단위로 연산이 가능한 각종 기능들을 제공한다.

 

* box 면적, box간의 iou, box format 변경, clip 등 수행 가능

 

 

테스트
  • box 3개를 정의하고 앞서 살펴본 box operator 기능들을 테스트
import numpy as np
import cv2
import torch
from torchvision.ops import *

# boxes shape : (N, 4)
# box format : x1, y1, x2, y2
boxes = torch.tensor([[50,50,150,110],      
                      [70,30,200,80],
                      [200,300,250,400]])

black_canvas = np.zeros((500,500,3))

cv2.rectangle(black_canvas, (boxes[0][0].item(), boxes[0][1].item()), (boxes[0][2].item(), boxes[0][3].item()), (0,0,255),5)
cv2.rectangle(black_canvas, (boxes[1][0].item(), boxes[1][1].item()), (boxes[1][2].item(), boxes[1][3].item()), (0,255,0),5)
cv2.rectangle(black_canvas, (boxes[2][0].item(), boxes[2][1].item()), (boxes[2][2].item(), boxes[2][3].item()), (255,0,0),5)

print('- boxes.shape :', boxes.shape)
print('- box_area :',box_area(boxes))
print('- box_convert (xyxy -> xywh) :\n',box_convert(boxes, in_fmt='xyxy', out_fmt='xywh'))
print('- box_convert (xyxy -> cxcywh) :\n',box_convert(boxes, in_fmt='xyxy', out_fmt='cxcywh'))
print('- box_iou (boxes[0], boxes[1]) :', box_iou(boxes[0].unsqueeze(0), boxes[1].unsqueeze(0)))
print('- box_iou (boxes[0], boxes[2]) :', box_iou(boxes[0].unsqueeze(0), boxes[2].unsqueeze(0)))
print('- generalized_box_iou (boxes[0], boxes[1]) :', generalized_box_iou(boxes[0].unsqueeze(0), boxes[1].unsqueeze(0)))
print('- generalized_box_iou (boxes[0], boxes[2]) :', generalized_box_iou(boxes[0].unsqueeze(0), boxes[2].unsqueeze(0)))
print('- clip_boxes_to_image (size = (100, 100)) :\n', clip_boxes_to_image(boxes, (100,100)))
print('- box_area :',box_area(boxes))
print('- remove_small_boxes (min_size = (60)) :', remove_small_boxes(boxes, 60))

cv2.imshow('test', black_canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

 

코드 결과

 

반응형

'💻 Programming > AI & ML' 카테고리의 다른 글

[Model Inference] Torch-TensorRT 사용법 | 딥러닝 모델 최적화 및 인퍼런스 가속화  (1) 2023.10.02
[pytorch] Multi-GPU Training | 다중 GPU 학습 예시| Distributed Data Parallel (DDP) | Data Parallel (DP)  (0) 2023.04.17
[pytorch] Dataloader의 'collate_fn'을 사용한 이미지 패딩. 가변 사이즈의 이미지를 batch로 묶어 Dataloader에 주입하는 방법.  (0) 2023.03.03
[HuggingFace] Swin Transformer 이미지 분류 모델 학습 튜토리얼  (0) 2023.01.11
[ONNX] pytorch 모델을 ONNX로 변환하고 실행하기  (0) 2022.12.21
'💻 Programming/AI & ML' 카테고리의 다른 글
  • [Model Inference] Torch-TensorRT 사용법 | 딥러닝 모델 최적화 및 인퍼런스 가속화
  • [pytorch] Multi-GPU Training | 다중 GPU 학습 예시| Distributed Data Parallel (DDP) | Data Parallel (DP)
  • [pytorch] Dataloader의 'collate_fn'을 사용한 이미지 패딩. 가변 사이즈의 이미지를 batch로 묶어 Dataloader에 주입하는 방법.
  • [HuggingFace] Swin Transformer 이미지 분류 모델 학습 튜토리얼
뭅즤
뭅즤
AI 기술 블로그
    반응형
  • 뭅즤
    moovzi’s Doodle
    뭅즤
  • 전체
    오늘
    어제
  • 공지사항

    • ✨ About Me
    • 분류 전체보기 (213)
      • 📖 Fundamentals (34)
        • Computer Vision (9)
        • 3D vision & Graphics (6)
        • AI & ML (16)
        • NLP (2)
        • etc. (1)
      • 🏛 Research (75)
        • Deep Learning (7)
        • Perception (19)
        • OCR (7)
        • Multi-modal (5)
        • Image•Video Generation (18)
        • 3D Vision (4)
        • Material • Texture Recognit.. (8)
        • Large-scale Model (7)
        • etc. (0)
      • 🛠️ Engineering (8)
        • Distributed Training & Infe.. (5)
        • AI & ML 인사이트 (3)
      • 💻 Programming (92)
        • Python (18)
        • Computer Vision (12)
        • LLM (4)
        • AI & ML (18)
        • Database (3)
        • Distributed Computing (6)
        • Apache Airflow (6)
        • Docker & Kubernetes (14)
        • 코딩 테스트 (4)
        • C++ (1)
        • etc. (6)
      • 💬 ETC (4)
        • 책 리뷰 (4)
  • 링크

    • 리틀리 프로필 (멘토링, 면접책,...)
    • 『나는 AI 엔지니어입니다』
    • Instagram
    • Brunch
    • Github
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
뭅즤
[pytorch]Box operator로 Bounding Box 쉽게 다루기
상단으로

티스토리툴바