Skip to content

office31

Office31

Bases: BaseDownloadableDataset

A custom train/test split of Office31Full.

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

Source code in pytorch_adapt\datasets\office31.py
37
38
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
class Office31(BaseDownloadableDataset):
    """
    A custom train/test split of [Office31Full][pytorch_adapt.datasets.Office31Full].

    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/3v2ftdkdhpz1lbbr4uhu0135w7m79p7q"
    filename = "office31.tar.gz"
    md5 = "89818e596f3cdda1d56da0f077435faa"

    def __init__(self, root: str, domain: str, train: bool, transform=None, **kwargs):
        """
        Arguments:
            root: The dataset must be located at ```<root>/office31```
            domain: One of ```"amazon", "dslr", "webcam"```.
            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, "office31", f"{self.domain}_{name}.txt")
        img_dir = os.path.join(root, "office31")

        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,
            {
                "amazon": {"train": 2253, "test": 564}[name],
                "dslr": {"train": 398, "test": 100}[name],
                "webcam": {"train": 636, "test": 159}[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>/office31

required
domain str

One of "amazon", "dslr", "webcam".

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\office31.py
50
51
52
53
54
55
56
57
58
59
60
def __init__(self, root: str, domain: str, train: bool, transform=None, **kwargs):
    """
    Arguments:
        root: The dataset must be located at ```<root>/office31```
        domain: One of ```"amazon", "dslr", "webcam"```.
        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

Office31Full

Bases: BaseDataset

A small dataset consisting of 31 classes in 3 domains: amazon, dslr, webcam.

Source code in pytorch_adapt\datasets\office31.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
class Office31Full(BaseDataset):
    """
    A small dataset consisting of 31 classes in 3 domains:
    amazon, dslr, webcam.
    """

    def __init__(self, root: str, domain: str, transform):
        """
        Arguments:
            root: The dataset must be located at ```<root>/office31```
            domain: One of ```"amazon", "dslr", "webcam"```.
            transform: The image transform applied to each sample.
        """

        super().__init__(domain=domain)
        self.transform = transform
        self.dataset = torch_datasets.ImageFolder(
            os.path.join(root, "office31", domain, "images"), transform=self.transform
        )
        check_length(self, {"amazon": 2817, "dslr": 498, "webcam": 795}[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>/office31

required
domain str

One of "amazon", "dslr", "webcam".

required
transform

The image transform applied to each sample.

required
Source code in pytorch_adapt\datasets\office31.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def __init__(self, root: str, domain: str, transform):
    """
    Arguments:
        root: The dataset must be located at ```<root>/office31```
        domain: One of ```"amazon", "dslr", "webcam"```.
        transform: The image transform applied to each sample.
    """

    super().__init__(domain=domain)
    self.transform = transform
    self.dataset = torch_datasets.ImageFolder(
        os.path.join(root, "office31", domain, "images"), transform=self.transform
    )
    check_length(self, {"amazon": 2817, "dslr": 498, "webcam": 795}[domain])