> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-serverless-sft-revamp.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 튜토리얼: 데이터셋 아티팩트 생성, 추적 및 사용하기

> W&B를 사용하여 데이터셋 아티팩트를 생성하고 추적하며 사용해 보세요.

이 튜토리얼에서는 데이터셋 Artifacts 를 생성, 추적 및 사용하는 방법을 설명합니다.

## 1. W\&B 로그인

W\&B 라이브러리를 임포트하고 W\&B에 로그인합니다. 아직 계정이 없다면 무료 W\&B 계정에 가입해야 합니다.

```python theme={null}
import wandb

wandb.login()
```

## 2. Run 초기화

[`wandb.init()`](/models/ref/python/functions/init) 을 사용하여 run 을 초기화합니다. 이 함수는 데이터 로깅 및 동기화를 위한 백그라운드 프로세스를 생성합니다. 프로젝트 이름과 job 유형 을 지정하세요:

```python theme={null}
# W&B Run을 생성합니다. 여기서는 데이터셋 Artifact를 생성하는 예제이므로
# job_type을 'upload-dataset'으로 지정합니다.
with wandb.init(project="artifacts-example", job_type="upload-dataset") as run:
    # 여기에 코드를 작성하세요
```

## 3. Artifact 오브젝트 생성

[`wandb.Artifact()`](/models/ref/python/experiments/artifact) 를 사용하여 artifact 오브젝트를 생성합니다. `name` 파라미터에는 artifact의 이름을, `type` 파라미터에는 파일 유형에 대한 설명을 입력합니다.

예를 들어, 다음 코드 조각 은 `‘bicycle-dataset’` 이라는 이름과 `‘dataset’` 이라는 레이블을 가진 artifact를 생성하는 방법을 보여줍니다:

```python theme={null}
artifact = wandb.Artifact(name="bicycle-dataset", type="dataset")
```

artifact 구성 방법에 대한 자세한 내용은 [Artifacts 구성](./construct-an-artifact)을 참조하세요.

## 4. Artifact에 데이터셋 추가

artifact에 파일을 추가합니다. 일반적인 파일 유형으로는 모델과 데이터셋이 있습니다. 다음 예제는 로컬 머신에 저장된 `dataset.h5` 라는 데이터셋을 artifact에 추가합니다:

```python theme={null}
# artifact의 콘텐츠에 파일을 추가합니다
artifact.add_file(local_path="dataset.h5")
```

위의 코드 조각 에서 `dataset.h5` 를 artifact에 추가하려는 실제 파일 경로로 바꾸세요.

## 5. 데이터셋 로그

W\&B run 오브젝트의 `wandb.Run.log_artifact()` 메소드를 사용하여 artifact 버전을 저장하고, 해당 artifact를 [run의 출력](/models/artifacts/explore-and-traverse-an-artifact-graph)으로 선언합니다.

```python theme={null}
# Artifact 버전을 W&B에 저장하고
# 이 run의 출력으로 표시합니다
run.log_artifact(artifact)
```

artifact를 로그할 때 기본적으로 `'latest'` [에일리어스](/models/artifacts/create-a-custom-alias) 가 생성됩니다. artifact 에일리어스 및 버전에 대한 자세한 내용은 각각 [커스텀 에일리어스 생성](./create-a-custom-alias) 및 [새로운 artifact 버전 생성](./create-a-new-artifact-version)을 참조하세요.

지금까지의 전체 스크립트는 다음과 같습니다:

```python theme={null}
import wandb

wandb.login()

with wandb.init(project="artifacts-example", job_type="upload-dataset") as run:
    artifact = wandb.Artifact(name="bicycle-dataset", type="dataset")
    artifact.add_file(local_path="dataset.h5")
    run.log_artifact(artifact)
```

## 6. Artifact 다운로드 및 사용

다음 코드 예제는 W\&B 서버에 로깅 및 저장된 artifact를 사용하는 단계를 보여줍니다.

1. 먼저, **`wandb.init()`** 으로 새로운 run 오브젝트를 초기화합니다.
2. 둘째, run 오브젝트의 [`wandb.Run.use_artifact()`](/models/ref/python/experiments/run#use_artifact) 메소드를 사용하여 어떤 artifact를 사용할지 W\&B에 알립니다. 이 메소드는 artifact 오브젝트를 반환합니다.
3. 셋째, artifact 오브젝트의 [`wandb.Artifact.download()`](/models/ref/python/experiments/artifact#download) 메소드를 사용하여 artifact의 콘텐츠를 다운로드합니다.

```python theme={null}
# W&B Run을 생성합니다. 여기서는 트레이닝 과정을 추적할 것이므로
# 'type'을 'training'으로 지정합니다.
with wandb.init(project="artifacts-example", job_type="training") as run:

  # W&B에서 Artifact를 쿼리하고 이 run의 입력으로 표시합니다
  artifact = run.use_artifact("bicycle-dataset:latest")

  # Artifact의 콘텐츠를 다운로드합니다
  artifact_dir = artifact.download()
```

또는 Public API (`wandb.Api`)를 사용하여 Run 외부에서 이미 W\&B에 저장된 데이터를 내보내거나 업데이트할 수 있습니다. 자세한 내용은 [외부 파일 추적](./track-external-files)을 참조하세요.
