-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset_process.py
665 lines (633 loc) · 30.7 KB
/
dataset_process.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
"""
dataset_process.py
process dataset
20181108
"""
import numpy as np
import os
from libtiff import TIFF
import shutil
from tqdm import tqdm
import cv2
def generate_train_validation_dataset_multisize(
source_path, validation_proportion):
"""
load multisize source images and generate train and validation datasets
:param source_path: the store path of source images
:param validation_proportion: the validation dataset proportion of total
dataset
:return: train_x, train_y, train_x_shape,
validation_x, validation_y, validation_x_shape,
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
number_of_categories = 0
for category in os.scandir(source_path):
if category.is_dir():
number_of_categories += 1
number_of_image_per_category = np.zeros(number_of_categories, dtype=int)
category_name = []
dataset_x = []
dataset_x_shap = []
index_category = 0
for category in os.scandir(source_path):
if category.is_dir():
index_category += 1
number_of_images = 0
category_name.append(category.name)
for img_file in os.scandir(category.path):
extension = os.path.splitext(img_file.path)[1][1:]
if extension == 'tif':
number_of_images += 1
tif = TIFF.open(img_file.path, mode='r')
image = tif.read_image()
dataset_x_shap.append([image.shape[0], image.shape[1]])
dataset_x.append(np.reshape(
np.sqrt(np.power(image[:, :, 0], 2) +
np.power(image[:, :, 1], 2)), (1, -1),
order='C'))
number_of_image_per_category[index_category-1] = number_of_images
# print(number_of_image_per_category)
dataset_y = np.zeros(
[sum(number_of_image_per_category), number_of_categories],
dtype=np.int32)
for index_category in range(number_of_categories):
dataset_y[sum(number_of_image_per_category[0:index_category]):
sum(number_of_image_per_category[0:index_category+1]),
index_category] = 1
# print(len(dataset_x))
number_total_images = len(dataset_x)
number_validation_images = int(np.floor(number_total_images *
validation_proportion))
validation_indices = np.random.choice(
number_total_images, number_validation_images, replace=False)
validation_x = []
validation_x_shape = []
for indices in validation_indices: # 'List' object, more complicated !!!
validation_x.append(dataset_x[indices])
validation_x_shape.append(dataset_x_shap[indices])
validation_y = dataset_y[validation_indices]
train_indices = np.array(
list(set(range(number_total_images)) - set(validation_indices)))
train_x = []
train_x_shape = []
for indices in train_indices:
train_x.append(dataset_x[indices])
train_x_shape.append(dataset_x_shap[indices])
train_y = dataset_y[train_indices]
return train_x, train_y, train_x_shape,\
validation_x, validation_y, validation_x_shape
def generate_train_validation_dataset_singlesize_with_category(
source_path, save_path, npz_file_name, validation_proportion,
file_extension='png', include_category='all', not_include_category=None, max_train_size=None,
max_validation_size=None, is_save_npz=True):
"""
load single size source images and generate train and validation datasets
(the struct of source path must be :source_path/category_name/image files)
and save them in a .npz file, with variable named 'train_x', 'train_y',
'validation_x', 'validation_y'
warning: For the params 'include_category' and 'not_include_category', only one of them can be used in one call.
:param source_path: the store path of source images
:param save_path: the path to save .npz file
:param validation_proportion: the validation dataset proportion of total
dataset
:param file_extension: the image files' extension, default to 'png', also support 'npy'
:param include_category: the category or the sub folder's name to be used, default to 'all', which means all
categories will be used. To chose specific categories, use ['ca_1', 'ca_2'] etc.
:param not_include_category: the category or the sub folder's name to be used, default to None, which means all
categories will be used. To chose specific categories, use ['ca_1', 'ca_2'] etc.
:param max_train_size: the max number of samples in each category of train set, default to None, means infinity.
:param max_validation_size: the max number of samples in each category of train set, default to None, infinity.
:param is_save_npz: if to save npz file, default to True.
:return: train_x, train_y, validation_x, validation_y, category_name
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
number_of_categories = 0
for category in os.scandir(source_path):
if category.is_dir():
number_of_categories += 1
if include_category == 'all' and not_include_category is None:
pass
else:
if include_category != 'all':
number_of_categories = len(include_category)
else:
number_of_categories = number_of_categories - len(not_include_category)
number_of_image_per_category = np.zeros(number_of_categories, dtype=int)
category_name = []
index_category = 0
is_first = True
is_first_total = True
for category in os.scandir(source_path):
if category.is_dir():
number_of_images = 0
if include_category != 'all' or not_include_category is not None:
if include_category != 'all':
if category.name not in include_category:
continue
else:
if category.name in not_include_category:
continue
index_category += 1
category_name.append(category.name)
else:
index_category += 1
category_name.append(category.name)
for img_file in os.scandir(category.path):
extension = os.path.splitext(img_file.path)[1][1:]
if extension == file_extension:
number_of_images += 1
if extension == 'npy':
temp_img = np.load(img_file.path)
else:
temp_img = cv2.imread(img_file.path, -1)
if is_first:
dataset_x = np.reshape(temp_img, [1, -1])
is_first = False
else:
dataset_x = np.append(dataset_x,
np.reshape(temp_img, [1, -1]),
axis=0)
is_first = True
dataset_y = np.zeros([number_of_images, number_of_categories],
dtype=np.int32)
dataset_y[:, index_category-1] = 1
number_of_image_per_category[index_category-1] = number_of_images
this_validation_size = int(np.floor(number_of_images *
validation_proportion))
this_validation_indices = np.random.choice(
number_of_images, this_validation_size, replace=False)
this_train_indices = np.array(
list(set(range(number_of_images)) -
set(this_validation_indices)))
if max_train_size is not None:
if this_train_indices.size > max_train_size:
this_train_indices = this_train_indices[:max_train_size]
if max_validation_size is not None:
if this_validation_indices.size >= max_validation_size:
this_validation_indices = this_validation_indices[:max_validation_size]
if is_first_total:
train_x = dataset_x[this_train_indices]
train_y = dataset_y[this_train_indices]
validation_x = dataset_x[this_validation_indices]
validation_y = dataset_y[this_validation_indices]
is_first_total = False
else:
train_x = np.append(train_x, dataset_x[this_train_indices],
axis=0)
train_y = np.append(train_y, dataset_y[this_train_indices],
axis=0)
validation_x = np.append(validation_x,
dataset_x[this_validation_indices],
axis=0)
validation_y = np.append(validation_y,
dataset_y[this_validation_indices],
axis=0)
# print(number_of_image_per_category)
if is_save_npz:
np.savez(os.path.join(save_path, npz_file_name), train_x=train_x,
train_y=train_y, validation_x=validation_x,
validation_y=validation_y)
return train_x, train_y, validation_x, validation_y, category_name
def generate_dataset_singlesize_with_category(
source_path, save_path, npz_file_name, file_extension='png', include_category='all', is_save_npz=True):
"""
load single size source images and generate datasets
(the struct of source path must be :source_path/category_name/image files)
and save them in a .npz file, with variable named 'x', 'y'
:param source_path: the store path of source images
:param save_path: the path to save .npz file
:param npz_file_name: str, the npz file name to be saved, end with '.npz'
:param file_extension: the image files' extension, default to 'png', also support 'npy'
:param include_category: the category or the sub folder's name to be used, default to 'all', which means all
categories will be used. To chose specific categories, use ['ca_1', 'ca_2'] etc.
:param is_save_npz: if to save npz file, default to True.
:return: dataset_x, dataset_y, category_name
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
number_of_categories = 0
if include_category == 'all':
for category in os.scandir(source_path):
if category.is_dir():
number_of_categories += 1
else:
number_of_categories = len(include_category)
number_of_image_per_category = np.zeros(number_of_categories, dtype=int)
category_name = []
dataset_x = []
dataset_y = []
index_category = 0
is_first_total = True
for category in os.scandir(source_path):
if category.is_dir():
if 'all' != include_category:
if category.name not in include_category:
continue
index_category += 1
number_of_images = 0
category_name.append(category.name)
is_first = True
for img_file in os.scandir(category.path):
extension = os.path.splitext(img_file.path)[1][1:]
print(f'processing {img_file.path}')
if extension == file_extension:
number_of_images += 1
if extension == 'npy':
temp_img = np.load(img_file.path)
else:
temp_img = cv2.imread(img_file.path, -1)
if is_first:
this_dataset_x = np.reshape(temp_img, [1, -1])
is_first = False
else:
this_dataset_x = np.append(this_dataset_x,
np.reshape(temp_img, [1, -1]),
axis=0)
if number_of_images > 0:
this_dataset_y = np.zeros([number_of_images, number_of_categories],
dtype=np.int32)
this_dataset_y[:, index_category-1] = 1
number_of_image_per_category[index_category-1] = number_of_images
if is_first_total:
dataset_x = this_dataset_x
dataset_y = this_dataset_y
is_first_total = False
else:
dataset_x = np.append(dataset_x, this_dataset_x, axis=0)
dataset_y = np.append(dataset_y, this_dataset_y, axis=0)
if is_save_npz:
np.savez(os.path.join(save_path, npz_file_name), x=dataset_x, y=dataset_y)
return dataset_x, dataset_y, category_name
def generate_dataset_singlesize_no_label(
source_path, save_path, npy_file_name, file_extension='png'):
"""
load single size source images and generate datasets
(the struct of source path must be :source_pathimage files)
and save them in a .npy file
:param source_path: the store path of source images
:param save_path: the path to save .npy file
:param file_extension: the image files' extension, default to 'png', also support 'npy'
:return: dataset_x
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
dataset_x = []
is_first = True
pbar = tqdm(os.scandir(source_path))
for img_file in pbar:
extension = os.path.splitext(img_file.path)[1][1:]
pbar.set_description("Processing %s" % img_file.path)
if extension == file_extension:
if extension == 'npy':
temp_img = np.load(img_file.path)
else:
temp_img = cv2.imread(img_file.path, -1)
if is_first:
dataset_x = np.reshape(temp_img, [1, -1])
is_first = False
else:
dataset_x = np.append(dataset_x, np.reshape(temp_img, [1, -1]), axis=0)
if npy_file_name is not None:
os.makedirs(save_path, exist_ok=True)
np.save(os.path.join(save_path, npy_file_name), dataset_x)
return dataset_x
def generate_train_test_validation_dataset_multisize_with_category(
source_path, test_proportion, validation_proportion):
"""
load source images and generate train, validation and test datasets
:param source_path: the store path of source images
:param test_proportion: the test dataset proportion of total dataset
:param validation_proportion: the validation dataset proportion of total
dataset
:return: train_x, train_y, train_x_shape,
validation_x, validation_y, validation_x_shape,
test_x, test_y, test_x_shape
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
number_of_categories = 0
for category in os.scandir(source_path):
if category.is_dir():
number_of_categories += 1
number_of_image_per_category = np.zeros(number_of_categories, dtype=int)
category_name = []
dataset_x = []
dataset_x_shap = []
index_category = 0
for category in os.scandir(source_path):
if category.is_dir():
index_category += 1
number_of_images = 0
category_name.append(category.name)
for img_file in os.scandir(category.path):
extension = os.path.splitext(img_file.path)[1][1:]
if extension == 'tif':
number_of_images += 1
tif = TIFF.open(img_file.path, mode='r')
image = tif.read_image()
dataset_x_shap.append([image.shape[0], image.shape[1]])
dataset_x.append(np.reshape(
np.sqrt(np.power(image[:, :, 0], 2) +
np.power(image[:, :, 1], 2)), (1, -1),
order='C'))
number_of_image_per_category[index_category-1] = number_of_images
# print(number_of_image_per_category)
dataset_y = np.zeros(
[sum(number_of_image_per_category), number_of_categories],
dtype=np.int32)
for index_category in range(number_of_categories):
dataset_y[sum(number_of_image_per_category[0:index_category]):
sum(number_of_image_per_category[0:index_category+1]),
index_category] = 1
# print(len(dataset_x))
number_total_images = len(dataset_x)
number_test_images = int(np.floor(number_total_images * test_proportion))
number_validation_images = int(np.floor(number_total_images *
validation_proportion))
test_indices = np.random.choice(
number_total_images, number_test_images, replace=False) # 不放回抽样
test_x = []
test_x_shape = []
for indices in test_indices: # 'List' object, more complicated !!!
test_x.append(dataset_x[indices])
test_x_shape.append(dataset_x_shap[indices])
test_y = dataset_y[test_indices]
train_validation_indices = np.array(
list(set(range(number_total_images)) - set(test_indices)))
validation_indices = train_validation_indices[np.random.choice(
number_total_images-number_test_images, number_validation_images,
replace=False)]
train_indices = list(set(train_validation_indices) -
set(validation_indices))
train_x = []
train_x_shape = []
for indices in train_indices:
train_x.append(dataset_x[indices])
train_x_shape.append(dataset_x_shap[indices])
train_y = dataset_y[train_indices]
validation_x = []
validation_x_shape = []
for indices in validation_indices:
validation_x.append(dataset_x[indices])
validation_x_shape.append(dataset_x_shap[indices])
validation_y = dataset_y[validation_indices]
return train_x, train_y, train_x_shape,\
validation_x, validation_y, validation_x_shape, \
test_x, test_y, test_x_shape
def _random_choose_fix_proportion_train_test_files(
source_path, train_save_path, test_save_path, train_files_proportion, pick_extension='all'):
"""
random pick fix proportion of files from source path and copy it to train
folder, the rest copy to test folder
:param source_path: the source images' store folder, struct should be source_path/image_files
:param train_save_path: the folder to store picked train image files
:param test_save_path: the folder to store picked test image files
:param train_files_proportion: the train files' proportion to be picked
:param pick_extension: the extension of files which will be picked, default to 'all' means this function will work
among all the files.
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
os.makedirs(train_save_path, exist_ok=True)
os.makedirs(test_save_path, exist_ok=True)
file_name_list = os.listdir(source_path)
if pick_extension != 'all':
for file_name in file_name_list:
if file_name.split('.')[-1] != pick_extension:
file_name_list.remove(file_name)
total_files_number = len(file_name_list)
train_files_number = int(
np.ceil(total_files_number * train_files_proportion))
train_files_indices = np.random.choice(
total_files_number, train_files_number, replace=False)
test_files_indices = np.array(
list(set(range(total_files_number)) - set(train_files_indices)))
for file_index in train_files_indices:
shutil.copyfile(os.path.join(source_path, file_name_list[file_index]),
os.path.join(train_save_path,
file_name_list[file_index]))
for file_index in test_files_indices:
shutil.copyfile(os.path.join(source_path, file_name_list[file_index]),
os.path.join(test_save_path,
file_name_list[file_index]))
return True
def split_dataset_to_train_test_with_category(
source_path, save_path, train_file_proportion=0.5, pick_extension='all'):
"""
split the total dataset to two part: train and test
:param source_path: dataset path, struct should be source_path/category/image_files
:param save_path: train and test folders' root path
:param train_file_proportion: train proportion of total dataset
:param pick_extension: the extension of files which will be picked, default to 'all' means this function will work
among all the files.
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
pbar = tqdm(os.scandir(source_path))
for sub_folder in pbar:
if sub_folder.is_dir():
pbar.set_description("Processing %s" % sub_folder.name)
train_save_path = os.path.join(save_path, 'train', sub_folder.name)
test_save_path = os.path.join(save_path, 'test', sub_folder.name)
_random_choose_fix_proportion_train_test_files(
sub_folder.path, train_save_path, test_save_path,
train_file_proportion, pick_extension=pick_extension)
return True
def random_choose_fix_number_image_files_with_category(source_path, save_path, pick_file_number, pick_extension='all'):
"""
random pick fix number of files from source path and copy them to save_path/category/images
:param source_path: the source images' store folder, struct should be source_path/category/image_files
:param save_path: the folder to store picked image files
:param pick_file_number: the number of image files to pick
:param pick_extension: the extension of files which will be picked, default to 'all' means this function will work
among all the files.
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
this_pbar = tqdm(os.scandir(source_path))
for category in this_pbar:
if category.is_dir():
this_pbar.set_description("Processing %s" % category.name)
os.makedirs(os.path.join(save_path, category.name), exist_ok=True)
file_name_list = os.listdir(os.path.join(source_path, category.name))
if pick_extension != 'all':
for file_name in file_name_list:
if file_name.split('.')[-1] != pick_extension:
file_name_list.remove(file_name)
total_files_number = len(file_name_list)
pick_files_indices = np.random.choice(total_files_number, pick_file_number, replace=False)
for file_index in pick_files_indices:
shutil.copyfile(os.path.join(source_path, category.name, file_name_list[file_index]),
os.path.join(save_path, category.name, file_name_list[file_index]))
return True
def random_choose_fix_number_image_files(source_path, save_path, pick_file_number, pick_extension='all'):
"""
random pick fix number of files from source path and copy them to save_path/images
:param source_path: the source images' store folder, struct should be source_path/image_files
:param save_path: the folder to store picked image files
:param pick_file_number: the number of image files to pick
:param pick_extension: the extension of files which will be picked, default to 'all' means this function will work
among all the files.
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
os.makedirs(save_path, exist_ok=True)
file_name_list = os.listdir(source_path)
if pick_extension != 'all':
for file_name in file_name_list:
if file_name.split('.')[-1] != pick_extension:
file_name_list.remove(file_name)
total_files_number = len(file_name_list)
pick_files_indices = np.random.choice(total_files_number, pick_file_number, replace=False)
for file_index in pick_files_indices:
shutil.copyfile(os.path.join(source_path, file_name_list[file_index]),
os.path.join(save_path, file_name_list[file_index]))
return True
def data_norm_zscore(src_data):
"""
normalization function : norm = (true - mean) / std
only can be used to 2D matrix, each row represents a data point, such as an image
"""
if not len(src_data.shape) == 2:
raise ValueError("normalization only support 2D data, but get %d data" % len(src_data.shape))
norm_data = np.zeros(src_data.shape, dtype=np.float32)
for ind_r in tqdm(range(src_data.shape[0])):
this_mean = np.mean(src_data[ind_r])
this_std = np.std(src_data[ind_r])
# print(this_mean)
norm_data[ind_r] = (src_data[ind_r]*1.0 - this_mean) / this_std
# print("normalization data size : %d × %d" % (norm_data.shape[0], norm_data.shape[1]))
return norm_data
def data_norm_minmax(src_data):
"""
normalization function : norm = (true - min) / (max - min)
only can be used to 2D matrix, each row represents a data point, such as an image
"""
if not len(src_data.shape) == 2:
raise ValueError("normalization only support 2D data, but get %d data" % len(src_data.shape))
norm_data = np.zeros(src_data.shape, dtype=np.float32)
for ind_r in tqdm(range(src_data.shape[0])):
this_min = np.min(src_data[ind_r])
this_max = np.max(src_data[ind_r])
# print(this_mean)
norm_data[ind_r] = (src_data[ind_r]*1.0 - this_min) / (this_max - this_min)
# print("normalization data size : %d × %d" % (norm_data.shape[0], norm_data.shape[1]))
return norm_data
def copy_npy_files_with_category(source_path, save_path):
"""
copy npy files from source path to save path
:param source_path: struct: source_path/category/npy files
:param save_path: save path
:return: True
"""
if not os.path.exists(source_path):
raise FileExistsError('path not found! : %s' % source_path)
for category in os.scandir(source_path):
if category.is_dir():
pbar = tqdm(os.scandir(category.path))
for npy_files in pbar:
if npy_files.is_file():
extension = os.path.splitext(npy_files.path)[1][1:]
if extension == 'npy':
pbar.set_description("Processing %s" % npy_files.name)
os.makedirs(os.path.join(save_path, category.name), exist_ok=True)
shutil.copyfile(npy_files.path,
os.path.join(save_path, category.name, npy_files.name))
return True
# multisize
def random_perm3(data_x, data_y, data_z):
"""
do random permutation on x, y and z, x and z are list object, y is ndarray object
:param data_x: x data, list object
:param data_y: label data, ndarray object
:param data_z: x data size, list object
"""
data_size = data_y.shape[0]
rand_perm = np.arange(data_size)
np.random.shuffle(rand_perm)
random_data_x = []
random_data_z = []
for indices in rand_perm: # 'List' object, more complicated !!!
random_data_x.append(data_x[indices])
random_data_z.append(data_z[indices])
# random_data_x = data_x[rand_perm]
random_data_y = data_y[rand_perm]
# random_data_z = data_z[rand_perm]
return random_data_x, random_data_y, random_data_z
def generate_dataset_multisize_with_cagtegory(source_path):
"""
load multisize source images and generate datasets
:param source_path: the store path of source images, source_path/category/image files
:return: x, y, x_shape,
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
number_of_categories = 0
for category in os.scandir(source_path):
if category.is_dir():
number_of_categories += 1
number_of_image_per_category = np.zeros(number_of_categories, dtype=np.int32)
category_name = []
dataset_x = []
dataset_x_shape = []
index_category = 0
for category in os.scandir(source_path):
if category.is_dir():
index_category += 1
number_of_images = 0
category_name.append(category.name)
for img_file in os.scandir(category.path):
extension = os.path.splitext(img_file.path)[1][1:]
if extension == 'tif':
number_of_images += 1
tif = TIFF.open(img_file.path, mode='r')
image = tif.read_image()
dataset_x_shape.append([image.shape[0], image.shape[1]])
this_x = np.reshape(np.sqrt(np.power(image[:, :, 0], 2) + np.power(image[:, :, 1], 2)), (1, -1),
order='C')
this_x_norml2 = (this_x * 1.0) / np.sqrt(np.sum(np.square(this_x)))
dataset_x.append(this_x_norml2)
number_of_image_per_category[index_category-1] = number_of_images
# print(number_of_image_per_category)
dataset_y = np.zeros(
[sum(number_of_image_per_category), number_of_categories],
dtype=np.int32)
for index_category in range(number_of_categories):
dataset_y[sum(number_of_image_per_category[0:index_category]):
sum(number_of_image_per_category[0:index_category+1]),
index_category] = 1
# print(len(dataset_x))
return dataset_x, dataset_y, dataset_x_shape
def generate_dataset_multisize(source_path, number_category, this_category_index):
"""
load multisize source images and generate datasets
:param source_path: the store path of source images, source_path/image files
:param number_category: for generating one hot label y
:param this_category_index: for generating one hot label y
:return: x, y, x_shape,
"""
if not os.path.exists(source_path):
raise FileExistsError('file not found! : %s' % source_path)
dataset_x = []
dataset_x_shape = []
for img_file in os.scandir(source_path):
extension = os.path.splitext(img_file.path)[1][1:]
if extension == 'tif':
tif = TIFF.open(img_file.path, mode='r')
image = tif.read_image()
dataset_x_shape.append([image.shape[0], image.shape[1]])
this_x = np.reshape(np.sqrt(np.power(image[:, :, 0], 2) + np.power(image[:, :, 1], 2)), (1, -1),
order='C')
this_x_norml2 = (this_x * 1.0) / np.sqrt(np.sum(np.square(this_x))) # norm with L2
dataset_x.append(this_x_norml2)
# print(number_of_image_per_category)
dataset_y = np.zeros([len(dataset_x), number_category], dtype=np.int32)
dataset_y[:, this_category_index] = 1
# print(len(dataset_x))
return dataset_x, dataset_y, dataset_x_shape