File size: 6,617 Bytes
4cef5ec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
-->
# ํคํฌ์ธํธ ํ์ง [[keypoint-detection]]
[[open-in-colab]]
ํคํฌ์ธํธ ๊ฐ์ง(Keypoint detection)์ ์ด๋ฏธ์ง ๋ด์ ํน์ ํฌ์ธํธ๋ฅผ ์๋ณํ๊ณ ์์น๋ฅผ ํ์งํฉ๋๋ค. ์ด๋ฌํ ํคํฌ์ธํธ๋ ๋๋๋งํฌ๋ผ๊ณ ๋ ๋ถ๋ฆฌ๋ฉฐ ์ผ๊ตด ํน์ง์ด๋ ๋ฌผ์ฒด์ ์ผ๋ถ์ ๊ฐ์ ์๋ฏธ ์๋ ํน์ง์ ๋ํ๋
๋๋ค.
ํคํฌ์ธํธ ๊ฐ์ง ๋ชจ๋ธ๋ค์ ์ด๋ฏธ์ง๋ฅผ ์
๋ ฅ์ผ๋ก ๋ฐ์ ์๋์ ๊ฐ์ ์ถ๋ ฅ์ ๋ฐํํฉ๋๋ค.
- **ํคํฌ์ธํธ๋ค๊ณผ ์ ์**: ๊ด์ฌ ํฌ์ธํธ๋ค๊ณผ ํด๋น ํฌ์ธํธ์ ๋ํ ์ ๋ขฐ๋ ์ ์
- **๋์คํฌ๋ฆฝํฐ(Descriptors)**: ๊ฐ ํคํฌ์ธํธ๋ฅผ ๋๋ฌ์ผ ์ด๋ฏธ์ง ์์ญ์ ํํ์ผ๋ก ํ
์ค์ฒ, ๊ทธ๋ผ๋ฐ์ด์
, ๋ฐฉํฅ ๋ฐ ๊ธฐํ ์์ฑ์ ์บก์ฒํฉ๋๋ค.
์ด๋ฒ ๊ฐ์ด๋์์๋ ์ด๋ฏธ์ง์์ ํคํฌ์ธํธ๋ฅผ ์ถ์ถํ๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃจ์ด ๋ณด๊ฒ ์ต๋๋ค.
์ด๋ฒ ํํ ๋ฆฌ์ผ์์๋ ํคํฌ์ธํธ ๊ฐ์ง์ ๊ธฐ๋ณธ์ด ๋๋ ๋ชจ๋ธ์ธ [SuperPoint](./model_doc/superpoint)๋ฅผ ์ฌ์ฉํด๋ณด๊ฒ ์ต๋๋ค.
```python
from transformers import AutoImageProcessor, SuperPointForKeypointDetection
processor = AutoImageProcessor.from_pretrained("magic-leap-community/superpoint")
model = SuperPointForKeypointDetection.from_pretrained("magic-leap-community/superpoint")
```
์๋์ ์ด๋ฏธ์ง๋ก ๋ชจ๋ธ์ ํ
์คํธ ํด๋ณด๊ฒ ์ต๋๋ค.
<div style="display: flex; align-items: center;">
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"
alt="Bee"
style="height: 200px; object-fit: contain; margin-right: 10px;">
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png"
alt="Cats"
style="height: 200px; object-fit: contain;">
</div>
```python
import torch
from PIL import Image
import requests
import cv2
url_image_1 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"
image_1 = Image.open(requests.get(url_image_1, stream=True).raw)
url_image_2 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png"
image_2 = Image.open(requests.get(url_image_2, stream=True).raw)
images = [image_1, image_2]
```
์ด์ ์
๋ ฅ์ ์ฒ๋ฆฌํ๊ณ ์ถ๋ก ์ ํ ์ ์์ต๋๋ค.
```python
inputs = processor(images,return_tensors="pt").to(model.device, model.dtype)
outputs = model(**inputs)
```
๋ชจ๋ธ ์ถ๋ ฅ์๋ ๋ฐฐ์น ๋ด์ ๊ฐ ํญ๋ชฉ์ ๋ํ ์๋์ ์ธ ํคํฌ์ธํธ, ๋์คํฌ๋ฆฝํฐ, ๋ง์คํฌ์ ์ ์๊ฐ ์์ต๋๋ค. ๋ง์คํฌ๋ ์ด๋ฏธ์ง์์ ํคํฌ์ธํธ๊ฐ ์๋ ์์ญ์ ๊ฐ์กฐํ๋ ์ญํ ์ ํฉ๋๋ค.
```python
SuperPointKeypointDescriptionOutput(loss=None, keypoints=tensor([[[0.0437, 0.0167],
[0.0688, 0.0167],
[0.0172, 0.0188],
...,
[0.5984, 0.9812],
[0.6953, 0.9812]]]),
scores=tensor([[0.0056, 0.0053, 0.0079, ..., 0.0125, 0.0539, 0.0377],
[0.0206, 0.0058, 0.0065, ..., 0.0000, 0.0000, 0.0000]],
grad_fn=<CopySlices>), descriptors=tensor([[[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357],
[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357],
[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357],
...],
grad_fn=<CopySlices>), mask=tensor([[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 0, 0, 0]], dtype=torch.int32), hidden_states=None)
```
์ด๋ฏธ์ง์ ์ค์ ํคํฌ์ธํธ๋ฅผ ํ์ํ๊ธฐ ์ํด์ ๊ฒฐ๊ณผ๊ฐ์ ํ์ฒ๋ฆฌ ํด์ผํฉ๋๋ค. ์ด๋ฅผ ์ํด ์ค์ ์ด๋ฏธ์ง ํฌ๊ธฐ๋ฅผ ๊ฒฐ๊ณผ๊ฐ๊ณผ ํจ๊ป `post_process_keypoint_detection`์ ์ ๋ฌํด์ผ ํฉ๋๋ค.
```python
image_sizes = [(image.size[1], image.size[0]) for image in images]
outputs = processor.post_process_keypoint_detection(outputs, image_sizes)
```
์ ์ฝ๋๋ฅผ ํตํด ๊ฒฐ๊ณผ๊ฐ์ ๋์
๋๋ฆฌ๋ฅผ ๊ฐ๋ ๋ฆฌ์คํธ๊ฐ ๋๊ณ , ๊ฐ ๋์
๋๋ฆฌ๋ค์ ํ์ฒ๋ฆฌ๋ ํคํฌ์ธํธ, ์ ์ ๋ฐ ๋์คํฌ๋ฆฝํฐ๋ก ์ด๋ฃจ์ด์ ธ์์ต๋๋ค.
```python
[{'keypoints': tensor([[ 226, 57],
[ 356, 57],
[ 89, 64],
...,
[3604, 3391]], dtype=torch.int32),
'scores': tensor([0.0056, 0.0053, ...], grad_fn=<IndexBackward0>),
'descriptors': tensor([[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357],
[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357]],
grad_fn=<IndexBackward0>)},
{'keypoints': tensor([[ 46, 6],
[ 78, 6],
[422, 6],
[206, 404]], dtype=torch.int32),
'scores': tensor([0.0206, 0.0058, 0.0065, 0.0053, 0.0070, ...,grad_fn=<IndexBackward0>),
'descriptors': tensor([[-0.0525, 0.0726, 0.0270, ..., 0.0389, -0.0189, -0.0211],
[-0.0525, 0.0726, 0.0270, ..., 0.0389, -0.0189, -0.0211]}]
```
์ด์ ์ ๋์
๋๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ํคํฌ์ธํธ๋ฅผ ํ์ํ ์ ์์ต๋๋ค.
```python
import matplotlib.pyplot as plt
import torch
for i in range(len(images)):
keypoints = outputs[i]["keypoints"]
scores = outputs[i]["scores"]
descriptors = outputs[i]["descriptors"]
keypoints = outputs[i]["keypoints"].detach().numpy()
scores = outputs[i]["scores"].detach().numpy()
image = images[i]
image_width, image_height = image.size
plt.axis('off')
plt.imshow(image)
plt.scatter(
keypoints[:, 0],
keypoints[:, 1],
s=scores * 100,
c='cyan',
alpha=0.4
)
plt.show()
```
์๋์์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
<div style="display: flex; align-items: center;">
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee_keypoint.png"
alt="Bee"
style="height: 200px; object-fit: contain; margin-right: 10px;">
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats_keypoint.png"
alt="Cats"
style="height: 200px; object-fit: contain;">
</div>
|