-
Notifications
You must be signed in to change notification settings - Fork 3
/
fish_dataset.py
222 lines (194 loc) · 9.25 KB
/
fish_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
222
import numpy as np
from PIL import Image,ImageOps
from io import BytesIO
def load_dataset(dataDir='/data1/train_data/', data_range=range(0,300),test=False, dark=10,exclude = False):
print("load dataset start")
print(" from: %s"%dataDir)
imgDataset = []
nightDataset = []
sonarDataset = []
if exclude:
# trainingに使えないデータ(エイがカメラの前を通った場面など)を除去
excludes = np.concatenate([np.arange(226,253), np.arange(445,455), np.arange(796, 803), np.arange(2100,2117),
np.arange(2267, 2317), np.arange(2764, 2835), np.arange(3009, 3029), np.arange(3176, 3230),
np.arange(3467, 3490), np.arange(3665, 3735), np.arange(3927, 4001), np.arange(4306,4308),
np.arange(4416, 4476), np.arange(4737, 4741), np.arange(4846, 4906), np.arange(5406, 5464),
np.arange(5807, 5841), np.arange(6101, 6145)]) # training対象外
mask = [d not in excludes for d in data_range]
data_range = data_range[mask]
imgStart = 0
sonarStart = 0
nightStart = 0
for i in data_range:
if test:
if i%3 != 1:
continue
if not test:
if i%3 != 0:
continue
imgNum = imgStart + i
sonarNum = sonarStart + i
nightNum = nightStart + i
img = Image.open(dataDir + "up/up%05d.png"%imgNum)
night = Image.open(dataDir + "night_100/" +"up_" + str(dark).replace('.','') + "night/night_up%05d.png"%nightNum)
sonar = Image.open(dataDir + "sonar/sonar%05d.png"%sonarNum)
sonar = sonar.convert("L")
# 短い辺が300pixになるようにresizeし、rgbを(-1,1)に正規化
w,h = img.size
r = 300/min(w,h)
img = img.resize((int(r*w), int(r*h)), Image.BILINEAR)
night = night.resize((int(r*w), int(r*h)),Image.BILINEAR)
sonar = sonar.resize((int(r*w), int(r*h)),Image.BILINEAR)
img = np.asarray(img)/128.0-1.0
sonar = (np.asarray(sonar)/128.0-1.0)[:,:,np.newaxis]
night = np.asarray(night)/128.0-1.0
#512 * 256にランダムクリップ → ランダムを廃止
h,w,_ = img.shape
xl = int((w-256)/2)
yl = int(h-512)
# xl = np.random.randint(0,w-256)
# yl = np.random.randint(0,h-512)
img = img[yl:yl+512, xl:xl+256, :]
sonar = sonar[yl:yl+512, xl:xl+256,:]
night = night[yl:yl+512, xl:xl+256,:]
imgDataset.append(img)
sonarDataset.append(sonar)
nightDataset.append(night)
print("load dataset done")
return np.array(imgDataset),np.array(sonarDataset),np.array(nightDataset)
def load_dataset_box(dataDir='/data1/train_data/', data_range=range(0,300),test=False, dark=10):
print("load dataset start")
print(" from: %s"%dataDir)
imgDataset = []
nightDataset = []
sonarDataset = []
# trainingに使えないデータ(エイがカメラの前を通った場面など)を除去
excludes = np.concatenate([np.arange(226,253), np.arange(445,455), np.arange(796, 803), np.arange(2100,2117),
np.arange(2267, 2317), np.arange(2764, 2835), np.arange(3009, 3029), np.arange(3176, 3230),
np.arange(3467, 3490), np.arange(3665, 3735), np.arange(3927, 4001), np.arange(4306,4308),
np.arange(4416, 4476), np.arange(4737, 4741), np.arange(4846, 4906), np.arange(5406, 5464),
np.arange(5807, 5841), np.arange(6101, 6145)]) # training対象外
mask = [d not in excludes for d in data_range]
data_range = data_range[mask]
imgStart = 0
sonarStart = 0
nightStart = 0
for i in data_range:
if test:
if i%3 != 1:
continue
if not test:
if i%3 != 0:
continue
imgNum = imgStart + i
sonarNum = sonarStart + i
nightNum = nightStart + i
img = Image.open(dataDir + "up/up%05d.png"%imgNum)
sonar = Image.open(dataDir + "sonar/sonar%05d.png"%sonarNum)
sonar = sonar.convert("L")
# 短い辺が300pixになるようにresizeし、rgbを(-1,1)に正規化
w,h = img.size
r = 300/min(w,h)
img = img.resize((int(r*w), int(r*h)), Image.BILINEAR)
sonar = sonar.resize((int(r*w), int(r*h)),Image.BILINEAR)
img = np.asarray(img)/128.0-1.0
sonar = (np.asarray(sonar)/128.0-1.0)[:,:,np.newaxis]
# 512 * 256にランダムクリップ
h,w,_ = img.shape
if test:
xl = int(w-256)
yl = int(h-512)
else:
xl = np.random.randint(0,w-256)
yl = np.random.randint(0,h-512)
img = img[yl:yl+512, xl:xl+256, :]
sonar = sonar[yl:yl+512, xl:xl+256,:]
imgDataset.append(img)
sonarDataset.append(sonar)
box_img = np.copy(img)
box_size = 150
xl = np.random.randint(0,256 - box_size)
yl = np.random.randint(0,512 - box_size)
box_img[yl:yl+box_size,xl:xl+box_size] = -1
nightDataset.append(box_img)
print("load dataset done")
return np.array(imgDataset),np.array(sonarDataset),np.array(nightDataset)
def load_dataset_data_augument(dataDir='/data1/train_data/', data_range=range(0,300),test=False, dark=10):
print("load dataset start")
print(" from: %s"%dataDir)
imgDataset = []
nightDataset = []
sonarDataset = []
# trainingに使えないデータ(エイがカメラの前を通った場面など)を除去
excludes = np.concatenate([np.arange(226,253), np.arange(445,455), np.arange(796, 803), np.arange(2100,2117),
np.arange(2267, 2317), np.arange(2764, 2835), np.arange(3009, 3029), np.arange(3176, 3230),
np.arange(3467, 3490), np.arange(3665, 3735), np.arange(3927, 4001), np.arange(4306,4308),
np.arange(4416, 4476), np.arange(4737, 4741), np.arange(4846, 4906), np.arange(5406, 5464),
np.arange(5807, 5841), np.arange(6101, 6145)]) # training対象外
mask = [d not in excludes for d in data_range]
data_range = data_range[mask]
imgStart = 0
sonarStart = 0
nightStart = 0
for i in data_range:
if test:
if i%3 != 1:
continue
if not test:
if i%3 != 0:
continue
imgNum = imgStart + i
sonarNum = sonarStart + i
nightNum = nightStart + i
img = Image.open(dataDir + "up/up%05d.png"%imgNum)
night = Image.open(dataDir + "night_100/" +"up_" + str(dark).replace('.','') + "night/night_up%05d.png"%nightNum)
sonar = Image.open(dataDir + "sonar/sonar%05d.png"%sonarNum)
sonar = sonar.convert("L")
# 短い辺が300pixになるようにresizeし、rgbを(-1,1)に正規化
# データを対称変換したaugmentation dataを追加
w,h = img.size
r = 300/min(w,h)
img = img.resize((int(r*w), int(r*h)), Image.BILINEAR)
night = night.resize((int(r*w), int(r*h)),Image.BILINEAR)
sonar = sonar.resize((int(r*w), int(r*h)),Image.BILINEAR)
aug_img = augument(img)
aug_night = augument(night)
aug_sonar = augument(sonar)
img = np.asarray(img)/128.0-1.0
sonar = (np.asarray(sonar)/128.0-1.0)[:,:,np.newaxis]
night = np.asarray(night)/128.0-1.0
aug_img = np.asarray(aug_img)/128.0-1.0
aug_sonar = (np.asarray(aug_sonar)/128.0-1.0)[:,:,np.newaxis]
aug_night = np.asarray(aug_night)/128.0-1.0
# 512 * 256にランダムクリップ
h,w,_ = img.shape
if test:
xl = int(w-256)
yl = int(h-512)
else:
xl = np.random.randint(0,w-256)
yl = np.random.randint(0,h-512)
# img = img[yl:yl+512, xl:xl+256, :]
# sonar = sonar[yl:yl+512, xl:xl+256,:]
# night = night[yl:yl+512, xl:xl+256,:]
aug_img = aug_img[yl:yl+512, xl:xl+256, :]
aug_sonar = aug_sonar[yl:yl+512, xl:xl+256,:]
aug_night = aug_night[yl:yl+512, xl:xl+256,:]
# imgDataset.append(img)
imgDataset.append(aug_img)
# sonarDataset.append(sonar)
sonarDataset.append(aug_sonar)
# nightDataset.append(night)
nightDataset.append(aug_night)
print("load dataset done")
return np.array(imgDataset),np.array(sonarDataset),np.array(nightDataset)
def augument(img):
flag = np.random.choice([0,1,2,3])
if flag == 0:
return ImageOps.mirror(img)
if flag == 1:
return ImageOps.flip(img)
if flag == 2:
return ImageOps.flip(ImageOps.mirror(img))
if flag == 3:
return img