Skip to content

officehome

OfficeHome

Bases: BaseDownloadableDataset

A custom train/test split of OfficeHomeFull.

Extends BaseDownloadableDataset, so the dataset can be downloaded by setting download=True when initializing.

Source code in pytorch_adapt\datasets\officehome.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class OfficeHome(BaseDownloadableDataset):
    """
    A custom train/test split of [OfficeHomeFull][pytorch_adapt.datasets.OfficeHomeFull].

    Extends [BaseDownloadableDataset][pytorch_adapt.datasets.BaseDownloadableDataset],
    so the dataset can be downloaded by setting ```download=True``` when
    initializing.
    """

    url = "https://cornell.box.com/shared/static/xwsbubtcr8flqfuds5f6okqbr3z0w82t"
    filename = "officehome_resized.tar.gz"
    md5 = "52d6039512434aa561d66de9c10828c3"

    def __init__(self, root: str, domain: str, train: bool, transform=None, **kwargs):
        """
        Arguments:
            root: The dataset must be located at ```<root>/officehome```
            domain: One of ```"art", "clipart", "product", "real"```.
            train: Whether or not to use the training set.
            transform: The image transform applied to each sample.
        """
        self.train = check_train(train)
        super().__init__(root=root, domain=domain, **kwargs)
        self.transform = transform

    def set_paths_and_labels(self, root):
        name = "train" if self.train else "test"
        labels_file = os.path.join(root, "officehome", f"{self.domain}_{name}.txt")
        img_dir = os.path.join(root, "officehome")

        with open(labels_file) as f:
            content = [line.rstrip().split(" ") for line in f]
        self.img_paths = [os.path.join(img_dir, x[0]) for x in content]
        check_img_paths(img_dir, self.img_paths, self.domain)
        check_length(
            self,
            {
                "art": {"train": 1941, "test": 486}[name],
                "clipart": {"train": 3492, "test": 873}[name],
                "product": {"train": 3551, "test": 888}[name],
                "real": {"train": 3485, "test": 872}[name],
            }[self.domain],
        )
        self.labels = [int(x[1]) for x in content]

__init__(root, domain, train, transform=None, **kwargs)

Parameters:

Name Type Description Default
root str

The dataset must be located at <root>/officehome

required
domain str

One of "art", "clipart", "product", "real".

required
train bool

Whether or not to use the training set.

required
transform

The image transform applied to each sample.

None
Source code in pytorch_adapt\datasets\officehome.py
52
53
54
55
56
57
58
59
60
61
62
def __init__(self, root: str, domain: str, train: bool, transform=None, **kwargs):
    """
    Arguments:
        root: The dataset must be located at ```<root>/officehome```
        domain: One of ```"art", "clipart", "product", "real"```.
        train: Whether or not to use the training set.
        transform: The image transform applied to each sample.
    """
    self.train = check_train(train)
    super().__init__(root=root, domain=domain, **kwargs)
    self.transform = transform

OfficeHomeFull

Bases: BaseDataset

A dataset consisting of 65 classes in 4 domains: art, clipart, product, and real.

Source code in pytorch_adapt\datasets\officehome.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class OfficeHomeFull(BaseDataset):
    """
    A dataset consisting of 65 classes in 4 domains:
    art, clipart, product, and real.
    """

    def __init__(self, root: str, domain: str, transform):
        """
        Arguments:
            root: The dataset must be located at ```<root>/officehome```
            domain: One of ```"art", "clipart", "product", "real"```.
            transform: The image transform applied to each sample.
        """
        super().__init__(domain=domain)
        self.transform = transform
        self.dataset = torch_datasets.ImageFolder(
            os.path.join(root, "officehome", domain),
            transform=self.transform,
        )
        check_length(
            self, {"art": 2427, "clipart": 4365, "product": 4439, "real": 4357}[domain]
        )

    def __len__(self):
        return len(self.dataset)

    def __getitem__(self, idx):
        return self.dataset[idx]

__init__(root, domain, transform)

Parameters:

Name Type Description Default
root str

The dataset must be located at <root>/officehome

required
domain str

One of "art", "clipart", "product", "real".

required
transform

The image transform applied to each sample.

required
Source code in pytorch_adapt\datasets\officehome.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def __init__(self, root: str, domain: str, transform):
    """
    Arguments:
        root: The dataset must be located at ```<root>/officehome```
        domain: One of ```"art", "clipart", "product", "real"```.
        transform: The image transform applied to each sample.
    """
    super().__init__(domain=domain)
    self.transform = transform
    self.dataset = torch_datasets.ImageFolder(
        os.path.join(root, "officehome", domain),
        transform=self.transform,
    )
    check_length(
        self, {"art": 2427, "clipart": 4365, "product": 4439, "real": 4357}[domain]
    )