Python 딥 러 닝 사용 Albummentations 이미지 강화

1.필요 한 라 이브 러 리 가 져 오기

import random
 
import cv2
from matplotlib import pyplot as plt
 
import albumentations as A
2.시각 화 함수 가 그림 의 경계 상자 와 클래스 라벨 을 표시 하도록 정의 합 니 다.
시각 화 함수 참조https://github.com/facebookresearch/Detectron/blob/master/detectron/utils/vis.py

BOX_COLOR = (255, 0, 0) # Red
TEXT_COLOR = (255, 255, 255) # White
 
 
def visualize_bbox(img, bbox, class_name, color=BOX_COLOR, thickness=2):
    """Visualizes a single bounding box on the image"""
    x_min, y_min, w, h = bbox
    x_min, x_max, y_min, y_max = int(x_min), int(x_min + w), int(y_min), int(y_min + h)
 
    cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=color, thickness=thickness)
 
    ((text_width, text_height), _) = cv2.getTextSize(class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.35, 1)    
    cv2.rectangle(img, (x_min, y_min - int(1.3 * text_height)), (x_min + text_width, y_min), BOX_COLOR, -1)
    cv2.putText(
        img,
        text=class_name,
        org=(x_min, y_min - int(0.3 * text_height)),
        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
        fontScale=0.35, 
        color=TEXT_COLOR, 
        lineType=cv2.LINE_AA,
    )
    return img
 
 
def visualize(image, bboxes, category_ids, category_id_to_name):
    img = image.copy()
    for bbox, category_id in zip(bboxes, category_ids):
        class_name = category_id_to_name[category_id]
        img = visualize_bbox(img, bbox, class_name)
    plt.figure(figsize=(12, 12))
    plt.axis('off')
    plt.imshow(img)
3.그림 과 레이 블 가 져 오기
이 예제 에서 저 희 는 COCO 데이터 세트 에서 온 그림 을 사용 할 것 입 니 다.이 그림 은 두 개의 연 결 된 경계 상 자 를 가지 고 있 습 니 다.에 자리잡다,...
디스크 에서 그림 불 러 오기

image = cv2.imread('images/000000386298.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
좌표 와 클래스 태그 로 두 개의 경계 상 자 를 정의 합 니 다.
이 경계 상자 의 좌 표 는 coco 형식 으로 설명 합 니 다.각 경계 상자 마다 네 개의 값[x_min, y_min, width, height]을 사용 하여 설명 합 니 다.경계 상자 좌표 의 서로 다른 형식 에 대한 상세 한 설명 은 경계 상자 에 관 한 문서 글-http://cocodataset.org/#explore?id=386298을 참조 하 십시오.

bboxes = [[5.66, 138.95, 147.09, 164.88], [366.7, 80.84, 132.8, 181.84]]
category_ids = [17, 18]
 
# We will use the mapping from category_id to the class name
# to visualize the class label for the bounding box on the image
category_id_to_name = {17: 'cat', 18: 'dog'}
그림 의 테두리 보이 기

visualize(image, bboxes, category_ids, category_id_to_name)

4.RandomSizedBBox SafeCrop 을 사용 하여 원본 그림 의 모든 경계 상 자 를 유지 합 니 다.RandomSizedBBoxSafeCrop  crops a random part of the image. It ensures that the cropped part will contain all bounding boxes from the original image. Then the transform rescales the crop to height and width specified by the respective parameters. The  erosion_rate  parameter controls how much area of the original bounding box could be lost after cropping.  erosion_rate = 0.2  means that the augmented bounding box's area could be up to 20% smaller than the area of the original bounding box.
RandomSizedBBox SafeCrop 그림 의 무 작위 부분 을 재단 합 니 다.재단 한 부분 에 원본 그림 의 모든 경계 상자 가 포함 되 어 있 는 지 확인 합 니 다.그 다음 에 작 물 을 해당 매개 변수 가 지정 한 높이 와 너비 로 다시 축소 합 니 다.erosion_rate 매개 변 수 는 재단 후 원본 경계 상자 의 면적 을 잃 어 버 릴 수 있 습 니 다.frosting_rate=0.2 는 확 장 된 경계선 의 면적 이 원시 경계선 의 면적 보다 20%작 을 수 있 음 을 나타 낸다.
5.강화 관 정의

transform = A.Compose(
    [A.RandomSizedBBoxSafeCrop(width=448, height=336, erosion_rate=0.2)],
    bbox_params=A.BboxParams(format='coco', label_fields=['category_ids']),
)
6.강화 에 사용 할 그림 과 테 두 리 를 입력 합 니 다.
우리 가 랜 덤 피 드 를 고정 시 키 는 것 은 시각 화 목적 을 위 한 것 이기 때문에 강 화 는 항상 같은 결 과 를 낳 을 것 이다.실제 컴퓨터 시각 파이프 에서 그림 을 변환 하기 전에 무 작위 피 드 를 고정 시 켜 서 는 안 됩 니 다.이 경우 파 이 프 는 항상 같은 그림 을 출력 하기 때 문 입 니 다.이미지 강화 의 목적 은 매번 다른 변환 을 사용 하 는 것 이다.

random.seed(7)
transformed = transform(image=image, bboxes=bboxes, category_ids=category_ids)
visualize(
    transformed['image'],
    transformed['bboxes'],
    transformed['category_ids'],
    category_id_to_name,
)

7.다른 랜 덤 피 드 의 예제

random.seed(3)
transformed = transform(image=image, bboxes=bboxes, category_ids=category_ids)
visualize(
    transformed['image'],
    transformed['bboxes'],
    transformed['category_ids'],
    category_id_to_name,
)

random.seed(444)
transformed = transform(image=image, bboxes=bboxes, category_ids=category_ids)
visualize(
    transformed['image'],
    transformed['bboxes'],
    transformed['category_ids'],
    category_id_to_name,
)

Python 딥 러 닝 의 사용 Albummentations 가 목표 검출 임무 에 대한 증강 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.관련 된 것 은 Albummentations 로 목표 에 대한 증강 내용 입 니 다.우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기