Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐ŸŒ [i18n-KO] Translated image_to_image.md to Korean #32327

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/source/ko/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
title: ์ œ๋กœ์ƒท(zero-shot) ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜
- local: tasks/monocular_depth_estimation
title: ๋‹จ์ผ ์˜์ƒ ๊ธฐ๋ฐ˜ ๊นŠ์ด ์ถ”์ •
- local: in_translation
title: (๋ฒˆ์—ญ์ค‘) Image-to-Image
- local: tasks/image_to_image
title: Image-to-Image
- local: in_translation
title: (๋ฒˆ์—ญ์ค‘) Image Feature Extraction
- local: in_translation
Expand Down
132 changes: 132 additions & 0 deletions docs/source/ko/tasks/image_to_image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<!--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.

-->

# Image-to-Image ์ž‘์—… ๊ฐ€์ด๋“œ [[image-to-image-task-guide]]

[[open-in-colab]]

Image-to-Image ์ž‘์—…์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋˜ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ์ด๋ฏธ์ง€ ํ–ฅ์ƒ(์ดˆ๊ณ ํ•ด์ƒ๋„, ์ €์กฐ๋„ ํ–ฅ์ƒ, ๋น—์ค„๊ธฐ ์ œ๊ฑฐ ๋“ฑ), ์ด๋ฏธ์ง€ ๋ณต์› ๋“ฑ ๋‹ค์–‘ํ•œ ํ•˜์œ„ ์ž‘์—…์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
- ์ดˆ๊ณ ํ•ด์ƒ๋„ ์ž‘์—…์„ ์œ„ํ•œ image-to-image ํŒŒ์ดํ”„๋ผ์ธ ์‚ฌ์šฉ,
- ํŒŒ์ดํ”„๋ผ์ธ ์—†์ด ๋™์ผํ•œ ์ž‘์—…์„ ์œ„ํ•œ image-to-image ๋ชจ๋ธ ์‹คํ–‰

์ด ๊ฐ€์ด๋“œ๊ฐ€ ๋ฐœํ‘œ๋œ ์‹œ์ ์—์„œ๋Š”, `image-to-image` ํŒŒ์ดํ”„๋ผ์ธ์€ ์ดˆ๊ณ ํ•ด์ƒ๋„ ์ž‘์—…๋งŒ ์ง€์›ํ•œ๋‹ค๋Š” ์ ์„ ์œ ์˜ํ•˜์„ธ์š”.

ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

```bash
pip install transformers
```

