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>