設定與初始化 DVC

使用 DVC 進行資料版本控管入門

Ravi Bhadauria

Machine Learning Engineer

安裝

  • DVC 是一個 Python 套件
    • pip 通用安裝
$ pip install dvc
  • 記得在虛擬環境中安裝
  • 確認已安裝 Git
使用 DVC 進行資料版本控管入門

驗證安裝

$ dvc version
DVC version: 3.40.1 (pip)

Platform: Python 3.9.16 on macOS-14.2.1-arm64-arm-64bit
Config: Global: /Users/<username>/Library/Application Support/dvc System: /Library/Application Support/dvc
Repo: dvc, git
1 https://dvc.org/doc/command-reference/version
使用 DVC 進行資料版本控管入門

初始化 DVC

  • 確認已初始化 Git
$ git init
Initialized empty Git repository in /path/to/repo/.git/
  • 在此版本庫中初始化 DVC
$ dvc init
Initialized DVC repository.

You can now commit the changes to git.
1 https://dvc.org/doc/command-reference/init
使用 DVC 進行資料版本控管入門

DVC 隱藏檔案

  • 初始化會建立內部檔案,應納入 Git 版本控管
$ git status
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   .dvc/.gitignore
    new file:   .dvc/config
    new file:   .dvcignore
  • 提交這些變更
$ git commit -m "initialized dvc"
使用 DVC 進行資料版本控管入門

.dvcignore 檔

  • 類似 .gitignore

    • 遵循相同規則
    • 列出 DVC 要忽略的檔案/目錄
  • 當有許多不需追蹤的資料檔時很實用

    • 可加快 DVC 操作執行時間
1 https://dvc.org/doc/user-guide/project-structure/dvcignore-files 2 https://git-scm.com/docs/gitignore
使用 DVC 進行資料版本控管入門

範例

# .dvcignore
# 忽略 'data' 目錄中的所有檔案
data/*

# 但不要忽略 'data/data.csv' !data/data.csv
# 忽略所有 .tmp 檔案 *.tmp
1 https://dvc.org/doc/user-guide/project-structure/dvcignore-files
使用 DVC 進行資料版本控管入門

檢查被忽略的檔案

  • 使用 dvc check-ignore 指令
$ dvc check-ignore data/file.txt
data/file.txt
  • 加上 -d 旗標可查看詳細資訊
$ dvc check-ignore -d data/file.txt
.dvcignore:3:data/*    data/file.txt
1 https://dvc.org/doc/command-reference/check-ignore
使用 DVC 進行資料版本控管入門

重點整理

  • pip install dvc 安裝 DVC
  • 驗證 DVC 的版本與平台等資訊
    • dvc version
  • 在工作目錄初始化 DVC
    • dvc init
    • 先初始化 Git
  • .dvcignore 指定排除的檔案
    • .gitignore 類似,語法相同
    • 檢查特定檔案是否被排除
      • dvc check-ignore <filename>
使用 DVC 進行資料版本控管入門

一起來練習吧!

使用 DVC 進行資料版本控管入門

Preparing Video For Download...