-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataloader.py
More file actions
156 lines (131 loc) · 4.84 KB
/
Copy pathdataloader.py
File metadata and controls
156 lines (131 loc) · 4.84 KB
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
import random
import numpy as np
import os
import torch
from torch.utils.data import Dataset, DataLoader, TensorDataset
from torch.autograd import Variable
#from sklearn.model_selection import train_test_split
import torchvision
import torchvision.transforms as transforms
from PIL import Image
import cv2
import matplotlib.pyplot as plt
import glob
class Dataload(Dataset):
def __init__(self, file_path, batch_size = 1, data_source = None, gray = False, image_shape = (224,224)
, same_matrix = True, num_require = 25,data_type = 'train'):
self.file_path = file_path
self.data_source = data_source
self.batch_size = batch_size
self.image_shape = image_shape
self.data_type = data_type
self.label_dict = {
0:'Bacillariophyta',
1:'Chlorella',
2:'Chrysophyta',
3:'Dunaliella_salina',
4:'Platymonas',
5:'translating_Symbiodinium',
6:'bleaching_Symbiodinium',
7:'normal_Symbiodinium}'
}
self.num_class = len(self.label_dict)
self.photo_set = []
self.same_matrix =same_matrix
self.gray = gray
self.num_require = num_require
self.load_data(file_path)
self.set_gan()
# self.X_train, self.Y_train = self.load_all_data(False ,gray, "train")
# self.X_val, self.Y_val = self.load_all_data(False ,gray, "val")
def check_dir(self, path):
if (not os.path.exists(path)):
return 0
return 1
def read_image_data(self, file_path, gray = False):
if(gray):
image = cv2.imread(file_path, 0)
else:
image = cv2.imread(file_path)
if(image is None):
raise RuntimeError('image can \'t read:' + file_path)
return image
def set_gan(self):
cifar_norm_mean = (0.49139968, 0.48215827, 0.44653124)
cifar_norm_std = (0.24703233, 0.24348505, 0.26158768)
method_list = [
transforms.ToPILImage(),
transforms.Resize(self.image_shape),
transforms.ToTensor(),
transforms.Normalize(cifar_norm_mean, cifar_norm_std),
]
self.datagen = transforms.Compose(method_list)
def load_data(self, file_path):
middle = file_path + "\\images\\"
label_path = file_path + "\\labels\\"
for i in os.listdir(middle):
num = os.path.splitext(i)[0]
x = middle + num + '.png'
y = label_path + num + '.txt'
self.photo_set.append([x,y])
if(not self.check_dir(middle)):
if(self.dataset_type == "train"):
raise RuntimeError('train dir not exists:'+ middle)
else:
raise RuntimeError('val dir not exists:' + middle)
self.total_number = len(self.photo_set)
print("total:", self.total_number)
def __getitem__(self, index):
"""
获取对应index的图像,并视情况进行数据增强
"""
if(index >= self.total_number):
raise StopIteration
try:
re_index = index
if(len(self.photo_set)>0):
image_path = self.photo_set[re_index][0]
image = self.read_image_data(image_path,self.gray)
label = []
if(self.data_type == "train"):
with open(self.photo_set[re_index][1]) as f:
lines = f.readlines()
for line in lines:
x = line.replace('\n', '').split(' ')
x = [float(i) for i in x]
label.append(x)
l = len(label)
if(self.same_matrix):
ones = np.zeros([self.num_require, 5])
ones[:,0] = self.num_class
ones[:l,:] =label
label = ones
else:
label = np.zeros([self.num_require, 5])
label = torch.tensor(label)
if self.datagen is not None:
image = self.datagen(image)
return image, label
except Exception as e:
print("发现异常")
print(e.__class__.__name__)
print(e)
def __len__(self):
return len(self.photo_set)
if __name__ == '__main__':
batch_size = 32
train_dataloader = Dataload(r"E:\Dataset\training_set\train")
train_loader= DataLoader(
dataset = train_dataloader,
batch_size = batch_size,
shuffle = False,
drop_last = True
)
# re_index, b= train_loader[500]
# print("photo")
# print(re_index)
# print("label")
# print(b)
for data in train_loader:
img,label =data
print(img.shape)