forked from fangpin/siamese-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydataset.py
120 lines (104 loc) · 4.12 KB
/
mydataset.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import torch
from torch.utils.data import Dataset, DataLoader
import os
from numpy.random import choice as npc
import numpy as np
import time
import random
import torchvision.datasets as dset
from PIL import Image
class OmniglotTrain(Dataset):
def __init__(self, dataPath, transform=None):
super(OmniglotTrain, self).__init__()
np.random.seed(0)
# self.dataset = dataset
self.transform = transform
self.datas, self.num_classes = self.loadToMem(dataPath)
def loadToMem(self, dataPath):
print("begin loading training dataset to memory")
datas = {}
agrees = [0, 90, 180, 270]
idx = 0
for agree in agrees:
for alphaPath in os.listdir(dataPath):
for charPath in os.listdir(os.path.join(dataPath, alphaPath)):
datas[idx] = []
for samplePath in os.listdir(os.path.join(dataPath, alphaPath, charPath)):
filePath = os.path.join(dataPath, alphaPath, charPath, samplePath)
datas[idx].append(Image.open(filePath).rotate(agree).convert('L'))
idx += 1
print("finish loading training dataset to memory")
return datas, idx
def __len__(self):
return 21000000
def __getitem__(self, index):
# image1 = random.choice(self.dataset.imgs)
label = None
img1 = None
img2 = None
# get image from same class
if index % 2 == 1:
label = 1.0
idx1 = random.randint(0, self.num_classes - 1)
image1 = random.choice(self.datas[idx1])
image2 = random.choice(self.datas[idx1])
# get image from different class
else:
label = 0.0
idx1 = random.randint(0, self.num_classes - 1)
idx2 = random.randint(0, self.num_classes - 1)
while idx1 == idx2:
idx2 = random.randint(0, self.num_classes - 1)
image1 = random.choice(self.datas[idx1])
image2 = random.choice(self.datas[idx2])
if self.transform:
image1 = self.transform(image1)
image2 = self.transform(image2)
return image1, image2, torch.from_numpy(np.array([label], dtype=np.float32))
class OmniglotTest(Dataset):
def __init__(self, dataPath, transform=None, times=200, way=20):
np.random.seed(1)
super(OmniglotTest, self).__init__()
self.transform = transform
self.times = times
self.way = way
self.img1 = None
self.c1 = None
self.datas, self.num_classes = self.loadToMem(dataPath)
def loadToMem(self, dataPath):
print("begin loading test dataset to memory")
datas = {}
idx = 0
for alphaPath in os.listdir(dataPath):
for charPath in os.listdir(os.path.join(dataPath, alphaPath)):
datas[idx] = []
for samplePath in os.listdir(os.path.join(dataPath, alphaPath, charPath)):
filePath = os.path.join(dataPath, alphaPath, charPath, samplePath)
datas[idx].append(Image.open(filePath).convert('L'))
idx += 1
print("finish loading test dataset to memory")
return datas, idx
def __len__(self):
return self.times * self.way
def __getitem__(self, index):
idx = index % self.way
label = None
# generate image pair from same class
if idx == 0:
self.c1 = random.randint(0, self.num_classes - 1)
self.img1 = random.choice(self.datas[self.c1])
img2 = random.choice(self.datas[self.c1])
# generate image pair from different class
else:
c2 = random.randint(0, self.num_classes - 1)
while self.c1 == c2:
c2 = random.randint(0, self.num_classes - 1)
img2 = random.choice(self.datas[c2])
if self.transform:
img1 = self.transform(self.img1)
img2 = self.transform(img2)
return img1, img2
# test
if __name__=='__main__':
omniglotTrain = OmniglotTrain('./images_background', 30000*8)
print(omniglotTrain)