-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_dataset.py
182 lines (168 loc) · 7.22 KB
/
create_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
import os
import glob
import random
import cv2
import numpy as np
from PIL import Image
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
class ISBI_Loader(Dataset):
def __init__(self, data_path):
self.data_path = data_path #dataset/images
self.dir_list = os.listdir(self.data_path) #dataset/images/*
print(self.dir_list)
self.img_list = []
for dir in self.dir_list:
print(dir)
self.img_list = self.img_list + glob.glob(os.path.join(self.data_path, dir, '*.png')) #dataset/images/xxxx/*.png
def augment(self, image, flipCode):
# 使用cv2.flip进行数据增强,filpCode为1水平翻转,0垂直翻转,-1水平+垂直翻转
flip = cv2.flip(image, flipCode)
return flip
def __getitem__(self, index):
# 根据index读取图片
image_path = self.img_list[index]
print(image_path)
# 根据image_path生成label_path
label_path = image_path.replace('images', 'label')
# 读取训练图片和标签图片
image = cv2.imread(image_path)
label = cv2.imread(label_path)
# 将数据转为单通道的图片
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
label = cv2.cvtColor(label, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (534, 534))
label = cv2.resize(label, (534, 534))
# img.reshape(1,534,534)
# label.reshape(1,534,534)
print(image.shape)
print(label.shape)
# 处理标签,将像素值为255的改为1
if label.max() > 1:
label = label / 255
# 随机进行数据增强,为2时不做处理
flipCode = random.choice([-1, 0, 1, 2])
if flipCode != 2:
image = self.augment(image, flipCode)
label = self.augment(label, flipCode)
return image, label
def __len__(self):
return len(self.img_list)
def create_dataset():
if not os.path.exists('dataset'):
os.makedirs('dataset')
os.makedirs('dataset/images')
os.makedirs('dataset/label')
# x_train = np.array( [] )
x_train = []
# x_label = np.array( [] )
x_label = []
target_size = (534,534,4)
target_size_label = (534,534)
for file in glob.glob('D:\\OS\\Unet\\dataset\\images\\*'):
print(file)
for file_name in glob.glob(file+'\\*'):
#
label_path = file_name.replace('images','label')
image_path = file_name
#读取训练图片和标签
img = cv2.imread(image_path)
label = cv2.imread(label_path)
#将数据转为单通道
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
label = cv2.cvtColor(label, cv2.COLOR_BGR2GRAY)
#将数据缩放为534x534
img = cv2.resize(img, (534, 534))
label = cv2.resize(label, (534, 534))
img.reshape(1,534,534)
label.reshape(1,534,534)
if label.max() > 1:
label = label / 255.0
flipCode = random.choice([-1,0,1,2])
if flipCode != 2:
img = cv2.flip(img, flipCode)
label = cv2.flip(label, flipCode)
# img = np.array(Image.open(file_name), dtype='float32') /255.0
# pad_width = []
# next = False
# # print(img.shape)
# for origin_dim,target_dim in zip(img.shape,target_size):
# if origin_dim < target_dim:
# row_pad = int(0.5*(target_dim-origin_dim))
# col_pad = int(0.5*(target_dim-origin_dim))
# if(target_dim - origin_dim - row_pad - col_pad) ==1:
# print("pad over 1")
# col_pad += 1
# elif(target_dim - origin_dim - row_pad - col_pad) ==2:
# print("pad over 2")
# col_pad += 1
# row_pad += 1
# pad_width.append((row_pad,col_pad))
# elif origin_dim > target_dim:
# next = True
# else:
# pad_width.append((0,0))
# if not next:
# img = np.pad(img,pad_width=pad_width,mode='constant',constant_values=0)
# if img.shape[0] != 534 or img.shape[1] != 534:
# print(file_name)
# print(img.shape)
# raise ValueError('Image size is not 534x534')
# # image = cv2.imread(image_path)
# img = np.transpose(img,(2,0,1))
# img = img.reshape((1,)+img.shape)
# x_train.append(img)
# print(img.shape)
# # print(x_train)
# # x_train = np.append(x_train, img)
# for file in glob.glob('.\\dataset\\label\\*'):
# print(file)
# for file_name in glob.glob(file+'\\*'):
# img = np.array(Image.open(file_name), dtype='float32') /255.0
# pad_width = []
# next = False
# # print(img.shape)
# for origin_dim,target_dim in zip(img.shape,target_size_label):
# if origin_dim < target_dim:
# row_pad = int(0.5*(target_dim-origin_dim))
# col_pad = int(0.5*(target_dim-origin_dim))
# if(target_dim - origin_dim - row_pad - col_pad) ==1:
# print("pad over 1")
# col_pad += 1
# elif(target_dim - origin_dim - row_pad - col_pad) ==2:
# print("pad over 2")
# col_pad += 1
# row_pad += 1
# pad_width.append((row_pad,col_pad))
# elif origin_dim > target_dim:
# next = True
# else:
# pad_width.append((0,0))
# if not next:
# img = np.pad(img,pad_width=pad_width,mode='constant',constant_values=0)
# if img.shape[0] != 534 or img.shape[1] != 534:
# print(file_name)
# print(img.shape)
# raise ValueError('Image size is not 534x534')
# # img = np.transpose(img,(2,0,1))
# img = img.reshape((1,)+img.shape)
# img = img.reshape((1,)+img.shape)
# x_label.append(img)
# print(img.shape)
# np.random.seed(116)
# x_train = np.array(x_train)
# x_label = np.array(x_label)
# np.random.shuffle(x_train)
# print(x_train.shape)
# np.random.seed(116)
# np.random.shuffle(x_label)
# # print(x_label.shape)
np.save('dataset\\x_train.npy', x_train[:4800])
np.save('dataset\\x_label.npy', x_label[:4800])
np.save('dataset\\x_test.npy', x_train[4800:])
np.save('dataset\\x_test.npy', x_label[4800:])
if __name__ == '__main__':
create_dataset()