-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreverse_mnist.py
59 lines (43 loc) · 1.45 KB
/
reverse_mnist.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
# @Author : Peizhao Li
# @Contact : [email protected]
from PIL import Image
import numpy as np
import torch
from torch.utils import data
from torchvision import transforms
kwargs = {"shuffle": True, "num_workers": 4, "pin_memory": True, "drop_last": True}
class digital(data.Dataset):
def __init__(self, subset, transform=None):
"""Load images with size 32x32
Args:
subset (str): What subset to load
transform (torchvision.transforms):
"""
file_dir = "./data/{}.txt".format(subset)
self.data_dir = open(file_dir).readlines()
self.transform = transform
def __getitem__(self, index):
img_dir, label = self.data_dir[index].split()
img = Image.open(img_dir)
img.show()
if self.transform is not None:
img = self.transform(img)
img = abs(img - 1.0)
label = torch.tensor(np.int64(label)).long()
return img, label
def __len__(self):
return len(self.data_dir)
def get_digital(args, subset):
transform = transforms.Compose([transforms.ToTensor(),])
data = digital(subset, transform)
data_loader = torch.utils.data.DataLoader(
dataset=data,
batch_size=args.bs,
**kwargs
)
return data_loader
def reverse_mnist_usps(args):
train_0 = get_digital(args, "train_mnist")
train_1 = get_digital(args, "train_usps")
train_data = [train_0, train_1]
return train_data