在 GitHub 上编辑

修改大型数据集

对于由许多文件组成的大规模数据集,一次性对整个数据集进行操作可能会非常缓慢。相反,你可以仅对需要修改的文件进行操作。

细粒度修改

假设你有一个由 DVC 跟踪的、包含多个独立文件的数据集:

$ tree images
images
├── test
│   ├── 0
│   │   ├── 00004.png
│   │   ├── 00011.png
│   │   ├── 00014.png
│   │   ├── 00026.png
│   │   ├── 00029.png
│   │   ├── 00056.png
│   │   ├── 00070.png
...
└── images.dvc

23 directories, 70001 files

你可以使用 dvc add 命令将一个或多个新文件或子目录添加到该数据集中,而无需重新添加整个数据集。假设我们在数据集中新增了一个文件:

$ cp ~/Downloads/new.png images/test/0/70001.png

$ dvc data status --granular
DVC uncommitted changes:
  (use "dvc commit <file>..." to track changes)
  (use "dvc checkout <file>..." to discard changes)
        modified: images/
        added: images/test/0/70001.png

以新文件为目标运行 dvc add 命令:

$ dvc add images/test/0/70001.png
100% Adding...|████████████████████████████████████████|1/1 [00:00,  1.69file/s]

$ dvc data status --granular
DVC committed changes:
  (git commit the corresponding dvc files to update the repo)
        modified: images/
        added: images/test/0/70001.png
(there are other changes not tracked by dvc, use "git status" to see)

你也可以修改一个或多个现有文件或子目录。假设我们已覆盖了数据集中的一个文件:

$ cp ~/Downloads/updated.png images/test/0/00004.png

$ dvc data status --granular
DVC uncommitted changes:
  (use "dvc commit <file>..." to track changes)
  (use "dvc checkout <file>..." to discard changes)
        modified: images/
        modified: images/test/0/00004.png

$ dvc add images/test/0/00004.png
100% Adding...|████████████████████████████████████████|1/1 [00:00,  1.70file/s]

$ dvc data status --granular
DVC committed changes:
  (git commit the corresponding dvc files to update the repo)
        modified: images/
        modified: images/test/0/00004.png
(there are other changes not tracked by dvc, use "git status" to see)

最后,你可以通过在工作区中删除一个或多个文件或子目录,并将其指定为操作目标来完成删除操作。假设我们已在数据集中删除了一个文件:

$ rm images/test/0/00011.png

$ dvc data status --granular
DVC uncommitted changes:
  (use "dvc commit <file>..." to track changes)
  (use "dvc checkout <file>..." to discard changes)
        modified: images/
        deleted: images/test/0/00011.png

$ dvc add images/test/0/00011.png
100% Adding...|████████████████████████████████████████|1/1 [00:00,  1.73file/s]

$ dvc data status --granular
DVC committed changes:
  (git commit the corresponding dvc files to update the repo)
        modified: images/
        deleted: images/test/0/00011.png
(there are other changes not tracked by dvc, use "git status" to see)

这与执行 dvc add images/test/0 效果相同(或者指向被删除文件的任意上级目录)。目标越具体,操作速度越快。

修改远程数据集

如果你的数据集位于 远程存储 中但尚未下载到本地工作区,那么为了更新其中一两个文件而使用 dvc pull 下载整个数据集会很不方便。你可以改为仅拉取需要更新的文件:

$ tree
.
└── images.dvc

0 directories, 1 file

$ dvc pull images/test/0

参见 dvc ls 命令,用于列出项目中可拉取的文件。

然后根据需要修改这些文件,并记录更改:

$ cp ~/Downloads/new.png images/test/0/70001.png

$ dvc add images/test/0/70001.png
100% Adding...|████████████████████████████████████████|1/1 [00:00,  1.73file/s]

最后,你可以将更改推送回远程存储,而无需下载完整的数据集:

$ dvc push
2 files pushed

共推送了 2 个文件:新的文件和更新后的目录列表。你可以通过这种方式向远程数据集添加、修改和删除文件。

内容

🐛 发现问题?告诉我们!或者修复它:

在 GitHub 上编辑

有疑问?加入我们的聊天,我们会为您提供帮助:

Discord 聊天