Skip to content

svhn_cropped

load_data

Load and return the SVHN Cropped digits dataset.

For more information about this dataset please visit http://ufldl.stanford.edu/housenumbers/. Here, we are using Format 2 to get MNIST-like 32-by-32 images.

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[NumpyDataset, NumpyDataset]

(train_data, test_data)

Source code in fastestimator/fastestimator/dataset/data/svhn_cropped.py
def load_data(root_dir: Optional[str] = None) -> Tuple[NumpyDataset, NumpyDataset]:
    """Load and return the SVHN Cropped digits dataset.

    For more information about this dataset please visit http://ufldl.stanford.edu/housenumbers/. Here, we are using
    Format 2 to get MNIST-like 32-by-32 images.

    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', 'SVHN_Cropped')
    else:
        root_dir = os.path.join(os.path.abspath(root_dir), 'SVHN_Cropped')
    os.makedirs(root_dir, exist_ok=True)

    # download data to memory
    train_path = os.path.join(root_dir, "train_32x32.mat")
    test_path = os.path.join(root_dir, "test_32x32.mat")

    if not os.path.exists(train_path):
        print("Downloading train data to {}".format(root_dir))
        wget.download('http://ufldl.stanford.edu/housenumbers/train_32x32.mat', root_dir, bar=bar_custom)

    if not os.path.exists(test_path):
        print("Downloading test data to {}".format(root_dir))
        wget.download('http://ufldl.stanford.edu/housenumbers/test_32x32.mat', root_dir, bar=bar_custom)

    xy_train = loadmat(train_path)
    xy_test = loadmat(test_path)

    # setting label of '0' digit from '10' to '0'
    xy_train['y'][xy_train['y'] == 10] = 0
    xy_test['y'][xy_test['y'] == 10] = 0

    # make datasets
    train_data = NumpyDataset({"x": np.transpose(xy_train['X'], (3, 0, 1, 2)), "y": xy_train['y']})
    test_data = NumpyDataset({"x": np.transpose(xy_test['X'], (3, 0, 1, 2)), "y": xy_test['y']})

    return train_data, test_data