-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_dataset.py
More file actions
executable file
·196 lines (171 loc) · 7.29 KB
/
split_dataset.py
File metadata and controls
executable file
·196 lines (171 loc) · 7.29 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
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
#!/usr/bin/env python3
# Copyright (c) 2019-2023
# Shell-ML Project
# Pedro Ribeiro Mendes Júnior <pedrormjunior@gmail.com> et al.
# Artificial Intelligence Lab. Recod.ai
# Institute of Computing (IC)
# University of Campinas (Unicamp)
# Campinas, São Paulo, Brazil
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import rocklib
import rocklibdata
import torchvision
import numpy as np
import random
import shelve
import copy
import envconfig
VERSIONS = [1, 2, 3]
CLASSES = [3, 5, 6, 1, 11, 9, 10]
THRESHOLD_PIXELS: int = 1700
QUANT_PARTITIONS: int = 16
PERC_TRAIN = 0.6
PERC_TEST = 0.24 # VAL: 0.16
envconfig._makedir(envconfig.records_dir)
PARTITIONS_FILENAME = envconfig.partitions_filename
if __name__ == '__main__':
random.seed(0.5699905559313857)
datasets = {
version: torchvision.datasets.ImageFolder(folder)
for version, folder in rocklib.get_dataset_folders(VERSIONS).items()
}
filenames_employed = []
keys_employed = []
counters = {
'threshold': 0,
'label': 0,
'ignored': 0,
}
for idx in datasets:
# logger.debug(f'Dataset {idx}')
for filename, data in (
zip(datasets[idx].imgs, datasets[idx])
):
filename, data = filename[0], np.array(data[0])
if filename.endswith('_mask.png'):
continue
if sum(data.shape[:2]) < THRESHOLD_PIXELS:
rocklib.logger.debug(f'THRESHOLD: Image {filename} '
f'{data.shape} is too small')
counters['threshold'] += 1
continue
key = rocklib.filename_to_key(filename)
_, label, _ = key
if label not in CLASSES:
rocklib.logger.debug(f'LABEL: Image {filename} has non valid '
f'label {label}')
counters['label'] += 1
continue
# 20221010
if filename in rocklibdata.ignored_filepaths:
# Those are the images that get accepted by the size criterion
# checked above, however, those images are of broken cores.
# Based on the segmentation mask for those images, no patch can
# be extracted for them which make them useless for
# experimentation. So they are being ignored.
rocklib.logger.debug(f'IGNORED: Image {filename} is ignored '
'due to its mask')
counters['ignored'] += 1
continue
keys_employed.append(key)
filenames_employed.append(filename)
rocklib.logger.debug(f'Not used: {counters}')
rocklib.logger.debug(f'keys_employed: {len(keys_employed)}')
rocklib.logger.debug(f'filenames_employed: {len(filenames_employed)}')
def generate_partition():
keys = copy.deepcopy(keys_employed)
keys = list(set(keys))
random.shuffle(keys)
quant_train = int(PERC_TRAIN * len(keys))
quant_test = int(PERC_TEST * len(keys))
train = keys[:quant_train]
test = keys[quant_train:quant_train+quant_test]
val = keys[quant_train+quant_test:]
assert len(train) + len(test) + len(val) == len(keys), \
(len(train), len(test), len(val), len(keys))
assert len(set(train + test + val)) == len(keys)
train_labels = [label for _, label, _ in train]
test_labels = [label for _, label, _ in test]
val_labels = [label for _, label, _ in val]
def print_labels(string, labels):
lst = [labels.count(label) for label in CLASSES]
print(f'{string} {lst} '
f'(min={min(lst)}) '
f'(sum={sum(lst)}) ')
print('Quant core plugs:')
print_labels('train', train_labels)
print_labels('test', test_labels)
print_labels('val', val_labels)
return {**{x: 'train' for x in train},
**{x: 'test' for x in test},
**{x: 'val' for x in val}}
partitions = shelve.open(PARTITIONS_FILENAME, flag='n', writeback=True)
partitions['filenames'] = filenames_employed
partitions['partitions'] = {}
for partition_num in range(QUANT_PARTITIONS):
# print(str(partition_num))
partition = generate_partition()
partitions['partitions'][partition_num] = {}
filenames_count = {part: {label: 0 for label in CLASSES}
for part in ['train', 'test', 'val']}
for filename in filenames_employed:
key = rocklib.filename_to_key(filename)
part = partition[key]
partitions['partitions'][partition_num][key] = part
_, label, _ = key
filenames_count[part][label] += 1
del key, part, label, filename
filenames_count = {part: [filenames_count[part][label]
for label in CLASSES]
for part in filenames_count}
print('Quant images:')
for part in filenames_count:
print(f'{part} {filenames_count[part]} '
f'(min={min(filenames_count[part])}) '
f'(sum={sum(filenames_count[part])})')
print()
partitions.close()
# logger.info(f'Shelve file of partitions: {PARTITIONS_FILENAME}')
partitions = shelve.open(PARTITIONS_FILENAME)
dic = {}
for partition_num in partitions['partitions']:
for filename in partitions['filenames']:
if filename not in dic:
dic[filename] = []
key = rocklib.filename_to_key(filename)
dic[filename].append(partitions['partitions'][partition_num][key])
partitions.close()
for filename in dic:
set_parts = set(dic[filename])
if len(set_parts) == 1:
print(filename)
print(set_parts)
else:
assert os.path.basename(sys.argv[0]) == os.path.basename(__file__) or \
os.path.exists(PARTITIONS_FILENAME + '.db'), \
(f"Before importing this module ({__file__}), you need to first "
f"generate the file {PARTITIONS_FILENAME + '.db'}; run `make "
"split_dataset` for this purpose.")
if os.path.exists(PARTITIONS_FILENAME + '.db'):
fd = shelve.open(PARTITIONS_FILENAME)
partitions = dict(fd)
fd.close()