|
| 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 | + |
| 13 | +# 단일 영상 기반 깊이 추정[[depth-estimation-pipeline]] |
| 14 | + |
| 15 | +단일 영상 기반 깊이 추정은 한 장면의 단일 이미지에서 장면의 깊이 정보를 예측하는 컴퓨터 비전 작업입니다. |
| 16 | +즉, 단일 카메라 시점의 장면에 있는 물체의 거리를 예측하는 과정입니다. |
| 17 | + |
| 18 | +단일 영상 기반 깊이 추정은 3D 재구성, 증강 현실, 자율 주행, 로봇 공학 등 다양한 분야에서 응용됩니다. |
| 19 | +조명 조건, 가려짐, 텍스처와 같은 요소의 영향을 받을 수 있는 장면 내 물체와 해당 깊이 정보 간의 복잡한 관계를 모델이 이해해야 하므로 까다로운 작업입니다. |
| 20 | + |
| 21 | + |
| 22 | +<Tip> |
| 23 | +이 튜토리얼에서 다루는 작업은 다음 모델 아키텍처에서 지원됩니다: |
| 24 | + |
| 25 | +<!--This tip is automatically generated by `make fix-copies`, do not fill manually!--> |
| 26 | + |
| 27 | +[DPT](../model_doc/dpt), [GLPN](../model_doc/glpn) |
| 28 | + |
| 29 | +<!--End of the generated tip--> |
| 30 | + |
| 31 | +</Tip> |
| 32 | + |
| 33 | +이번 가이드에서 배울 내용은 다음과 같습니다: |
| 34 | + |
| 35 | +* 깊이 추정 파이프라인 만들기 |
| 36 | +* 직접 깊이 추정 추론하기 |
| 37 | + |
| 38 | +시작하기 전에, 필요한 모든 라이브러리가 설치되어 있는지 확인하세요: |
| 39 | + |
| 40 | +```bash |
| 41 | +pip install -q transformers |
| 42 | +``` |
| 43 | + |
| 44 | +## 깊이 추정 파이프라인[[depth-estimation-inference-by-hand]] |
| 45 | + |
| 46 | +깊이 추정을 추론하는 가장 간단한 방법은 해당 기능을 제공하는 [`pipeline`]을 사용하는 것입니다. |
| 47 | +[Hugging Face Hub 체크포인트](https://huggingface.co/models?pipeline_tag=depth-estimation&sort=downloads)에서 파이프라인을 초기화합니다: |
| 48 | + |
| 49 | +```py |
| 50 | +>>> from transformers import pipeline |
| 51 | + |
| 52 | +>>> checkpoint = "vinvino02/glpn-nyu" |
| 53 | +>>> depth_estimator = pipeline("depth-estimation", model=checkpoint) |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +다음으로, 분석할 이미지를 한 장 선택하세요: |
| 58 | + |
| 59 | +```py |
| 60 | +>>> from PIL import Image |
| 61 | +>>> import requests |
| 62 | + |
| 63 | +>>> url = "https://unsplash.com/photos/HwBAsSbPBDU/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MzR8fGNhciUyMGluJTIwdGhlJTIwc3RyZWV0fGVufDB8MHx8fDE2Nzg5MDEwODg&force=true&w=640" |
| 64 | +>>> image = Image.open(requests.get(url, stream=True).raw) |
| 65 | +>>> image |
| 66 | +``` |
| 67 | + |
| 68 | +<div class="flex justify-center"> |
| 69 | + <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/depth-estimation-example.jpg" alt="Photo of a busy street"/> |
| 70 | +</div> |
| 71 | + |
| 72 | +이미지를 파이프라인으로 전달합니다. |
| 73 | + |
| 74 | +```py |
| 75 | +>>> predictions = depth_estimator(image) |
| 76 | +``` |
| 77 | + |
| 78 | +파이프라인은 두 개의 항목을 가지는 딕셔너리를 반환합니다. |
| 79 | +첫 번째는 `predicted_depth`로 각 픽셀의 깊이를 미터로 표현한 값을 가지는 텐서입니다. |
| 80 | +두 번째는 `depth`로 깊이 추정 결과를 시각화하는 PIL 이미지입니다. |
| 81 | + |
| 82 | +이제 시각화한 결과를 살펴보겠습니다: |
| 83 | + |
| 84 | +```py |
| 85 | +>>> predictions["depth"] |
| 86 | +``` |
| 87 | + |
| 88 | +<div class="flex justify-center"> |
| 89 | + <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/depth-visualization.png" alt="Depth estimation visualization"/> |
| 90 | +</div> |
| 91 | + |
| 92 | +## 직접 깊이 추정 추론하기[[depth-estimation-inference-by-hand]] |
| 93 | + |
| 94 | +이제 깊이 추정 파이프라인 사용법을 살펴보았으니 동일한 결과를 복제하는 방법을 살펴보겠습니다. |
| 95 | +[Hugging Face Hub 체크포인트](https://huggingface.co/models?pipeline_tag=depth-estimation&sort=downloads)에서 모델과 관련 프로세서를 가져오는 것부터 시작합니다. |
| 96 | +여기서 이전에 사용한 체크포인트와 동일한 것을 사용합니다: |
| 97 | + |
| 98 | +```py |
| 99 | +>>> from transformers import AutoImageProcessor, AutoModelForDepthEstimation |
| 100 | + |
| 101 | +>>> checkpoint = "vinvino02/glpn-nyu" |
| 102 | + |
| 103 | +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint) |
| 104 | +>>> model = AutoModelForDepthEstimation.from_pretrained(checkpoint) |
| 105 | +``` |
| 106 | + |
| 107 | +필요한 이미지 변환을 처리하는 `image_processor`를 사용하여 모델에 대한 이미지 입력을 준비합니다. |
| 108 | +`image_processor`는 크기 조정 및 정규화 등 필요한 이미지 변환을 처리합니다: |
| 109 | + |
| 110 | +```py |
| 111 | +>>> pixel_values = image_processor(image, return_tensors="pt").pixel_values |
| 112 | +``` |
| 113 | + |
| 114 | +준비한 입력을 모델로 전달합니다: |
| 115 | + |
| 116 | +```py |
| 117 | +>>> import torch |
| 118 | + |
| 119 | +>>> with torch.no_grad(): |
| 120 | +... outputs = model(pixel_values) |
| 121 | +... predicted_depth = outputs.predicted_depth |
| 122 | +``` |
| 123 | + |
| 124 | +결과를 시각화합니다: |
| 125 | + |
| 126 | +```py |
| 127 | +>>> import numpy as np |
| 128 | + |
| 129 | +>>> # 원본 사이즈로 복원 |
| 130 | +>>> prediction = torch.nn.functional.interpolate( |
| 131 | +... predicted_depth.unsqueeze(1), |
| 132 | +... size=image.size[::-1], |
| 133 | +... mode="bicubic", |
| 134 | +... align_corners=False, |
| 135 | +... ).squeeze() |
| 136 | +>>> output = prediction.numpy() |
| 137 | + |
| 138 | +>>> formatted = (output * 255 / np.max(output)).astype("uint8") |
| 139 | +>>> depth = Image.fromarray(formatted) |
| 140 | +>>> depth |
| 141 | +``` |
| 142 | + |
| 143 | +<div class="flex justify-center"> |
| 144 | + <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/depth-visualization.png" alt="Depth estimation visualization"/> |
| 145 | +</div> |
0 commit comments