-
Notifications
You must be signed in to change notification settings - Fork 87
/
datasets.py
78 lines (70 loc) · 2.63 KB
/
datasets.py
1
2
3
4
5
6
7
8
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
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
# -*- coding: utf-8 -*-
# @Date : 2019-07-25
# @Author : Xinyu Gong ([email protected])
# @Link : None
# @Version : 0.0
import torch
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import Dataset
class ImageDataset(object):
def __init__(self, args, cur_img_size=None):
img_size = cur_img_size if cur_img_size else args.img_size
if args.dataset.lower() == "cifar10":
Dt = datasets.CIFAR10
transform = transforms.Compose(
[
transforms.Resize(img_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]
)
args.n_classes = 10
elif args.dataset.lower() == "stl10":
Dt = datasets.STL10
transform = transforms.Compose(
[
transforms.Resize(img_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]
)
else:
raise NotImplementedError("Unknown dataset: {}".format(args.dataset))
if args.dataset.lower() == "stl10":
self.train = torch.utils.data.DataLoader(
Dt(
root=args.data_path,
split="train+unlabeled",
transform=transform,
download=True,
),
batch_size=args.dis_batch_size,
shuffle=True,
num_workers=args.num_workers,
pin_memory=True,
)
self.valid = torch.utils.data.DataLoader(
Dt(root=args.data_path, split="test", transform=transform),
batch_size=args.dis_batch_size,
shuffle=False,
num_workers=args.num_workers,
pin_memory=True,
)
self.test = self.valid
else:
self.train = torch.utils.data.DataLoader(
Dt(root=args.data_path, train=True, transform=transform, download=True),
batch_size=args.dis_batch_size,
shuffle=True,
num_workers=args.num_workers,
pin_memory=True,
)
self.valid = torch.utils.data.DataLoader(
Dt(root=args.data_path, train=False, transform=transform),
batch_size=args.dis_batch_size,
shuffle=False,
num_workers=args.num_workers,
pin_memory=True,
)
self.test = self.valid