forked from fangpin/siamese-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmydataset.py
168 lines (145 loc) · 5.83 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
def loadPath(dataPath):
print("begin loading training data")
datas = {}
# agrees = [0, 90, 180, 270]
agrees = [0]
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)):
file_path = os.path.join(dataPath, alphaPath, charPath, samplePath)
datas[idx].append(file_path)
idx += 1
print("finish loading training dataset to memory")
return datas, idx
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]
agrees = [0]
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).resize((224, 224))) # .rotate(agree).convert('L'))
idx += 1
print("finish loading training dataset to memory")
return datas, idx
def __len__(self):
return 21000000
def __getitem__v2(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)
path_image1 = random.choice(self.datas[idx1])
path_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)
path_image1 = random.choice(self.datas[idx1])
path_image2 = random.choice(self.datas[idx2])
if self.transform:
image1 = self.transform(Image.open(path_image1).resize((224, 224)))
image2 = self.transform(Image.open(path_image2).resize((224, 224)))
return image1, image2, torch.from_numpy(np.array([label], dtype=np.float32))
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 = 0.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 = 1.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).resize((224, 224)))
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)