-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_DataLoader.py
89 lines (86 loc) · 2.6 KB
/
05_DataLoader.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
import numpy as np
class dataset:
def __init__(self,dataset):
self.dataset = dataset
self.cursor = 0
def __len__(self):
return len(self.dataset)
def __getitem__(self,item):
image,label = self.dataset[item]
return image,label
# def __iter__(self):
# return self
# def __next__(self):
# if self.cursor<len(self.dataset):
# num = self.dataset[self.cursor]
# self.cursor +=1
# else:
# self.cursor=0
# raise StopIteration
# return num
class DataLoader:
def __init__(self,data,batch):
self.data = data.dataset
self.batch = batch
self.cursor = 0
def __iter__(self):
self.indexes = list(range(len(self.data)))
np.random.shuffle(self.indexes)
return self
def __next__(self):
begin = self.cursor
end = self.cursor + self.batch
if end <= len(self.data):
batch_data = self.data[begin:end]
self.cursor += self.batch
else:
self.cursor = 0
raise StopIteration
temp_batch = []
for index in self.indexes[begin:end]:
item = self.data[index]
temp_batch.append(item)
return temp_batch
#缝合dataset和 dataloader,没卵用
# class Monster:
# def __init__(self,data,batch):
# self.dataset = data
# self.batch = batch
# self.cursor = 0
#
# def __len__(self):
# return len(self.dataset)
# def __getitem__(self, item):
# image, label = self.dataset[item]
# return image, label
# def __iter__(self):
# self.indexes = list(range(len(self.dataset)))
# np.random.shuffle(self.indexes)
# return self
# def __next__(self):
# begin = self.cursor
# end = self.cursor + self.batch
# if end <= len(self.dataset):
# batch_data = self.dataset[begin:end]
# self.cursor += self.batch
# else:
# self.cursor = 0
# raise StopIteration()
# temp_batch = []
# for index in self.indexes[begin:end]:
# # print(index)
# item = self.dataset[index]
# temp_batch.append(item)
# return temp_batch
# images = [[f"images {i}",i] for i in range(10)]
# batch_size = 2
# monster = Monster(images,batch_size)
# for item in monster:
# print(item)
# print(images)
# images = [[f"images {i}",i] for i in range(10)]
# batch_size = 2
# dataset = dataset(images)
# DataLoader = DataLoader(dataset,batch_size)
# for item in DataLoader:
# print(item)