|
| 1 | +<!--Copyright 2023 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +
|
| 12 | +⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 | +rendered properly in your Markdown viewer. |
| 14 | +
|
| 15 | +--> |
| 16 | + |
| 17 | +# 키포인트 탐지 [[keypoint-detection]] |
| 18 | + |
| 19 | +[[open-in-colab]] |
| 20 | + |
| 21 | +키포인트 감지(Keypoint detection)은 이미지 내의 특정 포인트를 식별하고 위치를 탐지합니다. 이러한 키포인트는 랜드마크라고도 불리며 얼굴 특징이나 물체의 일부와 같은 의미 있는 특징을 나타냅니다. |
| 22 | +키포인트 감지 모델들은 이미지를 입력으로 받아 아래와 같은 출력을 반환합니다. |
| 23 | + |
| 24 | +- **키포인트들과 점수**: 관심 포인트들과 해당 포인트에 대한 신뢰도 점수 |
| 25 | +- **디스크립터(Descriptors)**: 각 키포인트를 둘러싼 이미지 영역의 표현으로 텍스처, 그라데이션, 방향 및 기타 속성을 캡처합니다. |
| 26 | + |
| 27 | +이번 가이드에서는 이미지에서 키포인트를 추출하는 방법을 다루어 보겠습니다. |
| 28 | + |
| 29 | +이번 튜토리얼에서는 키포인트 감지의 기본이 되는 모델인 [SuperPoint](./model_doc/superpoint)를 사용해보겠습니다. |
| 30 | + |
| 31 | +```python |
| 32 | +from transformers import AutoImageProcessor, SuperPointForKeypointDetection |
| 33 | +processor = AutoImageProcessor.from_pretrained("magic-leap-community/superpoint") |
| 34 | +model = SuperPointForKeypointDetection.from_pretrained("magic-leap-community/superpoint") |
| 35 | +``` |
| 36 | +아래의 이미지로 모델을 테스트 해보겠습니다. |
| 37 | + |
| 38 | +<div style="display: flex; align-items: center;"> |
| 39 | + <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg" |
| 40 | + alt="Bee" |
| 41 | + style="height: 200px; object-fit: contain; margin-right: 10px;"> |
| 42 | + <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png" |
| 43 | + alt="Cats" |
| 44 | + style="height: 200px; object-fit: contain;"> |
| 45 | +</div> |
| 46 | + |
| 47 | + |
| 48 | +```python |
| 49 | +import torch |
| 50 | +from PIL import Image |
| 51 | +import requests |
| 52 | +import cv2 |
| 53 | + |
| 54 | + |
| 55 | +url_image_1 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg" |
| 56 | +image_1 = Image.open(requests.get(url_image_1, stream=True).raw) |
| 57 | +url_image_2 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png" |
| 58 | +image_2 = Image.open(requests.get(url_image_2, stream=True).raw) |
| 59 | + |
| 60 | +images = [image_1, image_2] |
| 61 | +``` |
| 62 | + |
| 63 | +이제 입력을 처리하고 추론을 할 수 있습니다. |
| 64 | + |
| 65 | + |
| 66 | +```python |
| 67 | +inputs = processor(images,return_tensors="pt").to(model.device, model.dtype) |
| 68 | +outputs = model(**inputs) |
| 69 | +``` |
| 70 | +모델 출력에는 배치 내의 각 항목에 대한 상대적인 키포인트, 디스크립터, 마스크와 점수가 있습니다. 마스크는 이미지에서 키포인트가 있는 영역을 강조하는 역할을 합니다. |
| 71 | + |
| 72 | +```python |
| 73 | +SuperPointKeypointDescriptionOutput(loss=None, keypoints=tensor([[[0.0437, 0.0167], |
| 74 | + [0.0688, 0.0167], |
| 75 | + [0.0172, 0.0188], |
| 76 | + ..., |
| 77 | + [0.5984, 0.9812], |
| 78 | + [0.6953, 0.9812]]]), |
| 79 | + scores=tensor([[0.0056, 0.0053, 0.0079, ..., 0.0125, 0.0539, 0.0377], |
| 80 | + [0.0206, 0.0058, 0.0065, ..., 0.0000, 0.0000, 0.0000]], |
| 81 | + grad_fn=<CopySlices>), descriptors=tensor([[[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], |
| 82 | + [-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], |
| 83 | + [-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], |
| 84 | + ...], |
| 85 | + grad_fn=<CopySlices>), mask=tensor([[1, 1, 1, ..., 1, 1, 1], |
| 86 | + [1, 1, 1, ..., 0, 0, 0]], dtype=torch.int32), hidden_states=None) |
| 87 | +``` |
| 88 | + |
| 89 | +이미지에 실제 키포인트를 표시하기 위해선 결과값을 후처리 해야합니다. 이를 위해 실제 이미지 크기를 결과값과 함께 `post_process_keypoint_detection`에 전달해야 합니다. |
| 90 | + |
| 91 | +```python |
| 92 | +image_sizes = [(image.size[1], image.size[0]) for image in images] |
| 93 | +outputs = processor.post_process_keypoint_detection(outputs, image_sizes) |
| 94 | +``` |
| 95 | + |
| 96 | +위 코드를 통해 결과값은 딕셔너리를 갖는 리스트가 되고, 각 딕셔너리들은 후처리된 키포인트, 점수 및 디스크립터로 이루어져있습니다. |
| 97 | + |
| 98 | + |
| 99 | +```python |
| 100 | +[{'keypoints': tensor([[ 226, 57], |
| 101 | + [ 356, 57], |
| 102 | + [ 89, 64], |
| 103 | + ..., |
| 104 | + [3604, 3391]], dtype=torch.int32), |
| 105 | + 'scores': tensor([0.0056, 0.0053, ...], grad_fn=<IndexBackward0>), |
| 106 | + 'descriptors': tensor([[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], |
| 107 | + [-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357]], |
| 108 | + grad_fn=<IndexBackward0>)}, |
| 109 | + {'keypoints': tensor([[ 46, 6], |
| 110 | + [ 78, 6], |
| 111 | + [422, 6], |
| 112 | + [206, 404]], dtype=torch.int32), |
| 113 | + 'scores': tensor([0.0206, 0.0058, 0.0065, 0.0053, 0.0070, ...,grad_fn=<IndexBackward0>), |
| 114 | + 'descriptors': tensor([[-0.0525, 0.0726, 0.0270, ..., 0.0389, -0.0189, -0.0211], |
| 115 | + [-0.0525, 0.0726, 0.0270, ..., 0.0389, -0.0189, -0.0211]}] |
| 116 | +``` |
| 117 | + |
| 118 | +이제 위 딕셔너리를 사용하여 키포인트를 표시할 수 있습니다. |
| 119 | + |
| 120 | +```python |
| 121 | +import matplotlib.pyplot as plt |
| 122 | +import torch |
| 123 | + |
| 124 | +for i in range(len(images)): |
| 125 | + keypoints = outputs[i]["keypoints"] |
| 126 | + scores = outputs[i]["scores"] |
| 127 | + descriptors = outputs[i]["descriptors"] |
| 128 | + keypoints = outputs[i]["keypoints"].detach().numpy() |
| 129 | + scores = outputs[i]["scores"].detach().numpy() |
| 130 | + image = images[i] |
| 131 | + image_width, image_height = image.size |
| 132 | + |
| 133 | + plt.axis('off') |
| 134 | + plt.imshow(image) |
| 135 | + plt.scatter( |
| 136 | + keypoints[:, 0], |
| 137 | + keypoints[:, 1], |
| 138 | + s=scores * 100, |
| 139 | + c='cyan', |
| 140 | + alpha=0.4 |
| 141 | + ) |
| 142 | + plt.show() |
| 143 | +``` |
| 144 | + |
| 145 | +아래에서 결과를 확인할 수 있습니다. |
| 146 | + |
| 147 | +<div style="display: flex; align-items: center;"> |
| 148 | + <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee_keypoint.png" |
| 149 | + alt="Bee" |
| 150 | + style="height: 200px; object-fit: contain; margin-right: 10px;"> |
| 151 | + <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats_keypoint.png" |
| 152 | + alt="Cats" |
| 153 | + style="height: 200px; object-fit: contain;"> |
| 154 | +</div> |
| 155 | + |
0 commit comments