Repository: mindspore-lab/mindcv (language: python)

Issue:
[feat] add default download root instead of using current working dir
Thank you for your contribution to the MindCV repo.
Before submitting this PR, please make sure:

- [x] You have read the [Contributing Guidelines on pull requests](https://github.com/mindspore-lab/mindcv/blob/main/CONTRIBUTING.md)
- [x] Your code builds clean without any errors or warnings
- [x] You are using approved terminology
- [x] You have added unit tests

## Motivation

Set the default download root path as `~/.mindspore`(hereinafter referred to as `${DOWNLOAD_ROOT}`) instead of using the current working directory.
For different types of files, we place them in:
- Checkpoints of pretrained model: `${DOWNLOAD_ROOT}/models`
- Small datasets like mnist cifair10: `${DOWNLOAD_ROOT}/datasets`

We can change the default download path with function `set_default_download_root`, or explicitly pass in the download path when calling function `download_and_extract_archive` or `download_url`.

## Test Plan

Already in tests.

## Related Issues and PRs

None



Discussion / hints:

Fix patch:
diff --git a/examples/finetune.py b/examples/finetune.py
index 54c0a7228..1f3927bfb 100644
--- a/examples/finetune.py
+++ b/examples/finetune.py
@@ -8,7 +8,7 @@
 
 from mindspore import LossMonitor, Model, TimeMonitor
 
-from mindcv.data import create_dataset, create_loader, create_transforms
+from mindcv.data import create_dataset, create_loader, create_transforms, get_dataset_download_root
 from mindcv.loss import create_loss
 from mindcv.models import create_model
 from mindcv.optim import create_optimizer
@@ -20,16 +20,13 @@
 dataset_url = (
     "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/intermediate/Canidae_data.zip"
 )
-root_dir = "./"
-
-if not os.path.exists(os.path.join(root_dir, "data/Canidae")):
+root_dir = os.path.join(get_dataset_download_root(), "Canidae")
+data_dir = os.path.join(root_dir, "data", "Canidae")  # Canidae has prefix path "data/Canidae" in unzipped file.
+if not os.path.exists(data_dir):
     DownLoad().download_and_extract_archive(dataset_url, root_dir)
 
 num_workers = 8
 
-# 数据集目录路径
-data_dir = "./data/Canidae/"
-
 # 加载自定义数据集
 dataset_train = create_dataset(root=data_dir, split="train", num_parallel_workers=num_workers)
 dataset_val = create_dataset(root=data_dir, split="val", num_parallel_workers=num_workers)


Test patch:
diff --git a/tests/modules/test_dataset.py b/tests/modules/test_dataset.py
index 1c41a94ad..7fe368385 100644
--- a/tests/modules/test_dataset.py
+++ b/tests/modules/test_dataset.py
@@ -7,7 +7,7 @@
 
 import mindspore as ms
 
-from mindcv.data import create_dataset
+from mindcv.data import create_dataset, get_dataset_download_root
 from mindcv.utils.download import DownLoad
 
 
@@ -40,11 +40,10 @@ def test_create_dataset_standalone_imagenet(mode, name, split, shuffle, num_samp
     dataset_url = (
         "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/intermediate/Canidae_data.zip"
     )
-    root_dir = "./"
-
-    if not os.path.exists(os.path.join(root_dir, "data/Canidae")):
+    root_dir = os.path.join(get_dataset_download_root(), "Canidae")
+    data_dir = os.path.join(root_dir, "data", "Canidae")  # Canidae has prefix path "data/Canidae" in unzipped file.
+    if not os.path.exists(data_dir):
         DownLoad().download_and_extract_archive(dataset_url, root_dir)
-    data_dir = "./data/Canidae/"
     dataset = create_dataset(
         name=name,
         root=data_dir,
diff --git a/tests/modules/test_loader.py b/tests/modules/test_loader.py
index a22fb2b66..764b3b198 100644
--- a/tests/modules/test_loader.py
+++ b/tests/modules/test_loader.py
@@ -8,7 +8,7 @@
 import mindspore as ms
 from mindspore.dataset.transforms import OneHot
 
-from mindcv.data import create_dataset, create_loader
+from mindcv.data import create_dataset, create_loader, get_dataset_download_root
 from mindcv.utils.download import DownLoad
 
 num_classes = 1
@@ -56,15 +56,13 @@ def test_dataset_loader_standalone(
 
     ms.set_context(mode=mode)
     name = "ImageNet"
-    if name == "ImageNet":
-        dataset_url = (
-            "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/intermediate/Canidae_data.zip"
-        )
-        root_dir = "./"
-
-        if not os.path.exists(os.path.join(root_dir, "data/Canidae")):
-            DownLoad().download_and_extract_archive(dataset_url, root_dir)
-        data_dir = "./data/Canidae/"
+    dataset_url = (
+        "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/intermediate/Canidae_data.zip"
+    )
+    root_dir = os.path.join(get_dataset_download_root(), "Canidae")
+    data_dir = os.path.join(root_dir, "data", "Canidae")  # Canidae has prefix path "data/Canidae" in unzipped file.
+    if not os.path.exists(data_dir):
+        DownLoad().download_and_extract_archive(dataset_url, root_dir)
 
     dataset = create_dataset(
         name=name,
diff --git a/tests/modules/test_transforms.py b/tests/modules/test_transforms.py
index d522f1abc..46c563965 100644
--- a/tests/modules/test_transforms.py
+++ b/tests/modules/test_transforms.py
@@ -8,7 +8,7 @@
 
 import mindspore as ms
 
-from mindcv.data import create_dataset, create_loader, create_transforms
+from mindcv.data import create_dataset, create_loader, create_transforms, get_dataset_download_root
 from mindcv.utils.download import DownLoad
 
 
@@ -33,11 +33,11 @@ def test_transforms_standalone_imagenet(mode, name, image_resize, is_training):
     dataset_url = (
         "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/intermediate/Canidae_data.zip"
     )
-    root_dir = "./"
-
-    if not os.path.exists(os.path.join(root_dir, "data/Canidae")):
+    root_dir = os.path.join(get_dataset_download_root(), "Canidae")
+    data_dir = os.path.join(root_dir, "data", "Canidae")  # Canidae has prefix path "data/Canidae" in unzipped file.
+    if not os.path.exists(data_dir):
         DownLoad().download_and_extract_archive(dataset_url, root_dir)
-    data_dir = "./data/Canidae/"
+
     dataset = create_dataset(
         name=name,
         root=data_dir,
@@ -170,20 +170,14 @@ def test_repeated_aug():
         rank_id = 0
 
     name = "imagenet"
-    """
-    data_dir = '/data/imagenette2-320'
-    num_classes = 10
-    """
     dataset_url = (
         "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/intermediate/Canidae_data.zip"
     )
-    root_dir = "./"
-
-    if not os.path.exists(os.path.join(root_dir, "data/Canidae")):
+    root_dir = os.path.join(get_dataset_download_root(), "Canidae")
+    data_dir = os.path.join(root_dir, "data", "Canidae")  # Canidae has prefix path "data/Canidae" in unzipped file.
+    if not os.path.exists(data_dir):
         DownLoad().download_and_extract_archive(dataset_url, root_dir)
-    data_dir = "./data/Canidae/"
     num_classes = 2
-
     num_aug_repeats = 3
 
     dataset = create_dataset(
diff --git a/tests/tasks/non_cpu/test_train_val_imagenet_subset.py b/tests/tasks/non_cpu/test_train_val_imagenet_subset.py
index 9d53bca96..6ef086f11 100644
--- a/tests/tasks/non_cpu/test_train_val_imagenet_subset.py
+++ b/tests/tasks/non_cpu/test_train_val_imagenet_subset.py
@@ -10,6 +10,7 @@
 
 import pytest
 
+from mindcv.data import get_dataset_download_root
 from mindcv.utils.download import DownLoad
 
 check_acc = True
@@ -20,16 +21,17 @@
 def test_train_ema(ema, val_while_train, model="resnet18"):
     """train on a imagenet subset dataset"""
     # prepare data
-    data_dir = "data/Canidae"
-    num_classes = 2
     dataset_url = (
         "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/intermediate/Canidae_data.zip"
     )
+    root_dir = os.path.join(get_dataset_download_root(), "Canidae")
+    data_dir = os.path.join(root_dir, "data", "Canidae")  # Canidae has prefix path "data/Canidae" in unzipped file.
     if not os.path.exists(data_dir):
-        DownLoad().download_and_extract_archive(dataset_url, "./")
+        DownLoad().download_and_extract_archive(dataset_url, root_dir)
 
     # ---------------- test running train.py using the toy data ---------
     dataset = "imagenet"
+    num_classes = 2
     ckpt_dir = "./tests/ckpt_tmp"
     num_samples = 160
     num_epochs = 5
diff --git a/tests/tasks/test_train_mnist.py b/tests/tasks/test_train_mnist.py
index aa4804090..0faeb9486 100644
--- a/tests/tasks/test_train_mnist.py
+++ b/tests/tasks/test_train_mnist.py
@@ -1,4 +1,3 @@
-import os
 import sys
 
 sys.path.append(".")
@@ -28,18 +27,11 @@ def test_train_mnist(mode):
     set_sink_mode = True  # noqa: F841
 
     dataset_name = "mnist"
-    data_dir = "./datasets/mnist"
     model_name = "resnet18"
     scheduler_name = "constant"
     lr = 1e-3
     loss_name = "CE"
     opt_name = "adam"
-    # ckpt_save_dir = './tests/ckpt_tmp'
-
-    if not os.path.exists(data_dir):
-        download = True
-    else:
-        download = False
 
     ms.set_seed(1)
     ms.set_context(mode=mode)
@@ -49,11 +41,10 @@ def test_train_mnist(mode):
 
     dataset_train = create_dataset(
         name=dataset_name,
-        root=data_dir,
         num_samples=100,
         num_shards=device_num,
         shard_id=rank_id,
-        download=download,
+        download=True,
     )
 
     transform_train = create_transforms(dataset_name=dataset_name)
diff --git a/tests/tasks/test_train_val_imagenet_subset.py b/tests/tasks/test_train_val_imagenet_subset.py
index a7299f958..78ecda258 100644
--- a/tests/tasks/test_train_val_imagenet_subset.py
+++ b/tests/tasks/test_train_val_imagenet_subset.py
@@ -10,6 +10,7 @@
 
 import pytest
 
+from mindcv.data import get_dataset_download_root
 from mindcv.utils.download import DownLoad
 
 check_acc = True
@@ -20,16 +21,17 @@
 def test_train(mode, val_while_train, model="resnet18"):
     """train on a imagenet subset dataset"""
     # prepare data
-    data_dir = "data/Canidae"
-    num_classes = 2
     dataset_url = (
         "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/intermediate/Canidae_data.zip"
     )
+    root_dir = os.path.join(get_dataset_download_root(), "Canidae")
+    data_dir = os.path.join(root_dir, "data", "Canidae")  # Canidae has prefix path "data/Canidae" in unzipped file.
     if not os.path.exists(data_dir):
-        DownLoad().download_and_extract_archive(dataset_url, "./")
+        DownLoad().download_and_extract_archive(dataset_url, root_dir)
 
     # ---------------- test running train.py using the toy data ---------
     dataset = "imagenet"
+    num_classes = 2
     ckpt_dir = "./tests/ckpt_tmp"
     num_samples = 160
     num_epochs = 5