DVC - Data version control
데이터 모델링 과정 중 데이터셋이나 모델을 관리하기 위한 도구인 DVC를 사용해보았습니다.
1. DVC 란?
DVC는 머신러닝 프로젝트를 위한 open-source version control 시스템이라고 소개하고 있습니다. git과 유사한 형태로 ML 모델이나 사용된 데이터 셋을 관리할 수 있는 도구입니다.
(Reference: https://dvc.org)
2. Installation
설치하는 방법은 os에 따라 여러가지로 제공되고 있습니다. 저는 mac os 유저라 처음에는 homebrew로 설치해보았는데, 에러가 발생해서 pip을 통해 다시 설치했습니다.
brew install dvc
pip install dvc
3. Initialize
DVC는 Git과 같은 버전 관리 시스템과 같이 사용합니다.
mkdir dvc-test
cd dvc-test
git init
먼저 원하는 디렉토리에서 git init을 해주고,
dvc init
위 명령어로 initialze해주면
Adding '.dvc/lock' to '.dvc/.gitignore'.
Adding '.dvc/config.local' to '.dvc/.gitignore'.
Adding '.dvc/updater' to '.dvc/.gitignore'.
Adding '.dvc/updater.lock' to '.dvc/.gitignore'.
Adding '.dvc/state-journal' to '.dvc/.gitignore'.
Adding '.dvc/state-wal' to '.dvc/.gitignore'.
Adding '.dvc/state' to '.dvc/.gitignore'.
Adding '.dvc/cache' to '.dvc/.gitignore'.
You can now commit the changes to git.
다음과 같이 뜨면서 .dvc 디렉토리가 생성됩니다.
4. Configure
dvc 도 git 처럼 remote 저장소를 만들어서 다른 사람들과 쉽게 dvc 프로젝트를 공유할 수 있습니다.
remote 저장소는 local directory를 활용하거나, s3, gs, azure등 다양한 클라우드 스토리지도 활용가능합니다.
지원하는 remote 시스템 종류
- local: local directory
- s3: Amazon Simple Storage Service
- gs: Google Cloud Storage
- azure: Azure Blob Storage
- ssh: Secure Shell
- hdfs: Hadoop Distributed File System
- http: HTTP and HTTPS protocols
local remote
remote 저장소는 dvc remote add 명령어로 설정 가능합니다. 프로젝트 디렉토리에 내 dataset을 관리할 디렉토리를 생성하고 local remote 저장소를 설정했습니다.
mkdir dataset
dvc remote add -d dataset /Users/jeoninah/dev/dvc-test-remote
remote 저장소 설정 후 .dvc/config 파일을 열어보면 다음과 같이 remote 저장소에 관한 설정 내용이 추가된 것을 확인할 수 있습니다.
['remote "dataset"']
url = /Users/jeoninah/dev/dvc-test-remote
[core]
remote = dataset
S3 remote
aws s3를 remote 저장소로 활용하는 경우 다음과 같이 설정할 수 있습니다.
dvc remote add -d dataset s3://mybucket/myproject
s3를 저장소로 이용하는 경우에는 boto3를 같이 설치해야합니다.
pip install boto3
또, 해당 s3 저장소를 접근하기 위한 AWS_ACCESS_KEY_ID와 AWS_SECRET_ACCESS_KEY를 환경변수에 설정해주어야 합니다.
5. Usecase
dvc 기능 테스트를 위해 dataset에 text.txt 라는 파일을 만들었습니다.
cat > dataset/text.txt
abcd
(1) dvc add
dvc도 git 처럼 add를 사용하여 변경사항을 시스템에 추가할 수 있습니다.
dvc add dataset
dvc로 add한 디렉토리나 파일들은 .gitignore에 추가되고 dvc에서 관리됩니다. git status로 dvc add 명령 전후의 상태를 비교해보면,
- add 하기 전:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .dvc/config
Untracked files:
(use "git add <file>..." to include in what will be committed)
dataset/
no changes added to commit (use "git add" and/or "git commit -a")
- add 한 이후:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .dvc/config
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
dataset.dvc
no changes added to commit (use "git add" and/or "git commit -a")
dataset이라는 디렉토리 대신, dataset.dvc라는 파일이 생성되고 .gitignore가 변경된 것을 확인할 수 있습니다.
cat .gitignore
/dataset
.gitignore 파일에 /dataset이 추가된 것을 확인할 수 있습니다.
(2) dvc push
remote 저장소에 push 하는 것도 git과 동일하게 아래 명령어로 push할 수 있습니다.
dvc push
Preparing to upload data to '../dvc-test-remote'
Preparing to collect status from ../dvc-test-remote
Collecting information from local cache...
100%|██████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00, 3584.88md5/s]
Collecting information from remote cache...
100%|██████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00, 7121.06md5/s]
Analysing status: 100%|██████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00, 19021.79file/s]
파일이 업로드 된 것을 확인할 수 있고, remote 저장소로 설정해두었던 dvc-test-remote 디렉토리를 보면 무언가 알 수없는(?) 디렉토리들이 생성된 것을 확인할 수 있었습니다.
ls dvc-test-remote
6e f5
(3) dvc pull
remote 저장소에 저장된 내용을 받아오는 것도 pull 명령어로 가능합니다.
dataset 디렉토리를 제거했다가 pull 로 다시 받아올 수 있었습니다.
rm -rf dataset
dvc pull
아예 새로운 곳에 데이터를 받아오고 싶을 경우, git 저장소를 clone해서 내용을 받아온 후 dvc pull로 데이터를 가져올 수 있습니다.
특정 file 또는 directory 만 pull 하고 싶은 경우, 해당 데이터를 관리하는 some-file.dvc (dvc add some-file 시 생성되는 dvc 파일)파일만 지정해서 pull 할 수 있습니다.
dvc pull some-dir.dvc
기타
위와 같은 버전 관리 기능 외에도 코드와 연결해서 데이터 파이프라인을 구성하거나 모델을 만들고 결과를 비교할 수 있는 experiment기능들도 있는데, 사용해보진 않았습니다.
느낀점
- training 코드를 git에서 관리할 때 사용했던 dataset과 모델들은 따로 README에서 링크를 걸어두었는데, 데이터셋과 모델을 따로 관리할 수 있는 유용한 툴인 것 같습니다.
- 익숙한 git과 동일한 인터페이스를 제공하고 있어서 사용하기도 매우 편리해서 앞으로 잘 활용해볼 것 같습니다.