์ด์ œ [Swin2SR ๋ชจ๋ธ](https://huggingface.co/caidas/swin2SR-lightweight-x2-64)์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๋ฏธ์ง€์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์—ฌ ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์ด ํŒŒ์ดํ”„๋ผ์ธ์—์„œ๋Š” [Swin2SR ๋ชจ๋ธ](https://huggingface.co/caidas/swin2SR-lightweight-x2-64)๋งŒ ์ง€์›๋ฉ๋‹ˆ๋‹ค.

```python
from transformers import pipeline

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
pipe = pipeline(task="image-to-image", model="caidas/swin2SR-lightweight-x2-64", device=device)
```

์ด์ œ ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์™€ ๋ด…์‹œ๋‹ค.

```python
from PIL import Image
import requests

url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/cat.jpg"
image = Image.open(requests.get(url, stream=True).raw)

print(image.size)
```
```bash
# (532, 432)
```
<div class="flex justify-center">
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/cat.jpg" alt="Photo of a cat"/>
</div>

์ด์ œ ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณ ์–‘์ด ์ด๋ฏธ์ง€์˜ ์—…์Šค์ผ€์ผ๋œ ๋ฒ„์ „์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

```python
upscaled = pipe(image)
print(upscaled.size)
```
```bash
# (1072, 880)
```

ํŒŒ์ดํ”„๋ผ์ธ ์—†์ด ์ง์ ‘ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด Transformers์˜ `Swin2SRForImageSuperResolution` ๋ฐ `Swin2SRImageProcessor` ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋™์ผํ•œ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ๊ณผ ํ”„๋กœ์„ธ์„œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

```python
from transformers import Swin2SRForImageSuperResolution, Swin2SRImageProcessor

model = Swin2SRForImageSuperResolution.from_pretrained("caidas/swin2SR-lightweight-x2-64").to(device)
processor = Swin2SRImageProcessor("caidas/swin2SR-lightweight-x2-64")
```

`pipeline` ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ „์ฒ˜๋ฆฌ์™€ ํ›„์ฒ˜๋ฆฌ ๋‹จ๊ณ„๋ฅผ ์ถ”์ƒํ™”ํ•˜๋ฏ€๋กœ, ์ด๋ฏธ์ง€๋ฅผ ์ „์ฒ˜๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ํ”„๋กœ์„ธ์„œ์— ์ „๋‹ฌํ•œ ๋‹ค์Œ ํ”ฝ์…€๊ฐ’์„ GPU๋กœ ์ด๋™์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค.

```python
pixel_values = processor(image, return_tensors="pt").pixel_values
print(pixel_values.shape)

pixel_values = pixel_values.to(device)
```

์ด์ œ ํ”ฝ์…€๊ฐ’์„ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

```python
import torch

with torch.no_grad():
outputs = model(pixel_values)
```
์ถœ๋ ฅ์€ ์•„๋ž˜์™€ ๊ฐ™์€ `ImageSuperResolutionOutput` ์œ ํ˜•์˜ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค ๐Ÿ‘‡

```
(loss=None, reconstruction=tensor([[[[0.8270, 0.8269, 0.8275, ..., 0.7463, 0.7446, 0.7453],
[0.8287, 0.8278, 0.8283, ..., 0.7451, 0.7448, 0.7457],
[0.8280, 0.8273, 0.8269, ..., 0.7447, 0.7446, 0.7452],
...,
[0.5923, 0.5933, 0.5924, ..., 0.0697, 0.0695, 0.0706],
[0.5926, 0.5932, 0.5926, ..., 0.0673, 0.0687, 0.0705],
[0.5927, 0.5914, 0.5922, ..., 0.0664, 0.0694, 0.0718]]]],
device='cuda:0'), hidden_states=None, attentions=None)
```
`reconstruction`๋ฅผ ๊ฐ€์ ธ์™€ ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•ด ํ›„์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€ ์‚ดํŽด๋ด…์‹œ๋‹ค.

```python
outputs.reconstruction.data.shape
# torch.Size([1, 3, 880, 1072])
```

์ถœ๋ ฅ ํ…์„œ์˜ ์ฐจ์›์„ ์ถ•์†Œํ•˜๊ณ  0๋ฒˆ์งธ ์ถ•์„ ์ œ๊ฑฐํ•œ ๋‹ค์Œ, ๊ฐ’์„ ํด๋ฆฌํ•‘ํ•˜๊ณ  NumPy ๋ถ€๋™์†Œ์ˆ˜์  ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ [1072, 880] ๋ชจ์–‘์„ ๊ฐ–๋„๋ก ์ถ•์„ ์žฌ์ •๋ ฌํ•˜๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์ถœ๋ ฅ์„ 0๊ณผ 255 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ–๋„๋ก ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค.

```python
import numpy as np

# ํฌ๊ธฐ๋ฅผ ์ค„์ด๊ณ , CPU๋กœ ์ด๋™ํ•˜๊ณ , ๊ฐ’์„ ํด๋ฆฌํ•‘
output = outputs.reconstruction.data.squeeze().cpu().clamp_(0, 1).numpy()
# ์ถ•์„ ์žฌ์ •๋ ฌ
output = np.moveaxis(output, source=0, destination=-1)
# ๊ฐ’์„ ํ”ฝ์…€๊ฐ’ ๋ฒ”์œ„๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ
output = (output * 255.0).round().astype(np.uint8)
Image.fromarray(output)
```
<div class="flex justify-center">
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/cat_upscaled.png" alt="Upscaled photo of a cat"/>
</div>
Loading