-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_dataset.py
221 lines (168 loc) · 6.14 KB
/
make_dataset.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import cv2
import numpy as np
import pandas as pd
import os
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
# 读取训练数据并进行数据增强
# path: 数据集路径
# label:是否需要返回label
def readfile_add(path, label):
# 读取文件夹下的文件
shadow_dir = sorted(os.listdir(path + "shadow/"))
# 装数据增强后的原图的标签
x_list = []
y_list = []
# 进行数据增强
for _, file in enumerate(shadow_dir):
img = cv2.imread(os.path.join(path, "shadow/", file))
mask = cv2.imread(os.path.join(path, "mask/", file), 0)
# print(os.path.join(path, "shadow/", file))
# print(os.path.join(path, "mask/", file))
# print(img.shape)
# print(mask.shape)
# 数据增强 shape是长宽高, size是元素个数
if img.shape[0] > 256:
# print(img.shape[0], "> 256")
stride = int((img.shape[0] - 256) / 64) + 1
# data_num += (stride)*(stride)
# print((stride)*(stride))
for i in range(stride):
for j in range(stride):
# 左右边界和上下边界
x1 = 64 * i
x2 = x1 + 256
y1 = 64 * j
y2 = y1 + 256
# 如果超过了边界
if x2 >= img.shape[0]:
x1 = img.shape[0] - 256
x2 = img.shape[0]
if y2 >= img.shape[0]:
y1 = img.shape[0] - 256
y2 = img.shape[0]
sub_img = img[x1:x2, y1:y2, :]
sub_mask = mask[x1:x2, y1:y2]
x_list.append(sub_img)
y_list.append(sub_mask)
# cv2.imshow("all", img)
# cv2.imshow("sub", sub_img)
# cv2.waitKey(0)
if img.shape[0] == 256:
# print(img.shape[0], "= 256")
x_list.append(img)
y_list.append(mask)
print("训练数据增强后共:", x_list.__len__(), "张图片")
print("原始数据切片形状:", x_list[0].shape)
print("标签数据切片形状:", y_list[0].shape)
if label == True:
return x_list, y_list
return x_list
# 读取数据并进行数据增强
# path: 数据集路径
# label:是否需要返回label
def readfile(path, label):
# 读取文件夹下的文件
shadow_dir = sorted(os.listdir(path + "shadow/"))
# 装数据增强后的原图的标签
x_list = []
y_list = []
# 进行数据增强
for _, file in enumerate(shadow_dir):
img = cv2.imread(os.path.join(path, "shadow/", file))
mask = cv2.imread(os.path.join(path, "mask/", file), 0)
# print(os.path.join(path, "shadow/", file))
# print(os.path.join(path, "mask/", file))
# print(img.shape)
# print(mask.shape)
# 数据增强 shape是长宽高, size是元素个数
if img.shape[0] > 256:
# print(img.shape[0], "> 256")
stride = int((img.shape[0]-256)/64) + 1
# data_num += (stride)*(stride)
# print((stride)*(stride))
for i in range(stride):
for j in range(stride):
# 左右边界和上下边界
x1 = 64 * i
x2 = x1 + 256
y1 = 64 * j
y2 = y1 + 256
# 如果超过了边界
if x2 >= img.shape[0]:
x1 = img.shape[0] - 256
x2 = img.shape[0]
if y2 >= img.shape[0]:
y1 = img.shape[0] - 256
y2 = img.shape[0]
sub_img = img[x1:x2, y1:y2, :]
sub_mask = mask[x1:x2, y1:y2]
x_list.append(sub_img)
y_list.append(sub_mask)
# cv2.imshow("all", img)
# cv2.imshow("sub", sub_img)
#cv2.waitKey(0)
if img.shape[0] == 256:
# print(img.shape[0], "= 256")
x_list.append(img)
y_list.append(mask)
print("验证数据切片后共:", x_list.__len__(), "张图片")
print("原始数据切片形状:", x_list[0].shape)
print("标签数据切片形状:", y_list[0].shape)
# print(x_list[11835].shape)
# print(y_list[11835].shape)
# cv2.imshow("z1", x_list[11832])
# cv2.imshow("z2", x_list[11833])
# cv2.imshow("z3", x_list[11834])
# cv2.imshow("z4", x_list[11835])
# cv2.imshow("z5", y_list[11832])
# cv2.imshow("z6", y_list[11833])
# cv2.imshow("z7", y_list[11834])
# cv2.imshow("z8", y_list[11835])
# cv2.waitKey(0)
if label == True:
return x_list, y_list
return x_list
# HWC->CHW且进行了归一化
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor()
])
# # 构建数据集:继承Dataset类 重写__len__以及__getitem__方法
class ImgDataset(Dataset):
def __init__(self, x, y=None):
self.x = x
self.y = y
def __len__(self):
return len(self.x)
def __getitem__(self, index):
X = self.x[index]
X = transform(X)
if self.y is not None:
Y = self.y[index]
Y = transform(Y)
Y = torch.LongTensor(Y[0].numpy())
# Y = torch.LongTensor(Y)
return X, Y
return X
if __name__ == '__main__':
x, y = readfile("./AISD/Train412/", True)
dataloader = ImgDataset(x, y)
# print(dataloader.__len__())
x = x[11835]
x = transform(x)
y = y[11835]
print(y.shape)
y = transform(y)
y=y[0]
# print(x)
# x = x.transpose(0, 2).transpose(0, 1)
# x = np.array(x*255, dtype=np.uint8)
#
#
# print(y)
print(y.shape)
# cv2.imshow("x", y)
cv2.waitKey(0)