Skip to content

usps

load_data

Load and return the USPS dataset.

Parameters:

Name Type Description Default
root_dir Optional[str]

The path to store the downloaded data. When path is not provided, the data will be saved into fastestimator_data under the user's home directory.

None

Returns:

Type Description
Tuple[LabeledDirDataset, LabeledDirDataset]

(train_data, test_data)

Source code in fastestimator\fastestimator\dataset\data\usps.py
def load_data(root_dir: Optional[str] = None) -> Tuple[LabeledDirDataset, LabeledDirDataset]:
    """Load and return the USPS dataset.

    Args:
        root_dir: The path to store the downloaded data. When `path` is not provided, the data will be saved into
            `fastestimator_data` under the user's home directory.

    Returns:
        (train_data, test_data)
    """
    home = str(Path.home())

    if root_dir is None:
        root_dir = os.path.join(home, 'fastestimator_data', 'USPS')
    else:
        root_dir = os.path.join(os.path.abspath(root_dir), 'USPS')
    os.makedirs(root_dir, exist_ok=True)

    # download data to memory
    train_compressed_path = os.path.join(root_dir, "zip.train.gz")
    test_compressed_path = os.path.join(root_dir, "zip.test.gz")
    train_base_path = os.path.join(root_dir, "train")
    test_base_path = os.path.join(root_dir, "test")

    if not os.path.exists(train_base_path):
        if not os.path.exists(train_compressed_path):
            print("Downloading train data to {}".format(root_dir))
            wget.download('http://statweb.stanford.edu/~tibs/ElemStatLearn/datasets/zip.train.gz',
                          root_dir,
                          bar=bar_custom)
        train_images, train_labels = _extract_images_labels(train_compressed_path)
        _write_data(train_images, train_labels, train_base_path, "train")

    if not os.path.exists(test_base_path):
        if not os.path.exists(test_compressed_path):
            print("Downloading test data to {}".format(root_dir))
            wget.download('http://statweb.stanford.edu/~tibs/ElemStatLearn/datasets/zip.test.gz',
                          root_dir,
                          bar=bar_custom)
        test_images, test_labels = _extract_images_labels(test_compressed_path)
        _write_data(test_images, test_labels, test_base_path, "test")

    # make datasets
    return LabeledDirDataset(train_base_path, file_extension=".png"), LabeledDirDataset(test_base_path,
                                                                                        file_extension=".png")